1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: pfilt.c,v 2.22 2003/02/25 00:26:05 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* pfilt.c - program to post-process picture file. |
6 |
* |
7 |
* 9/26/85 |
8 |
* 6/23/93 Added additional buffers for value spreading |
9 |
*/ |
10 |
|
11 |
#include <signal.h> |
12 |
|
13 |
#include "standard.h" |
14 |
#include "color.h" |
15 |
#include "view.h" |
16 |
#include "paths.h" |
17 |
|
18 |
extern float *matchlamp(); |
19 |
|
20 |
#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) |
21 |
|
22 |
double CHECKRAD = 2.0; /* radius to check for filtering */ |
23 |
|
24 |
#define THRESHRAD 5.0 /* maximum sample spread in output */ |
25 |
|
26 |
COLOR exposure = WHTCOLOR; /* exposure for the frame */ |
27 |
|
28 |
double rad = 0.0; /* output pixel radius for filtering */ |
29 |
|
30 |
double thresh = 0.0; /* maximum contribution for subpixel */ |
31 |
|
32 |
int nrows = 0; /* number of rows for output */ |
33 |
int ncols = 0; /* number of columns for output */ |
34 |
|
35 |
double x_c = 1.0; /* ratio of output x size to input */ |
36 |
double y_r = 1.0; /* ratio of output y size to input */ |
37 |
|
38 |
int singlepass = 0; /* true means skip first pass */ |
39 |
|
40 |
int avghot = 0; /* true means average in bright spots */ |
41 |
|
42 |
double hotlvl = 100.0; /* level considered "hot" */ |
43 |
|
44 |
int npts = 0; /* (half) number of points for stars */ |
45 |
|
46 |
double spread = 1e-4; /* spread for star points */ |
47 |
|
48 |
char *tfname = NULL; |
49 |
|
50 |
char template[] = TEMPLATE; |
51 |
|
52 |
char *lampdat = "lamp.tab"; /* lamp data file */ |
53 |
|
54 |
int order; /* scanline ordering of input */ |
55 |
int xres, yres; /* resolution of input */ |
56 |
double inpaspect = 1.0; /* pixel aspect ratio of input */ |
57 |
int correctaspect = 0; /* aspect ratio correction? */ |
58 |
|
59 |
int wrongformat = 0; |
60 |
|
61 |
VIEW ourview = STDVIEW; |
62 |
int gotview = 0; |
63 |
int wrapfilt = 0; /* wrap filter horizontally? */ |
64 |
|
65 |
int estatus = 0; /* exit status (for non-fatal errors) */ |
66 |
|
67 |
int xrad; /* x search radius */ |
68 |
int yrad; /* y search radius */ |
69 |
int xbrad; /* x box size */ |
70 |
int ybrad; /* y box size */ |
71 |
|
72 |
int barsize; /* size of input scan bar */ |
73 |
COLOR **scanin; /* input scan bar */ |
74 |
COLOR *scanout; /* output scan line */ |
75 |
COLOR **scoutbar; /* output scan bar (if thresh > 0) */ |
76 |
float **greybar; /* grey-averaged input values */ |
77 |
int obarsize = 0; /* size of output scan bar */ |
78 |
int orad = 0; /* output window radius */ |
79 |
|
80 |
char *progname; |
81 |
|
82 |
|
83 |
main(argc, argv) |
84 |
int argc; |
85 |
char **argv; |
86 |
{ |
87 |
extern int headline(); |
88 |
FILE *fin; |
89 |
float *lampcolor; |
90 |
char *lamptype = NULL; |
91 |
long fpos; |
92 |
double outaspect = 0.0; |
93 |
double d; |
94 |
int i, j; |
95 |
SET_DEFAULT_BINARY(); |
96 |
SET_FILE_BINARY(stdin); |
97 |
SET_FILE_BINARY(stdout); |
98 |
if (signal(SIGINT, quit) == SIG_IGN) |
99 |
signal(SIGINT, SIG_IGN); |
100 |
if (signal(SIGHUP, quit) == SIG_IGN) |
101 |
signal(SIGHUP, SIG_IGN); |
102 |
signal(SIGTERM, quit); |
103 |
signal(SIGPIPE, quit); |
104 |
#ifdef SIGXCPU |
105 |
signal(SIGXCPU, quit); |
106 |
signal(SIGXFSZ, quit); |
107 |
#endif |
108 |
|
109 |
progname = argv[0] = fixargv0(argv[0]); |
110 |
|
111 |
for (i = 1; i < argc; i++) |
112 |
if (argv[i][0] == '-') |
113 |
switch (argv[i][1]) { |
114 |
case 'x': |
115 |
i++; |
116 |
if (argv[i][0] == '/') { |
117 |
x_c = 1.0/atof(argv[i]+1); |
118 |
ncols = 0; |
119 |
} else |
120 |
ncols = atoi(argv[i]); |
121 |
break; |
122 |
case 'y': |
123 |
i++; |
124 |
if (argv[i][0] == '/') { |
125 |
y_r = 1.0/atof(argv[i]+1); |
126 |
nrows = 0; |
127 |
} else |
128 |
nrows = atoi(argv[i]); |
129 |
break; |
130 |
case 'c': |
131 |
correctaspect = !correctaspect; |
132 |
break; |
133 |
case 'p': |
134 |
i++; |
135 |
outaspect = atof(argv[i]); |
136 |
break; |
137 |
case 'e': |
138 |
if (argv[i+1][0] == '+' || argv[i+1][0] == '-') |
139 |
d = pow(2.0, atof(argv[i+1])); |
140 |
else |
141 |
d = atof(argv[i+1]); |
142 |
if (d < 1e-20 || d > 1e20) { |
143 |
fprintf(stderr, |
144 |
"%s: exposure out of range\n", |
145 |
argv[0]); |
146 |
quit(1); |
147 |
} |
148 |
switch (argv[i][2]) { |
149 |
case '\0': |
150 |
scalecolor(exposure, d); |
151 |
break; |
152 |
case 'r': |
153 |
colval(exposure,RED) *= d; |
154 |
break; |
155 |
case 'g': |
156 |
colval(exposure,GRN) *= d; |
157 |
break; |
158 |
case 'b': |
159 |
colval(exposure,BLU) *= d; |
160 |
break; |
161 |
default: |
162 |
goto badopt; |
163 |
} |
164 |
i++; |
165 |
break; |
166 |
case 'f': |
167 |
lampdat = argv[++i]; |
168 |
break; |
169 |
case 't': |
170 |
lamptype = argv[++i]; |
171 |
break; |
172 |
case '1': |
173 |
singlepass = 1; |
174 |
break; |
175 |
case '2': |
176 |
singlepass = 0; |
177 |
break; |
178 |
case 'n': |
179 |
npts = atoi(argv[++i]) / 2; |
180 |
break; |
181 |
case 's': |
182 |
spread = atof(argv[++i]); |
183 |
break; |
184 |
case 'a': |
185 |
avghot = !avghot; |
186 |
break; |
187 |
case 'h': |
188 |
hotlvl = atof(argv[++i]); |
189 |
break; |
190 |
case 'r': |
191 |
rad = atof(argv[++i]); |
192 |
break; |
193 |
case 'm': |
194 |
thresh = atof(argv[++i]); |
195 |
if (rad <= FTINY) |
196 |
rad = 0.6; |
197 |
break; |
198 |
case 'b': |
199 |
rad = thresh = 0.0; |
200 |
break; |
201 |
default:; |
202 |
badopt: |
203 |
fprintf(stderr, "%s: unknown option: %s\n", |
204 |
progname, argv[i]); |
205 |
quit(1); |
206 |
break; |
207 |
} |
208 |
else |
209 |
break; |
210 |
/* get lamp data (if necessary) */ |
211 |
if (lamptype != NULL) { |
212 |
if (loadlamps(lampdat) < 0) |
213 |
quit(1); |
214 |
if ((lampcolor = matchlamp(lamptype)) == NULL) { |
215 |
fprintf(stderr, "%s: unknown lamp type\n", lamptype); |
216 |
quit(1); |
217 |
} |
218 |
for (j = 0; j < 3; j++) |
219 |
if (lampcolor[j] > 1e-4) |
220 |
colval(exposure,j) /= lampcolor[j]; |
221 |
freelamps(); |
222 |
} |
223 |
/* open input file */ |
224 |
if (i == argc) { |
225 |
if (singlepass) |
226 |
fin = stdin; |
227 |
else { |
228 |
tfname = mktemp(template); |
229 |
if ((fin = fopen(tfname, "w+")) == NULL) { |
230 |
fprintf(stderr, "%s: can't create ", progname); |
231 |
fprintf(stderr, "temp file \"%s\"\n", tfname); |
232 |
quit(1); |
233 |
} |
234 |
copyfile(stdin, fin); |
235 |
if (fseek(fin, 0L, 0) == -1) { |
236 |
fprintf(stderr, "%s: seek fail\n", progname); |
237 |
quit(1); |
238 |
} |
239 |
} |
240 |
} else if (i == argc-1) { |
241 |
if ((fin = fopen(argv[i], "r")) == NULL) { |
242 |
fprintf(stderr, "%s: can't open file \"%s\"\n", |
243 |
progname, argv[i]); |
244 |
quit(1); |
245 |
} |
246 |
} else { |
247 |
fprintf(stderr, "%s: bad # file arguments\n", progname); |
248 |
quit(1); |
249 |
} |
250 |
/* get header */ |
251 |
getheader(fin, headline, NULL); |
252 |
if (wrongformat) { |
253 |
fprintf(stderr, "%s: input must be a Radiance picture\n", |
254 |
progname); |
255 |
quit(1); |
256 |
} |
257 |
/* add new header info. */ |
258 |
printargs(i, argv, stdout); |
259 |
/* get picture size */ |
260 |
if ((order = fgetresolu(&xres, &yres, fin)) < 0) { |
261 |
fprintf(stderr, "%s: bad picture size\n", progname); |
262 |
quit(1); |
263 |
} |
264 |
if (!(order & YMAJOR)) |
265 |
inpaspect = 1.0/inpaspect; |
266 |
/* wrap around for cylindrical view? */ |
267 |
wrapfilt = gotview && ourview.type == VT_CYL && |
268 |
ourview.horiz >= 360.-FTINY && order & YMAJOR; |
269 |
/* compute output resolution */ |
270 |
if (ncols <= 0) |
271 |
ncols = x_c*xres + .5; |
272 |
if (nrows <= 0) |
273 |
nrows = y_r*yres + .5; |
274 |
if (outaspect > .01) { |
275 |
d = inpaspect * yres/xres / outaspect; |
276 |
if (d * ncols > nrows) |
277 |
ncols = nrows / d; |
278 |
else |
279 |
nrows = ncols * d; |
280 |
} |
281 |
x_c = (double)ncols/xres; |
282 |
y_r = (double)nrows/yres; |
283 |
|
284 |
if (singlepass) { /* skip exposure, etc. */ |
285 |
pass1default(); |
286 |
pass2(fin); |
287 |
quit(0); |
288 |
} |
289 |
|
290 |
fpos = ftell(fin); /* save input file position */ |
291 |
|
292 |
pass1(fin); |
293 |
|
294 |
if (fseek(fin, fpos, 0) == -1) { |
295 |
fprintf(stderr, "%s: seek fail\n", progname); |
296 |
quit(1); |
297 |
} |
298 |
pass2(fin); |
299 |
|
300 |
quit(estatus); |
301 |
} |
302 |
|
303 |
|
304 |
double |
305 |
rgb_bright(clr) |
306 |
COLOR clr; |
307 |
{ |
308 |
return(bright(clr)); |
309 |
} |
310 |
|
311 |
|
312 |
double |
313 |
xyz_bright(clr) |
314 |
COLOR clr; |
315 |
{ |
316 |
return(clr[CIEY]); |
317 |
} |
318 |
|
319 |
|
320 |
double (*ourbright)() = rgb_bright; |
321 |
|
322 |
|
323 |
int |
324 |
headline(s) /* process line from header */ |
325 |
char *s; |
326 |
{ |
327 |
char fmt[32]; |
328 |
|
329 |
fputs(s, stdout); /* copy to output */ |
330 |
if (isaspect(s)) /* get aspect ratio */ |
331 |
inpaspect *= aspectval(s); |
332 |
else if (isexpos(s)) /* get exposure */ |
333 |
hotlvl *= exposval(s); |
334 |
else if (formatval(fmt, s)) { /* get format */ |
335 |
wrongformat = 0; |
336 |
if (!strcmp(COLRFMT, fmt)) |
337 |
ourbright = rgb_bright; |
338 |
else if (!strcmp(CIEFMT, fmt)) |
339 |
ourbright = xyz_bright; |
340 |
else |
341 |
wrongformat = !globmatch(PICFMT, fmt); |
342 |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
343 |
gotview++; |
344 |
return(0); |
345 |
} |
346 |
|
347 |
|
348 |
copyfile(in, out) /* copy a file */ |
349 |
register FILE *in, *out; |
350 |
{ |
351 |
register int c; |
352 |
|
353 |
while ((c = getc(in)) != EOF) |
354 |
putc(c, out); |
355 |
|
356 |
if (ferror(out)) { |
357 |
fprintf(stderr, "%s: write error in copyfile\n", progname); |
358 |
quit(1); |
359 |
} |
360 |
} |
361 |
|
362 |
|
363 |
pass1(in) /* first pass of picture file */ |
364 |
FILE *in; |
365 |
{ |
366 |
int i; |
367 |
COLOR *scan; |
368 |
|
369 |
pass1init(); |
370 |
|
371 |
scan = (COLOR *)malloc(xres*sizeof(COLOR)); |
372 |
if (scan == NULL) { |
373 |
fprintf(stderr, "%s: out of memory\n", progname); |
374 |
quit(1); |
375 |
} |
376 |
for (i = 0; i < yres; i++) { |
377 |
if (freadscan(scan, xres, in) < 0) { |
378 |
nrows = (long)nrows * i / yres; /* adjust frame */ |
379 |
if (nrows <= 0) { |
380 |
fprintf(stderr, "%s: empty frame\n", progname); |
381 |
quit(1); |
382 |
} |
383 |
fprintf(stderr, "%s: warning - partial frame (%d%%)\n", |
384 |
progname, (int)(100L*i/yres)); |
385 |
yres = i; |
386 |
y_r = (double)nrows/yres; |
387 |
estatus++; |
388 |
break; |
389 |
} |
390 |
pass1scan(scan, i); |
391 |
} |
392 |
free((void *)scan); |
393 |
} |
394 |
|
395 |
|
396 |
pass2(in) /* last pass on file, write to stdout */ |
397 |
FILE *in; |
398 |
{ |
399 |
int yread; |
400 |
int ycent, xcent; |
401 |
int r, c; |
402 |
|
403 |
pass2init(); |
404 |
scan2init(); |
405 |
yread = 0; |
406 |
for (r = 0; r < nrows; r++) { |
407 |
ycent = (r+.5)*yres/nrows; |
408 |
while (yread <= ycent+yrad) { |
409 |
if (yread < yres) { |
410 |
if (freadscan(scanin[yread%barsize], |
411 |
xres, in) < 0) { |
412 |
fprintf(stderr, |
413 |
"%s: truncated input (y=%d)\n", |
414 |
progname, yres-1-yread); |
415 |
quit(1); |
416 |
} |
417 |
pass2scan(scanin[yread%barsize], yread); |
418 |
} |
419 |
yread++; |
420 |
} |
421 |
if (obarsize > 0) |
422 |
scan2sync(r); |
423 |
for (c = 0; c < ncols; c++) { |
424 |
xcent = (c+.5)*xres/ncols; |
425 |
if (thresh > FTINY) |
426 |
dothresh(xcent, ycent, c, r); |
427 |
else if (rad > FTINY) |
428 |
dogauss(scanout[c], xcent, ycent, c, r); |
429 |
else |
430 |
dobox(scanout[c], xcent, ycent, c, r); |
431 |
} |
432 |
if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) { |
433 |
fprintf(stderr, "%s: write error in pass2\n", progname); |
434 |
quit(1); |
435 |
} |
436 |
} |
437 |
/* skip leftover input */ |
438 |
while (yread < yres) { |
439 |
if (freadscan(scanin[0], xres, in) < 0) |
440 |
break; |
441 |
yread++; |
442 |
} |
443 |
scan2flush(); /* flush output */ |
444 |
} |
445 |
|
446 |
|
447 |
scan2init() /* prepare scanline arrays */ |
448 |
{ |
449 |
COLOR ctmp; |
450 |
double d; |
451 |
register int i; |
452 |
|
453 |
xbrad = xres/ncols/2 + 1; |
454 |
ybrad = yres/nrows/2 + 1; |
455 |
if (rad > FTINY) { |
456 |
if (nrows >= yres && ncols >= xres) |
457 |
rad *= (y_r + x_c)/2.0; |
458 |
|
459 |
if (thresh > FTINY) { |
460 |
orad = CHECKRAD*THRESHRAD*rad + 1; |
461 |
xrad = orad/x_c + xbrad; |
462 |
yrad = orad/y_r + ybrad; |
463 |
obarsize = 2*orad + 1; |
464 |
} else { |
465 |
xrad = CHECKRAD*rad/x_c + 1; |
466 |
yrad = CHECKRAD*rad/y_r + 1; |
467 |
} |
468 |
initmask(); /* initialize filter table */ |
469 |
} else { |
470 |
xrad = xbrad; |
471 |
yrad = ybrad; |
472 |
} |
473 |
barsize = 2*yrad + 1; |
474 |
scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); |
475 |
if (scanin == NULL) |
476 |
goto memerr; |
477 |
for (i = 0; i < barsize; i++) { |
478 |
scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
479 |
if (scanin[i] == NULL) |
480 |
goto memerr; |
481 |
} |
482 |
if (obarsize > 0) { |
483 |
scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *)); |
484 |
greybar = (float **)malloc(obarsize*sizeof(float *)); |
485 |
if (scoutbar == NULL | greybar == NULL) |
486 |
goto memerr; |
487 |
for (i = 0; i < obarsize; i++) { |
488 |
scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR)); |
489 |
greybar[i] = (float *)malloc(ncols*sizeof(float)); |
490 |
if (scoutbar[i] == NULL | greybar[i] == NULL) |
491 |
goto memerr; |
492 |
} |
493 |
} else { |
494 |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
495 |
if (scanout == NULL) |
496 |
goto memerr; |
497 |
} |
498 |
/* record pixel aspect ratio */ |
499 |
if (!correctaspect) { |
500 |
d = order & YMAJOR ? x_c/y_r : y_r/x_c ; |
501 |
if (!FEQ(d,1.0)) |
502 |
fputaspect(d, stdout); |
503 |
} |
504 |
/* record exposure */ |
505 |
d = (*ourbright)(exposure); |
506 |
if (!FEQ(d,1.0)) |
507 |
fputexpos(d, stdout); |
508 |
/* record color correction */ |
509 |
copycolor(ctmp, exposure); |
510 |
scalecolor(ctmp, 1.0/d); |
511 |
if (!FEQ(colval(ctmp,RED),colval(ctmp,GRN)) || |
512 |
!FEQ(colval(ctmp,GRN),colval(ctmp,BLU))) |
513 |
fputcolcor(ctmp, stdout); |
514 |
printf("\n"); |
515 |
/* write out resolution */ |
516 |
fputresolu(order, ncols, nrows, stdout); |
517 |
return; |
518 |
memerr: |
519 |
fprintf(stderr, "%s: out of memory\n", progname); |
520 |
quit(1); |
521 |
} |
522 |
|
523 |
|
524 |
scan2sync(r) /* synchronize grey averages and output scan */ |
525 |
int r; |
526 |
{ |
527 |
static int nextrow = 0; |
528 |
COLOR ctmp; |
529 |
int ybot; |
530 |
register int c; |
531 |
/* average input scanlines */ |
532 |
while (nextrow <= r+orad && nextrow < nrows) { |
533 |
ybot = (nextrow+.5)*yres/nrows; |
534 |
for (c = 0; c < ncols; c++) { |
535 |
dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow); |
536 |
greybar[nextrow%obarsize][c] = (*ourbright)(ctmp); |
537 |
} |
538 |
/* and zero output scanline */ |
539 |
bzero((char *)scoutbar[nextrow%obarsize], ncols*sizeof(COLOR)); |
540 |
nextrow++; |
541 |
} |
542 |
/* point to top scanline for output */ |
543 |
if (r-orad >= 0) |
544 |
scanout = scoutbar[(r-orad)%obarsize]; |
545 |
else |
546 |
scanout = NULL; |
547 |
} |
548 |
|
549 |
|
550 |
scan2flush() /* flush output buffer */ |
551 |
{ |
552 |
register int r; |
553 |
|
554 |
for (r = nrows-orad; r < nrows; r++) |
555 |
if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0) |
556 |
break; |
557 |
if (fflush(stdout) < 0) { |
558 |
fprintf(stderr, "%s: write error at end of pass2\n", progname); |
559 |
quit(1); |
560 |
} |
561 |
} |
562 |
|
563 |
|
564 |
void |
565 |
quit(code) /* remove temporary file and exit */ |
566 |
int code; |
567 |
{ |
568 |
if (tfname != NULL) |
569 |
unlink(tfname); |
570 |
exit(code); |
571 |
} |