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.6 by greg, Fri Jan 10 16:48:21 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 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 < float   modhist[HISTRES];               /* modified histogram */
18 > double  modhist[HISTRES];               /* modified histogram */
19   double  mhistot;                        /* modified histogram total */
20 < float   cumf[HISTRES+1];                /* cumulative distribution function */
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;
# Line 88 | 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 98 | 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 +        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 +        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 <        double  T, b, s;
351 >        double  Tdb, b, s;
352          double  ceiling, trimmings;
353          register int    i;
354                                          /* copy initial histogram */
355 <        bcopy((char *)bwhist, (char *)modhist, sizeof(modhist));
356 <        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          do {
359                  mkcumf();                       /* sync brightness mapping */
360                  if (mhistot <= histot*CVRATIO)
361                          return(-1);             /* no compression needed! */
362 <                T = mhistot * (bwmax - bwmin) / HISTRES;
362 >                Tdb = mhistot * s;
363                  trimmings = 0.;                 /* clip to envelope */
364                  for (i = 0, b = bwmin + .5*s; i < HISTRES; i++, b += s) {
365 <                        ceiling = T*clampf(Lb(b));
365 >                        ceiling = Tdb*clampf(Lb(b));
366                          if (modhist[i] > ceiling) {
367                                  trimmings += modhist[i] - ceiling;
368                                  modhist[i] = ceiling;
# Line 124 | Line 370 | mkbrmap()                      /* make dynamic range map */
370                  }
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  
# Line 164 | 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 185 | 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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines