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.23 by greg, Wed May 10 15:21:58 2006 UTC vs.
Revision 3.35 by greg, Mon Mar 7 20:49:19 2011 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 23 | Line 24 | struct tmPackage       *tmPkg[TM_MAXPKG];
24   int     tmNumPkgs = 0;                  /* number of registered packages */
25  
26                                          /* luminance->brightness lookup */
27 < static TMbright         *tmFloat2BrtLUT;
27 > static TMbright         *tmFloat2BrtLUT = NULL;
28  
29   #define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15]
30  
# Line 53 | 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 116 | 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 179 | Line 180 | double lum
180   #endif
181                  return(TM_NOBRT);
182          d = TM_BRTSCALE*log(lum);
183 <        if (d > 0.)
183 <                return((TMbright)(d+.5));
184 <        return((TMbright)(d-.5));
183 >        return((TMbright)(d + .5 - (d < 0.)));
184   }
185  
186  
# Line 232 | Line 231 | int            len
231   )
232   {
233          static const char funcName[] = "tmCvGrays";
235        double  d;
234          int     i;
235  
236          if (tms == NULL)
237                  returnErr(TM_E_TMINVAL);
238          if ((ls == NULL) | (scan == NULL) | (len < 0))
239                  returnErr(TM_E_ILLEGAL);
240 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
241 +                tmCvLums(NULL, NULL, 0);
242          for (i = len; i--; ) {
243                  float   lum = tms->inpsf * scan[i];
244                  if (lum <= TM_NOLUM)
# Line 260 | Line 260 | int    len
260   )
261   {
262          static const char funcName[] = "tmCvColors";
263        static COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
263          static BYTE     gamtab[1024];
264          static double   curgam = .0;
265          COLOR   cmon;
# Line 271 | Line 270 | int    len
270                  returnErr(TM_E_TMINVAL);
271          if ((ls == NULL) | (scan == NULL) | (len < 0))
272                  returnErr(TM_E_ILLEGAL);
273 <        tmCvLums(NULL, NULL, 0);                        /* initialize */
273 >        if (tmFloat2BrtLUT == NULL)                     /* initialize */
274 >                tmCvLums(NULL, NULL, 0);
275          if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
276                  curgam = tms->mongam;                   /* (re)build table */
277                  for (i = 1024; i--; )
# Line 298 | Line 298 | int    len
298                  lum =   tms->clf[RED]*cmon[RED] +
299                          tms->clf[GRN]*cmon[GRN] +
300                          tms->clf[BLU]*cmon[BLU] ;
301 <                if (lum <= TM_NOLUM)                    /* convert brightness */
301 >                if (lum <= TM_NOLUM) {                  /* convert brightness */
302 >                        lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM;
303                          ls[i] = TM_NOBRT;
304 <                else
304 >                } else
305                          ls[i] = tmCvLumLUfp(&lum);
306                  if (cs == TM_NOCHROM)                   /* no color? */
307                          continue;
308                  if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
309                          slum = scotlum(cmon);           /* mesopic adj. */
310 <                        if (lum < LMESLOWER)
310 >                        if (lum < LMESLOWER) {
311                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
312 <                        else {
312 >                        } else {
313                                  d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
314                                  if (tms->flags & TM_F_BW)
315                                          cmon[RED] = cmon[GRN] =
# Line 361 | Line 362 | int    wt
362                  tms->hbrmin = tms->hbrmax = ls[i];
363                  oldlen = 0;
364          } else {
365 <                oldorig = (tms->hbrmin-MINBRT)/HISTEP;
366 <                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
365 >                oldorig = HISTI(tms->hbrmin);
366 >                oldlen = HISTI(tms->hbrmax) + 1 - oldorig;
367          }
368          for (i = len; i--; ) {
369                  if ((j = ls[i]) < MINBRT)
# Line 372 | Line 373 | int    wt
373                  else if (j > tms->hbrmax)
374                          tms->hbrmax = j;
375          }
376 <        horig = (tms->hbrmin-MINBRT)/HISTEP;
377 <        hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
376 >        horig = HISTI(tms->hbrmin);
377 >        hlen = HISTI(tms->hbrmax) + 1 - horig;
378          if (hlen > oldlen) {                    /* (re)allocate histogram */
379                  int     *newhist = (int *)calloc(hlen, sizeof(int));
380                  if (newhist == NULL)
# Line 389 | Line 390 | int    wt
390                  returnOK;
391          for (i = len; i--; )                    /* add in new counts */
392                  if (ls[i] >= MINBRT)
393 <                        tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
393 >                        tms->histo[ HISTI(ls[i]) - horig ] += wt;
394          returnOK;
395   }
396  
# Line 417 | Line 418 | double La
418   }
419  
420  
420 static int
421 tmNewMap(                       /* allocate new tone-mapping array */
422 TMstruct        *tms
423 )
424 {
425        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
426                                        (tms->hbrmax - tms->hbrmin)) {
427                free((MEM_PTR)tms->lumap);
428                tms->lumap = NULL;
429        }
430        tms->mbrmin = tms->hbrmin;
431        tms->mbrmax = tms->hbrmax;
432        if (tms->mbrmin > tms->mbrmax)
433                return 0;
434        if (tms->lumap == NULL)
435                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
436                                        (tms->mbrmax-tms->mbrmin+1));
437        return(tms->lumap != NULL);
438 }
439
440
421   int
422   tmFixedMapping(                 /* compute fixed, linear tone-mapping */
423   TMstruct        *tms,
# Line 456 | Line 436 | double gamval
436          if (gamval < MINGAM)
437                  gamval = tms->mongam;
438          d = log(expmult/tms->inpsf);
439 <        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
440 <                tms->lumap[i] = 256. * exp(
439 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
440 >                double  val = 256. * exp(
441                          ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
442 <                        / gamval );
442 >                        / gamval);
443 >                tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val;
444 >        }
445          returnOK;
446   }
447  
# Line 492 | Line 474 | double Ldmax
474          Ldmin = Ldmax/Lddyn;
475          logLddyn = log(Lddyn);
476          Ldavg = sqrt(Ldmax*Ldmin);
477 <        i = (tms->hbrmin-MINBRT)/HISTEP;
478 <        brt0 = MINBRT + HISTEP/2 + i*HISTEP;
479 <        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
477 >        i = HISTI(tms->hbrmin);
478 >        brt0 = HISTV(i);
479 >        histlen = HISTI(tms->hbrmax) + 1 - i;
480                                          /* histogram total and mean */
481          histot = 0; sum = 0;
482          j = brt0 + histlen*HISTEP;
483          for (i = histlen; i--; ) {
484                  histot += tms->histo[i];
485 <                sum += (j -= HISTEP) * tms->histo[i];
485 >                sum += (double)(j -= HISTEP) * tms->histo[i];
486          }
487          threshold = histot*0.005 + .5;
488 <        if (threshold < 4)
488 >        if (!histot)
489                  returnErr(TM_E_TMFAIL);
490          Lwavg = tmLuminance( (double)sum / histot );
509                                        /* allocate space for mapping */
510        if (!tmNewMap(tms))
511                returnErr(TM_E_NOMEM);
491                                          /* use linear tone mapping? */
492 <        if (tms->flags & TM_F_LINEAR)
492 >        if (tms->flags & TM_F_LINEAR || threshold < 4 ||
493 >                        tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn)
494                  goto linearmap;
495                                          /* clamp histogram */
496          histo = (int *)malloc(histlen*sizeof(int));
# Line 551 | Line 531 | double Ldmax
531                          goto linearmap;
532                  }
533          } while (trimmings > threshold);
534 +                                        /* allocate space for mapping */
535 +        if (!tmNewMap(tms))
536 +                returnErr(TM_E_NOMEM);
537                                          /* assign tone-mapping */
538          for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
539                  j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
# Line 610 | Line 593 | int    len
593   }
594  
595  
613
614
596   TMstruct *
597   tmDup(                          /* duplicate top tone mapping */
598   TMstruct        *tms
# Line 628 | Line 609 | TMstruct       *tms
609                  return(NULL);
610          *tmnew = *tms;          /* copy everything */
611          if (tmnew->histo != NULL) {     /* duplicate histogram */
612 <                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
632 <                                (tmnew->hbrmin-MINBRT)/HISTEP;
612 >                len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
613                  tmnew->histo = (int *)malloc(len*sizeof(int));
614                  if (tmnew->histo != NULL)
615                          for (i = len; i--; )
# Line 687 | Line 667 | tmMkMesofact()                         /* build mesopic lookup factor table
667                  tmMesofact[i-BMESLOWER] = 256. *
668                                  (tmLuminance(i) - LMESLOWER) /
669                                  (LMESUPPER - LMESLOWER);
670 + }
671 +
672 +
673 + int
674 + tmNewMap(                       /* allocate new tone-mapping array */
675 + TMstruct        *tms
676 + )
677 + {
678 +        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
679 +                                        (tms->hbrmax - tms->hbrmin)) {
680 +                free((MEM_PTR)tms->lumap);
681 +                tms->lumap = NULL;
682 +        }
683 +        tms->mbrmin = tms->hbrmin;
684 +        tms->mbrmax = tms->hbrmax;
685 +        if (tms->mbrmin > tms->mbrmax)
686 +                return 0;
687 +        if (tms->lumap == NULL)
688 +                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
689 +                                        (tms->mbrmax-tms->mbrmin+1));
690 +        return(tms->lumap != NULL);
691   }
692  
693  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines