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