ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 1.2
Committed: Tue Apr 11 17:22:05 1989 UTC (35 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +19 -6 lines
Log Message:
added scaling options for x and y resolution

File Contents

# Content
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 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 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 if (argv[i][2] == '/')
93 xrat = atof(argv[++i]);
94 else
95 ncols = atoi(argv[++i]);
96 break;
97 case 'y':
98 if (argv[i][2] == '/')
99 yrat = atof(argv[++i]);
100 else
101 nrows = atoi(argv[++i]);
102 break;
103 case 'e':
104 if (argv[i+1][0] == '+' || argv[i+1][0] == '-')
105 d = pow(2.0, atof(argv[i+1]));
106 else
107 d = atof(argv[i+1]);
108 switch (argv[i][2]) {
109 case '\0':
110 scalecolor(exposure, d);
111 break;
112 case 'r':
113 colval(exposure,RED) *= d;
114 break;
115 case 'g':
116 colval(exposure,GRN) *= d;
117 break;
118 case 'b':
119 colval(exposure,BLU) *= d;
120 break;
121 default:
122 goto badopt;
123 }
124 i++;
125 break;
126 case '1':
127 singlepass = 1;
128 break;
129 case 'p':
130 npts = atoi(argv[++i]) / 2;
131 break;
132 case 's':
133 spread = atof(argv[++i]);
134 break;
135 case 'a':
136 avghot = !avghot;
137 break;
138 case 'h':
139 hotlvl = atof(argv[++i]);
140 break;
141 case 'r':
142 rad = atof(argv[++i]);
143 break;
144 case 'b':
145 rad = 0.0;
146 break;
147 default:;
148 badopt:
149 fprintf(stderr, "%s: unknown option: %s\n",
150 progname, argv[i]);
151 quit(1);
152 break;
153 }
154 else
155 break;
156
157 if (i == argc) {
158 if (singlepass)
159 fin = stdin;
160 else {
161 tfname = mktemp(TEMPLATE);
162 if ((fin = fopen(tfname, "w+")) == NULL) {
163 fprintf(stderr, "%s: can't create ", progname);
164 fprintf(stderr, "temp file \"%s\"\n", tfname);
165 quit(1);
166 }
167 copyfile(stdin, fin);
168 if (fseek(fin, 0L, 0) == -1) {
169 fprintf(stderr, "%s: seek fail\n", progname);
170 quit(1);
171 }
172 }
173 } else if (i == argc-1) {
174 if ((fin = fopen(argv[i], "r")) == NULL) {
175 fprintf(stderr, "%s: can't open file \"%s\"\n",
176 progname, argv[i]);
177 quit(1);
178 }
179 } else {
180 fprintf(stderr, "%s: bad # file arguments\n", progname);
181 quit(1);
182 }
183 /* copy header */
184 copyheader(fin, stdout);
185 /* add new header info. */
186 printargs(i, argv, stdout);
187 /* get picture size */
188 if (fscanf(fin, "-Y %d +X %d\n", &yres, &xres) != 2) {
189 fprintf(stderr, "%s: bad picture size\n", progname);
190 quit(1);
191 }
192 if (ncols > 0)
193 xrat = (double)xres/ncols;
194 else
195 ncols = xres/xrat + .5;
196 if (nrows > 0)
197 yrat = (double)yres/nrows;
198 else
199 nrows = yres/yrat + .5;
200
201 if (singlepass) {
202 /* skip exposure, etc. */
203 pass1default();
204 pass2(fin);
205 quit(0);
206 }
207
208 fpos = ftell(fin); /* save input file position */
209
210 pass1(fin);
211
212 if (fseek(fin, fpos, 0) == -1) {
213 fprintf(stderr, "%s: seek fail\n", progname);
214 quit(1);
215 }
216 pass2(fin);
217
218 quit(0);
219 }
220
221
222 copyfile(in, out) /* copy a file */
223 register FILE *in, *out;
224 {
225 register int c;
226
227 while ((c = getc(in)) != EOF)
228 putc(c, out);
229
230 if (ferror(out)) {
231 fprintf(stderr, "%s: write error in copyfile\n", progname);
232 quit(1);
233 }
234 }
235
236
237 pass1(in) /* first pass of picture file */
238 FILE *in;
239 {
240 int i;
241 COLOR *scan;
242
243 pass1init();
244
245 scan = (COLOR *)malloc(xres*sizeof(COLOR));
246 if (scan == NULL) {
247 fprintf(stderr, "%s: out of memory\n", progname);
248 quit(1);
249 }
250 for (i = 0; i < yres; i++) {
251 if (freadscan(scan, xres, in) < 0) {
252 nrows = nrows * i / yres; /* adjust frame */
253 if (nrows <= 0) {
254 fprintf(stderr, "%s: empty frame\n", progname);
255 quit(1);
256 }
257 fprintf(stderr, "%s: warning - partial frame (%d%%)\n",
258 progname, 100*i/yres);
259 yres = i;
260 break;
261 }
262 pass1scan(scan, i);
263 }
264 free((char *)scan);
265 }
266
267
268 pass2(in) /* last pass on file, write to stdout */
269 FILE *in;
270 {
271 int yread;
272 int ycent, xcent;
273 int r, c;
274
275 pass2init();
276 scan2init();
277 yread = 0;
278 for (r = 0; r < nrows; r++) {
279 ycent = (long)r*yres/nrows;
280 while (yread <= ycent+yrad) {
281 if (yread < yres) {
282 if (freadscan(scanin[yread%barsize],
283 xres, in) < 0) {
284 fprintf(stderr,
285 "%s: bad read (y=%d)\n",
286 progname, yres-1-yread);
287 quit(1);
288 }
289 pass2scan(scanin[yread%barsize], yread);
290 }
291 yread++;
292 }
293 for (c = 0; c < ncols; c++) {
294 xcent = (long)c*xres/ncols;
295 if (rad <= 0.0)
296 dobox(scanout[c], xcent, ycent, c, r);
297 else
298 dogauss(scanout[c], xcent, ycent, c, r);
299 }
300 if (fwritescan(scanout, ncols, stdout) < 0) {
301 fprintf(stderr, "%s: write error in pass2\n", progname);
302 quit(1);
303 }
304 }
305 }
306
307
308 scan2init() /* prepare scanline arrays */
309 {
310 double e;
311 register int i;
312
313 x_c = (double)ncols/xres;
314 y_r = (double)nrows/yres;
315
316 if (rad <= 0.0) {
317 xrad = xres/ncols/2 + 1;
318 yrad = yres/nrows/2 + 1;
319 } else {
320 if (nrows >= yres && ncols >= xres)
321 rad *= (y_r + x_c)/2.0;
322
323 xrad = CHECKRAD*rad/x_c + 1;
324 yrad = CHECKRAD*rad/y_r + 1;
325
326 initmask(); /* initialize filter table */
327 }
328 barsize = 2 * yrad;
329 scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
330 for (i = 0; i < barsize; i++) {
331 scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
332 if (scanin[i] == NULL) {
333 fprintf(stderr, "%s: out of memory\n", progname);
334 quit(1);
335 }
336 }
337 scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
338 if (scanout == NULL) {
339 fprintf(stderr, "%s: out of memory\n", progname);
340 quit(1);
341 }
342 e = bright(exposure);
343 if (e < 1-1e-7 || e > 1+1e-7) /* record exposure */
344 printf("EXPOSURE=%e\n", e);
345 printf("\n");
346 printf("-Y %d +X %d\n", nrows, ncols); /* write picture size */
347 }
348
349
350 quit(code) /* remove temporary file and exit */
351 int code;
352 {
353 if (tfname != NULL)
354 unlink(tfname);
355 exit(code);
356 }