--- ray/src/px/pvalue.c 2003/06/05 19:29:34 2.18 +++ ray/src/px/pvalue.c 2019/08/14 18:20:02 2.37 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: pvalue.c,v 2.18 2003/06/05 19:29:34 schorsch Exp $"; +static const char RCSid[] = "$Id: pvalue.c,v 2.37 2019/08/14 18:20:02 greg Exp $"; #endif /* * pvalue.c - program to print pixel values. @@ -7,14 +7,12 @@ static const char RCSid[] = "$Id: pvalue.c,v 2.18 2003 * 4/23/86 */ -#include - +#include "platform.h" #include "standard.h" #include "color.h" #include "resolu.h" +#include "view.h" -typedef unsigned short uint16; /* sizeof (uint16) must == 2 */ - #define min(a,b) ((a)<(b)?(a):(b)) /* what to put out (also RED, GRN, BLU) */ @@ -22,36 +20,25 @@ typedef unsigned short uint16; /* sizeof (uint16) must #define BRIGHT 4 RESOLU picres; /* resolution of picture */ - int uniq = 0; /* print only unique values? */ - int doexposure = 0; /* exposure change? (>100 to print) */ - int dataonly = 0; /* data only format? */ - int putprim = ALL; /* what to put out */ - int reverse = 0; /* reverse conversion? */ - int format = 'a'; /* input/output format */ char *fmtid = "ascii"; /* format identifier for header */ - int header = 1; /* do header? */ - long skipbytes = 0; /* skip bytes in input? */ - -int swapbytes = 0; /* swap bytes in 16-bit words? */ - +int swapbytes = 0; /* swap bytes? */ int interleave = 1; /* file is interleaved? */ - int resolution = 1; /* put/get resolution string? */ - int original = 0; /* convert to original values? */ - int wrongformat = 0; /* wrong input format? */ - double gamcor = 1.0; /* gamma correction */ +RGBPRIMP outprims = stdprims; /* output primaries for reverse conversion */ +RGBPRIMS myprims; + int ord[3] = {RED, GRN, BLU}; /* RGB ordering */ int rord[4]; /* reverse ordering */ @@ -62,35 +49,56 @@ char *progname; FILE *fin; FILE *fin2 = NULL, *fin3 = NULL; /* for other color channels */ -int (*getval)(), (*putval)(); -double -rgb_bright(clr) -COLOR clr; +typedef int getfunc_t(COLOR col); +typedef int putfunc_t(COLOR col); +typedef double brightfunc_t(COLOR col); + +getfunc_t *getval; +putfunc_t *putval; +brightfunc_t *mybright; + +static gethfunc checkhead; +static brightfunc_t rgb_bright, xyz_bright; +static getfunc_t getbascii, getbint, getbdouble, getbfloat, getbbyte, getbword; +static getfunc_t getcascii, getcint, getcdouble, getcfloat, getcbyte, getcword; +static putfunc_t putbascii, putbint, putbdouble, putbfloat, putbbyte, putbword; +static putfunc_t putcascii, putcint, putcdouble, putcfloat, putcbyte, putcword; +static putfunc_t putpascii, putpint, putpdouble, putpfloat, putpbyte, putpword; + +static void set_io(void); +static void pixtoval(void); +static void valtopix(void); + + +static double +rgb_bright( + COLOR clr +) { return(bright(clr)); } -double -xyz_bright(clr) -COLOR clr; +static double +xyz_bright( + COLOR clr +) { return(clr[CIEY]); } -double (*mybright)() = &rgb_bright; - - -main(argc, argv) -int argc; -char **argv; +int +main( + int argc, + char **argv +) { - extern int checkhead(); - extern long atol(); - double d, expval = 1.0; - int i; + const char *inpmode = "r"; + double d, expval = 1.0; + int i; progname = argv[0]; + mybright = &rgb_bright; /* default */ for (i = 1; i < argc; i++) if (argv[i][0] == '-' || argv[i][0] == '+') @@ -142,8 +150,22 @@ char **argv; case 'b': /* brightness values */ putprim = argv[i][0] == '-' ? BRIGHT : ALL; break; - case 'p': /* put primary */ + case 'p': /* primary controls */ switch (argv[i][2]) { + /* these two options affect -r conversion */ + case '\0': + myprims[RED][CIEX] = atof(argv[++i]); + myprims[RED][CIEY] = atof(argv[++i]); + myprims[GRN][CIEX] = atof(argv[++i]); + myprims[GRN][CIEY] = atof(argv[++i]); + myprims[BLU][CIEX] = atof(argv[++i]); + myprims[BLU][CIEY] = atof(argv[++i]); + myprims[WHT][CIEX] = atof(argv[++i]); + myprims[WHT][CIEY] = atof(argv[++i]); + outprims = myprims; + break; + case 'x': case 'X': outprims = NULL; break; + /* the following options affect +r only */ case 'r': case 'R': putprim = RED; break; case 'g': case 'G': putprim = GRN; break; case 'b': case 'B': putprim = BLU; break; @@ -174,11 +196,15 @@ char **argv; 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'; @@ -214,36 +240,50 @@ unkopt: else break; /* recognize special formats */ - if (dataonly && format == 'b') + if (dataonly && format == 'b') { if (putprim == ALL) fmtid = "24-bit_rgb"; else fmtid = "8-bit_grey"; - if (dataonly && format == 'w') + } + if (dataonly && format == 'w') { if (putprim == ALL) fmtid = "48-bit_rgb"; else fmtid = "16-bit_grey"; + } + if (!reverse || (format != 'a') & (format != 'i')) + inpmode = "rb"; /* assign reverse ordering */ rord[ord[0]] = 0; rord[ord[1]] = 1; rord[ord[2]] = 2; /* get input */ if (i == argc) { + long n = skipbytes; + if (inpmode[1] == 'b') + SET_FILE_BINARY(stdin); + while (n-- > 0) + if (getchar() == EOF) { + fprintf(stderr, + "%s: cannot skip %ld bytes on standard input\n", + progname, skipbytes); + quit(1); + } fin = stdin; } else if (i < argc) { - if ((fin = fopen(argv[i], "r")) == NULL) { + if ((fin = fopen(argv[i], inpmode)) == NULL) { fprintf(stderr, "%s: can't open file \"%s\"\n", progname, argv[i]); quit(1); } if (reverse && putprim != BRIGHT && i == argc-3) { - if ((fin2 = fopen(argv[i+1], "r")) == NULL) { + if ((fin2 = fopen(argv[i+1], inpmode)) == NULL) { fprintf(stderr, "%s: can't open file \"%s\"\n", progname, argv[i+1]); quit(1); } - if ((fin3 = fopen(argv[i+2], "r")) == NULL) { + if ((fin3 = fopen(argv[i+2], inpmode)) == NULL) { fprintf(stderr, "%s: can't open file \"%s\"\n", progname, argv[i+2]); quit(1); @@ -252,12 +292,12 @@ unkopt: } else if (i != argc-1) fin = NULL; if (reverse && putprim != BRIGHT && !interleave) { - fin2 = fopen(argv[i], "r"); - fin3 = fopen(argv[i], "r"); + fin2 = fopen(argv[i], inpmode); + fin3 = fopen(argv[i], inpmode); } - if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL && - (fseek(fin2, skipbytes, 0) || - fseek(fin3, skipbytes, 0))))) { + if (skipbytes && (fseek(fin, skipbytes, SEEK_SET) || (fin2 != NULL && + fseek(fin2, skipbytes, SEEK_SET) | + fseek(fin3, skipbytes, SEEK_SET)))) { fprintf(stderr, "%s: cannot skip %ld bytes on input\n", progname, skipbytes); quit(1); @@ -269,11 +309,7 @@ unkopt: } if (reverse) { -#ifdef _WIN32 SET_FILE_BINARY(stdout); - if (format != 'a' && format != 'i') - SET_FILE_BINARY(fin); -#endif /* get header */ if (header) { if (checkheader(fin, fmtid, stdout) < 0) { @@ -312,16 +348,18 @@ unkopt: printargs(i, argv, stdout); if (expval < .99 || expval > 1.01) fputexpos(expval, stdout); - fputformat(COLRFMT, stdout); + if (outprims != NULL) { + if (outprims != stdprims) + fputprims(outprims, stdout); + fputformat(COLRFMT, stdout); + } else /* XYZ data */ + fputformat(CIEFMT, stdout); putchar('\n'); fputsresolu(&picres, stdout); /* always put resolution */ valtopix(); } else { -#ifdef _WIN32 - SET_FILE_BINARY(fin); - if (format != 'a' && format != 'i') + if ((format != 'a') & (format != 'i')) SET_FILE_BINARY(stdout); -#endif /* get header */ getheader(fin, checkhead, NULL); if (wrongformat) { @@ -338,6 +376,13 @@ unkopt: printargs(i, argv, stdout); if (expval < .99 || expval > 1.01) fputexpos(expval, stdout); + if (swapbytes) { + if (nativebigendian()) + puts("BigEndian=0"); + else + puts("BigEndian=1"); + } else if ((format != 'a') & (format != 'i')) + fputendian(stdout); fputformat(fmtid, stdout); putchar('\n'); } @@ -347,25 +392,25 @@ unkopt: } quit(0); + return 0; /* pro forma return */ } -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]; + char fmt[MAXFMTLEN]; double d; COLOR ctmp; + int rv; if (formatval(fmt, line)) { - if (!strcmp(fmt, CIEFMT)) { + if (!strcmp(fmt, CIEFMT)) mybright = &xyz_bright; - if (original) { - scalecolor(exposure, 1./WHTEFFICACY); - doexposure++; - } - } else if (!strcmp(fmt, COLRFMT)) + else if (!strcmp(fmt, COLRFMT)) mybright = &rgb_bright; else wrongformat++; @@ -379,22 +424,24 @@ char *line; colval(exposure,GRN)/colval(ctmp,GRN), colval(exposure,BLU)/colval(ctmp,BLU)); doexposure++; + } else if (reverse && (rv = isbigendian(line)) >= 0) { + swapbytes = (nativebigendian() != rv); } else if (header) fputs(line, stdout); return(0); } -pixtoval() /* convert picture to values */ +static void +pixtoval(void) /* convert picture to values */ { - register COLOR *scanln; + COLOR *scanln; int dogamma; COLOR lastc; - FLOAT hv[2]; + RREAL hv[2]; int startprim, endprim; long startpos; - int y; - register int x; + int x, y; scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); if (scanln == NULL) { @@ -402,14 +449,14 @@ pixtoval() /* convert picture to values */ quit(1); } dogamma = gamcor < .95 || gamcor > 1.05; - if (putprim == ALL && !interleave) { + if ((putprim == ALL) & !interleave) { startprim = RED; endprim = BLU; startpos = ftell(fin); } else { startprim = putprim; endprim = putprim; } for (putprim = startprim; putprim <= endprim; putprim++) { - if (putprim != startprim && fseek(fin, startpos, 0)) { + if (putprim != startprim && fseek(fin, startpos, SEEK_SET)) { fprintf(stderr, "%s: seek error on input file\n", progname); quit(1); @@ -422,7 +469,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) == @@ -432,6 +479,7 @@ pixtoval() /* convert picture to values */ continue; else copycolor(lastc, scanln[x]); + } if (doexposure) multcolor(scanln[x], exposure); if (dogamma) @@ -457,12 +505,13 @@ pixtoval() /* convert picture to values */ } -valtopix() /* convert values to a pixel file */ +static void +valtopix(void) /* convert values to a pixel file */ { - int dogamma; - register COLOR *scanln; - int y; - register int x; + int dogamma; + COLOR *scanln; + COLR rgbe; + int x, y; scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); if (scanln == NULL) { @@ -491,13 +540,23 @@ valtopix() /* convert values to a pixel file */ pow(colval(scanln[x],BLU), gamcor)); if (doexposure) multcolor(scanln[x], exposure); + if (uniq) { /* uncompressed? */ + setcolr(rgbe, scanln[x][RED], + scanln[x][GRN], + scanln[x][BLU]); + if (putbinary(rgbe, sizeof(COLR), 1, stdout) != 1) + goto writerr; + } } - if (fwritescan(scanln, scanlen(&picres), stdout) < 0) { - fprintf(stderr, "%s: write error\n", progname); - quit(1); - } + /* write scan if compressed */ + if (!uniq && fwritescan(scanln, scanlen(&picres), stdout) < 0) + goto writerr; } free((void *)scanln); + return; +writerr: + fprintf(stderr, "%s: write error\n", progname); + quit(1); } @@ -509,19 +568,11 @@ int code; } -swap16(wp, n) /* swap n 16-bit words */ -register uint16 *wp; -int n; +static int +getcascii( /* get an ascii color value from stream(s) */ + COLOR col +) { - while (n-- > 0) { - *wp = *wp << 8 | ((*wp >> 8) & 0xff); - wp++; - } -} - -getcascii(col) /* get an ascii color value from stream(s) */ -COLOR col; -{ double vd[3]; if (fin2 == NULL) { @@ -538,46 +589,56 @@ COLOR col; } -getcdouble(col) /* get a double color value from stream(s) */ -COLOR col; +static int +getcdouble( /* get a double color value from stream(s) */ + COLOR col +) { double vd[3]; if (fin2 == NULL) { - if (fread((char *)vd, sizeof(double), 3, fin) != 3) + if (getbinary(vd, sizeof(double), 3, fin) != 3) return(-1); } else { - if (fread((char *)vd, sizeof(double), 1, fin) != 1 || - fread((char *)(vd+1), sizeof(double), 1, fin2) != 1 || - fread((char *)(vd+2), sizeof(double), 1, fin3) != 1) + if (getbinary(vd, sizeof(double), 1, fin) != 1 || + getbinary(vd+1, sizeof(double), 1, fin2) != 1 || + getbinary(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); } -getcfloat(col) /* get a float color value from stream(s) */ -COLOR col; +static int +getcfloat( /* get a float color value from stream(s) */ + COLOR col +) { float vf[3]; if (fin2 == NULL) { - if (fread((char *)vf, sizeof(float), 3, fin) != 3) + if (getbinary(vf, sizeof(float), 3, fin) != 3) return(-1); } else { - if (fread((char *)vf, sizeof(float), 1, fin) != 1 || - fread((char *)(vf+1), sizeof(float), 1, fin2) != 1 || - fread((char *)(vf+2), sizeof(float), 1, fin3) != 1) + if (getbinary(vf, sizeof(float), 1, fin) != 1 || + getbinary(vf+1, sizeof(float), 1, fin2) != 1 || + getbinary(vf+2, sizeof(float), 1, fin3) != 1) return(-1); } + if (swapbytes) + swap32((char *)vf, 3); setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]); return(0); } -getcint(col) /* get an int color value from stream(s) */ -COLOR col; +static int +getcint( /* get an int color value from stream(s) */ + COLOR col +) { int vi[3]; @@ -596,18 +657,20 @@ COLOR col; } -getcbyte(col) /* get a byte color value from stream(s) */ -COLOR col; +static int +getcbyte( /* get a byte color value from stream(s) */ + COLOR col +) { - BYTE vb[3]; + uby8 vb[3]; if (fin2 == NULL) { - if (fread((char *)vb, sizeof(BYTE), 3, fin) != 3) + if (getbinary(vb, sizeof(uby8), 3, fin) != 3) return(-1); } else { - if (fread((char *)vb, sizeof(BYTE), 1, fin) != 1 || - fread((char *)(vb+1), sizeof(BYTE), 1, fin2) != 1 || - fread((char *)(vb+2), sizeof(BYTE), 1, fin3) != 1) + if (getbinary(vb, sizeof(uby8), 1, fin) != 1 || + getbinary(vb+1, sizeof(uby8), 1, fin2) != 1 || + getbinary(vb+2, sizeof(uby8), 1, fin3) != 1) return(-1); } setcolor(col, (vb[rord[RED]]+.5)/256., @@ -616,30 +679,34 @@ COLOR col; } -getcword(col) /* get a 16-bit color value from stream(s) */ -COLOR col; +static int +getcword( /* 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) + if (getbinary(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) + if (getbinary(vw, sizeof(uint16), 1, fin) != 1 || + getbinary(vw+1, sizeof(uint16), 1, fin2) != 1 || + getbinary(vw+2, sizeof(uint16), 1, fin3) != 1) return(-1); } if (swapbytes) - swap16(vw, 3); + swap16((char *)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; +static int +getbascii( /* get an ascii brightness value from fin */ + COLOR col +) { double vd; @@ -650,32 +717,42 @@ COLOR col; } -getbdouble(col) /* get a double brightness value from fin */ -COLOR col; +static int +getbdouble( /* get a double brightness value from fin */ + COLOR col +) { double vd; - if (fread((char *)&vd, sizeof(double), 1, fin) != 1) + if (getbinary(&vd, sizeof(double), 1, fin) != 1) return(-1); + if (swapbytes) + swap64((char *)&vd, 1); setcolor(col, vd, vd, vd); return(0); } -getbfloat(col) /* get a float brightness value from fin */ -COLOR col; +static int +getbfloat( /* get a float brightness value from fin */ + COLOR col +) { float vf; - if (fread((char *)&vf, sizeof(float), 1, fin) != 1) + if (getbinary(&vf, sizeof(float), 1, fin) != 1) return(-1); + if (swapbytes) + swap32((char *)&vf, 1); setcolor(col, vf, vf, vf); return(0); } -getbint(col) /* get an int brightness value from fin */ -COLOR col; +static int +getbint( /* get an int brightness value from fin */ + COLOR col +) { int vi; double d; @@ -688,13 +765,15 @@ COLOR col; } -getbbyte(col) /* get a byte brightness value from fin */ -COLOR col; +static int +getbbyte( /* get a byte brightness value from fin */ + COLOR col +) { - BYTE vb; + uby8 vb; double d; - if (fread((char *)&vb, sizeof(BYTE), 1, fin) != 1) + if (getbinary(&vb, sizeof(uby8), 1, fin) != 1) return(-1); d = (vb+.5)/256.; setcolor(col, d, d, d); @@ -702,24 +781,28 @@ COLOR col; } -getbword(col) /* get a 16-bit brightness value from fin */ -COLOR col; +static int +getbword( /* get a 16-bit brightness value from fin */ + COLOR col +) { uint16 vw; double d; - if (fread((char *)&vw, sizeof(uint16), 1, fin) != 1) + if (getbinary(&vw, sizeof(uint16), 1, fin) != 1) return(-1); if (swapbytes) - swap16(&vw, 1); + swap16((char *)&vw, 1); d = (vw+.5)/65536.; setcolor(col, d, d, d); return(0); } -putcascii(col) /* put an ascii color to stdout */ -COLOR col; +static int +putcascii( /* put an ascii color to stdout */ + COLOR col +) { fprintf(stdout, "%15.3e %15.3e %15.3e\n", colval(col,ord[0]), @@ -730,36 +813,46 @@ COLOR col; } -putcfloat(col) /* put a float color to stdout */ -COLOR col; +static int +putcfloat( /* put a float color to stdout */ + COLOR col +) { float vf[3]; vf[0] = colval(col,ord[0]); vf[1] = colval(col,ord[1]); vf[2] = colval(col,ord[2]); - fwrite((char *)vf, sizeof(float), 3, stdout); + if (swapbytes) + swap32((char *)vf, 3); + putbinary(vf, sizeof(float), 3, stdout); return(ferror(stdout) ? -1 : 0); } -putcdouble(col) /* put a double color to stdout */ -COLOR col; +static int +putcdouble( /* put a double color to stdout */ + COLOR col +) { double vd[3]; vd[0] = colval(col,ord[0]); vd[1] = colval(col,ord[1]); vd[2] = colval(col,ord[2]); - fwrite((char *)vd, sizeof(double), 3, stdout); + if (swapbytes) + swap64((char *)vd, 3); + putbinary(vd, sizeof(double), 3, stdout); return(ferror(stdout) ? -1 : 0); } -putcint(col) /* put an int color to stdout */ -COLOR col; +static int +putcint( /* put an int color to stdout */ + COLOR col +) { fprintf(stdout, "%d %d %d\n", (int)(colval(col,ord[0])*256.), @@ -770,11 +863,13 @@ COLOR col; } -putcbyte(col) /* put a byte color to stdout */ -COLOR col; +static int +putcbyte( /* put a byte color to stdout */ + COLOR col +) { long i; - BYTE vb[3]; + uby8 vb[3]; i = colval(col,ord[0])*256.; vb[0] = min(i,255); @@ -782,14 +877,16 @@ COLOR col; vb[1] = min(i,255); i = colval(col,ord[2])*256.; vb[2] = min(i,255); - fwrite((char *)vb, sizeof(BYTE), 3, stdout); + putbinary(vb, sizeof(uby8), 3, stdout); return(ferror(stdout) ? -1 : 0); } -putcword(col) /* put a 16-bit color to stdout */ -COLOR col; +static int +putcword( /* put a 16-bit color to stdout */ + COLOR col +) { long i; uint16 vw[3]; @@ -801,15 +898,17 @@ COLOR col; i = colval(col,ord[2])*65536.; vw[2] = min(i,65535); if (swapbytes) - swap16(vw, 3); - fwrite((char *)vw, sizeof(uint16), 3, stdout); + swap16((char *)vw, 3); + putbinary(vw, sizeof(uint16), 3, stdout); return(ferror(stdout) ? -1 : 0); } -putbascii(col) /* put an ascii brightness to stdout */ -COLOR col; +static int +putbascii( /* put an ascii brightness to stdout */ + COLOR col +) { fprintf(stdout, "%15.3e\n", (*mybright)(col)); @@ -817,32 +916,42 @@ COLOR col; } -putbfloat(col) /* put a float brightness to stdout */ -COLOR col; +static int +putbfloat( /* put a float brightness to stdout */ + COLOR col +) { float vf; vf = (*mybright)(col); - fwrite((char *)&vf, sizeof(float), 1, stdout); + if (swapbytes) + swap32((char *)&vf, 1); + putbinary(&vf, sizeof(float), 1, stdout); return(ferror(stdout) ? -1 : 0); } -putbdouble(col) /* put a double brightness to stdout */ -COLOR col; +static int +putbdouble( /* put a double brightness to stdout */ + COLOR col +) { double vd; vd = (*mybright)(col); - fwrite((char *)&vd, sizeof(double), 1, stdout); + if (swapbytes) + swap64((char *)&vd, 1); + putbinary(&vd, sizeof(double), 1, stdout); return(ferror(stdout) ? -1 : 0); } -putbint(col) /* put an int brightness to stdout */ -COLOR col; +static int +putbint( /* put an int brightness to stdout */ + COLOR col +) { fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.)); @@ -850,22 +959,26 @@ COLOR col; } -putbbyte(col) /* put a byte brightness to stdout */ -COLOR col; +static int +putbbyte( /* put a byte brightness to stdout */ + COLOR col +) { - register int i; - BYTE vb; + int i; + uby8 vb; i = (*mybright)(col)*256.; vb = min(i,255); - fwrite((char *)&vb, sizeof(BYTE), 1, stdout); + putbinary(&vb, sizeof(uby8), 1, stdout); return(ferror(stdout) ? -1 : 0); } -putbword(col) /* put a 16-bit brightness to stdout */ -COLOR col; +static int +putbword( /* put a 16-bit brightness to stdout */ + COLOR col +) { long i; uint16 vw; @@ -873,15 +986,17 @@ COLOR col; i = (*mybright)(col)*65536.; vw = min(i,65535); if (swapbytes) - swap16(&vw, 1); - fwrite((char *)&vw, sizeof(uint16), 1, stdout); + swap16((char *)&vw, 1); + putbinary(&vw, sizeof(uint16), 1, stdout); return(ferror(stdout) ? -1 : 0); } -putpascii(col) /* put an ascii primary to stdout */ -COLOR col; +static int +putpascii( /* put an ascii primary to stdout */ + COLOR col +) { fprintf(stdout, "%15.3e\n", colval(col,putprim)); @@ -889,32 +1004,42 @@ COLOR col; } -putpfloat(col) /* put a float primary to stdout */ -COLOR col; +static int +putpfloat( /* put a float primary to stdout */ + COLOR col +) { float vf; vf = colval(col,putprim); - fwrite((char *)&vf, sizeof(float), 1, stdout); + if (swapbytes) + swap32((char *)&vf, 1); + putbinary(&vf, sizeof(float), 1, stdout); return(ferror(stdout) ? -1 : 0); } -putpdouble(col) /* put a double primary to stdout */ -COLOR col; +static int +putpdouble( /* put a double primary to stdout */ + COLOR col +) { double vd; vd = colval(col,putprim); - fwrite((char *)&vd, sizeof(double), 1, stdout); + if (swapbytes) + swap64((char *)&vd, 1); + putbinary(&vd, sizeof(double), 1, stdout); return(ferror(stdout) ? -1 : 0); } -putpint(col) /* put an int primary to stdout */ -COLOR col; +static int +putpint( /* put an int primary to stdout */ + COLOR col +) { fprintf(stdout, "%d\n", (int)(colval(col,putprim)*256.)); @@ -922,22 +1047,26 @@ COLOR col; } -putpbyte(col) /* put a byte primary to stdout */ -COLOR col; +static int +putpbyte( /* put a byte primary to stdout */ + COLOR col +) { long i; - BYTE vb; + uby8 vb; i = colval(col,putprim)*256.; vb = min(i,255); - fwrite((char *)&vb, sizeof(BYTE), 1, stdout); + putbinary(&vb, sizeof(uby8), 1, stdout); return(ferror(stdout) ? -1 : 0); } -putpword(col) /* put a 16-bit primary to stdout */ -COLOR col; +static int +putpword( /* put a 16-bit primary to stdout */ + COLOR col +) { long i; uint16 vw; @@ -945,14 +1074,15 @@ COLOR col; i = colval(col,putprim)*65536.; vw = min(i,65535); if (swapbytes) - swap16(&vw, 1); - fwrite((char *)&vw, sizeof(uint16), 1, stdout); + swap16((char *)&vw, 1); + putbinary(&vw, sizeof(uint16), 1, stdout); return(ferror(stdout) ? -1 : 0); } -set_io() /* set put and get functions */ +static void +set_io(void) /* set put and get functions */ { switch (format) { case 'a': /* ascii */ @@ -987,10 +1117,10 @@ set_io() /* set put and get functions */ if (fin2 == NULL) goto namerr; if (fseek(fin2, - (long)sizeof(float)*picres.xr*picres.yr, 1)) + (long)sizeof(float)*picres.xr*picres.yr, SEEK_CUR)) goto seekerr; if (fseek(fin3, - (long)sizeof(float)*2*picres.xr*picres.yr, 1)) + (long)sizeof(float)*2*picres.xr*picres.yr, SEEK_CUR)) goto seekerr; } } @@ -1009,10 +1139,10 @@ set_io() /* set put and get functions */ if (fin2 == NULL) goto namerr; if (fseek(fin2, - (long)sizeof(double)*picres.xr*picres.yr, 1)) + (long)sizeof(double)*picres.xr*picres.yr, SEEK_CUR)) goto seekerr; if (fseek(fin3, - (long)sizeof(double)*2*picres.xr*picres.yr, 1)) + (long)sizeof(double)*2*picres.xr*picres.yr, SEEK_CUR)) goto seekerr; } } @@ -1049,10 +1179,10 @@ set_io() /* set put and get functions */ if (fin2 == NULL) goto namerr; if (fseek(fin2, - (long)sizeof(BYTE)*picres.xr*picres.yr, 1)) + (long)sizeof(uby8)*picres.xr*picres.yr, SEEK_CUR)) goto seekerr; if (fseek(fin3, - (long)sizeof(BYTE)*2*picres.xr*picres.yr, 1)) + (long)sizeof(uby8)*2*picres.xr*picres.yr, SEEK_CUR)) goto seekerr; } } @@ -1071,16 +1201,16 @@ set_io() /* set put and get functions */ if (fin2 == NULL) goto namerr; if (fseek(fin2, - (long)sizeof(uint16)*picres.xr*picres.yr, 1)) + (long)sizeof(uint16)*picres.xr*picres.yr, SEEK_CUR)) goto seekerr; if (fseek(fin3, - (long)sizeof(uint16)*2*picres.xr*picres.yr, 1)) + (long)sizeof(uint16)*2*picres.xr*picres.yr, SEEK_CUR)) goto seekerr; } } return; } -badopt: +/* badopt: */ /* label not used */ fprintf(stderr, "%s: botched file type\n", progname); quit(1); namerr: