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.17 by greg, Fri Jan 7 21:41:06 2005 UTC vs.
Revision 3.34 by greg, Mon Feb 9 20:48:08 2009 UTC

# Line 12 | Line 12 | static const char      RCSid[] = "$Id$";
12   #include "copyright.h"
13  
14   #include        <stdio.h>
15 + #include        <stdlib.h>
16   #include        <math.h>
17   #include        "tmprivat.h"
18   #include        "tmerrmsg.h"
# Line 22 | Line 23 | static const char      RCSid[] = "$Id$";
23   struct tmPackage        *tmPkg[TM_MAXPKG];
24   int     tmNumPkgs = 0;                  /* number of registered packages */
25  
26 +                                        /* luminance->brightness lookup */
27 + static TMbright         *tmFloat2BrtLUT = NULL;
28  
29 + #define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15]
30 +
31 +
32   TMstruct *
33   tmInit(                                 /* initialize new tone mapping */
34   int     flags,
# Line 48 | Line 54 | double gamval
54                  tmnew->clf[GRN] = rgb2xyzmat[1][1];
55                  tmnew->clf[BLU] = rgb2xyzmat[1][2];
56          } else {
57 <                comprgb2xyzWBmat(cmat, tmnew->monpri=monpri);
57 >                comprgb2xyzmat(cmat, tmnew->monpri=monpri);
58                  tmnew->clf[RED] = cmat[1][0];
59                  tmnew->clf[GRN] = cmat[1][1];
60                  tmnew->clf[BLU] = cmat[1][2];
# Line 68 | Line 74 | double gamval
74                          tmnew->inpsf = WHTEFFICACY;
75          tmnew->cmat[0][1] = tmnew->cmat[0][2] = tmnew->cmat[1][0] =
76          tmnew->cmat[1][2] = tmnew->cmat[2][0] = tmnew->cmat[2][1] = 0.;
77 +        tmnew->inpdat = NULL;
78          tmnew->hbrmin = 10; tmnew->hbrmax = -10;
79          tmnew->histo = NULL;
80          tmnew->mbrmin = 10; tmnew->mbrmax = -10;
# Line 86 | Line 93 | int
93   tmSetSpace(                     /* set input color space for conversions */
94   TMstruct        *tms,
95   RGBPRIMP        pri,
96 < double  sf
96 > double  sf,
97 > MEM_PTR dat
98   )
99   {
100          static const char funcName[] = "tmSetSpace";
# Line 97 | Line 105 | double sf
105          if (sf <= 1e-12)
106                  returnErr(TM_E_ILLEGAL);
107                                                  /* check if no change */
108 <        if (pri == tms->inppri && FEQ(sf, tms->inpsf))
108 >        if (pri == tms->inppri && FEQ(sf, tms->inpsf) && dat == tms->inpdat)
109                  returnOK;
110          tms->inppri = pri;                      /* let's set it */
111          tms->inpsf = sf;
112 +        tms->inpdat = dat;
113  
114          if (tms->flags & TM_F_BW) {             /* color doesn't matter */
115                  tms->monpri = tms->inppri;              /* eliminate xform */
# Line 108 | Line 117 | double sf
117                          tms->clf[CIEX] = tms->clf[CIEZ] = 0.;
118                          tms->clf[CIEY] = 1.;
119                  } else {
120 <                        comprgb2xyzWBmat(tms->cmat, tms->monpri);
120 >                        comprgb2xyzmat(tms->cmat, tms->monpri);
121                          tms->clf[RED] = tms->cmat[1][0];
122                          tms->clf[GRN] = tms->cmat[1][1];
123                          tms->clf[BLU] = tms->cmat[1][2];
# Line 146 | Line 155 | double sf
155  
156  
157   void
158 < tmClearHisto(                   /* clear current histogram */
158 > tmClearHisto(                           /* clear current histogram */
159   TMstruct        *tms
160   )
161   {
# Line 157 | Line 166 | TMstruct       *tms
166   }
167  
168  
169 + TMbright
170 + tmCvLuminance(                          /* convert a single luminance */
171 + double  lum
172 + )
173 + {
174 +        double  d;
175 +
176 + #ifdef isfinite
177 +        if (!isfinite(lum) || lum <= TM_NOLUM)
178 + #else
179 +        if (lum <= TM_NOLUM)
180 + #endif
181 +                return(TM_NOBRT);
182 +        d = TM_BRTSCALE*log(lum);
183 +        if (d > 0.)
184 +                return((TMbright)(d+.5));
185 +        return((TMbright)(d-.5));
186 + }
187 +
188 +
189   int
190 + tmCvLums(                               /* convert luminances using lookup */
191 + TMbright        *ls,
192 + float           *scan,
193 + int             len
194 + )
195 + {
196 +        if (tmFloat2BrtLUT == NULL) {   /* initialize lookup table */
197 +                int32   i;
198 +                tmFloat2BrtLUT = (TMbright *)malloc(sizeof(TMbright)*0x10000);
199 +                if (tmFloat2BrtLUT == NULL)
200 +                        return(TM_E_NOMEM);
201 +                for (i = 0; i < 0x10000; i++) {
202 +                        int32   l = (i<<1 | 1) << 14;
203 + #ifndef isfinite
204 +                        if ((l & 0x7f800000) == 0x7f800000)
205 +                                tmFloat2BrtLUT[i] = TM_NOBRT;
206 +                        else
207 + #endif
208 +                        tmFloat2BrtLUT[i] = tmCvLuminance(*(float *)&l);
209 +                }
210 +        }
211 +        if (len <= 0)
212 +                return(TM_E_OK);
213 +        if ((ls == NULL) | (scan == NULL))
214 +                return(TM_E_ILLEGAL);
215 +        while (len--) {
216 +                if (*scan <= TM_NOLUM) {
217 +                        *ls++ = TM_NOBRT;
218 +                        ++scan;
219 +                        continue;
220 +                }
221 +                *ls++ = tmCvLumLUfp(scan++);
222 +        }
223 +        return(TM_E_OK);
224 + }
225 +
226 +
227 + int
228 + tmCvGrays(                              /* convert float gray values */
229 + TMstruct        *tms,
230 + TMbright        *ls,
231 + float           *scan,
232 + int             len
233 + )
234 + {
235 +        static const char funcName[] = "tmCvGrays";
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 167 | 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;
173 <        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 185 | 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] ;
197 <                if (lum < MINLUM) {
198 <                        ls[i] = MINBRT-1;               /* bogus value */
199 <                        lum = MINLUM;
200 <                } else {
201 <                        d = TM_BRTSCALE*log(lum);       /* encode it */
202 <                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
203 <                }
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 223 | 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 :
227 <                                (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 :
230 <                                (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 :
233 <                                (int)(256.*pow(d, 1./tms->mongam));
334 >                cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
335          }
336          returnOK;
337   }
338  
339  
340   int
240 tmCvGrays(                              /* convert float gray values */
241 TMstruct        *tms,
242 TMbright        *ls,
243 float   *scan,
244 int     len
245 )
246 {
247        static const char funcName[] = "tmCvGrays";
248        double  d;
249        int     i;
250
251        if (tms == NULL)
252                returnErr(TM_E_TMINVAL);
253        if ((ls == NULL) | (scan == NULL) | (len < 0))
254                returnErr(TM_E_ILLEGAL);
255        for (i = len; i--; )
256                if (scan[i] <= TM_NOLUM) {
257                        ls[i] = TM_NOBRT;               /* bogus value */
258                } else {
259                        d = TM_BRTSCALE*log(scan[i]);   /* encode it */
260                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
261                }
262        returnOK;
263 }
264
265
266 int
341   tmAddHisto(                             /* add values to histogram */
342   TMstruct        *tms,
343   TMbright        *ls,
# Line 290 | Line 364 | int    wt
364                  tms->hbrmin = tms->hbrmax = ls[i];
365                  oldlen = 0;
366          } else {
367 <                oldorig = (tms->hbrmin-MINBRT)/HISTEP;
368 <                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
367 >                oldorig = HISTI(tms->hbrmin);
368 >                oldlen = HISTI(tms->hbrmax) + 1 - oldorig;
369          }
370          for (i = len; i--; ) {
371                  if ((j = ls[i]) < MINBRT)
# Line 301 | Line 375 | int    wt
375                  else if (j > tms->hbrmax)
376                          tms->hbrmax = j;
377          }
378 <        horig = (tms->hbrmin-MINBRT)/HISTEP;
379 <        hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
378 >        horig = HISTI(tms->hbrmin);
379 >        hlen = HISTI(tms->hbrmax) + 1 - horig;
380          if (hlen > oldlen) {                    /* (re)allocate histogram */
381                  int     *newhist = (int *)calloc(hlen, sizeof(int));
382                  if (newhist == NULL)
# Line 318 | Line 392 | int    wt
392                  returnOK;
393          for (i = len; i--; )                    /* add in new counts */
394                  if (ls[i] >= MINBRT)
395 <                        tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
395 >                        tms->histo[ HISTI(ls[i]) - horig ] += wt;
396          returnOK;
397   }
398  
# Line 346 | Line 420 | double La
420   }
421  
422  
349 static int
350 tmNewMap(                       /* allocate new tone-mapping array */
351 TMstruct        *tms
352 )
353 {
354        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
355                                        (tms->hbrmax - tms->hbrmin)) {
356                free((MEM_PTR)tms->lumap);
357                tms->lumap = NULL;
358        }
359        tms->mbrmin = tms->hbrmin;
360        tms->mbrmax = tms->hbrmax;
361        if (tms->mbrmin > tms->mbrmax)
362                return 0;
363        if (tms->lumap == NULL)
364                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
365                                        (tms->mbrmax-tms->mbrmin+1));
366        return(tms->lumap != NULL);
367 }
368
369
423   int
424   tmFixedMapping(                 /* compute fixed, linear tone-mapping */
425   TMstruct        *tms,
# Line 385 | Line 438 | double gamval
438          if (gamval < MINGAM)
439                  gamval = tms->mongam;
440          d = log(expmult/tms->inpsf);
441 <        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
442 <                tms->lumap[i] = 256. * exp(
441 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
442 >                double  val = 256. * exp(
443                          ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
444 <                        / gamval );
444 >                        / gamval);
445 >                tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val;
446 >        }
447          returnOK;
448   }
449  
# Line 421 | Line 476 | double Ldmax
476          Ldmin = Ldmax/Lddyn;
477          logLddyn = log(Lddyn);
478          Ldavg = sqrt(Ldmax*Ldmin);
479 <        i = (tms->hbrmin-MINBRT)/HISTEP;
480 <        brt0 = MINBRT + HISTEP/2 + i*HISTEP;
481 <        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
479 >        i = HISTI(tms->hbrmin);
480 >        brt0 = HISTV(i);
481 >        histlen = HISTI(tms->hbrmax) + 1 - i;
482                                          /* histogram total and mean */
483          histot = 0; sum = 0;
484          j = brt0 + histlen*HISTEP;
485          for (i = histlen; i--; ) {
486                  histot += tms->histo[i];
487 <                sum += (j -= HISTEP) * tms->histo[i];
487 >                sum += (double)(j -= HISTEP) * tms->histo[i];
488          }
489          threshold = histot*0.005 + .5;
490 <        if (threshold < 4)
490 >        if (!histot)
491                  returnErr(TM_E_TMFAIL);
492          Lwavg = tmLuminance( (double)sum / histot );
438                                        /* allocate space for mapping */
439        if (!tmNewMap(tms))
440                returnErr(TM_E_NOMEM);
493                                          /* use linear tone mapping? */
494 <        if (tms->flags & TM_F_LINEAR)
494 >        if (tms->flags & TM_F_LINEAR || threshold < 4 ||
495 >                        tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn)
496                  goto linearmap;
497                                          /* clamp histogram */
498          histo = (int *)malloc(histlen*sizeof(int));
# Line 480 | Line 533 | double Ldmax
533                          goto linearmap;
534                  }
535          } while (trimmings > threshold);
536 +                                        /* allocate space for mapping */
537 +        if (!tmNewMap(tms))
538 +                returnErr(TM_E_NOMEM);
539                                          /* assign tone-mapping */
540          for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
541                  j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
# Line 539 | Line 595 | int    len
595   }
596  
597  
542
543
598   TMstruct *
599   tmDup(                          /* duplicate top tone mapping */
600   TMstruct        *tms
# Line 557 | Line 611 | TMstruct       *tms
611                  return(NULL);
612          *tmnew = *tms;          /* copy everything */
613          if (tmnew->histo != NULL) {     /* duplicate histogram */
614 <                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
561 <                                (tmnew->hbrmin-MINBRT)/HISTEP;
614 >                len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
615                  tmnew->histo = (int *)malloc(len*sizeof(int));
616                  if (tmnew->histo != NULL)
617                          for (i = len; i--; )
# Line 616 | Line 669 | tmMkMesofact()                         /* build mesopic lookup factor table
669                  tmMesofact[i-BMESLOWER] = 256. *
670                                  (tmLuminance(i) - LMESLOWER) /
671                                  (LMESUPPER - LMESLOWER);
672 + }
673 +
674 +
675 + int
676 + tmNewMap(                       /* allocate new tone-mapping array */
677 + TMstruct        *tms
678 + )
679 + {
680 +        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
681 +                                        (tms->hbrmax - tms->hbrmin)) {
682 +                free((MEM_PTR)tms->lumap);
683 +                tms->lumap = NULL;
684 +        }
685 +        tms->mbrmin = tms->hbrmin;
686 +        tms->mbrmax = tms->hbrmax;
687 +        if (tms->mbrmin > tms->mbrmax)
688 +                return 0;
689 +        if (tms->lumap == NULL)
690 +                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
691 +                                        (tms->mbrmax-tms->mbrmin+1));
692 +        return(tms->lumap != NULL);
693   }
694  
695  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines