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