1 |
|
#ifndef lint |
2 |
< |
static const char RCSid[] = "$Id$"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
|
/* |
5 |
|
* pvalue.c - program to print pixel values. |
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) */ |
44 |
|
|
45 |
|
long skipbytes = 0; /* skip bytes in input? */ |
46 |
|
|
47 |
+ |
int swapbytes = 0; /* swap bytes in 16-bit words? */ |
48 |
+ |
|
49 |
|
int interleave = 1; /* file is interleaved? */ |
50 |
|
|
51 |
|
int resolution = 1; /* put/get resolution string? */ |
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': /* float */ |
182 |
|
dataonly = 1; |
183 |
|
format = 'f'; |
223 |
|
fmtid = "8-bit_grey"; |
224 |
|
else |
225 |
|
fmtid = "24-bit_rgb"; |
226 |
+ |
if (dataonly && format == 'w') |
227 |
+ |
if (brightonly) |
228 |
+ |
fmtid = "16-bit_grey"; |
229 |
+ |
else |
230 |
+ |
fmtid = "48-bit_rgb"; |
231 |
|
/* assign reverse ordering */ |
232 |
|
rord[ord[0]] = 0; |
233 |
|
rord[ord[1]] = 1; |
513 |
|
} |
514 |
|
|
515 |
|
|
516 |
+ |
swap16(wp, n) /* swap n 16-bit words */ |
517 |
+ |
register uint16 *wp; |
518 |
+ |
int n; |
519 |
+ |
{ |
520 |
+ |
while (n-- > 0) { |
521 |
+ |
*wp = *wp << 8 | ((*wp >> 8) & 0xff); |
522 |
+ |
wp++; |
523 |
+ |
} |
524 |
+ |
} |
525 |
+ |
|
526 |
|
getcascii(col) /* get an ascii color value from stream(s) */ |
527 |
|
COLOR col; |
528 |
|
{ |
620 |
|
} |
621 |
|
|
622 |
|
|
623 |
+ |
getcword(col) /* get a 16-bit color value from stream(s) */ |
624 |
+ |
COLOR col; |
625 |
+ |
{ |
626 |
+ |
uint16 vw[3]; |
627 |
+ |
|
628 |
+ |
if (fin2 == NULL) { |
629 |
+ |
if (fread((char *)vw, sizeof(uint16), 3, fin) != 3) |
630 |
+ |
return(-1); |
631 |
+ |
} else { |
632 |
+ |
if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 || |
633 |
+ |
fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 || |
634 |
+ |
fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1) |
635 |
+ |
return(-1); |
636 |
+ |
} |
637 |
+ |
if (swapbytes) |
638 |
+ |
swap16(vw, 3); |
639 |
+ |
setcolor(col, (vw[rord[RED]]+.5)/65536., |
640 |
+ |
(vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.); |
641 |
+ |
return(0); |
642 |
+ |
} |
643 |
+ |
|
644 |
+ |
|
645 |
|
getbascii(col) /* get an ascii brightness value from fin */ |
646 |
|
COLOR col; |
647 |
|
{ |
706 |
|
} |
707 |
|
|
708 |
|
|
709 |
+ |
getbword(col) /* get a 16-bit brightness value from fin */ |
710 |
+ |
COLOR col; |
711 |
+ |
{ |
712 |
+ |
uint16 vw; |
713 |
+ |
double d; |
714 |
+ |
|
715 |
+ |
if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1) |
716 |
+ |
return(-1); |
717 |
+ |
if (swapbytes) |
718 |
+ |
swap16(&vw, 1); |
719 |
+ |
d = (vw+.5)/65536.; |
720 |
+ |
setcolor(col, d, d, d); |
721 |
+ |
return(0); |
722 |
+ |
} |
723 |
+ |
|
724 |
+ |
|
725 |
|
putcascii(col) /* put an ascii color to stdout */ |
726 |
|
COLOR col; |
727 |
|
{ |
777 |
|
putcbyte(col) /* put a byte color to stdout */ |
778 |
|
COLOR col; |
779 |
|
{ |
780 |
< |
register int i; |
780 |
> |
long i; |
781 |
|
BYTE vb[3]; |
782 |
|
|
783 |
|
i = colval(col,ord[0])*256.; |
792 |
|
} |
793 |
|
|
794 |
|
|
795 |
+ |
putcword(col) /* put a 16-bit color to stdout */ |
796 |
+ |
COLOR col; |
797 |
+ |
{ |
798 |
+ |
long i; |
799 |
+ |
uint16 vw[3]; |
800 |
+ |
|
801 |
+ |
i = colval(col,ord[0])*65536.; |
802 |
+ |
vw[0] = min(i,65535); |
803 |
+ |
i = colval(col,ord[1])*65536.; |
804 |
+ |
vw[1] = min(i,65535); |
805 |
+ |
i = colval(col,ord[2])*65536.; |
806 |
+ |
vw[2] = min(i,65535); |
807 |
+ |
if (swapbytes) |
808 |
+ |
swap16(vw, 3); |
809 |
+ |
fwrite((char *)vw, sizeof(uint16), 3, stdout); |
810 |
+ |
|
811 |
+ |
return(ferror(stdout) ? -1 : 0); |
812 |
+ |
} |
813 |
+ |
|
814 |
+ |
|
815 |
|
putbascii(col) /* put an ascii brightness to stdout */ |
816 |
|
COLOR col; |
817 |
|
{ |
868 |
|
} |
869 |
|
|
870 |
|
|
871 |
+ |
putbword(col) /* put a 16-bit brightness to stdout */ |
872 |
+ |
COLOR col; |
873 |
+ |
{ |
874 |
+ |
long i; |
875 |
+ |
uint16 vw; |
876 |
+ |
|
877 |
+ |
i = (*mybright)(col)*65536.; |
878 |
+ |
vw = min(i,65535); |
879 |
+ |
if (swapbytes) |
880 |
+ |
swap16(&vw, 1); |
881 |
+ |
fwrite((char *)&vw, sizeof(uint16), 1, stdout); |
882 |
+ |
|
883 |
+ |
return(ferror(stdout) ? -1 : 0); |
884 |
+ |
} |
885 |
+ |
|
886 |
+ |
|
887 |
|
putpascii(col) /* put an ascii primary to stdout */ |
888 |
|
COLOR col; |
889 |
|
{ |
929 |
|
putpbyte(col) /* put a byte primary to stdout */ |
930 |
|
COLOR col; |
931 |
|
{ |
932 |
< |
register int i; |
932 |
> |
long i; |
933 |
|
BYTE vb; |
934 |
|
|
935 |
|
i = colval(col,putprim)*256.; |
940 |
|
} |
941 |
|
|
942 |
|
|
943 |
+ |
putpword(col) /* put a 16-bit primary to stdout */ |
944 |
+ |
COLOR col; |
945 |
+ |
{ |
946 |
+ |
long i; |
947 |
+ |
uint16 vw; |
948 |
+ |
|
949 |
+ |
i = colval(col,putprim)*65536.; |
950 |
+ |
vw = min(i,65535); |
951 |
+ |
if (swapbytes) |
952 |
+ |
swap16(&vw, 1); |
953 |
+ |
fwrite((char *)&vw, sizeof(uint16), 1, stdout); |
954 |
+ |
|
955 |
+ |
return(ferror(stdout) ? -1 : 0); |
956 |
+ |
} |
957 |
+ |
|
958 |
+ |
|
959 |
|
set_io() /* set put and get functions */ |
960 |
|
{ |
961 |
|
switch (format) { |
1057 |
|
goto seekerr; |
1058 |
|
if (fseek(fin3, |
1059 |
|
(long)sizeof(BYTE)*2*picres.xr*picres.yr, 1)) |
1060 |
+ |
goto seekerr; |
1061 |
+ |
} |
1062 |
+ |
} |
1063 |
+ |
return; |
1064 |
+ |
case 'w': /* 16-bit */ |
1065 |
+ |
if (putprim == BRIGHT) { |
1066 |
+ |
getval = getbword; |
1067 |
+ |
putval = putbword; |
1068 |
+ |
} else if (putprim != ALL) { |
1069 |
+ |
getval = getbword; |
1070 |
+ |
putval = putpword; |
1071 |
+ |
} else { |
1072 |
+ |
getval = getcword; |
1073 |
+ |
putval = putcword; |
1074 |
+ |
if (reverse && !interleave) { |
1075 |
+ |
if (fin2 == NULL) |
1076 |
+ |
goto namerr; |
1077 |
+ |
if (fseek(fin2, |
1078 |
+ |
(long)sizeof(uint16)*picres.xr*picres.yr, 1)) |
1079 |
+ |
goto seekerr; |
1080 |
+ |
if (fseek(fin3, |
1081 |
+ |
(long)sizeof(uint16)*2*picres.xr*picres.yr, 1)) |
1082 |
|
goto seekerr; |
1083 |
|
} |
1084 |
|
} |