1 |
– |
/* Copyright (c) 1992 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 "standard.h" |
11 |
> |
#include "copyright.h" |
12 |
|
|
13 |
|
#include <signal.h> |
14 |
+ |
#include <string.h> |
15 |
|
|
16 |
+ |
#include "platform.h" |
17 |
+ |
#include "standard.h" |
18 |
+ |
#include "rtio.h" |
19 |
|
#include "color.h" |
20 |
< |
|
19 |
< |
#include "resolu.h" |
20 |
< |
|
20 |
> |
#include "view.h" |
21 |
|
#include "paths.h" |
22 |
+ |
#include "pfilt.h" |
23 |
|
|
23 |
– |
extern float *matchlamp(); |
24 |
|
|
25 |
|
#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) |
26 |
|
|
27 |
< |
#define CHECKRAD 1.5 /* radius to check for filtering */ |
27 |
> |
double CHECKRAD = 2.0; /* radius to check for filtering */ |
28 |
|
|
29 |
+ |
#define THRESHRAD 5.0 /* maximum sample spread in output */ |
30 |
+ |
|
31 |
|
COLOR exposure = WHTCOLOR; /* exposure for the frame */ |
32 |
|
|
33 |
|
double rad = 0.0; /* output pixel radius for filtering */ |
34 |
|
|
35 |
+ |
double thresh = 0.0; /* maximum contribution for subpixel */ |
36 |
+ |
|
37 |
|
int nrows = 0; /* number of rows for output */ |
38 |
|
int ncols = 0; /* number of columns for output */ |
39 |
|
|
52 |
|
|
53 |
|
char *tfname = NULL; |
54 |
|
|
55 |
+ |
char template[] = TEMPLATE; |
56 |
+ |
|
57 |
|
char *lampdat = "lamp.tab"; /* lamp data file */ |
58 |
|
|
59 |
|
int order; /* scanline ordering of input */ |
63 |
|
|
64 |
|
int wrongformat = 0; |
65 |
|
|
66 |
< |
int xrad; /* x window size */ |
67 |
< |
int yrad; /* y window size */ |
66 |
> |
VIEW ourview = STDVIEW; |
67 |
> |
int gotview = 0; |
68 |
> |
int wrapfilt = 0; /* wrap filter horizontally? */ |
69 |
|
|
70 |
+ |
int estatus = 0; /* exit status (for non-fatal errors) */ |
71 |
+ |
|
72 |
+ |
int xrad; /* x search radius */ |
73 |
+ |
int yrad; /* y search radius */ |
74 |
+ |
int xbrad; /* x box size */ |
75 |
+ |
int ybrad; /* y box size */ |
76 |
+ |
|
77 |
|
int barsize; /* size of input scan bar */ |
78 |
|
COLOR **scanin; /* input scan bar */ |
79 |
|
COLOR *scanout; /* output scan line */ |
80 |
+ |
COLOR **scoutbar; /* output scan bar (if thresh > 0) */ |
81 |
+ |
float **greybar; /* grey-averaged input values */ |
82 |
+ |
int obarsize = 0; /* size of output scan bar */ |
83 |
+ |
int orad = 0; /* output window radius */ |
84 |
|
|
85 |
|
char *progname; |
86 |
|
|
87 |
+ |
static gethfunc headline; |
88 |
+ |
static double rgb_bright(COLOR clr); |
89 |
+ |
static double xyz_bright(COLOR clr); |
90 |
+ |
static void copyfile(FILE *in, FILE *out); |
91 |
+ |
static void pass1(FILE *in); |
92 |
+ |
static void pass2(FILE *in); |
93 |
+ |
static void scan2init(void); |
94 |
+ |
static void scan2sync(int r); |
95 |
+ |
static void scan2flush(void); |
96 |
|
|
97 |
< |
main(argc, argv) |
98 |
< |
int argc; |
99 |
< |
char **argv; |
97 |
> |
|
98 |
> |
int |
99 |
> |
main( |
100 |
> |
int argc, |
101 |
> |
char **argv |
102 |
> |
) |
103 |
|
{ |
74 |
– |
extern long ftell(); |
75 |
– |
extern int quit(), headline(); |
104 |
|
FILE *fin; |
105 |
|
float *lampcolor; |
106 |
|
char *lamptype = NULL; |
108 |
|
double outaspect = 0.0; |
109 |
|
double d; |
110 |
|
int i, j; |
111 |
< |
#ifdef MSDOS |
112 |
< |
extern int _fmode; |
113 |
< |
_fmode = O_BINARY; |
86 |
< |
setmode(fileno(stdin), O_BINARY); |
87 |
< |
setmode(fileno(stdout), O_BINARY); |
88 |
< |
#endif |
111 |
> |
SET_DEFAULT_BINARY(); |
112 |
> |
SET_FILE_BINARY(stdin); |
113 |
> |
SET_FILE_BINARY(stdout); |
114 |
|
if (signal(SIGINT, quit) == SIG_IGN) |
115 |
|
signal(SIGINT, SIG_IGN); |
116 |
+ |
#ifdef SIGHUP |
117 |
|
if (signal(SIGHUP, quit) == SIG_IGN) |
118 |
< |
signal(SIGINT, SIG_IGN); |
118 |
> |
signal(SIGHUP, SIG_IGN); |
119 |
> |
#endif |
120 |
|
signal(SIGTERM, quit); |
121 |
+ |
#ifdef SIGPIPE |
122 |
|
signal(SIGPIPE, quit); |
123 |
+ |
#endif |
124 |
|
#ifdef SIGXCPU |
125 |
|
signal(SIGXCPU, quit); |
126 |
|
signal(SIGXFSZ, quit); |
163 |
|
fprintf(stderr, |
164 |
|
"%s: exposure out of range\n", |
165 |
|
argv[0]); |
166 |
< |
exit(1); |
166 |
> |
quit(1); |
167 |
|
} |
168 |
|
switch (argv[i][2]) { |
169 |
|
case '\0': |
210 |
|
case 'r': |
211 |
|
rad = atof(argv[++i]); |
212 |
|
break; |
213 |
+ |
case 'm': |
214 |
+ |
thresh = atof(argv[++i]); |
215 |
+ |
if (rad <= FTINY) |
216 |
+ |
rad = 0.6; |
217 |
+ |
break; |
218 |
|
case 'b': |
219 |
< |
rad = 0.0; |
219 |
> |
rad = thresh = 0.0; |
220 |
|
break; |
221 |
|
default:; |
222 |
|
badopt: |
235 |
|
fprintf(stderr, "%s: unknown lamp type\n", lamptype); |
236 |
|
quit(1); |
237 |
|
} |
238 |
< |
for (i = 0; i < 3; i++) |
239 |
< |
if (lampcolor[i] > 1e-4) |
240 |
< |
colval(exposure,i) /= lampcolor[i]; |
238 |
> |
for (j = 0; j < 3; j++) |
239 |
> |
if (lampcolor[j] > 1e-4) |
240 |
> |
colval(exposure,j) /= lampcolor[j]; |
241 |
|
freelamps(); |
242 |
|
} |
243 |
|
/* open input file */ |
245 |
|
if (singlepass) |
246 |
|
fin = stdin; |
247 |
|
else { |
248 |
< |
tfname = mktemp(TEMPLATE); |
248 |
> |
tfname = mktemp(template); |
249 |
|
if ((fin = fopen(tfname, "w+")) == NULL) { |
250 |
|
fprintf(stderr, "%s: can't create ", progname); |
251 |
|
fprintf(stderr, "temp file \"%s\"\n", tfname); |
283 |
|
} |
284 |
|
if (!(order & YMAJOR)) |
285 |
|
inpaspect = 1.0/inpaspect; |
286 |
+ |
/* wrap around for cylindrical view? */ |
287 |
+ |
wrapfilt = gotview && ourview.type == VT_CYL && |
288 |
+ |
ourview.horiz >= 360.-FTINY && order & YMAJOR; |
289 |
|
/* compute output resolution */ |
290 |
|
if (ncols <= 0) |
291 |
|
ncols = x_c*xres + .5; |
317 |
|
} |
318 |
|
pass2(fin); |
319 |
|
|
320 |
< |
quit(0); |
320 |
> |
quit(estatus); |
321 |
> |
return estatus; /* pro forma return */ |
322 |
|
} |
323 |
|
|
324 |
|
|
325 |
< |
headline(s) /* process line from header */ |
326 |
< |
char *s; |
325 |
> |
static double |
326 |
> |
rgb_bright( |
327 |
> |
COLOR clr |
328 |
> |
) |
329 |
|
{ |
330 |
+ |
return(bright(clr)); |
331 |
+ |
} |
332 |
+ |
|
333 |
+ |
|
334 |
+ |
static double |
335 |
+ |
xyz_bright( |
336 |
+ |
COLOR clr |
337 |
+ |
) |
338 |
+ |
{ |
339 |
+ |
return(clr[CIEY]); |
340 |
+ |
} |
341 |
+ |
|
342 |
+ |
|
343 |
+ |
double (*ourbright)() = rgb_bright; |
344 |
+ |
|
345 |
+ |
|
346 |
+ |
static int |
347 |
+ |
headline( /* process line from header */ |
348 |
+ |
char *s, |
349 |
+ |
void *p |
350 |
+ |
) |
351 |
+ |
{ |
352 |
|
char fmt[32]; |
353 |
|
|
354 |
|
fputs(s, stdout); /* copy to output */ |
355 |
|
if (isaspect(s)) /* get aspect ratio */ |
356 |
|
inpaspect *= aspectval(s); |
357 |
< |
else if (isexpos(s)) |
357 |
> |
else if (isexpos(s)) /* get exposure */ |
358 |
|
hotlvl *= exposval(s); |
359 |
< |
else if (isformat(s)) { |
360 |
< |
formatval(fmt, s); |
361 |
< |
wrongformat = strcmp(fmt, COLRFMT); |
362 |
< |
} |
359 |
> |
else if (formatval(fmt, s)) { /* get format */ |
360 |
> |
wrongformat = 0; |
361 |
> |
if (!strcmp(COLRFMT, fmt)) |
362 |
> |
ourbright = rgb_bright; |
363 |
> |
else if (!strcmp(CIEFMT, fmt)) |
364 |
> |
ourbright = xyz_bright; |
365 |
> |
else |
366 |
> |
wrongformat = !globmatch(PICFMT, fmt); |
367 |
> |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
368 |
> |
gotview++; |
369 |
> |
return(0); |
370 |
|
} |
371 |
|
|
372 |
|
|
373 |
< |
copyfile(in, out) /* copy a file */ |
374 |
< |
register FILE *in, *out; |
373 |
> |
static void |
374 |
> |
copyfile( /* copy a file */ |
375 |
> |
register FILE *in, |
376 |
> |
register FILE *out |
377 |
> |
) |
378 |
|
{ |
379 |
|
register int c; |
380 |
|
|
388 |
|
} |
389 |
|
|
390 |
|
|
391 |
< |
pass1(in) /* first pass of picture file */ |
392 |
< |
FILE *in; |
391 |
> |
static void |
392 |
> |
pass1( /* first pass of picture file */ |
393 |
> |
FILE *in |
394 |
> |
) |
395 |
|
{ |
396 |
|
int i; |
397 |
|
COLOR *scan; |
414 |
|
progname, (int)(100L*i/yres)); |
415 |
|
yres = i; |
416 |
|
y_r = (double)nrows/yres; |
417 |
+ |
estatus++; |
418 |
|
break; |
419 |
|
} |
420 |
|
pass1scan(scan, i); |
421 |
|
} |
422 |
< |
free((char *)scan); |
422 |
> |
free((void *)scan); |
423 |
|
} |
424 |
|
|
425 |
|
|
426 |
< |
pass2(in) /* last pass on file, write to stdout */ |
427 |
< |
FILE *in; |
426 |
> |
static void |
427 |
> |
pass2( /* last pass on file, write to stdout */ |
428 |
> |
FILE *in |
429 |
> |
) |
430 |
|
{ |
431 |
|
int yread; |
432 |
|
int ycent, xcent; |
436 |
|
scan2init(); |
437 |
|
yread = 0; |
438 |
|
for (r = 0; r < nrows; r++) { |
439 |
< |
ycent = (long)r*yres/nrows; |
439 |
> |
ycent = (r+.5)*yres/nrows; |
440 |
|
while (yread <= ycent+yrad) { |
441 |
|
if (yread < yres) { |
442 |
|
if (freadscan(scanin[yread%barsize], |
443 |
|
xres, in) < 0) { |
444 |
|
fprintf(stderr, |
445 |
< |
"%s: bad read (y=%d)\n", |
445 |
> |
"%s: truncated input (y=%d)\n", |
446 |
|
progname, yres-1-yread); |
447 |
|
quit(1); |
448 |
|
} |
450 |
|
} |
451 |
|
yread++; |
452 |
|
} |
453 |
+ |
if (obarsize > 0) |
454 |
+ |
scan2sync(r); |
455 |
|
for (c = 0; c < ncols; c++) { |
456 |
< |
xcent = (long)c*xres/ncols; |
457 |
< |
if (rad <= 0.0) |
458 |
< |
dobox(scanout[c], xcent, ycent, c, r); |
459 |
< |
else |
456 |
> |
xcent = (c+.5)*xres/ncols; |
457 |
> |
if (thresh > FTINY) |
458 |
> |
dothresh(xcent, ycent, c, r); |
459 |
> |
else if (rad > FTINY) |
460 |
|
dogauss(scanout[c], xcent, ycent, c, r); |
461 |
+ |
else |
462 |
+ |
dobox(scanout[c], xcent, ycent, c, r); |
463 |
|
} |
464 |
< |
if (fwritescan(scanout, ncols, stdout) < 0) { |
464 |
> |
if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) { |
465 |
|
fprintf(stderr, "%s: write error in pass2\n", progname); |
466 |
|
quit(1); |
467 |
|
} |
468 |
|
} |
469 |
< |
/* skip leftovers */ |
469 |
> |
/* skip leftover input */ |
470 |
|
while (yread < yres) { |
471 |
|
if (freadscan(scanin[0], xres, in) < 0) |
472 |
|
break; |
473 |
|
yread++; |
474 |
|
} |
475 |
+ |
scan2flush(); /* flush output */ |
476 |
|
} |
477 |
|
|
478 |
|
|
479 |
< |
scan2init() /* prepare scanline arrays */ |
479 |
> |
static void |
480 |
> |
scan2init(void) /* prepare scanline arrays */ |
481 |
|
{ |
482 |
|
COLOR ctmp; |
483 |
|
double d; |
484 |
|
register int i; |
485 |
|
|
486 |
< |
if (rad <= 0.0) { |
487 |
< |
xrad = xres/ncols/2 + 1; |
488 |
< |
yrad = yres/nrows/2 + 1; |
406 |
< |
} else { |
486 |
> |
xbrad = xres/ncols/2 + 1; |
487 |
> |
ybrad = yres/nrows/2 + 1; |
488 |
> |
if (rad > FTINY) { |
489 |
|
if (nrows >= yres && ncols >= xres) |
490 |
|
rad *= (y_r + x_c)/2.0; |
491 |
|
|
492 |
< |
xrad = CHECKRAD*rad/x_c + 1; |
493 |
< |
yrad = CHECKRAD*rad/y_r + 1; |
494 |
< |
|
492 |
> |
if (thresh > FTINY) { |
493 |
> |
orad = CHECKRAD*THRESHRAD*rad + 1; |
494 |
> |
xrad = orad/x_c + xbrad; |
495 |
> |
yrad = orad/y_r + ybrad; |
496 |
> |
obarsize = 2*orad + 1; |
497 |
> |
} else { |
498 |
> |
xrad = CHECKRAD*rad/x_c + 1; |
499 |
> |
yrad = CHECKRAD*rad/y_r + 1; |
500 |
> |
} |
501 |
|
initmask(); /* initialize filter table */ |
502 |
+ |
} else { |
503 |
+ |
xrad = xbrad; |
504 |
+ |
yrad = ybrad; |
505 |
|
} |
506 |
|
barsize = 2*yrad + 1; |
507 |
|
scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); |
508 |
+ |
if (scanin == NULL) |
509 |
+ |
goto memerr; |
510 |
|
for (i = 0; i < barsize; i++) { |
511 |
|
scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
512 |
< |
if (scanin[i] == NULL) { |
513 |
< |
fprintf(stderr, "%s: out of memory\n", progname); |
514 |
< |
quit(1); |
512 |
> |
if (scanin[i] == NULL) |
513 |
> |
goto memerr; |
514 |
> |
} |
515 |
> |
if (obarsize > 0) { |
516 |
> |
scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *)); |
517 |
> |
greybar = (float **)malloc(obarsize*sizeof(float *)); |
518 |
> |
if ((scoutbar == NULL) | (greybar == NULL)) |
519 |
> |
goto memerr; |
520 |
> |
for (i = 0; i < obarsize; i++) { |
521 |
> |
scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR)); |
522 |
> |
greybar[i] = (float *)malloc(ncols*sizeof(float)); |
523 |
> |
if ((scoutbar[i] == NULL) | (greybar[i] == NULL)) |
524 |
> |
goto memerr; |
525 |
|
} |
526 |
+ |
} else { |
527 |
+ |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
528 |
+ |
if (scanout == NULL) |
529 |
+ |
goto memerr; |
530 |
|
} |
424 |
– |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
425 |
– |
if (scanout == NULL) { |
426 |
– |
fprintf(stderr, "%s: out of memory\n", progname); |
427 |
– |
quit(1); |
428 |
– |
} |
531 |
|
/* record pixel aspect ratio */ |
532 |
|
if (!correctaspect) { |
533 |
|
d = order & YMAJOR ? x_c/y_r : y_r/x_c ; |
535 |
|
fputaspect(d, stdout); |
536 |
|
} |
537 |
|
/* record exposure */ |
538 |
< |
d = bright(exposure); |
538 |
> |
d = (*ourbright)(exposure); |
539 |
|
if (!FEQ(d,1.0)) |
540 |
|
fputexpos(d, stdout); |
541 |
|
/* record color correction */ |
547 |
|
printf("\n"); |
548 |
|
/* write out resolution */ |
549 |
|
fputresolu(order, ncols, nrows, stdout); |
550 |
+ |
return; |
551 |
+ |
memerr: |
552 |
+ |
fprintf(stderr, "%s: out of memory\n", progname); |
553 |
+ |
quit(1); |
554 |
|
} |
555 |
|
|
556 |
|
|
557 |
+ |
static void |
558 |
+ |
scan2sync( /* synchronize grey averages and output scan */ |
559 |
+ |
int r |
560 |
+ |
) |
561 |
+ |
{ |
562 |
+ |
static int nextrow = 0; |
563 |
+ |
COLOR ctmp; |
564 |
+ |
int ybot; |
565 |
+ |
register int c; |
566 |
+ |
/* average input scanlines */ |
567 |
+ |
while (nextrow <= r+orad && nextrow < nrows) { |
568 |
+ |
ybot = (nextrow+.5)*yres/nrows; |
569 |
+ |
for (c = 0; c < ncols; c++) { |
570 |
+ |
dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow); |
571 |
+ |
greybar[nextrow%obarsize][c] = (*ourbright)(ctmp); |
572 |
+ |
} |
573 |
+ |
/* and zero output scanline */ |
574 |
+ |
memset((char *)scoutbar[nextrow%obarsize], '\0', ncols*sizeof(COLOR)); |
575 |
+ |
nextrow++; |
576 |
+ |
} |
577 |
+ |
/* point to top scanline for output */ |
578 |
+ |
if (r-orad >= 0) |
579 |
+ |
scanout = scoutbar[(r-orad)%obarsize]; |
580 |
+ |
else |
581 |
+ |
scanout = NULL; |
582 |
+ |
} |
583 |
+ |
|
584 |
+ |
|
585 |
+ |
static void |
586 |
+ |
scan2flush(void) /* flush output buffer */ |
587 |
+ |
{ |
588 |
+ |
register int r; |
589 |
+ |
|
590 |
+ |
for (r = nrows-orad; r < nrows; r++) |
591 |
+ |
if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0) |
592 |
+ |
break; |
593 |
+ |
if (fflush(stdout) < 0) { |
594 |
+ |
fprintf(stderr, "%s: write error at end of pass2\n", progname); |
595 |
+ |
quit(1); |
596 |
+ |
} |
597 |
+ |
} |
598 |
+ |
|
599 |
+ |
|
600 |
+ |
void |
601 |
|
quit(code) /* remove temporary file and exit */ |
602 |
|
int code; |
603 |
|
{ |