ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.12
Committed: Thu Nov 18 09:56:17 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.11: +1 -1 lines
Log Message:
minor compiler warning fixes

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