--- ray/src/px/pvalue.c 2010/12/16 21:34:41 2.30 +++ ray/src/px/pvalue.c 2020/06/30 22:30:29 2.40 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: pvalue.c,v 2.30 2010/12/16 21:34:41 greg Exp $"; +static const char RCSid[] = "$Id: pvalue.c,v 2.40 2020/06/30 22:30:29 greg Exp $"; #endif /* * pvalue.c - program to print pixel values. @@ -93,8 +93,9 @@ main( char **argv ) { - 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 */ @@ -172,7 +173,7 @@ main( } break; case 'd': /* data only (no indices) */ - dataonly = argv[i][0] == '-'; + dataonly = (argv[i][0] == '-'); switch (argv[i][2]) { case '\0': case 'a': /* ascii */ @@ -251,26 +252,38 @@ unkopt: 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); @@ -279,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); @@ -296,16 +309,13 @@ 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) { - fprintf(stderr, "%s: wrong input format\n", - progname); + getheader(fin, checkhead, stdout); + if (wrongformat) { + fprintf(stderr, "%s: wrong input format (expected %s)\n", + progname, fmtid); quit(1); } if (fin2 != NULL) { @@ -349,13 +359,10 @@ unkopt: 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); + getheader(fin, checkhead, header ? stdout : (FILE *)NULL); if (wrongformat) { fprintf(stderr, "%s: input not a Radiance RGBE picture\n", @@ -368,8 +375,20 @@ unkopt: } if (header) { printargs(i, argv, stdout); + printf("NCOMP=%d\n", putprim==ALL ? 3 : 1); + if (!resolution && dataonly && !uniq) + printf("NCOLS=%d\nNROWS=%d\n", scanlen(&picres), + numscans(&picres)); 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') & + (format != 'b')) + fputendian(stdout); fputformat(fmtid, stdout); putchar('\n'); } @@ -389,29 +408,44 @@ checkhead( /* deal with line from header */ void *p ) { - char fmt[32]; + FILE *fout = (FILE *)p; + char fmt[MAXFMTLEN]; double d; COLOR ctmp; + int rv; if (formatval(fmt, line)) { - if (!strcmp(fmt, CIEFMT)) + if (reverse) + wrongformat = strcmp(fmt, fmtid); + else if (!strcmp(fmt, CIEFMT)) mybright = &xyz_bright; else if (!strcmp(fmt, COLRFMT)) mybright = &rgb_bright; else wrongformat++; - } else if (original && isexpos(line)) { + return(1); + } + if (original && isexpos(line)) { d = 1.0/exposval(line); scalecolor(exposure, d); doexposure++; - } else if (original && iscolcor(line)) { + return(1); + } + if (original && iscolcor(line)) { colcorval(ctmp, line); setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED), colval(exposure,GRN)/colval(ctmp,GRN), colval(exposure,BLU)/colval(ctmp,BLU)); doexposure++; - } else if (header) - fputs(line, stdout); + return(1); + } + if ((rv = isbigendian(line)) >= 0) { + if (reverse) + swapbytes = (nativebigendian() != rv); + return(1); + } + if (fout != NULL) + fputs(line, fout); return(0); } @@ -419,14 +453,13 @@ checkhead( /* deal with line from header */ static void pixtoval(void) /* convert picture to values */ { - register COLOR *scanln; + COLOR *scanln; int dogamma; COLOR lastc; 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) { @@ -434,14 +467,14 @@ pixtoval(void) /* 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); @@ -493,10 +526,10 @@ pixtoval(void) /* convert picture to values */ 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) { @@ -525,13 +558,23 @@ valtopix(void) /* 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); } @@ -572,12 +615,12 @@ getcdouble( /* get a double color value from stream(s 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) @@ -595,12 +638,12 @@ getcfloat( /* get a float color value from stream(s) 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) @@ -637,15 +680,15 @@ 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., @@ -662,12 +705,12 @@ getcword( /* get a 16-bit color value from stream(s) 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) @@ -699,7 +742,7 @@ getbdouble( /* get a double brightness value from fin { 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); @@ -715,7 +758,7 @@ getbfloat( /* get a float brightness value from fin * { 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); @@ -745,10 +788,10 @@ 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); @@ -764,7 +807,7 @@ getbword( /* get a 16-bit brightness value from fin * 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((char *)&vw, 1); @@ -800,7 +843,7 @@ putcfloat( /* put a float color to stdout */ vf[2] = colval(col,ord[2]); if (swapbytes) swap32((char *)vf, 3); - fwrite((char *)vf, sizeof(float), 3, stdout); + putbinary(vf, sizeof(float), 3, stdout); return(ferror(stdout) ? -1 : 0); } @@ -818,7 +861,7 @@ putcdouble( /* put a double color to stdout */ vd[2] = colval(col,ord[2]); if (swapbytes) swap64((char *)vd, 3); - fwrite((char *)vd, sizeof(double), 3, stdout); + putbinary(vd, sizeof(double), 3, stdout); return(ferror(stdout) ? -1 : 0); } @@ -844,7 +887,7 @@ putcbyte( /* put a byte color to stdout */ ) { long i; - BYTE vb[3]; + uby8 vb[3]; i = colval(col,ord[0])*256.; vb[0] = min(i,255); @@ -852,7 +895,7 @@ putcbyte( /* put a byte color to stdout */ 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); } @@ -874,7 +917,7 @@ putcword( /* put a 16-bit color to stdout */ vw[2] = min(i,65535); if (swapbytes) swap16((char *)vw, 3); - fwrite((char *)vw, sizeof(uint16), 3, stdout); + putbinary(vw, sizeof(uint16), 3, stdout); return(ferror(stdout) ? -1 : 0); } @@ -901,7 +944,7 @@ putbfloat( /* put a float brightness to stdout */ vf = (*mybright)(col); if (swapbytes) swap32((char *)&vf, 1); - fwrite((char *)&vf, sizeof(float), 1, stdout); + putbinary(&vf, sizeof(float), 1, stdout); return(ferror(stdout) ? -1 : 0); } @@ -917,7 +960,7 @@ putbdouble( /* put a double brightness to stdout */ vd = (*mybright)(col); if (swapbytes) swap64((char *)&vd, 1); - fwrite((char *)&vd, sizeof(double), 1, stdout); + putbinary(&vd, sizeof(double), 1, stdout); return(ferror(stdout) ? -1 : 0); } @@ -939,12 +982,12 @@ 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); } @@ -962,7 +1005,7 @@ putbword( /* put a 16-bit brightness to stdout */ vw = min(i,65535); if (swapbytes) swap16((char *)&vw, 1); - fwrite((char *)&vw, sizeof(uint16), 1, stdout); + putbinary(&vw, sizeof(uint16), 1, stdout); return(ferror(stdout) ? -1 : 0); } @@ -989,7 +1032,7 @@ putpfloat( /* put a float primary to stdout */ vf = colval(col,putprim); if (swapbytes) swap32((char *)&vf, 1); - fwrite((char *)&vf, sizeof(float), 1, stdout); + putbinary(&vf, sizeof(float), 1, stdout); return(ferror(stdout) ? -1 : 0); } @@ -1005,7 +1048,7 @@ putpdouble( /* put a double primary to stdout */ vd = colval(col,putprim); if (swapbytes) swap64((char *)&vd, 1); - fwrite((char *)&vd, sizeof(double), 1, stdout); + putbinary(&vd, sizeof(double), 1, stdout); return(ferror(stdout) ? -1 : 0); } @@ -1028,11 +1071,11 @@ putpbyte( /* put a byte primary to stdout */ ) { 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); } @@ -1050,7 +1093,7 @@ putpword( /* put a 16-bit primary to stdout */ vw = min(i,65535); if (swapbytes) swap16((char *)&vw, 1); - fwrite((char *)&vw, sizeof(uint16), 1, stdout); + putbinary(&vw, sizeof(uint16), 1, stdout); return(ferror(stdout) ? -1 : 0); } @@ -1092,10 +1135,10 @@ set_io(void) /* 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; } } @@ -1114,10 +1157,10 @@ set_io(void) /* 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; } } @@ -1154,10 +1197,10 @@ set_io(void) /* 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; } } @@ -1176,10 +1219,10 @@ set_io(void) /* 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; } }