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.28 by greg, Wed Aug 9 16:26:54 2006 UTC vs.
Revision 3.33 by greg, Mon Feb 9 20:23:51 2009 UTC

# Line 363 | Line 363 | int    wt
363                  tms->hbrmin = tms->hbrmax = ls[i];
364                  oldlen = 0;
365          } else {
366 <                oldorig = (tms->hbrmin-MINBRT)/HISTEP;
367 <                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
366 >                oldorig = HISTI(tms->hbrmin);
367 >                oldlen = HISTI(tms->hbrmax) + 1 - oldorig;
368          }
369          for (i = len; i--; ) {
370                  if ((j = ls[i]) < MINBRT)
# Line 374 | Line 374 | int    wt
374                  else if (j > tms->hbrmax)
375                          tms->hbrmax = j;
376          }
377 <        horig = (tms->hbrmin-MINBRT)/HISTEP;
378 <        hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
377 >        horig = HISTI(tms->hbrmin);
378 >        hlen = HISTI(tms->hbrmax) + 1 - horig;
379          if (hlen > oldlen) {                    /* (re)allocate histogram */
380                  int     *newhist = (int *)calloc(hlen, sizeof(int));
381                  if (newhist == NULL)
# Line 391 | Line 391 | int    wt
391                  returnOK;
392          for (i = len; i--; )                    /* add in new counts */
393                  if (ls[i] >= MINBRT)
394 <                        tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
394 >                        tms->histo[ HISTI(ls[i]) - horig ] += wt;
395          returnOK;
396   }
397  
# Line 419 | Line 419 | double La
419   }
420  
421  
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
422   int
423   tmFixedMapping(                 /* compute fixed, linear tone-mapping */
424   TMstruct        *tms,
# Line 496 | Line 475 | double Ldmax
475          Ldmin = Ldmax/Lddyn;
476          logLddyn = log(Lddyn);
477          Ldavg = sqrt(Ldmax*Ldmin);
478 <        i = (tms->hbrmin-MINBRT)/HISTEP;
479 <        brt0 = MINBRT + HISTEP/2 + i*HISTEP;
480 <        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
478 >        i = HISTI(tms->hbrmin);
479 >        brt0 = HISTV(i);
480 >        histlen = HISTI(tms->hbrmax) + 1 - i;
481                                          /* histogram total and mean */
482          histot = 0; sum = 0;
483          j = brt0 + histlen*HISTEP;
484          for (i = histlen; i--; ) {
485                  histot += tms->histo[i];
486 <                sum += (j -= HISTEP) * tms->histo[i];
486 >                sum += (double)(j -= HISTEP) * tms->histo[i];
487          }
488          threshold = histot*0.005 + .5;
489 <        if (threshold < 4)
489 >        if (!histot)
490                  returnErr(TM_E_TMFAIL);
491          Lwavg = tmLuminance( (double)sum / histot );
513                                        /* allocate space for mapping */
514        if (!tmNewMap(tms))
515                returnErr(TM_E_NOMEM);
492                                          /* use linear tone mapping? */
493 <        if (tms->flags & TM_F_LINEAR)
493 >        if (tms->flags & TM_F_LINEAR || threshold < 4 ||
494 >                        tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn)
495                  goto linearmap;
496                                          /* clamp histogram */
497          histo = (int *)malloc(histlen*sizeof(int));
# Line 555 | Line 532 | double Ldmax
532                          goto linearmap;
533                  }
534          } while (trimmings > threshold);
535 +                                        /* allocate space for mapping */
536 +        if (!tmNewMap(tms))
537 +                returnErr(TM_E_NOMEM);
538                                          /* assign tone-mapping */
539          for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
540                  j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
# Line 614 | Line 594 | int    len
594   }
595  
596  
617
618
597   TMstruct *
598   tmDup(                          /* duplicate top tone mapping */
599   TMstruct        *tms
# Line 632 | Line 610 | TMstruct       *tms
610                  return(NULL);
611          *tmnew = *tms;          /* copy everything */
612          if (tmnew->histo != NULL) {     /* duplicate histogram */
613 <                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
636 <                                (tmnew->hbrmin-MINBRT)/HISTEP;
613 >                len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
614                  tmnew->histo = (int *)malloc(len*sizeof(int));
615                  if (tmnew->histo != NULL)
616                          for (i = len; i--; )
# Line 691 | Line 668 | tmMkMesofact()                         /* build mesopic lookup factor table
668                  tmMesofact[i-BMESLOWER] = 256. *
669                                  (tmLuminance(i) - LMESLOWER) /
670                                  (LMESUPPER - LMESLOWER);
671 + }
672 +
673 +
674 + int
675 + tmNewMap(                       /* allocate new tone-mapping array */
676 + TMstruct        *tms
677 + )
678 + {
679 +        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
680 +                                        (tms->hbrmax - tms->hbrmin)) {
681 +                free((MEM_PTR)tms->lumap);
682 +                tms->lumap = NULL;
683 +        }
684 +        tms->mbrmin = tms->hbrmin;
685 +        tms->mbrmax = tms->hbrmax;
686 +        if (tms->mbrmin > tms->mbrmax)
687 +                return 0;
688 +        if (tms->lumap == NULL)
689 +                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
690 +                                        (tms->mbrmax-tms->mbrmin+1));
691 +        return(tms->lumap != NULL);
692   }
693  
694  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines