| 26 |
|
int putprim = ALL; /* what to put out */ |
| 27 |
|
int reverse = 0; /* reverse conversion? */ |
| 28 |
|
int format = 'a'; /* input/output format */ |
| 29 |
< |
char *fmtid = "ascii"; /* format identifier for header */ |
| 29 |
> |
const char *fmtid = "ascii"; /* format identifier for header */ |
| 30 |
|
int header = 1; /* do header? */ |
| 31 |
|
long skipbytes = 0; /* skip bytes in input? */ |
| 32 |
|
int swapbytes = 0; /* swap bytes? */ |
| 36 |
|
int wrongformat = 0; /* wrong input format? */ |
| 37 |
|
double gamcor = 1.0; /* gamma correction */ |
| 38 |
|
|
| 39 |
+ |
RGBPRIMP outprims = stdprims; /* output primaries for reverse conversion */ |
| 40 |
+ |
RGBPRIMS myprims; |
| 41 |
+ |
|
| 42 |
|
int ord[3] = {RED, GRN, BLU}; /* RGB ordering */ |
| 43 |
|
int rord[4]; /* reverse ordering */ |
| 44 |
|
|
| 45 |
|
COLOR exposure = WHTCOLOR; |
| 46 |
|
|
| 44 |
– |
char *progname; |
| 45 |
– |
|
| 47 |
|
FILE *fin; |
| 48 |
|
FILE *fin2 = NULL, *fin3 = NULL; /* for other color channels */ |
| 49 |
|
|
| 64 |
|
static putfunc_t putcascii, putcint, putcdouble, putcfloat, putcbyte, putcword; |
| 65 |
|
static putfunc_t putpascii, putpint, putpdouble, putpfloat, putpbyte, putpword; |
| 66 |
|
|
| 67 |
+ |
static int IDformat(const char *s); |
| 68 |
|
static void set_io(void); |
| 69 |
|
static void pixtoval(void); |
| 70 |
|
static void valtopix(void); |
| 69 |
– |
static void swap16(uint16 *wp, int n); |
| 70 |
– |
static void swap32(uint32 *wp, int n); |
| 71 |
– |
static void swap64(char *wp, int n); |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
static double |
| 92 |
|
char **argv |
| 93 |
|
) |
| 94 |
|
{ |
| 95 |
< |
double d, expval = 1.0; |
| 96 |
< |
int i; |
| 95 |
> |
const char *inpmode = "r"; |
| 96 |
> |
double d, expval = 1.0; |
| 97 |
> |
int i; |
| 98 |
|
|
| 99 |
< |
progname = argv[0]; |
| 99 |
> |
fixargv0(argv[0]); /* sets global progname */ |
| 100 |
|
mybright = &rgb_bright; /* default */ |
| 101 |
+ |
picres.rt = PIXSTANDARD; |
| 102 |
|
|
| 103 |
|
for (i = 1; i < argc; i++) |
| 104 |
|
if (argv[i][0] == '-' || argv[i][0] == '+') |
| 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] == '+') |
| 153 |
|
case 'b': /* brightness values */ |
| 154 |
|
putprim = argv[i][0] == '-' ? BRIGHT : ALL; |
| 155 |
|
break; |
| 156 |
< |
case 'p': /* put primary */ |
| 156 |
> |
case 'p': /* primary controls */ |
| 157 |
|
switch (argv[i][2]) { |
| 158 |
+ |
/* these two options affect -r conversion */ |
| 159 |
+ |
case '\0': |
| 160 |
+ |
myprims[RED][CIEX] = atof(argv[++i]); |
| 161 |
+ |
myprims[RED][CIEY] = atof(argv[++i]); |
| 162 |
+ |
myprims[GRN][CIEX] = atof(argv[++i]); |
| 163 |
+ |
myprims[GRN][CIEY] = atof(argv[++i]); |
| 164 |
+ |
myprims[BLU][CIEX] = atof(argv[++i]); |
| 165 |
+ |
myprims[BLU][CIEY] = atof(argv[++i]); |
| 166 |
+ |
myprims[WHT][CIEX] = atof(argv[++i]); |
| 167 |
+ |
myprims[WHT][CIEY] = atof(argv[++i]); |
| 168 |
+ |
outprims = myprims; |
| 169 |
+ |
break; |
| 170 |
+ |
case 'x': case 'X': outprims = NULL; break; |
| 171 |
+ |
/* the following options affect +r only */ |
| 172 |
|
case 'r': case 'R': putprim = RED; break; |
| 173 |
|
case 'g': case 'G': putprim = GRN; break; |
| 174 |
|
case 'b': case 'B': putprim = BLU; break; |
| 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 */ |
| 218 |
|
} |
| 219 |
|
break; |
| 220 |
|
case 'x': /* x resolution */ |
| 221 |
< |
case 'X': /* x resolution */ |
| 221 |
> |
case 'X': |
| 222 |
|
resolution = 0; |
| 223 |
|
if (argv[i][0] == '-') |
| 224 |
|
picres.rt |= XDECR; |
| 225 |
+ |
else |
| 226 |
+ |
picres.rt &= ~XDECR; |
| 227 |
+ |
if (picres.yr == 0) |
| 228 |
+ |
picres.rt &= ~YMAJOR; |
| 229 |
|
picres.xr = atoi(argv[++i]); |
| 230 |
|
break; |
| 231 |
|
case 'y': /* y resolution */ |
| 232 |
< |
case 'Y': /* y resolution */ |
| 232 |
> |
case 'Y': |
| 233 |
|
resolution = 0; |
| 234 |
|
if (argv[i][0] == '-') |
| 235 |
|
picres.rt |= YDECR; |
| 236 |
+ |
else |
| 237 |
+ |
picres.rt &= ~YDECR; |
| 238 |
|
if (picres.xr == 0) |
| 239 |
|
picres.rt |= YMAJOR; |
| 240 |
|
picres.yr = atoi(argv[++i]); |
| 249 |
|
else |
| 250 |
|
break; |
| 251 |
|
/* recognize special formats */ |
| 252 |
< |
if (dataonly && format == 'b') { |
| 252 |
> |
if (dataonly & (format == 'b')) { |
| 253 |
|
if (putprim == ALL) |
| 254 |
|
fmtid = "24-bit_rgb"; |
| 255 |
|
else |
| 256 |
|
fmtid = "8-bit_grey"; |
| 257 |
|
} |
| 258 |
< |
if (dataonly && format == 'w') { |
| 258 |
> |
if (dataonly & (format == 'w')) { |
| 259 |
|
if (putprim == ALL) |
| 260 |
|
fmtid = "48-bit_rgb"; |
| 261 |
|
else |
| 262 |
|
fmtid = "16-bit_grey"; |
| 263 |
|
} |
| 264 |
+ |
if (!reverse | ((format != 'a') & (format != 'i'))) |
| 265 |
+ |
inpmode = "rb"; |
| 266 |
|
/* assign reverse ordering */ |
| 267 |
|
rord[ord[0]] = 0; |
| 268 |
|
rord[ord[1]] = 1; |
| 269 |
|
rord[ord[2]] = 2; |
| 270 |
|
/* get input */ |
| 271 |
|
if (i == argc) { |
| 272 |
+ |
long n = skipbytes; |
| 273 |
+ |
if (inpmode[1] == 'b') |
| 274 |
+ |
SET_FILE_BINARY(stdin); |
| 275 |
+ |
while (n-- > 0) |
| 276 |
+ |
if (getchar() == EOF) { |
| 277 |
+ |
fprintf(stderr, |
| 278 |
+ |
"%s: cannot skip %ld bytes on standard input\n", |
| 279 |
+ |
progname, skipbytes); |
| 280 |
+ |
quit(1); |
| 281 |
+ |
} |
| 282 |
|
fin = stdin; |
| 283 |
|
} else if (i < argc) { |
| 284 |
< |
if ((fin = fopen(argv[i], "r")) == NULL) { |
| 284 |
> |
if ((fin = fopen(argv[i], inpmode)) == NULL) { |
| 285 |
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
| 286 |
|
progname, argv[i]); |
| 287 |
|
quit(1); |
| 288 |
|
} |
| 289 |
< |
if (reverse && putprim != BRIGHT && i == argc-3) { |
| 290 |
< |
if ((fin2 = fopen(argv[i+1], "r")) == NULL) { |
| 289 |
> |
if (reverse & (putprim != BRIGHT ) & (i == argc-3)) { |
| 290 |
> |
if ((fin2 = fopen(argv[i+1], inpmode)) == NULL) { |
| 291 |
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
| 292 |
|
progname, argv[i+1]); |
| 293 |
|
quit(1); |
| 294 |
|
} |
| 295 |
< |
if ((fin3 = fopen(argv[i+2], "r")) == NULL) { |
| 295 |
> |
if ((fin3 = fopen(argv[i+2], inpmode)) == NULL) { |
| 296 |
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
| 297 |
|
progname, argv[i+2]); |
| 298 |
|
quit(1); |
| 300 |
|
interleave = -1; |
| 301 |
|
} else if (i != argc-1) |
| 302 |
|
fin = NULL; |
| 303 |
< |
if (reverse && putprim != BRIGHT && !interleave) { |
| 304 |
< |
fin2 = fopen(argv[i], "r"); |
| 305 |
< |
fin3 = fopen(argv[i], "r"); |
| 303 |
> |
if (reverse & (putprim != BRIGHT) & !interleave) { |
| 304 |
> |
fin2 = fopen(argv[i], inpmode); |
| 305 |
> |
fin3 = fopen(argv[i], inpmode); |
| 306 |
|
} |
| 307 |
< |
if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL && |
| 308 |
< |
(fseek(fin2, skipbytes, 0) || |
| 309 |
< |
fseek(fin3, skipbytes, 0))))) { |
| 307 |
> |
if (skipbytes && (fseek(fin, skipbytes, SEEK_SET) || (fin2 != NULL && |
| 308 |
> |
fseek(fin2, skipbytes, SEEK_SET) | |
| 309 |
> |
fseek(fin3, skipbytes, SEEK_SET)))) { |
| 310 |
|
fprintf(stderr, "%s: cannot skip %ld bytes on input\n", |
| 311 |
|
progname, skipbytes); |
| 312 |
|
quit(1); |
| 316 |
|
fprintf(stderr, "%s: bad # file arguments\n", progname); |
| 317 |
|
quit(1); |
| 318 |
|
} |
| 283 |
– |
|
| 319 |
|
if (reverse) { |
| 285 |
– |
#ifdef _WIN32 |
| 320 |
|
SET_FILE_BINARY(stdout); |
| 287 |
– |
if (format != 'a' && format != 'i') |
| 288 |
– |
SET_FILE_BINARY(fin); |
| 289 |
– |
#endif |
| 321 |
|
/* get header */ |
| 322 |
|
if (header) { |
| 323 |
< |
if (checkheader(fin, fmtid, stdout) < 0) { |
| 324 |
< |
fprintf(stderr, "%s: wrong input format\n", |
| 325 |
< |
progname); |
| 323 |
> |
getheader(fin, checkhead, stdout); |
| 324 |
> |
if (wrongformat) { |
| 325 |
> |
fprintf(stderr, "%s: wrong input format (expected %s)\n", |
| 326 |
> |
progname, fmtid); |
| 327 |
|
quit(1); |
| 328 |
|
} |
| 329 |
|
if (fin2 != NULL) { |
| 330 |
|
getheader(fin2, NULL, NULL); |
| 331 |
|
getheader(fin3, NULL, NULL); |
| 332 |
|
} |
| 333 |
< |
} else |
| 333 |
> |
resolution &= (picres.xr <= 0) | (picres.yr <= 0); |
| 334 |
> |
} else { |
| 335 |
|
newheader("RADIANCE", stdout); |
| 336 |
+ |
} |
| 337 |
|
/* get resolution */ |
| 338 |
|
if ((resolution && !fgetsresolu(&picres, fin)) || |
| 339 |
< |
picres.xr <= 0 || picres.yr <= 0) { |
| 339 |
> |
(picres.xr <= 0) | (picres.yr <= 0)) { |
| 340 |
|
fprintf(stderr, "%s: missing resolution\n", progname); |
| 341 |
|
quit(1); |
| 342 |
|
} |
| 343 |
|
if (resolution && fin2 != NULL) { |
| 344 |
|
RESOLU pres2; |
| 345 |
|
if (!fgetsresolu(&pres2, fin2) || |
| 346 |
< |
pres2.rt != picres.rt || |
| 347 |
< |
pres2.xr != picres.xr || |
| 348 |
< |
pres2.yr != picres.yr || |
| 346 |
> |
(pres2.rt != picres.rt) | |
| 347 |
> |
(pres2.xr != picres.xr) | |
| 348 |
> |
(pres2.yr != picres.yr) || |
| 349 |
|
!fgetsresolu(&pres2, fin3) || |
| 350 |
< |
pres2.rt != picres.rt || |
| 351 |
< |
pres2.xr != picres.xr || |
| 352 |
< |
pres2.yr != picres.yr) { |
| 350 |
> |
(pres2.rt != picres.rt) | |
| 351 |
> |
(pres2.xr != picres.xr) | |
| 352 |
> |
(pres2.yr != picres.yr)) { |
| 353 |
|
fprintf(stderr, "%s: resolution mismatch\n", |
| 354 |
|
progname); |
| 355 |
|
quit(1); |
| 357 |
|
} |
| 358 |
|
/* add to header */ |
| 359 |
|
printargs(i, argv, stdout); |
| 360 |
< |
if (expval < .99 || expval > 1.01) |
| 360 |
> |
if ((expval < .99) | (expval > 1.01)) |
| 361 |
|
fputexpos(expval, stdout); |
| 362 |
< |
fputformat(COLRFMT, stdout); |
| 362 |
> |
if (!header & (putprim != ALL)) { |
| 363 |
> |
NCSAMP = 1; /* no header, so trust user */ |
| 364 |
> |
} else if (NCSAMP == 1) { |
| 365 |
> |
if (putprim == ALL) |
| 366 |
> |
putprim = BRIGHT; |
| 367 |
> |
} else { |
| 368 |
> |
if (NCSAMP != 3) { |
| 369 |
> |
fprintf(stderr, "%s: input NCOMP must be 1 or 3\n", |
| 370 |
> |
progname); |
| 371 |
> |
quit(1); |
| 372 |
> |
} |
| 373 |
> |
if (putprim != ALL) { |
| 374 |
> |
fprintf(stderr, "%s: NCOMP mismatch\n", progname); |
| 375 |
> |
quit(1); |
| 376 |
> |
} |
| 377 |
> |
} |
| 378 |
> |
if (outprims != NULL) { |
| 379 |
> |
if (outprims != stdprims) |
| 380 |
> |
fputprims(outprims, stdout); |
| 381 |
> |
fputformat(COLRFMT, stdout); |
| 382 |
> |
} else { /* XYZ data */ |
| 383 |
> |
if (original < 0) { |
| 384 |
> |
scalecolor(exposure, WHTEFFICACY); |
| 385 |
> |
doexposure++; |
| 386 |
> |
} |
| 387 |
> |
fputformat(CIEFMT, stdout); |
| 388 |
> |
} |
| 389 |
|
putchar('\n'); |
| 390 |
|
fputsresolu(&picres, stdout); /* always put resolution */ |
| 391 |
|
valtopix(); |
| 392 |
|
} else { |
| 393 |
< |
#ifdef _WIN32 |
| 334 |
< |
SET_FILE_BINARY(fin); |
| 335 |
< |
if (format != 'a' && format != 'i') |
| 393 |
> |
if ((format != 'a') & (format != 'i')) |
| 394 |
|
SET_FILE_BINARY(stdout); |
| 337 |
– |
#endif |
| 395 |
|
/* get header */ |
| 396 |
< |
getheader(fin, checkhead, NULL); |
| 396 |
> |
picres.rt = PIXSTANDARD; |
| 397 |
> |
picres.xr = picres.yr = 0; |
| 398 |
> |
getheader(fin, checkhead, header ? stdout : (FILE *)NULL); |
| 399 |
|
if (wrongformat) { |
| 400 |
|
fprintf(stderr, |
| 401 |
|
"%s: input not a Radiance RGBE picture\n", |
| 402 |
|
progname); |
| 403 |
|
quit(1); |
| 404 |
|
} |
| 405 |
< |
if (!fgetsresolu(&picres, fin)) { |
| 405 |
> |
if ((picres.xr <= 0) | (picres.yr <= 0) && |
| 406 |
> |
!fgetsresolu(&picres, fin)) { |
| 407 |
|
fprintf(stderr, "%s: missing resolution\n", progname); |
| 408 |
|
quit(1); |
| 409 |
|
} |
| 410 |
+ |
if ((original < 0) & (mybright == &xyz_bright)) { |
| 411 |
+ |
scalecolor(exposure, 1./WHTEFFICACY); |
| 412 |
+ |
doexposure++; |
| 413 |
+ |
} |
| 414 |
|
if (header) { |
| 415 |
|
printargs(i, argv, stdout); |
| 416 |
< |
if (expval < .99 || expval > 1.01) |
| 416 |
> |
if (dataonly) |
| 417 |
> |
printf("%s%c\n", NCOMPSTR, "13"[putprim==ALL]); |
| 418 |
> |
if (dataonly && !resolution & !uniq) |
| 419 |
> |
printf("NCOLS=%d\nNROWS=%d\n", scanlen(&picres), |
| 420 |
> |
numscans(&picres)); |
| 421 |
> |
if ((expval < .99) | (expval > 1.01)) |
| 422 |
|
fputexpos(expval, stdout); |
| 423 |
+ |
if (swapbytes) { |
| 424 |
+ |
if (nativebigendian()) |
| 425 |
+ |
puts("BigEndian=0"); |
| 426 |
+ |
else |
| 427 |
+ |
puts("BigEndian=1"); |
| 428 |
+ |
} else if ((format != 'a') & (format != 'i') & |
| 429 |
+ |
(format != 'b')) |
| 430 |
+ |
fputendian(stdout); |
| 431 |
|
fputformat(fmtid, stdout); |
| 432 |
|
putchar('\n'); |
| 433 |
|
} |
| 442 |
|
|
| 443 |
|
|
| 444 |
|
static int |
| 445 |
+ |
IDformat(const char *s) |
| 446 |
+ |
{ |
| 447 |
+ |
if (!s || !*s) return(0); |
| 448 |
+ |
if (!strcmp(s, "ascii")) return('a'); |
| 449 |
+ |
if (!strcmp(s, "float")) return('f'); |
| 450 |
+ |
if (!strcmp(s, "double")) return('d'); |
| 451 |
+ |
if (!strcmp(s, "byte")) return('b'); |
| 452 |
+ |
if (!strcmp(s, "16-bit")) return('w'); |
| 453 |
+ |
return(0); |
| 454 |
+ |
} |
| 455 |
+ |
|
| 456 |
+ |
|
| 457 |
+ |
static int |
| 458 |
|
checkhead( /* deal with line from header */ |
| 459 |
|
char *line, |
| 460 |
|
void *p |
| 461 |
|
) |
| 462 |
|
{ |
| 463 |
< |
char fmt[32]; |
| 463 |
> |
static char fmt[MAXFMTLEN]; |
| 464 |
> |
FILE *fout = (FILE *)p; |
| 465 |
|
double d; |
| 466 |
|
COLOR ctmp; |
| 467 |
+ |
int rv; |
| 468 |
|
|
| 469 |
|
if (formatval(fmt, line)) { |
| 470 |
< |
if (!strcmp(fmt, CIEFMT)) { |
| 470 |
> |
if (reverse) { |
| 471 |
> |
if (format == 'a') { |
| 472 |
> |
format = IDformat(fmt); |
| 473 |
> |
if (format) |
| 474 |
> |
fmtid = fmt; |
| 475 |
> |
else |
| 476 |
> |
wrongformat = 1; |
| 477 |
> |
} else |
| 478 |
> |
wrongformat = strcmp(fmt, fmtid); |
| 479 |
> |
} else if (!strcmp(fmt, CIEFMT)) |
| 480 |
|
mybright = &xyz_bright; |
| 481 |
< |
if (original) { |
| 381 |
< |
scalecolor(exposure, 1./WHTEFFICACY); |
| 382 |
< |
doexposure++; |
| 383 |
< |
} |
| 384 |
< |
} else if (!strcmp(fmt, COLRFMT)) |
| 481 |
> |
else if (!strcmp(fmt, COLRFMT) || !strcmp(fmt, SPECFMT)) |
| 482 |
|
mybright = &rgb_bright; |
| 483 |
|
else |
| 484 |
< |
wrongformat++; |
| 485 |
< |
} else if (original && isexpos(line)) { |
| 484 |
> |
wrongformat = 1; |
| 485 |
> |
return(1); |
| 486 |
> |
} |
| 487 |
> |
if (!strncmp(line,"NROWS=",6)) { |
| 488 |
> |
picres.yr = atoi(line+6); |
| 489 |
> |
return(1); |
| 490 |
> |
} |
| 491 |
> |
if (!strncmp(line,"NCOLS=",6)) { |
| 492 |
> |
picres.xr = atoi(line+6); |
| 493 |
> |
return(1); |
| 494 |
> |
} |
| 495 |
> |
if (isncomp(line)) { |
| 496 |
> |
NCSAMP = ncompval(line); |
| 497 |
> |
dataonly |= reverse; /* pretty safe assumption */ |
| 498 |
> |
return(1); |
| 499 |
> |
} |
| 500 |
> |
if (iswlsplit(line)) { |
| 501 |
> |
wlsplitval(WLPART, line); |
| 502 |
> |
return(1); |
| 503 |
> |
} |
| 504 |
> |
if (original && isexpos(line)) { |
| 505 |
|
d = 1.0/exposval(line); |
| 506 |
|
scalecolor(exposure, d); |
| 507 |
|
doexposure++; |
| 508 |
< |
} else if (original && iscolcor(line)) { |
| 508 |
> |
return(1); |
| 509 |
> |
} |
| 510 |
> |
if (original && iscolcor(line)) { |
| 511 |
|
colcorval(ctmp, line); |
| 512 |
|
setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED), |
| 513 |
|
colval(exposure,GRN)/colval(ctmp,GRN), |
| 514 |
|
colval(exposure,BLU)/colval(ctmp,BLU)); |
| 515 |
|
doexposure++; |
| 516 |
< |
} else if (header) |
| 517 |
< |
fputs(line, stdout); |
| 516 |
> |
return(1); |
| 517 |
> |
} |
| 518 |
> |
if ((rv = isbigendian(line)) >= 0) { |
| 519 |
> |
if (reverse) |
| 520 |
> |
swapbytes = (nativebigendian() != rv); |
| 521 |
> |
return(1); |
| 522 |
> |
} |
| 523 |
> |
if (fout != NULL) |
| 524 |
> |
fputs(line, fout); |
| 525 |
|
return(0); |
| 526 |
|
} |
| 527 |
|
|
| 529 |
|
static void |
| 530 |
|
pixtoval(void) /* convert picture to values */ |
| 531 |
|
{ |
| 532 |
< |
register COLOR *scanln; |
| 532 |
> |
COLOR *scanln; |
| 533 |
|
int dogamma; |
| 534 |
|
COLOR lastc; |
| 535 |
|
RREAL hv[2]; |
| 536 |
|
int startprim, endprim; |
| 537 |
|
long startpos; |
| 538 |
< |
int y; |
| 414 |
< |
register int x; |
| 538 |
> |
int x, y; |
| 539 |
|
|
| 540 |
|
scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); |
| 541 |
|
if (scanln == NULL) { |
| 543 |
|
quit(1); |
| 544 |
|
} |
| 545 |
|
dogamma = gamcor < .95 || gamcor > 1.05; |
| 546 |
< |
if (putprim == ALL && !interleave) { |
| 546 |
> |
if ((putprim == ALL) & !interleave) { |
| 547 |
|
startprim = RED; endprim = BLU; |
| 548 |
|
startpos = ftell(fin); |
| 549 |
|
} else { |
| 550 |
|
startprim = putprim; endprim = putprim; |
| 551 |
|
} |
| 552 |
|
for (putprim = startprim; putprim <= endprim; putprim++) { |
| 553 |
< |
if (putprim != startprim && fseek(fin, startpos, 0)) { |
| 553 |
> |
if (putprim != startprim && fseek(fin, startpos, SEEK_SET)) { |
| 554 |
|
fprintf(stderr, "%s: seek error on input file\n", |
| 555 |
|
progname); |
| 556 |
|
quit(1); |
| 558 |
|
set_io(); |
| 559 |
|
setcolor(lastc, 0.0, 0.0, 0.0); |
| 560 |
|
for (y = 0; y < numscans(&picres); y++) { |
| 561 |
< |
if (freadscan(scanln, scanlen(&picres), fin) < 0) { |
| 561 |
> |
if (fread2scan(scanln, scanlen(&picres), fin, |
| 562 |
> |
NCSAMP, WLPART) < 0) { |
| 563 |
|
fprintf(stderr, "%s: read error\n", progname); |
| 564 |
|
quit(1); |
| 565 |
|
} |
| 603 |
|
static void |
| 604 |
|
valtopix(void) /* convert values to a pixel file */ |
| 605 |
|
{ |
| 606 |
< |
int dogamma; |
| 607 |
< |
register COLOR *scanln; |
| 608 |
< |
int y; |
| 609 |
< |
register int x; |
| 606 |
> |
int dogamma; |
| 607 |
> |
COLOR *scanln; |
| 608 |
> |
COLR rgbe; |
| 609 |
> |
int x, y; |
| 610 |
|
|
| 611 |
|
scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); |
| 612 |
|
if (scanln == NULL) { |
| 635 |
|
pow(colval(scanln[x],BLU), gamcor)); |
| 636 |
|
if (doexposure) |
| 637 |
|
multcolor(scanln[x], exposure); |
| 638 |
+ |
if (uniq) { /* uncompressed? */ |
| 639 |
+ |
setcolr(rgbe, scanln[x][RED], |
| 640 |
+ |
scanln[x][GRN], |
| 641 |
+ |
scanln[x][BLU]); |
| 642 |
+ |
if (putbinary(rgbe, sizeof(COLR), 1, stdout) != 1) |
| 643 |
+ |
goto writerr; |
| 644 |
+ |
} |
| 645 |
|
} |
| 646 |
< |
if (fwritescan(scanln, scanlen(&picres), stdout) < 0) { |
| 647 |
< |
fprintf(stderr, "%s: write error\n", progname); |
| 648 |
< |
quit(1); |
| 517 |
< |
} |
| 646 |
> |
/* write scan if compressed */ |
| 647 |
> |
if (!uniq && fwritescan(scanln, scanlen(&picres), stdout) < 0) |
| 648 |
> |
goto writerr; |
| 649 |
|
} |
| 650 |
|
free((void *)scanln); |
| 651 |
+ |
return; |
| 652 |
+ |
writerr: |
| 653 |
+ |
fprintf(stderr, "%s: write error\n", progname); |
| 654 |
+ |
quit(1); |
| 655 |
|
} |
| 656 |
|
|
| 657 |
|
|
| 663 |
|
} |
| 664 |
|
|
| 665 |
|
|
| 531 |
– |
static void |
| 532 |
– |
swap16( /* swap n 16-bit words */ |
| 533 |
– |
register uint16 *wp, |
| 534 |
– |
int n |
| 535 |
– |
) |
| 536 |
– |
{ |
| 537 |
– |
while (n-- > 0) { |
| 538 |
– |
*wp = *wp << 8 | ((*wp >> 8) & 0xff); |
| 539 |
– |
wp++; |
| 540 |
– |
} |
| 541 |
– |
} |
| 542 |
– |
|
| 543 |
– |
|
| 544 |
– |
static void |
| 545 |
– |
swap32( /* swap n 32-bit words */ |
| 546 |
– |
register uint32 *wp, |
| 547 |
– |
int n |
| 548 |
– |
) |
| 549 |
– |
{ |
| 550 |
– |
while (n-- > 0) { |
| 551 |
– |
*wp = *wp << 24 | ((*wp >> 24) & 0xff) | |
| 552 |
– |
(*wp & 0xff00) << 8 | (*wp & 0xff0000) >> 8; |
| 553 |
– |
wp++; |
| 554 |
– |
} |
| 555 |
– |
} |
| 556 |
– |
|
| 557 |
– |
|
| 558 |
– |
static void |
| 559 |
– |
swap64( /* swap n 64-bit words */ |
| 560 |
– |
register char *wp, |
| 561 |
– |
int n |
| 562 |
– |
) |
| 563 |
– |
{ |
| 564 |
– |
register int t; |
| 565 |
– |
|
| 566 |
– |
while (n-- > 0) { |
| 567 |
– |
t = wp[0]; wp[0] = wp[7]; wp[7] = t; |
| 568 |
– |
t = wp[1]; wp[1] = wp[6]; wp[6] = t; |
| 569 |
– |
t = wp[2]; wp[2] = wp[5]; wp[5] = t; |
| 570 |
– |
t = wp[3]; wp[3] = wp[4]; wp[4] = t; |
| 571 |
– |
wp += 8; |
| 572 |
– |
} |
| 573 |
– |
} |
| 574 |
– |
|
| 575 |
– |
|
| 666 |
|
static int |
| 667 |
|
getcascii( /* get an ascii color value from stream(s) */ |
| 668 |
|
COLOR col |
| 692 |
|
double vd[3]; |
| 693 |
|
|
| 694 |
|
if (fin2 == NULL) { |
| 695 |
< |
if (fread((char *)vd, sizeof(double), 3, fin) != 3) |
| 695 |
> |
if (getbinary(vd, sizeof(double), 3, fin) != 3) |
| 696 |
|
return(-1); |
| 697 |
|
} else { |
| 698 |
< |
if (fread((char *)vd, sizeof(double), 1, fin) != 1 || |
| 699 |
< |
fread((char *)(vd+1), sizeof(double), 1, fin2) != 1 || |
| 700 |
< |
fread((char *)(vd+2), sizeof(double), 1, fin3) != 1) |
| 698 |
> |
if (getbinary(vd, sizeof(double), 1, fin) != 1 || |
| 699 |
> |
getbinary(vd+1, sizeof(double), 1, fin2) != 1 || |
| 700 |
> |
getbinary(vd+2, sizeof(double), 1, fin3) != 1) |
| 701 |
|
return(-1); |
| 702 |
|
} |
| 703 |
|
if (swapbytes) |
| 715 |
|
float vf[3]; |
| 716 |
|
|
| 717 |
|
if (fin2 == NULL) { |
| 718 |
< |
if (fread((char *)vf, sizeof(float), 3, fin) != 3) |
| 718 |
> |
if (getbinary(vf, sizeof(float), 3, fin) != 3) |
| 719 |
|
return(-1); |
| 720 |
|
} else { |
| 721 |
< |
if (fread((char *)vf, sizeof(float), 1, fin) != 1 || |
| 722 |
< |
fread((char *)(vf+1), sizeof(float), 1, fin2) != 1 || |
| 723 |
< |
fread((char *)(vf+2), sizeof(float), 1, fin3) != 1) |
| 721 |
> |
if (getbinary(vf, sizeof(float), 1, fin) != 1 || |
| 722 |
> |
getbinary(vf+1, sizeof(float), 1, fin2) != 1 || |
| 723 |
> |
getbinary(vf+2, sizeof(float), 1, fin3) != 1) |
| 724 |
|
return(-1); |
| 725 |
|
} |
| 726 |
|
if (swapbytes) |
| 727 |
< |
swap32((uint32 *)vf, 3); |
| 727 |
> |
swap32((char *)vf, 3); |
| 728 |
|
setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]); |
| 729 |
|
return(0); |
| 730 |
|
} |
| 757 |
|
COLOR col |
| 758 |
|
) |
| 759 |
|
{ |
| 760 |
< |
BYTE vb[3]; |
| 760 |
> |
uby8 vb[3]; |
| 761 |
|
|
| 762 |
|
if (fin2 == NULL) { |
| 763 |
< |
if (fread((char *)vb, sizeof(BYTE), 3, fin) != 3) |
| 763 |
> |
if (getbinary(vb, sizeof(uby8), 3, fin) != 3) |
| 764 |
|
return(-1); |
| 765 |
|
} else { |
| 766 |
< |
if (fread((char *)vb, sizeof(BYTE), 1, fin) != 1 || |
| 767 |
< |
fread((char *)(vb+1), sizeof(BYTE), 1, fin2) != 1 || |
| 768 |
< |
fread((char *)(vb+2), sizeof(BYTE), 1, fin3) != 1) |
| 766 |
> |
if (getbinary(vb, sizeof(uby8), 1, fin) != 1 || |
| 767 |
> |
getbinary(vb+1, sizeof(uby8), 1, fin2) != 1 || |
| 768 |
> |
getbinary(vb+2, sizeof(uby8), 1, fin3) != 1) |
| 769 |
|
return(-1); |
| 770 |
|
} |
| 771 |
|
setcolor(col, (vb[rord[RED]]+.5)/256., |
| 782 |
|
uint16 vw[3]; |
| 783 |
|
|
| 784 |
|
if (fin2 == NULL) { |
| 785 |
< |
if (fread((char *)vw, sizeof(uint16), 3, fin) != 3) |
| 785 |
> |
if (getbinary(vw, sizeof(uint16), 3, fin) != 3) |
| 786 |
|
return(-1); |
| 787 |
|
} else { |
| 788 |
< |
if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 || |
| 789 |
< |
fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 || |
| 790 |
< |
fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1) |
| 788 |
> |
if (getbinary(vw, sizeof(uint16), 1, fin) != 1 || |
| 789 |
> |
getbinary(vw+1, sizeof(uint16), 1, fin2) != 1 || |
| 790 |
> |
getbinary(vw+2, sizeof(uint16), 1, fin3) != 1) |
| 791 |
|
return(-1); |
| 792 |
|
} |
| 793 |
|
if (swapbytes) |
| 794 |
< |
swap16(vw, 3); |
| 794 |
> |
swap16((char *)vw, 3); |
| 795 |
|
setcolor(col, (vw[rord[RED]]+.5)/65536., |
| 796 |
|
(vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.); |
| 797 |
|
return(0); |
| 819 |
|
{ |
| 820 |
|
double vd; |
| 821 |
|
|
| 822 |
< |
if (fread((char *)&vd, sizeof(double), 1, fin) != 1) |
| 822 |
> |
if (getbinary(&vd, sizeof(double), 1, fin) != 1) |
| 823 |
|
return(-1); |
| 824 |
|
if (swapbytes) |
| 825 |
|
swap64((char *)&vd, 1); |
| 835 |
|
{ |
| 836 |
|
float vf; |
| 837 |
|
|
| 838 |
< |
if (fread((char *)&vf, sizeof(float), 1, fin) != 1) |
| 838 |
> |
if (getbinary(&vf, sizeof(float), 1, fin) != 1) |
| 839 |
|
return(-1); |
| 840 |
|
if (swapbytes) |
| 841 |
< |
swap32((uint32 *)&vf, 1); |
| 841 |
> |
swap32((char *)&vf, 1); |
| 842 |
|
setcolor(col, vf, vf, vf); |
| 843 |
|
return(0); |
| 844 |
|
} |
| 865 |
|
COLOR col |
| 866 |
|
) |
| 867 |
|
{ |
| 868 |
< |
BYTE vb; |
| 868 |
> |
uby8 vb; |
| 869 |
|
double d; |
| 870 |
|
|
| 871 |
< |
if (fread((char *)&vb, sizeof(BYTE), 1, fin) != 1) |
| 871 |
> |
if (getbinary(&vb, sizeof(uby8), 1, fin) != 1) |
| 872 |
|
return(-1); |
| 873 |
|
d = (vb+.5)/256.; |
| 874 |
|
setcolor(col, d, d, d); |
| 884 |
|
uint16 vw; |
| 885 |
|
double d; |
| 886 |
|
|
| 887 |
< |
if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1) |
| 887 |
> |
if (getbinary(&vw, sizeof(uint16), 1, fin) != 1) |
| 888 |
|
return(-1); |
| 889 |
|
if (swapbytes) |
| 890 |
< |
swap16(&vw, 1); |
| 890 |
> |
swap16((char *)&vw, 1); |
| 891 |
|
d = (vw+.5)/65536.; |
| 892 |
|
setcolor(col, d, d, d); |
| 893 |
|
return(0); |
| 919 |
|
vf[1] = colval(col,ord[1]); |
| 920 |
|
vf[2] = colval(col,ord[2]); |
| 921 |
|
if (swapbytes) |
| 922 |
< |
swap32((uint32 *)vf, 3); |
| 923 |
< |
fwrite((char *)vf, sizeof(float), 3, stdout); |
| 922 |
> |
swap32((char *)vf, 3); |
| 923 |
> |
putbinary(vf, sizeof(float), 3, stdout); |
| 924 |
|
|
| 925 |
|
return(ferror(stdout) ? -1 : 0); |
| 926 |
|
} |
| 938 |
|
vd[2] = colval(col,ord[2]); |
| 939 |
|
if (swapbytes) |
| 940 |
|
swap64((char *)vd, 3); |
| 941 |
< |
fwrite((char *)vd, sizeof(double), 3, stdout); |
| 941 |
> |
putbinary(vd, sizeof(double), 3, stdout); |
| 942 |
|
|
| 943 |
|
return(ferror(stdout) ? -1 : 0); |
| 944 |
|
} |
| 964 |
|
) |
| 965 |
|
{ |
| 966 |
|
long i; |
| 967 |
< |
BYTE vb[3]; |
| 967 |
> |
uby8 vb[3]; |
| 968 |
|
|
| 969 |
|
i = colval(col,ord[0])*256.; |
| 970 |
|
vb[0] = min(i,255); |
| 972 |
|
vb[1] = min(i,255); |
| 973 |
|
i = colval(col,ord[2])*256.; |
| 974 |
|
vb[2] = min(i,255); |
| 975 |
< |
fwrite((char *)vb, sizeof(BYTE), 3, stdout); |
| 975 |
> |
putbinary(vb, sizeof(uby8), 3, stdout); |
| 976 |
|
|
| 977 |
|
return(ferror(stdout) ? -1 : 0); |
| 978 |
|
} |
| 993 |
|
i = colval(col,ord[2])*65536.; |
| 994 |
|
vw[2] = min(i,65535); |
| 995 |
|
if (swapbytes) |
| 996 |
< |
swap16(vw, 3); |
| 997 |
< |
fwrite((char *)vw, sizeof(uint16), 3, stdout); |
| 996 |
> |
swap16((char *)vw, 3); |
| 997 |
> |
putbinary(vw, sizeof(uint16), 3, stdout); |
| 998 |
|
|
| 999 |
|
return(ferror(stdout) ? -1 : 0); |
| 1000 |
|
} |
| 1020 |
|
|
| 1021 |
|
vf = (*mybright)(col); |
| 1022 |
|
if (swapbytes) |
| 1023 |
< |
swap32((uint32 *)&vf, 1); |
| 1024 |
< |
fwrite((char *)&vf, sizeof(float), 1, stdout); |
| 1023 |
> |
swap32((char *)&vf, 1); |
| 1024 |
> |
putbinary(&vf, sizeof(float), 1, stdout); |
| 1025 |
|
|
| 1026 |
|
return(ferror(stdout) ? -1 : 0); |
| 1027 |
|
} |
| 1037 |
|
vd = (*mybright)(col); |
| 1038 |
|
if (swapbytes) |
| 1039 |
|
swap64((char *)&vd, 1); |
| 1040 |
< |
fwrite((char *)&vd, sizeof(double), 1, stdout); |
| 1040 |
> |
putbinary(&vd, sizeof(double), 1, stdout); |
| 1041 |
|
|
| 1042 |
|
return(ferror(stdout) ? -1 : 0); |
| 1043 |
|
} |
| 1059 |
|
COLOR col |
| 1060 |
|
) |
| 1061 |
|
{ |
| 1062 |
< |
register int i; |
| 1063 |
< |
BYTE vb; |
| 1062 |
> |
int i; |
| 1063 |
> |
uby8 vb; |
| 1064 |
|
|
| 1065 |
|
i = (*mybright)(col)*256.; |
| 1066 |
|
vb = min(i,255); |
| 1067 |
< |
fwrite((char *)&vb, sizeof(BYTE), 1, stdout); |
| 1067 |
> |
putbinary(&vb, sizeof(uby8), 1, stdout); |
| 1068 |
|
|
| 1069 |
|
return(ferror(stdout) ? -1 : 0); |
| 1070 |
|
} |
| 1081 |
|
i = (*mybright)(col)*65536.; |
| 1082 |
|
vw = min(i,65535); |
| 1083 |
|
if (swapbytes) |
| 1084 |
< |
swap16(&vw, 1); |
| 1085 |
< |
fwrite((char *)&vw, sizeof(uint16), 1, stdout); |
| 1084 |
> |
swap16((char *)&vw, 1); |
| 1085 |
> |
putbinary(&vw, sizeof(uint16), 1, stdout); |
| 1086 |
|
|
| 1087 |
|
return(ferror(stdout) ? -1 : 0); |
| 1088 |
|
} |
| 1108 |
|
|
| 1109 |
|
vf = colval(col,putprim); |
| 1110 |
|
if (swapbytes) |
| 1111 |
< |
swap32((uint32 *)&vf, 1); |
| 1112 |
< |
fwrite((char *)&vf, sizeof(float), 1, stdout); |
| 1111 |
> |
swap32((char *)&vf, 1); |
| 1112 |
> |
putbinary(&vf, sizeof(float), 1, stdout); |
| 1113 |
|
|
| 1114 |
|
return(ferror(stdout) ? -1 : 0); |
| 1115 |
|
} |
| 1125 |
|
vd = colval(col,putprim); |
| 1126 |
|
if (swapbytes) |
| 1127 |
|
swap64((char *)&vd, 1); |
| 1128 |
< |
fwrite((char *)&vd, sizeof(double), 1, stdout); |
| 1128 |
> |
putbinary(&vd, sizeof(double), 1, stdout); |
| 1129 |
|
|
| 1130 |
|
return(ferror(stdout) ? -1 : 0); |
| 1131 |
|
} |
| 1148 |
|
) |
| 1149 |
|
{ |
| 1150 |
|
long i; |
| 1151 |
< |
BYTE vb; |
| 1151 |
> |
uby8 vb; |
| 1152 |
|
|
| 1153 |
|
i = colval(col,putprim)*256.; |
| 1154 |
|
vb = min(i,255); |
| 1155 |
< |
fwrite((char *)&vb, sizeof(BYTE), 1, stdout); |
| 1155 |
> |
putbinary(&vb, sizeof(uby8), 1, stdout); |
| 1156 |
|
|
| 1157 |
|
return(ferror(stdout) ? -1 : 0); |
| 1158 |
|
} |
| 1169 |
|
i = colval(col,putprim)*65536.; |
| 1170 |
|
vw = min(i,65535); |
| 1171 |
|
if (swapbytes) |
| 1172 |
< |
swap16(&vw, 1); |
| 1173 |
< |
fwrite((char *)&vw, sizeof(uint16), 1, stdout); |
| 1172 |
> |
swap16((char *)&vw, 1); |
| 1173 |
> |
putbinary(&vw, sizeof(uint16), 1, stdout); |
| 1174 |
|
|
| 1175 |
|
return(ferror(stdout) ? -1 : 0); |
| 1176 |
|
} |
| 1212 |
|
if (fin2 == NULL) |
| 1213 |
|
goto namerr; |
| 1214 |
|
if (fseek(fin2, |
| 1215 |
< |
(long)sizeof(float)*picres.xr*picres.yr, 1)) |
| 1215 |
> |
(long)sizeof(float)*picres.xr*picres.yr, SEEK_CUR)) |
| 1216 |
|
goto seekerr; |
| 1217 |
|
if (fseek(fin3, |
| 1218 |
< |
(long)sizeof(float)*2*picres.xr*picres.yr, 1)) |
| 1218 |
> |
(long)sizeof(float)*2*picres.xr*picres.yr, SEEK_CUR)) |
| 1219 |
|
goto seekerr; |
| 1220 |
|
} |
| 1221 |
|
} |
| 1234 |
|
if (fin2 == NULL) |
| 1235 |
|
goto namerr; |
| 1236 |
|
if (fseek(fin2, |
| 1237 |
< |
(long)sizeof(double)*picres.xr*picres.yr, 1)) |
| 1237 |
> |
(long)sizeof(double)*picres.xr*picres.yr, SEEK_CUR)) |
| 1238 |
|
goto seekerr; |
| 1239 |
|
if (fseek(fin3, |
| 1240 |
< |
(long)sizeof(double)*2*picres.xr*picres.yr, 1)) |
| 1240 |
> |
(long)sizeof(double)*2*picres.xr*picres.yr, SEEK_CUR)) |
| 1241 |
|
goto seekerr; |
| 1242 |
|
} |
| 1243 |
|
} |
| 1274 |
|
if (fin2 == NULL) |
| 1275 |
|
goto namerr; |
| 1276 |
|
if (fseek(fin2, |
| 1277 |
< |
(long)sizeof(BYTE)*picres.xr*picres.yr, 1)) |
| 1277 |
> |
(long)sizeof(uby8)*picres.xr*picres.yr, SEEK_CUR)) |
| 1278 |
|
goto seekerr; |
| 1279 |
|
if (fseek(fin3, |
| 1280 |
< |
(long)sizeof(BYTE)*2*picres.xr*picres.yr, 1)) |
| 1280 |
> |
(long)sizeof(uby8)*2*picres.xr*picres.yr, SEEK_CUR)) |
| 1281 |
|
goto seekerr; |
| 1282 |
|
} |
| 1283 |
|
} |
| 1296 |
|
if (fin2 == NULL) |
| 1297 |
|
goto namerr; |
| 1298 |
|
if (fseek(fin2, |
| 1299 |
< |
(long)sizeof(uint16)*picres.xr*picres.yr, 1)) |
| 1299 |
> |
(long)sizeof(uint16)*picres.xr*picres.yr, SEEK_CUR)) |
| 1300 |
|
goto seekerr; |
| 1301 |
|
if (fseek(fin3, |
| 1302 |
< |
(long)sizeof(uint16)*2*picres.xr*picres.yr, 1)) |
| 1302 |
> |
(long)sizeof(uint16)*2*picres.xr*picres.yr, SEEK_CUR)) |
| 1303 |
|
goto seekerr; |
| 1304 |
|
} |
| 1305 |
|
} |