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 "standard.h" |
10 |
> |
#include <time.h> |
11 |
|
|
12 |
+ |
#include "standard.h" |
13 |
|
#include "color.h" |
16 |
– |
|
14 |
|
#include "resolu.h" |
15 |
|
|
16 |
+ |
typedef unsigned short uint16; /* sizeof (uint16) must == 2 */ |
17 |
+ |
|
18 |
|
#define min(a,b) ((a)<(b)?(a):(b)) |
19 |
|
|
20 |
|
/* what to put out (also RED, GRN, BLU) */ |
21 |
|
#define ALL 3 |
22 |
|
#define BRIGHT 4 |
23 |
|
|
25 |
– |
#define brightonly (putprim==BRIGHT) |
26 |
– |
|
24 |
|
RESOLU picres; /* resolution of picture */ |
25 |
|
|
26 |
|
int uniq = 0; /* print only unique values? */ |
40 |
|
|
41 |
|
long skipbytes = 0; /* skip bytes in input? */ |
42 |
|
|
43 |
+ |
int swapbytes = 0; /* swap bytes in 16-bit words? */ |
44 |
+ |
|
45 |
|
int interleave = 1; /* file is interleaved? */ |
46 |
|
|
47 |
|
int resolution = 1; /* put/get resolution string? */ |
64 |
|
|
65 |
|
int (*getval)(), (*putval)(); |
66 |
|
|
67 |
+ |
double |
68 |
+ |
rgb_bright(clr) |
69 |
+ |
COLOR clr; |
70 |
+ |
{ |
71 |
+ |
return(bright(clr)); |
72 |
+ |
} |
73 |
|
|
74 |
+ |
double |
75 |
+ |
xyz_bright(clr) |
76 |
+ |
COLOR clr; |
77 |
+ |
{ |
78 |
+ |
return(clr[CIEY]); |
79 |
+ |
} |
80 |
+ |
|
81 |
+ |
double (*mybright)() = &rgb_bright; |
82 |
+ |
|
83 |
+ |
|
84 |
|
main(argc, argv) |
85 |
|
int argc; |
86 |
|
char **argv; |
167 |
|
format = 'b'; |
168 |
|
fmtid = "byte"; |
169 |
|
break; |
170 |
+ |
case 'W': /* 16-bit swapped */ |
171 |
+ |
swapbytes = 1; |
172 |
+ |
case 'w': /* 16-bit */ |
173 |
+ |
dataonly = 1; |
174 |
+ |
format = 'w'; |
175 |
+ |
fmtid = "16-bit"; |
176 |
+ |
break; |
177 |
|
case 'f': /* float */ |
178 |
|
dataonly = 1; |
179 |
|
format = 'f'; |
192 |
|
case 'X': /* x resolution */ |
193 |
|
resolution = 0; |
194 |
|
if (argv[i][0] == '-') |
195 |
< |
picres.or |= XDECR; |
195 |
> |
picres.rt |= XDECR; |
196 |
|
picres.xr = atoi(argv[++i]); |
197 |
|
break; |
198 |
|
case 'y': /* y resolution */ |
199 |
|
case 'Y': /* y resolution */ |
200 |
|
resolution = 0; |
201 |
|
if (argv[i][0] == '-') |
202 |
< |
picres.or |= YDECR; |
202 |
> |
picres.rt |= YDECR; |
203 |
|
if (picres.xr == 0) |
204 |
< |
picres.or |= YMAJOR; |
204 |
> |
picres.rt |= YMAJOR; |
205 |
|
picres.yr = atoi(argv[++i]); |
206 |
|
break; |
207 |
|
default: |
215 |
|
break; |
216 |
|
/* recognize special formats */ |
217 |
|
if (dataonly && format == 'b') |
218 |
< |
if (brightonly) |
218 |
> |
if (putprim == ALL) |
219 |
> |
fmtid = "24-bit_rgb"; |
220 |
> |
else |
221 |
|
fmtid = "8-bit_grey"; |
222 |
+ |
if (dataonly && format == 'w') |
223 |
+ |
if (putprim == ALL) |
224 |
+ |
fmtid = "48-bit_rgb"; |
225 |
|
else |
226 |
< |
fmtid = "24-bit_rgb"; |
226 |
> |
fmtid = "16-bit_grey"; |
227 |
|
/* assign reverse ordering */ |
228 |
|
rord[ord[0]] = 0; |
229 |
|
rord[ord[1]] = 1; |
237 |
|
progname, argv[i]); |
238 |
|
quit(1); |
239 |
|
} |
240 |
< |
if (reverse && !brightonly && i == argc-3) { |
240 |
> |
if (reverse && putprim != BRIGHT && i == argc-3) { |
241 |
|
if ((fin2 = fopen(argv[i+1], "r")) == NULL) { |
242 |
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
243 |
|
progname, argv[i+1]); |
251 |
|
interleave = -1; |
252 |
|
} else if (i != argc-1) |
253 |
|
fin = NULL; |
254 |
< |
if (reverse && !brightonly && !interleave) { |
254 |
> |
if (reverse && putprim != BRIGHT && !interleave) { |
255 |
|
fin2 = fopen(argv[i], "r"); |
256 |
|
fin3 = fopen(argv[i], "r"); |
257 |
|
} |
269 |
|
} |
270 |
|
|
271 |
|
if (reverse) { |
272 |
< |
#ifdef MSDOS |
273 |
< |
setmode(fileno(stdout), O_BINARY); |
272 |
> |
#ifdef _WIN32 |
273 |
> |
SET_FILE_BINARY(stdout); |
274 |
|
if (format != 'a' && format != 'i') |
275 |
< |
setmode(fileno(fin), O_BINARY); |
275 |
> |
SET_FILE_BINARY(fin); |
276 |
|
#endif |
277 |
|
/* get header */ |
278 |
|
if (header) { |
296 |
|
if (resolution && fin2 != NULL) { |
297 |
|
RESOLU pres2; |
298 |
|
if (!fgetsresolu(&pres2, fin2) || |
299 |
< |
pres2.or != picres.or || |
299 |
> |
pres2.rt != picres.rt || |
300 |
|
pres2.xr != picres.xr || |
301 |
|
pres2.yr != picres.yr || |
302 |
|
!fgetsresolu(&pres2, fin3) || |
303 |
< |
pres2.or != picres.or || |
303 |
> |
pres2.rt != picres.rt || |
304 |
|
pres2.xr != picres.xr || |
305 |
|
pres2.yr != picres.yr) { |
306 |
|
fprintf(stderr, "%s: resolution mismatch\n", |
317 |
|
fputsresolu(&picres, stdout); /* always put resolution */ |
318 |
|
valtopix(); |
319 |
|
} else { |
320 |
< |
#ifdef MSDOS |
321 |
< |
setmode(fileno(fin), O_BINARY); |
320 |
> |
#ifdef _WIN32 |
321 |
> |
SET_FILE_BINARY(fin); |
322 |
|
if (format != 'a' && format != 'i') |
323 |
< |
setmode(fileno(stdout), O_BINARY); |
323 |
> |
SET_FILE_BINARY(stdout); |
324 |
|
#endif |
325 |
|
/* get header */ |
326 |
|
getheader(fin, checkhead, NULL); |
327 |
|
if (wrongformat) { |
328 |
< |
fprintf(stderr, "%s: input not a Radiance picture\n", |
328 |
> |
fprintf(stderr, |
329 |
> |
"%s: input not a Radiance RGBE picture\n", |
330 |
|
progname); |
331 |
|
quit(1); |
332 |
|
} |
358 |
|
double d; |
359 |
|
COLOR ctmp; |
360 |
|
|
361 |
< |
if (formatval(fmt, line)) |
362 |
< |
wrongformat = strcmp(fmt, COLRFMT); |
363 |
< |
else if (original && isexpos(line)) { |
361 |
> |
if (formatval(fmt, line)) { |
362 |
> |
if (!strcmp(fmt, CIEFMT)) { |
363 |
> |
mybright = &xyz_bright; |
364 |
> |
if (original) { |
365 |
> |
scalecolor(exposure, 1./WHTEFFICACY); |
366 |
> |
doexposure++; |
367 |
> |
} |
368 |
> |
} else if (!strcmp(fmt, COLRFMT)) |
369 |
> |
mybright = &rgb_bright; |
370 |
> |
else |
371 |
> |
wrongformat++; |
372 |
> |
} else if (original && isexpos(line)) { |
373 |
|
d = 1.0/exposval(line); |
374 |
|
scalecolor(exposure, d); |
375 |
|
doexposure++; |
453 |
|
} |
454 |
|
} |
455 |
|
} |
456 |
< |
free((char *)scanln); |
456 |
> |
free((void *)scanln); |
457 |
|
} |
458 |
|
|
459 |
|
|
497 |
|
quit(1); |
498 |
|
} |
499 |
|
} |
500 |
< |
free((char *)scanln); |
500 |
> |
free((void *)scanln); |
501 |
|
} |
502 |
|
|
503 |
|
|
504 |
+ |
void |
505 |
|
quit(code) |
506 |
|
int code; |
507 |
|
{ |
509 |
|
} |
510 |
|
|
511 |
|
|
512 |
+ |
swap16(wp, n) /* swap n 16-bit words */ |
513 |
+ |
register uint16 *wp; |
514 |
+ |
int n; |
515 |
+ |
{ |
516 |
+ |
while (n-- > 0) { |
517 |
+ |
*wp = *wp << 8 | ((*wp >> 8) & 0xff); |
518 |
+ |
wp++; |
519 |
+ |
} |
520 |
+ |
} |
521 |
+ |
|
522 |
|
getcascii(col) /* get an ascii color value from stream(s) */ |
523 |
|
COLOR col; |
524 |
|
{ |
616 |
|
} |
617 |
|
|
618 |
|
|
619 |
+ |
getcword(col) /* get a 16-bit color value from stream(s) */ |
620 |
+ |
COLOR col; |
621 |
+ |
{ |
622 |
+ |
uint16 vw[3]; |
623 |
+ |
|
624 |
+ |
if (fin2 == NULL) { |
625 |
+ |
if (fread((char *)vw, sizeof(uint16), 3, fin) != 3) |
626 |
+ |
return(-1); |
627 |
+ |
} else { |
628 |
+ |
if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 || |
629 |
+ |
fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 || |
630 |
+ |
fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1) |
631 |
+ |
return(-1); |
632 |
+ |
} |
633 |
+ |
if (swapbytes) |
634 |
+ |
swap16(vw, 3); |
635 |
+ |
setcolor(col, (vw[rord[RED]]+.5)/65536., |
636 |
+ |
(vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.); |
637 |
+ |
return(0); |
638 |
+ |
} |
639 |
+ |
|
640 |
+ |
|
641 |
|
getbascii(col) /* get an ascii brightness value from fin */ |
642 |
|
COLOR col; |
643 |
|
{ |
702 |
|
} |
703 |
|
|
704 |
|
|
705 |
+ |
getbword(col) /* get a 16-bit brightness value from fin */ |
706 |
+ |
COLOR col; |
707 |
+ |
{ |
708 |
+ |
uint16 vw; |
709 |
+ |
double d; |
710 |
+ |
|
711 |
+ |
if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1) |
712 |
+ |
return(-1); |
713 |
+ |
if (swapbytes) |
714 |
+ |
swap16(&vw, 1); |
715 |
+ |
d = (vw+.5)/65536.; |
716 |
+ |
setcolor(col, d, d, d); |
717 |
+ |
return(0); |
718 |
+ |
} |
719 |
+ |
|
720 |
+ |
|
721 |
|
putcascii(col) /* put an ascii color to stdout */ |
722 |
|
COLOR col; |
723 |
|
{ |
773 |
|
putcbyte(col) /* put a byte color to stdout */ |
774 |
|
COLOR col; |
775 |
|
{ |
776 |
< |
register int i; |
776 |
> |
long i; |
777 |
|
BYTE vb[3]; |
778 |
|
|
779 |
|
i = colval(col,ord[0])*256.; |
788 |
|
} |
789 |
|
|
790 |
|
|
791 |
+ |
putcword(col) /* put a 16-bit color to stdout */ |
792 |
+ |
COLOR col; |
793 |
+ |
{ |
794 |
+ |
long i; |
795 |
+ |
uint16 vw[3]; |
796 |
+ |
|
797 |
+ |
i = colval(col,ord[0])*65536.; |
798 |
+ |
vw[0] = min(i,65535); |
799 |
+ |
i = colval(col,ord[1])*65536.; |
800 |
+ |
vw[1] = min(i,65535); |
801 |
+ |
i = colval(col,ord[2])*65536.; |
802 |
+ |
vw[2] = min(i,65535); |
803 |
+ |
if (swapbytes) |
804 |
+ |
swap16(vw, 3); |
805 |
+ |
fwrite((char *)vw, sizeof(uint16), 3, stdout); |
806 |
+ |
|
807 |
+ |
return(ferror(stdout) ? -1 : 0); |
808 |
+ |
} |
809 |
+ |
|
810 |
+ |
|
811 |
|
putbascii(col) /* put an ascii brightness to stdout */ |
812 |
|
COLOR col; |
813 |
|
{ |
814 |
< |
fprintf(stdout, "%15.3e\n", bright(col)); |
814 |
> |
fprintf(stdout, "%15.3e\n", (*mybright)(col)); |
815 |
|
|
816 |
|
return(ferror(stdout) ? -1 : 0); |
817 |
|
} |
822 |
|
{ |
823 |
|
float vf; |
824 |
|
|
825 |
< |
vf = bright(col); |
825 |
> |
vf = (*mybright)(col); |
826 |
|
fwrite((char *)&vf, sizeof(float), 1, stdout); |
827 |
|
|
828 |
|
return(ferror(stdout) ? -1 : 0); |
834 |
|
{ |
835 |
|
double vd; |
836 |
|
|
837 |
< |
vd = bright(col); |
837 |
> |
vd = (*mybright)(col); |
838 |
|
fwrite((char *)&vd, sizeof(double), 1, stdout); |
839 |
|
|
840 |
|
return(ferror(stdout) ? -1 : 0); |
844 |
|
putbint(col) /* put an int brightness to stdout */ |
845 |
|
COLOR col; |
846 |
|
{ |
847 |
< |
fprintf(stdout, "%d\n", (int)(bright(col)*256.)); |
847 |
> |
fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.)); |
848 |
|
|
849 |
|
return(ferror(stdout) ? -1 : 0); |
850 |
|
} |
856 |
|
register int i; |
857 |
|
BYTE vb; |
858 |
|
|
859 |
< |
i = bright(col)*256.; |
859 |
> |
i = (*mybright)(col)*256.; |
860 |
|
vb = min(i,255); |
861 |
|
fwrite((char *)&vb, sizeof(BYTE), 1, stdout); |
862 |
|
|
864 |
|
} |
865 |
|
|
866 |
|
|
867 |
+ |
putbword(col) /* put a 16-bit brightness to stdout */ |
868 |
+ |
COLOR col; |
869 |
+ |
{ |
870 |
+ |
long i; |
871 |
+ |
uint16 vw; |
872 |
+ |
|
873 |
+ |
i = (*mybright)(col)*65536.; |
874 |
+ |
vw = min(i,65535); |
875 |
+ |
if (swapbytes) |
876 |
+ |
swap16(&vw, 1); |
877 |
+ |
fwrite((char *)&vw, sizeof(uint16), 1, stdout); |
878 |
+ |
|
879 |
+ |
return(ferror(stdout) ? -1 : 0); |
880 |
+ |
} |
881 |
+ |
|
882 |
+ |
|
883 |
|
putpascii(col) /* put an ascii primary to stdout */ |
884 |
|
COLOR col; |
885 |
|
{ |
925 |
|
putpbyte(col) /* put a byte primary to stdout */ |
926 |
|
COLOR col; |
927 |
|
{ |
928 |
< |
register int i; |
928 |
> |
long i; |
929 |
|
BYTE vb; |
930 |
|
|
931 |
|
i = colval(col,putprim)*256.; |
936 |
|
} |
937 |
|
|
938 |
|
|
939 |
+ |
putpword(col) /* put a 16-bit primary to stdout */ |
940 |
+ |
COLOR col; |
941 |
+ |
{ |
942 |
+ |
long i; |
943 |
+ |
uint16 vw; |
944 |
+ |
|
945 |
+ |
i = colval(col,putprim)*65536.; |
946 |
+ |
vw = min(i,65535); |
947 |
+ |
if (swapbytes) |
948 |
+ |
swap16(&vw, 1); |
949 |
+ |
fwrite((char *)&vw, sizeof(uint16), 1, stdout); |
950 |
+ |
|
951 |
+ |
return(ferror(stdout) ? -1 : 0); |
952 |
+ |
} |
953 |
+ |
|
954 |
+ |
|
955 |
|
set_io() /* set put and get functions */ |
956 |
|
{ |
957 |
|
switch (format) { |
1053 |
|
goto seekerr; |
1054 |
|
if (fseek(fin3, |
1055 |
|
(long)sizeof(BYTE)*2*picres.xr*picres.yr, 1)) |
1056 |
+ |
goto seekerr; |
1057 |
+ |
} |
1058 |
+ |
} |
1059 |
+ |
return; |
1060 |
+ |
case 'w': /* 16-bit */ |
1061 |
+ |
if (putprim == BRIGHT) { |
1062 |
+ |
getval = getbword; |
1063 |
+ |
putval = putbword; |
1064 |
+ |
} else if (putprim != ALL) { |
1065 |
+ |
getval = getbword; |
1066 |
+ |
putval = putpword; |
1067 |
+ |
} else { |
1068 |
+ |
getval = getcword; |
1069 |
+ |
putval = putcword; |
1070 |
+ |
if (reverse && !interleave) { |
1071 |
+ |
if (fin2 == NULL) |
1072 |
+ |
goto namerr; |
1073 |
+ |
if (fseek(fin2, |
1074 |
+ |
(long)sizeof(uint16)*picres.xr*picres.yr, 1)) |
1075 |
+ |
goto seekerr; |
1076 |
+ |
if (fseek(fin3, |
1077 |
+ |
(long)sizeof(uint16)*2*picres.xr*picres.yr, 1)) |
1078 |
|
goto seekerr; |
1079 |
|
} |
1080 |
|
} |