--- ray/src/px/pcond4.c 1996/10/04 16:10:43 3.3 +++ ray/src/px/pcond4.c 1997/01/09 13:56:32 3.11 @@ -14,13 +14,8 @@ static char SCCSid[] = "$SunId$ LBL"; #define VADAPT 0.08 /* fraction of adaptation from veil */ -extern COLOR *fovimg; /* foveal (1 degree) averaged image */ -extern short fvxr, fvyr; /* foveal image resolution */ +static COLOR *veilimg = NULL; /* veiling image */ -#define fovscan(y) (fovimg+(y)*fvxr) - -static COLOR *veilimg; /* veiling image */ - #define veilscan(y) (veilimg+(y)*fvxr) static float (*raydir)[3] = NULL; /* ray direction for each pixel */ @@ -83,6 +78,9 @@ compveil() /* compute veiling image */ COLOR ctmp, vsum; int px, py; register int x, y; + + if (veilimg != NULL) /* already done? */ + return; /* compute ray directions */ compraydir(); /* compute veil image */ @@ -99,8 +97,11 @@ compveil() /* compute veiling image */ t2 = DOT(rdirscan(py)[px], rdirscan(y)[x]); if (t2 <= FTINY) continue; + /* use approximation instead t2 = acos(t2); t2 = 1./(t2*t2); + */ + t2 = .5 / (1. - t2); copycolor(ctmp, fovscan(y)[x]); scalecolor(ctmp, t2); addcolor(vsum, ctmp); @@ -110,6 +111,13 @@ compveil() /* compute veiling image */ scalecolor(vsum, VADAPT/t2sum); copycolor(veilscan(py)[px], vsum); } + /* modify FOV sample image */ + for (y = 0; y < fvyr; y++) + for (x = 0; x < fvxr; x++) { + scalecolor(fovscan(y)[x], 1.-VADAPT); + addcolor(fovscan(y)[x], veilscan(y)[x]); + } + comphist(); /* recompute histogram */ } @@ -123,11 +131,11 @@ int y; register int x, i; vy = dy = (y+.5)/numscans(&inpres)*fvyr - .5; - if (vy >= fvyr-1) vy--; + while (vy >= fvyr-1) vy--; dy -= (double)vy; for (x = 0; x < scanlen(&inpres); x++) { vx = dx = (x+.5)/scanlen(&inpres)*fvxr - .5; - if (vx >= fvxr-1) vx--; + while (vx >= fvxr-1) vx--; dx -= (double)vx; for (i = 0; i < 3; i++) { lv = (1.-dy)*colval(veilscan(vy)[vx],i) + @@ -143,16 +151,15 @@ int y; /****************** ACUITY STUFF *******************/ -typedef struct scanbar { - short sampr; /* sample area size (power of 2) */ +typedef struct { + short sampe; /* sample area size (exponent of 2) */ short nscans; /* number of scanlines in this bar */ int len; /* individual scanline length */ - struct scanbar *next; /* next higher resolution scanbar */ int nread; /* number of scanlines loaded */ - /* followed by the scanline data */ + COLOR *sdata; /* scanbar data */ } SCANBAR; -#define bscan(sb,y) ((COLOR *)((sb)+1)+((y)%(sb)->nscans)*(sb)->len) +#define bscan(sb,y) ((COLOR *)(sb)->sdata+((y)%(sb)->nscans)*(sb)->len) SCANBAR *rootbar; /* root scan bar (lowest resolution) */ @@ -177,7 +184,12 @@ double La; }; double l10La; register int i; - /* interpolate/extrapolate data */ + /* check limits */ + if (La <= 7.85e-4) + return(resfreq[0]); + if (La >= 1.78e3) + return(resfreq[NPOINTS-1]); + /* interpolate data */ l10La = log10(La); for (i = 0; i < NPOINTS-2 && l10lum[i+1] <= l10La; i++) ; @@ -196,22 +208,21 @@ int y; register COLOR *sl0, *sl1, *mysl; register int i; - if (y < sb->nread - sb->nscans) { - fprintf(stderr, "%s: internal - cannot backspace in getascan\n", - progname); - exit(1); - } + if (y < sb->nread - sb->nscans) /* too far back? */ + return(NULL); for ( ; y >= sb->nread; sb->nread++) { /* read as necessary */ mysl = bscan(sb, sb->nread); - if (sb->sampr == 1) { + if (sb->sampe == 0) { if (freadscan(mysl, sb->len, infp) < 0) { fprintf(stderr, "%s: %s: scanline read error\n", progname, infn); exit(1); } } else { - sl0 = getascan(sb->next, 2*y); - sl1 = getascan(sb->next, 2*y+1); + sl0 = getascan(sb+1, 2*y); + if (sl0 == NULL) + return(NULL); + sl1 = getascan(sb+1, 2*y+1); for (i = 0; i < sb->len; i++) { copycolor(mysl[i], sl0[2*i]); addcolor(mysl[i], sl0[2*i+1]); @@ -235,12 +246,12 @@ int y; register int x; /* compute foveal y position */ iy = dy = (y+.5)/numscans(&inpres)*fvyr - .5; - if (iy >= fvyr-1) iy--; + while (iy >= fvyr-1) iy--; dy -= (double)iy; for (x = 0; x < scanlen(&inpres); x++) { /* compute foveal x position */ ix = dx = (x+.5)/scanlen(&inpres)*fvxr - .5; - if (ix >= fvxr-1) ix--; + while (ix >= fvxr-1) ix--; dx -= (double)ix; /* interpolate sample rate */ sr = (1.-dy)*((1.-dx)*tsampr(ix,iy) + dx*tsampr(ix+1,iy)) + @@ -260,14 +271,13 @@ double sr; double d; register SCANBAR *sb0; - for (sb0 = rootbar; sb0->next != NULL && sb0->next->sampr > sr; - sb0 = sb0->next) + for (sb0 = rootbar; sb0->sampe != 0 && 1< sr; sb0++) ; ascanval(col, x, y, sb0); - if (sb0->next == NULL) /* don't extrapolate highest */ + if (sb0->sampe == 0) /* don't extrapolate highest */ return; - ascanval(c1, x, y, sb0->next); - d = (sb0->sampr - sr)/(sb0->sampr - sb0->next->sampr); + ascanval(c1, x, y, sb0+1); + d = ((1<sampe) - sr)/(1<sampr - .5; - if (ix >= sb->len-1) ix--; + if (sb->sampe == 0) { /* no need to interpolate */ + sl0 = getascan(sb, y); + copycolor(col, sl0[x]); + return; + } + /* compute coordinates for sb */ + ix = dx = (x+.5)/(1<sampe) - .5; + while (ix >= sb->len-1) ix--; dx -= (double)ix; - iy = dy = (y+.5)/sb->sampr - .5; - if (iy >= numscans(&inpres)/sb->sampr-1) iy--; + iy = dy = (y+.5)/(1<sampe) - .5; + while (iy >= (numscans(&inpres)>>sb->sampe)-1) iy--; dy -= (double)iy; /* get scanlines */ sl0 = getascan(sb, iy); +#ifdef DEBUG + if (sl0 == NULL) { + fprintf(stderr, "%s: internal - cannot backspace in ascanval\n", + progname); + abort(); + } +#endif sl1 = getascan(sb, iy+1); /* 2D linear interpolation */ copycolor(col, sl0[ix]); @@ -306,28 +329,36 @@ SCANBAR *sb; scalecolor(col, 1.-dy); scalecolor(c1y, dy); addcolor(col, c1y); + for (ix = 0; ix < 3; ix++) /* make sure no negative */ + if (colval(col,ix) < 0.) + colval(col,ix) = 0.; } SCANBAR * -sballoc(sr, ns, sl) /* allocate scanbar */ -int sr; /* sampling rate */ +sballoc(se, ns, sl) /* allocate scanbar */ +int se; /* sampling rate exponent */ int ns; /* number of scanlines */ int sl; /* original scanline length */ { + SCANBAR *sbarr; register SCANBAR *sb; - sb = (SCANBAR *)malloc(sizeof(SCANBAR)+(sl/sr)*ns*sizeof(COLOR)); + sbarr = sb = (SCANBAR *)malloc((se+1)*sizeof(SCANBAR)); if (sb == NULL) syserror("malloc"); - sb->nscans = ns; - sb->len = sl/sr; - sb->nread = 0; - if ((sb->sampr = sr) > 1) - sb->next = sballoc(sr/2, ns*2, sl); - else - sb->next = NULL; - return(sb); + do { + sb->sampe = se; + sb->len = sl>>se; + sb->nscans = ns; + sb->sdata = (COLOR *)malloc(sb->len*ns*sizeof(COLOR)); + if (sb->sdata == NULL) + syserror("malloc"); + sb->nread = 0; + ns <<= 1; + sb++; + } while (--se >= 0); + return(sbarr); } @@ -355,9 +386,10 @@ initacuity() /* initialize variable acuity sampling } fcross(cp, diffx, diffy); omega = 0.5 * sqrt(DOT(cp,cp)); - tsampr(x,y) = PI/180. / sqrt(omega) / - hacuity(plum(fovscan(y)[x])); - if (tsampr(x,y) > maxsr) + if (omega <= FTINY*FTINY) + tsampr(x,y) = 1.; + else if ((tsampr(x,y) = PI/180. / sqrt(omega) / + hacuity(plum(fovscan(y)[x]))) > maxsr) maxsr = tsampr(x,y); } /* copy perimeter (easier) */ @@ -366,9 +398,9 @@ initacuity() /* initialize variable acuity sampling tsampr(x,fvyr-1) = tsampr(x,fvyr-2); } for (y = 0; y < fvyr; y++) { - tsampr(y,0) = tsampr(y,1); - tsampr(y,fvxr-1) = tsampr(y,fvxr-2); + tsampr(0,y) = tsampr(1,y); + tsampr(fvxr-1,y) = tsampr(fvxr-2,y); } /* initialize with next power of two */ - rootbar = sballoc(2<<(int)(log(maxsr)/log(2.)), 2, scanlen(&inpres)); + rootbar = sballoc((int)(log(maxsr)/log(2.))+1, 2, scanlen(&inpres)); }