ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.19
Committed: Wed Oct 2 16:56:16 1996 UTC (27 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.18: +4 -4 lines
Log Message:
made output pixel positioning more precise

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