--- ray/src/px/pvalue.c 1990/12/14 16:33:19 1.5 +++ ray/src/px/pvalue.c 1991/11/12 16:04:16 2.1 @@ -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"; @@ -10,18 +10,19 @@ static char SCCSid[] = "$SunId$ LBL"; * 4/23/86 */ -#include +#include "standard.h" #include "color.h" +#include "resolu.h" + #define min(a,b) ((a)<(b)?(a):(b)) -int xres = 0; /* resolution of input */ -int yres = 0; +RESOLU picres; /* resolution of picture */ -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 +31,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 */ +int wrongformat = 0; /* wrong input format? */ + +double gamcor = 1.0; /* gamma correction */ + COLOR exposure = WHTCOLOR; char *progname; FILE *fin; +extern double atof(), pow(); + int (*getval)(), (*putval)(); @@ -46,52 +54,76 @@ main(argc, argv) int argc; char **argv; { - extern double atof(); extern int checkhead(); 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 '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 */ + gamcor = atof(argv[++i]); + if (argv[i][0] == '+') + gamcor = 1.0/gamcor; + 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 */ format = 'a'; + fmtid = "ascii"; break; case 'i': /* integer */ + format = 'i'; + 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 */ - format = argv[i][2]; + dataonly = 1; + format = 'd'; + fmtid = "double"; break; default: goto unkopt; } break; case 'x': /* x resolution */ - xres = atoi(argv[++i]); + if (argv[i][0] == '-') + picres.or |= XDECR; + picres.xr = atoi(argv[++i]); break; case 'y': /* y resolution */ - yres = atoi(argv[++i]); + if (argv[i][0] == '-') + picres.or |= YDECR; + if (picres.xr == 0) + picres.or |= YMAJOR; + picres.yr = atoi(argv[++i]); break; default: unkopt: @@ -102,7 +134,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,32 +157,42 @@ 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); + /* get header */ + if (header && checkheader(fin, fmtid, stdout) < 0) { + fprintf(stderr, "%s: wrong input format\n", progname); quit(1); } - fputresolu(YMAJOR|YDECR, xres, yres, stdout); + if (picres.xr <= 0 || picres.yr <= 0) /* get resolution */ + if (!fgetsresolu(&picres, fin)) { + fprintf(stderr, "%s: missing resolution\n", + progname); + quit(1); + } + /* add to header */ + printargs(i, argv, stdout); + fputformat(COLRFMT, stdout); + putchar('\n'); + fputsresolu(&picres, 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 (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { - fprintf(stderr, - "%s: missing x and y resolution\n", + if (picres.xr <= 0 || picres.yr <= 0) /* get picture size */ + if (!fgetsresolu(&picres, fin)) { + fprintf(stderr, "%s: missing resolution\n", progname); quit(1); } if (header) { printargs(i, argv, stdout); - printf("\n"); + fputformat(fmtid, stdout); + putchar('\n'); } pixtoval(); } @@ -156,53 +204,70 @@ unkopt: checkhead(line) /* deal with line from header */ char *line; { + char fmt[32]; double d; COLOR ctmp; - if (header) - fputs(line, stdout); - if (isexpos(line)) { + 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 (iscolcor(line)) { + } else if (original && iscolcor(line)) { colcorval(ctmp, line); - colval(exposure,RED) /= colval(ctmp,RED); - colval(exposure,GRN) /= colval(ctmp,GRN); - colval(exposure,BLU) /= colval(ctmp,BLU); - } + 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); } pixtoval() /* convert picture to values */ { - 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 = 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 ( 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) multcolor(scanln[x], exposure); - if (!dataonly) - printf("%7d %7d ", x, y); + 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], stdout) < 0) { fprintf(stderr, "%s: write error\n", progname); quit(1); @@ -215,26 +280,32 @@ pixtoval() /* convert picture to values */ valtopix() /* convert values to a pixel file */ { - COLOR *scanln; + int dogamma; + 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); } - 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) { fprintf(stderr, "%s: read error\n", progname); quit(1); } + if (dogamma) + setcolor(scanln[x], + pow(colval(scanln[x],RED), gamcor), + pow(colval(scanln[x],GRN), gamcor), + pow(colval(scanln[x],BLU), gamcor)); } - if (fwritescan(scanln, xres, stdout) < 0 - || fflush(stdout) < 0) { + if (fwritescan(scanln, scanlen(&picres), stdout) < 0) { fprintf(stderr, "%s: write error\n", progname); quit(1); }