--- ray/src/px/pvalue.c 1997/01/08 08:00:23 2.11 +++ ray/src/px/pvalue.c 2004/03/28 20:33:14 2.26 @@ -1,20 +1,17 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: pvalue.c,v 2.26 2004/03/28 20:33:14 schorsch Exp $"; #endif - /* * pvalue.c - program to print pixel values. * * 4/23/86 */ +#include "platform.h" #include "standard.h" - #include "color.h" - #include "resolu.h" +#include "view.h" #define min(a,b) ((a)<(b)?(a):(b)) @@ -22,33 +19,21 @@ static char SCCSid[] = "$SunId$ LBL"; #define ALL 3 #define BRIGHT 4 -#define brightonly (putprim==BRIGHT) - 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? */ +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 */ int ord[3] = {RED, GRN, BLU}; /* RGB ordering */ @@ -61,19 +46,58 @@ char *progname; FILE *fin; FILE *fin2 = NULL, *fin3 = NULL; /* for other color channels */ -int (*getval)(), (*putval)(); +typedef int getfunc_t(COLOR col); +typedef int putfunc_t(COLOR col); +typedef double brightfunc_t(COLOR col); -main(argc, argv) -int argc; -char **argv; +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 void swap16(uint16 *wp, int n); +static void swap32(uint32 *wp, int n); +static void swap64(char *wp, int n); + + +static double +rgb_bright( + COLOR clr +) { - extern int checkhead(); - extern long atol(); + return(bright(clr)); +} + +static double +xyz_bright( + COLOR clr +) +{ + return(clr[CIEY]); +} + +int +main( + int argc, + char **argv +) +{ 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] == '+') @@ -119,6 +143,9 @@ char **argv; case 'r': /* reverse conversion */ reverse = argv[i][0] == '-'; break; + case 'n': /* non-interleaved RGB */ + interleave = argv[i][0] == '+'; + break; case 'b': /* brightness values */ putprim = argv[i][0] == '-' ? BRIGHT : ALL; break; @@ -147,11 +174,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'; @@ -165,16 +203,16 @@ char **argv; case 'X': /* x resolution */ resolution = 0; if (argv[i][0] == '-') - picres.or |= XDECR; + picres.rt |= XDECR; picres.xr = atoi(argv[++i]); break; case 'y': /* y resolution */ case 'Y': /* y resolution */ resolution = 0; if (argv[i][0] == '-') - picres.or |= YDECR; + picres.rt |= YDECR; if (picres.xr == 0) - picres.or |= YMAJOR; + picres.rt |= YMAJOR; picres.yr = atoi(argv[++i]); break; default: @@ -187,11 +225,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; @@ -205,9 +250,7 @@ unkopt: progname, argv[i]); quit(1); } - if (skipbytes && fseek(fin, skipbytes, 0)) - goto seekerr; - 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]); @@ -218,24 +261,31 @@ unkopt: progname, argv[i+2]); quit(1); } - if (skipbytes && (fseek(fin2, skipbytes, 0) || - fseek(fin3, skipbytes, 0))) - goto seekerr; + interleave = -1; } else if (i != argc-1) fin = NULL; + if (reverse && putprim != BRIGHT && !interleave) { + fin2 = fopen(argv[i], "r"); + fin3 = fopen(argv[i], "r"); + } + if (skipbytes && (fseek(fin, skipbytes, 0) || (fin2 != NULL && + (fseek(fin2, skipbytes, 0) || + fseek(fin3, skipbytes, 0))))) { + fprintf(stderr, "%s: cannot skip %ld bytes on input\n", + progname, skipbytes); + quit(1); + } } if (fin == NULL) { fprintf(stderr, "%s: bad # file arguments\n", progname); quit(1); } - set_io(); - 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) { @@ -259,11 +309,11 @@ unkopt: if (resolution && fin2 != NULL) { RESOLU pres2; if (!fgetsresolu(&pres2, fin2) || - pres2.or != picres.or || + pres2.rt != picres.rt || pres2.xr != picres.xr || pres2.yr != picres.yr || !fgetsresolu(&pres2, fin3) || - pres2.or != picres.or || + pres2.rt != picres.rt || pres2.xr != picres.xr || pres2.yr != picres.yr) { fprintf(stderr, "%s: resolution mismatch\n", @@ -280,15 +330,16 @@ 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); if (wrongformat) { - fprintf(stderr, "%s: input not a Radiance picture\n", + fprintf(stderr, + "%s: input not a Radiance RGBE picture\n", progname); quit(1); } @@ -309,23 +360,32 @@ unkopt: } quit(0); -seekerr: - fprintf(stderr, "%s: cannot skip %ld bytes on input\n", - progname, skipbytes); - quit(1); + return 0; /* pro forma return */ } -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; COLOR ctmp; - if (formatval(fmt, line)) - wrongformat = strcmp(fmt, COLRFMT); - else if (original && isexpos(line)) { + if (formatval(fmt, line)) { + if (!strcmp(fmt, CIEFMT)) { + mybright = &xyz_bright; + if (original) { + scalecolor(exposure, 1./WHTEFFICACY); + doexposure++; + } + } else if (!strcmp(fmt, COLRFMT)) + mybright = &rgb_bright; + else + wrongformat++; + } else if (original && isexpos(line)) { d = 1.0/exposval(line); scalecolor(exposure, d); doexposure++; @@ -337,15 +397,19 @@ char *line; doexposure++; } else if (header) fputs(line, stdout); + return(0); } -pixtoval() /* convert picture to values */ +static void +pixtoval(void) /* convert picture to values */ { register COLOR *scanln; int dogamma; COLOR lastc; - FLOAT hv[2]; + RREAL hv[2]; + int startprim, endprim; + long startpos; int y; register int x; @@ -355,46 +419,64 @@ pixtoval() /* convert picture to values */ quit(1); } dogamma = gamcor < .95 || gamcor > 1.05; - setcolor(lastc, 0.0, 0.0, 0.0); - for (y = 0; y < numscans(&picres); y++) { - if (freadscan(scanln, scanlen(&picres), fin) < 0) { - fprintf(stderr, "%s: read error\n", progname); + 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)) { + fprintf(stderr, "%s: seek error on input file\n", + progname); quit(1); } - for (x = 0; x < scanlen(&picres); x++) { - if (uniq) - if ( colval(scanln[x],RED) == - colval(lastc,RED) && - colval(scanln[x],GRN) == - colval(lastc,GRN) && - colval(scanln[x],BLU) == - colval(lastc,BLU) ) - continue; - else - copycolor(lastc, scanln[x]); - if (doexposure) - multcolor(scanln[x], exposure); - if (dogamma) - setcolor(scanln[x], - pow(colval(scanln[x],RED), 1.0/gamcor), - pow(colval(scanln[x],GRN), 1.0/gamcor), - pow(colval(scanln[x],BLU), 1.0/gamcor)); - if (!dataonly) { - pix2loc(hv, &picres, x, y); - printf("%7d %7d ", (int)(hv[0]*picres.xr), - (int)(hv[1]*picres.yr)); - } - if ((*putval)(scanln[x]) < 0) { - fprintf(stderr, "%s: write error\n", progname); + set_io(); + setcolor(lastc, 0.0, 0.0, 0.0); + for (y = 0; y < numscans(&picres); y++) { + if (freadscan(scanln, scanlen(&picres), fin) < 0) { + fprintf(stderr, "%s: read error\n", progname); quit(1); } + for (x = 0; x < scanlen(&picres); x++) { + if (uniq) { + if ( colval(scanln[x],RED) == + colval(lastc,RED) && + colval(scanln[x],GRN) == + colval(lastc,GRN) && + colval(scanln[x],BLU) == + colval(lastc,BLU) ) + continue; + else + copycolor(lastc, scanln[x]); + } + if (doexposure) + multcolor(scanln[x], exposure); + if (dogamma) + setcolor(scanln[x], + pow(colval(scanln[x],RED), 1.0/gamcor), + pow(colval(scanln[x],GRN), 1.0/gamcor), + pow(colval(scanln[x],BLU), 1.0/gamcor)); + if (!dataonly) { + pix2loc(hv, &picres, x, y); + printf("%7d %7d ", + (int)(hv[0]*picres.xr), + (int)(hv[1]*picres.yr)); + } + if ((*putval)(scanln[x]) < 0) { + fprintf(stderr, "%s: write error\n", + progname); + quit(1); + } + } } } - free((char *)scanln); + free((void *)scanln); } -valtopix() /* convert values to a pixel file */ +static void +valtopix(void) /* convert values to a pixel file */ { int dogamma; register COLOR *scanln; @@ -407,6 +489,7 @@ valtopix() /* convert values to a pixel file */ quit(1); } dogamma = gamcor < .95 || gamcor > 1.05; + set_io(); for (y = 0; y < numscans(&picres); y++) { for (x = 0; x < scanlen(&picres); x++) { if (!dataonly) { @@ -433,10 +516,11 @@ valtopix() /* convert values to a pixel file */ quit(1); } } - free((char *)scanln); + free((void *)scanln); } +void quit(code) int code; { @@ -444,9 +528,56 @@ int code; } -getcascii(col) /* get an ascii color value from stream(s) */ -COLOR col; +static void +swap16( /* swap n 16-bit words */ + register uint16 *wp, + int n +) { + while (n-- > 0) { + *wp = *wp << 8 | ((*wp >> 8) & 0xff); + wp++; + } +} + + +static void +swap32( /* 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++; + } +} + + +static void +swap64( /* 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; + } +} + + +static int +getcascii( /* get an ascii color value from stream(s) */ + COLOR col +) +{ double vd[3]; if (fin2 == NULL) { @@ -463,8 +594,10 @@ 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]; @@ -477,13 +610,17 @@ 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); } -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]; @@ -496,13 +633,17 @@ 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); } -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]; @@ -521,8 +662,10 @@ 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]; @@ -541,9 +684,35 @@ COLOR col; } -getbascii(col) /* get an ascii brightness value from fin */ -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) + 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); +} + + +static int +getbascii( /* get an ascii brightness value from fin */ + COLOR col +) +{ double vd; if (fscanf(fin, "%lf", &vd) != 1) @@ -553,32 +722,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) 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) return(-1); + if (swapbytes) + swap32((uint32 *)&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; @@ -591,8 +770,10 @@ 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; double d; @@ -605,9 +786,29 @@ COLOR col; } -putcascii(col) /* put an ascii color to stdout */ -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) + return(-1); + if (swapbytes) + swap16(&vw, 1); + d = (vw+.5)/65536.; + setcolor(col, d, d, d); + return(0); +} + + +static int +putcascii( /* put an ascii color to stdout */ + COLOR col +) +{ fprintf(stdout, "%15.3e %15.3e %15.3e\n", colval(col,ord[0]), colval(col,ord[1]), @@ -617,36 +818,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]); + if (swapbytes) + swap32((uint32 *)vf, 3); fwrite((char *)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]); + if (swapbytes) + swap64((char *)vd, 3); fwrite((char *)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.), @@ -657,10 +868,12 @@ COLOR col; } -putcbyte(col) /* put a byte color to stdout */ -COLOR col; +static int +putcbyte( /* put a byte color to stdout */ + COLOR col +) { - register int i; + long i; BYTE vb[3]; i = colval(col,ord[0])*256.; @@ -675,55 +888,91 @@ COLOR col; } -putbascii(col) /* put an ascii brightness to stdout */ -COLOR col; +static int +putcword( /* put a 16-bit color to stdout */ + COLOR col +) { - fprintf(stdout, "%15.3e\n", bright(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); } -putbfloat(col) /* put a float brightness to stdout */ -COLOR col; +static int +putbascii( /* put an ascii brightness to stdout */ + COLOR col +) { + fprintf(stdout, "%15.3e\n", (*mybright)(col)); + + return(ferror(stdout) ? -1 : 0); +} + + +static int +putbfloat( /* put a float brightness to stdout */ + COLOR col +) +{ float vf; - vf = bright(col); + vf = (*mybright)(col); + if (swapbytes) + swap32((uint32 *)&vf, 1); fwrite((char *)&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 = bright(col); + vd = (*mybright)(col); + if (swapbytes) + swap64((char *)&vd, 1); fwrite((char *)&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)(bright(col)*256.)); + fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.)); return(ferror(stdout) ? -1 : 0); } -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; - i = bright(col)*256.; + i = (*mybright)(col)*256.; vb = min(i,255); fwrite((char *)&vb, sizeof(BYTE), 1, stdout); @@ -731,41 +980,71 @@ COLOR col; } -putpascii(col) /* put an ascii primary to stdout */ -COLOR col; +static int +putbword( /* 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); +} + + +static int +putpascii( /* put an ascii primary to stdout */ + COLOR col +) +{ fprintf(stdout, "%15.3e\n", colval(col,putprim)); return(ferror(stdout) ? -1 : 0); } -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); + if (swapbytes) + swap32((uint32 *)&vf, 1); fwrite((char *)&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); + if (swapbytes) + swap64((char *)&vd, 1); fwrite((char *)&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.)); @@ -773,10 +1052,12 @@ COLOR col; } -putpbyte(col) /* put a byte primary to stdout */ -COLOR col; +static int +putpbyte( /* put a byte primary to stdout */ + COLOR col +) { - register int i; + long i; BYTE vb; i = colval(col,putprim)*256.; @@ -787,8 +1068,27 @@ COLOR col; } -set_io() /* set put and get functions */ +static int +putpword( /* 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); +} + + +static void +set_io(void) /* set put and get functions */ +{ switch (format) { case 'a': /* ascii */ if (putprim == BRIGHT) { @@ -800,6 +1100,12 @@ set_io() /* set put and get functions */ } else { getval = getcascii; putval = putcascii; + if (reverse && !interleave) { + fprintf(stderr, + "%s: ASCII input files must be interleaved\n", + progname); + quit(1); + } } return; case 'f': /* binary float */ @@ -812,6 +1118,16 @@ set_io() /* set put and get functions */ } else { getval = getcfloat; putval = putcfloat; + if (reverse && !interleave) { + if (fin2 == NULL) + goto namerr; + if (fseek(fin2, + (long)sizeof(float)*picres.xr*picres.yr, 1)) + goto seekerr; + if (fseek(fin3, + (long)sizeof(float)*2*picres.xr*picres.yr, 1)) + goto seekerr; + } } return; case 'd': /* binary double */ @@ -824,6 +1140,16 @@ set_io() /* set put and get functions */ } else { getval = getcdouble; putval = putcdouble; + if (reverse && !interleave) { + if (fin2 == NULL) + goto namerr; + if (fseek(fin2, + (long)sizeof(double)*picres.xr*picres.yr, 1)) + goto seekerr; + if (fseek(fin3, + (long)sizeof(double)*2*picres.xr*picres.yr, 1)) + goto seekerr; + } } return; case 'i': /* integer */ @@ -836,6 +1162,12 @@ set_io() /* set put and get functions */ } else { getval = getcint; putval = putcint; + if (reverse && !interleave) { + fprintf(stderr, + "%s: integer input files must be interleaved\n", + progname); + quit(1); + } } return; case 'b': /* byte */ @@ -848,7 +1180,50 @@ set_io() /* set put and get functions */ } else { getval = getcbyte; putval = putcbyte; + if (reverse && !interleave) { + if (fin2 == NULL) + goto namerr; + if (fseek(fin2, + (long)sizeof(BYTE)*picres.xr*picres.yr, 1)) + 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; + } + } + return; } +/* badopt: */ /* label not used */ + fprintf(stderr, "%s: botched file type\n", progname); + quit(1); +namerr: + fprintf(stderr, "%s: non-interleaved file(s) must be named\n", + progname); + quit(1); +seekerr: + fprintf(stderr, "%s: cannot seek on interleaved input file\n", + progname); + quit(1); }