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.27 by schorsch, Wed Jun 7 17:52:03 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 363 | 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 374 | 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 391 | 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 419 | Line 420 | double La
420   }
421  
422  
422 static int
423 tmNewMap(                       /* allocate new tone-mapping array */
424 TMstruct        *tms
425 )
426 {
427        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
428                                        (tms->hbrmax - tms->hbrmin)) {
429                free((MEM_PTR)tms->lumap);
430                tms->lumap = NULL;
431        }
432        tms->mbrmin = tms->hbrmin;
433        tms->mbrmax = tms->hbrmax;
434        if (tms->mbrmin > tms->mbrmax)
435                return 0;
436        if (tms->lumap == NULL)
437                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
438                                        (tms->mbrmax-tms->mbrmin+1));
439        return(tms->lumap != NULL);
440 }
441
442
423   int
424   tmFixedMapping(                 /* compute fixed, linear tone-mapping */
425   TMstruct        *tms,
# Line 458 | 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 494 | 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 );
511                                        /* allocate space for mapping */
512        if (!tmNewMap(tms))
513                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 553 | 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 612 | Line 595 | int    len
595   }
596  
597  
615
616
598   TMstruct *
599   tmDup(                          /* duplicate top tone mapping */
600   TMstruct        *tms
# Line 630 | 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 -
634 <                                (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 689 | 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