| 1 |
< |
/* Copyright (c) 1992 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 "standard.h" |
| 17 |
|
|
| 18 |
|
#include "color.h" |
| 19 |
|
|
| 20 |
+ |
#include "view.h" |
| 21 |
+ |
|
| 22 |
|
#include "resolu.h" |
| 23 |
|
|
| 24 |
|
#include "paths.h" |
| 27 |
|
|
| 28 |
|
#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) |
| 29 |
|
|
| 30 |
< |
#define CHECKRAD 1.5 /* radius to check for filtering */ |
| 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 */ |
| 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 |
|
|
| 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 |
|
|
| 55 |
|
|
| 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 */ |
| 66 |
|
|
| 67 |
|
int wrongformat = 0; |
| 68 |
|
|
| 69 |
< |
int xrad; /* x window size */ |
| 70 |
< |
int yrad; /* y window size */ |
| 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 |
|
|
| 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 |
| 155 |
|
fprintf(stderr, |
| 156 |
|
"%s: exposure out of range\n", |
| 157 |
|
argv[0]); |
| 158 |
< |
exit(1); |
| 158 |
> |
quit(1); |
| 159 |
|
} |
| 160 |
|
switch (argv[i][2]) { |
| 161 |
|
case '\0': |
| 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: |
| 227 |
|
fprintf(stderr, "%s: unknown lamp type\n", lamptype); |
| 228 |
|
quit(1); |
| 229 |
|
} |
| 230 |
< |
for (i = 0; i < 3; i++) |
| 231 |
< |
if (lampcolor[i] > 1e-4) |
| 232 |
< |
colval(exposure,i) /= lampcolor[i]; |
| 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 */ |
| 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); |
| 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 |
+ |
int |
| 336 |
|
headline(s) /* process line from header */ |
| 337 |
|
char *s; |
| 338 |
|
{ |
| 341 |
|
fputs(s, stdout); /* copy to output */ |
| 342 |
|
if (isaspect(s)) /* get aspect ratio */ |
| 343 |
|
inpaspect *= aspectval(s); |
| 344 |
< |
else if (isformat(s)) { |
| 345 |
< |
formatval(fmt, s); |
| 346 |
< |
wrongformat = strcmp(fmt, COLRFMT); |
| 347 |
< |
} |
| 344 |
> |
else if (isexpos(s)) /* get exposure */ |
| 345 |
> |
hotlvl *= exposval(s); |
| 346 |
> |
else if (formatval(fmt, s)) { /* get format */ |
| 347 |
> |
wrongformat = 0; |
| 348 |
> |
if (!strcmp(COLRFMT, fmt)) |
| 349 |
> |
ourbright = rgb_bright; |
| 350 |
> |
else if (!strcmp(CIEFMT, fmt)) |
| 351 |
> |
ourbright = xyz_bright; |
| 352 |
> |
else |
| 353 |
> |
wrongformat = !globmatch(PICFMT, fmt); |
| 354 |
> |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
| 355 |
> |
gotview++; |
| 356 |
> |
return(0); |
| 357 |
|
} |
| 358 |
|
|
| 359 |
|
|
| 387 |
|
} |
| 388 |
|
for (i = 0; i < yres; i++) { |
| 389 |
|
if (freadscan(scan, xres, in) < 0) { |
| 390 |
< |
nrows = nrows * i / yres; /* adjust frame */ |
| 390 |
> |
nrows = (long)nrows * i / yres; /* adjust frame */ |
| 391 |
|
if (nrows <= 0) { |
| 392 |
|
fprintf(stderr, "%s: empty frame\n", progname); |
| 393 |
|
quit(1); |
| 394 |
|
} |
| 395 |
|
fprintf(stderr, "%s: warning - partial frame (%d%%)\n", |
| 396 |
< |
progname, 100*i/yres); |
| 396 |
> |
progname, (int)(100L*i/yres)); |
| 397 |
|
yres = i; |
| 398 |
|
y_r = (double)nrows/yres; |
| 399 |
+ |
estatus++; |
| 400 |
|
break; |
| 401 |
|
} |
| 402 |
|
pass1scan(scan, i); |
| 416 |
|
scan2init(); |
| 417 |
|
yread = 0; |
| 418 |
|
for (r = 0; r < nrows; r++) { |
| 419 |
< |
ycent = (long)r*yres/nrows; |
| 419 |
> |
ycent = (r+.5)*yres/nrows; |
| 420 |
|
while (yread <= ycent+yrad) { |
| 421 |
|
if (yread < yres) { |
| 422 |
|
if (freadscan(scanin[yread%barsize], |
| 423 |
|
xres, in) < 0) { |
| 424 |
|
fprintf(stderr, |
| 425 |
< |
"%s: bad read (y=%d)\n", |
| 425 |
> |
"%s: truncated input (y=%d)\n", |
| 426 |
|
progname, yres-1-yread); |
| 427 |
|
quit(1); |
| 428 |
|
} |
| 430 |
|
} |
| 431 |
|
yread++; |
| 432 |
|
} |
| 433 |
+ |
if (obarsize > 0) |
| 434 |
+ |
scan2sync(r); |
| 435 |
|
for (c = 0; c < ncols; c++) { |
| 436 |
< |
xcent = (long)c*xres/ncols; |
| 437 |
< |
if (rad <= 0.0) |
| 438 |
< |
dobox(scanout[c], xcent, ycent, c, r); |
| 439 |
< |
else |
| 436 |
> |
xcent = (c+.5)*xres/ncols; |
| 437 |
> |
if (thresh > FTINY) |
| 438 |
> |
dothresh(xcent, ycent, c, r); |
| 439 |
> |
else if (rad > FTINY) |
| 440 |
|
dogauss(scanout[c], xcent, ycent, c, r); |
| 441 |
+ |
else |
| 442 |
+ |
dobox(scanout[c], xcent, ycent, c, r); |
| 443 |
|
} |
| 444 |
< |
if (fwritescan(scanout, ncols, stdout) < 0) { |
| 444 |
> |
if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) { |
| 445 |
|
fprintf(stderr, "%s: write error in pass2\n", progname); |
| 446 |
|
quit(1); |
| 447 |
|
} |
| 448 |
|
} |
| 449 |
< |
/* skip leftovers */ |
| 449 |
> |
/* skip leftover input */ |
| 450 |
|
while (yread < yres) { |
| 451 |
|
if (freadscan(scanin[0], xres, in) < 0) |
| 452 |
|
break; |
| 453 |
|
yread++; |
| 454 |
|
} |
| 455 |
+ |
scan2flush(); /* flush output */ |
| 456 |
|
} |
| 457 |
|
|
| 458 |
|
|
| 462 |
|
double d; |
| 463 |
|
register int i; |
| 464 |
|
|
| 465 |
< |
if (rad <= 0.0) { |
| 466 |
< |
xrad = xres/ncols/2 + 1; |
| 467 |
< |
yrad = yres/nrows/2 + 1; |
| 404 |
< |
} else { |
| 465 |
> |
xbrad = xres/ncols/2 + 1; |
| 466 |
> |
ybrad = yres/nrows/2 + 1; |
| 467 |
> |
if (rad > FTINY) { |
| 468 |
|
if (nrows >= yres && ncols >= xres) |
| 469 |
|
rad *= (y_r + x_c)/2.0; |
| 470 |
|
|
| 471 |
< |
xrad = CHECKRAD*rad/x_c + 1; |
| 472 |
< |
yrad = CHECKRAD*rad/y_r + 1; |
| 473 |
< |
|
| 471 |
> |
if (thresh > FTINY) { |
| 472 |
> |
orad = CHECKRAD*THRESHRAD*rad + 1; |
| 473 |
> |
xrad = orad/x_c + xbrad; |
| 474 |
> |
yrad = orad/y_r + ybrad; |
| 475 |
> |
obarsize = 2*orad + 1; |
| 476 |
> |
} else { |
| 477 |
> |
xrad = CHECKRAD*rad/x_c + 1; |
| 478 |
> |
yrad = CHECKRAD*rad/y_r + 1; |
| 479 |
> |
} |
| 480 |
|
initmask(); /* initialize filter table */ |
| 481 |
+ |
} else { |
| 482 |
+ |
xrad = xbrad; |
| 483 |
+ |
yrad = ybrad; |
| 484 |
|
} |
| 485 |
|
barsize = 2*yrad + 1; |
| 486 |
|
scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); |
| 487 |
+ |
if (scanin == NULL) |
| 488 |
+ |
goto memerr; |
| 489 |
|
for (i = 0; i < barsize; i++) { |
| 490 |
|
scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
| 491 |
< |
if (scanin[i] == NULL) { |
| 492 |
< |
fprintf(stderr, "%s: out of memory\n", progname); |
| 493 |
< |
quit(1); |
| 491 |
> |
if (scanin[i] == NULL) |
| 492 |
> |
goto memerr; |
| 493 |
> |
} |
| 494 |
> |
if (obarsize > 0) { |
| 495 |
> |
scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *)); |
| 496 |
> |
greybar = (float **)malloc(obarsize*sizeof(float *)); |
| 497 |
> |
if (scoutbar == NULL | greybar == NULL) |
| 498 |
> |
goto memerr; |
| 499 |
> |
for (i = 0; i < obarsize; i++) { |
| 500 |
> |
scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR)); |
| 501 |
> |
greybar[i] = (float *)malloc(ncols*sizeof(float)); |
| 502 |
> |
if (scoutbar[i] == NULL | greybar[i] == NULL) |
| 503 |
> |
goto memerr; |
| 504 |
|
} |
| 505 |
+ |
} else { |
| 506 |
+ |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
| 507 |
+ |
if (scanout == NULL) |
| 508 |
+ |
goto memerr; |
| 509 |
|
} |
| 422 |
– |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
| 423 |
– |
if (scanout == NULL) { |
| 424 |
– |
fprintf(stderr, "%s: out of memory\n", progname); |
| 425 |
– |
quit(1); |
| 426 |
– |
} |
| 510 |
|
/* record pixel aspect ratio */ |
| 511 |
|
if (!correctaspect) { |
| 512 |
|
d = order & YMAJOR ? x_c/y_r : y_r/x_c ; |
| 514 |
|
fputaspect(d, stdout); |
| 515 |
|
} |
| 516 |
|
/* record exposure */ |
| 517 |
< |
d = bright(exposure); |
| 517 |
> |
d = (*ourbright)(exposure); |
| 518 |
|
if (!FEQ(d,1.0)) |
| 519 |
|
fputexpos(d, stdout); |
| 520 |
|
/* record color correction */ |
| 526 |
|
printf("\n"); |
| 527 |
|
/* write out resolution */ |
| 528 |
|
fputresolu(order, ncols, nrows, stdout); |
| 529 |
+ |
return; |
| 530 |
+ |
memerr: |
| 531 |
+ |
fprintf(stderr, "%s: out of memory\n", progname); |
| 532 |
+ |
quit(1); |
| 533 |
+ |
} |
| 534 |
+ |
|
| 535 |
+ |
|
| 536 |
+ |
scan2sync(r) /* synchronize grey averages and output scan */ |
| 537 |
+ |
int r; |
| 538 |
+ |
{ |
| 539 |
+ |
static int nextrow = 0; |
| 540 |
+ |
COLOR ctmp; |
| 541 |
+ |
int ybot; |
| 542 |
+ |
register int c; |
| 543 |
+ |
/* average input scanlines */ |
| 544 |
+ |
while (nextrow <= r+orad && nextrow < nrows) { |
| 545 |
+ |
ybot = (nextrow+.5)*yres/nrows; |
| 546 |
+ |
for (c = 0; c < ncols; c++) { |
| 547 |
+ |
dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow); |
| 548 |
+ |
greybar[nextrow%obarsize][c] = (*ourbright)(ctmp); |
| 549 |
+ |
} |
| 550 |
+ |
/* and zero output scanline */ |
| 551 |
+ |
bzero((char *)scoutbar[nextrow%obarsize], ncols*sizeof(COLOR)); |
| 552 |
+ |
nextrow++; |
| 553 |
+ |
} |
| 554 |
+ |
/* point to top scanline for output */ |
| 555 |
+ |
if (r-orad >= 0) |
| 556 |
+ |
scanout = scoutbar[(r-orad)%obarsize]; |
| 557 |
+ |
else |
| 558 |
+ |
scanout = NULL; |
| 559 |
+ |
} |
| 560 |
+ |
|
| 561 |
+ |
|
| 562 |
+ |
scan2flush() /* flush output buffer */ |
| 563 |
+ |
{ |
| 564 |
+ |
register int r; |
| 565 |
+ |
|
| 566 |
+ |
for (r = nrows-orad; r < nrows; r++) |
| 567 |
+ |
if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0) |
| 568 |
+ |
break; |
| 569 |
+ |
if (fflush(stdout) < 0) { |
| 570 |
+ |
fprintf(stderr, "%s: write error at end of pass2\n", progname); |
| 571 |
+ |
quit(1); |
| 572 |
+ |
} |
| 573 |
|
} |
| 574 |
|
|
| 575 |
|
|