ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.23
Committed: Thu Jun 5 19:29:34 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.22: +5 -11 lines
Log Message:
Macros for setting binary file mode. Replacing MSDOS by _WIN32.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.23 static const char RCSid[] = "$Id: pfilt.c,v 2.22 2003/02/25 00:26:05 greg 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     #include <signal.h>
12    
13 schorsch 2.23 #include "standard.h"
14 greg 1.1 #include "color.h"
15 greg 2.17 #include "view.h"
16 greg 2.3 #include "paths.h"
17    
18 greg 1.13 extern float *matchlamp();
19 greg 1.1
20 greg 2.4 #define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b))
21 greg 1.15
22 greg 2.22 double CHECKRAD = 2.0; /* radius to check for filtering */
23 greg 1.1
24 greg 2.9 #define THRESHRAD 5.0 /* maximum sample spread in output */
25    
26 greg 1.1 COLOR exposure = WHTCOLOR; /* exposure for the frame */
27    
28 greg 2.4 double rad = 0.0; /* output pixel radius for filtering */
29 greg 1.1
30 greg 2.9 double thresh = 0.0; /* maximum contribution for subpixel */
31    
32 greg 1.1 int nrows = 0; /* number of rows for output */
33     int ncols = 0; /* number of columns for output */
34    
35 greg 2.4 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 greg 1.2
38 greg 1.1 int singlepass = 0; /* true means skip first pass */
39    
40     int avghot = 0; /* true means average in bright spots */
41    
42 greg 2.8 double hotlvl = 100.0; /* level considered "hot" */
43 greg 1.1
44     int npts = 0; /* (half) number of points for stars */
45    
46 greg 2.4 double spread = 1e-4; /* spread for star points */
47 greg 1.1
48     char *tfname = NULL;
49    
50 greg 2.11 char template[] = TEMPLATE;
51    
52 greg 1.13 char *lampdat = "lamp.tab"; /* lamp data file */
53    
54 greg 1.19 int order; /* scanline ordering of input */
55 greg 1.1 int xres, yres; /* resolution of input */
56 greg 2.4 double inpaspect = 1.0; /* pixel aspect ratio of input */
57 greg 1.12 int correctaspect = 0; /* aspect ratio correction? */
58 greg 1.1
59 greg 1.16 int wrongformat = 0;
60    
61 greg 2.17 VIEW ourview = STDVIEW;
62     int gotview = 0;
63     int wrapfilt = 0; /* wrap filter horizontally? */
64    
65 greg 2.18 int estatus = 0; /* exit status (for non-fatal errors) */
66    
67 greg 2.9 int xrad; /* x search radius */
68     int yrad; /* y search radius */
69     int xbrad; /* x box size */
70     int ybrad; /* y box size */
71 greg 1.1
72     int barsize; /* size of input scan bar */
73     COLOR **scanin; /* input scan bar */
74     COLOR *scanout; /* output scan line */
75 greg 2.9 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 greg 1.1
80     char *progname;
81    
82    
83     main(argc, argv)
84     int argc;
85     char **argv;
86     {
87 greg 2.21 extern int headline();
88 greg 1.1 FILE *fin;
89 greg 1.13 float *lampcolor;
90     char *lamptype = NULL;
91 greg 1.1 long fpos;
92 greg 2.4 double outaspect = 0.0;
93     double d;
94 greg 2.13 int i, j;
95 schorsch 2.23 SET_DEFAULT_BINARY();
96     SET_FILE_BINARY(stdin);
97     SET_FILE_BINARY(stdout);
98 greg 1.1 if (signal(SIGINT, quit) == SIG_IGN)
99     signal(SIGINT, SIG_IGN);
100     if (signal(SIGHUP, quit) == SIG_IGN)
101 greg 2.11 signal(SIGHUP, SIG_IGN);
102 greg 1.1 signal(SIGTERM, quit);
103     signal(SIGPIPE, quit);
104 greg 2.4 #ifdef SIGXCPU
105 greg 1.1 signal(SIGXCPU, quit);
106     signal(SIGXFSZ, quit);
107     #endif
108    
109 greg 2.6 progname = argv[0] = fixargv0(argv[0]);
110 greg 1.1
111     for (i = 1; i < argc; i++)
112     if (argv[i][0] == '-')
113     switch (argv[i][1]) {
114     case 'x':
115 greg 1.3 i++;
116 greg 1.5 if (argv[i][0] == '/') {
117 greg 1.4 x_c = 1.0/atof(argv[i]+1);
118 greg 1.5 ncols = 0;
119     } else
120 greg 1.3 ncols = atoi(argv[i]);
121 greg 1.1 break;
122     case 'y':
123 greg 1.3 i++;
124 greg 1.5 if (argv[i][0] == '/') {
125 greg 1.4 y_r = 1.0/atof(argv[i]+1);
126 greg 1.5 nrows = 0;
127     } else
128 greg 1.3 nrows = atoi(argv[i]);
129 greg 1.1 break;
130 greg 1.12 case 'c':
131     correctaspect = !correctaspect;
132     break;
133 greg 1.9 case 'p':
134     i++;
135     outaspect = atof(argv[i]);
136     break;
137 greg 1.1 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 greg 1.15 if (d < 1e-20 || d > 1e20) {
143     fprintf(stderr,
144     "%s: exposure out of range\n",
145     argv[0]);
146 greg 2.18 quit(1);
147 greg 1.15 }
148 greg 1.1 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 greg 1.13 case 'f':
167     lampdat = argv[++i];
168     break;
169     case 't':
170     lamptype = argv[++i];
171     break;
172 greg 1.1 case '1':
173     singlepass = 1;
174     break;
175 greg 1.6 case '2':
176     singlepass = 0;
177     break;
178 greg 1.9 case 'n':
179 greg 1.1 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 greg 2.9 case 'm':
194     thresh = atof(argv[++i]);
195     if (rad <= FTINY)
196 greg 2.21 rad = 0.6;
197 greg 2.9 break;
198 greg 1.1 case 'b':
199 greg 2.9 rad = thresh = 0.0;
200 greg 1.1 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 greg 1.14 /* 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 greg 2.13 for (j = 0; j < 3; j++)
219     if (lampcolor[j] > 1e-4)
220     colval(exposure,j) /= lampcolor[j];
221 greg 1.14 freelamps();
222     }
223     /* open input file */
224 greg 1.1 if (i == argc) {
225     if (singlepass)
226     fin = stdin;
227     else {
228 greg 2.11 tfname = mktemp(template);
229 greg 1.1 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 greg 1.9 /* get header */
251 greg 1.16 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 greg 1.1 /* add new header info. */
258     printargs(i, argv, stdout);
259     /* get picture size */
260 greg 1.19 if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
261 greg 1.1 fprintf(stderr, "%s: bad picture size\n", progname);
262     quit(1);
263     }
264 greg 1.19 if (!(order & YMAJOR))
265     inpaspect = 1.0/inpaspect;
266 greg 2.17 /* wrap around for cylindrical view? */
267     wrapfilt = gotview && ourview.type == VT_CYL &&
268     ourview.horiz >= 360.-FTINY && order & YMAJOR;
269 greg 1.9 /* compute output resolution */
270     if (ncols <= 0)
271 greg 1.4 ncols = x_c*xres + .5;
272 greg 1.9 if (nrows <= 0)
273 greg 1.4 nrows = y_r*yres + .5;
274 greg 1.9 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 greg 1.1
284 greg 1.9 if (singlepass) { /* skip exposure, etc. */
285 greg 1.1 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 greg 2.18 quit(estatus);
301 greg 1.1 }
302    
303    
304 greg 2.15 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 gwlarson 2.20 int
324 greg 1.9 headline(s) /* process line from header */
325     char *s;
326     {
327 greg 1.16 char fmt[32];
328    
329 greg 1.9 fputs(s, stdout); /* copy to output */
330     if (isaspect(s)) /* get aspect ratio */
331     inpaspect *= aspectval(s);
332 greg 2.15 else if (isexpos(s)) /* get exposure */
333 greg 2.8 hotlvl *= exposval(s);
334 greg 2.15 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 greg 2.17 } else if (isview(s) && sscanview(&ourview, s) > 0)
343     gotview++;
344 gwlarson 2.20 return(0);
345 greg 1.9 }
346    
347    
348 greg 1.1 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 greg 2.7 nrows = (long)nrows * i / yres; /* adjust frame */
379 greg 1.1 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 greg 2.7 progname, (int)(100L*i/yres));
385 greg 1.1 yres = i;
386 greg 1.11 y_r = (double)nrows/yres;
387 greg 2.18 estatus++;
388 greg 1.1 break;
389     }
390     pass1scan(scan, i);
391     }
392 greg 2.21 free((void *)scan);
393 greg 1.1 }
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 greg 2.4
403 greg 1.1 pass2init();
404     scan2init();
405     yread = 0;
406     for (r = 0; r < nrows; r++) {
407 greg 2.19 ycent = (r+.5)*yres/nrows;
408 greg 1.1 while (yread <= ycent+yrad) {
409     if (yread < yres) {
410     if (freadscan(scanin[yread%barsize],
411     xres, in) < 0) {
412     fprintf(stderr,
413 greg 2.14 "%s: truncated input (y=%d)\n",
414 greg 1.1 progname, yres-1-yread);
415     quit(1);
416     }
417     pass2scan(scanin[yread%barsize], yread);
418     }
419     yread++;
420     }
421 greg 2.9 if (obarsize > 0)
422     scan2sync(r);
423 greg 1.1 for (c = 0; c < ncols; c++) {
424 greg 2.19 xcent = (c+.5)*xres/ncols;
425 greg 2.9 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 greg 1.1 dobox(scanout[c], xcent, ycent, c, r);
431     }
432 greg 2.9 if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) {
433 greg 1.1 fprintf(stderr, "%s: write error in pass2\n", progname);
434     quit(1);
435     }
436     }
437 greg 2.9 /* skip leftover input */
438 greg 1.10 while (yread < yres) {
439     if (freadscan(scanin[0], xres, in) < 0)
440     break;
441     yread++;
442     }
443 greg 2.9 scan2flush(); /* flush output */
444 greg 1.1 }
445    
446    
447     scan2init() /* prepare scanline arrays */
448     {
449 greg 1.15 COLOR ctmp;
450 greg 2.4 double d;
451 greg 1.1 register int i;
452    
453 greg 2.9 xbrad = xres/ncols/2 + 1;
454     ybrad = yres/nrows/2 + 1;
455     if (rad > FTINY) {
456 greg 1.1 if (nrows >= yres && ncols >= xres)
457     rad *= (y_r + x_c)/2.0;
458    
459 greg 2.9 if (thresh > FTINY) {
460     orad = CHECKRAD*THRESHRAD*rad + 1;
461 greg 2.16 xrad = orad/x_c + xbrad;
462     yrad = orad/y_r + ybrad;
463 greg 2.9 obarsize = 2*orad + 1;
464     } else {
465     xrad = CHECKRAD*rad/x_c + 1;
466     yrad = CHECKRAD*rad/y_r + 1;
467     }
468 greg 1.1 initmask(); /* initialize filter table */
469 greg 2.9 } else {
470     xrad = xbrad;
471     yrad = ybrad;
472 greg 1.1 }
473 greg 1.17 barsize = 2*yrad + 1;
474 greg 1.1 scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
475 greg 2.10 if (scanin == NULL)
476     goto memerr;
477 greg 1.1 for (i = 0; i < barsize; i++) {
478     scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
479 greg 2.10 if (scanin[i] == NULL)
480     goto memerr;
481 greg 1.1 }
482 greg 2.9 if (obarsize > 0) {
483     scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *));
484     greybar = (float **)malloc(obarsize*sizeof(float *));
485 greg 2.10 if (scoutbar == NULL | greybar == NULL)
486     goto memerr;
487 greg 2.9 for (i = 0; i < obarsize; i++) {
488     scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR));
489     greybar[i] = (float *)malloc(ncols*sizeof(float));
490 greg 2.10 if (scoutbar[i] == NULL | greybar[i] == NULL)
491     goto memerr;
492 greg 2.9 }
493     } else {
494     scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
495 greg 2.10 if (scanout == NULL)
496     goto memerr;
497 greg 1.1 }
498 greg 1.15 /* record pixel aspect ratio */
499 greg 1.12 if (!correctaspect) {
500 greg 1.19 d = order & YMAJOR ? x_c/y_r : y_r/x_c ;
501 greg 1.15 if (!FEQ(d,1.0))
502 greg 1.12 fputaspect(d, stdout);
503     }
504 greg 1.15 /* record exposure */
505 greg 2.15 d = (*ourbright)(exposure);
506 greg 1.15 if (!FEQ(d,1.0))
507 greg 1.9 fputexpos(d, stdout);
508 greg 1.15 /* 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 greg 1.1 printf("\n");
515 greg 1.15 /* write out resolution */
516 greg 1.19 fputresolu(order, ncols, nrows, stdout);
517 greg 2.10 return;
518     memerr:
519     fprintf(stderr, "%s: out of memory\n", progname);
520     quit(1);
521 greg 2.9 }
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 greg 2.16 while (nextrow <= r+orad && nextrow < nrows) {
533 greg 2.19 ybot = (nextrow+.5)*yres/nrows;
534 greg 2.9 for (c = 0; c < ncols; c++) {
535 greg 2.19 dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow);
536 greg 2.15 greybar[nextrow%obarsize][c] = (*ourbright)(ctmp);
537 greg 2.9 }
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 greg 2.18 quit(1);
560 greg 2.9 }
561 greg 1.1 }
562    
563    
564 greg 2.21 void
565 greg 1.1 quit(code) /* remove temporary file and exit */
566     int code;
567     {
568     if (tfname != NULL)
569     unlink(tfname);
570     exit(code);
571     }