--- ray/src/px/pvalue.c 1989/02/02 10:49:31 1.1 +++ ray/src/px/pvalue.c 1991/05/10 08:51:53 1.7 @@ -1,4 +1,4 @@ -/* Copyright (c) 1986 Regents of the University of California */ +/* Copyright (c) 1991 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -19,9 +19,9 @@ static char SCCSid[] = "$SunId$ LBL"; int xres = 0; /* resolution of input */ int yres = 0; -int uniq = 0; /* unique values? */ +int uniq = 0; /* print only unique values? */ -int original = 0; /* original values? */ +int original = 0; /* convert back to original values? */ int dataonly = 0; /* data only format? */ @@ -30,15 +30,22 @@ int brightonly = 0; /* only brightness values? */ int reverse = 0; /* reverse conversion? */ int format = 'a'; /* input/output format */ +char *fmtid = "ascii"; /* format identifier for header */ int header = 1; /* do header */ -double exposure = 1.0; +int wrongformat = 0; /* wrong input format? */ +double gamma = 1.0; /* gamma correction */ + +COLOR exposure = WHTCOLOR; + char *progname; FILE *fin; +extern double atof(), pow(); + int (*getval)(), (*putval)(); @@ -46,7 +53,6 @@ main(argc, argv) int argc; char **argv; { - extern double atof(); extern int checkhead(); int i; @@ -64,6 +70,9 @@ char **argv; case 'o': /* original values */ original = 1; break; + case 'g': /* gamma correction */ + gamma = atof(argv[++i]); + break; case 'r': /* reverse conversion */ reverse = 1; break; @@ -76,12 +85,23 @@ char **argv; case '\0': case 'a': /* ascii */ format = 'a'; + fmtid = "ascii"; break; case 'i': /* integer */ + format = 'i'; + fmtid = "ascii"; + break; case 'b': /* byte */ + format = 'b'; + fmtid = "byte"; + break; case 'f': /* float */ + format = 'f'; + fmtid = "float"; + break; case 'd': /* double */ - format = argv[i][2]; + format = 'd'; + fmtid = "double"; break; default: goto unkopt; @@ -102,7 +122,13 @@ unkopt: } else break; - + /* recognize special formats */ + if (dataonly && format == 'b') + if (brightonly) + fmtid = "8-bit_grey"; + else + fmtid = "24-bit_rgb"; + if (i == argc) { fin = stdin; } else if (i == argc-1) { @@ -119,24 +145,33 @@ unkopt: set_io(); if (reverse) { - if (header) /* get header */ - copyheader(fin, stdout); - /* add to header */ - printargs(i, argv, stdout); - printf("\n"); if (yres <= 0 || xres <= 0) { fprintf(stderr, "%s: missing x and y resolution\n", progname); quit(1); } - printf("-Y %d +X %d\n", yres, xres); /* resolution */ + /* get header */ + if (header && checkheader(fin, fmtid, stdout) < 0) { + fprintf(stderr, "%s: wrong input format\n", progname); + quit(1); + } + /* add to header */ + printargs(i, argv, stdout); + fputformat(COLRFMT, stdout); + printf("\n"); + fputresolu(YMAJOR|YDECR, xres, yres, stdout); valtopix(); } else { /* get header */ - getheader(fin, checkhead); + getheader(fin, checkhead, NULL); + if (wrongformat) { + fprintf(stderr, "%s: input not a Radiance picture\n", + progname); + quit(1); + } if (xres <= 0 || yres <= 0) /* get picture size */ - if (fscanf(fin, "-Y %d +X %d\n", &yres, &xres) != 2) { + if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { fprintf(stderr, "%s: missing x and y resolution\n", progname); @@ -144,6 +179,7 @@ unkopt: } if (header) { printargs(i, argv, stdout); + fputformat(fmtid, stdout); printf("\n"); } pixtoval(); @@ -156,16 +192,30 @@ unkopt: checkhead(line) /* deal with line from header */ char *line; { - if (header) + char fmt[32]; + double d; + COLOR ctmp; + + if (isformat(line)) { + formatval(fmt, line); + wrongformat = strcmp(fmt, COLRFMT); + } else if (original && isexpos(line)) { + d = 1.0/exposval(line); + scalecolor(exposure, d); + } 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)); + } else if (header) fputs(line, stdout); - if (!strncmp(line, "EXPOSURE=", 9)) - exposure *= atof(line+9); } pixtoval() /* convert picture to values */ { - COLOR *scanln; + register COLOR *scanln; + int dogamma; COLOR lastc; int y; register int x; @@ -175,6 +225,7 @@ pixtoval() /* convert picture to values */ fprintf(stderr, "%s: out of memory\n", progname); quit(1); } + dogamma = gamma < .95 || gamma > 1.05; setcolor(lastc, 0.0, 0.0, 0.0); for (y = yres-1; y >= 0; y--) { if (freadscan(scanln, xres, fin) < 0) { @@ -183,14 +234,22 @@ pixtoval() /* convert picture to values */ } for (x = 0; x < xres; x++) { if (uniq) - if ( scanln[x][RED] == lastc[RED] && - scanln[x][GRN] == lastc[GRN] && - scanln[x][BLU] == lastc[BLU] ) + 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 (original) - scalecolor(scanln[x], 1.0/exposure); + 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); if ((*putval)(scanln[x], stdout) < 0) { @@ -205,7 +264,8 @@ pixtoval() /* convert picture to values */ valtopix() /* convert values to a pixel file */ { - COLOR *scanln; + int dogamma; + register COLOR *scanln; int y; register int x; @@ -214,6 +274,7 @@ valtopix() /* convert values to a pixel file */ 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++) { if (!dataonly) @@ -222,9 +283,13 @@ valtopix() /* convert values to a pixel file */ fprintf(stderr, "%s: read error\n", progname); quit(1); } + if (dogamma) + setcolor(scanln[x], + pow(colval(scanln[x],RED), gamma), + pow(colval(scanln[x],GRN), gamma), + pow(colval(scanln[x],BLU), gamma)); } - if (fwritescan(scanln, xres, stdout) < 0 - || fflush(stdout) < 0) { + if (fwritescan(scanln, xres, stdout) < 0) { fprintf(stderr, "%s: write error\n", progname); quit(1); } @@ -259,7 +324,7 @@ FILE *fp; { double vd[3]; - if (fread(vd, sizeof(double), 3, fp) != 3) + if (fread((char *)vd, sizeof(double), 3, fp) != 3) return(-1); setcolor(col, vd[0], vd[1], vd[2]); return(0); @@ -272,7 +337,7 @@ FILE *fp; { float vf[3]; - if (fread(vf, sizeof(float), 3, fp) != 3) + if (fread((char *)vf, sizeof(float), 3, fp) != 3) return(-1); setcolor(col, vf[0], vf[1], vf[2]); return(0); @@ -298,7 +363,7 @@ FILE *fp; { BYTE vb[3]; - if (fread(vb, sizeof(BYTE), 3, fp) != 3) + 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.); return(0); @@ -324,7 +389,7 @@ FILE *fp; { double vd; - if (fread(&vd, sizeof(double), 1, fp) != 1) + if (fread((char *)&vd, sizeof(double), 1, fp) != 1) return(-1); setcolor(col, vd, vd, vd); return(0); @@ -337,7 +402,7 @@ FILE *fp; { float vf; - if (fread(&vf, sizeof(float), 1, fp) != 1) + if (fread((char *)&vf, sizeof(float), 1, fp) != 1) return(-1); setcolor(col, vf, vf, vf); return(0); @@ -366,7 +431,7 @@ FILE *fp; BYTE vb; double d; - if (fread(&vb, sizeof(BYTE), 1, fp) != 1) + if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1) return(-1); d = (vb+.5)/256.; setcolor(col, d, d, d); @@ -396,7 +461,7 @@ FILE *fp; vf[0] = colval(col,RED); vf[1] = colval(col,GRN); vf[2] = colval(col,BLU); - fwrite(vf, sizeof(float), 3, fp); + fwrite((char *)vf, sizeof(float), 3, fp); return(ferror(fp) ? -1 : 0); } @@ -411,7 +476,7 @@ FILE *fp; vd[0] = colval(col,RED); vd[1] = colval(col,GRN); vd[2] = colval(col,BLU); - fwrite(vd, sizeof(double), 3, fp); + fwrite((char *)vd, sizeof(double), 3, fp); return(ferror(fp) ? -1 : 0); } @@ -443,7 +508,7 @@ FILE *fp; vb[1] = min(i,255); i = colval(col,BLU)*256.; vb[2] = min(i,255); - fwrite(vb, sizeof(BYTE), 3, fp); + fwrite((char *)vb, sizeof(BYTE), 3, fp); return(ferror(fp) ? -1 : 0); } @@ -466,7 +531,7 @@ FILE *fp; float vf; vf = bright(col); - fwrite(&vf, sizeof(float), 1, fp); + fwrite((char *)&vf, sizeof(float), 1, fp); return(ferror(fp) ? -1 : 0); } @@ -479,7 +544,7 @@ FILE *fp; double vd; vd = bright(col); - fwrite(&vd, sizeof(double), 1, fp); + fwrite((char *)&vd, sizeof(double), 1, fp); return(ferror(fp) ? -1 : 0); } @@ -504,7 +569,7 @@ FILE *fp; i = bright(col)*256.; vb = min(i,255); - fwrite(&vb, sizeof(BYTE), 1, fp); + fwrite((char *)&vb, sizeof(BYTE), 1, fp); return(ferror(fp) ? -1 : 0); }