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 |
|
* pvalue.c - program to print pixel values. |
6 |
|
* |
7 |
|
* 4/23/86 |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#include "platform.h" |
11 |
|
#include "standard.h" |
14 |
– |
|
12 |
|
#include "color.h" |
16 |
– |
|
13 |
|
#include "resolu.h" |
14 |
+ |
#include "view.h" |
15 |
|
|
16 |
|
#define min(a,b) ((a)<(b)?(a):(b)) |
17 |
|
|
19 |
|
#define ALL 3 |
20 |
|
#define BRIGHT 4 |
21 |
|
|
25 |
– |
#define brightonly (putprim==BRIGHT) |
26 |
– |
|
22 |
|
RESOLU picres; /* resolution of picture */ |
28 |
– |
|
23 |
|
int uniq = 0; /* print only unique values? */ |
30 |
– |
|
24 |
|
int doexposure = 0; /* exposure change? (>100 to print) */ |
32 |
– |
|
25 |
|
int dataonly = 0; /* data only format? */ |
34 |
– |
|
26 |
|
int putprim = ALL; /* what to put out */ |
36 |
– |
|
27 |
|
int reverse = 0; /* reverse conversion? */ |
38 |
– |
|
28 |
|
int format = 'a'; /* input/output format */ |
29 |
|
char *fmtid = "ascii"; /* format identifier for header */ |
41 |
– |
|
30 |
|
int header = 1; /* do header? */ |
43 |
– |
|
31 |
|
long skipbytes = 0; /* skip bytes in input? */ |
32 |
< |
|
32 |
> |
int swapbytes = 0; /* swap bytes? */ |
33 |
> |
int interleave = 1; /* file is interleaved? */ |
34 |
|
int resolution = 1; /* put/get resolution string? */ |
47 |
– |
|
35 |
|
int original = 0; /* convert to original values? */ |
49 |
– |
|
36 |
|
int wrongformat = 0; /* wrong input format? */ |
51 |
– |
|
37 |
|
double gamcor = 1.0; /* gamma correction */ |
38 |
|
|
39 |
|
int ord[3] = {RED, GRN, BLU}; /* RGB ordering */ |
46 |
|
FILE *fin; |
47 |
|
FILE *fin2 = NULL, *fin3 = NULL; /* for other color channels */ |
48 |
|
|
64 |
– |
int (*getval)(), (*putval)(); |
49 |
|
|
50 |
+ |
typedef int getfunc_t(COLOR col); |
51 |
+ |
typedef int putfunc_t(COLOR col); |
52 |
+ |
typedef double brightfunc_t(COLOR col); |
53 |
|
|
54 |
< |
main(argc, argv) |
55 |
< |
int argc; |
56 |
< |
char **argv; |
54 |
> |
getfunc_t *getval; |
55 |
> |
putfunc_t *putval; |
56 |
> |
brightfunc_t *mybright; |
57 |
> |
|
58 |
> |
static gethfunc checkhead; |
59 |
> |
static brightfunc_t rgb_bright, xyz_bright; |
60 |
> |
static getfunc_t getbascii, getbint, getbdouble, getbfloat, getbbyte, getbword; |
61 |
> |
static getfunc_t getcascii, getcint, getcdouble, getcfloat, getcbyte, getcword; |
62 |
> |
static putfunc_t putbascii, putbint, putbdouble, putbfloat, putbbyte, putbword; |
63 |
> |
static putfunc_t putcascii, putcint, putcdouble, putcfloat, putcbyte, putcword; |
64 |
> |
static putfunc_t putpascii, putpint, putpdouble, putpfloat, putpbyte, putpword; |
65 |
> |
|
66 |
> |
static void set_io(void); |
67 |
> |
static void pixtoval(void); |
68 |
> |
static void valtopix(void); |
69 |
> |
|
70 |
> |
|
71 |
> |
static double |
72 |
> |
rgb_bright( |
73 |
> |
COLOR clr |
74 |
> |
) |
75 |
|
{ |
76 |
< |
extern int checkhead(); |
77 |
< |
extern long atol(); |
76 |
> |
return(bright(clr)); |
77 |
> |
} |
78 |
> |
|
79 |
> |
static double |
80 |
> |
xyz_bright( |
81 |
> |
COLOR clr |
82 |
> |
) |
83 |
> |
{ |
84 |
> |
return(clr[CIEY]); |
85 |
> |
} |
86 |
> |
|
87 |
> |
int |
88 |
> |
main( |
89 |
> |
int argc, |
90 |
> |
char **argv |
91 |
> |
) |
92 |
> |
{ |
93 |
|
double d, expval = 1.0; |
94 |
|
int i; |
95 |
|
|
96 |
|
progname = argv[0]; |
97 |
+ |
mybright = &rgb_bright; /* default */ |
98 |
|
|
99 |
|
for (i = 1; i < argc; i++) |
100 |
|
if (argv[i][0] == '-' || argv[i][0] == '+') |
140 |
|
case 'r': /* reverse conversion */ |
141 |
|
reverse = argv[i][0] == '-'; |
142 |
|
break; |
143 |
+ |
case 'n': /* non-interleaved RGB */ |
144 |
+ |
interleave = argv[i][0] == '+'; |
145 |
+ |
break; |
146 |
|
case 'b': /* brightness values */ |
147 |
|
putprim = argv[i][0] == '-' ? BRIGHT : ALL; |
148 |
|
break; |
171 |
|
format = 'b'; |
172 |
|
fmtid = "byte"; |
173 |
|
break; |
174 |
+ |
case 'W': /* 16-bit swapped */ |
175 |
+ |
swapbytes = 1; |
176 |
+ |
case 'w': /* 16-bit */ |
177 |
+ |
dataonly = 1; |
178 |
+ |
format = 'w'; |
179 |
+ |
fmtid = "16-bit"; |
180 |
+ |
break; |
181 |
+ |
case 'F': /* swapped floats */ |
182 |
+ |
swapbytes = 1; |
183 |
|
case 'f': /* float */ |
184 |
|
dataonly = 1; |
185 |
|
format = 'f'; |
186 |
|
fmtid = "float"; |
187 |
|
break; |
188 |
+ |
case 'D': /* swapped doubles */ |
189 |
+ |
swapbytes = 1; |
190 |
|
case 'd': /* double */ |
191 |
|
dataonly = 1; |
192 |
|
format = 'd'; |
200 |
|
case 'X': /* x resolution */ |
201 |
|
resolution = 0; |
202 |
|
if (argv[i][0] == '-') |
203 |
< |
picres.or |= XDECR; |
203 |
> |
picres.rt |= XDECR; |
204 |
|
picres.xr = atoi(argv[++i]); |
205 |
|
break; |
206 |
|
case 'y': /* y resolution */ |
207 |
|
case 'Y': /* y resolution */ |
208 |
|
resolution = 0; |
209 |
|
if (argv[i][0] == '-') |
210 |
< |
picres.or |= YDECR; |
210 |
> |
picres.rt |= YDECR; |
211 |
|
if (picres.xr == 0) |
212 |
< |
picres.or |= YMAJOR; |
212 |
> |
picres.rt |= YMAJOR; |
213 |
|
picres.yr = atoi(argv[++i]); |
214 |
|
break; |
215 |
|
default: |
222 |
|
else |
223 |
|
break; |
224 |
|
/* recognize special formats */ |
225 |
< |
if (dataonly && format == 'b') |
226 |
< |
if (brightonly) |
225 |
> |
if (dataonly && format == 'b') { |
226 |
> |
if (putprim == ALL) |
227 |
> |
fmtid = "24-bit_rgb"; |
228 |
> |
else |
229 |
|
fmtid = "8-bit_grey"; |
230 |
+ |
} |
231 |
+ |
if (dataonly && format == 'w') { |
232 |
+ |
if (putprim == ALL) |
233 |
+ |
fmtid = "48-bit_rgb"; |
234 |
|
else |
235 |
< |
fmtid = "24-bit_rgb"; |
235 |
> |
fmtid = "16-bit_grey"; |
236 |
> |
} |
237 |
|
/* assign reverse ordering */ |
238 |
|
rord[ord[0]] = 0; |
239 |
|
rord[ord[1]] = 1; |
247 |
|
progname, argv[i]); |
248 |
|
quit(1); |
249 |
|
} |
250 |
< |
if (skipbytes && fseek(fin, skipbytes, 0)) |
209 |
< |
goto seekerr; |
210 |
< |
if (reverse && !brightonly && i == argc-3) { |
250 |
> |
if (reverse && putprim != BRIGHT && i == argc-3) { |
251 |
|
if ((fin2 = fopen(argv[i+1], "r")) == NULL) { |
252 |
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
253 |
|
progname, argv[i+1]); |
258 |
|
progname, argv[i+2]); |
259 |
|
quit(1); |
260 |
|
} |
261 |
< |
if (skipbytes && (fseek(fin2, skipbytes, 0) || |
222 |
< |
fseek(fin3, skipbytes, 0))) |
223 |
< |
goto seekerr; |
261 |
> |
interleave = -1; |
262 |
|
} else if (i != argc-1) |
263 |
|
fin = NULL; |
264 |
+ |
if (reverse && putprim != BRIGHT && !interleave) { |
265 |
+ |
fin2 = fopen(argv[i], "r"); |
266 |
+ |
fin3 = fopen(argv[i], "r"); |
267 |
+ |
} |
268 |
+ |
if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL && |
269 |
+ |
(fseek(fin2, skipbytes, 0) || |
270 |
+ |
fseek(fin3, skipbytes, 0))))) { |
271 |
+ |
fprintf(stderr, "%s: cannot skip %ld bytes on input\n", |
272 |
+ |
progname, skipbytes); |
273 |
+ |
quit(1); |
274 |
+ |
} |
275 |
|
} |
276 |
|
if (fin == NULL) { |
277 |
|
fprintf(stderr, "%s: bad # file arguments\n", progname); |
278 |
|
quit(1); |
279 |
|
} |
280 |
|
|
232 |
– |
set_io(); |
233 |
– |
|
281 |
|
if (reverse) { |
282 |
< |
#ifdef MSDOS |
283 |
< |
setmode(fileno(stdout), O_BINARY); |
282 |
> |
#ifdef _WIN32 |
283 |
> |
SET_FILE_BINARY(stdout); |
284 |
|
if (format != 'a' && format != 'i') |
285 |
< |
setmode(fileno(fin), O_BINARY); |
285 |
> |
SET_FILE_BINARY(fin); |
286 |
|
#endif |
287 |
|
/* get header */ |
288 |
|
if (header) { |
306 |
|
if (resolution && fin2 != NULL) { |
307 |
|
RESOLU pres2; |
308 |
|
if (!fgetsresolu(&pres2, fin2) || |
309 |
< |
pres2.or != picres.or || |
309 |
> |
pres2.rt != picres.rt || |
310 |
|
pres2.xr != picres.xr || |
311 |
|
pres2.yr != picres.yr || |
312 |
|
!fgetsresolu(&pres2, fin3) || |
313 |
< |
pres2.or != picres.or || |
313 |
> |
pres2.rt != picres.rt || |
314 |
|
pres2.xr != picres.xr || |
315 |
|
pres2.yr != picres.yr) { |
316 |
|
fprintf(stderr, "%s: resolution mismatch\n", |
327 |
|
fputsresolu(&picres, stdout); /* always put resolution */ |
328 |
|
valtopix(); |
329 |
|
} else { |
330 |
< |
#ifdef MSDOS |
331 |
< |
setmode(fileno(fin), O_BINARY); |
330 |
> |
#ifdef _WIN32 |
331 |
> |
SET_FILE_BINARY(fin); |
332 |
|
if (format != 'a' && format != 'i') |
333 |
< |
setmode(fileno(stdout), O_BINARY); |
333 |
> |
SET_FILE_BINARY(stdout); |
334 |
|
#endif |
335 |
|
/* get header */ |
336 |
|
getheader(fin, checkhead, NULL); |
337 |
|
if (wrongformat) { |
338 |
< |
fprintf(stderr, "%s: input not a Radiance picture\n", |
338 |
> |
fprintf(stderr, |
339 |
> |
"%s: input not a Radiance RGBE picture\n", |
340 |
|
progname); |
341 |
|
quit(1); |
342 |
|
} |
357 |
|
} |
358 |
|
|
359 |
|
quit(0); |
360 |
< |
seekerr: |
313 |
< |
fprintf(stderr, "%s: cannot skip %ld bytes on input\n", |
314 |
< |
progname, skipbytes); |
315 |
< |
quit(1); |
360 |
> |
return 0; /* pro forma return */ |
361 |
|
} |
362 |
|
|
363 |
|
|
364 |
< |
checkhead(line) /* deal with line from header */ |
365 |
< |
char *line; |
364 |
> |
static int |
365 |
> |
checkhead( /* deal with line from header */ |
366 |
> |
char *line, |
367 |
> |
void *p |
368 |
> |
) |
369 |
|
{ |
370 |
|
char fmt[32]; |
371 |
|
double d; |
372 |
|
COLOR ctmp; |
373 |
|
|
374 |
< |
if (formatval(fmt, line)) |
375 |
< |
wrongformat = strcmp(fmt, COLRFMT); |
376 |
< |
else if (original && isexpos(line)) { |
374 |
> |
if (formatval(fmt, line)) { |
375 |
> |
if (!strcmp(fmt, CIEFMT)) |
376 |
> |
mybright = &xyz_bright; |
377 |
> |
else if (!strcmp(fmt, COLRFMT)) |
378 |
> |
mybright = &rgb_bright; |
379 |
> |
else |
380 |
> |
wrongformat++; |
381 |
> |
} else if (original && isexpos(line)) { |
382 |
|
d = 1.0/exposval(line); |
383 |
|
scalecolor(exposure, d); |
384 |
|
doexposure++; |
390 |
|
doexposure++; |
391 |
|
} else if (header) |
392 |
|
fputs(line, stdout); |
393 |
+ |
return(0); |
394 |
|
} |
395 |
|
|
396 |
|
|
397 |
< |
pixtoval() /* convert picture to values */ |
397 |
> |
static void |
398 |
> |
pixtoval(void) /* convert picture to values */ |
399 |
|
{ |
400 |
|
register COLOR *scanln; |
401 |
|
int dogamma; |
402 |
|
COLOR lastc; |
403 |
< |
FLOAT hv[2]; |
403 |
> |
RREAL hv[2]; |
404 |
> |
int startprim, endprim; |
405 |
> |
long startpos; |
406 |
|
int y; |
407 |
|
register int x; |
408 |
|
|
412 |
|
quit(1); |
413 |
|
} |
414 |
|
dogamma = gamcor < .95 || gamcor > 1.05; |
415 |
< |
setcolor(lastc, 0.0, 0.0, 0.0); |
416 |
< |
for (y = 0; y < numscans(&picres); y++) { |
417 |
< |
if (freadscan(scanln, scanlen(&picres), fin) < 0) { |
418 |
< |
fprintf(stderr, "%s: read error\n", progname); |
415 |
> |
if (putprim == ALL && !interleave) { |
416 |
> |
startprim = RED; endprim = BLU; |
417 |
> |
startpos = ftell(fin); |
418 |
> |
} else { |
419 |
> |
startprim = putprim; endprim = putprim; |
420 |
> |
} |
421 |
> |
for (putprim = startprim; putprim <= endprim; putprim++) { |
422 |
> |
if (putprim != startprim && fseek(fin, startpos, 0)) { |
423 |
> |
fprintf(stderr, "%s: seek error on input file\n", |
424 |
> |
progname); |
425 |
|
quit(1); |
426 |
|
} |
427 |
< |
for (x = 0; x < scanlen(&picres); x++) { |
428 |
< |
if (uniq) |
429 |
< |
if ( colval(scanln[x],RED) == |
430 |
< |
colval(lastc,RED) && |
431 |
< |
colval(scanln[x],GRN) == |
369 |
< |
colval(lastc,GRN) && |
370 |
< |
colval(scanln[x],BLU) == |
371 |
< |
colval(lastc,BLU) ) |
372 |
< |
continue; |
373 |
< |
else |
374 |
< |
copycolor(lastc, scanln[x]); |
375 |
< |
if (doexposure) |
376 |
< |
multcolor(scanln[x], exposure); |
377 |
< |
if (dogamma) |
378 |
< |
setcolor(scanln[x], |
379 |
< |
pow(colval(scanln[x],RED), 1.0/gamcor), |
380 |
< |
pow(colval(scanln[x],GRN), 1.0/gamcor), |
381 |
< |
pow(colval(scanln[x],BLU), 1.0/gamcor)); |
382 |
< |
if (!dataonly) { |
383 |
< |
pix2loc(hv, &picres, x, y); |
384 |
< |
printf("%7d %7d ", (int)(hv[0]*picres.xr), |
385 |
< |
(int)(hv[1]*picres.yr)); |
386 |
< |
} |
387 |
< |
if ((*putval)(scanln[x]) < 0) { |
388 |
< |
fprintf(stderr, "%s: write error\n", progname); |
427 |
> |
set_io(); |
428 |
> |
setcolor(lastc, 0.0, 0.0, 0.0); |
429 |
> |
for (y = 0; y < numscans(&picres); y++) { |
430 |
> |
if (freadscan(scanln, scanlen(&picres), fin) < 0) { |
431 |
> |
fprintf(stderr, "%s: read error\n", progname); |
432 |
|
quit(1); |
433 |
|
} |
434 |
+ |
for (x = 0; x < scanlen(&picres); x++) { |
435 |
+ |
if (uniq) { |
436 |
+ |
if ( colval(scanln[x],RED) == |
437 |
+ |
colval(lastc,RED) && |
438 |
+ |
colval(scanln[x],GRN) == |
439 |
+ |
colval(lastc,GRN) && |
440 |
+ |
colval(scanln[x],BLU) == |
441 |
+ |
colval(lastc,BLU) ) |
442 |
+ |
continue; |
443 |
+ |
else |
444 |
+ |
copycolor(lastc, scanln[x]); |
445 |
+ |
} |
446 |
+ |
if (doexposure) |
447 |
+ |
multcolor(scanln[x], exposure); |
448 |
+ |
if (dogamma) |
449 |
+ |
setcolor(scanln[x], |
450 |
+ |
pow(colval(scanln[x],RED), 1.0/gamcor), |
451 |
+ |
pow(colval(scanln[x],GRN), 1.0/gamcor), |
452 |
+ |
pow(colval(scanln[x],BLU), 1.0/gamcor)); |
453 |
+ |
if (!dataonly) { |
454 |
+ |
pix2loc(hv, &picres, x, y); |
455 |
+ |
printf("%7d %7d ", |
456 |
+ |
(int)(hv[0]*picres.xr), |
457 |
+ |
(int)(hv[1]*picres.yr)); |
458 |
+ |
} |
459 |
+ |
if ((*putval)(scanln[x]) < 0) { |
460 |
+ |
fprintf(stderr, "%s: write error\n", |
461 |
+ |
progname); |
462 |
+ |
quit(1); |
463 |
+ |
} |
464 |
+ |
} |
465 |
|
} |
466 |
|
} |
467 |
< |
free((char *)scanln); |
467 |
> |
free((void *)scanln); |
468 |
|
} |
469 |
|
|
470 |
|
|
471 |
< |
valtopix() /* convert values to a pixel file */ |
471 |
> |
static void |
472 |
> |
valtopix(void) /* convert values to a pixel file */ |
473 |
|
{ |
474 |
|
int dogamma; |
475 |
|
register COLOR *scanln; |
482 |
|
quit(1); |
483 |
|
} |
484 |
|
dogamma = gamcor < .95 || gamcor > 1.05; |
485 |
+ |
set_io(); |
486 |
|
for (y = 0; y < numscans(&picres); y++) { |
487 |
|
for (x = 0; x < scanlen(&picres); x++) { |
488 |
|
if (!dataonly) { |
509 |
|
quit(1); |
510 |
|
} |
511 |
|
} |
512 |
< |
free((char *)scanln); |
512 |
> |
free((void *)scanln); |
513 |
|
} |
514 |
|
|
515 |
|
|
516 |
+ |
void |
517 |
|
quit(code) |
518 |
|
int code; |
519 |
|
{ |
521 |
|
} |
522 |
|
|
523 |
|
|
524 |
< |
getcascii(col) /* get an ascii color value from stream(s) */ |
525 |
< |
COLOR col; |
524 |
> |
static int |
525 |
> |
getcascii( /* get an ascii color value from stream(s) */ |
526 |
> |
COLOR col |
527 |
> |
) |
528 |
|
{ |
529 |
|
double vd[3]; |
530 |
|
|
542 |
|
} |
543 |
|
|
544 |
|
|
545 |
< |
getcdouble(col) /* get a double color value from stream(s) */ |
546 |
< |
COLOR col; |
545 |
> |
static int |
546 |
> |
getcdouble( /* get a double color value from stream(s) */ |
547 |
> |
COLOR col |
548 |
> |
) |
549 |
|
{ |
550 |
|
double vd[3]; |
551 |
|
|
558 |
|
fread((char *)(vd+2), sizeof(double), 1, fin3) != 1) |
559 |
|
return(-1); |
560 |
|
} |
561 |
+ |
if (swapbytes) |
562 |
+ |
swap64((char *)vd, 3); |
563 |
|
setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]); |
564 |
|
return(0); |
565 |
|
} |
566 |
|
|
567 |
|
|
568 |
< |
getcfloat(col) /* get a float color value from stream(s) */ |
569 |
< |
COLOR col; |
568 |
> |
static int |
569 |
> |
getcfloat( /* get a float color value from stream(s) */ |
570 |
> |
COLOR col |
571 |
> |
) |
572 |
|
{ |
573 |
|
float vf[3]; |
574 |
|
|
581 |
|
fread((char *)(vf+2), sizeof(float), 1, fin3) != 1) |
582 |
|
return(-1); |
583 |
|
} |
584 |
+ |
if (swapbytes) |
585 |
+ |
swap32((char *)vf, 3); |
586 |
|
setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]); |
587 |
|
return(0); |
588 |
|
} |
589 |
|
|
590 |
|
|
591 |
< |
getcint(col) /* get an int color value from stream(s) */ |
592 |
< |
COLOR col; |
591 |
> |
static int |
592 |
> |
getcint( /* get an int color value from stream(s) */ |
593 |
> |
COLOR col |
594 |
> |
) |
595 |
|
{ |
596 |
|
int vi[3]; |
597 |
|
|
610 |
|
} |
611 |
|
|
612 |
|
|
613 |
< |
getcbyte(col) /* get a byte color value from stream(s) */ |
614 |
< |
COLOR col; |
613 |
> |
static int |
614 |
> |
getcbyte( /* get a byte color value from stream(s) */ |
615 |
> |
COLOR col |
616 |
> |
) |
617 |
|
{ |
618 |
|
BYTE vb[3]; |
619 |
|
|
632 |
|
} |
633 |
|
|
634 |
|
|
635 |
< |
getbascii(col) /* get an ascii brightness value from fin */ |
636 |
< |
COLOR col; |
635 |
> |
static int |
636 |
> |
getcword( /* get a 16-bit color value from stream(s) */ |
637 |
> |
COLOR col |
638 |
> |
) |
639 |
|
{ |
640 |
+ |
uint16 vw[3]; |
641 |
+ |
|
642 |
+ |
if (fin2 == NULL) { |
643 |
+ |
if (fread((char *)vw, sizeof(uint16), 3, fin) != 3) |
644 |
+ |
return(-1); |
645 |
+ |
} else { |
646 |
+ |
if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 || |
647 |
+ |
fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 || |
648 |
+ |
fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1) |
649 |
+ |
return(-1); |
650 |
+ |
} |
651 |
+ |
if (swapbytes) |
652 |
+ |
swap16((char *)vw, 3); |
653 |
+ |
setcolor(col, (vw[rord[RED]]+.5)/65536., |
654 |
+ |
(vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.); |
655 |
+ |
return(0); |
656 |
+ |
} |
657 |
+ |
|
658 |
+ |
|
659 |
+ |
static int |
660 |
+ |
getbascii( /* get an ascii brightness value from fin */ |
661 |
+ |
COLOR col |
662 |
+ |
) |
663 |
+ |
{ |
664 |
|
double vd; |
665 |
|
|
666 |
|
if (fscanf(fin, "%lf", &vd) != 1) |
670 |
|
} |
671 |
|
|
672 |
|
|
673 |
< |
getbdouble(col) /* get a double brightness value from fin */ |
674 |
< |
COLOR col; |
673 |
> |
static int |
674 |
> |
getbdouble( /* get a double brightness value from fin */ |
675 |
> |
COLOR col |
676 |
> |
) |
677 |
|
{ |
678 |
|
double vd; |
679 |
|
|
680 |
|
if (fread((char *)&vd, sizeof(double), 1, fin) != 1) |
681 |
|
return(-1); |
682 |
+ |
if (swapbytes) |
683 |
+ |
swap64((char *)&vd, 1); |
684 |
|
setcolor(col, vd, vd, vd); |
685 |
|
return(0); |
686 |
|
} |
687 |
|
|
688 |
|
|
689 |
< |
getbfloat(col) /* get a float brightness value from fin */ |
690 |
< |
COLOR col; |
689 |
> |
static int |
690 |
> |
getbfloat( /* get a float brightness value from fin */ |
691 |
> |
COLOR col |
692 |
> |
) |
693 |
|
{ |
694 |
|
float vf; |
695 |
|
|
696 |
|
if (fread((char *)&vf, sizeof(float), 1, fin) != 1) |
697 |
|
return(-1); |
698 |
+ |
if (swapbytes) |
699 |
+ |
swap32((char *)&vf, 1); |
700 |
|
setcolor(col, vf, vf, vf); |
701 |
|
return(0); |
702 |
|
} |
703 |
|
|
704 |
|
|
705 |
< |
getbint(col) /* get an int brightness value from fin */ |
706 |
< |
COLOR col; |
705 |
> |
static int |
706 |
> |
getbint( /* get an int brightness value from fin */ |
707 |
> |
COLOR col |
708 |
> |
) |
709 |
|
{ |
710 |
|
int vi; |
711 |
|
double d; |
718 |
|
} |
719 |
|
|
720 |
|
|
721 |
< |
getbbyte(col) /* get a byte brightness value from fin */ |
722 |
< |
COLOR col; |
721 |
> |
static int |
722 |
> |
getbbyte( /* get a byte brightness value from fin */ |
723 |
> |
COLOR col |
724 |
> |
) |
725 |
|
{ |
726 |
|
BYTE vb; |
727 |
|
double d; |
734 |
|
} |
735 |
|
|
736 |
|
|
737 |
< |
putcascii(col) /* put an ascii color to stdout */ |
738 |
< |
COLOR col; |
737 |
> |
static int |
738 |
> |
getbword( /* get a 16-bit brightness value from fin */ |
739 |
> |
COLOR col |
740 |
> |
) |
741 |
|
{ |
742 |
+ |
uint16 vw; |
743 |
+ |
double d; |
744 |
+ |
|
745 |
+ |
if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1) |
746 |
+ |
return(-1); |
747 |
+ |
if (swapbytes) |
748 |
+ |
swap16((char *)&vw, 1); |
749 |
+ |
d = (vw+.5)/65536.; |
750 |
+ |
setcolor(col, d, d, d); |
751 |
+ |
return(0); |
752 |
+ |
} |
753 |
+ |
|
754 |
+ |
|
755 |
+ |
static int |
756 |
+ |
putcascii( /* put an ascii color to stdout */ |
757 |
+ |
COLOR col |
758 |
+ |
) |
759 |
+ |
{ |
760 |
|
fprintf(stdout, "%15.3e %15.3e %15.3e\n", |
761 |
|
colval(col,ord[0]), |
762 |
|
colval(col,ord[1]), |
766 |
|
} |
767 |
|
|
768 |
|
|
769 |
< |
putcfloat(col) /* put a float color to stdout */ |
770 |
< |
COLOR col; |
769 |
> |
static int |
770 |
> |
putcfloat( /* put a float color to stdout */ |
771 |
> |
COLOR col |
772 |
> |
) |
773 |
|
{ |
774 |
|
float vf[3]; |
775 |
|
|
776 |
|
vf[0] = colval(col,ord[0]); |
777 |
|
vf[1] = colval(col,ord[1]); |
778 |
|
vf[2] = colval(col,ord[2]); |
779 |
+ |
if (swapbytes) |
780 |
+ |
swap32((char *)vf, 3); |
781 |
|
fwrite((char *)vf, sizeof(float), 3, stdout); |
782 |
|
|
783 |
|
return(ferror(stdout) ? -1 : 0); |
784 |
|
} |
785 |
|
|
786 |
|
|
787 |
< |
putcdouble(col) /* put a double color to stdout */ |
788 |
< |
COLOR col; |
787 |
> |
static int |
788 |
> |
putcdouble( /* put a double color to stdout */ |
789 |
> |
COLOR col |
790 |
> |
) |
791 |
|
{ |
792 |
|
double vd[3]; |
793 |
|
|
794 |
|
vd[0] = colval(col,ord[0]); |
795 |
|
vd[1] = colval(col,ord[1]); |
796 |
|
vd[2] = colval(col,ord[2]); |
797 |
+ |
if (swapbytes) |
798 |
+ |
swap64((char *)vd, 3); |
799 |
|
fwrite((char *)vd, sizeof(double), 3, stdout); |
800 |
|
|
801 |
|
return(ferror(stdout) ? -1 : 0); |
802 |
|
} |
803 |
|
|
804 |
|
|
805 |
< |
putcint(col) /* put an int color to stdout */ |
806 |
< |
COLOR col; |
805 |
> |
static int |
806 |
> |
putcint( /* put an int color to stdout */ |
807 |
> |
COLOR col |
808 |
> |
) |
809 |
|
{ |
810 |
|
fprintf(stdout, "%d %d %d\n", |
811 |
|
(int)(colval(col,ord[0])*256.), |
816 |
|
} |
817 |
|
|
818 |
|
|
819 |
< |
putcbyte(col) /* put a byte color to stdout */ |
820 |
< |
COLOR col; |
819 |
> |
static int |
820 |
> |
putcbyte( /* put a byte color to stdout */ |
821 |
> |
COLOR col |
822 |
> |
) |
823 |
|
{ |
824 |
< |
register int i; |
824 |
> |
long i; |
825 |
|
BYTE vb[3]; |
826 |
|
|
827 |
|
i = colval(col,ord[0])*256.; |
836 |
|
} |
837 |
|
|
838 |
|
|
839 |
< |
putbascii(col) /* put an ascii brightness to stdout */ |
840 |
< |
COLOR col; |
839 |
> |
static int |
840 |
> |
putcword( /* put a 16-bit color to stdout */ |
841 |
> |
COLOR col |
842 |
> |
) |
843 |
|
{ |
844 |
< |
fprintf(stdout, "%15.3e\n", bright(col)); |
844 |
> |
long i; |
845 |
> |
uint16 vw[3]; |
846 |
|
|
847 |
+ |
i = colval(col,ord[0])*65536.; |
848 |
+ |
vw[0] = min(i,65535); |
849 |
+ |
i = colval(col,ord[1])*65536.; |
850 |
+ |
vw[1] = min(i,65535); |
851 |
+ |
i = colval(col,ord[2])*65536.; |
852 |
+ |
vw[2] = min(i,65535); |
853 |
+ |
if (swapbytes) |
854 |
+ |
swap16((char *)vw, 3); |
855 |
+ |
fwrite((char *)vw, sizeof(uint16), 3, stdout); |
856 |
+ |
|
857 |
|
return(ferror(stdout) ? -1 : 0); |
858 |
|
} |
859 |
|
|
860 |
|
|
861 |
< |
putbfloat(col) /* put a float brightness to stdout */ |
862 |
< |
COLOR col; |
861 |
> |
static int |
862 |
> |
putbascii( /* put an ascii brightness to stdout */ |
863 |
> |
COLOR col |
864 |
> |
) |
865 |
|
{ |
866 |
+ |
fprintf(stdout, "%15.3e\n", (*mybright)(col)); |
867 |
+ |
|
868 |
+ |
return(ferror(stdout) ? -1 : 0); |
869 |
+ |
} |
870 |
+ |
|
871 |
+ |
|
872 |
+ |
static int |
873 |
+ |
putbfloat( /* put a float brightness to stdout */ |
874 |
+ |
COLOR col |
875 |
+ |
) |
876 |
+ |
{ |
877 |
|
float vf; |
878 |
|
|
879 |
< |
vf = bright(col); |
879 |
> |
vf = (*mybright)(col); |
880 |
> |
if (swapbytes) |
881 |
> |
swap32((char *)&vf, 1); |
882 |
|
fwrite((char *)&vf, sizeof(float), 1, stdout); |
883 |
|
|
884 |
|
return(ferror(stdout) ? -1 : 0); |
885 |
|
} |
886 |
|
|
887 |
|
|
888 |
< |
putbdouble(col) /* put a double brightness to stdout */ |
889 |
< |
COLOR col; |
888 |
> |
static int |
889 |
> |
putbdouble( /* put a double brightness to stdout */ |
890 |
> |
COLOR col |
891 |
> |
) |
892 |
|
{ |
893 |
|
double vd; |
894 |
|
|
895 |
< |
vd = bright(col); |
895 |
> |
vd = (*mybright)(col); |
896 |
> |
if (swapbytes) |
897 |
> |
swap64((char *)&vd, 1); |
898 |
|
fwrite((char *)&vd, sizeof(double), 1, stdout); |
899 |
|
|
900 |
|
return(ferror(stdout) ? -1 : 0); |
901 |
|
} |
902 |
|
|
903 |
|
|
904 |
< |
putbint(col) /* put an int brightness to stdout */ |
905 |
< |
COLOR col; |
904 |
> |
static int |
905 |
> |
putbint( /* put an int brightness to stdout */ |
906 |
> |
COLOR col |
907 |
> |
) |
908 |
|
{ |
909 |
< |
fprintf(stdout, "%d\n", (int)(bright(col)*256.)); |
909 |
> |
fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.)); |
910 |
|
|
911 |
|
return(ferror(stdout) ? -1 : 0); |
912 |
|
} |
913 |
|
|
914 |
|
|
915 |
< |
putbbyte(col) /* put a byte brightness to stdout */ |
916 |
< |
COLOR col; |
915 |
> |
static int |
916 |
> |
putbbyte( /* put a byte brightness to stdout */ |
917 |
> |
COLOR col |
918 |
> |
) |
919 |
|
{ |
920 |
|
register int i; |
921 |
|
BYTE vb; |
922 |
|
|
923 |
< |
i = bright(col)*256.; |
923 |
> |
i = (*mybright)(col)*256.; |
924 |
|
vb = min(i,255); |
925 |
|
fwrite((char *)&vb, sizeof(BYTE), 1, stdout); |
926 |
|
|
928 |
|
} |
929 |
|
|
930 |
|
|
931 |
< |
putpascii(col) /* put an ascii primary to stdout */ |
932 |
< |
COLOR col; |
931 |
> |
static int |
932 |
> |
putbword( /* put a 16-bit brightness to stdout */ |
933 |
> |
COLOR col |
934 |
> |
) |
935 |
|
{ |
936 |
+ |
long i; |
937 |
+ |
uint16 vw; |
938 |
+ |
|
939 |
+ |
i = (*mybright)(col)*65536.; |
940 |
+ |
vw = min(i,65535); |
941 |
+ |
if (swapbytes) |
942 |
+ |
swap16((char *)&vw, 1); |
943 |
+ |
fwrite((char *)&vw, sizeof(uint16), 1, stdout); |
944 |
+ |
|
945 |
+ |
return(ferror(stdout) ? -1 : 0); |
946 |
+ |
} |
947 |
+ |
|
948 |
+ |
|
949 |
+ |
static int |
950 |
+ |
putpascii( /* put an ascii primary to stdout */ |
951 |
+ |
COLOR col |
952 |
+ |
) |
953 |
+ |
{ |
954 |
|
fprintf(stdout, "%15.3e\n", colval(col,putprim)); |
955 |
|
|
956 |
|
return(ferror(stdout) ? -1 : 0); |
957 |
|
} |
958 |
|
|
959 |
|
|
960 |
< |
putpfloat(col) /* put a float primary to stdout */ |
961 |
< |
COLOR col; |
960 |
> |
static int |
961 |
> |
putpfloat( /* put a float primary to stdout */ |
962 |
> |
COLOR col |
963 |
> |
) |
964 |
|
{ |
965 |
|
float vf; |
966 |
|
|
967 |
|
vf = colval(col,putprim); |
968 |
+ |
if (swapbytes) |
969 |
+ |
swap32((char *)&vf, 1); |
970 |
|
fwrite((char *)&vf, sizeof(float), 1, stdout); |
971 |
|
|
972 |
|
return(ferror(stdout) ? -1 : 0); |
973 |
|
} |
974 |
|
|
975 |
|
|
976 |
< |
putpdouble(col) /* put a double primary to stdout */ |
977 |
< |
COLOR col; |
976 |
> |
static int |
977 |
> |
putpdouble( /* put a double primary to stdout */ |
978 |
> |
COLOR col |
979 |
> |
) |
980 |
|
{ |
981 |
|
double vd; |
982 |
|
|
983 |
|
vd = colval(col,putprim); |
984 |
+ |
if (swapbytes) |
985 |
+ |
swap64((char *)&vd, 1); |
986 |
|
fwrite((char *)&vd, sizeof(double), 1, stdout); |
987 |
|
|
988 |
|
return(ferror(stdout) ? -1 : 0); |
989 |
|
} |
990 |
|
|
991 |
|
|
992 |
< |
putpint(col) /* put an int primary to stdout */ |
993 |
< |
COLOR col; |
992 |
> |
static int |
993 |
> |
putpint( /* put an int primary to stdout */ |
994 |
> |
COLOR col |
995 |
> |
) |
996 |
|
{ |
997 |
|
fprintf(stdout, "%d\n", (int)(colval(col,putprim)*256.)); |
998 |
|
|
1000 |
|
} |
1001 |
|
|
1002 |
|
|
1003 |
< |
putpbyte(col) /* put a byte primary to stdout */ |
1004 |
< |
COLOR col; |
1003 |
> |
static int |
1004 |
> |
putpbyte( /* put a byte primary to stdout */ |
1005 |
> |
COLOR col |
1006 |
> |
) |
1007 |
|
{ |
1008 |
< |
register int i; |
1008 |
> |
long i; |
1009 |
|
BYTE vb; |
1010 |
|
|
1011 |
|
i = colval(col,putprim)*256.; |
1016 |
|
} |
1017 |
|
|
1018 |
|
|
1019 |
< |
set_io() /* set put and get functions */ |
1019 |
> |
static int |
1020 |
> |
putpword( /* put a 16-bit primary to stdout */ |
1021 |
> |
COLOR col |
1022 |
> |
) |
1023 |
|
{ |
1024 |
+ |
long i; |
1025 |
+ |
uint16 vw; |
1026 |
+ |
|
1027 |
+ |
i = colval(col,putprim)*65536.; |
1028 |
+ |
vw = min(i,65535); |
1029 |
+ |
if (swapbytes) |
1030 |
+ |
swap16((char *)&vw, 1); |
1031 |
+ |
fwrite((char *)&vw, sizeof(uint16), 1, stdout); |
1032 |
+ |
|
1033 |
+ |
return(ferror(stdout) ? -1 : 0); |
1034 |
+ |
} |
1035 |
+ |
|
1036 |
+ |
|
1037 |
+ |
static void |
1038 |
+ |
set_io(void) /* set put and get functions */ |
1039 |
+ |
{ |
1040 |
|
switch (format) { |
1041 |
|
case 'a': /* ascii */ |
1042 |
|
if (putprim == BRIGHT) { |
1048 |
|
} else { |
1049 |
|
getval = getcascii; |
1050 |
|
putval = putcascii; |
1051 |
+ |
if (reverse && !interleave) { |
1052 |
+ |
fprintf(stderr, |
1053 |
+ |
"%s: ASCII input files must be interleaved\n", |
1054 |
+ |
progname); |
1055 |
+ |
quit(1); |
1056 |
+ |
} |
1057 |
|
} |
1058 |
|
return; |
1059 |
|
case 'f': /* binary float */ |
1066 |
|
} else { |
1067 |
|
getval = getcfloat; |
1068 |
|
putval = putcfloat; |
1069 |
+ |
if (reverse && !interleave) { |
1070 |
+ |
if (fin2 == NULL) |
1071 |
+ |
goto namerr; |
1072 |
+ |
if (fseek(fin2, |
1073 |
+ |
(long)sizeof(float)*picres.xr*picres.yr, 1)) |
1074 |
+ |
goto seekerr; |
1075 |
+ |
if (fseek(fin3, |
1076 |
+ |
(long)sizeof(float)*2*picres.xr*picres.yr, 1)) |
1077 |
+ |
goto seekerr; |
1078 |
+ |
} |
1079 |
|
} |
1080 |
|
return; |
1081 |
|
case 'd': /* binary double */ |
1088 |
|
} else { |
1089 |
|
getval = getcdouble; |
1090 |
|
putval = putcdouble; |
1091 |
+ |
if (reverse && !interleave) { |
1092 |
+ |
if (fin2 == NULL) |
1093 |
+ |
goto namerr; |
1094 |
+ |
if (fseek(fin2, |
1095 |
+ |
(long)sizeof(double)*picres.xr*picres.yr, 1)) |
1096 |
+ |
goto seekerr; |
1097 |
+ |
if (fseek(fin3, |
1098 |
+ |
(long)sizeof(double)*2*picres.xr*picres.yr, 1)) |
1099 |
+ |
goto seekerr; |
1100 |
+ |
} |
1101 |
|
} |
1102 |
|
return; |
1103 |
|
case 'i': /* integer */ |
1110 |
|
} else { |
1111 |
|
getval = getcint; |
1112 |
|
putval = putcint; |
1113 |
+ |
if (reverse && !interleave) { |
1114 |
+ |
fprintf(stderr, |
1115 |
+ |
"%s: integer input files must be interleaved\n", |
1116 |
+ |
progname); |
1117 |
+ |
quit(1); |
1118 |
+ |
} |
1119 |
|
} |
1120 |
|
return; |
1121 |
|
case 'b': /* byte */ |
1128 |
|
} else { |
1129 |
|
getval = getcbyte; |
1130 |
|
putval = putcbyte; |
1131 |
+ |
if (reverse && !interleave) { |
1132 |
+ |
if (fin2 == NULL) |
1133 |
+ |
goto namerr; |
1134 |
+ |
if (fseek(fin2, |
1135 |
+ |
(long)sizeof(BYTE)*picres.xr*picres.yr, 1)) |
1136 |
+ |
goto seekerr; |
1137 |
+ |
if (fseek(fin3, |
1138 |
+ |
(long)sizeof(BYTE)*2*picres.xr*picres.yr, 1)) |
1139 |
+ |
goto seekerr; |
1140 |
+ |
} |
1141 |
|
} |
1142 |
|
return; |
1143 |
+ |
case 'w': /* 16-bit */ |
1144 |
+ |
if (putprim == BRIGHT) { |
1145 |
+ |
getval = getbword; |
1146 |
+ |
putval = putbword; |
1147 |
+ |
} else if (putprim != ALL) { |
1148 |
+ |
getval = getbword; |
1149 |
+ |
putval = putpword; |
1150 |
+ |
} else { |
1151 |
+ |
getval = getcword; |
1152 |
+ |
putval = putcword; |
1153 |
+ |
if (reverse && !interleave) { |
1154 |
+ |
if (fin2 == NULL) |
1155 |
+ |
goto namerr; |
1156 |
+ |
if (fseek(fin2, |
1157 |
+ |
(long)sizeof(uint16)*picres.xr*picres.yr, 1)) |
1158 |
+ |
goto seekerr; |
1159 |
+ |
if (fseek(fin3, |
1160 |
+ |
(long)sizeof(uint16)*2*picres.xr*picres.yr, 1)) |
1161 |
+ |
goto seekerr; |
1162 |
+ |
} |
1163 |
+ |
} |
1164 |
+ |
return; |
1165 |
|
} |
1166 |
+ |
/* badopt: */ /* label not used */ |
1167 |
+ |
fprintf(stderr, "%s: botched file type\n", progname); |
1168 |
+ |
quit(1); |
1169 |
+ |
namerr: |
1170 |
+ |
fprintf(stderr, "%s: non-interleaved file(s) must be named\n", |
1171 |
+ |
progname); |
1172 |
+ |
quit(1); |
1173 |
+ |
seekerr: |
1174 |
+ |
fprintf(stderr, "%s: cannot seek on interleaved input file\n", |
1175 |
+ |
progname); |
1176 |
+ |
quit(1); |
1177 |
|
} |