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.36 by greg, Fri Apr 22 14:35:54 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 133 | Line 134 | MEM_PTR        dat
134                  if (tms->inppri != tms->monpri &&
135                                  PRIMEQ(tms->inppri, tms->monpri))
136                          tms->inppri = tms->monpri;      /* no xform */
137 <                comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri);
137 >                if (!comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri))
138 >                        returnErr(TM_E_ILLEGAL);
139          }
140          for (i = 0; i < 3; i++)
141                  for (j = 0; j < 3; j++)
142                          tms->cmat[i][j] *= tms->inpsf;
143                                                  /* set color divisors */
144          for (i = 0; i < 3; i++)
145 <                if (tms->clf[i] > .001)
146 <                        tms->cdiv[i] =
145 <                                256.*pow(tms->clf[i], 1./tms->mongam);
146 <                else
147 <                        tms->cdiv[i] = 1;
145 >                tms->cdiv[i] = 256.*pow(tms->clf[i] < .001 ? .001 :
146 >                                                tms->clf[i], 1./tms->mongam);
147                                                  /* notify packages */
148          for (i = tmNumPkgs; i--; )
149                  if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL)
# Line 179 | Line 178 | double lum
178   #endif
179                  return(TM_NOBRT);
180          d = TM_BRTSCALE*log(lum);
181 <        if (d > 0.)
183 <                return((TMbright)(d+.5));
184 <        return((TMbright)(d-.5));
181 >        return((TMbright)(d + .5 - (d < 0.)));
182   }
183  
184  
# Line 232 | Line 229 | int            len
229   )
230   {
231          static const char funcName[] = "tmCvGrays";
235        double  d;
232          int     i;
233  
234          if (tms == NULL)
235                  returnErr(TM_E_TMINVAL);
236          if ((ls == NULL) | (scan == NULL) | (len < 0))
237                  returnErr(TM_E_ILLEGAL);
238 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
239 +                tmCvLums(NULL, NULL, 0);
240          for (i = len; i--; ) {
241                  float   lum = tms->inpsf * scan[i];
242                  if (lum <= TM_NOLUM)
# Line 260 | Line 258 | int    len
258   )
259   {
260          static const char funcName[] = "tmCvColors";
263        static COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
261          static BYTE     gamtab[1024];
262          static double   curgam = .0;
263          COLOR   cmon;
# Line 271 | Line 268 | int    len
268                  returnErr(TM_E_TMINVAL);
269          if ((ls == NULL) | (scan == NULL) | (len < 0))
270                  returnErr(TM_E_ILLEGAL);
271 <        tmCvLums(NULL, NULL, 0);                        /* initialize */
271 >        if (tmFloat2BrtLUT == NULL)                     /* initialize */
272 >                tmCvLums(NULL, NULL, 0);
273          if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
274                  curgam = tms->mongam;                   /* (re)build table */
275                  for (i = 1024; i--; )
# Line 298 | Line 296 | int    len
296                  lum =   tms->clf[RED]*cmon[RED] +
297                          tms->clf[GRN]*cmon[GRN] +
298                          tms->clf[BLU]*cmon[BLU] ;
299 <                if (lum <= TM_NOLUM)                    /* convert brightness */
299 >                if (lum <= TM_NOLUM) {                  /* convert brightness */
300 >                        lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM;
301                          ls[i] = TM_NOBRT;
302 <                else
302 >                } else
303                          ls[i] = tmCvLumLUfp(&lum);
304                  if (cs == TM_NOCHROM)                   /* no color? */
305                          continue;
306                  if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
307                          slum = scotlum(cmon);           /* mesopic adj. */
308 <                        if (lum < LMESLOWER)
308 >                        if (lum < LMESLOWER) {
309                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
310 <                        else {
310 >                        } else {
311                                  d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
312                                  if (tms->flags & TM_F_BW)
313                                          cmon[RED] = cmon[GRN] =
# Line 361 | Line 360 | int    wt
360                  tms->hbrmin = tms->hbrmax = ls[i];
361                  oldlen = 0;
362          } else {
363 <                oldorig = (tms->hbrmin-MINBRT)/HISTEP;
364 <                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
363 >                oldorig = HISTI(tms->hbrmin);
364 >                oldlen = HISTI(tms->hbrmax) + 1 - oldorig;
365          }
366          for (i = len; i--; ) {
367                  if ((j = ls[i]) < MINBRT)
# Line 372 | Line 371 | int    wt
371                  else if (j > tms->hbrmax)
372                          tms->hbrmax = j;
373          }
374 <        horig = (tms->hbrmin-MINBRT)/HISTEP;
375 <        hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
374 >        horig = HISTI(tms->hbrmin);
375 >        hlen = HISTI(tms->hbrmax) + 1 - horig;
376          if (hlen > oldlen) {                    /* (re)allocate histogram */
377                  int     *newhist = (int *)calloc(hlen, sizeof(int));
378                  if (newhist == NULL)
# Line 389 | Line 388 | int    wt
388                  returnOK;
389          for (i = len; i--; )                    /* add in new counts */
390                  if (ls[i] >= MINBRT)
391 <                        tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
391 >                        tms->histo[ HISTI(ls[i]) - horig ] += wt;
392          returnOK;
393   }
394  
# Line 417 | Line 416 | double La
416   }
417  
418  
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
419   int
420   tmFixedMapping(                 /* compute fixed, linear tone-mapping */
421   TMstruct        *tms,
# Line 456 | Line 434 | double gamval
434          if (gamval < MINGAM)
435                  gamval = tms->mongam;
436          d = log(expmult/tms->inpsf);
437 <        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
438 <                tms->lumap[i] = 256. * exp(
437 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
438 >                double  val = 256. * exp(
439                          ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
440 <                        / gamval );
440 >                        / gamval);
441 >                tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val;
442 >        }
443          returnOK;
444   }
445  
# Line 492 | Line 472 | double Ldmax
472          Ldmin = Ldmax/Lddyn;
473          logLddyn = log(Lddyn);
474          Ldavg = sqrt(Ldmax*Ldmin);
475 <        i = (tms->hbrmin-MINBRT)/HISTEP;
476 <        brt0 = MINBRT + HISTEP/2 + i*HISTEP;
477 <        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
475 >        i = HISTI(tms->hbrmin);
476 >        brt0 = HISTV(i);
477 >        histlen = HISTI(tms->hbrmax) + 1 - i;
478                                          /* histogram total and mean */
479          histot = 0; sum = 0;
480          j = brt0 + histlen*HISTEP;
481          for (i = histlen; i--; ) {
482                  histot += tms->histo[i];
483 <                sum += (j -= HISTEP) * tms->histo[i];
483 >                sum += (double)(j -= HISTEP) * tms->histo[i];
484          }
485          threshold = histot*0.005 + .5;
486 <        if (threshold < 4)
486 >        if (!histot)
487                  returnErr(TM_E_TMFAIL);
488          Lwavg = tmLuminance( (double)sum / histot );
509                                        /* allocate space for mapping */
510        if (!tmNewMap(tms))
511                returnErr(TM_E_NOMEM);
489                                          /* use linear tone mapping? */
490 <        if (tms->flags & TM_F_LINEAR)
490 >        if (tms->flags & TM_F_LINEAR || threshold < 4 ||
491 >                        tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn)
492                  goto linearmap;
493                                          /* clamp histogram */
494          histo = (int *)malloc(histlen*sizeof(int));
# Line 551 | Line 529 | double Ldmax
529                          goto linearmap;
530                  }
531          } while (trimmings > threshold);
532 +                                        /* allocate space for mapping */
533 +        if (!tmNewMap(tms))
534 +                returnErr(TM_E_NOMEM);
535                                          /* assign tone-mapping */
536          for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
537                  j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
# Line 610 | Line 591 | int    len
591   }
592  
593  
613
614
594   TMstruct *
595   tmDup(                          /* duplicate top tone mapping */
596   TMstruct        *tms
# Line 628 | Line 607 | TMstruct       *tms
607                  return(NULL);
608          *tmnew = *tms;          /* copy everything */
609          if (tmnew->histo != NULL) {     /* duplicate histogram */
610 <                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
632 <                                (tmnew->hbrmin-MINBRT)/HISTEP;
610 >                len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
611                  tmnew->histo = (int *)malloc(len*sizeof(int));
612                  if (tmnew->histo != NULL)
613                          for (i = len; i--; )
# Line 687 | Line 665 | tmMkMesofact()                         /* build mesopic lookup factor table
665                  tmMesofact[i-BMESLOWER] = 256. *
666                                  (tmLuminance(i) - LMESLOWER) /
667                                  (LMESUPPER - LMESLOWER);
668 + }
669 +
670 +
671 + int
672 + tmNewMap(                       /* allocate new tone-mapping array */
673 + TMstruct        *tms
674 + )
675 + {
676 +        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
677 +                                        (tms->hbrmax - tms->hbrmin)) {
678 +                free((MEM_PTR)tms->lumap);
679 +                tms->lumap = NULL;
680 +        }
681 +        tms->mbrmin = tms->hbrmin;
682 +        tms->mbrmax = tms->hbrmax;
683 +        if (tms->mbrmin > tms->mbrmax)
684 +                return 0;
685 +        if (tms->lumap == NULL)
686 +                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
687 +                                        (tms->mbrmax-tms->mbrmin+1));
688 +        return(tms->lumap != NULL);
689   }
690  
691  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines