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. |
8 |
|
*/ |
9 |
|
|
10 |
|
#include "standard.h" |
11 |
< |
|
11 |
> |
#include "platform.h" |
12 |
|
#include "color.h" |
13 |
– |
|
14 |
– |
#include <time.h> |
15 |
– |
|
13 |
|
#include "resolu.h" |
14 |
|
|
15 |
|
#define min(a,b) ((a)<(b)?(a):(b)) |
18 |
|
#define ALL 3 |
19 |
|
#define BRIGHT 4 |
20 |
|
|
24 |
– |
#define brightonly (putprim==BRIGHT) |
25 |
– |
|
21 |
|
RESOLU picres; /* resolution of picture */ |
22 |
|
|
23 |
|
int uniq = 0; /* print only unique values? */ |
37 |
|
|
38 |
|
long skipbytes = 0; /* skip bytes in input? */ |
39 |
|
|
40 |
+ |
int swapbytes = 0; /* swap bytes? */ |
41 |
+ |
|
42 |
|
int interleave = 1; /* file is interleaved? */ |
43 |
|
|
44 |
|
int resolution = 1; /* put/get resolution string? */ |
164 |
|
format = 'b'; |
165 |
|
fmtid = "byte"; |
166 |
|
break; |
167 |
+ |
case 'W': /* 16-bit swapped */ |
168 |
+ |
swapbytes = 1; |
169 |
+ |
case 'w': /* 16-bit */ |
170 |
+ |
dataonly = 1; |
171 |
+ |
format = 'w'; |
172 |
+ |
fmtid = "16-bit"; |
173 |
+ |
break; |
174 |
+ |
case 'F': /* swapped floats */ |
175 |
+ |
swapbytes = 1; |
176 |
|
case 'f': /* float */ |
177 |
|
dataonly = 1; |
178 |
|
format = 'f'; |
179 |
|
fmtid = "float"; |
180 |
|
break; |
181 |
+ |
case 'D': /* swapped doubles */ |
182 |
+ |
swapbytes = 1; |
183 |
|
case 'd': /* double */ |
184 |
|
dataonly = 1; |
185 |
|
format = 'd'; |
215 |
|
else |
216 |
|
break; |
217 |
|
/* recognize special formats */ |
218 |
< |
if (dataonly && format == 'b') |
219 |
< |
if (brightonly) |
218 |
> |
if (dataonly && format == 'b') { |
219 |
> |
if (putprim == ALL) |
220 |
> |
fmtid = "24-bit_rgb"; |
221 |
> |
else |
222 |
|
fmtid = "8-bit_grey"; |
223 |
+ |
} |
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 |
> |
} |
230 |
|
/* assign reverse ordering */ |
231 |
|
rord[ord[0]] = 0; |
232 |
|
rord[ord[1]] = 1; |
240 |
|
progname, argv[i]); |
241 |
|
quit(1); |
242 |
|
} |
243 |
< |
if (reverse && !brightonly && i == argc-3) { |
243 |
> |
if (reverse && putprim != BRIGHT && i == argc-3) { |
244 |
|
if ((fin2 = fopen(argv[i+1], "r")) == NULL) { |
245 |
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
246 |
|
progname, argv[i+1]); |
254 |
|
interleave = -1; |
255 |
|
} else if (i != argc-1) |
256 |
|
fin = NULL; |
257 |
< |
if (reverse && !brightonly && !interleave) { |
257 |
> |
if (reverse && putprim != BRIGHT && !interleave) { |
258 |
|
fin2 = fopen(argv[i], "r"); |
259 |
|
fin3 = fopen(argv[i], "r"); |
260 |
|
} |
272 |
|
} |
273 |
|
|
274 |
|
if (reverse) { |
275 |
< |
#ifdef MSDOS |
276 |
< |
setmode(fileno(stdout), O_BINARY); |
275 |
> |
#ifdef _WIN32 |
276 |
> |
SET_FILE_BINARY(stdout); |
277 |
|
if (format != 'a' && format != 'i') |
278 |
< |
setmode(fileno(fin), O_BINARY); |
278 |
> |
SET_FILE_BINARY(fin); |
279 |
|
#endif |
280 |
|
/* get header */ |
281 |
|
if (header) { |
320 |
|
fputsresolu(&picres, stdout); /* always put resolution */ |
321 |
|
valtopix(); |
322 |
|
} else { |
323 |
< |
#ifdef MSDOS |
324 |
< |
setmode(fileno(fin), O_BINARY); |
323 |
> |
#ifdef _WIN32 |
324 |
> |
SET_FILE_BINARY(fin); |
325 |
|
if (format != 'a' && format != 'i') |
326 |
< |
setmode(fileno(stdout), O_BINARY); |
326 |
> |
SET_FILE_BINARY(stdout); |
327 |
|
#endif |
328 |
|
/* get header */ |
329 |
|
getheader(fin, checkhead, NULL); |
393 |
|
register COLOR *scanln; |
394 |
|
int dogamma; |
395 |
|
COLOR lastc; |
396 |
< |
FLOAT hv[2]; |
396 |
> |
RREAL hv[2]; |
397 |
|
int startprim, endprim; |
398 |
|
long startpos; |
399 |
|
int y; |
425 |
|
quit(1); |
426 |
|
} |
427 |
|
for (x = 0; x < scanlen(&picres); x++) { |
428 |
< |
if (uniq) |
428 |
> |
if (uniq) { |
429 |
|
if ( colval(scanln[x],RED) == |
430 |
|
colval(lastc,RED) && |
431 |
|
colval(scanln[x],GRN) == |
435 |
|
continue; |
436 |
|
else |
437 |
|
copycolor(lastc, scanln[x]); |
438 |
+ |
} |
439 |
|
if (doexposure) |
440 |
|
multcolor(scanln[x], exposure); |
441 |
|
if (dogamma) |
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 |
+ |
|
527 |
+ |
swap32(wp, n) /* swap n 32-bit words */ |
528 |
+ |
register uint32 *wp; |
529 |
+ |
int n; |
530 |
+ |
{ |
531 |
+ |
while (n-- > 0) { |
532 |
+ |
*wp = *wp << 24 | ((*wp >> 24) & 0xff) | |
533 |
+ |
(*wp & 0xff00) << 8 | (*wp & 0xff0000) >> 8; |
534 |
+ |
wp++; |
535 |
+ |
} |
536 |
+ |
} |
537 |
+ |
|
538 |
+ |
|
539 |
+ |
swap64(wp, n) /* swap n 64-bit words */ |
540 |
+ |
register char *wp; |
541 |
+ |
int n; |
542 |
+ |
{ |
543 |
+ |
register int t; |
544 |
+ |
|
545 |
+ |
while (n-- > 0) { |
546 |
+ |
t = wp[0]; wp[0] = wp[7]; wp[7] = t; |
547 |
+ |
t = wp[1]; wp[1] = wp[6]; wp[6] = t; |
548 |
+ |
t = wp[2]; wp[2] = wp[5]; wp[5] = t; |
549 |
+ |
t = wp[3]; wp[3] = wp[4]; wp[4] = t; |
550 |
+ |
wp += 8; |
551 |
+ |
} |
552 |
+ |
} |
553 |
+ |
|
554 |
+ |
|
555 |
|
getcascii(col) /* get an ascii color value from stream(s) */ |
556 |
|
COLOR col; |
557 |
|
{ |
585 |
|
fread((char *)(vd+2), sizeof(double), 1, fin3) != 1) |
586 |
|
return(-1); |
587 |
|
} |
588 |
+ |
if (swapbytes) |
589 |
+ |
swap64((char *)vd, 3); |
590 |
|
setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]); |
591 |
|
return(0); |
592 |
|
} |
606 |
|
fread((char *)(vf+2), sizeof(float), 1, fin3) != 1) |
607 |
|
return(-1); |
608 |
|
} |
609 |
+ |
if (swapbytes) |
610 |
+ |
swap32((uint32 *)vf, 3); |
611 |
|
setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]); |
612 |
|
return(0); |
613 |
|
} |
653 |
|
} |
654 |
|
|
655 |
|
|
656 |
+ |
getcword(col) /* get a 16-bit color value from stream(s) */ |
657 |
+ |
COLOR col; |
658 |
+ |
{ |
659 |
+ |
uint16 vw[3]; |
660 |
+ |
|
661 |
+ |
if (fin2 == NULL) { |
662 |
+ |
if (fread((char *)vw, sizeof(uint16), 3, fin) != 3) |
663 |
+ |
return(-1); |
664 |
+ |
} else { |
665 |
+ |
if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 || |
666 |
+ |
fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 || |
667 |
+ |
fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1) |
668 |
+ |
return(-1); |
669 |
+ |
} |
670 |
+ |
if (swapbytes) |
671 |
+ |
swap16(vw, 3); |
672 |
+ |
setcolor(col, (vw[rord[RED]]+.5)/65536., |
673 |
+ |
(vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.); |
674 |
+ |
return(0); |
675 |
+ |
} |
676 |
+ |
|
677 |
+ |
|
678 |
|
getbascii(col) /* get an ascii brightness value from fin */ |
679 |
|
COLOR col; |
680 |
|
{ |
694 |
|
|
695 |
|
if (fread((char *)&vd, sizeof(double), 1, fin) != 1) |
696 |
|
return(-1); |
697 |
+ |
if (swapbytes) |
698 |
+ |
swap64((char *)&vd, 1); |
699 |
|
setcolor(col, vd, vd, vd); |
700 |
|
return(0); |
701 |
|
} |
708 |
|
|
709 |
|
if (fread((char *)&vf, sizeof(float), 1, fin) != 1) |
710 |
|
return(-1); |
711 |
+ |
if (swapbytes) |
712 |
+ |
swap32((uint32 *)&vf, 1); |
713 |
|
setcolor(col, vf, vf, vf); |
714 |
|
return(0); |
715 |
|
} |
743 |
|
} |
744 |
|
|
745 |
|
|
746 |
+ |
getbword(col) /* get a 16-bit brightness value from fin */ |
747 |
+ |
COLOR col; |
748 |
+ |
{ |
749 |
+ |
uint16 vw; |
750 |
+ |
double d; |
751 |
+ |
|
752 |
+ |
if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1) |
753 |
+ |
return(-1); |
754 |
+ |
if (swapbytes) |
755 |
+ |
swap16(&vw, 1); |
756 |
+ |
d = (vw+.5)/65536.; |
757 |
+ |
setcolor(col, d, d, d); |
758 |
+ |
return(0); |
759 |
+ |
} |
760 |
+ |
|
761 |
+ |
|
762 |
|
putcascii(col) /* put an ascii color to stdout */ |
763 |
|
COLOR col; |
764 |
|
{ |
779 |
|
vf[0] = colval(col,ord[0]); |
780 |
|
vf[1] = colval(col,ord[1]); |
781 |
|
vf[2] = colval(col,ord[2]); |
782 |
+ |
if (swapbytes) |
783 |
+ |
swap32((uint32 *)vf, 3); |
784 |
|
fwrite((char *)vf, sizeof(float), 3, stdout); |
785 |
|
|
786 |
|
return(ferror(stdout) ? -1 : 0); |
795 |
|
vd[0] = colval(col,ord[0]); |
796 |
|
vd[1] = colval(col,ord[1]); |
797 |
|
vd[2] = colval(col,ord[2]); |
798 |
+ |
if (swapbytes) |
799 |
+ |
swap64((char *)vd, 3); |
800 |
|
fwrite((char *)vd, sizeof(double), 3, stdout); |
801 |
|
|
802 |
|
return(ferror(stdout) ? -1 : 0); |
818 |
|
putcbyte(col) /* put a byte color to stdout */ |
819 |
|
COLOR col; |
820 |
|
{ |
821 |
< |
register int i; |
821 |
> |
long i; |
822 |
|
BYTE vb[3]; |
823 |
|
|
824 |
|
i = colval(col,ord[0])*256.; |
833 |
|
} |
834 |
|
|
835 |
|
|
836 |
+ |
putcword(col) /* put a 16-bit color to stdout */ |
837 |
+ |
COLOR col; |
838 |
+ |
{ |
839 |
+ |
long i; |
840 |
+ |
uint16 vw[3]; |
841 |
+ |
|
842 |
+ |
i = colval(col,ord[0])*65536.; |
843 |
+ |
vw[0] = min(i,65535); |
844 |
+ |
i = colval(col,ord[1])*65536.; |
845 |
+ |
vw[1] = min(i,65535); |
846 |
+ |
i = colval(col,ord[2])*65536.; |
847 |
+ |
vw[2] = min(i,65535); |
848 |
+ |
if (swapbytes) |
849 |
+ |
swap16(vw, 3); |
850 |
+ |
fwrite((char *)vw, sizeof(uint16), 3, stdout); |
851 |
+ |
|
852 |
+ |
return(ferror(stdout) ? -1 : 0); |
853 |
+ |
} |
854 |
+ |
|
855 |
+ |
|
856 |
|
putbascii(col) /* put an ascii brightness to stdout */ |
857 |
|
COLOR col; |
858 |
|
{ |
868 |
|
float vf; |
869 |
|
|
870 |
|
vf = (*mybright)(col); |
871 |
+ |
if (swapbytes) |
872 |
+ |
swap32((uint32 *)&vf, 1); |
873 |
|
fwrite((char *)&vf, sizeof(float), 1, stdout); |
874 |
|
|
875 |
|
return(ferror(stdout) ? -1 : 0); |
882 |
|
double vd; |
883 |
|
|
884 |
|
vd = (*mybright)(col); |
885 |
+ |
if (swapbytes) |
886 |
+ |
swap64((char *)&vd, 1); |
887 |
|
fwrite((char *)&vd, sizeof(double), 1, stdout); |
888 |
|
|
889 |
|
return(ferror(stdout) ? -1 : 0); |
913 |
|
} |
914 |
|
|
915 |
|
|
916 |
+ |
putbword(col) /* put a 16-bit brightness to stdout */ |
917 |
+ |
COLOR col; |
918 |
+ |
{ |
919 |
+ |
long i; |
920 |
+ |
uint16 vw; |
921 |
+ |
|
922 |
+ |
i = (*mybright)(col)*65536.; |
923 |
+ |
vw = min(i,65535); |
924 |
+ |
if (swapbytes) |
925 |
+ |
swap16(&vw, 1); |
926 |
+ |
fwrite((char *)&vw, sizeof(uint16), 1, stdout); |
927 |
+ |
|
928 |
+ |
return(ferror(stdout) ? -1 : 0); |
929 |
+ |
} |
930 |
+ |
|
931 |
+ |
|
932 |
|
putpascii(col) /* put an ascii primary to stdout */ |
933 |
|
COLOR col; |
934 |
|
{ |
944 |
|
float vf; |
945 |
|
|
946 |
|
vf = colval(col,putprim); |
947 |
+ |
if (swapbytes) |
948 |
+ |
swap32((uint32 *)&vf, 1); |
949 |
|
fwrite((char *)&vf, sizeof(float), 1, stdout); |
950 |
|
|
951 |
|
return(ferror(stdout) ? -1 : 0); |
958 |
|
double vd; |
959 |
|
|
960 |
|
vd = colval(col,putprim); |
961 |
+ |
if (swapbytes) |
962 |
+ |
swap64((char *)&vd, 1); |
963 |
|
fwrite((char *)&vd, sizeof(double), 1, stdout); |
964 |
|
|
965 |
|
return(ferror(stdout) ? -1 : 0); |
978 |
|
putpbyte(col) /* put a byte primary to stdout */ |
979 |
|
COLOR col; |
980 |
|
{ |
981 |
< |
register int i; |
981 |
> |
long i; |
982 |
|
BYTE vb; |
983 |
|
|
984 |
|
i = colval(col,putprim)*256.; |
989 |
|
} |
990 |
|
|
991 |
|
|
992 |
+ |
putpword(col) /* put a 16-bit primary to stdout */ |
993 |
+ |
COLOR col; |
994 |
+ |
{ |
995 |
+ |
long i; |
996 |
+ |
uint16 vw; |
997 |
+ |
|
998 |
+ |
i = colval(col,putprim)*65536.; |
999 |
+ |
vw = min(i,65535); |
1000 |
+ |
if (swapbytes) |
1001 |
+ |
swap16(&vw, 1); |
1002 |
+ |
fwrite((char *)&vw, sizeof(uint16), 1, stdout); |
1003 |
+ |
|
1004 |
+ |
return(ferror(stdout) ? -1 : 0); |
1005 |
+ |
} |
1006 |
+ |
|
1007 |
+ |
|
1008 |
|
set_io() /* set put and get functions */ |
1009 |
|
{ |
1010 |
|
switch (format) { |
1106 |
|
goto seekerr; |
1107 |
|
if (fseek(fin3, |
1108 |
|
(long)sizeof(BYTE)*2*picres.xr*picres.yr, 1)) |
1109 |
+ |
goto seekerr; |
1110 |
+ |
} |
1111 |
+ |
} |
1112 |
+ |
return; |
1113 |
+ |
case 'w': /* 16-bit */ |
1114 |
+ |
if (putprim == BRIGHT) { |
1115 |
+ |
getval = getbword; |
1116 |
+ |
putval = putbword; |
1117 |
+ |
} else if (putprim != ALL) { |
1118 |
+ |
getval = getbword; |
1119 |
+ |
putval = putpword; |
1120 |
+ |
} else { |
1121 |
+ |
getval = getcword; |
1122 |
+ |
putval = putcword; |
1123 |
+ |
if (reverse && !interleave) { |
1124 |
+ |
if (fin2 == NULL) |
1125 |
+ |
goto namerr; |
1126 |
+ |
if (fseek(fin2, |
1127 |
+ |
(long)sizeof(uint16)*picres.xr*picres.yr, 1)) |
1128 |
+ |
goto seekerr; |
1129 |
+ |
if (fseek(fin3, |
1130 |
+ |
(long)sizeof(uint16)*2*picres.xr*picres.yr, 1)) |
1131 |
|
goto seekerr; |
1132 |
|
} |
1133 |
|
} |