ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 1.10
Committed: Thu Sep 13 09:46:00 1990 UTC (33 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +6 -0 lines
Log Message:
fixed pass2() so it reads all its input regardless

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