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

Comparing ray/src/common/tonemap.c (file contents):
Revision 3.18 by greg, Fri Jan 7 22:05:30 2005 UTC vs.
Revision 3.25 by greg, Thu May 11 00:58:48 2006 UTC

# Line 22 | Line 22 | static const char      RCSid[] = "$Id$";
22   struct tmPackage        *tmPkg[TM_MAXPKG];
23   int     tmNumPkgs = 0;                  /* number of registered packages */
24  
25 +                                        /* luminance->brightness lookup */
26 + static TMbright         *tmFloat2BrtLUT = NULL;
27  
28 + #define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15]
29 +
30 +
31   TMstruct *
32   tmInit(                                 /* initialize new tone mapping */
33   int     flags,
# Line 149 | Line 154 | MEM_PTR        dat
154  
155  
156   void
157 < tmClearHisto(                   /* clear current histogram */
157 > tmClearHisto(                           /* clear current histogram */
158   TMstruct        *tms
159   )
160   {
# Line 160 | Line 165 | TMstruct       *tms
165   }
166  
167  
168 + TMbright
169 + tmCvLuminance(                          /* convert a single luminance */
170 + double  lum
171 + )
172 + {
173 +        double  d;
174 +
175 + #ifdef isfinite
176 +        if (!isfinite(lum) || lum <= TM_NOLUM)
177 + #else
178 +        if (lum <= TM_NOLUM)
179 + #endif
180 +                return(TM_NOBRT);
181 +        d = TM_BRTSCALE*log(lum);
182 +        if (d > 0.)
183 +                return((TMbright)(d+.5));
184 +        return((TMbright)(d-.5));
185 + }
186 +
187 +
188   int
189 + tmCvLums(                               /* convert luminances using lookup */
190 + TMbright        *ls,
191 + float           *scan,
192 + int             len
193 + )
194 + {
195 +        if (tmFloat2BrtLUT == NULL) {   /* initialize lookup table */
196 +                int32   i;
197 +                tmFloat2BrtLUT = (TMbright *)malloc(sizeof(TMbright)*0x10000);
198 +                if (tmFloat2BrtLUT == NULL)
199 +                        return(TM_E_NOMEM);
200 +                for (i = 0; i < 0x10000; i++) {
201 +                        int32   l = (i<<1 | 1) << 14;
202 + #ifndef isfinite
203 +                        if ((l & 0x7f800000) == 0x7f800000)
204 +                                tmFloat2BrtLUT[i] = TM_NOBRT;
205 +                        else
206 + #endif
207 +                        tmFloat2BrtLUT[i] = tmCvLuminance(*(float *)&l);
208 +                }
209 +        }
210 +        if (len <= 0)
211 +                return(TM_E_OK);
212 +        if ((ls == NULL) | (scan == NULL))
213 +                return(TM_E_ILLEGAL);
214 +        while (len--) {
215 +                if (*scan <= TM_NOLUM) {
216 +                        *ls++ = TM_NOBRT;
217 +                        ++scan;
218 +                        continue;
219 +                }
220 +                *ls++ = tmCvLumLUfp(scan++);
221 +        }
222 +        return(TM_E_OK);
223 + }
224 +
225 +
226 + int
227 + tmCvGrays(                              /* convert float gray values */
228 + TMstruct        *tms,
229 + TMbright        *ls,
230 + float           *scan,
231 + int             len
232 + )
233 + {
234 +        static const char funcName[] = "tmCvGrays";
235 +        double  d;
236 +        int     i;
237 +
238 +        if (tms == NULL)
239 +                returnErr(TM_E_TMINVAL);
240 +        if ((ls == NULL) | (scan == NULL) | (len < 0))
241 +                returnErr(TM_E_ILLEGAL);
242 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
243 +                tmCvLums(NULL, NULL, 0);
244 +        for (i = len; i--; ) {
245 +                float   lum = tms->inpsf * scan[i];
246 +                if (lum <= TM_NOLUM)
247 +                        ls[i] = TM_NOBRT;
248 +                else
249 +                        ls[i] = tmCvLumLUfp(&lum);
250 +        }
251 +        returnOK;
252 + }
253 +
254 +
255 + int
256   tmCvColors(                             /* convert float colors */
257   TMstruct        *tms,
258   TMbright        *ls,
# Line 170 | Line 262 | int    len
262   )
263   {
264          static const char funcName[] = "tmCvColors";
265 <        static COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
265 >        static BYTE     gamtab[1024];
266 >        static double   curgam = .0;
267          COLOR   cmon;
268 <        double  lum, slum;
176 <        double  d;
268 >        float   lum, slum, d;
269          int     i;
270  
271          if (tms == NULL)
272                  returnErr(TM_E_TMINVAL);
273          if ((ls == NULL) | (scan == NULL) | (len < 0))
274                  returnErr(TM_E_ILLEGAL);
275 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
276 +                tmCvLums(NULL, NULL, 0);
277 +        if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
278 +                curgam = tms->mongam;                   /* (re)build table */
279 +                for (i = 1024; i--; )
280 +                        gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam));
281 +        }
282          for (i = len; i--; ) {
283                  if (tmNeedMatrix(tms)) {                /* get monitor RGB */
284                          colortrans(cmon, tms->cmat, scan[i]);
# Line 188 | Line 287 | int    len
287                          cmon[GRN] = tms->inpsf*scan[i][GRN];
288                          cmon[BLU] = tms->inpsf*scan[i][BLU];
289                  }
290 + #ifdef isfinite
291 +                if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f;
292 +                if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f;
293 +                if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f;
294 + #else
295 +                if (cmon[RED] < .0f) cmon[RED] = .0f;
296 +                if (cmon[GRN] < .0f) cmon[GRN] = .0f;
297 +                if (cmon[BLU] < .0f) cmon[BLU] = .0f;
298 + #endif
299                                                          /* world luminance */
300                  lum =   tms->clf[RED]*cmon[RED] +
301                          tms->clf[GRN]*cmon[GRN] +
302                          tms->clf[BLU]*cmon[BLU] ;
303 <                                                        /* check range */
304 <                if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
305 <                        lum =   tms->clf[RED]*cmon[RED] +
306 <                                tms->clf[GRN]*cmon[GRN] +
307 <                                tms->clf[BLU]*cmon[BLU] ;
200 <                if (lum < MINLUM) {
201 <                        ls[i] = MINBRT-1;               /* bogus value */
202 <                        lum = MINLUM;
203 <                } else {
204 <                        d = TM_BRTSCALE*log(lum);       /* encode it */
205 <                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
206 <                }
303 >                if (lum <= TM_NOLUM) {                  /* convert brightness */
304 >                        lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM;
305 >                        ls[i] = TM_NOBRT;
306 >                } else
307 >                        ls[i] = tmCvLumLUfp(&lum);
308                  if (cs == TM_NOCHROM)                   /* no color? */
309                          continue;
310                  if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
311                          slum = scotlum(cmon);           /* mesopic adj. */
312 <                        if (lum < LMESLOWER)
312 >                        if (lum < LMESLOWER) {
313                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
314 <                        else {
314 >                        } else {
315                                  d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
316                                  if (tms->flags & TM_F_BW)
317                                          cmon[RED] = cmon[GRN] =
318                                                          cmon[BLU] = d*lum;
319                                  else
320                                          scalecolor(cmon, d);
321 <                                d = (1.-d)*slum;
321 >                                d = (1.f-d)*slum;
322                                  cmon[RED] += d;
323                                  cmon[GRN] += d;
324                                  cmon[BLU] += d;
# Line 226 | Line 327 | int    len
327                          cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
328                  }
329                  d = tms->clf[RED]*cmon[RED]/lum;
330 <                cs[3*i  ] = d>=.999 ? 255 :
230 <                                (int)(256.*pow(d, 1./tms->mongam));
330 >                cs[3*i  ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
331                  d = tms->clf[GRN]*cmon[GRN]/lum;
332 <                cs[3*i+1] = d>=.999 ? 255 :
233 <                                (int)(256.*pow(d, 1./tms->mongam));
332 >                cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
333                  d = tms->clf[BLU]*cmon[BLU]/lum;
334 <                cs[3*i+2] = d>=.999 ? 255 :
236 <                                (int)(256.*pow(d, 1./tms->mongam));
334 >                cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
335          }
238        returnOK;
239 }
240
241
242 int
243 tmCvGrays(                              /* convert float gray values */
244 TMstruct        *tms,
245 TMbright        *ls,
246 float   *scan,
247 int     len
248 )
249 {
250        static const char funcName[] = "tmCvGrays";
251        double  d;
252        int     i;
253
254        if (tms == NULL)
255                returnErr(TM_E_TMINVAL);
256        if ((ls == NULL) | (scan == NULL) | (len < 0))
257                returnErr(TM_E_ILLEGAL);
258        for (i = len; i--; )
259                if (scan[i] <= TM_NOLUM) {
260                        ls[i] = TM_NOBRT;               /* bogus value */
261                } else {
262                        d = TM_BRTSCALE*log(scan[i]);   /* encode it */
263                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
264                }
336          returnOK;
337   }
338  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines