1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
8 |
|
* pfilt.c - program to post-process picture file. |
9 |
|
* |
10 |
|
* 9/26/85 |
11 |
+ |
* 6/23/93 Added additional buffers for value spreading |
12 |
|
*/ |
13 |
|
|
14 |
< |
#include <stdio.h> |
14 |
> |
#include "standard.h" |
15 |
|
|
16 |
|
#include <signal.h> |
17 |
|
|
18 |
|
#include "color.h" |
19 |
|
|
20 |
< |
extern char *malloc(); |
20 |
> |
#include "view.h" |
21 |
|
|
22 |
< |
#define CHECKRAD 1.5 /* radius to check for filtering */ |
22 |
> |
#include "resolu.h" |
23 |
|
|
24 |
+ |
#include "paths.h" |
25 |
+ |
|
26 |
+ |
extern float *matchlamp(); |
27 |
+ |
|
28 |
+ |
#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) |
29 |
+ |
|
30 |
+ |
double CHECKRAD = 1.5; /* radius to check for filtering */ |
31 |
+ |
|
32 |
+ |
#define THRESHRAD 5.0 /* maximum sample spread in output */ |
33 |
+ |
|
34 |
|
COLOR exposure = WHTCOLOR; /* exposure for the frame */ |
35 |
|
|
36 |
< |
double rad = 0.0; /* output pixel radius for filtering */ |
36 |
> |
double rad = 0.0; /* output pixel radius for filtering */ |
37 |
|
|
38 |
+ |
double thresh = 0.0; /* maximum contribution for subpixel */ |
39 |
+ |
|
40 |
|
int nrows = 0; /* number of rows for output */ |
41 |
|
int ncols = 0; /* number of columns for output */ |
42 |
|
|
43 |
< |
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 */ |
43 |
> |
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 |
|
|
46 |
|
int singlepass = 0; /* true means skip first pass */ |
47 |
|
|
48 |
|
int avghot = 0; /* true means average in bright spots */ |
49 |
|
|
50 |
< |
double hotlvl = 1000.0; /* level considered "hot" */ |
50 |
> |
double hotlvl = 100.0; /* level considered "hot" */ |
51 |
|
|
52 |
|
int npts = 0; /* (half) number of points for stars */ |
53 |
|
|
54 |
< |
double spread = 1e-4; /* spread for star points */ |
54 |
> |
double spread = 1e-4; /* spread for star points */ |
55 |
|
|
43 |
– |
#define TEMPLATE "/usr/tmp/pfXXXXXX" |
44 |
– |
|
56 |
|
char *tfname = NULL; |
57 |
|
|
58 |
+ |
char template[] = TEMPLATE; |
59 |
+ |
|
60 |
+ |
char *lampdat = "lamp.tab"; /* lamp data file */ |
61 |
+ |
|
62 |
+ |
int order; /* scanline ordering of input */ |
63 |
|
int xres, yres; /* resolution of input */ |
64 |
< |
double inpaspect = 1.0; /* pixel aspect ratio of input */ |
64 |
> |
double inpaspect = 1.0; /* pixel aspect ratio of input */ |
65 |
|
int correctaspect = 0; /* aspect ratio correction? */ |
66 |
|
|
67 |
< |
int xrad; /* x window size */ |
52 |
< |
int yrad; /* y window size */ |
67 |
> |
int wrongformat = 0; |
68 |
|
|
69 |
+ |
VIEW ourview = STDVIEW; |
70 |
+ |
int gotview = 0; |
71 |
+ |
int wrapfilt = 0; /* wrap filter horizontally? */ |
72 |
+ |
|
73 |
+ |
int estatus = 0; /* exit status (for non-fatal errors) */ |
74 |
+ |
|
75 |
+ |
int xrad; /* x search radius */ |
76 |
+ |
int yrad; /* y search radius */ |
77 |
+ |
int xbrad; /* x box size */ |
78 |
+ |
int ybrad; /* y box size */ |
79 |
+ |
|
80 |
|
int barsize; /* size of input scan bar */ |
81 |
|
COLOR **scanin; /* input scan bar */ |
82 |
|
COLOR *scanout; /* output scan line */ |
83 |
+ |
COLOR **scoutbar; /* output scan bar (if thresh > 0) */ |
84 |
+ |
float **greybar; /* grey-averaged input values */ |
85 |
+ |
int obarsize = 0; /* size of output scan bar */ |
86 |
+ |
int orad = 0; /* output window radius */ |
87 |
|
|
88 |
|
char *progname; |
89 |
|
|
92 |
|
int argc; |
93 |
|
char **argv; |
94 |
|
{ |
65 |
– |
extern char *mktemp(); |
66 |
– |
extern double atof(), pow(); |
95 |
|
extern long ftell(); |
96 |
|
extern int quit(), headline(); |
97 |
|
FILE *fin; |
98 |
+ |
float *lampcolor; |
99 |
+ |
char *lamptype = NULL; |
100 |
|
long fpos; |
101 |
< |
double outaspect = 0.0; |
102 |
< |
double d; |
103 |
< |
int i; |
104 |
< |
|
101 |
> |
double outaspect = 0.0; |
102 |
> |
double d; |
103 |
> |
int i, j; |
104 |
> |
#ifdef MSDOS |
105 |
> |
extern int _fmode; |
106 |
> |
_fmode = O_BINARY; |
107 |
> |
setmode(fileno(stdin), O_BINARY); |
108 |
> |
setmode(fileno(stdout), O_BINARY); |
109 |
> |
#endif |
110 |
|
if (signal(SIGINT, quit) == SIG_IGN) |
111 |
|
signal(SIGINT, SIG_IGN); |
112 |
|
if (signal(SIGHUP, quit) == SIG_IGN) |
113 |
< |
signal(SIGINT, SIG_IGN); |
113 |
> |
signal(SIGHUP, SIG_IGN); |
114 |
|
signal(SIGTERM, quit); |
115 |
|
signal(SIGPIPE, quit); |
116 |
< |
#ifdef SIGXCPU |
116 |
> |
#ifdef SIGXCPU |
117 |
|
signal(SIGXCPU, quit); |
118 |
|
signal(SIGXFSZ, quit); |
119 |
|
#endif |
120 |
|
|
121 |
< |
progname = argv[0]; |
121 |
> |
progname = argv[0] = fixargv0(argv[0]); |
122 |
|
|
123 |
|
for (i = 1; i < argc; i++) |
124 |
|
if (argv[i][0] == '-') |
151 |
|
d = pow(2.0, atof(argv[i+1])); |
152 |
|
else |
153 |
|
d = atof(argv[i+1]); |
154 |
+ |
if (d < 1e-20 || d > 1e20) { |
155 |
+ |
fprintf(stderr, |
156 |
+ |
"%s: exposure out of range\n", |
157 |
+ |
argv[0]); |
158 |
+ |
quit(1); |
159 |
+ |
} |
160 |
|
switch (argv[i][2]) { |
161 |
|
case '\0': |
162 |
|
scalecolor(exposure, d); |
175 |
|
} |
176 |
|
i++; |
177 |
|
break; |
178 |
+ |
case 'f': |
179 |
+ |
lampdat = argv[++i]; |
180 |
+ |
break; |
181 |
+ |
case 't': |
182 |
+ |
lamptype = argv[++i]; |
183 |
+ |
break; |
184 |
|
case '1': |
185 |
|
singlepass = 1; |
186 |
|
break; |
202 |
|
case 'r': |
203 |
|
rad = atof(argv[++i]); |
204 |
|
break; |
205 |
+ |
case 'm': |
206 |
+ |
thresh = atof(argv[++i]); |
207 |
+ |
if (rad <= FTINY) |
208 |
+ |
rad = 1.0; |
209 |
+ |
break; |
210 |
|
case 'b': |
211 |
< |
rad = 0.0; |
211 |
> |
rad = thresh = 0.0; |
212 |
|
break; |
213 |
|
default:; |
214 |
|
badopt: |
219 |
|
} |
220 |
|
else |
221 |
|
break; |
222 |
< |
|
222 |
> |
/* get lamp data (if necessary) */ |
223 |
> |
if (lamptype != NULL) { |
224 |
> |
if (loadlamps(lampdat) < 0) |
225 |
> |
quit(1); |
226 |
> |
if ((lampcolor = matchlamp(lamptype)) == NULL) { |
227 |
> |
fprintf(stderr, "%s: unknown lamp type\n", lamptype); |
228 |
> |
quit(1); |
229 |
> |
} |
230 |
> |
for (j = 0; j < 3; j++) |
231 |
> |
if (lampcolor[j] > 1e-4) |
232 |
> |
colval(exposure,j) /= lampcolor[j]; |
233 |
> |
freelamps(); |
234 |
> |
} |
235 |
> |
/* open input file */ |
236 |
|
if (i == argc) { |
237 |
|
if (singlepass) |
238 |
|
fin = stdin; |
239 |
|
else { |
240 |
< |
tfname = mktemp(TEMPLATE); |
240 |
> |
tfname = mktemp(template); |
241 |
|
if ((fin = fopen(tfname, "w+")) == NULL) { |
242 |
|
fprintf(stderr, "%s: can't create ", progname); |
243 |
|
fprintf(stderr, "temp file \"%s\"\n", tfname); |
260 |
|
quit(1); |
261 |
|
} |
262 |
|
/* get header */ |
263 |
< |
getheader(fin, headline); |
263 |
> |
getheader(fin, headline, NULL); |
264 |
> |
if (wrongformat) { |
265 |
> |
fprintf(stderr, "%s: input must be a Radiance picture\n", |
266 |
> |
progname); |
267 |
> |
quit(1); |
268 |
> |
} |
269 |
|
/* add new header info. */ |
270 |
|
printargs(i, argv, stdout); |
271 |
|
/* get picture size */ |
272 |
< |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
272 |
> |
if ((order = fgetresolu(&xres, &yres, fin)) < 0) { |
273 |
|
fprintf(stderr, "%s: bad picture size\n", progname); |
274 |
|
quit(1); |
275 |
|
} |
276 |
+ |
if (!(order & YMAJOR)) |
277 |
+ |
inpaspect = 1.0/inpaspect; |
278 |
+ |
/* wrap around for cylindrical view? */ |
279 |
+ |
wrapfilt = gotview && ourview.type == VT_CYL && |
280 |
+ |
ourview.horiz >= 360.-FTINY && order & YMAJOR; |
281 |
|
/* compute output resolution */ |
282 |
|
if (ncols <= 0) |
283 |
|
ncols = x_c*xres + .5; |
309 |
|
} |
310 |
|
pass2(fin); |
311 |
|
|
312 |
< |
quit(0); |
312 |
> |
quit(estatus); |
313 |
|
} |
314 |
|
|
315 |
|
|
316 |
+ |
double |
317 |
+ |
rgb_bright(clr) |
318 |
+ |
COLOR clr; |
319 |
+ |
{ |
320 |
+ |
return(bright(clr)); |
321 |
+ |
} |
322 |
+ |
|
323 |
+ |
|
324 |
+ |
double |
325 |
+ |
xyz_bright(clr) |
326 |
+ |
COLOR clr; |
327 |
+ |
{ |
328 |
+ |
return(clr[CIEY]); |
329 |
+ |
} |
330 |
+ |
|
331 |
+ |
|
332 |
+ |
double (*ourbright)() = rgb_bright; |
333 |
+ |
|
334 |
+ |
|
335 |
|
headline(s) /* process line from header */ |
336 |
|
char *s; |
337 |
|
{ |
338 |
+ |
char fmt[32]; |
339 |
+ |
|
340 |
|
fputs(s, stdout); /* copy to output */ |
341 |
|
if (isaspect(s)) /* get aspect ratio */ |
342 |
|
inpaspect *= aspectval(s); |
343 |
+ |
else if (isexpos(s)) /* get exposure */ |
344 |
+ |
hotlvl *= exposval(s); |
345 |
+ |
else if (formatval(fmt, s)) { /* get format */ |
346 |
+ |
wrongformat = 0; |
347 |
+ |
if (!strcmp(COLRFMT, fmt)) |
348 |
+ |
ourbright = rgb_bright; |
349 |
+ |
else if (!strcmp(CIEFMT, fmt)) |
350 |
+ |
ourbright = xyz_bright; |
351 |
+ |
else |
352 |
+ |
wrongformat = !globmatch(PICFMT, fmt); |
353 |
+ |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
354 |
+ |
gotview++; |
355 |
|
} |
356 |
|
|
357 |
|
|
385 |
|
} |
386 |
|
for (i = 0; i < yres; i++) { |
387 |
|
if (freadscan(scan, xres, in) < 0) { |
388 |
< |
nrows = nrows * i / yres; /* adjust frame */ |
388 |
> |
nrows = (long)nrows * i / yres; /* adjust frame */ |
389 |
|
if (nrows <= 0) { |
390 |
|
fprintf(stderr, "%s: empty frame\n", progname); |
391 |
|
quit(1); |
392 |
|
} |
393 |
|
fprintf(stderr, "%s: warning - partial frame (%d%%)\n", |
394 |
< |
progname, 100*i/yres); |
394 |
> |
progname, (int)(100L*i/yres)); |
395 |
|
yres = i; |
396 |
|
y_r = (double)nrows/yres; |
397 |
+ |
estatus++; |
398 |
|
break; |
399 |
|
} |
400 |
|
pass1scan(scan, i); |
409 |
|
int yread; |
410 |
|
int ycent, xcent; |
411 |
|
int r, c; |
412 |
< |
|
412 |
> |
|
413 |
|
pass2init(); |
414 |
|
scan2init(); |
415 |
|
yread = 0; |
416 |
|
for (r = 0; r < nrows; r++) { |
417 |
< |
ycent = (long)r*yres/nrows; |
417 |
> |
ycent = (r+.5)*yres/nrows; |
418 |
|
while (yread <= ycent+yrad) { |
419 |
|
if (yread < yres) { |
420 |
|
if (freadscan(scanin[yread%barsize], |
421 |
|
xres, in) < 0) { |
422 |
|
fprintf(stderr, |
423 |
< |
"%s: bad read (y=%d)\n", |
423 |
> |
"%s: truncated input (y=%d)\n", |
424 |
|
progname, yres-1-yread); |
425 |
|
quit(1); |
426 |
|
} |
428 |
|
} |
429 |
|
yread++; |
430 |
|
} |
431 |
+ |
if (obarsize > 0) |
432 |
+ |
scan2sync(r); |
433 |
|
for (c = 0; c < ncols; c++) { |
434 |
< |
xcent = (long)c*xres/ncols; |
435 |
< |
if (rad <= 0.0) |
436 |
< |
dobox(scanout[c], xcent, ycent, c, r); |
437 |
< |
else |
434 |
> |
xcent = (c+.5)*xres/ncols; |
435 |
> |
if (thresh > FTINY) |
436 |
> |
dothresh(xcent, ycent, c, r); |
437 |
> |
else if (rad > FTINY) |
438 |
|
dogauss(scanout[c], xcent, ycent, c, r); |
439 |
+ |
else |
440 |
+ |
dobox(scanout[c], xcent, ycent, c, r); |
441 |
|
} |
442 |
< |
if (fwritescan(scanout, ncols, stdout) < 0) { |
442 |
> |
if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) { |
443 |
|
fprintf(stderr, "%s: write error in pass2\n", progname); |
444 |
|
quit(1); |
445 |
|
} |
446 |
|
} |
447 |
< |
/* skip leftovers */ |
447 |
> |
/* skip leftover input */ |
448 |
|
while (yread < yres) { |
449 |
|
if (freadscan(scanin[0], xres, in) < 0) |
450 |
|
break; |
451 |
|
yread++; |
452 |
|
} |
453 |
+ |
scan2flush(); /* flush output */ |
454 |
|
} |
455 |
|
|
456 |
|
|
457 |
|
scan2init() /* prepare scanline arrays */ |
458 |
|
{ |
459 |
< |
double d; |
459 |
> |
COLOR ctmp; |
460 |
> |
double d; |
461 |
|
register int i; |
462 |
|
|
463 |
< |
if (rad <= 0.0) { |
464 |
< |
xrad = xres/ncols/2 + 1; |
465 |
< |
yrad = yres/nrows/2 + 1; |
351 |
< |
} else { |
463 |
> |
xbrad = xres/ncols/2 + 1; |
464 |
> |
ybrad = yres/nrows/2 + 1; |
465 |
> |
if (rad > FTINY) { |
466 |
|
if (nrows >= yres && ncols >= xres) |
467 |
|
rad *= (y_r + x_c)/2.0; |
468 |
|
|
469 |
< |
xrad = CHECKRAD*rad/x_c + 1; |
470 |
< |
yrad = CHECKRAD*rad/y_r + 1; |
471 |
< |
|
469 |
> |
if (thresh > FTINY) { |
470 |
> |
orad = CHECKRAD*THRESHRAD*rad + 1; |
471 |
> |
xrad = orad/x_c + xbrad; |
472 |
> |
yrad = orad/y_r + ybrad; |
473 |
> |
obarsize = 2*orad + 1; |
474 |
> |
} else { |
475 |
> |
xrad = CHECKRAD*rad/x_c + 1; |
476 |
> |
yrad = CHECKRAD*rad/y_r + 1; |
477 |
> |
} |
478 |
|
initmask(); /* initialize filter table */ |
479 |
+ |
} else { |
480 |
+ |
xrad = xbrad; |
481 |
+ |
yrad = ybrad; |
482 |
|
} |
483 |
< |
barsize = 2 * yrad; |
483 |
> |
barsize = 2*yrad + 1; |
484 |
|
scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); |
485 |
+ |
if (scanin == NULL) |
486 |
+ |
goto memerr; |
487 |
|
for (i = 0; i < barsize; i++) { |
488 |
|
scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
489 |
< |
if (scanin[i] == NULL) { |
490 |
< |
fprintf(stderr, "%s: out of memory\n", progname); |
491 |
< |
quit(1); |
489 |
> |
if (scanin[i] == NULL) |
490 |
> |
goto memerr; |
491 |
> |
} |
492 |
> |
if (obarsize > 0) { |
493 |
> |
scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *)); |
494 |
> |
greybar = (float **)malloc(obarsize*sizeof(float *)); |
495 |
> |
if (scoutbar == NULL | greybar == NULL) |
496 |
> |
goto memerr; |
497 |
> |
for (i = 0; i < obarsize; i++) { |
498 |
> |
scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR)); |
499 |
> |
greybar[i] = (float *)malloc(ncols*sizeof(float)); |
500 |
> |
if (scoutbar[i] == NULL | greybar[i] == NULL) |
501 |
> |
goto memerr; |
502 |
|
} |
503 |
+ |
} else { |
504 |
+ |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
505 |
+ |
if (scanout == NULL) |
506 |
+ |
goto memerr; |
507 |
|
} |
508 |
< |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
370 |
< |
if (scanout == NULL) { |
371 |
< |
fprintf(stderr, "%s: out of memory\n", progname); |
372 |
< |
quit(1); |
373 |
< |
} |
374 |
< |
/* record pixel aspect and exposure */ |
508 |
> |
/* record pixel aspect ratio */ |
509 |
|
if (!correctaspect) { |
510 |
< |
d = x_c / y_r; |
511 |
< |
if (d < .99 || d > 1.01) |
510 |
> |
d = order & YMAJOR ? x_c/y_r : y_r/x_c ; |
511 |
> |
if (!FEQ(d,1.0)) |
512 |
|
fputaspect(d, stdout); |
513 |
|
} |
514 |
< |
d = bright(exposure); |
515 |
< |
if (d < .995 || d > 1.005) |
514 |
> |
/* record exposure */ |
515 |
> |
d = (*ourbright)(exposure); |
516 |
> |
if (!FEQ(d,1.0)) |
517 |
|
fputexpos(d, stdout); |
518 |
+ |
/* record color correction */ |
519 |
+ |
copycolor(ctmp, exposure); |
520 |
+ |
scalecolor(ctmp, 1.0/d); |
521 |
+ |
if (!FEQ(colval(ctmp,RED),colval(ctmp,GRN)) || |
522 |
+ |
!FEQ(colval(ctmp,GRN),colval(ctmp,BLU))) |
523 |
+ |
fputcolcor(ctmp, stdout); |
524 |
|
printf("\n"); |
525 |
< |
fputresolu(YMAJOR|YDECR, ncols, nrows, stdout); /* resolution */ |
525 |
> |
/* write out resolution */ |
526 |
> |
fputresolu(order, ncols, nrows, stdout); |
527 |
> |
return; |
528 |
> |
memerr: |
529 |
> |
fprintf(stderr, "%s: out of memory\n", progname); |
530 |
> |
quit(1); |
531 |
> |
} |
532 |
> |
|
533 |
> |
|
534 |
> |
scan2sync(r) /* synchronize grey averages and output scan */ |
535 |
> |
int r; |
536 |
> |
{ |
537 |
> |
static int nextrow = 0; |
538 |
> |
COLOR ctmp; |
539 |
> |
int ybot; |
540 |
> |
register int c; |
541 |
> |
/* average input scanlines */ |
542 |
> |
while (nextrow <= r+orad && nextrow < nrows) { |
543 |
> |
ybot = (nextrow+.5)*yres/nrows; |
544 |
> |
for (c = 0; c < ncols; c++) { |
545 |
> |
dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow); |
546 |
> |
greybar[nextrow%obarsize][c] = (*ourbright)(ctmp); |
547 |
> |
} |
548 |
> |
/* and zero output scanline */ |
549 |
> |
bzero((char *)scoutbar[nextrow%obarsize], ncols*sizeof(COLOR)); |
550 |
> |
nextrow++; |
551 |
> |
} |
552 |
> |
/* point to top scanline for output */ |
553 |
> |
if (r-orad >= 0) |
554 |
> |
scanout = scoutbar[(r-orad)%obarsize]; |
555 |
> |
else |
556 |
> |
scanout = NULL; |
557 |
> |
} |
558 |
> |
|
559 |
> |
|
560 |
> |
scan2flush() /* flush output buffer */ |
561 |
> |
{ |
562 |
> |
register int r; |
563 |
> |
|
564 |
> |
for (r = nrows-orad; r < nrows; r++) |
565 |
> |
if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0) |
566 |
> |
break; |
567 |
> |
if (fflush(stdout) < 0) { |
568 |
> |
fprintf(stderr, "%s: write error at end of pass2\n", progname); |
569 |
> |
quit(1); |
570 |
> |
} |
571 |
|
} |
572 |
|
|
573 |
|
|