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

Comparing ray/src/px/pcond4.c (file contents):
Revision 3.9 by greg, Thu Oct 10 16:36:39 1996 UTC vs.
Revision 3.19 by greg, Sun Nov 14 16:57:18 2004 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 veiling glare and loss of acuity.
6   */
# Line 14 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11  
12   #define VADAPT          0.08            /* fraction of adaptation from veil */
13  
14 < extern COLOR    *fovimg;                /* foveal (1 degree) averaged image */
18 < extern short    fvxr, fvyr;             /* foveal image resolution */
14 > static COLOR    *veilimg = NULL;        /* veiling image */
15  
20 #define fovscan(y)      (fovimg+(y)*fvxr)
21
22 static COLOR    *veilimg;               /* veiling image */
23
16   #define veilscan(y)     (veilimg+(y)*fvxr)
17  
18   static float    (*raydir)[3] = NULL;    /* ray direction for each pixel */
19  
20   #define rdirscan(y)     (raydir+(y)*fvxr)
21  
22 + static void compraydir(void);
23 + #if ADJ_VEIL
24 + static void smoothveil(void);
25 + #endif
26  
27 < compraydir()                            /* compute ray directions */
27 >
28 > static void
29 > compraydir(void)                                /* compute ray directions */
30   {
31          FVECT   rorg, rdir;
32          double  h, v;
# Line 41 | Line 39 | compraydir()                           /* compute ray directions */
39                  syserror("malloc");
40  
41          for (y = 0; y < fvyr; y++) {
42 <                switch (inpres.or) {
42 >                switch (inpres.rt) {
43                  case YMAJOR: case YMAJOR|XDECR:
44                          v = (y+.5)/fvyr; break;
45                  case YMAJOR|YDECR: case YMAJOR|YDECR|XDECR:
# Line 52 | Line 50 | compraydir()                           /* compute ray directions */
50                          h = 1. - (y+.5)/fvyr; break;
51                  }
52                  for (x = 0; x < fvxr; x++) {
53 <                        switch (inpres.or) {
53 >                        switch (inpres.rt) {
54                          case YMAJOR: case YMAJOR|YDECR:
55                                  h = (x+.5)/fvxr; break;
56                          case YMAJOR|XDECR: case YMAJOR|XDECR|YDECR:
# Line 77 | Line 75 | compraydir()                           /* compute ray directions */
75   }
76  
77  
78 < compveil()                              /* compute veiling image */
78 > extern void
79 > compveil(void)                          /* compute veiling image */
80   {
81          double  t2, t2sum;
82          COLOR   ctmp, vsum;
83          int     px, py;
84          register int    x, y;
85 +
86 +        if (veilimg != NULL)            /* already done? */
87 +                return;
88                                          /* compute ray directions */
89          compraydir();
90                                          /* compute veil image */
# Line 100 | Line 102 | compveil()                             /* compute veiling image */
102                                                          rdirscan(y)[x]);
103                                          if (t2 <= FTINY) continue;
104                                          /*      use approximation instead
105 <                                        t2 = acos(t2);
106 <                                        t2 = 1./(t2*t2);
105 >                                        t3 = acos(t2);
106 >                                        t2 = t2/(t3*t3);
107                                          */
108 <                                        t2 = .5 / (1. - t2);
108 >                                        t2 *= .5 / (1. - t2);
109                                          copycolor(ctmp, fovscan(y)[x]);
110                                          scalecolor(ctmp, t2);
111                                          addcolor(vsum, ctmp);
112                                          t2sum += t2;
113                                  }
114                          /* VADAPT of original is subtracted in addveil() */
115 <                        scalecolor(vsum, VADAPT/t2sum);
115 >                        if (t2sum > FTINY)
116 >                                scalecolor(vsum, VADAPT/t2sum);
117                          copycolor(veilscan(py)[px], vsum);
118                  }
119 +                                        /* modify FOV sample image */
120 +        for (y = 0; y < fvyr; y++)
121 +                for (x = 0; x < fvxr; x++) {
122 +                        scalecolor(fovscan(y)[x], 1.-VADAPT);
123 +                        addcolor(fovscan(y)[x], veilscan(y)[x]);
124 +                }
125 +        comphist();                     /* recompute histogram */
126   }
127  
128  
129 < addveil(sl, y)                          /* add veil to scanline */
130 < COLOR   *sl;
131 < int     y;
129 > #if ADJ_VEIL
130 > /*
131 > * The following veil adjustment was added to compensate for
132 > * the fact that contrast reduction gets confused with veil
133 > * in the human visual system.  Therefore, we reduce the
134 > * veil in portions of the image where our mapping has
135 > * already reduced contrast below the target value.
136 > * This gets called after the intial veil has been computed
137 > * and added to the foveal image, and the mapping has been
138 > * determined.
139 > */
140 > extern void
141 > adjveil(void)                           /* adjust veil image */
142   {
143 +        float   *crfptr = crfimg;
144 +        COLOR   *fovptr = fovimg;
145 +        COLOR   *veilptr = veilimg;
146 +        double  s2nits = 1./inpexp;
147 +        double  vl, vl2, fovl, vlsum;
148 +        double  deltavc[3];
149 +        int     i, j;
150 +
151 +        if (lumf == rgblum)
152 +                s2nits *= WHTEFFICACY;
153 +
154 +        for (i = fvxr*fvyr; i--; crfptr++, fovptr++, veilptr++) {
155 +                if (crfptr[0] >= 0.95)
156 +                        continue;
157 +                vl = plum(veilptr[0]);
158 +                fovl = (plum(fovptr[0]) - vl) * (1./(1.-VADAPT));
159 +                if (vl <= 0.05*fovl)
160 +                        continue;
161 +                vlsum = vl;
162 +                for (j = 2; j < 11; j++) {
163 +                        vlsum += crfptr[0]*vl - (1.0 - crfptr[0])*fovl;
164 +                        vl2 = vlsum / (double)j;
165 +                        if (vl2 < 0.0)
166 +                                vl2 = 0.0;
167 +                        crfptr[0] = crfactor(fovl + vl2);
168 +                }
169 +                /* desaturation code causes color fringes at this level */
170 +                for (j = 3; j--; ) {
171 +                        double  vc = colval(veilptr[0],j);
172 +                        double  fovc = (colval(fovptr[0],j) - vc) *
173 +                                                (1./(1.-VADAPT));
174 +                        deltavc[j] = (1.-crfptr[0])*(fovl/s2nits - fovc);
175 +                        if (vc + deltavc[j] < 0.0)
176 +                                break;
177 +                }
178 +                if (j < 0)
179 +                        addcolor(veilptr[0], deltavc);
180 +                else
181 +                        scalecolor(veilptr[0], vl2/vl);
182 +        }
183 +        smoothveil();                   /* smooth our result */
184 + }
185 +
186 +
187 + static void
188 + smoothveil(void)                                /* smooth veil image */
189 + {
190 +        COLOR   *nveilimg;
191 +        COLOR   *ovptr, *nvptr;
192 +        int     x, y, i;
193 +
194 +        nveilimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR));
195 +        if (nveilimg == NULL)
196 +                return;
197 +        for (y = 1; y < fvyr-1; y++) {
198 +                ovptr = veilimg + y*fvxr + 1;
199 +                nvptr = nveilimg + y*fvxr + 1;
200 +                for (x = 1; x < fvxr-1; x++, ovptr++, nvptr++)
201 +                        for (i = 3; i--; )
202 +                                nvptr[0][i] = 0.5 * ovptr[0][i]
203 +                                        + (1./12.) *
204 +                                (ovptr[-1][i] + ovptr[-fvxr][i] +
205 +                                        ovptr[1][i] + ovptr[fvxr][i])
206 +                                        + (1./24.) *
207 +                                (ovptr[-fvxr-1][i] + ovptr[-fvxr+1][i] +
208 +                                        ovptr[fvxr-1][i] + ovptr[fvxr+1][i]);
209 +        }
210 +        ovptr = veilimg + 1;
211 +        nvptr = nveilimg + 1;
212 +        for (x = 1; x < fvxr-1; x++, ovptr++, nvptr++)
213 +                for (i = 3; i--; )
214 +                        nvptr[0][i] = 0.5 * ovptr[0][i]
215 +                                + (1./9.) *
216 +                        (ovptr[-1][i] + ovptr[1][i] + ovptr[fvxr][i])
217 +                                + (1./12.) *
218 +                        (ovptr[fvxr-1][i] + ovptr[fvxr+1][i]);
219 +        ovptr = veilimg + (fvyr-1)*fvxr + 1;
220 +        nvptr = nveilimg + (fvyr-1)*fvxr + 1;
221 +        for (x = 1; x < fvxr-1; x++, ovptr++, nvptr++)
222 +                for (i = 3; i--; )
223 +                        nvptr[0][i] = 0.5 * ovptr[0][i]
224 +                                + (1./9.) *
225 +                        (ovptr[-1][i] + ovptr[1][i] + ovptr[-fvxr][i])
226 +                                + (1./12.) *
227 +                        (ovptr[-fvxr-1][i] + ovptr[-fvxr+1][i]);
228 +        ovptr = veilimg + fvxr;
229 +        nvptr = nveilimg + fvxr;
230 +        for (y = 1; y < fvyr-1; y++, ovptr += fvxr, nvptr += fvxr)
231 +                for (i = 3; i--; )
232 +                        nvptr[0][i] = 0.5 * ovptr[0][i]
233 +                                + (1./9.) *
234 +                        (ovptr[-fvxr][i] + ovptr[1][i] + ovptr[fvxr][i])
235 +                                + (1./12.) *
236 +                        (ovptr[-fvxr+1][i] + ovptr[fvxr+1][i]);
237 +        ovptr = veilimg + fvxr - 1;
238 +        nvptr = nveilimg + fvxr - 1;
239 +        for (y = 1; y < fvyr-1; y++, ovptr += fvxr, nvptr += fvxr)
240 +                for (i = 3; i--; )
241 +                        nvptr[0][i] = 0.5 * ovptr[0][i]
242 +                                + (1./9.) *
243 +                        (ovptr[-fvxr][i] + ovptr[-1][i] + ovptr[fvxr][i])
244 +                                + (1./12.) *
245 +                        (ovptr[-fvxr-1][i] + ovptr[fvxr-1][i]);
246 +        for (i = 3; i--; ) {
247 +                nveilimg[0][i] = veilimg[0][i];
248 +                nveilimg[fvxr-1][i] = veilimg[fvxr-1][i];
249 +                nveilimg[(fvyr-1)*fvxr][i] = veilimg[(fvyr-1)*fvxr][i];
250 +                nveilimg[fvyr*fvxr-1][i] = veilimg[fvyr*fvxr-1][i];
251 +        }
252 +        free((void *)veilimg);
253 +        veilimg = nveilimg;
254 + }
255 + #endif
256 +
257 + extern void
258 + addveil(                                /* add veil to scanline */
259 +        COLOR   *sl,
260 +        int     y
261 + )
262 + {
263          int     vx, vy;
264          double  dx, dy;
265          double  lv, uv;
# Line 158 | Line 298 | typedef struct {
298  
299   SCANBAR *rootbar;               /* root scan bar (lowest resolution) */
300  
301 < float   *inpacuD;               /* input acuity data (cycles/degree) */
301 > float   *inpacuD = NULL;        /* input acuity data (cycles/degree) */
302  
303   #define tsampr(x,y)     inpacuD[(y)*fvxr+(x)]
304  
305 + static COLOR * getascan(SCANBAR *sb, int        y);
306 + static void acusample(COLOR     col, int        x, int  y, double       sr);
307 + static void ascanval(COLOR      col, int        x, int  y, SCANBAR      *sb);
308 + static SCANBAR  *sballoc(int    se, int ns, int sl);
309  
310 < double
311 < hacuity(La)                     /* return visual acuity in cycles/degree */
312 < double  La;
313 < {                               /* data due to S. Shaler (we should fit it!) */
314 < #define NPOINTS 20
315 <        static float    l10lum[NPOINTS] = {
316 <                -3.10503,-2.66403,-2.37703,-2.09303,-1.64403,-1.35803,
173 <                -1.07403,-0.67203,-0.38503,-0.10103,0.29397,0.58097,0.86497,
174 <                1.25697,1.54397,1.82797,2.27597,2.56297,2.84697,3.24897
175 <        };
176 <        static float    resfreq[NPOINTS] = {
177 <                2.09,3.28,3.79,4.39,6.11,8.83,10.94,18.66,23.88,31.05,37.42,
178 <                37.68,41.60,43.16,45.30,47.00,48.43,48.32,51.06,51.09
179 <        };
180 <        double  l10La;
181 <        register int    i;
182 <                                        /* check limits */
183 <        if (La <= 7.85e-4)
184 <                return(resfreq[0]);
185 <        if (La >= 1.78e3)
186 <                return(resfreq[NPOINTS-1]);
187 <                                        /* interpolate data */
188 <        l10La = log10(La);
189 <        for (i = 0; i < NPOINTS-2 && l10lum[i+1] <= l10La; i++)
190 <                ;
191 <        return( ( (l10lum[i+1] - l10La)*resfreq[i] +
192 <                        (l10La - l10lum[i])*resfreq[i+1] ) /
193 <                        (l10lum[i+1] - l10lum[i]) );
194 < #undef NPOINTS
310 > extern double
311 > hacuity(                        /* return visual acuity in cycles/degree */
312 >        double  La
313 > )
314 > {
315 >                                        /* functional fit */
316 >        return(17.25*atan(1.4*log10(La) + 0.35) + 25.72);
317   }
318  
319  
320 < COLOR *
321 < getascan(sb, y)                 /* find/read scanline y for scanbar sb */
322 < register SCANBAR        *sb;
323 < int     y;
320 > static COLOR *
321 > getascan(                       /* find/read scanline y for scanbar sb */
322 >        register SCANBAR        *sb,
323 >        int     y
324 > )
325   {
326          register COLOR  *sl0, *sl1, *mysl;
327          register int    i;
# Line 231 | Line 354 | int    y;
354   }
355  
356  
357 < acuscan(scln, y)                /* get acuity-sampled scanline */
358 < COLOR   *scln;
359 < int     y;
357 > extern void
358 > acuscan(                /* get acuity-sampled scanline */
359 >        COLOR   *scln,
360 >        int     y
361 > )
362   {
363          double  sr;
364          double  dx, dy;
365          int     ix, iy;
366          register int    x;
367 +        
368 +        if (inpacuD == NULL)
369 +                return;
370                                          /* compute foveal y position */
371          iy = dy = (y+.5)/numscans(&inpres)*fvyr - .5;
372          while (iy >= fvyr-1) iy--;
# Line 257 | Line 385 | int    y;
385   }
386  
387  
388 < acusample(col, x, y, sr)        /* interpolate sample at (x,y) using rate sr */
389 < COLOR   col;
390 < int     x, y;
391 < double  sr;
388 > static void
389 > acusample(      /* interpolate sample at (x,y) using rate sr */
390 >        COLOR   col,
391 >        int     x,
392 >        int     y,
393 >        double  sr
394 > )
395   {
396          COLOR   c1;
397          double  d;
# Line 279 | Line 410 | double sr;
410   }
411  
412  
413 < ascanval(col, x, y, sb)         /* interpolate scanbar at orig. coords (x,y) */
414 < COLOR   col;
415 < int     x, y;
416 < SCANBAR *sb;
413 > static void
414 > ascanval(               /* interpolate scanbar at orig. coords (x,y) */
415 >        COLOR   col,
416 >        int     x,
417 >        int     y,
418 >        SCANBAR *sb
419 > )
420   {
421          COLOR   *sl0, *sl1, c1, c1y;
422          double  dx, dy;
# Line 303 | Line 437 | SCANBAR        *sb;
437                                          /* get scanlines */
438          sl0 = getascan(sb, iy);
439   #ifdef DEBUG
440 <        if (sl0 == NULL) {
441 <                fprintf(stderr, "%s: internal - cannot backspace in ascanval\n",
308 <                                progname);
309 <                abort();
310 <        }
440 >        if (sl0 == NULL)
441 >                error(INTERNAL, "cannot backspace in ascanval");
442   #endif
443          sl1 = getascan(sb, iy+1);
444                                          /* 2D linear interpolation */
# Line 330 | Line 461 | SCANBAR        *sb;
461   }
462  
463  
464 < SCANBAR *
465 < sballoc(se, ns, sl)             /* allocate scanbar */
466 < int     se;             /* sampling rate exponent */
467 < int     ns;             /* number of scanlines */
468 < int     sl;             /* original scanline length */
464 > static SCANBAR  *
465 > sballoc(                /* allocate scanbar */
466 >        int     se,             /* sampling rate exponent */
467 >        int     ns,             /* number of scanlines */
468 >        int     sl              /* original scanline length */
469 > )
470   {
471          SCANBAR *sbarr;
472          register SCANBAR        *sb;
# Line 343 | Line 475 | int    sl;             /* original scanline length */
475          if (sb == NULL)
476                  syserror("malloc");
477          do {
346                sb->sampe = se;
478                  sb->len = sl>>se;
479 +                if (sb->len <= 0)
480 +                        continue;
481 +                sb->sampe = se;
482                  sb->nscans = ns;
483                  sb->sdata = (COLOR *)malloc(sb->len*ns*sizeof(COLOR));
484                  if (sb->sdata == NULL)
# Line 357 | Line 491 | int    sl;             /* original scanline length */
491   }
492  
493  
494 < initacuity()                    /* initialize variable acuity sampling */
494 > extern void
495 > initacuity(void)                        /* initialize variable acuity sampling */
496   {
497          FVECT   diffx, diffy, cp;
498          double  omega, maxsr;
499          register int    x, y, i;
500 <
500 >        
501          compraydir();                   /* compute ray directions */
502  
503          inpacuD = (float *)malloc(fvxr*fvyr*sizeof(float));
# Line 381 | Line 516 | initacuity()                   /* initialize variable acuity sampling
516                          }
517                          fcross(cp, diffx, diffy);
518                          omega = 0.5 * sqrt(DOT(cp,cp));
519 <                        if (omega <= FTINY)
519 >                        if (omega <= FTINY*FTINY)
520                                  tsampr(x,y) = 1.;
521                          else if ((tsampr(x,y) = PI/180. / sqrt(omega) /
522                                          hacuity(plum(fovscan(y)[x]))) > maxsr)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines