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 |
extern char *malloc(); |
20 |
extern float *matchlamp(); |
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 |
char *lampdat = "lamp.tab"; /* lamp data file */ |
49 |
|
50 |
int xres, yres; /* resolution of input */ |
51 |
double inpaspect = 1.0; /* pixel aspect ratio of input */ |
52 |
int correctaspect = 0; /* aspect ratio correction? */ |
53 |
|
54 |
int xrad; /* x window size */ |
55 |
int yrad; /* y window size */ |
56 |
|
57 |
int barsize; /* size of input scan bar */ |
58 |
COLOR **scanin; /* input scan bar */ |
59 |
COLOR *scanout; /* output scan line */ |
60 |
|
61 |
char *progname; |
62 |
|
63 |
|
64 |
main(argc, argv) |
65 |
int argc; |
66 |
char **argv; |
67 |
{ |
68 |
extern char *mktemp(); |
69 |
extern double atof(), pow(); |
70 |
extern long ftell(); |
71 |
extern int quit(), headline(); |
72 |
FILE *fin; |
73 |
float *lampcolor; |
74 |
char *lamptype = NULL; |
75 |
long fpos; |
76 |
double outaspect = 0.0; |
77 |
double d; |
78 |
int i; |
79 |
|
80 |
if (signal(SIGINT, quit) == SIG_IGN) |
81 |
signal(SIGINT, SIG_IGN); |
82 |
if (signal(SIGHUP, quit) == SIG_IGN) |
83 |
signal(SIGINT, SIG_IGN); |
84 |
signal(SIGTERM, quit); |
85 |
signal(SIGPIPE, quit); |
86 |
#ifdef SIGXCPU |
87 |
signal(SIGXCPU, quit); |
88 |
signal(SIGXFSZ, quit); |
89 |
#endif |
90 |
|
91 |
progname = argv[0]; |
92 |
|
93 |
for (i = 1; i < argc; i++) |
94 |
if (argv[i][0] == '-') |
95 |
switch (argv[i][1]) { |
96 |
case 'x': |
97 |
i++; |
98 |
if (argv[i][0] == '/') { |
99 |
x_c = 1.0/atof(argv[i]+1); |
100 |
ncols = 0; |
101 |
} else |
102 |
ncols = atoi(argv[i]); |
103 |
break; |
104 |
case 'y': |
105 |
i++; |
106 |
if (argv[i][0] == '/') { |
107 |
y_r = 1.0/atof(argv[i]+1); |
108 |
nrows = 0; |
109 |
} else |
110 |
nrows = atoi(argv[i]); |
111 |
break; |
112 |
case 'c': |
113 |
correctaspect = !correctaspect; |
114 |
break; |
115 |
case 'p': |
116 |
i++; |
117 |
outaspect = atof(argv[i]); |
118 |
break; |
119 |
case 'e': |
120 |
if (argv[i+1][0] == '+' || argv[i+1][0] == '-') |
121 |
d = pow(2.0, atof(argv[i+1])); |
122 |
else |
123 |
d = atof(argv[i+1]); |
124 |
switch (argv[i][2]) { |
125 |
case '\0': |
126 |
scalecolor(exposure, d); |
127 |
break; |
128 |
case 'r': |
129 |
colval(exposure,RED) *= d; |
130 |
break; |
131 |
case 'g': |
132 |
colval(exposure,GRN) *= d; |
133 |
break; |
134 |
case 'b': |
135 |
colval(exposure,BLU) *= d; |
136 |
break; |
137 |
default: |
138 |
goto badopt; |
139 |
} |
140 |
i++; |
141 |
break; |
142 |
case 'f': |
143 |
lampdat = argv[++i]; |
144 |
break; |
145 |
case 't': |
146 |
lamptype = argv[++i]; |
147 |
break; |
148 |
case '1': |
149 |
singlepass = 1; |
150 |
break; |
151 |
case '2': |
152 |
singlepass = 0; |
153 |
break; |
154 |
case 'n': |
155 |
npts = atoi(argv[++i]) / 2; |
156 |
break; |
157 |
case 's': |
158 |
spread = atof(argv[++i]); |
159 |
break; |
160 |
case 'a': |
161 |
avghot = !avghot; |
162 |
break; |
163 |
case 'h': |
164 |
hotlvl = atof(argv[++i]); |
165 |
break; |
166 |
case 'r': |
167 |
rad = atof(argv[++i]); |
168 |
break; |
169 |
case 'b': |
170 |
rad = 0.0; |
171 |
break; |
172 |
default:; |
173 |
badopt: |
174 |
fprintf(stderr, "%s: unknown option: %s\n", |
175 |
progname, argv[i]); |
176 |
quit(1); |
177 |
break; |
178 |
} |
179 |
else |
180 |
break; |
181 |
|
182 |
if (i == argc) { |
183 |
if (singlepass) |
184 |
fin = stdin; |
185 |
else { |
186 |
tfname = mktemp(TEMPLATE); |
187 |
if ((fin = fopen(tfname, "w+")) == NULL) { |
188 |
fprintf(stderr, "%s: can't create ", progname); |
189 |
fprintf(stderr, "temp file \"%s\"\n", tfname); |
190 |
quit(1); |
191 |
} |
192 |
copyfile(stdin, fin); |
193 |
if (fseek(fin, 0L, 0) == -1) { |
194 |
fprintf(stderr, "%s: seek fail\n", progname); |
195 |
quit(1); |
196 |
} |
197 |
} |
198 |
} else if (i == argc-1) { |
199 |
if ((fin = fopen(argv[i], "r")) == NULL) { |
200 |
fprintf(stderr, "%s: can't open file \"%s\"\n", |
201 |
progname, argv[i]); |
202 |
quit(1); |
203 |
} |
204 |
} else { |
205 |
fprintf(stderr, "%s: bad # file arguments\n", progname); |
206 |
quit(1); |
207 |
} |
208 |
/* get lamp data (if necessary) */ |
209 |
if (lamptype != NULL) { |
210 |
if (loadlamps(lampdat) < 0) |
211 |
quit(1); |
212 |
if ((lampcolor = matchlamp(lamptype)) == NULL) { |
213 |
fprintf(stderr, "%s: unknown lamp type\n", lamptype); |
214 |
quit(1); |
215 |
} |
216 |
colval(exposure,RED) /= lampcolor[0]; |
217 |
colval(exposure,GRN) /= lampcolor[1]; |
218 |
colval(exposure,BLU) /= lampcolor[2]; |
219 |
freelamps(); |
220 |
} |
221 |
/* get header */ |
222 |
getheader(fin, headline); |
223 |
/* add new header info. */ |
224 |
printargs(i, argv, stdout); |
225 |
/* get picture size */ |
226 |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
227 |
fprintf(stderr, "%s: bad picture size\n", progname); |
228 |
quit(1); |
229 |
} |
230 |
/* compute output resolution */ |
231 |
if (ncols <= 0) |
232 |
ncols = x_c*xres + .5; |
233 |
if (nrows <= 0) |
234 |
nrows = y_r*yres + .5; |
235 |
if (outaspect > .01) { |
236 |
d = inpaspect * yres/xres / outaspect; |
237 |
if (d * ncols > nrows) |
238 |
ncols = nrows / d; |
239 |
else |
240 |
nrows = ncols * d; |
241 |
} |
242 |
x_c = (double)ncols/xres; |
243 |
y_r = (double)nrows/yres; |
244 |
|
245 |
if (singlepass) { /* skip exposure, etc. */ |
246 |
pass1default(); |
247 |
pass2(fin); |
248 |
quit(0); |
249 |
} |
250 |
|
251 |
fpos = ftell(fin); /* save input file position */ |
252 |
|
253 |
pass1(fin); |
254 |
|
255 |
if (fseek(fin, fpos, 0) == -1) { |
256 |
fprintf(stderr, "%s: seek fail\n", progname); |
257 |
quit(1); |
258 |
} |
259 |
pass2(fin); |
260 |
|
261 |
quit(0); |
262 |
} |
263 |
|
264 |
|
265 |
headline(s) /* process line from header */ |
266 |
char *s; |
267 |
{ |
268 |
fputs(s, stdout); /* copy to output */ |
269 |
if (isaspect(s)) /* get aspect ratio */ |
270 |
inpaspect *= aspectval(s); |
271 |
} |
272 |
|
273 |
|
274 |
copyfile(in, out) /* copy a file */ |
275 |
register FILE *in, *out; |
276 |
{ |
277 |
register int c; |
278 |
|
279 |
while ((c = getc(in)) != EOF) |
280 |
putc(c, out); |
281 |
|
282 |
if (ferror(out)) { |
283 |
fprintf(stderr, "%s: write error in copyfile\n", progname); |
284 |
quit(1); |
285 |
} |
286 |
} |
287 |
|
288 |
|
289 |
pass1(in) /* first pass of picture file */ |
290 |
FILE *in; |
291 |
{ |
292 |
int i; |
293 |
COLOR *scan; |
294 |
|
295 |
pass1init(); |
296 |
|
297 |
scan = (COLOR *)malloc(xres*sizeof(COLOR)); |
298 |
if (scan == NULL) { |
299 |
fprintf(stderr, "%s: out of memory\n", progname); |
300 |
quit(1); |
301 |
} |
302 |
for (i = 0; i < yres; i++) { |
303 |
if (freadscan(scan, xres, in) < 0) { |
304 |
nrows = nrows * i / yres; /* adjust frame */ |
305 |
if (nrows <= 0) { |
306 |
fprintf(stderr, "%s: empty frame\n", progname); |
307 |
quit(1); |
308 |
} |
309 |
fprintf(stderr, "%s: warning - partial frame (%d%%)\n", |
310 |
progname, 100*i/yres); |
311 |
yres = i; |
312 |
y_r = (double)nrows/yres; |
313 |
break; |
314 |
} |
315 |
pass1scan(scan, i); |
316 |
} |
317 |
free((char *)scan); |
318 |
} |
319 |
|
320 |
|
321 |
pass2(in) /* last pass on file, write to stdout */ |
322 |
FILE *in; |
323 |
{ |
324 |
int yread; |
325 |
int ycent, xcent; |
326 |
int r, c; |
327 |
|
328 |
pass2init(); |
329 |
scan2init(); |
330 |
yread = 0; |
331 |
for (r = 0; r < nrows; r++) { |
332 |
ycent = (long)r*yres/nrows; |
333 |
while (yread <= ycent+yrad) { |
334 |
if (yread < yres) { |
335 |
if (freadscan(scanin[yread%barsize], |
336 |
xres, in) < 0) { |
337 |
fprintf(stderr, |
338 |
"%s: bad read (y=%d)\n", |
339 |
progname, yres-1-yread); |
340 |
quit(1); |
341 |
} |
342 |
pass2scan(scanin[yread%barsize], yread); |
343 |
} |
344 |
yread++; |
345 |
} |
346 |
for (c = 0; c < ncols; c++) { |
347 |
xcent = (long)c*xres/ncols; |
348 |
if (rad <= 0.0) |
349 |
dobox(scanout[c], xcent, ycent, c, r); |
350 |
else |
351 |
dogauss(scanout[c], xcent, ycent, c, r); |
352 |
} |
353 |
if (fwritescan(scanout, ncols, stdout) < 0) { |
354 |
fprintf(stderr, "%s: write error in pass2\n", progname); |
355 |
quit(1); |
356 |
} |
357 |
} |
358 |
/* skip leftovers */ |
359 |
while (yread < yres) { |
360 |
if (freadscan(scanin[0], xres, in) < 0) |
361 |
break; |
362 |
yread++; |
363 |
} |
364 |
} |
365 |
|
366 |
|
367 |
scan2init() /* prepare scanline arrays */ |
368 |
{ |
369 |
double d; |
370 |
register int i; |
371 |
|
372 |
if (rad <= 0.0) { |
373 |
xrad = xres/ncols/2 + 1; |
374 |
yrad = yres/nrows/2 + 1; |
375 |
} else { |
376 |
if (nrows >= yres && ncols >= xres) |
377 |
rad *= (y_r + x_c)/2.0; |
378 |
|
379 |
xrad = CHECKRAD*rad/x_c + 1; |
380 |
yrad = CHECKRAD*rad/y_r + 1; |
381 |
|
382 |
initmask(); /* initialize filter table */ |
383 |
} |
384 |
barsize = 2 * yrad; |
385 |
scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); |
386 |
for (i = 0; i < barsize; i++) { |
387 |
scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
388 |
if (scanin[i] == NULL) { |
389 |
fprintf(stderr, "%s: out of memory\n", progname); |
390 |
quit(1); |
391 |
} |
392 |
} |
393 |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
394 |
if (scanout == NULL) { |
395 |
fprintf(stderr, "%s: out of memory\n", progname); |
396 |
quit(1); |
397 |
} |
398 |
/* record pixel aspect and exposure */ |
399 |
if (!correctaspect) { |
400 |
d = x_c / y_r; |
401 |
if (d < .99 || d > 1.01) |
402 |
fputaspect(d, stdout); |
403 |
} |
404 |
d = bright(exposure); |
405 |
if (d < .995 || d > 1.005) |
406 |
fputexpos(d, stdout); |
407 |
printf("\n"); |
408 |
fputresolu(YMAJOR|YDECR, ncols, nrows, stdout); /* resolution */ |
409 |
} |
410 |
|
411 |
|
412 |
quit(code) /* remove temporary file and exit */ |
413 |
int code; |
414 |
{ |
415 |
if (tfname != NULL) |
416 |
unlink(tfname); |
417 |
exit(code); |
418 |
} |