| 11 |
|
#include "copyright.h" |
| 12 |
|
|
| 13 |
|
#include <signal.h> |
| 14 |
< |
#include <string.h> |
| 15 |
< |
|
| 14 |
> |
#include "pfilt.h" |
| 15 |
|
#include "platform.h" |
| 17 |
– |
#include "standard.h" |
| 18 |
– |
#include "rtio.h" |
| 19 |
– |
#include "color.h" |
| 16 |
|
#include "view.h" |
| 21 |
– |
#include "paths.h" |
| 22 |
– |
#include "pfilt.h" |
| 17 |
|
|
| 24 |
– |
|
| 18 |
|
#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) |
| 19 |
|
|
| 20 |
|
double CHECKRAD = 2.0; /* radius to check for filtering */ |
| 68 |
|
int ybrad; /* y box size */ |
| 69 |
|
|
| 70 |
|
int barsize; /* size of input scan bar */ |
| 71 |
< |
COLOR **scanin; /* input scan bar */ |
| 72 |
< |
COLOR *scanout; /* output scan line */ |
| 73 |
< |
COLOR **scoutbar; /* output scan bar (if thresh > 0) */ |
| 71 |
> |
COLORV **scanin; /* input scan bar */ |
| 72 |
> |
COLORV *scanout; /* output scan line */ |
| 73 |
> |
COLORV **scoutbar; /* output scan bar (if thresh > 0) */ |
| 74 |
|
float **greybar; /* grey-averaged input values */ |
| 75 |
|
int obarsize = 0; /* size of output scan bar */ |
| 76 |
|
int orad = 0; /* output window radius */ |
| 77 |
|
|
| 78 |
< |
char *progname; |
| 79 |
< |
|
| 80 |
< |
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); |
| 78 |
> |
static void copyfile(FILE *infp, FILE *out); |
| 79 |
> |
static void pass1(FILE *infp); |
| 80 |
> |
static void pass2(FILE *infp); |
| 81 |
|
static void scan2init(void); |
| 82 |
|
static void scan2sync(int r); |
| 83 |
|
static void scan2flush(void); |
| 84 |
|
|
| 85 |
|
|
| 86 |
+ |
static double |
| 87 |
+ |
rgb_bright( |
| 88 |
+ |
SCOLOR clr |
| 89 |
+ |
) |
| 90 |
+ |
{ |
| 91 |
+ |
return(bright(clr)); |
| 92 |
+ |
} |
| 93 |
+ |
|
| 94 |
+ |
|
| 95 |
+ |
static double |
| 96 |
+ |
xyz_bright( |
| 97 |
+ |
SCOLOR clr |
| 98 |
+ |
) |
| 99 |
+ |
{ |
| 100 |
+ |
return(colval(clr,CIEY)); |
| 101 |
+ |
} |
| 102 |
+ |
|
| 103 |
+ |
|
| 104 |
+ |
static double |
| 105 |
+ |
spec_bright( |
| 106 |
+ |
SCOLOR clr |
| 107 |
+ |
) |
| 108 |
+ |
{ |
| 109 |
+ |
return(pbright(clr)); |
| 110 |
+ |
} |
| 111 |
+ |
|
| 112 |
+ |
|
| 113 |
+ |
brightfunc_t *ourbright = rgb_bright; |
| 114 |
+ |
|
| 115 |
+ |
|
| 116 |
+ |
static int |
| 117 |
+ |
headline( /* process line from header */ |
| 118 |
+ |
char *s, |
| 119 |
+ |
void *p |
| 120 |
+ |
) |
| 121 |
+ |
{ |
| 122 |
+ |
char fmt[MAXFMTLEN]; |
| 123 |
+ |
|
| 124 |
+ |
fputs(s, stdout); /* copy to output */ |
| 125 |
+ |
if (isaspect(s)) /* get aspect ratio */ |
| 126 |
+ |
inpaspect *= aspectval(s); |
| 127 |
+ |
else if (isexpos(s)) /* get exposure */ |
| 128 |
+ |
hotlvl *= exposval(s); |
| 129 |
+ |
else if (isncomp(s)) /* get #components (spectral) */ |
| 130 |
+ |
NCSAMP = ncompval(s); |
| 131 |
+ |
else if (iswlsplit(s)) /* get wavelength partitions */ |
| 132 |
+ |
wlsplitval(WLPART, s); |
| 133 |
+ |
else if (formatval(fmt, s)) { /* get format */ |
| 134 |
+ |
wrongformat = 0; |
| 135 |
+ |
if (!strcmp(COLRFMT, fmt)) |
| 136 |
+ |
ourbright = rgb_bright; |
| 137 |
+ |
else if (!strcmp(CIEFMT, fmt)) |
| 138 |
+ |
ourbright = xyz_bright; |
| 139 |
+ |
else if (!strcmp(SPECFMT, fmt)) |
| 140 |
+ |
ourbright = spec_bright; |
| 141 |
+ |
else |
| 142 |
+ |
wrongformat = !globmatch(PICFMT, fmt); |
| 143 |
+ |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
| 144 |
+ |
gotview++; |
| 145 |
+ |
return(0); |
| 146 |
+ |
} |
| 147 |
+ |
|
| 148 |
+ |
|
| 149 |
|
int |
| 150 |
|
main( |
| 151 |
|
int argc, |
| 159 |
|
double outaspect = 0.0; |
| 160 |
|
double d; |
| 161 |
|
int i, j; |
| 162 |
+ |
|
| 163 |
+ |
fixargv0(argv[0]); /* sets global progname */ |
| 164 |
|
SET_DEFAULT_BINARY(); |
| 165 |
|
SET_FILE_BINARY(stdin); |
| 166 |
|
SET_FILE_BINARY(stdout); |
| 178 |
|
signal(SIGXCPU, quit); |
| 179 |
|
signal(SIGXFSZ, quit); |
| 180 |
|
#endif |
| 128 |
– |
|
| 129 |
– |
progname = argv[0] = fixargv0(argv[0]); |
| 130 |
– |
|
| 181 |
|
for (i = 1; i < argc; i++) |
| 182 |
|
if (argv[i][0] == '-') |
| 183 |
|
switch (argv[i][1]) { |
| 212 |
|
if (d < 1e-20 || d > 1e20) { |
| 213 |
|
fprintf(stderr, |
| 214 |
|
"%s: exposure out of range\n", |
| 215 |
< |
argv[0]); |
| 215 |
> |
progname); |
| 216 |
|
quit(1); |
| 217 |
|
} |
| 218 |
|
switch (argv[i][2]) { |
| 296 |
|
fin = stdin; |
| 297 |
|
else { |
| 298 |
|
tfname = mktemp(template); |
| 299 |
< |
if ((fin = fopen(tfname, "w+")) == NULL) { |
| 299 |
> |
if ((fin = fopen(tfname, "w+b")) == NULL) { |
| 300 |
|
fprintf(stderr, "%s: can't create ", progname); |
| 301 |
|
fprintf(stderr, "temp file \"%s\"\n", tfname); |
| 302 |
|
quit(1); |
| 308 |
|
} |
| 309 |
|
} |
| 310 |
|
} else if (i == argc-1) { |
| 311 |
< |
if ((fin = fopen(argv[i], "r")) == NULL) { |
| 311 |
> |
if ((fin = fopen(argv[i], "rb")) == NULL) { |
| 312 |
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
| 313 |
|
progname, argv[i]); |
| 314 |
|
quit(1); |
| 324 |
|
progname); |
| 325 |
|
quit(1); |
| 326 |
|
} |
| 327 |
+ |
if ((ourbright == spec_bright) ^ (NCSAMP > 3) || |
| 328 |
+ |
setspectrsamp(CNDX, WLPART) < 0) { |
| 329 |
+ |
fprintf(stderr, "%s: bad number of components\n", progname); |
| 330 |
+ |
quit(1); |
| 331 |
+ |
} |
| 332 |
|
/* add new header info. */ |
| 333 |
|
printargs(i, argv, stdout); |
| 334 |
|
/* get picture size */ |
| 377 |
|
} |
| 378 |
|
|
| 379 |
|
|
| 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)) /* get exposure */ |
| 358 |
– |
hotlvl *= exposval(s); |
| 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 |
– |
|
| 380 |
|
static void |
| 381 |
|
copyfile( /* copy a file */ |
| 382 |
< |
register FILE *in, |
| 383 |
< |
register FILE *out |
| 382 |
> |
FILE *infp, |
| 383 |
> |
FILE *out |
| 384 |
|
) |
| 385 |
|
{ |
| 386 |
< |
register int c; |
| 386 |
> |
int c; |
| 387 |
|
|
| 388 |
< |
while ((c = getc(in)) != EOF) |
| 388 |
> |
while ((c = getc(infp)) != EOF) |
| 389 |
|
putc(c, out); |
| 390 |
|
|
| 391 |
|
if (ferror(out)) { |
| 397 |
|
|
| 398 |
|
static void |
| 399 |
|
pass1( /* first pass of picture file */ |
| 400 |
< |
FILE *in |
| 400 |
> |
FILE *infp |
| 401 |
|
) |
| 402 |
|
{ |
| 403 |
|
int i; |
| 404 |
< |
COLOR *scan; |
| 404 |
> |
COLORV *scan; |
| 405 |
|
|
| 406 |
|
pass1init(); |
| 407 |
|
|
| 408 |
< |
scan = (COLOR *)malloc(xres*sizeof(COLOR)); |
| 408 |
> |
scan = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV)); |
| 409 |
|
if (scan == NULL) { |
| 410 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
| 411 |
|
quit(1); |
| 412 |
|
} |
| 413 |
|
for (i = 0; i < yres; i++) { |
| 414 |
< |
if (freadscan(scan, xres, in) < 0) { |
| 414 |
> |
if (freadsscan(scan, NCSAMP, xres, infp) < 0) { |
| 415 |
|
nrows = (long)nrows * i / yres; /* adjust frame */ |
| 416 |
|
if (nrows <= 0) { |
| 417 |
|
fprintf(stderr, "%s: empty frame\n", progname); |
| 426 |
|
} |
| 427 |
|
pass1scan(scan, i); |
| 428 |
|
} |
| 429 |
< |
free((void *)scan); |
| 429 |
> |
free(scan); |
| 430 |
|
} |
| 431 |
|
|
| 432 |
|
|
| 433 |
|
static void |
| 434 |
|
pass2( /* last pass on file, write to stdout */ |
| 435 |
< |
FILE *in |
| 435 |
> |
FILE *infp |
| 436 |
|
) |
| 437 |
|
{ |
| 438 |
|
int yread; |
| 446 |
|
ycent = (r+.5)*yres/nrows; |
| 447 |
|
while (yread <= ycent+yrad) { |
| 448 |
|
if (yread < yres) { |
| 449 |
< |
if (freadscan(scanin[yread%barsize], |
| 450 |
< |
xres, in) < 0) { |
| 449 |
> |
if (freadsscan(scanin[yread%barsize], |
| 450 |
> |
NCSAMP, xres, infp) < 0) { |
| 451 |
|
fprintf(stderr, |
| 452 |
|
"%s: truncated input (y=%d)\n", |
| 453 |
|
progname, yres-1-yread); |
| 457 |
|
} |
| 458 |
|
yread++; |
| 459 |
|
} |
| 460 |
< |
if (obarsize > 0) |
| 460 |
> |
if (obarsize > 0) /* => thresh > FTINY */ |
| 461 |
|
scan2sync(r); |
| 462 |
|
for (c = 0; c < ncols; c++) { |
| 463 |
|
xcent = (c+.5)*xres/ncols; |
| 464 |
|
if (thresh > FTINY) |
| 465 |
|
dothresh(xcent, ycent, c, r); |
| 466 |
|
else if (rad > FTINY) |
| 467 |
< |
dogauss(scanout[c], xcent, ycent, c, r); |
| 467 |
> |
dogauss(scanout+c*NCSAMP, xcent, ycent, c, r); |
| 468 |
|
else |
| 469 |
< |
dobox(scanout[c], xcent, ycent, c, r); |
| 469 |
> |
dobox(scanout+c*NCSAMP, xcent, ycent, c, r); |
| 470 |
|
} |
| 471 |
< |
if (scanout != NULL && fwritescan(scanout, ncols, stdout) < 0) { |
| 471 |
> |
if (scanout != NULL && fwritesscan(scanout, NCSAMP, ncols, stdout) < 0) { |
| 472 |
|
fprintf(stderr, "%s: write error in pass2\n", progname); |
| 473 |
|
quit(1); |
| 474 |
|
} |
| 475 |
|
} |
| 476 |
|
/* skip leftover input */ |
| 477 |
|
while (yread < yres) { |
| 478 |
< |
if (freadscan(scanin[0], xres, in) < 0) |
| 478 |
> |
if (freadscolrs((uby8 *)tempbuffer(xres*(NCSAMP+1)), |
| 479 |
> |
NCSAMP, xres, infp) < 0) |
| 480 |
|
break; |
| 481 |
|
yread++; |
| 482 |
|
} |
| 489 |
|
{ |
| 490 |
|
COLOR ctmp; |
| 491 |
|
double d; |
| 492 |
< |
register int i; |
| 492 |
> |
int i; |
| 493 |
|
|
| 494 |
|
xbrad = xres/ncols/2 + 1; |
| 495 |
|
ybrad = yres/nrows/2 + 1; |
| 512 |
|
yrad = ybrad; |
| 513 |
|
} |
| 514 |
|
barsize = 2*yrad + 1; |
| 515 |
< |
scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); |
| 515 |
> |
scanin = (COLORV **)malloc(barsize*sizeof(COLORV *)); |
| 516 |
|
if (scanin == NULL) |
| 517 |
|
goto memerr; |
| 518 |
|
for (i = 0; i < barsize; i++) { |
| 519 |
< |
scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
| 519 |
> |
scanin[i] = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV)); |
| 520 |
|
if (scanin[i] == NULL) |
| 521 |
|
goto memerr; |
| 522 |
|
} |
| 523 |
|
if (obarsize > 0) { |
| 524 |
< |
scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *)); |
| 524 |
> |
scoutbar = (COLORV **)malloc(obarsize*sizeof(COLORV *)); |
| 525 |
|
greybar = (float **)malloc(obarsize*sizeof(float *)); |
| 526 |
|
if ((scoutbar == NULL) | (greybar == NULL)) |
| 527 |
|
goto memerr; |
| 528 |
|
for (i = 0; i < obarsize; i++) { |
| 529 |
< |
scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR)); |
| 529 |
> |
scoutbar[i] = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV)); |
| 530 |
|
greybar[i] = (float *)malloc(ncols*sizeof(float)); |
| 531 |
|
if ((scoutbar[i] == NULL) | (greybar[i] == NULL)) |
| 532 |
|
goto memerr; |
| 533 |
|
} |
| 534 |
|
} else { |
| 535 |
< |
scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); |
| 535 |
> |
scanout = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV)); |
| 536 |
|
if (scanout == NULL) |
| 537 |
|
goto memerr; |
| 538 |
|
} |
| 539 |
|
/* record pixel aspect ratio */ |
| 540 |
|
if (!correctaspect) { |
| 541 |
|
d = order & YMAJOR ? x_c/y_r : y_r/x_c ; |
| 542 |
< |
if (!FEQ(d,1.0)) |
| 542 |
> |
if ((d < 0.995) | (d > 1.005)) |
| 543 |
|
fputaspect(d, stdout); |
| 544 |
|
} |
| 545 |
|
/* record exposure */ |
| 546 |
< |
d = (*ourbright)(exposure); |
| 546 |
> |
d = bright(exposure); |
| 547 |
|
if (!FEQ(d,1.0)) |
| 548 |
|
fputexpos(d, stdout); |
| 549 |
|
/* record color correction */ |
| 568 |
|
) |
| 569 |
|
{ |
| 570 |
|
static int nextrow = 0; |
| 571 |
< |
COLOR ctmp; |
| 571 |
> |
SCOLOR ctmp; |
| 572 |
|
int ybot; |
| 573 |
< |
register int c; |
| 573 |
> |
int c; |
| 574 |
|
/* average input scanlines */ |
| 575 |
|
while (nextrow <= r+orad && nextrow < nrows) { |
| 576 |
|
ybot = (nextrow+.5)*yres/nrows; |
| 579 |
|
greybar[nextrow%obarsize][c] = (*ourbright)(ctmp); |
| 580 |
|
} |
| 581 |
|
/* and zero output scanline */ |
| 582 |
< |
memset((char *)scoutbar[nextrow%obarsize], '\0', ncols*sizeof(COLOR)); |
| 582 |
> |
memset(scoutbar[nextrow%obarsize], 0, ncols*NCSAMP*sizeof(COLORV)); |
| 583 |
|
nextrow++; |
| 584 |
|
} |
| 585 |
|
/* point to top scanline for output */ |
| 593 |
|
static void |
| 594 |
|
scan2flush(void) /* flush output buffer */ |
| 595 |
|
{ |
| 596 |
< |
register int r; |
| 596 |
> |
int r; |
| 597 |
|
|
| 598 |
|
for (r = nrows-orad; r < nrows; r++) |
| 599 |
< |
if (fwritescan(scoutbar[r%obarsize], ncols, stdout) < 0) |
| 599 |
> |
if (fwritesscan(scoutbar[r%obarsize], NCSAMP, ncols, stdout) < 0) |
| 600 |
|
break; |
| 601 |
|
if (fflush(stdout) < 0) { |
| 602 |
|
fprintf(stderr, "%s: write error at end of pass2\n", progname); |