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