ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.22
Committed: Tue Feb 25 00:26:05 2003 UTC (21 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.21: +2 -2 lines
Log Message:
Fixed pre-release upsampling bug in new filtering kernel

File Contents

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