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.19 by greg, Tue Nov 15 06:53:00 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 111 | Line 117 | MEM_PTR        dat
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 149 | Line 155 | MEM_PTR        dat
155  
156  
157   void
158 < tmClearHisto(                   /* clear current histogram */
158 > tmClearHisto(                           /* clear current histogram */
159   TMstruct        *tms
160   )
161   {
# Line 160 | 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 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          }
336          returnOK;
337   }
338  
339  
242 TMbright
243 tmCvLuminance(                          /* convert a single luminance */
244 double  lum
245 )
246 {
247        double  d;
248
249        if (lum <= TM_NOLUM)
250                return(TM_NOBRT);
251        d = TM_BRTSCALE*log(lum);
252        if (d > 0.)
253                return((TMbright)(d+.5));
254        return((TMbright)(d-.5));
255 }
256
257
340   int
259 tmCvGrays(                              /* convert float gray values */
260 TMstruct        *tms,
261 TMbright        *ls,
262 float   *scan,
263 int     len
264 )
265 {
266        static const char funcName[] = "tmCvGrays";
267        double  d;
268        int     i;
269
270        if (tms == NULL)
271                returnErr(TM_E_TMINVAL);
272        if ((ls == NULL) | (scan == NULL) | (len < 0))
273                returnErr(TM_E_ILLEGAL);
274        for (i = len; i--; )
275                if (scan[i] <= TM_NOLUM) {
276                        ls[i] = TM_NOBRT;               /* bogus value */
277                } else {
278                        d = TM_BRTSCALE*log(scan[i]);   /* encode it */
279                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
280                }
281        returnOK;
282 }
283
284
285 int
341   tmAddHisto(                             /* add values to histogram */
342   TMstruct        *tms,
343   TMbright        *ls,
# Line 309 | 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 320 | 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 337 | 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 365 | Line 420 | double La
420   }
421  
422  
368 static int
369 tmNewMap(                       /* allocate new tone-mapping array */
370 TMstruct        *tms
371 )
372 {
373        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
374                                        (tms->hbrmax - tms->hbrmin)) {
375                free((MEM_PTR)tms->lumap);
376                tms->lumap = NULL;
377        }
378        tms->mbrmin = tms->hbrmin;
379        tms->mbrmax = tms->hbrmax;
380        if (tms->mbrmin > tms->mbrmax)
381                return 0;
382        if (tms->lumap == NULL)
383                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
384                                        (tms->mbrmax-tms->mbrmin+1));
385        return(tms->lumap != NULL);
386 }
387
388
423   int
424   tmFixedMapping(                 /* compute fixed, linear tone-mapping */
425   TMstruct        *tms,
# Line 404 | 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 440 | 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 );
457                                        /* allocate space for mapping */
458        if (!tmNewMap(tms))
459                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 499 | 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 558 | Line 595 | int    len
595   }
596  
597  
561
562
598   TMstruct *
599   tmDup(                          /* duplicate top tone mapping */
600   TMstruct        *tms
# Line 576 | 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 -
580 <                                (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 635 | 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