1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* pfilt.c - program to post-process picture file. |
9 |
* |
10 |
* 9/26/85 |
11 |
*/ |
12 |
|
13 |
#include <stdio.h> |
14 |
|
15 |
#include <signal.h> |
16 |
|
17 |
#include "color.h" |
18 |
|
19 |
|
20 |
extern char *malloc(); |
21 |
|
22 |
#define CHECKRAD 1.5 /* radius to check for filtering */ |
23 |
|
24 |
COLOR exposure = WHTCOLOR; /* exposure for the frame */ |
25 |
|
26 |
double rad = 0.0; /* output pixel radius for filtering */ |
27 |
|
28 |
int nrows = 0; /* number of rows for output */ |
29 |
int ncols = 0; /* number of columns for output */ |
30 |
|
31 |
double x_c = 1.0; /* ratio of output x size to input */ |
32 |
double y_r = 1.0; /* ratio of output y size to input */ |
33 |
|
34 |
int singlepass = 0; /* true means skip first pass */ |
35 |
|
36 |
int avghot = 0; /* true means average in bright spots */ |
37 |
|
38 |
double hotlvl = 1000.0; /* level considered "hot" */ |
39 |
|
40 |
int npts = 0; /* (half) number of points for stars */ |
41 |
|
42 |
double spread = 1e-4; /* spread for star points */ |
43 |
|
44 |
#define TEMPLATE "/usr/tmp/pfXXXXXX" |
45 |
|
46 |
char *tfname = NULL; |
47 |
|
48 |
int xres, yres; /* resolution of input */ |
49 |
|
50 |
int xrad; /* x window size */ |
51 |
int yrad; /* y window size */ |
52 |
|
53 |
int barsize; /* size of input scan bar */ |
54 |
COLOR **scanin; /* input scan bar */ |
55 |
COLOR *scanout; /* output scan line */ |
56 |
|
57 |
char *progname; |
58 |
|
59 |
|
60 |
main(argc, argv) |
61 |
int argc; |
62 |
char **argv; |
63 |
{ |
64 |
extern char *mktemp(); |
65 |
extern double atof(), pow(); |
66 |
extern long ftell(); |
67 |
extern int quit(); |
68 |
FILE *fin; |
69 |
long fpos; |
70 |
double d; |
71 |
int i; |
72 |
|
73 |
if (signal(SIGINT, quit) == SIG_IGN) |
74 |
signal(SIGINT, SIG_IGN); |
75 |
if (signal(SIGHUP, quit) == SIG_IGN) |
76 |
signal(SIGINT, SIG_IGN); |
77 |
signal(SIGTERM, quit); |
78 |
signal(SIGPIPE, quit); |
79 |
#ifdef SIGXCPU |
80 |
signal(SIGXCPU, quit); |
81 |
signal(SIGXFSZ, quit); |
82 |
#endif |
83 |
|
84 |
progname = argv[0]; |
85 |
|
86 |
for (i = 1; i < argc; i++) |
87 |
if (argv[i][0] == '-') |
88 |
switch (argv[i][1]) { |
89 |
case 'x': |
90 |
i++; |
91 |
if (argv[i][0] == '/') { |
92 |
x_c = 1.0/atof(argv[i]+1); |
93 |
ncols = 0; |
94 |
} else |
95 |
ncols = atoi(argv[i]); |
96 |
break; |
97 |
case 'y': |
98 |
i++; |
99 |
if (argv[i][0] == '/') { |
100 |
y_r = 1.0/atof(argv[i]+1); |
101 |
nrows = 0; |
102 |
} else |
103 |
nrows = atoi(argv[i]); |
104 |
break; |
105 |
case 'e': |
106 |
if (argv[i+1][0] == '+' || argv[i+1][0] == '-') |
107 |
d = pow(2.0, atof(argv[i+1])); |
108 |
else |
109 |
d = atof(argv[i+1]); |
110 |
switch (argv[i][2]) { |
111 |
case '\0': |
112 |
scalecolor(exposure, d); |
113 |
break; |
114 |
case 'r': |
115 |
colval(exposure,RED) *= d; |
116 |
break; |
117 |
case 'g': |
118 |
colval(exposure,GRN) *= d; |
119 |
break; |
120 |
case 'b': |
121 |
colval(exposure,BLU) *= d; |
122 |
break; |
123 |
default: |
124 |
goto badopt; |
125 |
} |
126 |
i++; |
127 |
break; |
128 |
case '1': |
129 |
singlepass = 1; |
130 |
break; |
131 |
case 'p': |
132 |
npts = atoi(argv[++i]) / 2; |
133 |
break; |
134 |
case 's': |
135 |
spread = atof(argv[++i]); |
136 |
break; |
137 |
case 'a': |
138 |
avghot = !avghot; |
139 |
break; |
140 |
case 'h': |
141 |
hotlvl = atof(argv[++i]); |
142 |
break; |
143 |
case 'r': |
144 |
rad = atof(argv[++i]); |
145 |
break; |
146 |
case 'b': |
147 |
rad = 0.0; |
148 |
break; |
149 |
default:; |
150 |
badopt: |
151 |
fprintf(stderr, "%s: unknown option: %s\n", |
152 |
progname, argv[i]); |
153 |
quit(1); |
154 |
break; |
155 |
} |
156 |
else |
157 |
break; |
158 |
|
159 |
if (i == argc) { |
160 |
if (singlepass) |
161 |
fin = stdin; |
162 |
else { |
163 |
tfname = mktemp(TEMPLATE); |
164 |
if ((fin = fopen(tfname, "w+")) == NULL) { |
165 |
fprintf(stderr, "%s: can't create ", progname); |
166 |
fprintf(stderr, "temp file \"%s\"\n", tfname); |
167 |
quit(1); |
168 |
} |
169 |
copyfile(stdin, fin); |
170 |
if (fseek(fin, 0L, 0) == -1) { |
171 |
fprintf(stderr, "%s: seek fail\n", progname); |
172 |
quit(1); |
173 |
} |
174 |
} |
175 |
} else if (i == argc-1) { |
176 |
if ((fin = fopen(argv[i], "r")) == NULL) { |
177 |
fprintf(stderr, "%s: can't open file \"%s\"\n", |
178 |
progname, argv[i]); |
179 |
quit(1); |
180 |
} |
181 |
} else { |
182 |
fprintf(stderr, "%s: bad # file arguments\n", progname); |
183 |
quit(1); |
184 |
} |
185 |
/* copy header */ |
186 |
copyheader(fin, stdout); |
187 |
/* add new header info. */ |
188 |
printargs(i, argv, stdout); |
189 |
/* get picture size */ |
190 |
if (fscanf(fin, "-Y %d +X %d\n", &yres, &xres) != 2) { |
191 |
fprintf(stderr, "%s: bad picture size\n", progname); |
192 |
quit(1); |
193 |
} |
194 |
if (ncols > 0) |
195 |
x_c = (double)ncols/xres; |
196 |
else |
197 |
ncols = x_c*xres + .5; |
198 |
if (nrows > 0) |
199 |
y_r = (double)nrows/yres; |
200 |
else |
201 |
nrows = y_r*yres + .5; |
202 |
|
203 |
if (singlepass) { |
204 |
/* skip exposure, etc. */ |
205 |
pass1default(); |
206 |
pass2(fin); |
207 |
quit(0); |
208 |
} |
209 |
|
210 |
fpos = ftell(fin); /* save input file position */ |
211 |
|
212 |
pass1(fin); |
213 |
|
214 |
if (fseek(fin, fpos, 0) == -1) { |
215 |
fprintf(stderr, "%s: seek fail\n", progname); |
216 |
quit(1); |
217 |
} |
218 |
pass2(fin); |
219 |
|
220 |
quit(0); |
221 |
} |
222 |
|
223 |
|
224 |
copyfile(in, out) /* copy a file */ |
225 |
register FILE *in, *out; |
226 |
{ |
227 |
register int c; |
228 |
|
229 |
while ((c = getc(in)) != EOF) |
230 |
putc(c, out); |
231 |
|
232 |
if (ferror(out)) { |
233 |
fprintf(stderr, "%s: write error in copyfile\n", progname); |
234 |
quit(1); |
235 |
} |
236 |
} |
237 |
|
238 |
|
239 |
pass1(in) /* first pass of picture file */ |
240 |
FILE *in; |
241 |
{ |
242 |
int i; |
243 |
COLOR *scan; |
244 |
|
245 |
pass1init(); |
246 |
|
247 |
scan = (COLOR *)malloc(xres*sizeof(COLOR)); |
248 |
if (scan == NULL) { |
249 |
fprintf(stderr, "%s: out of memory\n", progname); |
250 |
quit(1); |
251 |
} |
252 |
for (i = 0; i < yres; i++) { |
253 |
if (freadscan(scan, xres, in) < 0) { |
254 |
nrows = nrows * i / yres; /* adjust frame */ |
255 |
if (nrows <= 0) { |
256 |
fprintf(stderr, "%s: empty frame\n", progname); |
257 |
quit(1); |
258 |
} |
259 |
fprintf(stderr, "%s: warning - partial frame (%d%%)\n", |
260 |
progname, 100*i/yres); |
261 |
yres = i; |
262 |
break; |
263 |
} |
264 |
pass1scan(scan, i); |
265 |
} |
266 |
free((char *)scan); |
267 |
} |
268 |
|
269 |
|
270 |
pass2(in) /* last pass on file, write to stdout */ |
271 |
FILE *in; |
272 |
{ |
273 |
int yread; |
274 |
int ycent, xcent; |
275 |
int r, c; |
276 |
|
277 |
pass2init(); |
278 |
scan2init(); |
279 |
yread = 0; |
280 |
for (r = 0; r < nrows; r++) { |
281 |
ycent = (long)r*yres/nrows; |
282 |
while (yread <= ycent+yrad) { |
283 |
if (yread < yres) { |
284 |
if (freadscan(scanin[yread%barsize], |
285 |
xres, in) < 0) { |
286 |
fprintf(stderr, |
287 |
"%s: bad read (y=%d)\n", |
288 |
progname, yres-1-yread); |
289 |
quit(1); |
290 |
} |
291 |
pass2scan(scanin[yread%barsize], yread); |
292 |
} |
293 |
yread++; |
294 |
} |
295 |
for (c = 0; c < ncols; c++) { |
296 |
xcent = (long)c*xres/ncols; |
297 |
if (rad <= 0.0) |
298 |
dobox(scanout[c], xcent, ycent, c, r); |
299 |
else |
300 |
dogauss(scanout[c], xcent, ycent, c, r); |
301 |
} |
302 |
if (fwritescan(scanout, ncols, stdout) < 0) { |
303 |
fprintf(stderr, "%s: write error in pass2\n", progname); |
304 |
quit(1); |
305 |
} |
306 |
} |
307 |
} |
308 |
|
309 |
|
310 |
scan2init() /* prepare scanline arrays */ |
311 |
{ |
312 |
double e; |
313 |
register int i; |
314 |
|
315 |
if (rad <= 0.0) { |
316 |
xrad = xres/ncols/2 + 1; |
317 |
yrad = yres/nrows/2 + 1; |
318 |
} else { |
319 |
if (nrows >= yres && ncols >= xres) |
320 |
rad *= (y_r + x_c)/2.0; |
321 |
|
322 |
xrad = CHECKRAD*rad/x_c + 1; |
323 |
yrad = CHECKRAD*rad/y_r + 1; |
324 |
|
325 |
initmask(); /* initialize filter table */ |
326 |
} |
327 |
barsize = 2 * yrad; |
328 |
scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); |
329 |
for (i = 0; i < barsize; i++) { |
330 |
scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
331 |
if (scanin[i] == NULL) { |
332 |
fprintf(stderr, "%s: out of memory\n", progname); |
333 |
quit(1); |
334 |
} |
335 |
} |
336 |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
337 |
if (scanout == NULL) { |
338 |
fprintf(stderr, "%s: out of memory\n", progname); |
339 |
quit(1); |
340 |
} |
341 |
e = bright(exposure); |
342 |
if (e < 1-1e-7 || e > 1+1e-7) /* record exposure */ |
343 |
printf("EXPOSURE=%e\n", e); |
344 |
printf("\n"); |
345 |
printf("-Y %d +X %d\n", nrows, ncols); /* write picture size */ |
346 |
} |
347 |
|
348 |
|
349 |
quit(code) /* remove temporary file and exit */ |
350 |
int code; |
351 |
{ |
352 |
if (tfname != NULL) |
353 |
unlink(tfname); |
354 |
exit(code); |
355 |
} |