ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.9
Committed: Fri Jun 25 15:01:17 1993 UTC (30 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +103 -20 lines
Log Message:
added -m option for sample smoothing

File Contents

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