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