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.4 by greg, Wed Jan 8 17:57:01 1997 UTC vs.
Revision 3.14 by schorsch, Mon Jun 30 14:59:12 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1996 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Routines for computing and applying brightness mapping.
6   */
7  
8 + #include <string.h>
9 +
10   #include "pcond.h"
11  
12  
13 < #define CVRATIO         0.025           /* fraction of pixels allowed > env. */
13 > #define CVRATIO         0.025           /* fraction of samples allowed > env. */
14  
15 < #define exp10(x)        exp(2.302585093*(x))
15 > #define LN_10           2.30258509299404568402
16 > #define exp10(x)        exp(LN_10*(x))
17  
18 < int     modhist[HISTRES];               /* modified histogram */
19 < float   cumf[HISTRES+1];                /* cumulative distribution function */
18 > double  modhist[HISTRES];               /* modified histogram */
19 > double  mhistot;                        /* modified histogram total */
20 > double  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.rt=inpres.rt)&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((void *)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 + gethisto(fp)                    /* load precomputed luminance histogram */
85 + FILE    *fp;
86 + {
87 +        double  histo[MAXPREHIST];
88 +        double  histart, histep;
89 +        double  l, b, lastb, w;
90 +        int     n;
91 +        register int    i;
92 +                                        /* load data */
93 +        for (i = 0; i < MAXPREHIST &&
94 +                        fscanf(fp, "%lf %lf", &b, &histo[i]) == 2; i++) {
95 +                if (i > 1 && fabs(b - lastb - histep) > .001) {
96 +                        fprintf(stderr,
97 +                                "%s: uneven step size in histogram data\n",
98 +                                        progname);
99 +                        exit(1);
100 +                }
101 +                if (i == 1)
102 +                        if ((histep = b - (histart = lastb)) <= FTINY) {
103 +                                fprintf(stderr,
104 +                                        "%s: illegal step in histogram data\n",
105 +                                                progname);
106 +                                exit(1);
107 +                        }
108 +                lastb = b;
109 +        }
110 +        if (i < 2 || !feof(fp)) {
111 +                fprintf(stderr,
112 +                "%s: format/length error loading histogram (log10L %f at %d)\n",
113 +                                progname, b, i);
114 +                exit(1);
115 +        }
116 +        n = i;
117 +        histart *= LN_10;
118 +        histep *= LN_10;
119 +                                        /* find extrema */
120 +        for (i = 0; i < n && histo[i] <= FTINY; i++)
121 +                ;
122 +        bwmin = histart + (i-.001)*histep;
123 +        for (i = n; i-- && histo[i] <= FTINY; )
124 +                ;
125 +        bwmax = histart + (i+1.001)*histep;
126 +        if (bwmax > Bl(LMAX))
127 +                bwmax = Bl(LMAX);
128 +        if (bwmin < Bl(LMIN))
129 +                bwmin = Bl(LMIN);
130 +        else                            /* duplicate bottom bin */
131 +                bwmin = bwmax - (bwmax-bwmin)*HISTRES/(HISTRES-1);
132 +                                        /* convert histogram */
133 +        bwavg = 0.; histot = 0.;
134 +        for (i = 0; i < HISTRES; i++)
135 +                bwhist[i] = 0.;
136 +        for (i = 0, b = histart; i < n; i++, b += histep) {
137 +                if (b < bwmin+FTINY) continue;
138 +                if (b >= bwmax-FTINY) break;
139 +                w = histo[i];
140 +                bwavg += w*b;
141 +                bwhist[bwhi(b)] += w;
142 +                histot += w;
143 +        }
144 +        bwavg /= histot;
145 +        if (bwmin > Bl(LMIN)+FTINY) {   /* add false samples at bottom */
146 +                bwhist[1] *= 0.5;
147 +                bwhist[0] += bwhist[1];
148 +        }
149 + }
150 +
151 +
152 + double
153 + centprob(x, y)                  /* center-weighting probability function */
154 + int     x, y;
155 + {
156 +        double  xr, yr, p;
157 +                                /* paraboloid, 0 at 90 degrees from center */
158 +        xr = (x - .5*(fvxr-1))/90.;     /* 180 degree fisheye has fv?r == 90 */
159 +        yr = (y - .5*(fvyr-1))/90.;
160 +        p = 1. - xr*xr - yr*yr;
161 +        return(p < 0. ? 0. : p);
162 + }
163 +
164 +
165 + comphist()                      /* create foveal sampling histogram */
166 + {
167 +        double  l, b, w, lwmin, lwmax;
168 +        register int    x, y;
169 +                                        /* check for precalculated histogram */
170 +        if (what2do&DO_PREHIST)
171 +                return;
172 +        lwmin = 1e10;                   /* find extrema */
173 +        lwmax = 0.;
174 +        for (y = 0; y < fvyr; y++)
175 +                for (x = 0; x < fvxr; x++) {
176 +                        l = plum(fovscan(y)[x]);
177 +                        if (l < lwmin) lwmin = l;
178 +                        if (l > lwmax) lwmax = l;
179 +                }
180 +        lwmax *= 1.01;
181 +        if (lwmax > LMAX)
182 +                lwmax = LMAX;
183 +        bwmax = Bl(lwmax);
184 +        if (lwmin < LMIN) {
185 +                lwmin = LMIN;
186 +                bwmin = Bl(LMIN);
187 +        } else {                        /* duplicate bottom bin */
188 +                bwmin = bwmax - (bwmax-Bl(lwmin))*HISTRES/(HISTRES-1);
189 +                lwmin = Lb(bwmin);
190 +        }
191 +                                        /* (re)compute histogram */
192 +        bwavg = 0.;
193 +        histot = 0.;
194 +        for (x = 0; x < HISTRES; x++)
195 +                bwhist[x] = 0.;
196 +                                        /* global average */
197 +        if (!(what2do&DO_FIXHIST) || fixfrac < 1.-FTINY)
198 +                for (y = 0; y < fvyr; y++)
199 +                        for (x = 0; x < fvxr; x++) {
200 +                                l = plum(fovscan(y)[x]);
201 +                                if (l < lwmin+FTINY) continue;
202 +                                if (l >= lwmax-FTINY) continue;
203 +                                b = Bl(l);
204 +                                w = what2do&DO_CWEIGHT ? centprob(x,y) : 1.;
205 +                                bwavg += w*b;
206 +                                bwhist[bwhi(b)] += w;
207 +                                histot += w;
208 +                        }
209 +                                        /* average fixation points */
210 +        if (what2do&DO_FIXHIST && nfixations > 0) {
211 +                if (histot > FTINY)
212 +                        w = fixfrac/(1.-fixfrac)*histot/nfixations;
213 +                else
214 +                        w = 1.;
215 +                for (x = 0; x < nfixations; x++) {
216 +                        l = plum(fovscan(fixlst[x][1])[fixlst[x][0]]);
217 +                        if (l < lwmin+FTINY) continue;
218 +                        if (l >= lwmax-FTINY) continue;
219 +                        b = Bl(l);
220 +                        bwavg += w*b;
221 +                        bwhist[bwhi(b)] += w;
222 +                        histot += w;
223 +                }
224 +        }
225 +        bwavg /= histot;
226 +        if (lwmin > LMIN+FTINY) {       /* add false samples at bottom */
227 +                bwhist[1] *= 0.5;
228 +                bwhist[0] += bwhist[1];
229 +        }
230 + }
231 +
232 +
233   mkcumf()                        /* make cumulative distribution function */
234   {
235          register int    i;
236 <        register long   sum;
236 >        register double sum;
237  
238 <        cumf[0] = 0.;
239 <        sum = modhist[0];
240 <        for (i = 1; i < HISTRES; i++) {
241 <                cumf[i] = (double)sum/histot;
238 >        mhistot = 0.;           /* compute modified total */
239 >        for (i = 0; i < HISTRES; i++)
240 >                mhistot += modhist[i];
241 >
242 >        sum = 0.;               /* compute cumulative function */
243 >        for (i = 0; i < HISTRES; i++) {
244 >                cumf[i] = sum/mhistot;
245                  sum += modhist[i];
246          }
247          cumf[HISTRES] = 1.;
# Line 84 | Line 298 | double La;
298  
299  
300   double
301 < clampf(Lw)              /* derivative clamping function */
301 > clampf(Lw)                      /* histogram clamping function */
302   double  Lw;
303   {
304          double  bLw, ratio;
# Line 94 | Line 308 | double Lw;
308          return(ratio/(Lb1(bLw)*(Bldmax-Bldmin)*Bl1(Lw)));
309   }
310  
311 + double
312 + crfactor(Lw)                    /* contrast reduction factor */
313 + double  Lw;
314 + {
315 +        int     i = HISTRES*(Bl(Lw) - bwmin)/(bwmax - bwmin);
316 +        double  bLw, ratio, Tdb;
317  
318 < int
319 < shiftdir(bw)            /* compute shift direction for histogram */
320 < double  bw;
318 >        if (i <= 0)
319 >                return(1.0);
320 >        if (i >= HISTRES)
321 >                return(1.0);
322 >        bLw = BLw(Lw);
323 >        ratio = what2do&DO_HSENS ? htcontrs(Lb(bLw))/htcontrs(Lw) : Lb(bLw)/Lw;
324 >        Tdb = mhistot * (bwmax - bwmin) / HISTRES;
325 >        return(modhist[i]*Lb1(bLw)*(Bldmax-Bldmin)*Bl1(Lw)/(Tdb*ratio));
326 > }
327 >
328 >
329 > #if ADJ_VEIL
330 > mkcrfimage()                    /* compute contrast reduction factor image */
331   {
332 <        if (what2do&DO_HSENS && cf(bw) - (bw - bwmin)/(Bldmax - bwmin))
333 <                return(1);
334 <        return(-1);
332 >        int     i;
333 >        float   *crfptr;
334 >        COLOR   *fovptr;
335 >
336 >        if (crfimg == NULL)
337 >                crfimg = (float *)malloc(fvxr*fvyr*sizeof(float));
338 >        if (crfimg == NULL)
339 >                syserror("malloc");
340 >        crfptr = crfimg;
341 >        fovptr = fovimg;
342 >        for (i = fvxr*fvyr; i--; crfptr++, fovptr++)
343 >                crfptr[0] = crfactor(plum(fovptr[0]));
344   }
345 + #endif
346  
347  
348   int
349   mkbrmap()                       /* make dynamic range map */
350   {
351 <        int     hdiffs[HISTRES], above, below;
352 <        double  T, b, s;
113 <        int     maxd, maxi, sd;
351 >        double  Tdb, b, s;
352 >        double  ceiling, trimmings;
353          register int    i;
354                                          /* copy initial histogram */
355 <        for (i = 0; i < HISTRES; i++)
356 <                modhist[i] = bwhist[i];
118 <        T = histot * (bwmax - bwmin) / HISTRES;
119 <        s = (bwmax - bwmin)/HISTRES;
355 >        memcpy((void *)modhist, (void *)bwhist, sizeof(modhist));
356 >        s = (bwmax - bwmin)/HISTRES;    /* s is delta b */
357                                          /* loop until satisfactory */
358 <        for ( ; ; ) {
359 <                mkcumf();               /* sync brightness mapping */
360 <                above = below = 0;      /* compute visibility overflow */
358 >        do {
359 >                mkcumf();                       /* sync brightness mapping */
360 >                if (mhistot <= histot*CVRATIO)
361 >                        return(-1);             /* no compression needed! */
362 >                Tdb = mhistot * s;
363 >                trimmings = 0.;                 /* clip to envelope */
364                  for (i = 0, b = bwmin + .5*s; i < HISTRES; i++, b += s) {
365 <                        hdiffs[i] = modhist[i] - (int)(T*clampf(Lb(b)) + .5);
366 <                        if (hdiffs[i] > 0) above += hdiffs[i];
367 <                        else below -= hdiffs[i];
365 >                        ceiling = Tdb*clampf(Lb(b));
366 >                        if (modhist[i] > ceiling) {
367 >                                trimmings += modhist[i] - ceiling;
368 >                                modhist[i] = ceiling;
369 >                        }
370                  }
371 <                if (above <= histot*CVRATIO)
372 <                        break;          /* close enough */
373 <                if (above-below >= 0)
374 <                        return(-1);     /* Houston, we have a problem.... */
375 <                /* original looped here as well (BEGIN_L2) */
376 <                maxd = 0;               /* find largest overvis */
377 <                for (i = 0; i < HISTRES; i++)
136 <                        if (hdiffs[i] > maxd)
137 <                                maxd = hdiffs[maxi=i];
138 <                /* broke loop here when (maxd == 0) (BREAK_L2) */
139 <                for (sd = shiftdir((maxi+.5)/HISTRES*(bwmax-bwmin)+bwmin);
140 <                                hdiffs[maxi] == maxd; sd = -sd)
141 <                        for (i = maxi+sd; i >= 0 & i < HISTRES; i += sd)
142 <                                if (hdiffs[i] < 0) {
143 <                                        if (hdiffs[i] <= -maxd) {
144 <                                                modhist[i] += maxd;
145 <                                                modhist[maxi] -= maxd;
146 <                                                hdiffs[i] += maxd;
147 <                                                hdiffs[maxi] = 0;
148 <                                        } else {
149 <                                                modhist[maxi] += hdiffs[i];
150 <                                                modhist[i] -= hdiffs[i];
151 <                                                hdiffs[maxi] += hdiffs[i];
152 <                                                hdiffs[i] = 0;
153 <                                        }
154 <                                        break;
155 <                                }
156 <                /* (END_L2) */
157 <        }
158 <        return(0);
371 >        } while (trimmings > histot*CVRATIO);
372 >
373 > #if ADJ_VEIL
374 >        mkcrfimage();                   /* contrast reduction image */
375 > #endif
376 >
377 >        return(0);                      /* we got it */
378   }
379  
380  
# Line 195 | Line 414 | COLOR  *scan;
414   int     xres;
415   {
416          double  mult, Lw, b;
417 <        register int    i;
417 >        register int    x;
418  
419 <        for (i = 0; i < xres; i++) {
420 <                Lw = plum(scan[i]);
419 >        for (x = 0; x < xres; x++) {
420 >                Lw = plum(scan[x]);
421                  if (Lw < LMIN) {
422 <                        setcolor(scan[i], 0., 0., 0.);
422 >                        setcolor(scan[x], 0., 0., 0.);
423                          continue;
424                  }
425 <                b = BLw(Lw);
426 <                mult = (Lb(b) - ldmin)/(ldmax - ldmin) / (Lw*inpexp);
425 >                b = BLw(Lw);            /* apply brightness mapping */
426 >                mult = (Lb(b) - ldmin)/(ldmax - ldmin)/(Lw*inpexp);
427                  if (lumf == rgblum) mult *= WHTEFFICACY;
428 <                scalecolor(scan[i], mult);
428 >                scalecolor(scan[x], mult);
429          }
430   }
431  
# Line 216 | Line 435 | FILE   *fp;
435   {
436          double  b, s;
437          register int    i;
438 <        double  wlum, sf;
438 >        double  wlum, sf, dlum;
439  
440          sf = scalef*inpexp;
441          if (lumf == cielum) sf *= WHTEFFICACY;
442          s = (bwmax - bwmin)/HISTRES;
443          for (i = 0, b = bwmin + .5*s; i < HISTRES; i++, b += s) {
444                  wlum = Lb(b);
445 <                if (what2do&DO_LINEAR)
446 <                        fprintf(fp, "%e %e\n", wlum, sf*wlum);
447 <                else
445 >                if (what2do&DO_LINEAR) {
446 >                        dlum = sf*wlum;
447 >                        if (dlum > ldmax) dlum = ldmax;
448 >                        else if (dlum < ldmin) dlum = ldmin;
449 >                        fprintf(fp, "%e %e\n", wlum, dlum);
450 >                } else
451                          fprintf(fp, "%e %e\n", wlum, Lb(BLw(wlum)));
452          }
453   }
232
233
234 #ifdef DEBUG
235 doplots()                       /* generate debugging plots */
236 {
237        double  T, b, s;
238        FILE    *fp;
239        char    fname[128];
240        register int    i;
241
242        T = histot * (bwmax - bwmin) / HISTRES;
243        s = (bwmax - bwmin)/HISTRES;
244
245        sprintf(fname, "%s_hist.plt", infn);
246        if ((fp = fopen(fname, "w")) == NULL)
247                syserror(fname);
248        fputs("include=curve.plt\n", fp);
249        fputs("title=\"Brightness Frequency Distribution\"\n", fp);
250        fprintf(fp, "subtitle=%s\n", infn);
251        fputs("ymin=0\n", fp);
252        fputs("xlabel=\"Perceptual Brightness B(Lw)\"\n", fp);
253        fputs("ylabel=\"Frequency Count\"\n", fp);
254        fputs("Alabel=\"Histogram\"\n", fp);
255        fputs("Alintype=0\n", fp);
256        fputs("Blabel=\"Envelope\"\n", fp);
257        fputs("Bsymsize=0\n", fp);
258        fputs("Adata=\n", fp);
259        for (i = 0, b = bwmin + .5*s; i < HISTRES; i++, b += s)
260                fprintf(fp, "\t%f %d\n", b, modhist[i]);
261        fputs(";\nBdata=\n", fp);
262        for (i = 0, b = bwmin + .5*s; i < HISTRES; i++, b += s)
263                fprintf(fp, "\t%f %f\n", b, T*clampf(Lb(b)));
264        fputs(";\n", fp);
265        fclose(fp);
266 }
267 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines