ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.30
Committed: Mon Mar 29 00:34:23 2004 UTC (20 years ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9
Changes since 2.29: +6 -5 lines
Log Message:
Continued ANSIfication. Added headerfiles pfilt.h and clrtab.h.

File Contents

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