ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pfilt.c
Revision: 1.11
Committed: Tue Oct 16 13:52:44 1990 UTC (33 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.10: +1 -0 lines
Log Message:
recomputed y_r after picture adjustment (bug fix)

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 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 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
33 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 double inpaspect = 1.0; /* pixel aspect ratio of input */
49
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 extern int quit(), headline();
68 FILE *fin;
69 long fpos;
70 double outaspect = 0.0;
71 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 i++;
92 if (argv[i][0] == '/') {
93 x_c = 1.0/atof(argv[i]+1);
94 ncols = 0;
95 } else
96 ncols = atoi(argv[i]);
97 break;
98 case 'y':
99 i++;
100 if (argv[i][0] == '/') {
101 y_r = 1.0/atof(argv[i]+1);
102 nrows = 0;
103 } else
104 nrows = atoi(argv[i]);
105 break;
106 case 'p':
107 i++;
108 outaspect = atof(argv[i]);
109 break;
110 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 case '2':
137 singlepass = 0;
138 break;
139 case 'n':
140 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 /* get header */
194 getheader(fin, headline);
195 /* add new header info. */
196 printargs(i, argv, stdout);
197 /* get picture size */
198 if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) {
199 fprintf(stderr, "%s: bad picture size\n", progname);
200 quit(1);
201 }
202 /* compute output resolution */
203 if (ncols <= 0)
204 ncols = x_c*xres + .5;
205 if (nrows <= 0)
206 nrows = y_r*yres + .5;
207 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
217 if (singlepass) { /* skip exposure, etc. */
218 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 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 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 y_r = (double)nrows/yres;
285 break;
286 }
287 pass1scan(scan, i);
288 }
289 free((char *)scan);
290 }
291
292
293 pass2(in) /* last pass on file, write to stdout */
294 FILE *in;
295 {
296 int yread;
297 int ycent, xcent;
298 int r, c;
299
300 pass2init();
301 scan2init();
302 yread = 0;
303 for (r = 0; r < nrows; r++) {
304 ycent = (long)r*yres/nrows;
305 while (yread <= ycent+yrad) {
306 if (yread < yres) {
307 if (freadscan(scanin[yread%barsize],
308 xres, in) < 0) {
309 fprintf(stderr,
310 "%s: bad read (y=%d)\n",
311 progname, yres-1-yread);
312 quit(1);
313 }
314 pass2scan(scanin[yread%barsize], yread);
315 }
316 yread++;
317 }
318 for (c = 0; c < ncols; c++) {
319 xcent = (long)c*xres/ncols;
320 if (rad <= 0.0)
321 dobox(scanout[c], xcent, ycent, c, r);
322 else
323 dogauss(scanout[c], xcent, ycent, c, r);
324 }
325 if (fwritescan(scanout, ncols, stdout) < 0) {
326 fprintf(stderr, "%s: write error in pass2\n", progname);
327 quit(1);
328 }
329 }
330 /* skip leftovers */
331 while (yread < yres) {
332 if (freadscan(scanin[0], xres, in) < 0)
333 break;
334 yread++;
335 }
336 }
337
338
339 scan2init() /* prepare scanline arrays */
340 {
341 double d;
342 register int i;
343
344 if (rad <= 0.0) {
345 xrad = xres/ncols/2 + 1;
346 yrad = yres/nrows/2 + 1;
347 } else {
348 if (nrows >= yres && ncols >= xres)
349 rad *= (y_r + x_c)/2.0;
350
351 xrad = CHECKRAD*rad/x_c + 1;
352 yrad = CHECKRAD*rad/y_r + 1;
353
354 initmask(); /* initialize filter table */
355 }
356 barsize = 2 * yrad;
357 scanin = (COLOR **)malloc(barsize*sizeof(COLOR *));
358 for (i = 0; i < barsize; i++) {
359 scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR));
360 if (scanin[i] == NULL) {
361 fprintf(stderr, "%s: out of memory\n", progname);
362 quit(1);
363 }
364 }
365 scanout = (COLOR *)malloc(ncols*sizeof(COLOR));
366 if (scanout == NULL) {
367 fprintf(stderr, "%s: out of memory\n", progname);
368 quit(1);
369 }
370 /* record pixel aspect and exposure */
371 d = x_c / y_r;
372 if (d < .99 || d > 1.01)
373 fputaspect(d, stdout);
374 d = bright(exposure);
375 if (d < .995 || d > 1.005)
376 fputexpos(d, stdout);
377 printf("\n");
378 fputresolu(YMAJOR|YDECR, ncols, nrows, stdout); /* resolution */
379 }
380
381
382 quit(code) /* remove temporary file and exit */
383 int code;
384 {
385 if (tfname != NULL)
386 unlink(tfname);
387 exit(code);
388 }