ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 2.4
Committed: Mon Sep 21 12:14:17 1992 UTC (31 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +24 -15 lines
Log Message:
Changes for PC port

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