ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.20
Committed: Tue Oct 27 09:08:26 1998 UTC (25 years, 5 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.19: +2 -0 lines
Log Message:
changed getheader() to listen to return value of passed function

File Contents

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