| 93 |
|
char **argv |
| 94 |
|
) |
| 95 |
|
{ |
| 96 |
< |
double d, expval = 1.0; |
| 97 |
< |
int i; |
| 96 |
> |
const char *inpmode = "r"; |
| 97 |
> |
double d, expval = 1.0; |
| 98 |
> |
int i; |
| 99 |
|
|
| 100 |
|
progname = argv[0]; |
| 101 |
|
mybright = &rgb_bright; /* default */ |
| 118 |
|
case 'o': /* original values */ |
| 119 |
|
original = argv[i][0] == '-'; |
| 120 |
|
break; |
| 121 |
+ |
case 'O': /* original watts/sr/m^2 */ |
| 122 |
+ |
original = -(argv[i][0] == '-'); |
| 123 |
+ |
break; |
| 124 |
|
case 'g': /* gamma correction */ |
| 125 |
|
gamcor = atof(argv[i+1]); |
| 126 |
|
if (argv[i][0] == '+') |
| 176 |
|
} |
| 177 |
|
break; |
| 178 |
|
case 'd': /* data only (no indices) */ |
| 179 |
< |
dataonly = argv[i][0] == '-'; |
| 179 |
> |
dataonly = (argv[i][0] == '-'); |
| 180 |
|
switch (argv[i][2]) { |
| 181 |
|
case '\0': |
| 182 |
|
case 'a': /* ascii */ |
| 255 |
|
else |
| 256 |
|
fmtid = "16-bit_grey"; |
| 257 |
|
} |
| 258 |
+ |
if (!reverse || (format != 'a') & (format != 'i')) |
| 259 |
+ |
inpmode = "rb"; |
| 260 |
|
/* assign reverse ordering */ |
| 261 |
|
rord[ord[0]] = 0; |
| 262 |
|
rord[ord[1]] = 1; |
| 263 |
|
rord[ord[2]] = 2; |
| 264 |
|
/* get input */ |
| 265 |
|
if (i == argc) { |
| 266 |
+ |
long n = skipbytes; |
| 267 |
+ |
if (inpmode[1] == 'b') |
| 268 |
+ |
SET_FILE_BINARY(stdin); |
| 269 |
+ |
while (n-- > 0) |
| 270 |
+ |
if (getchar() == EOF) { |
| 271 |
+ |
fprintf(stderr, |
| 272 |
+ |
"%s: cannot skip %ld bytes on standard input\n", |
| 273 |
+ |
progname, skipbytes); |
| 274 |
+ |
quit(1); |
| 275 |
+ |
} |
| 276 |
|
fin = stdin; |
| 277 |
|
} else if (i < argc) { |
| 278 |
< |
if ((fin = fopen(argv[i], "r")) == NULL) { |
| 278 |
> |
if ((fin = fopen(argv[i], inpmode)) == NULL) { |
| 279 |
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
| 280 |
|
progname, argv[i]); |
| 281 |
|
quit(1); |
| 282 |
|
} |
| 283 |
|
if (reverse && putprim != BRIGHT && i == argc-3) { |
| 284 |
< |
if ((fin2 = fopen(argv[i+1], "r")) == NULL) { |
| 284 |
> |
if ((fin2 = fopen(argv[i+1], inpmode)) == NULL) { |
| 285 |
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
| 286 |
|
progname, argv[i+1]); |
| 287 |
|
quit(1); |
| 288 |
|
} |
| 289 |
< |
if ((fin3 = fopen(argv[i+2], "r")) == NULL) { |
| 289 |
> |
if ((fin3 = fopen(argv[i+2], inpmode)) == NULL) { |
| 290 |
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
| 291 |
|
progname, argv[i+2]); |
| 292 |
|
quit(1); |
| 295 |
|
} else if (i != argc-1) |
| 296 |
|
fin = NULL; |
| 297 |
|
if (reverse && putprim != BRIGHT && !interleave) { |
| 298 |
< |
fin2 = fopen(argv[i], "r"); |
| 299 |
< |
fin3 = fopen(argv[i], "r"); |
| 298 |
> |
fin2 = fopen(argv[i], inpmode); |
| 299 |
> |
fin3 = fopen(argv[i], inpmode); |
| 300 |
|
} |
| 301 |
< |
if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL && |
| 302 |
< |
(fseek(fin2, skipbytes, 0) || |
| 303 |
< |
fseek(fin3, skipbytes, 0))))) { |
| 301 |
> |
if (skipbytes && (fseek(fin, skipbytes, SEEK_SET) || (fin2 != NULL && |
| 302 |
> |
fseek(fin2, skipbytes, SEEK_SET) | |
| 303 |
> |
fseek(fin3, skipbytes, SEEK_SET)))) { |
| 304 |
|
fprintf(stderr, "%s: cannot skip %ld bytes on input\n", |
| 305 |
|
progname, skipbytes); |
| 306 |
|
quit(1); |
| 312 |
|
} |
| 313 |
|
|
| 314 |
|
if (reverse) { |
| 299 |
– |
#if defined(_WIN32) || defined(_WIN64) |
| 315 |
|
SET_FILE_BINARY(stdout); |
| 301 |
– |
if (format != 'a' && format != 'i') |
| 302 |
– |
SET_FILE_BINARY(fin); |
| 303 |
– |
#endif |
| 316 |
|
/* get header */ |
| 317 |
|
if (header) { |
| 318 |
< |
if (checkheader(fin, fmtid, stdout) < 0) { |
| 319 |
< |
fprintf(stderr, "%s: wrong input format\n", |
| 320 |
< |
progname); |
| 318 |
> |
getheader(fin, checkhead, stdout); |
| 319 |
> |
if (wrongformat) { |
| 320 |
> |
fprintf(stderr, "%s: wrong input format (expected %s)\n", |
| 321 |
> |
progname, fmtid); |
| 322 |
|
quit(1); |
| 323 |
|
} |
| 324 |
|
if (fin2 != NULL) { |
| 356 |
|
if (outprims != stdprims) |
| 357 |
|
fputprims(outprims, stdout); |
| 358 |
|
fputformat(COLRFMT, stdout); |
| 359 |
< |
} else /* XYZ data */ |
| 359 |
> |
} else { /* XYZ data */ |
| 360 |
> |
if (original < 0) { |
| 361 |
> |
scalecolor(exposure, WHTEFFICACY); |
| 362 |
> |
doexposure++; |
| 363 |
> |
} |
| 364 |
|
fputformat(CIEFMT, stdout); |
| 365 |
+ |
} |
| 366 |
|
putchar('\n'); |
| 367 |
|
fputsresolu(&picres, stdout); /* always put resolution */ |
| 368 |
|
valtopix(); |
| 369 |
|
} else { |
| 370 |
< |
#if defined(_WIN32) || defined(_WIN64) |
| 353 |
< |
SET_FILE_BINARY(fin); |
| 354 |
< |
if (format != 'a' && format != 'i') |
| 370 |
> |
if ((format != 'a') & (format != 'i')) |
| 371 |
|
SET_FILE_BINARY(stdout); |
| 356 |
– |
#endif |
| 372 |
|
/* get header */ |
| 373 |
< |
getheader(fin, checkhead, NULL); |
| 373 |
> |
getheader(fin, checkhead, header ? stdout : (FILE *)NULL); |
| 374 |
|
if (wrongformat) { |
| 375 |
|
fprintf(stderr, |
| 376 |
|
"%s: input not a Radiance RGBE picture\n", |
| 381 |
|
fprintf(stderr, "%s: missing resolution\n", progname); |
| 382 |
|
quit(1); |
| 383 |
|
} |
| 384 |
+ |
if (original < 0 && mybright == &xyz_bright) { |
| 385 |
+ |
scalecolor(exposure, 1./WHTEFFICACY); |
| 386 |
+ |
doexposure++; |
| 387 |
+ |
} |
| 388 |
|
if (header) { |
| 389 |
|
printargs(i, argv, stdout); |
| 390 |
+ |
printf("NCOMP=%d\n", putprim==ALL ? 3 : 1); |
| 391 |
+ |
if (!resolution && dataonly && !uniq) |
| 392 |
+ |
printf("NCOLS=%d\nNROWS=%d\n", scanlen(&picres), |
| 393 |
+ |
numscans(&picres)); |
| 394 |
|
if (expval < .99 || expval > 1.01) |
| 395 |
|
fputexpos(expval, stdout); |
| 396 |
+ |
if (swapbytes) { |
| 397 |
+ |
if (nativebigendian()) |
| 398 |
+ |
puts("BigEndian=0"); |
| 399 |
+ |
else |
| 400 |
+ |
puts("BigEndian=1"); |
| 401 |
+ |
} else if ((format != 'a') & (format != 'i') & |
| 402 |
+ |
(format != 'b')) |
| 403 |
+ |
fputendian(stdout); |
| 404 |
|
fputformat(fmtid, stdout); |
| 405 |
|
putchar('\n'); |
| 406 |
|
} |
| 420 |
|
void *p |
| 421 |
|
) |
| 422 |
|
{ |
| 423 |
< |
char fmt[32]; |
| 423 |
> |
FILE *fout = (FILE *)p; |
| 424 |
> |
char fmt[MAXFMTLEN]; |
| 425 |
|
double d; |
| 426 |
|
COLOR ctmp; |
| 427 |
+ |
int rv; |
| 428 |
|
|
| 429 |
|
if (formatval(fmt, line)) { |
| 430 |
< |
if (!strcmp(fmt, CIEFMT)) |
| 430 |
> |
if (reverse) |
| 431 |
> |
wrongformat = strcmp(fmt, fmtid); |
| 432 |
> |
else if (!strcmp(fmt, CIEFMT)) |
| 433 |
|
mybright = &xyz_bright; |
| 434 |
|
else if (!strcmp(fmt, COLRFMT)) |
| 435 |
|
mybright = &rgb_bright; |
| 436 |
|
else |
| 437 |
|
wrongformat++; |
| 438 |
< |
} else if (original && isexpos(line)) { |
| 438 |
> |
return(1); |
| 439 |
> |
} |
| 440 |
> |
if (!strncmp(line,"NROWS=",6) || |
| 441 |
> |
!strncmp(line,"NCOLS=",6) || |
| 442 |
> |
!strncmp(line,"NCOMP=",6)) |
| 443 |
> |
return(1); |
| 444 |
> |
|
| 445 |
> |
if (original && isexpos(line)) { |
| 446 |
|
d = 1.0/exposval(line); |
| 447 |
|
scalecolor(exposure, d); |
| 448 |
|
doexposure++; |
| 449 |
< |
} else if (original && iscolcor(line)) { |
| 449 |
> |
return(1); |
| 450 |
> |
} |
| 451 |
> |
if (original && iscolcor(line)) { |
| 452 |
|
colcorval(ctmp, line); |
| 453 |
|
setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED), |
| 454 |
|
colval(exposure,GRN)/colval(ctmp,GRN), |
| 455 |
|
colval(exposure,BLU)/colval(ctmp,BLU)); |
| 456 |
|
doexposure++; |
| 457 |
< |
} else if (header) |
| 458 |
< |
fputs(line, stdout); |
| 457 |
> |
return(1); |
| 458 |
> |
} |
| 459 |
> |
if ((rv = isbigendian(line)) >= 0) { |
| 460 |
> |
if (reverse) |
| 461 |
> |
swapbytes = (nativebigendian() != rv); |
| 462 |
> |
return(1); |
| 463 |
> |
} |
| 464 |
> |
if (fout != NULL) |
| 465 |
> |
fputs(line, fout); |
| 466 |
|
return(0); |
| 467 |
|
} |
| 468 |
|
|
| 470 |
|
static void |
| 471 |
|
pixtoval(void) /* convert picture to values */ |
| 472 |
|
{ |
| 473 |
< |
register COLOR *scanln; |
| 473 |
> |
COLOR *scanln; |
| 474 |
|
int dogamma; |
| 475 |
|
COLOR lastc; |
| 476 |
|
RREAL hv[2]; |
| 477 |
|
int startprim, endprim; |
| 478 |
|
long startpos; |
| 479 |
< |
int y; |
| 429 |
< |
register int x; |
| 479 |
> |
int x, y; |
| 480 |
|
|
| 481 |
|
scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); |
| 482 |
|
if (scanln == NULL) { |
| 484 |
|
quit(1); |
| 485 |
|
} |
| 486 |
|
dogamma = gamcor < .95 || gamcor > 1.05; |
| 487 |
< |
if (putprim == ALL && !interleave) { |
| 487 |
> |
if ((putprim == ALL) & !interleave) { |
| 488 |
|
startprim = RED; endprim = BLU; |
| 489 |
|
startpos = ftell(fin); |
| 490 |
|
} else { |
| 491 |
|
startprim = putprim; endprim = putprim; |
| 492 |
|
} |
| 493 |
|
for (putprim = startprim; putprim <= endprim; putprim++) { |
| 494 |
< |
if (putprim != startprim && fseek(fin, startpos, 0)) { |
| 494 |
> |
if (putprim != startprim && fseek(fin, startpos, SEEK_SET)) { |
| 495 |
|
fprintf(stderr, "%s: seek error on input file\n", |
| 496 |
|
progname); |
| 497 |
|
quit(1); |
| 543 |
|
static void |
| 544 |
|
valtopix(void) /* convert values to a pixel file */ |
| 545 |
|
{ |
| 546 |
< |
int dogamma; |
| 547 |
< |
register COLOR *scanln; |
| 548 |
< |
int y; |
| 549 |
< |
register int x; |
| 546 |
> |
int dogamma; |
| 547 |
> |
COLOR *scanln; |
| 548 |
> |
COLR rgbe; |
| 549 |
> |
int x, y; |
| 550 |
|
|
| 551 |
|
scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); |
| 552 |
|
if (scanln == NULL) { |
| 575 |
|
pow(colval(scanln[x],BLU), gamcor)); |
| 576 |
|
if (doexposure) |
| 577 |
|
multcolor(scanln[x], exposure); |
| 578 |
+ |
if (uniq) { /* uncompressed? */ |
| 579 |
+ |
setcolr(rgbe, scanln[x][RED], |
| 580 |
+ |
scanln[x][GRN], |
| 581 |
+ |
scanln[x][BLU]); |
| 582 |
+ |
if (putbinary(rgbe, sizeof(COLR), 1, stdout) != 1) |
| 583 |
+ |
goto writerr; |
| 584 |
+ |
} |
| 585 |
|
} |
| 586 |
< |
if (fwritescan(scanln, scanlen(&picres), stdout) < 0) { |
| 587 |
< |
fprintf(stderr, "%s: write error\n", progname); |
| 588 |
< |
quit(1); |
| 532 |
< |
} |
| 586 |
> |
/* write scan if compressed */ |
| 587 |
> |
if (!uniq && fwritescan(scanln, scanlen(&picres), stdout) < 0) |
| 588 |
> |
goto writerr; |
| 589 |
|
} |
| 590 |
|
free((void *)scanln); |
| 591 |
+ |
return; |
| 592 |
+ |
writerr: |
| 593 |
+ |
fprintf(stderr, "%s: write error\n", progname); |
| 594 |
+ |
quit(1); |
| 595 |
|
} |
| 596 |
|
|
| 597 |
|
|
| 999 |
|
COLOR col |
| 1000 |
|
) |
| 1001 |
|
{ |
| 1002 |
< |
register int i; |
| 1002 |
> |
int i; |
| 1003 |
|
uby8 vb; |
| 1004 |
|
|
| 1005 |
|
i = (*mybright)(col)*256.; |
| 1152 |
|
if (fin2 == NULL) |
| 1153 |
|
goto namerr; |
| 1154 |
|
if (fseek(fin2, |
| 1155 |
< |
(long)sizeof(float)*picres.xr*picres.yr, 1)) |
| 1155 |
> |
(long)sizeof(float)*picres.xr*picres.yr, SEEK_CUR)) |
| 1156 |
|
goto seekerr; |
| 1157 |
|
if (fseek(fin3, |
| 1158 |
< |
(long)sizeof(float)*2*picres.xr*picres.yr, 1)) |
| 1158 |
> |
(long)sizeof(float)*2*picres.xr*picres.yr, SEEK_CUR)) |
| 1159 |
|
goto seekerr; |
| 1160 |
|
} |
| 1161 |
|
} |
| 1174 |
|
if (fin2 == NULL) |
| 1175 |
|
goto namerr; |
| 1176 |
|
if (fseek(fin2, |
| 1177 |
< |
(long)sizeof(double)*picres.xr*picres.yr, 1)) |
| 1177 |
> |
(long)sizeof(double)*picres.xr*picres.yr, SEEK_CUR)) |
| 1178 |
|
goto seekerr; |
| 1179 |
|
if (fseek(fin3, |
| 1180 |
< |
(long)sizeof(double)*2*picres.xr*picres.yr, 1)) |
| 1180 |
> |
(long)sizeof(double)*2*picres.xr*picres.yr, SEEK_CUR)) |
| 1181 |
|
goto seekerr; |
| 1182 |
|
} |
| 1183 |
|
} |
| 1214 |
|
if (fin2 == NULL) |
| 1215 |
|
goto namerr; |
| 1216 |
|
if (fseek(fin2, |
| 1217 |
< |
(long)sizeof(uby8)*picres.xr*picres.yr, 1)) |
| 1217 |
> |
(long)sizeof(uby8)*picres.xr*picres.yr, SEEK_CUR)) |
| 1218 |
|
goto seekerr; |
| 1219 |
|
if (fseek(fin3, |
| 1220 |
< |
(long)sizeof(uby8)*2*picres.xr*picres.yr, 1)) |
| 1220 |
> |
(long)sizeof(uby8)*2*picres.xr*picres.yr, SEEK_CUR)) |
| 1221 |
|
goto seekerr; |
| 1222 |
|
} |
| 1223 |
|
} |
| 1236 |
|
if (fin2 == NULL) |
| 1237 |
|
goto namerr; |
| 1238 |
|
if (fseek(fin2, |
| 1239 |
< |
(long)sizeof(uint16)*picres.xr*picres.yr, 1)) |
| 1239 |
> |
(long)sizeof(uint16)*picres.xr*picres.yr, SEEK_CUR)) |
| 1240 |
|
goto seekerr; |
| 1241 |
|
if (fseek(fin3, |
| 1242 |
< |
(long)sizeof(uint16)*2*picres.xr*picres.yr, 1)) |
| 1242 |
> |
(long)sizeof(uint16)*2*picres.xr*picres.yr, SEEK_CUR)) |
| 1243 |
|
goto seekerr; |
| 1244 |
|
} |
| 1245 |
|
} |