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.15 by schorsch, Sun Mar 28 20:33:14 2004 UTC vs.
Revision 3.20 by greg, Wed Sep 11 18:56:12 2024 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
31  
32  
33  
34 < extern void
34 > void
35   getfixations(           /* load fixation history list */
36   FILE    *fp
37   )
# Line 37 | Line 39 | FILE   *fp
39   #define FIXHUNK         128
40          RESOLU  fvres;
41          int     pos[2];
42 <        register int    px, py, i;
42 >        int     px, py, i;
43                                  /* initialize our resolution struct */
44          if ((fvres.rt=inpres.rt)&YMAJOR) {
45                  fvres.xr = fvxr;
# Line 92 | Line 94 | FILE   *fp
94   }
95  
96  
97 < extern void
97 > void
98   gethisto(                       /* load precomputed luminance histogram */
99          FILE    *fp
100   )
# Line 101 | Line 103 | gethisto(                      /* load precomputed luminance histogram */
103          double  histart, histep;
104          double  b, lastb, w;
105          int     n;
106 <        register int    i;
106 >        int     i;
107                                          /* load data */
108          for (i = 0; i < MAXPREHIST &&
109                          fscanf(fp, "%lf %lf", &b, &histo[i]) == 2; i++) {
# Line 177 | Line 179 | centprob(                      /* center-weighting probability function *
179   }
180  
181  
182 < extern void
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 && fread2colrs(cscan, x=scanlen(&inpres), infp,
200 >                                                NCSAMP, WLPART) < 0) {
201 >                        fprintf(stderr, "%s: %s: scanline read error\n",
202 >                                        progname, infn);
203 >                        exit(1);
204 >                }
205 >                rval = frandom();       /* random distance to next sample */
206 >                while ((cumprob += (1.-cumprob)*n/(npleft-sv)) < rval)
207 >                        sv++;
208 >                if (x < ++sv) {         /* out of pixels in this scanline */
209 >                        npleft -= x;
210 >                        x = 0;
211 >                        continue;
212 >                }
213 >                x -= sv;
214 >                colr_color(col, cscan[x]);
215 >                ls[--n] = plum(col);
216 >                npleft -= sv;
217 >        }
218 >        free(cscan);                    /* clean up and reset file pointer */
219 >        if (fseek(infp, startpos, SEEK_SET) < 0)
220 >                syserror("fseek");
221 >        return(ls);
222 > }
223 >
224 >
225 > void
226   comphist(void)                  /* create foveal sampling histogram */
227   {
228          double  l, b, w, lwmin, lwmax;
229 <        register int    x, y;
229 >        float   *lumsamp;
230 >        int     nlumsamp;
231 >        int     x, y;
232                                          /* check for precalculated histogram */
233          if (what2do&DO_PREHIST)
234                  return;
# Line 193 | Line 240 | comphist(void)                 /* create foveal sampling histogram *
240                          if (l < lwmin) lwmin = l;
241                          if (l > lwmax) lwmax = l;
242                  }
243 +                                        /* sample luminance pixels */
244 +        nlumsamp = fvxr*fvyr*16;
245 +        if (nlumsamp > inpres.xr*inpres.yr)
246 +                nlumsamp = inpres.xr*inpres.yr;
247 +        lumsamp = getlumsamp(nlumsamp);
248 +        for (x = nlumsamp; x--; ) {
249 +                l = lumsamp[x];
250 +                if (l < lwmin) lwmin = l;
251 +                if (l > lwmax) lwmax = l;
252 +        }
253          lwmax *= 1.01;
254          if (lwmax > LMAX)
255                  lwmax = LMAX;
# Line 207 | Line 264 | comphist(void)                 /* create foveal sampling histogram *
264                                          /* (re)compute histogram */
265          bwavg = 0.;
266          histot = 0.;
267 <        for (x = 0; x < HISTRES; x++)
211 <                bwhist[x] = 0.;
267 >        memset(bwhist, 0, sizeof(bwhist));
268                                          /* global average */
269 <        if (!(what2do&DO_FIXHIST) || fixfrac < 1.-FTINY)
269 >        if (!(what2do&DO_FIXHIST) || fixfrac < 1.-FTINY) {
270                  for (y = 0; y < fvyr; y++)
271                          for (x = 0; x < fvxr; x++) {
272                                  l = plum(fovscan(y)[x]);
# Line 222 | Line 278 | comphist(void)                 /* create foveal sampling histogram *
278                                  bwhist[bwhi(b)] += w;
279                                  histot += w;
280                          }
281 +                                        /* weight for point luminances */
282 +                w = 1. * histot / nlumsamp;
283 +                for (x = nlumsamp; x--; ) {
284 +                        l = lumsamp[x];
285 +                        if (l < lwmin+FTINY) continue;
286 +                        if (l >= lwmax-FTINY) continue;
287 +                        b = Bl(l);
288 +                        bwavg += w*b;
289 +                        bwhist[bwhi(b)] += w;
290 +                        histot += w;
291 +                }
292 +        }
293                                          /* average fixation points */
294          if (what2do&DO_FIXHIST && nfixations > 0) {
295                  if (histot > FTINY)
# Line 243 | Line 311 | comphist(void)                 /* create foveal sampling histogram *
311                  bwhist[1] *= 0.5;
312                  bwhist[0] += bwhist[1];
313          }
314 +        free(lumsamp);
315   }
316  
317  
318   static void
319   mkcumf(void)                    /* make cumulative distribution function */
320   {
321 <        register int    i;
322 <        register double sum;
321 >        int     i;
322 >        double  sum;
323  
324          mhistot = 0.;           /* compute modified total */
325          for (i = 0; i < HISTRES; i++)
# Line 271 | Line 340 | cf(                            /* return cumulative function at b */
340   )
341   {
342          double  x;
343 <        register int    i;
343 >        int     i;
344  
345          i = x = HISTRES*(b - bwmin)/(bwmax - bwmin);
346          x -= (double)i;
# Line 294 | Line 363 | BLw(                           /* map world luminance to display brightness *
363   }
364  
365  
366 < extern double
366 > double
367   htcontrs(               /* human threshold contrast sensitivity, dL(La) */
368          double  La
369   )
# Line 317 | Line 386 | htcontrs(              /* human threshold contrast sensitivity, dL
386   }
387  
388  
389 < extern double
389 > double
390   clampf(                 /* histogram clamping function */
391          double  Lw
392   )
# Line 329 | Line 398 | clampf(                        /* histogram clamping function */
398          return(ratio/(Lb1(bLw)*(Bldmax-Bldmin)*Bl1(Lw)));
399   }
400  
401 < extern double
401 > double
402   crfactor(                       /* contrast reduction factor */
403          double  Lw
404   )
# Line 368 | Line 437 | mkcrfimage(void)                       /* compute contrast reduction facto
437   #endif
438  
439  
440 < extern int
440 > int
441   mkbrmap(void)                   /* make dynamic range map */
442   {
443          double  Tdb, b, s;
444          double  ceiling, trimmings;
445 <        register int    i;
445 >        int     i;
446                                          /* copy initial histogram */
447          memcpy((void *)modhist, (void *)bwhist, sizeof(modhist));
448          s = (bwmax - bwmin)/HISTRES;    /* s is delta b */
# Line 391 | Line 460 | mkbrmap(void)                  /* make dynamic range map */
460                                  modhist[i] = ceiling;
461                          }
462                  }
463 <        } while (trimmings > histot*CVRATIO);
463 >        } while (trimmings > mhistot*CVRATIO);
464  
465   #if ADJ_VEIL
466          mkcrfimage();                   /* contrast reduction image */
# Line 401 | Line 470 | mkbrmap(void)                  /* make dynamic range map */
470   }
471  
472  
473 < extern void
473 > void
474   scotscan(               /* apply scotopic color sensitivity loss */
475          COLOR   *scan,
476          int     xres
# Line 409 | Line 478 | scotscan(              /* apply scotopic color sensitivity loss */
478   {
479          COLOR   ctmp;
480          double  incolor, b, Lw;
481 <        register int    i;
481 >        int     i;
482  
483          for (i = 0; i < xres; i++) {
484                  Lw = plum(scan[i]);
# Line 434 | Line 503 | scotscan(              /* apply scotopic color sensitivity loss */
503   }
504  
505  
506 < extern void
506 > void
507   mapscan(                /* apply tone mapping operator to scanline */
508          COLOR   *scan,
509          int     xres
510   )
511   {
512          double  mult, Lw, b;
513 <        register int    x;
513 >        int     x;
514  
515          for (x = 0; x < xres; x++) {
516                  Lw = plum(scan[x]);
# Line 457 | Line 526 | mapscan(               /* apply tone mapping operator to scanline *
526   }
527  
528  
529 < extern void
529 > void
530   putmapping(                     /* put out mapping function */
531          FILE    *fp
532   )
533   {
534          double  b, s;
535 <        register int    i;
535 >        int     i;
536          double  wlum, sf, dlum;
537  
538          sf = scalef*inpexp;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines