ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.29
Committed: Sun Mar 28 20:33:14 2004 UTC (20 years ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.28: +46 -21 lines
Log Message:
Continued ANSIfication, and other fixes and clarifications.

File Contents

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