--- ray/src/px/pvalue.c 1991/05/10 08:51:53 1.7 +++ ray/src/px/pvalue.c 1993/11/09 15:21:00 2.7 @@ -1,4 +1,4 @@ -/* Copyright (c) 1991 Regents of the University of California */ +/* Copyright (c) 1992 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -10,18 +10,19 @@ static char SCCSid[] = "$SunId$ LBL"; * 4/23/86 */ -#include +#include "standard.h" #include "color.h" -#define min(a,b) ((a)<(b)?(a):(b)) +#include "resolu.h" -int xres = 0; /* resolution of input */ -int yres = 0; +#define min(a,b) ((a)<(b)?(a):(b)) +RESOLU picres; /* resolution of picture */ + int uniq = 0; /* print only unique values? */ -int original = 0; /* convert back to original values? */ +int doexposure = 0; /* exposure change? (>100 to print) */ int dataonly = 0; /* data only format? */ @@ -32,20 +33,25 @@ int reverse = 0; /* reverse conversion? */ int format = 'a'; /* input/output format */ char *fmtid = "ascii"; /* format identifier for header */ -int header = 1; /* do header */ +int header = 1; /* do header? */ +int resolution = 1; /* put/get resolution string? */ + +int original = 0; /* convert to original values? */ + int wrongformat = 0; /* wrong input format? */ -double gamma = 1.0; /* gamma correction */ +double gamcor = 1.0; /* gamma correction */ +int ord[3] = {RED, GRN, BLU}; /* RGB ordering */ +int rord[4]; /* reverse ordering */ + COLOR exposure = WHTCOLOR; char *progname; FILE *fin; -extern double atof(), pow(); - int (*getval)(), (*putval)(); @@ -54,33 +60,58 @@ int argc; char **argv; { extern int checkhead(); + double d, expval = 1.0; int i; progname = argv[0]; for (i = 1; i < argc; i++) - if (argv[i][0] == '-') + if (argv[i][0] == '-' || argv[i][0] == '+') switch (argv[i][1]) { - case 'h': /* no header */ - header = 0; + case 'h': /* header */ + header = argv[i][0] == '+'; break; + case 'H': /* resolution string */ + resolution = argv[i][0] == '+'; + break; case 'u': /* unique values */ - uniq = 1; + uniq = argv[i][0] == '-'; break; case 'o': /* original values */ - original = 1; + original = argv[i][0] == '-'; break; case 'g': /* gamma correction */ - gamma = atof(argv[++i]); + gamcor = atof(argv[i+1]); + if (argv[i][0] == '+') + gamcor = 1.0/gamcor; + i++; break; + case 'e': /* exposure correction */ + d = atof(argv[i+1]); + if (argv[i+1][0] == '-' || argv[i+1][0] == '+') + d = pow(2.0, d); + if (argv[i][0] == '-') + doexposure = 100; + scalecolor(exposure, d); + expval *= d; + doexposure++; + i++; + break; + case 'R': /* reverse byte sequence */ + if (argv[i][0] == '-') { + ord[0]=BLU; ord[1]=GRN; ord[2]=RED; + } else { + ord[0]=RED; ord[1]=GRN; ord[2]=BLU; + } + break; case 'r': /* reverse conversion */ - reverse = 1; + reverse = argv[i][0] == '-'; break; case 'b': /* brightness values */ - brightonly = 1; + brightonly = argv[i][0] == '-'; break; case 'd': /* data only (no indices) */ - dataonly = 1; + dataonly = argv[i][0] == '-'; switch (argv[i][2]) { case '\0': case 'a': /* ascii */ @@ -92,14 +123,17 @@ char **argv; fmtid = "ascii"; break; case 'b': /* byte */ + dataonly = 1; format = 'b'; fmtid = "byte"; break; case 'f': /* float */ + dataonly = 1; format = 'f'; fmtid = "float"; break; case 'd': /* double */ + dataonly = 1; format = 'd'; fmtid = "double"; break; @@ -108,10 +142,20 @@ char **argv; } break; case 'x': /* x resolution */ - xres = atoi(argv[++i]); + case 'X': /* x resolution */ + resolution = 0; + if (argv[i][0] == '-') + picres.or |= XDECR; + picres.xr = atoi(argv[++i]); break; case 'y': /* y resolution */ - yres = atoi(argv[++i]); + case 'Y': /* y resolution */ + resolution = 0; + if (argv[i][0] == '-') + picres.or |= YDECR; + if (picres.xr == 0) + picres.or |= YMAJOR; + picres.yr = atoi(argv[++i]); break; default: unkopt: @@ -128,7 +172,11 @@ unkopt: fmtid = "8-bit_grey"; else fmtid = "24-bit_rgb"; - + /* assign reverse ordering */ + rord[ord[0]] = 0; + rord[ord[1]] = 1; + rord[ord[2]] = 2; + /* get input */ if (i == argc) { fin = stdin; } else if (i == argc-1) { @@ -145,23 +193,36 @@ unkopt: set_io(); if (reverse) { - if (yres <= 0 || xres <= 0) { - fprintf(stderr, "%s: missing x and y resolution\n", - progname); - quit(1); - } +#ifdef MSDOS + setmode(fileno(stdout), O_BINARY); + if (format != 'a' && format != 'i') + setmode(fileno(fin), O_BINARY); +#endif /* get header */ if (header && checkheader(fin, fmtid, stdout) < 0) { fprintf(stderr, "%s: wrong input format\n", progname); quit(1); } + /* get resolution */ + if ((resolution && !fgetsresolu(&picres, fin)) || + picres.xr <= 0 || picres.yr <= 0) { + fprintf(stderr, "%s: missing resolution\n", progname); + quit(1); + } /* add to header */ printargs(i, argv, stdout); + if (doexposure > 100) + fputexpos(expval, stdout); fputformat(COLRFMT, stdout); - printf("\n"); - fputresolu(YMAJOR|YDECR, xres, yres, stdout); + putchar('\n'); + fputsresolu(&picres, stdout); /* always put resolution */ valtopix(); } else { +#ifdef MSDOS + setmode(fileno(fin), O_BINARY); + if (format != 'a' && format != 'i') + setmode(fileno(stdout), O_BINARY); +#endif /* get header */ getheader(fin, checkhead, NULL); if (wrongformat) { @@ -169,19 +230,19 @@ unkopt: progname); quit(1); } - - if (xres <= 0 || yres <= 0) /* get picture size */ - if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { - fprintf(stderr, - "%s: missing x and y resolution\n", - progname); - quit(1); - } + if (!fgetsresolu(&picres, fin)) { + fprintf(stderr, "%s: missing resolution\n", progname); + quit(1); + } if (header) { printargs(i, argv, stdout); + if (doexposure > 100) + fputexpos(expval, stdout); fputformat(fmtid, stdout); - printf("\n"); + putchar('\n'); } + if (resolution) /* put resolution */ + fputsresolu(&picres, stdout); pixtoval(); } @@ -202,11 +263,13 @@ char *line; } else if (original && isexpos(line)) { d = 1.0/exposval(line); scalecolor(exposure, d); + doexposure++; } else 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); } @@ -214,25 +277,26 @@ char *line; pixtoval() /* convert picture to values */ { - register COLOR *scanln; + register COLOR *scanln; int dogamma; COLOR lastc; + FLOAT hv[2]; int y; register int x; - scanln = (COLOR *)malloc(xres*sizeof(COLOR)); + scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); if (scanln == NULL) { fprintf(stderr, "%s: out of memory\n", progname); quit(1); } - dogamma = gamma < .95 || gamma > 1.05; + dogamma = gamcor < .95 || gamcor > 1.05; setcolor(lastc, 0.0, 0.0, 0.0); - for (y = yres-1; y >= 0; y--) { - if (freadscan(scanln, xres, fin) < 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 < xres; x++) { + for (x = 0; x < scanlen(&picres); x++) { if (uniq) if ( colval(scanln[x],RED) == colval(lastc,RED) && @@ -243,15 +307,18 @@ pixtoval() /* convert picture to values */ continue; else copycolor(lastc, scanln[x]); - if (original) + if (doexposure) multcolor(scanln[x], exposure); if (dogamma) setcolor(scanln[x], - pow(colval(scanln[x],RED), 1.0/gamma), - pow(colval(scanln[x],GRN), 1.0/gamma), - pow(colval(scanln[x],BLU), 1.0/gamma)); - if (!dataonly) - printf("%7d %7d ", x, y); + 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], stdout) < 0) { fprintf(stderr, "%s: write error\n", progname); quit(1); @@ -265,18 +332,18 @@ pixtoval() /* convert picture to values */ valtopix() /* convert values to a pixel file */ { int dogamma; - register COLOR *scanln; + register COLOR *scanln; int y; register int x; - scanln = (COLOR *)malloc(xres*sizeof(COLOR)); + scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); if (scanln == NULL) { fprintf(stderr, "%s: out of memory\n", progname); quit(1); } - dogamma = gamma < .95 || gamma > 1.05; - for (y = yres-1; y >= 0; y--) { - for (x = 0; x < xres; x++) { + dogamma = gamcor < .95 || gamcor > 1.05; + for (y = 0; y < numscans(&picres); y++) { + for (x = 0; x < scanlen(&picres); x++) { if (!dataonly) fscanf(fin, "%*d %*d"); if ((*getval)(scanln[x], fin) < 0) { @@ -285,11 +352,13 @@ valtopix() /* convert values to a pixel file */ } if (dogamma) setcolor(scanln[x], - pow(colval(scanln[x],RED), gamma), - pow(colval(scanln[x],GRN), gamma), - pow(colval(scanln[x],BLU), gamma)); + pow(colval(scanln[x],RED), gamcor), + pow(colval(scanln[x],GRN), gamcor), + pow(colval(scanln[x],BLU), gamcor)); + if (doexposure) + multcolor(scanln[x], exposure); } - if (fwritescan(scanln, xres, stdout) < 0) { + if (fwritescan(scanln, scanlen(&picres), stdout) < 0) { fprintf(stderr, "%s: write error\n", progname); quit(1); } @@ -309,11 +378,11 @@ getcascii(col, fp) /* get an ascii color value from f COLOR col; FILE *fp; { - double vd[3]; + double vd[3]; if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3) return(-1); - setcolor(col, vd[0], vd[1], vd[2]); + setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]); return(0); } @@ -322,11 +391,11 @@ getcdouble(col, fp) /* get a double color value from COLOR col; FILE *fp; { - double vd[3]; + double vd[3]; if (fread((char *)vd, sizeof(double), 3, fp) != 3) return(-1); - setcolor(col, vd[0], vd[1], vd[2]); + setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]); return(0); } @@ -339,7 +408,7 @@ FILE *fp; if (fread((char *)vf, sizeof(float), 3, fp) != 3) return(-1); - setcolor(col, vf[0], vf[1], vf[2]); + setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]); return(0); } @@ -352,7 +421,8 @@ FILE *fp; if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3) return(-1); - setcolor(col,(vi[0]+.5)/256.,(vi[1]+.5)/256.,(vi[2]+.5)/256.); + setcolor(col, (vi[rord[RED]]+.5)/256., + (vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.); return(0); } @@ -365,7 +435,8 @@ FILE *fp; if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3) return(-1); - setcolor(col,(vb[0]+.5)/256.,(vb[1]+.5)/256.,(vb[2]+.5)/256.); + setcolor(col, (vb[rord[RED]]+.5)/256., + (vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.); return(0); } @@ -374,7 +445,7 @@ getbascii(col, fp) /* get an ascii brightness value f COLOR col; FILE *fp; { - double vd; + double vd; if (fscanf(fp, "%lf", &vd) != 1) return(-1); @@ -387,7 +458,7 @@ getbdouble(col, fp) /* get a double brightness value COLOR col; FILE *fp; { - double vd; + double vd; if (fread((char *)&vd, sizeof(double), 1, fp) != 1) return(-1); @@ -414,7 +485,7 @@ COLOR col; FILE *fp; { int vi; - double d; + double d; if (fscanf(fp, "%d", &vi) != 1) return(-1); @@ -429,7 +500,7 @@ COLOR col; FILE *fp; { BYTE vb; - double d; + double d; if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1) return(-1); @@ -444,9 +515,9 @@ COLOR col; FILE *fp; { fprintf(fp, "%15.3e %15.3e %15.3e\n", - colval(col,RED), - colval(col,GRN), - colval(col,BLU)); + colval(col,ord[0]), + colval(col,ord[1]), + colval(col,ord[2])); return(ferror(fp) ? -1 : 0); } @@ -458,9 +529,9 @@ FILE *fp; { float vf[3]; - vf[0] = colval(col,RED); - vf[1] = colval(col,GRN); - vf[2] = colval(col,BLU); + vf[0] = colval(col,ord[0]); + vf[1] = colval(col,ord[1]); + vf[2] = colval(col,ord[2]); fwrite((char *)vf, sizeof(float), 3, fp); return(ferror(fp) ? -1 : 0); @@ -471,11 +542,11 @@ putcdouble(col, fp) /* put a double color to fp */ COLOR col; FILE *fp; { - double vd[3]; + double vd[3]; - vd[0] = colval(col,RED); - vd[1] = colval(col,GRN); - vd[2] = colval(col,BLU); + vd[0] = colval(col,ord[0]); + vd[1] = colval(col,ord[1]); + vd[2] = colval(col,ord[2]); fwrite((char *)vd, sizeof(double), 3, fp); return(ferror(fp) ? -1 : 0); @@ -487,9 +558,9 @@ COLOR col; FILE *fp; { fprintf(fp, "%d %d %d\n", - (int)(colval(col,RED)*256.), - (int)(colval(col,GRN)*256.), - (int)(colval(col,BLU)*256.)); + (int)(colval(col,ord[0])*256.), + (int)(colval(col,ord[1])*256.), + (int)(colval(col,ord[2])*256.)); return(ferror(fp) ? -1 : 0); } @@ -502,11 +573,11 @@ FILE *fp; register int i; BYTE vb[3]; - i = colval(col,RED)*256.; + i = colval(col,ord[0])*256.; vb[0] = min(i,255); - i = colval(col,GRN)*256.; + i = colval(col,ord[1])*256.; vb[1] = min(i,255); - i = colval(col,BLU)*256.; + i = colval(col,ord[2])*256.; vb[2] = min(i,255); fwrite((char *)vb, sizeof(BYTE), 3, fp); @@ -541,7 +612,7 @@ putbdouble(col, fp) /* put a double brightness to fp COLOR col; FILE *fp; { - double vd; + double vd; vd = bright(col); fwrite((char *)&vd, sizeof(double), 1, fp);