ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.24
Committed: Sun Jun 8 12:03:10 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.23: +6 -1 lines
Log Message:
Reduced compile warnings/errors on Windows.

File Contents

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