ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 1.16
Committed: Thu Apr 18 14:35:21 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.15: +14 -1 lines
Log Message:
added format information to headers

File Contents

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