ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 1.3
Committed: Tue Apr 11 21:39:00 1989 UTC (35 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +8 -6 lines
Log Message:
changed syntax for resolution ratios

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    
20     extern char *malloc();
21    
22     #define CHECKRAD 1.5 /* radius to check for filtering */
23    
24     COLOR exposure = WHTCOLOR; /* exposure for the frame */
25    
26     double rad = 0.0; /* output pixel radius for filtering */
27    
28     int nrows = 0; /* number of rows for output */
29     int ncols = 0; /* number of columns for output */
30    
31 greg 1.2 double xrat = 1.0; /* ratio of input x size to output */
32     double yrat = 1.0; /* ratio of input y size to output */
33    
34 greg 1.1 int singlepass = 0; /* true means skip first pass */
35    
36     int avghot = 0; /* true means average in bright spots */
37    
38     double hotlvl = 1000.0; /* level considered "hot" */
39    
40     int npts = 0; /* (half) number of points for stars */
41    
42     double spread = 1e-4; /* spread for star points */
43    
44     #define TEMPLATE "/usr/tmp/pfXXXXXX"
45    
46     char *tfname = NULL;
47    
48     int xres, yres; /* resolution of input */
49    
50     double x_c, y_r; /* conversion factors */
51    
52     int xrad; /* x window size */
53     int yrad; /* y window size */
54    
55     int barsize; /* size of input scan bar */
56     COLOR **scanin; /* input scan bar */
57     COLOR *scanout; /* output scan line */
58    
59     char *progname;
60    
61    
62     main(argc, argv)
63     int argc;
64     char **argv;
65     {
66     extern char *mktemp();
67     extern double atof(), pow();
68     extern long ftell();
69     extern int quit();
70     FILE *fin;
71     long fpos;
72     double d;
73     int i;
74    
75     if (signal(SIGINT, quit) == SIG_IGN)
76     signal(SIGINT, SIG_IGN);
77     if (signal(SIGHUP, quit) == SIG_IGN)
78     signal(SIGINT, SIG_IGN);
79     signal(SIGTERM, quit);
80     signal(SIGPIPE, quit);
81     #ifdef SIGXCPU
82     signal(SIGXCPU, quit);
83     signal(SIGXFSZ, quit);
84     #endif
85    
86     progname = argv[0];
87    
88     for (i = 1; i < argc; i++)
89     if (argv[i][0] == '-')
90     switch (argv[i][1]) {
91     case 'x':
92 greg 1.3 i++;
93     if (argv[i][0] == '/')
94     xrat = atof(argv[i]+1);
95 greg 1.2 else
96 greg 1.3 ncols = atoi(argv[i]);
97 greg 1.1 break;
98     case 'y':
99 greg 1.3 i++;
100     if (argv[i][0] == '/')
101     yrat = atof(argv[i]+1);
102 greg 1.2 else
103 greg 1.3 nrows = atoi(argv[i]);
104 greg 1.1 break;
105     case 'e':
106     if (argv[i+1][0] == '+' || argv[i+1][0] == '-')
107     d = pow(2.0, atof(argv[i+1]));
108     else
109     d = atof(argv[i+1]);
110     switch (argv[i][2]) {
111     case '\0':
112     scalecolor(exposure, d);
113     break;
114     case 'r':
115     colval(exposure,RED) *= d;
116     break;
117     case 'g':
118     colval(exposure,GRN) *= d;
119     break;
120     case 'b':
121     colval(exposure,BLU) *= d;
122     break;
123     default:
124     goto badopt;
125     }
126     i++;
127     break;
128     case '1':
129     singlepass = 1;
130     break;
131     case 'p':
132     npts = atoi(argv[++i]) / 2;
133     break;
134     case 's':
135     spread = atof(argv[++i]);
136     break;
137     case 'a':
138     avghot = !avghot;
139     break;
140     case 'h':
141     hotlvl = atof(argv[++i]);
142     break;
143     case 'r':
144     rad = atof(argv[++i]);
145     break;
146     case 'b':
147     rad = 0.0;
148     break;
149     default:;
150     badopt:
151     fprintf(stderr, "%s: unknown option: %s\n",
152     progname, argv[i]);
153     quit(1);
154     break;
155     }
156     else
157     break;
158    
159     if (i == argc) {
160     if (singlepass)
161     fin = stdin;
162     else {
163     tfname = mktemp(TEMPLATE);
164     if ((fin = fopen(tfname, "w+")) == NULL) {
165     fprintf(stderr, "%s: can't create ", progname);
166     fprintf(stderr, "temp file \"%s\"\n", tfname);
167     quit(1);
168     }
169     copyfile(stdin, fin);
170     if (fseek(fin, 0L, 0) == -1) {
171     fprintf(stderr, "%s: seek fail\n", progname);
172     quit(1);
173     }
174     }
175     } else if (i == argc-1) {
176     if ((fin = fopen(argv[i], "r")) == NULL) {
177     fprintf(stderr, "%s: can't open file \"%s\"\n",
178     progname, argv[i]);
179     quit(1);
180     }
181     } else {
182     fprintf(stderr, "%s: bad # file arguments\n", progname);
183     quit(1);
184     }
185     /* copy header */
186     copyheader(fin, stdout);
187     /* add new header info. */
188     printargs(i, argv, stdout);
189     /* get picture size */
190     if (fscanf(fin, "-Y %d +X %d\n", &yres, &xres) != 2) {
191     fprintf(stderr, "%s: bad picture size\n", progname);
192     quit(1);
193     }
194 greg 1.2 if (ncols > 0)
195     xrat = (double)xres/ncols;
196     else
197     ncols = xres/xrat + .5;
198     if (nrows > 0)
199     yrat = (double)yres/nrows;
200     else
201     nrows = yres/yrat + .5;
202 greg 1.1
203     if (singlepass) {
204     /* skip exposure, etc. */
205     pass1default();
206     pass2(fin);
207     quit(0);
208     }
209    
210     fpos = ftell(fin); /* save input file position */
211    
212     pass1(fin);
213    
214     if (fseek(fin, fpos, 0) == -1) {
215     fprintf(stderr, "%s: seek fail\n", progname);
216     quit(1);
217     }
218     pass2(fin);
219    
220     quit(0);
221     }
222    
223    
224     copyfile(in, out) /* copy a file */
225     register FILE *in, *out;
226     {
227     register int c;
228    
229     while ((c = getc(in)) != EOF)
230     putc(c, out);
231    
232     if (ferror(out)) {
233     fprintf(stderr, "%s: write error in copyfile\n", progname);
234     quit(1);
235     }
236     }
237    
238    
239     pass1(in) /* first pass of picture file */
240     FILE *in;
241     {
242     int i;
243     COLOR *scan;
244    
245     pass1init();
246    
247     scan = (COLOR *)malloc(xres*sizeof(COLOR));
248     if (scan == NULL) {
249     fprintf(stderr, "%s: out of memory\n", progname);
250     quit(1);
251     }
252     for (i = 0; i < yres; i++) {
253     if (freadscan(scan, xres, in) < 0) {
254     nrows = nrows * i / yres; /* adjust frame */
255     if (nrows <= 0) {
256     fprintf(stderr, "%s: empty frame\n", progname);
257     quit(1);
258     }
259     fprintf(stderr, "%s: warning - partial frame (%d%%)\n",
260     progname, 100*i/yres);
261     yres = i;
262     break;
263     }
264     pass1scan(scan, i);
265     }
266     free((char *)scan);
267     }
268    
269    
270     pass2(in) /* last pass on file, write to stdout */
271     FILE *in;
272     {
273     int yread;
274     int ycent, xcent;
275     int r, c;
276    
277     pass2init();
278     scan2init();
279     yread = 0;
280     for (r = 0; r < nrows; r++) {
281     ycent = (long)r*yres/nrows;
282     while (yread <= ycent+yrad) {
283     if (yread < yres) {
284     if (freadscan(scanin[yread%barsize],
285     xres, in) < 0) {
286     fprintf(stderr,
287     "%s: bad read (y=%d)\n",
288     progname, yres-1-yread);
289     quit(1);
290     }
291     pass2scan(scanin[yread%barsize], yread);
292     }
293     yread++;
294     }
295     for (c = 0; c < ncols; c++) {
296     xcent = (long)c*xres/ncols;
297     if (rad <= 0.0)
298     dobox(scanout[c], xcent, ycent, c, r);
299     else
300     dogauss(scanout[c], xcent, ycent, c, r);
301     }
302     if (fwritescan(scanout, ncols, stdout) < 0) {
303     fprintf(stderr, "%s: write error in pass2\n", progname);
304     quit(1);
305     }
306     }
307     }
308    
309    
310     scan2init() /* prepare scanline arrays */
311     {
312     double e;
313     register int i;
314    
315     x_c = (double)ncols/xres;
316     y_r = (double)nrows/yres;
317    
318     if (rad <= 0.0) {
319     xrad = xres/ncols/2 + 1;
320     yrad = yres/nrows/2 + 1;
321     } else {
322     if (nrows >= yres && ncols >= xres)
323     rad *= (y_r + x_c)/2.0;
324    
325     xrad = CHECKRAD*rad/x_c + 1;
326     yrad = CHECKRAD*rad/y_r + 1;
327    
328     initmask(); /* initialize filter table */
329     }
330     barsize = 2 * yrad;
331     scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
332     for (i = 0; i < barsize; i++) {
333     scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
334     if (scanin[i] == NULL) {
335     fprintf(stderr, "%s: out of memory\n", progname);
336     quit(1);
337     }
338     }
339     scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
340     if (scanout == NULL) {
341     fprintf(stderr, "%s: out of memory\n", progname);
342     quit(1);
343     }
344     e = bright(exposure);
345     if (e < 1-1e-7 || e > 1+1e-7) /* record exposure */
346     printf("EXPOSURE=%e\n", e);
347     printf("\n");
348     printf("-Y %d +X %d\n", nrows, ncols); /* write picture size */
349     }
350    
351    
352     quit(code) /* remove temporary file and exit */
353     int code;
354     {
355     if (tfname != NULL)
356     unlink(tfname);
357     exit(code);
358     }