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.25 by greg, Thu May 11 00:58:48 2006 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 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 232 | Line 233 | int            len
233   )
234   {
235          static const char funcName[] = "tmCvGrays";
235        double  d;
236          int     i;
237  
238          if (tms == NULL)
# Line 364 | 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 375 | 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 392 | 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 420 | Line 420 | double La
420   }
421  
422  
423 static int
424 tmNewMap(                       /* allocate new tone-mapping array */
425 TMstruct        *tms
426 )
427 {
428        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
429                                        (tms->hbrmax - tms->hbrmin)) {
430                free((MEM_PTR)tms->lumap);
431                tms->lumap = NULL;
432        }
433        tms->mbrmin = tms->hbrmin;
434        tms->mbrmax = tms->hbrmax;
435        if (tms->mbrmin > tms->mbrmax)
436                return 0;
437        if (tms->lumap == NULL)
438                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
439                                        (tms->mbrmax-tms->mbrmin+1));
440        return(tms->lumap != NULL);
441 }
442
443
423   int
424   tmFixedMapping(                 /* compute fixed, linear tone-mapping */
425   TMstruct        *tms,
# Line 459 | 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 495 | 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 );
512                                        /* allocate space for mapping */
513        if (!tmNewMap(tms))
514                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 554 | 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 613 | Line 595 | int    len
595   }
596  
597  
616
617
598   TMstruct *
599   tmDup(                          /* duplicate top tone mapping */
600   TMstruct        *tms
# Line 631 | 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 -
635 <                                (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 690 | 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