ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 1.14
Committed: Sat Dec 8 13:23:47 1990 UTC (33 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.13: +14 -14 lines
Log Message:
moved loading of lamp data before opening of input file

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