--- ray/src/px/pvalue.c 1998/10/27 09:08:27 2.13 +++ ray/src/px/pvalue.c 2003/06/08 12:03:10 2.19 @@ -1,29 +1,27 @@ -/* 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.19 2003/06/08 12:03:10 schorsch Exp $"; #endif - /* * pvalue.c - program to print pixel values. * * 4/23/86 */ -#include "standard.h" +#include +#include "standard.h" +#include "platform.h" #include "color.h" - #include "resolu.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) */ #define ALL 3 #define BRIGHT 4 -#define brightonly (putprim==BRIGHT) - RESOLU picres; /* resolution of picture */ int uniq = 0; /* print only unique values? */ @@ -43,6 +41,8 @@ int header = 1; /* do header? */ long skipbytes = 0; /* skip bytes in input? */ +int swapbytes = 0; /* swap bytes in 16-bit words? */ + int interleave = 1; /* file is interleaved? */ int resolution = 1; /* put/get resolution string? */ @@ -65,7 +65,23 @@ FILE *fin2 = NULL, *fin3 = NULL; /* for other color c int (*getval)(), (*putval)(); +double +rgb_bright(clr) +COLOR clr; +{ + return(bright(clr)); +} +double +xyz_bright(clr) +COLOR clr; +{ + return(clr[CIEY]); +} + +double (*mybright)() = &rgb_bright; + + main(argc, argv) int argc; char **argv; @@ -152,6 +168,13 @@ 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': /* float */ dataonly = 1; format = 'f'; @@ -170,16 +193,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: @@ -193,10 +216,15 @@ unkopt: break; /* recognize special formats */ if (dataonly && format == 'b') - if (brightonly) + 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; @@ -210,7 +238,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]); @@ -224,7 +252,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"); } @@ -242,10 +270,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) { @@ -269,11 +297,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", @@ -290,15 +318,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); } @@ -330,9 +359,18 @@ char *line; 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++; @@ -416,7 +454,7 @@ pixtoval() /* convert picture to values */ } } } - free((char *)scanln); + free((void *)scanln); } @@ -460,10 +498,11 @@ valtopix() /* convert values to a pixel file */ quit(1); } } - free((char *)scanln); + free((void *)scanln); } +void quit(code) int code; { @@ -471,6 +510,16 @@ 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++; + } +} + getcascii(col) /* get an ascii color value from stream(s) */ COLOR col; { @@ -568,6 +617,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; { @@ -632,6 +703,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; { @@ -687,7 +774,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.; @@ -702,10 +789,30 @@ 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; { - fprintf(stdout, "%15.3e\n", bright(col)); + fprintf(stdout, "%15.3e\n", (*mybright)(col)); return(ferror(stdout) ? -1 : 0); } @@ -716,7 +823,7 @@ COLOR col; { float vf; - vf = bright(col); + vf = (*mybright)(col); fwrite((char *)&vf, sizeof(float), 1, stdout); return(ferror(stdout) ? -1 : 0); @@ -728,7 +835,7 @@ COLOR col; { double vd; - vd = bright(col); + vd = (*mybright)(col); fwrite((char *)&vd, sizeof(double), 1, stdout); return(ferror(stdout) ? -1 : 0); @@ -738,7 +845,7 @@ COLOR col; putbint(col) /* 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); } @@ -750,7 +857,7 @@ 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); @@ -758,6 +865,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; { @@ -803,7 +926,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.; @@ -814,6 +937,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) { @@ -915,6 +1054,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; } }