--- ray/src/px/pcond3.c 1997/01/11 10:39:25 3.7 +++ ray/src/px/pcond3.c 1998/03/12 15:47:34 3.10 @@ -13,11 +13,217 @@ static char SCCSid[] = "$SunId$ LBL"; #define CVRATIO 0.025 /* fraction of samples allowed > env. */ -#define exp10(x) exp(2.302585093*(x)) +#define LN_10 2.30258509299404568402 +#define exp10(x) exp(LN_10*(x)) float modhist[HISTRES]; /* modified histogram */ double mhistot; /* modified histogram total */ float cumf[HISTRES+1]; /* cumulative distribution function */ + + +getfixations(fp) /* load fixation history list */ +FILE *fp; +{ +#define FIXHUNK 128 + RESOLU fvres; + int pos[2]; + register int px, py, i; + /* initialize our resolution struct */ + if ((fvres.or=inpres.or)&YMAJOR) { + fvres.xr = fvxr; + fvres.yr = fvyr; + } else { + fvres.xr = fvyr; + fvres.yr = fvxr; + } + /* read each picture position */ + while (fscanf(fp, "%d %d", &pos[0], &pos[1]) == 2) { + /* convert to closest index in foveal image */ + loc2pix(pos, &fvres, + (pos[0]+.5)/inpres.xr, (pos[1]+.5)/inpres.yr); + /* include nine neighborhood samples */ + for (px = pos[0]-1; px <= pos[0]+1; px++) { + if (px < 0 || px >= fvxr) + continue; + for (py = pos[1]-1; py <= pos[1]+1; py++) { + if (py < 0 || py >= fvyr) + continue; + for (i = nfixations; i-- > 0; ) + if (fixlst[i][0] == px && + fixlst[i][1] == py) + break; + if (i >= 0) + continue; /* already there */ + if (nfixations % FIXHUNK == 0) { + if (nfixations) + fixlst = (short (*)[2]) + realloc((char *)fixlst, + (nfixations+FIXHUNK)* + 2*sizeof(short)); + else + fixlst = (short (*)[2])malloc( + FIXHUNK*2*sizeof(short) + ); + if (fixlst == NULL) + syserror("malloc"); + } + fixlst[nfixations][0] = px; + fixlst[nfixations][1] = py; + nfixations++; + } + } + } + if (!feof(fp)) { + fprintf(stderr, "%s: format error reading fixation data\n", + progname); + exit(1); + } +#undef FIXHUNK +} + + +gethisto(fp) /* load precomputed luminance histogram */ +FILE *fp; +{ + float histo[MAXPREHIST]; + double histart, histep; + double l, b, lastb, w; + int n; + register int i; + /* load data */ + for (i = 0; i < MAXPREHIST && + fscanf(fp, "%lf %f", &b, &histo[i]) == 2; i++) { + if (i > 1 && fabs(b - lastb - histep) > 1e-3) { + fprintf(stderr, + "%s: uneven step size in histogram data\n", + progname); + exit(1); + } + if (i == 1) + histep = b - (histart = lastb); + lastb = b; + } + if (i < 2 || !feof(fp)) { + fprintf(stderr, + "%s: format/length error loading histogram (log10L %f at %d)\n", + progname, b, i); + exit(1); + } + n = i; + histart *= LN_10; + histep *= LN_10; + /* find extrema */ + for (i = 0; i < n && histo[i] <= FTINY; i++) + ; + bwmin = histart + i*histep; + for (i = n; i-- && histo[i] <= FTINY; ) + ; + bwmax = histart + i*histep; + if (bwmax > Bl(LMAX)) + bwmax = Bl(LMAX); + if (bwmin < Bl(LMIN)) + bwmin = Bl(LMIN); + else /* duplicate bottom bin */ + bwmin = bwmax - (bwmax-bwmin)*HISTRES/(HISTRES-1); + /* convert histogram */ + bwavg = 0.; histot = 0.; + for (i = 0; i < HISTRES; i++) + bwhist[i] = 0.; + for (i = 0, b = histart; i < n; i++, b += histep) { + if (b < bwmin) continue; + if (b > bwmax) break; + w = histo[i]; + bwavg += w*b; + bwhist[bwhi(b)] += w; + histot += w; + } + bwavg /= histot; + if (bwmin > Bl(LMIN)+FTINY) { /* add false samples at bottom */ + bwhist[1] *= 0.5; + bwhist[0] += bwhist[1]; + } +} + + +double +centprob(x, y) /* center-weighting probability function */ +int x, y; +{ + 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); +} + + +comphist() /* create foveal sampling histogram */ +{ + double l, b, w, lwmin, lwmax; + register int x, y; + /* check for precalculated histogram */ + if (what2do&DO_PREHIST) + return; + lwmin = 1e10; /* find extrema */ + lwmax = 0.; + for (y = 0; y < fvyr; y++) + for (x = 0; x < fvxr; x++) { + l = plum(fovscan(y)[x]); + if (l < lwmin) lwmin = l; + if (l > lwmax) lwmax = l; + } + lwmax *= 1.01; + if (lwmax > LMAX) + lwmax = LMAX; + bwmax = Bl(lwmax); + if (lwmin < LMIN) { + lwmin = LMIN; + bwmin = Bl(LMIN); + } else { /* duplicate bottom bin */ + bwmin = bwmax - (bwmax-Bl(lwmin))*HISTRES/(HISTRES-1); + lwmin = Lb(bwmin); + } + /* (re)compute histogram */ + bwavg = 0.; + histot = 0.; + for (x = 0; x < HISTRES; x++) + bwhist[x] = 0.; + /* global average */ + if (!(what2do&DO_FIXHIST) || fixfrac < 1.-FTINY) + for (y = 0; y < fvyr; y++) + for (x = 0; x < fvxr; x++) { + l = plum(fovscan(y)[x]); + if (l < lwmin) continue; + if (l > lwmax) continue; + b = Bl(l); + w = what2do&DO_CWEIGHT ? centprob(x,y) : 1.; + bwavg += w*b; + bwhist[bwhi(b)] += w; + histot += w; + } + /* average fixation points */ + if (what2do&DO_FIXHIST && nfixations > 0) { + if (histot > FTINY) + w = fixfrac/(1.-fixfrac)*histot/nfixations; + else + w = 1.; + for (x = 0; x < nfixations; x++) { + l = plum(fovscan(fixlst[x][1])[fixlst[x][0]]); + if (l < lwmin) continue; + if (l > lwmax) continue; + b = Bl(l); + bwavg += w*b; + bwhist[bwhi(b)] += w; + histot += w; + } + } + bwavg /= histot; + if (lwmin > LMIN+FTINY) { /* add false samples at bottom */ + bwhist[1] *= 0.5; + bwhist[0] += bwhist[1]; + } +} mkcumf() /* make cumulative distribution function */