--- ray/src/px/pvalue.c 2003/02/22 02:07:27 2.14 +++ ray/src/px/pvalue.c 2004/01/02 12:47:01 2.25 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: pvalue.c,v 2.14 2003/02/22 02:07:27 greg Exp $"; +static const char RCSid[] = "$Id: pvalue.c,v 2.25 2004/01/02 12:47:01 schorsch Exp $"; #endif /* * pvalue.c - program to print pixel values. @@ -7,12 +7,9 @@ static const char RCSid[] = "$Id: pvalue.c,v 2.14 2003 * 4/23/86 */ +#include "platform.h" #include "standard.h" - #include "color.h" - -#include - #include "resolu.h" #define min(a,b) ((a)<(b)?(a):(b)) @@ -21,8 +18,6 @@ static const char RCSid[] = "$Id: pvalue.c,v 2.14 2003 #define ALL 3 #define BRIGHT 4 -#define brightonly (putprim==BRIGHT) - RESOLU picres; /* resolution of picture */ int uniq = 0; /* print only unique values? */ @@ -42,6 +37,8 @@ int header = 1; /* do header? */ long skipbytes = 0; /* skip bytes in input? */ +int swapbytes = 0; /* swap bytes? */ + int interleave = 1; /* file is interleaved? */ int resolution = 1; /* put/get resolution string? */ @@ -64,6 +61,9 @@ FILE *fin2 = NULL, *fin3 = NULL; /* for other color c int (*getval)(), (*putval)(); +static gethfunc checkhead; + + double rgb_bright(clr) COLOR clr; @@ -85,7 +85,6 @@ main(argc, argv) int argc; char **argv; { - extern int checkhead(); extern long atol(); double d, expval = 1.0; int i; @@ -167,11 +166,22 @@ char **argv; format = 'b'; fmtid = "byte"; break; + case 'W': /* 16-bit swapped */ + swapbytes = 1; + case 'w': /* 16-bit */ + dataonly = 1; + format = 'w'; + fmtid = "16-bit"; + break; + case 'F': /* swapped floats */ + swapbytes = 1; case 'f': /* float */ dataonly = 1; format = 'f'; fmtid = "float"; break; + case 'D': /* swapped doubles */ + swapbytes = 1; case 'd': /* double */ dataonly = 1; format = 'd'; @@ -207,11 +217,18 @@ unkopt: else break; /* recognize special formats */ - if (dataonly && format == 'b') - if (brightonly) + if (dataonly && format == 'b') { + if (putprim == ALL) + fmtid = "24-bit_rgb"; + else fmtid = "8-bit_grey"; + } + if (dataonly && format == 'w') { + if (putprim == ALL) + fmtid = "48-bit_rgb"; else - fmtid = "24-bit_rgb"; + fmtid = "16-bit_grey"; + } /* assign reverse ordering */ rord[ord[0]] = 0; rord[ord[1]] = 1; @@ -225,7 +242,7 @@ unkopt: progname, argv[i]); quit(1); } - if (reverse && !brightonly && i == argc-3) { + if (reverse && putprim != BRIGHT && i == argc-3) { if ((fin2 = fopen(argv[i+1], "r")) == NULL) { fprintf(stderr, "%s: can't open file \"%s\"\n", progname, argv[i+1]); @@ -239,7 +256,7 @@ unkopt: interleave = -1; } else if (i != argc-1) fin = NULL; - if (reverse && !brightonly && !interleave) { + if (reverse && putprim != BRIGHT && !interleave) { fin2 = fopen(argv[i], "r"); fin3 = fopen(argv[i], "r"); } @@ -257,10 +274,10 @@ unkopt: } if (reverse) { -#ifdef MSDOS - setmode(fileno(stdout), O_BINARY); +#ifdef _WIN32 + SET_FILE_BINARY(stdout); if (format != 'a' && format != 'i') - setmode(fileno(fin), O_BINARY); + SET_FILE_BINARY(fin); #endif /* get header */ if (header) { @@ -305,10 +322,10 @@ unkopt: fputsresolu(&picres, stdout); /* always put resolution */ valtopix(); } else { -#ifdef MSDOS - setmode(fileno(fin), O_BINARY); +#ifdef _WIN32 + SET_FILE_BINARY(fin); if (format != 'a' && format != 'i') - setmode(fileno(stdout), O_BINARY); + SET_FILE_BINARY(stdout); #endif /* get header */ getheader(fin, checkhead, NULL); @@ -338,9 +355,11 @@ unkopt: } -int -checkhead(line) /* deal with line from header */ -char *line; +static int +checkhead( /* deal with line from header */ + char *line, + void *p +) { char fmt[32]; double d; @@ -378,7 +397,7 @@ pixtoval() /* convert picture to values */ register COLOR *scanln; int dogamma; COLOR lastc; - FLOAT hv[2]; + RREAL hv[2]; int startprim, endprim; long startpos; int y; @@ -410,7 +429,7 @@ pixtoval() /* convert picture to values */ quit(1); } for (x = 0; x < scanlen(&picres); x++) { - if (uniq) + if (uniq) { if ( colval(scanln[x],RED) == colval(lastc,RED) && colval(scanln[x],GRN) == @@ -420,6 +439,7 @@ pixtoval() /* convert picture to values */ continue; else copycolor(lastc, scanln[x]); + } if (doexposure) multcolor(scanln[x], exposure); if (dogamma) @@ -497,6 +517,45 @@ int code; } +swap16(wp, n) /* swap n 16-bit words */ +register uint16 *wp; +int n; +{ + while (n-- > 0) { + *wp = *wp << 8 | ((*wp >> 8) & 0xff); + wp++; + } +} + + +swap32(wp, n) /* swap n 32-bit words */ +register uint32 *wp; +int n; +{ + while (n-- > 0) { + *wp = *wp << 24 | ((*wp >> 24) & 0xff) | + (*wp & 0xff00) << 8 | (*wp & 0xff0000) >> 8; + wp++; + } +} + + +swap64(wp, n) /* swap n 64-bit words */ +register char *wp; +int n; +{ + register int t; + + while (n-- > 0) { + t = wp[0]; wp[0] = wp[7]; wp[7] = t; + t = wp[1]; wp[1] = wp[6]; wp[6] = t; + t = wp[2]; wp[2] = wp[5]; wp[5] = t; + t = wp[3]; wp[3] = wp[4]; wp[4] = t; + wp += 8; + } +} + + getcascii(col) /* get an ascii color value from stream(s) */ COLOR col; { @@ -530,6 +589,8 @@ COLOR col; fread((char *)(vd+2), sizeof(double), 1, fin3) != 1) return(-1); } + if (swapbytes) + swap64((char *)vd, 3); setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]); return(0); } @@ -549,6 +610,8 @@ COLOR col; fread((char *)(vf+2), sizeof(float), 1, fin3) != 1) return(-1); } + if (swapbytes) + swap32((uint32 *)vf, 3); setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]); return(0); } @@ -594,6 +657,28 @@ COLOR col; } +getcword(col) /* get a 16-bit color value from stream(s) */ +COLOR col; +{ + uint16 vw[3]; + + if (fin2 == NULL) { + if (fread((char *)vw, sizeof(uint16), 3, fin) != 3) + return(-1); + } else { + if (fread((char *)vw, sizeof(uint16), 1, fin) != 1 || + fread((char *)(vw+1), sizeof(uint16), 1, fin2) != 1 || + fread((char *)(vw+2), sizeof(uint16), 1, fin3) != 1) + return(-1); + } + if (swapbytes) + swap16(vw, 3); + setcolor(col, (vw[rord[RED]]+.5)/65536., + (vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.); + return(0); +} + + getbascii(col) /* get an ascii brightness value from fin */ COLOR col; { @@ -613,6 +698,8 @@ COLOR col; if (fread((char *)&vd, sizeof(double), 1, fin) != 1) return(-1); + if (swapbytes) + swap64((char *)&vd, 1); setcolor(col, vd, vd, vd); return(0); } @@ -625,6 +712,8 @@ COLOR col; if (fread((char *)&vf, sizeof(float), 1, fin) != 1) return(-1); + if (swapbytes) + swap32((uint32 *)&vf, 1); setcolor(col, vf, vf, vf); return(0); } @@ -658,6 +747,22 @@ COLOR col; } +getbword(col) /* get a 16-bit brightness value from fin */ +COLOR col; +{ + uint16 vw; + double d; + + if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1) + return(-1); + if (swapbytes) + swap16(&vw, 1); + d = (vw+.5)/65536.; + setcolor(col, d, d, d); + return(0); +} + + putcascii(col) /* put an ascii color to stdout */ COLOR col; { @@ -678,6 +783,8 @@ COLOR col; vf[0] = colval(col,ord[0]); vf[1] = colval(col,ord[1]); vf[2] = colval(col,ord[2]); + if (swapbytes) + swap32((uint32 *)vf, 3); fwrite((char *)vf, sizeof(float), 3, stdout); return(ferror(stdout) ? -1 : 0); @@ -692,6 +799,8 @@ COLOR col; vd[0] = colval(col,ord[0]); vd[1] = colval(col,ord[1]); vd[2] = colval(col,ord[2]); + if (swapbytes) + swap64((char *)vd, 3); fwrite((char *)vd, sizeof(double), 3, stdout); return(ferror(stdout) ? -1 : 0); @@ -713,7 +822,7 @@ COLOR col; putcbyte(col) /* put a byte color to stdout */ COLOR col; { - register int i; + long i; BYTE vb[3]; i = colval(col,ord[0])*256.; @@ -728,6 +837,26 @@ COLOR col; } +putcword(col) /* put a 16-bit color to stdout */ +COLOR col; +{ + long i; + uint16 vw[3]; + + i = colval(col,ord[0])*65536.; + vw[0] = min(i,65535); + i = colval(col,ord[1])*65536.; + vw[1] = min(i,65535); + i = colval(col,ord[2])*65536.; + vw[2] = min(i,65535); + if (swapbytes) + swap16(vw, 3); + fwrite((char *)vw, sizeof(uint16), 3, stdout); + + return(ferror(stdout) ? -1 : 0); +} + + putbascii(col) /* put an ascii brightness to stdout */ COLOR col; { @@ -743,6 +872,8 @@ COLOR col; float vf; vf = (*mybright)(col); + if (swapbytes) + swap32((uint32 *)&vf, 1); fwrite((char *)&vf, sizeof(float), 1, stdout); return(ferror(stdout) ? -1 : 0); @@ -755,6 +886,8 @@ COLOR col; double vd; vd = (*mybright)(col); + if (swapbytes) + swap64((char *)&vd, 1); fwrite((char *)&vd, sizeof(double), 1, stdout); return(ferror(stdout) ? -1 : 0); @@ -784,6 +917,22 @@ COLOR col; } +putbword(col) /* put a 16-bit brightness to stdout */ +COLOR col; +{ + long i; + uint16 vw; + + i = (*mybright)(col)*65536.; + vw = min(i,65535); + if (swapbytes) + swap16(&vw, 1); + fwrite((char *)&vw, sizeof(uint16), 1, stdout); + + return(ferror(stdout) ? -1 : 0); +} + + putpascii(col) /* put an ascii primary to stdout */ COLOR col; { @@ -799,6 +948,8 @@ COLOR col; float vf; vf = colval(col,putprim); + if (swapbytes) + swap32((uint32 *)&vf, 1); fwrite((char *)&vf, sizeof(float), 1, stdout); return(ferror(stdout) ? -1 : 0); @@ -811,6 +962,8 @@ COLOR col; double vd; vd = colval(col,putprim); + if (swapbytes) + swap64((char *)&vd, 1); fwrite((char *)&vd, sizeof(double), 1, stdout); return(ferror(stdout) ? -1 : 0); @@ -829,7 +982,7 @@ COLOR col; putpbyte(col) /* put a byte primary to stdout */ COLOR col; { - register int i; + long i; BYTE vb; i = colval(col,putprim)*256.; @@ -840,6 +993,22 @@ COLOR col; } +putpword(col) /* put a 16-bit primary to stdout */ +COLOR col; +{ + long i; + uint16 vw; + + i = colval(col,putprim)*65536.; + vw = min(i,65535); + if (swapbytes) + swap16(&vw, 1); + fwrite((char *)&vw, sizeof(uint16), 1, stdout); + + return(ferror(stdout) ? -1 : 0); +} + + set_io() /* set put and get functions */ { switch (format) { @@ -941,6 +1110,28 @@ set_io() /* set put and get functions */ goto seekerr; if (fseek(fin3, (long)sizeof(BYTE)*2*picres.xr*picres.yr, 1)) + goto seekerr; + } + } + return; + case 'w': /* 16-bit */ + if (putprim == BRIGHT) { + getval = getbword; + putval = putbword; + } else if (putprim != ALL) { + getval = getbword; + putval = putpword; + } else { + getval = getcword; + putval = putcword; + if (reverse && !interleave) { + if (fin2 == NULL) + goto namerr; + if (fseek(fin2, + (long)sizeof(uint16)*picres.xr*picres.yr, 1)) + goto seekerr; + if (fseek(fin3, + (long)sizeof(uint16)*2*picres.xr*picres.yr, 1)) goto seekerr; } }