ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond3.c
(Generate patch)

Comparing ray/src/px/pcond3.c (file contents):
Revision 3.2 by greg, Thu Oct 10 17:09:26 1996 UTC vs.
Revision 3.9 by greg, Fri Apr 11 18:34:39 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1996 Regents of the University of California */
1 > /* Copyright (c) 1997 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 11 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11   #include "pcond.h"
12  
13  
14 < #define CVRATIO         0.025           /* fraction of pixels allowed > env. */
14 > #define CVRATIO         0.025           /* fraction of samples allowed > env. */
15  
16 #define BotMesopic      5.62e-3         /* top of scotopic range */
17 #define TopMesopic      5.62            /* bottom of photopic range */
18
16   #define exp10(x)        exp(2.302585093*(x))
17  
18 < int     modhist[HISTRES];               /* modified histogram */
18 > float   modhist[HISTRES];               /* modified histogram */
19 > double  mhistot;                        /* modified histogram total */
20   float   cumf[HISTRES+1];                /* cumulative distribution function */
21  
22  
23 + getfixations(fp)                /* load fixation history list */
24 + FILE    *fp;
25 + {
26 + #define FIXHUNK         128
27 +        RESOLU  fvres;
28 +        int     pos[2];
29 +        register int    px, py, i;
30 +                                /* initialize our resolution struct */
31 +        if ((fvres.or=inpres.or)&YMAJOR) {
32 +                fvres.xr = fvxr;
33 +                fvres.yr = fvyr;
34 +        } else {
35 +                fvres.xr = fvyr;
36 +                fvres.yr = fvxr;
37 +        }
38 +                                /* read each picture position */
39 +        while (fscanf(fp, "%d %d", &pos[0], &pos[1]) == 2) {
40 +                                /* convert to closest index in foveal image */
41 +                loc2pix(pos, &fvres,
42 +                                (pos[0]+.5)/inpres.xr, (pos[1]+.5)/inpres.yr);
43 +                                /* include nine neighborhood samples */
44 +                for (px = pos[0]-1; px <= pos[0]+1; px++) {
45 +                        if (px < 0 || px >= fvxr)
46 +                                continue;
47 +                        for (py = pos[1]-1; py <= pos[1]+1; py++) {
48 +                                if (py < 0 || py >= fvyr)
49 +                                        continue;
50 +                                for (i = nfixations; i-- > 0; )
51 +                                        if (fixlst[i][0] == px &&
52 +                                                        fixlst[i][1] == py)
53 +                                                break;
54 +                                if (i >= 0)
55 +                                        continue;       /* already there */
56 +                                if (nfixations % FIXHUNK == 0) {
57 +                                        if (nfixations)
58 +                                                fixlst = (short (*)[2])
59 +                                                        realloc((char *)fixlst,
60 +                                                        (nfixations+FIXHUNK)*
61 +                                                        2*sizeof(short));
62 +                                        else
63 +                                                fixlst = (short (*)[2])malloc(
64 +                                                        FIXHUNK*2*sizeof(short)
65 +                                                        );
66 +                                        if (fixlst == NULL)
67 +                                                syserror("malloc");
68 +                                }
69 +                                fixlst[nfixations][0] = px;
70 +                                fixlst[nfixations][1] = py;
71 +                                nfixations++;
72 +                        }
73 +                }
74 +        }
75 +        if (!feof(fp)) {
76 +                fprintf(stderr, "%s: format error reading fixation data\n",
77 +                                progname);
78 +                exit(1);
79 +        }
80 + #undef  FIXHUNK
81 + }
82 +
83 +
84 + double
85 + centprob(x, y)                  /* center-weighting probability function */
86 + int     x, y;
87 + {
88 +        double  xr, yr, p;
89 +                                /* paraboloid, 0 at 90 degrees from center */
90 +        xr = (x - .5*(fvxr-1))/90.;     /* 180 degree fisheye has fv?r == 90 */
91 +        yr = (y - .5*(fvyr-1))/90.;
92 +        p = 1. - xr*xr - yr*yr;
93 +        return(p < 0. ? 0. : p);
94 + }
95 +
96 +
97 + comphist()                      /* create foveal sampling histogram */
98 + {
99 +        double  l, b, w, lwmin, lwmax;
100 +        register int    x, y;
101 +
102 +        lwmin = 1e10;                   /* find extrema */
103 +        lwmax = 0.;
104 +        for (y = 0; y < fvyr; y++)
105 +                for (x = 0; x < fvxr; x++) {
106 +                        l = plum(fovscan(y)[x]);
107 +                        if (l < lwmin) lwmin = l;
108 +                        if (l > lwmax) lwmax = l;
109 +                }
110 +        lwmax *= 1.01;
111 +        if (lwmax > LMAX)
112 +                lwmax = LMAX;
113 +        bwmax = Bl(lwmax);
114 +        if (lwmin < LMIN) {
115 +                lwmin = LMIN;
116 +                bwmin = Bl(LMIN);
117 +        } else {                        /* duplicate bottom bin */
118 +                bwmin = bwmax - (bwmax-Bl(lwmin))*HISTRES/(HISTRES-1);
119 +                lwmin = Lb(bwmin);
120 +        }
121 +                                        /* (re)compute histogram */
122 +        bwavg = 0.;
123 +        histot = 0.;
124 +        for (x = 0; x < HISTRES; x++)
125 +                bwhist[x] = 0.;
126 +                                        /* global average */
127 +        if (!(what2do&DO_FIXHIST) || fixfrac < 1.-FTINY)
128 +                for (y = 0; y < fvyr; y++)
129 +                        for (x = 0; x < fvxr; x++) {
130 +                                l = plum(fovscan(y)[x]);
131 +                                if (l < lwmin) continue;
132 +                                if (l > lwmax) continue;
133 +                                b = Bl(l);
134 +                                bwavg += b;
135 +                                w = what2do&DO_CWEIGHT ? centprob(x,y) : 1.;
136 +                                bwhist[bwhi(b)] += w;
137 +                                histot += w;
138 +                        }
139 +                                        /* average fixation points */
140 +        if (what2do&DO_FIXHIST && nfixations > 0) {
141 +                if (histot > FTINY)
142 +                        w = fixfrac/(1.-fixfrac)*histot/nfixations;
143 +                else
144 +                        w = 1.;
145 +                for (x = 0; x < nfixations; x++) {
146 +                        l = plum(fovscan(fixlst[x][1])[fixlst[x][0]]);
147 +                        if (l < lwmin) continue;
148 +                        if (l > lwmax) continue;
149 +                        b = Bl(l);
150 +                        bwavg += b;
151 +                        bwhist[bwhi(b)] += w;
152 +                        histot += w;
153 +                }
154 +        }
155 +        bwavg /= histot;
156 +        if (lwmin > LMIN+FTINY) {       /* add false samples at bottom */
157 +                bwhist[1] *= 0.5;
158 +                bwhist[0] += bwhist[1];
159 +        }
160 + }
161 +
162 +
163   mkcumf()                        /* make cumulative distribution function */
164   {
165          register int    i;
166 <        register long   sum;
166 >        register double sum;
167  
168 <        cumf[0] = 0.;
169 <        sum = modhist[0];
170 <        for (i = 1; i < HISTRES; i++) {
171 <                cumf[i] = (double)sum/histot;
168 >        mhistot = 0.;           /* compute modified total */
169 >        for (i = 0; i < HISTRES; i++)
170 >                mhistot += modhist[i];
171 >
172 >        sum = 0.;               /* compute cumulative function */
173 >        for (i = 0; i < HISTRES; i++) {
174 >                cumf[i] = sum/mhistot;
175                  sum += modhist[i];
176          }
177          cumf[HISTRES] = 1.;
# Line 60 | Line 201 | double Lw;
201                  return(Bldmin);
202          if (b >= bwmax-FTINY)
203                  return(Bldmax);
204 <        return(Bldmin + cf(Bl(Lw))*(Bldmax-Bldmin));
204 >        return(Bldmin + cf(b)*(Bldmax-Bldmin));
205   }
206  
207  
# Line 99 | Line 240 | double Lw;
240  
241  
242   int
102 shiftdir(bw)            /* compute shift direction for histogram */
103 double  bw;
104 {
105        if (what2do&DO_HSENS && cf(bw) - (bw - bwmin)/(Bldmax - bwmin))
106                return(1);
107        return(-1);
108 }
109
110
111 int
243   mkbrmap()                       /* make dynamic range map */
244   {
114        int     hdiffs[HISTRES], above, below;
245          double  T, b, s;
246 <        int     maxd, maxi, sd;
246 >        double  ceiling, trimmings;
247          register int    i;
248                                          /* copy initial histogram */
249 <        for (i = 0; i < HISTRES; i++)
120 <                modhist[i] = bwhist[i];
121 <        T = histot * (bwmax - bwmin) / HISTRES;
249 >        bcopy((char *)bwhist, (char *)modhist, sizeof(modhist));
250          s = (bwmax - bwmin)/HISTRES;
251                                          /* loop until satisfactory */
252 <        for ( ; ; ) {
253 <                mkcumf();               /* sync brightness mapping */
254 <                above = below = 0;      /* compute visibility overflow */
252 >        do {
253 >                mkcumf();                       /* sync brightness mapping */
254 >                if (mhistot <= histot*CVRATIO)
255 >                        return(-1);             /* no compression needed! */
256 >                T = mhistot * (bwmax - bwmin) / HISTRES;
257 >                trimmings = 0.;                 /* clip to envelope */
258                  for (i = 0, b = bwmin + .5*s; i < HISTRES; i++, b += s) {
259 <                        hdiffs[i] = modhist[i] - (int)(T*clampf(Lb(b)) + .5);
260 <                        if (hdiffs[i] > 0) above += hdiffs[i];
261 <                        else below -= hdiffs[i];
259 >                        ceiling = T*clampf(Lb(b));
260 >                        if (modhist[i] > ceiling) {
261 >                                trimmings += modhist[i] - ceiling;
262 >                                modhist[i] = ceiling;
263 >                        }
264                  }
265 <                if (above <= histot*CVRATIO)
266 <                        break;          /* close enough */
267 <                if (above-below >= 0)
135 <                        return(-1);     /* Houston, we have a problem.... */
136 <                /* original looped here as well (BEGIN_L2) */
137 <                maxd = 0;               /* find largest overvis */
138 <                for (i = 0; i < HISTRES; i++)
139 <                        if (hdiffs[i] > maxd)
140 <                                maxd = hdiffs[maxi=i];
141 <                /* broke loop here when (maxd == 0) (BREAK_L2) */
142 <                for (sd = shiftdir((maxi+.5)/HISTRES*(bwmax-bwmin)+bwmin);
143 <                                hdiffs[maxi] == maxd; sd = -sd)
144 <                        for (i = maxi+sd; i >= 0 & i < HISTRES; i += sd)
145 <                                if (hdiffs[i] < 0) {
146 <                                        if (hdiffs[i] <= -maxd) {
147 <                                                modhist[i] += maxd;
148 <                                                modhist[maxi] -= maxd;
149 <                                                hdiffs[i] += maxd;
150 <                                                hdiffs[maxi] = 0;
151 <                                        } else {
152 <                                                modhist[maxi] += hdiffs[i];
153 <                                                modhist[i] -= hdiffs[i];
154 <                                                hdiffs[maxi] += hdiffs[i];
155 <                                                hdiffs[i] = 0;
156 <                                        }
157 <                                        break;
158 <                                }
159 <                /* (END_L2) */
160 <        }
161 <        return(0);
265 >        } while (trimmings > histot*CVRATIO);
266 >
267 >        return(0);                      /* we got it */
268   }
269  
270  
# Line 214 | Line 320 | int    xres;
320   }
321  
322  
323 < #ifdef DEBUG
324 < doplots()                       /* generate debugging plots */
323 > putmapping(fp)                  /* put out mapping function */
324 > FILE    *fp;
325   {
326 <        double  T, b, s;
221 <        FILE    *fp;
222 <        char    fname[128];
326 >        double  b, s;
327          register int    i;
328 +        double  wlum, sf, dlum;
329  
330 <        T = histot * (bwmax - bwmin) / HISTRES;
330 >        sf = scalef*inpexp;
331 >        if (lumf == cielum) sf *= WHTEFFICACY;
332          s = (bwmax - bwmin)/HISTRES;
333 <
334 <        sprintf(fname, "%s_hist.plt", infn);
335 <        if ((fp = fopen(fname, "w")) == NULL)
336 <                syserror(fname);
337 <        fputs("include=curve.plt\n", fp);
338 <        fputs("title=\"Brightness Frequency Distribution\"\n", fp);
339 <        fprintf(fp, "subtitle=%s\n", infn);
340 <        fputs("ymin=0\n", fp);
341 <        fputs("xlabel=\"Perceptual Brightness B(Lw)\"\n", fp);
342 <        fputs("ylabel=\"Frequency Count\"\n", fp);
237 <        fputs("Alabel=\"Histogram\"\n", fp);
238 <        fputs("Alintype=0\n", fp);
239 <        fputs("Blabel=\"Envelope\"\n", fp);
240 <        fputs("Bsymsize=0\n", fp);
241 <        fputs("Adata=\n", fp);
242 <        for (i = 0, b = bwmin + .5*s; i < HISTRES; i++, b += s)
243 <                fprintf(fp, "\t%f %d\n", b, modhist[i]);
244 <        fputs(";\nBdata=\n", fp);
245 <        for (i = 0, b = bwmin + .5*s; i < HISTRES; i++, b += s)
246 <                fprintf(fp, "\t%f %f\n", b, T*clampf(Lb(b)));
247 <        fputs(";\n", fp);
248 <        fclose(fp);
333 >        for (i = 0, b = bwmin + .5*s; i < HISTRES; i++, b += s) {
334 >                wlum = Lb(b);
335 >                if (what2do&DO_LINEAR) {
336 >                        dlum = sf*wlum;
337 >                        if (dlum > ldmax) dlum = ldmax;
338 >                        else if (dlum < ldmin) dlum = ldmin;
339 >                        fprintf(fp, "%e %e\n", wlum, dlum);
340 >                } else
341 >                        fprintf(fp, "%e %e\n", wlum, Lb(BLw(wlum)));
342 >        }
343   }
250 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines