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.17 by greg, Thu Nov 30 18:43:05 2017 UTC vs.
Revision 3.19 by greg, Tue Apr 13 02:56:42 2021 UTC

# Line 8 | Line 8 | static const char      RCSid[] = "$Id$";
8   #include <string.h>
9  
10   #include "pcond.h"
11 + #include "random.h"
12  
13  
14   #define CVRATIO         0.025           /* fraction of samples allowed > env. */
# Line 19 | Line 20 | double modhist[HISTRES];               /* modified histogram */
20   double  mhistot;                        /* modified histogram total */
21   double  cumf[HISTRES+1];                /* cumulative distribution function */
22  
23 < static double centprob(int      x, int  y);
23 > static double centprob(int x, int y);
24   static void mkcumf(void);
25 < static double cf(double b);
26 < static double BLw(double        Lw);
25 > static double cf(double b);
26 > static double BLw(double Lw);
27 > static float *getlumsamp(int n);
28   #if ADJ_VEIL
29   static void mkcrfimage(void);
30   #endif
# Line 177 | Line 179 | centprob(                      /* center-weighting probability function *
179   }
180  
181  
182 + static float *
183 + getlumsamp(int n)               /* get set of random point sample luminances */
184 + {
185 +        float   *ls = (float *)malloc(n*sizeof(float));
186 +        COLR    *cscan = (COLR *)malloc(scanlen(&inpres)*sizeof(COLR));
187 +        long    startpos = ftell(infp);
188 +        long    npleft = (long)inpres.xr*inpres.yr;
189 +        int     x;
190 +        
191 +        if ((ls == NULL) | (cscan == NULL))
192 +                syserror("malloc");
193 +        x = 0;                          /* read/convert samples */
194 +        while (n > 0) {
195 +                COLOR   col;
196 +                int     sv = 0;
197 +                double  rval, cumprob = 0;
198 +
199 +                if (x <= 0 && freadcolrs(cscan, x=scanlen(&inpres), infp) < 0) {
200 +                        fprintf(stderr, "%s: %s: scanline read error\n",
201 +                                        progname, infn);
202 +                        exit(1);
203 +                }
204 +                rval = frandom();       /* random distance to next sample */
205 +                while ((cumprob += (1.-cumprob)*n/(npleft-sv)) < rval)
206 +                        sv++;
207 +                if (x < ++sv) {         /* out of pixels in this scanline */
208 +                        npleft -= x;
209 +                        x = 0;
210 +                        continue;
211 +                }
212 +                x -= sv;
213 +                colr_color(col, cscan[x]);
214 +                ls[--n] = plum(col);
215 +                npleft -= sv;
216 +        }
217 +        free(cscan);                    /* clean up and reset file pointer */
218 +        if (fseek(infp, startpos, SEEK_SET) < 0)
219 +                syserror("fseek");
220 +        return(ls);
221 + }
222 +
223 +
224   void
225   comphist(void)                  /* create foveal sampling histogram */
226   {
227          double  l, b, w, lwmin, lwmax;
228 +        float   *lumsamp;
229 +        int     nlumsamp;
230          int     x, y;
231                                          /* check for precalculated histogram */
232          if (what2do&DO_PREHIST)
# Line 193 | Line 239 | comphist(void)                 /* create foveal sampling histogram *
239                          if (l < lwmin) lwmin = l;
240                          if (l > lwmax) lwmax = l;
241                  }
242 +                                        /* sample luminance pixels */
243 +        nlumsamp = fvxr*fvyr*16;
244 +        if (nlumsamp > inpres.xr*inpres.yr)
245 +                nlumsamp = inpres.xr*inpres.yr;
246 +        lumsamp = getlumsamp(nlumsamp);
247 +        for (x = nlumsamp; x--; ) {
248 +                l = lumsamp[x];
249 +                if (l < lwmin) lwmin = l;
250 +                if (l > lwmax) lwmax = l;
251 +        }
252          lwmax *= 1.01;
253          if (lwmax > LMAX)
254                  lwmax = LMAX;
# Line 207 | Line 263 | comphist(void)                 /* create foveal sampling histogram *
263                                          /* (re)compute histogram */
264          bwavg = 0.;
265          histot = 0.;
266 <        for (x = 0; x < HISTRES; x++)
211 <                bwhist[x] = 0.;
266 >        memset(bwhist, 0, sizeof(bwhist));
267                                          /* global average */
268 <        if (!(what2do&DO_FIXHIST) || fixfrac < 1.-FTINY)
268 >        if (!(what2do&DO_FIXHIST) || fixfrac < 1.-FTINY) {
269                  for (y = 0; y < fvyr; y++)
270                          for (x = 0; x < fvxr; x++) {
271                                  l = plum(fovscan(y)[x]);
# Line 222 | Line 277 | comphist(void)                 /* create foveal sampling histogram *
277                                  bwhist[bwhi(b)] += w;
278                                  histot += w;
279                          }
280 +                                        /* weight for point luminances */
281 +                w = 1. * histot / nlumsamp;
282 +                for (x = nlumsamp; x--; ) {
283 +                        l = lumsamp[x];
284 +                        if (l < lwmin+FTINY) continue;
285 +                        if (l >= lwmax-FTINY) continue;
286 +                        b = Bl(l);
287 +                        bwavg += w*b;
288 +                        bwhist[bwhi(b)] += w;
289 +                        histot += w;
290 +                }
291 +        }
292                                          /* average fixation points */
293          if (what2do&DO_FIXHIST && nfixations > 0) {
294                  if (histot > FTINY)
# Line 243 | Line 310 | comphist(void)                 /* create foveal sampling histogram *
310                  bwhist[1] *= 0.5;
311                  bwhist[0] += bwhist[1];
312          }
313 +        free(lumsamp);
314   }
315  
316  
# Line 391 | Line 459 | mkbrmap(void)                  /* make dynamic range map */
459                                  modhist[i] = ceiling;
460                          }
461                  }
462 <        } while (trimmings > histot*CVRATIO);
462 >        } while (trimmings > mhistot*CVRATIO);
463  
464   #if ADJ_VEIL
465          mkcrfimage();                   /* contrast reduction image */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines