ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.28
Committed: Fri Jan 2 12:47:01 2004 UTC (20 years, 4 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.27: +8 -5 lines
Log Message:
Fixed typing/prototype of getheader() and its callback.

File Contents

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