1 |
– |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* pfilt.c - program to post-process picture file. |
6 |
|
* |
7 |
|
* 9/26/85 |
8 |
+ |
* 6/23/93 Added additional buffers for value spreading |
9 |
|
*/ |
10 |
|
|
13 |
– |
#include <stdio.h> |
14 |
– |
|
11 |
|
#include <signal.h> |
12 |
|
|
13 |
+ |
#include "standard.h" |
14 |
|
#include "color.h" |
15 |
+ |
#include "view.h" |
16 |
+ |
#include "paths.h" |
17 |
|
|
19 |
– |
extern char *malloc(); |
18 |
|
extern float *matchlamp(); |
19 |
|
|
20 |
< |
#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) |
20 |
> |
#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) |
21 |
|
|
22 |
< |
#define CHECKRAD 1.5 /* radius to check for filtering */ |
22 |
> |
double CHECKRAD = 2.0; /* radius to check for filtering */ |
23 |
|
|
24 |
+ |
#define THRESHRAD 5.0 /* maximum sample spread in output */ |
25 |
+ |
|
26 |
|
COLOR exposure = WHTCOLOR; /* exposure for the frame */ |
27 |
|
|
28 |
< |
double rad = 0.0; /* output pixel radius for filtering */ |
28 |
> |
double rad = 0.0; /* output pixel radius for filtering */ |
29 |
|
|
30 |
+ |
double thresh = 0.0; /* maximum contribution for subpixel */ |
31 |
+ |
|
32 |
|
int nrows = 0; /* number of rows for output */ |
33 |
|
int ncols = 0; /* number of columns for output */ |
34 |
|
|
35 |
< |
double x_c = 1.0; /* ratio of output x size to input */ |
36 |
< |
double y_r = 1.0; /* ratio of output y size to input */ |
35 |
> |
double x_c = 1.0; /* ratio of output x size to input */ |
36 |
> |
double y_r = 1.0; /* ratio of output y size to input */ |
37 |
|
|
38 |
|
int singlepass = 0; /* true means skip first pass */ |
39 |
|
|
40 |
|
int avghot = 0; /* true means average in bright spots */ |
41 |
|
|
42 |
< |
double hotlvl = 1000.0; /* level considered "hot" */ |
42 |
> |
double hotlvl = 100.0; /* level considered "hot" */ |
43 |
|
|
44 |
|
int npts = 0; /* (half) number of points for stars */ |
45 |
|
|
46 |
< |
double spread = 1e-4; /* spread for star points */ |
46 |
> |
double spread = 1e-4; /* spread for star points */ |
47 |
|
|
46 |
– |
#define TEMPLATE "/usr/tmp/pfXXXXXX" |
47 |
– |
|
48 |
|
char *tfname = NULL; |
49 |
|
|
50 |
+ |
char template[] = TEMPLATE; |
51 |
+ |
|
52 |
|
char *lampdat = "lamp.tab"; /* lamp data file */ |
53 |
|
|
54 |
+ |
int order; /* scanline ordering of input */ |
55 |
|
int xres, yres; /* resolution of input */ |
56 |
< |
double inpaspect = 1.0; /* pixel aspect ratio of input */ |
56 |
> |
double inpaspect = 1.0; /* pixel aspect ratio of input */ |
57 |
|
int correctaspect = 0; /* aspect ratio correction? */ |
58 |
|
|
59 |
< |
int xrad; /* x window size */ |
57 |
< |
int yrad; /* y window size */ |
59 |
> |
int wrongformat = 0; |
60 |
|
|
61 |
+ |
VIEW ourview = STDVIEW; |
62 |
+ |
int gotview = 0; |
63 |
+ |
int wrapfilt = 0; /* wrap filter horizontally? */ |
64 |
+ |
|
65 |
+ |
int estatus = 0; /* exit status (for non-fatal errors) */ |
66 |
+ |
|
67 |
+ |
int xrad; /* x search radius */ |
68 |
+ |
int yrad; /* y search radius */ |
69 |
+ |
int xbrad; /* x box size */ |
70 |
+ |
int ybrad; /* y box size */ |
71 |
+ |
|
72 |
|
int barsize; /* size of input scan bar */ |
73 |
|
COLOR **scanin; /* input scan bar */ |
74 |
|
COLOR *scanout; /* output scan line */ |
75 |
+ |
COLOR **scoutbar; /* output scan bar (if thresh > 0) */ |
76 |
+ |
float **greybar; /* grey-averaged input values */ |
77 |
+ |
int obarsize = 0; /* size of output scan bar */ |
78 |
+ |
int orad = 0; /* output window radius */ |
79 |
|
|
80 |
|
char *progname; |
81 |
|
|
84 |
|
int argc; |
85 |
|
char **argv; |
86 |
|
{ |
87 |
< |
extern char *mktemp(); |
71 |
< |
extern double atof(), pow(); |
72 |
< |
extern long ftell(); |
73 |
< |
extern int quit(), headline(); |
87 |
> |
extern int headline(); |
88 |
|
FILE *fin; |
89 |
|
float *lampcolor; |
90 |
|
char *lamptype = NULL; |
91 |
|
long fpos; |
92 |
< |
double outaspect = 0.0; |
93 |
< |
double d; |
94 |
< |
int i; |
95 |
< |
|
92 |
> |
double outaspect = 0.0; |
93 |
> |
double d; |
94 |
> |
int i, j; |
95 |
> |
SET_DEFAULT_BINARY(); |
96 |
> |
SET_FILE_BINARY(stdin); |
97 |
> |
SET_FILE_BINARY(stdout); |
98 |
|
if (signal(SIGINT, quit) == SIG_IGN) |
99 |
|
signal(SIGINT, SIG_IGN); |
100 |
|
if (signal(SIGHUP, quit) == SIG_IGN) |
101 |
< |
signal(SIGINT, SIG_IGN); |
101 |
> |
signal(SIGHUP, SIG_IGN); |
102 |
|
signal(SIGTERM, quit); |
103 |
|
signal(SIGPIPE, quit); |
104 |
< |
#ifdef SIGXCPU |
104 |
> |
#ifdef SIGXCPU |
105 |
|
signal(SIGXCPU, quit); |
106 |
|
signal(SIGXFSZ, quit); |
107 |
|
#endif |
108 |
|
|
109 |
< |
progname = argv[0]; |
109 |
> |
progname = argv[0] = fixargv0(argv[0]); |
110 |
|
|
111 |
|
for (i = 1; i < argc; i++) |
112 |
|
if (argv[i][0] == '-') |
143 |
|
fprintf(stderr, |
144 |
|
"%s: exposure out of range\n", |
145 |
|
argv[0]); |
146 |
< |
exit(1); |
146 |
> |
quit(1); |
147 |
|
} |
148 |
|
switch (argv[i][2]) { |
149 |
|
case '\0': |
190 |
|
case 'r': |
191 |
|
rad = atof(argv[++i]); |
192 |
|
break; |
193 |
+ |
case 'm': |
194 |
+ |
thresh = atof(argv[++i]); |
195 |
+ |
if (rad <= FTINY) |
196 |
+ |
rad = 0.6; |
197 |
+ |
break; |
198 |
|
case 'b': |
199 |
< |
rad = 0.0; |
199 |
> |
rad = thresh = 0.0; |
200 |
|
break; |
201 |
|
default:; |
202 |
|
badopt: |
215 |
|
fprintf(stderr, "%s: unknown lamp type\n", lamptype); |
216 |
|
quit(1); |
217 |
|
} |
218 |
< |
colval(exposure,RED) /= lampcolor[0]; |
219 |
< |
colval(exposure,GRN) /= lampcolor[1]; |
220 |
< |
colval(exposure,BLU) /= lampcolor[2]; |
218 |
> |
for (j = 0; j < 3; j++) |
219 |
> |
if (lampcolor[j] > 1e-4) |
220 |
> |
colval(exposure,j) /= lampcolor[j]; |
221 |
|
freelamps(); |
222 |
|
} |
223 |
|
/* open input file */ |
225 |
|
if (singlepass) |
226 |
|
fin = stdin; |
227 |
|
else { |
228 |
< |
tfname = mktemp(TEMPLATE); |
228 |
> |
tfname = mktemp(template); |
229 |
|
if ((fin = fopen(tfname, "w+")) == NULL) { |
230 |
|
fprintf(stderr, "%s: can't create ", progname); |
231 |
|
fprintf(stderr, "temp file \"%s\"\n", tfname); |
248 |
|
quit(1); |
249 |
|
} |
250 |
|
/* get header */ |
251 |
< |
getheader(fin, headline); |
251 |
> |
getheader(fin, headline, NULL); |
252 |
> |
if (wrongformat) { |
253 |
> |
fprintf(stderr, "%s: input must be a Radiance picture\n", |
254 |
> |
progname); |
255 |
> |
quit(1); |
256 |
> |
} |
257 |
|
/* add new header info. */ |
258 |
|
printargs(i, argv, stdout); |
259 |
|
/* get picture size */ |
260 |
< |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
260 |
> |
if ((order = fgetresolu(&xres, &yres, fin)) < 0) { |
261 |
|
fprintf(stderr, "%s: bad picture size\n", progname); |
262 |
|
quit(1); |
263 |
|
} |
264 |
+ |
if (!(order & YMAJOR)) |
265 |
+ |
inpaspect = 1.0/inpaspect; |
266 |
+ |
/* wrap around for cylindrical view? */ |
267 |
+ |
wrapfilt = gotview && ourview.type == VT_CYL && |
268 |
+ |
ourview.horiz >= 360.-FTINY && order & YMAJOR; |
269 |
|
/* compute output resolution */ |
270 |
|
if (ncols <= 0) |
271 |
|
ncols = x_c*xres + .5; |
297 |
|
} |
298 |
|
pass2(fin); |
299 |
|
|
300 |
< |
quit(0); |
300 |
> |
quit(estatus); |
301 |
|
} |
302 |
|
|
303 |
|
|
304 |
+ |
double |
305 |
+ |
rgb_bright(clr) |
306 |
+ |
COLOR clr; |
307 |
+ |
{ |
308 |
+ |
return(bright(clr)); |
309 |
+ |
} |
310 |
+ |
|
311 |
+ |
|
312 |
+ |
double |
313 |
+ |
xyz_bright(clr) |
314 |
+ |
COLOR clr; |
315 |
+ |
{ |
316 |
+ |
return(clr[CIEY]); |
317 |
+ |
} |
318 |
+ |
|
319 |
+ |
|
320 |
+ |
double (*ourbright)() = rgb_bright; |
321 |
+ |
|
322 |
+ |
|
323 |
+ |
int |
324 |
|
headline(s) /* process line from header */ |
325 |
|
char *s; |
326 |
|
{ |
327 |
+ |
char fmt[32]; |
328 |
+ |
|
329 |
|
fputs(s, stdout); /* copy to output */ |
330 |
|
if (isaspect(s)) /* get aspect ratio */ |
331 |
|
inpaspect *= aspectval(s); |
332 |
+ |
else if (isexpos(s)) /* get exposure */ |
333 |
+ |
hotlvl *= exposval(s); |
334 |
+ |
else if (formatval(fmt, s)) { /* get format */ |
335 |
+ |
wrongformat = 0; |
336 |
+ |
if (!strcmp(COLRFMT, fmt)) |
337 |
+ |
ourbright = rgb_bright; |
338 |
+ |
else if (!strcmp(CIEFMT, fmt)) |
339 |
+ |
ourbright = xyz_bright; |
340 |
+ |
else |
341 |
+ |
wrongformat = !globmatch(PICFMT, fmt); |
342 |
+ |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
343 |
+ |
gotview++; |
344 |
+ |
return(0); |
345 |
|
} |
346 |
|
|
347 |
|
|
375 |
|
} |
376 |
|
for (i = 0; i < yres; i++) { |
377 |
|
if (freadscan(scan, xres, in) < 0) { |
378 |
< |
nrows = nrows * i / yres; /* adjust frame */ |
378 |
> |
nrows = (long)nrows * i / yres; /* adjust frame */ |
379 |
|
if (nrows <= 0) { |
380 |
|
fprintf(stderr, "%s: empty frame\n", progname); |
381 |
|
quit(1); |
382 |
|
} |
383 |
|
fprintf(stderr, "%s: warning - partial frame (%d%%)\n", |
384 |
< |
progname, 100*i/yres); |
384 |
> |
progname, (int)(100L*i/yres)); |
385 |
|
yres = i; |
386 |
|
y_r = (double)nrows/yres; |
387 |
+ |
estatus++; |
388 |
|
break; |
389 |
|
} |
390 |
|
pass1scan(scan, i); |
391 |
|
} |
392 |
< |
free((char *)scan); |
392 |
> |
free((void *)scan); |
393 |
|
} |
394 |
|
|
395 |
|
|
399 |
|
int yread; |
400 |
|
int ycent, xcent; |
401 |
|
int r, c; |
402 |
< |
|
402 |
> |
|
403 |
|
pass2init(); |
404 |
|
scan2init(); |
405 |
|
yread = 0; |
406 |
|
for (r = 0; r < nrows; r++) { |
407 |
< |
ycent = (long)r*yres/nrows; |
407 |
> |
ycent = (r+.5)*yres/nrows; |
408 |
|
while (yread <= ycent+yrad) { |
409 |
|
if (yread < yres) { |
410 |
|
if (freadscan(scanin[yread%barsize], |
411 |
|
xres, in) < 0) { |
412 |
|
fprintf(stderr, |
413 |
< |
"%s: bad read (y=%d)\n", |
413 |
> |
"%s: truncated input (y=%d)\n", |
414 |
|
progname, yres-1-yread); |
415 |
|
quit(1); |
416 |
|
} |
418 |
|
} |
419 |
|
yread++; |
420 |
|
} |
421 |
+ |
if (obarsize > 0) |
422 |
+ |
scan2sync(r); |
423 |
|
for (c = 0; c < ncols; c++) { |
424 |
< |
xcent = (long)c*xres/ncols; |
425 |
< |
if (rad <= 0.0) |
426 |
< |
dobox(scanout[c], xcent, ycent, c, r); |
427 |
< |
else |
424 |
> |
xcent = (c+.5)*xres/ncols; |
425 |
> |
if (thresh > FTINY) |
426 |
> |
dothresh(xcent, ycent, c, r); |
427 |
> |
else if (rad > FTINY) |
428 |
|
dogauss(scanout[c], xcent, ycent, c, r); |
429 |
+ |
else |
430 |
+ |
dobox(scanout[c], xcent, ycent, c, r); |
431 |
|
} |
432 |
< |
if (fwritescan(scanout, ncols, stdout) < 0) { |
432 |
> |
if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) { |
433 |
|
fprintf(stderr, "%s: write error in pass2\n", progname); |
434 |
|
quit(1); |
435 |
|
} |
436 |
|
} |
437 |
< |
/* skip leftovers */ |
437 |
> |
/* skip leftover input */ |
438 |
|
while (yread < yres) { |
439 |
|
if (freadscan(scanin[0], xres, in) < 0) |
440 |
|
break; |
441 |
|
yread++; |
442 |
|
} |
443 |
+ |
scan2flush(); /* flush output */ |
444 |
|
} |
445 |
|
|
446 |
|
|
447 |
|
scan2init() /* prepare scanline arrays */ |
448 |
|
{ |
449 |
|
COLOR ctmp; |
450 |
< |
double d; |
450 |
> |
double d; |
451 |
|
register int i; |
452 |
|
|
453 |
< |
if (rad <= 0.0) { |
454 |
< |
xrad = xres/ncols/2 + 1; |
455 |
< |
yrad = yres/nrows/2 + 1; |
384 |
< |
} else { |
453 |
> |
xbrad = xres/ncols/2 + 1; |
454 |
> |
ybrad = yres/nrows/2 + 1; |
455 |
> |
if (rad > FTINY) { |
456 |
|
if (nrows >= yres && ncols >= xres) |
457 |
|
rad *= (y_r + x_c)/2.0; |
458 |
|
|
459 |
< |
xrad = CHECKRAD*rad/x_c + 1; |
460 |
< |
yrad = CHECKRAD*rad/y_r + 1; |
461 |
< |
|
459 |
> |
if (thresh > FTINY) { |
460 |
> |
orad = CHECKRAD*THRESHRAD*rad + 1; |
461 |
> |
xrad = orad/x_c + xbrad; |
462 |
> |
yrad = orad/y_r + ybrad; |
463 |
> |
obarsize = 2*orad + 1; |
464 |
> |
} else { |
465 |
> |
xrad = CHECKRAD*rad/x_c + 1; |
466 |
> |
yrad = CHECKRAD*rad/y_r + 1; |
467 |
> |
} |
468 |
|
initmask(); /* initialize filter table */ |
469 |
+ |
} else { |
470 |
+ |
xrad = xbrad; |
471 |
+ |
yrad = ybrad; |
472 |
|
} |
473 |
< |
barsize = 2 * yrad; |
473 |
> |
barsize = 2*yrad + 1; |
474 |
|
scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); |
475 |
+ |
if (scanin == NULL) |
476 |
+ |
goto memerr; |
477 |
|
for (i = 0; i < barsize; i++) { |
478 |
|
scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
479 |
< |
if (scanin[i] == NULL) { |
480 |
< |
fprintf(stderr, "%s: out of memory\n", progname); |
481 |
< |
quit(1); |
479 |
> |
if (scanin[i] == NULL) |
480 |
> |
goto memerr; |
481 |
> |
} |
482 |
> |
if (obarsize > 0) { |
483 |
> |
scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *)); |
484 |
> |
greybar = (float **)malloc(obarsize*sizeof(float *)); |
485 |
> |
if (scoutbar == NULL | greybar == NULL) |
486 |
> |
goto memerr; |
487 |
> |
for (i = 0; i < obarsize; i++) { |
488 |
> |
scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR)); |
489 |
> |
greybar[i] = (float *)malloc(ncols*sizeof(float)); |
490 |
> |
if (scoutbar[i] == NULL | greybar[i] == NULL) |
491 |
> |
goto memerr; |
492 |
|
} |
493 |
+ |
} else { |
494 |
+ |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
495 |
+ |
if (scanout == NULL) |
496 |
+ |
goto memerr; |
497 |
|
} |
402 |
– |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
403 |
– |
if (scanout == NULL) { |
404 |
– |
fprintf(stderr, "%s: out of memory\n", progname); |
405 |
– |
quit(1); |
406 |
– |
} |
498 |
|
/* record pixel aspect ratio */ |
499 |
|
if (!correctaspect) { |
500 |
< |
d = x_c / y_r; |
500 |
> |
d = order & YMAJOR ? x_c/y_r : y_r/x_c ; |
501 |
|
if (!FEQ(d,1.0)) |
502 |
|
fputaspect(d, stdout); |
503 |
|
} |
504 |
|
/* record exposure */ |
505 |
< |
d = bright(exposure); |
505 |
> |
d = (*ourbright)(exposure); |
506 |
|
if (!FEQ(d,1.0)) |
507 |
|
fputexpos(d, stdout); |
508 |
|
/* record color correction */ |
513 |
|
fputcolcor(ctmp, stdout); |
514 |
|
printf("\n"); |
515 |
|
/* write out resolution */ |
516 |
< |
fputresolu(YMAJOR|YDECR, ncols, nrows, stdout); |
516 |
> |
fputresolu(order, ncols, nrows, stdout); |
517 |
> |
return; |
518 |
> |
memerr: |
519 |
> |
fprintf(stderr, "%s: out of memory\n", progname); |
520 |
> |
quit(1); |
521 |
|
} |
522 |
|
|
523 |
|
|
524 |
+ |
scan2sync(r) /* synchronize grey averages and output scan */ |
525 |
+ |
int r; |
526 |
+ |
{ |
527 |
+ |
static int nextrow = 0; |
528 |
+ |
COLOR ctmp; |
529 |
+ |
int ybot; |
530 |
+ |
register int c; |
531 |
+ |
/* average input scanlines */ |
532 |
+ |
while (nextrow <= r+orad && nextrow < nrows) { |
533 |
+ |
ybot = (nextrow+.5)*yres/nrows; |
534 |
+ |
for (c = 0; c < ncols; c++) { |
535 |
+ |
dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow); |
536 |
+ |
greybar[nextrow%obarsize][c] = (*ourbright)(ctmp); |
537 |
+ |
} |
538 |
+ |
/* and zero output scanline */ |
539 |
+ |
bzero((char *)scoutbar[nextrow%obarsize], ncols*sizeof(COLOR)); |
540 |
+ |
nextrow++; |
541 |
+ |
} |
542 |
+ |
/* point to top scanline for output */ |
543 |
+ |
if (r-orad >= 0) |
544 |
+ |
scanout = scoutbar[(r-orad)%obarsize]; |
545 |
+ |
else |
546 |
+ |
scanout = NULL; |
547 |
+ |
} |
548 |
+ |
|
549 |
+ |
|
550 |
+ |
scan2flush() /* flush output buffer */ |
551 |
+ |
{ |
552 |
+ |
register int r; |
553 |
+ |
|
554 |
+ |
for (r = nrows-orad; r < nrows; r++) |
555 |
+ |
if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0) |
556 |
+ |
break; |
557 |
+ |
if (fflush(stdout) < 0) { |
558 |
+ |
fprintf(stderr, "%s: write error at end of pass2\n", progname); |
559 |
+ |
quit(1); |
560 |
+ |
} |
561 |
+ |
} |
562 |
+ |
|
563 |
+ |
|
564 |
+ |
void |
565 |
|
quit(code) /* remove temporary file and exit */ |
566 |
|
int code; |
567 |
|
{ |