ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.17
Committed: Mon Apr 1 17:24:44 1996 UTC (28 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.16: +11 -1 lines
Log Message:
added filter wrapping for 360 degree cylindrical views

File Contents

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