--- ray/src/px/pcond.c 1996/10/05 11:16:51 3.3 +++ ray/src/px/pcond.c 1997/01/09 13:56:23 3.8 @@ -1,4 +1,4 @@ -/* Copyright (c) 1996 Regents of the University of California */ +/* Copyright (c) 1997 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -10,9 +10,7 @@ static char SCCSid[] = "$SunId$ LBL"; #include "pcond.h" -#include "random.h" - #define LDMAX 100 /* default max. display luminance */ #define LDMINF 0.01 /* default min. display lum. factor */ @@ -26,6 +24,7 @@ char *progname; /* global argv[0] */ char *infn; /* input file name */ FILE *infp; /* input stream */ +FILE *mapfp = NULL; /* tone-mapping function stream */ VIEW ourview = STDVIEW; /* picture view */ int gotview = 0; /* picture has view */ double pixaspect = 1.0; /* pixel aspect ratio */ @@ -33,8 +32,8 @@ RESOLU inpres; /* input picture resolution */ COLOR *fovimg; /* foveal (1 degree) averaged image */ short fvxr, fvyr; /* foveal image resolution */ -int bwhist[HISTRES]; /* luminance histogram */ -long histot; /* total count of histogram */ +float bwhist[HISTRES]; /* luminance histogram */ +double histot; /* total count of histogram */ double bwmin, bwmax; /* histogram limits */ double bwavg; /* mean brightness */ @@ -113,6 +112,15 @@ char *argv[]; if (i+1 >= argc) goto userr; ldmin = atof(argv[++i]); break; + case 'm': + if (i+1 >= argc) goto userr; + if ((mapfp = fopen(argv[++i], "w")) == NULL) { + fprintf(stderr, + "%s: cannot open for writing\n", + argv[i]); + exit(1); + } + break; default: goto userr; } @@ -147,9 +155,11 @@ char *argv[]; if (outprims != inprims) fputprims(outprims, stdout); mapimage(); /* map the picture */ + if (mapfp != NULL) /* write out basic mapping */ + putmapping(mapfp); exit(0); userr: - fprintf(stderr, "Usage: %s [-{h|a|v|s|c|l|w}[+-]][-e ev][-p xr yr xg yg xb yb xw yw|-f mbf.cal][-t Ldmax][-b Ldmin] inpic [outpic]\n", + fprintf(stderr, "Usage: %s [-{h|a|v|s|c|l|w}[+-]][-e ev][-p xr yr xg yg xb yb xw yw|-f mbf.cal][-t Ldmax][-b Ldmin][-m mapfile] inpic [outpic]\n", progname); exit(1); #undef bool @@ -239,7 +249,8 @@ mapimage() /* map picture and send to stdout */ #ifdef DEBUG fprintf(stderr, "%s: generating histogram...", progname); #endif - fovhist(); /* generate adaptation histogram */ + getfovimg(); /* get foveal sample image */ + comphist(); /* generate adaptation histogram */ #ifdef DEBUG fputs("done\n", stderr); #endif @@ -278,8 +289,7 @@ mapimage() /* map picture and send to stdout */ fprintf(stderr, "%s: linear scaling factor = %f\n", progname, scalef); #endif - if (scalef < 0.99 | scalef > 1.01) - fputexpos(scalef, stdout); /* write in header */ + fputexpos(inpexp*scalef, stdout); /* record exposure */ if (lumf == cielum) scalef /= WHTEFFICACY; } putchar('\n'); /* complete header */ @@ -298,22 +308,22 @@ double centprob(x, y) /* center-weighting probability function */ int x, y; { - double xr, yr; - - xr = (x+.5)/fvxr - .5; - yr = (y+.5)/fvyr - .5; - return(1. - xr*xr - yr*yr); /* radial, == 0.5 at corners */ + double xr, yr, p; + /* paraboloid, 0 at 90 degrees from center */ + xr = (x - .5*(fvxr-1))/90.; /* 180 degree fisheye has fv?r == 90 */ + yr = (y - .5*(fvyr-1))/90.; + p = 1. - xr*xr - yr*yr; + return(p < 0. ? 0. : p); } -fovhist() /* create foveal sampled image and histogram */ +getfovimg() /* load foveal sampled image */ { extern FILE *popen(); char combuf[128]; - double l, b, lwmin, lwmax; FILE *fp; int x, y; - + /* compute image size */ fvxr = sqrt(ourview.hn2)/FOVDIA + 0.5; if (fvxr < 2) fvxr = 2; fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5; @@ -335,6 +345,19 @@ fovhist() /* create foveal sampled image and histogr if (freadscan(fovscan(y), fvxr, fp) < 0) goto readerr; pclose(fp); + return; +readerr: + fprintf(stderr, "%s: error reading from pfilt process in fovimage\n", + progname); + exit(1); +} + + +comphist() /* create foveal sampled image and histogram */ +{ + double l, b, lwmin, lwmax; + register int x, y; + lwmin = 1e10; /* find extrema */ lwmax = 0.; for (y = 0; y < fvyr; y++) @@ -343,37 +366,32 @@ fovhist() /* create foveal sampled image and histogr if (l < lwmin) lwmin = l; if (l > lwmax) lwmax = l; } + lwmin -= FTINY; + lwmax += FTINY; if (lwmin < LMIN) lwmin = LMIN; if (lwmax > LMAX) lwmax = LMAX; /* compute histogram */ - bwmin = Bl(lwmin)*(1. - .01/HISTRES); - bwmax = Bl(lwmax)*(1. + .01/HISTRES); + bwmin = Bl(lwmin); + bwmax = Bl(lwmax); bwavg = 0.; for (y = 0; y < fvyr; y++) for (x = 0; x < fvxr; x++) { - if (what2do & DO_CWEIGHT && - frandom() > centprob(x,y)) - continue; l = plum(fovscan(y)[x]); + if (l < lwmin) continue; + if (l > lwmax) continue; b = Bl(l); - if (b < bwmin) continue; - if (b > bwmax) continue; bwavg += b; - bwhc(b)++; - histot++; + l = what2do&DO_CWEIGHT ? centprob(x,y) : 1.; + bwhist[bwhi(b)] += l; + histot += l; } - bwavg /= (double)histot; - return; -readerr: - fprintf(stderr, "%s: error reading from pfilt process in fovimage\n", - progname); - exit(1); + bwavg /= histot; } check2do() /* check histogram to see what isn't worth doing */ { - long sum; + double sum; double b, l; register int i; @@ -388,7 +406,7 @@ check2do() /* check histogram to see what isn't worth if (!(what2do & (DO_ACUITY|DO_COLOR))) return; /* find 5th percentile */ - sum = histot*0.05 + .5; + sum = histot*0.05; for (i = 0; i < HISTRES; i++) if ((sum -= bwhist[i]) <= 0) break; @@ -397,9 +415,9 @@ check2do() /* check histogram to see what isn't worth /* determine if acuity adj. useful */ if (what2do&DO_ACUITY && hacuity(l) >= (inpres.xr/sqrt(ourview.hn2) + - inpres.yr/sqrt(ourview.vn2))/(2.*180./PI*2.)) + inpres.yr/sqrt(ourview.vn2))/(2.*180./PI)) what2do &= ~DO_ACUITY; /* color sensitivity loss? */ - if (l >= 6.0) + if (l >= TopMesopic) what2do &= ~DO_COLOR; }