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.31 by greg, Thu Jul 3 03:30:21 2008 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 458 | Line 458 | double gamval
458          if (gamval < MINGAM)
459                  gamval = tms->mongam;
460          d = log(expmult/tms->inpsf);
461 <        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
462 <                tms->lumap[i] = 256. * exp(
461 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
462 >                double  val = 256. * exp(
463                          ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
464 <                        / gamval );
464 >                        / gamval);
465 >                tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val;
466 >        }
467          returnOK;
468   }
469  
# Line 494 | Line 496 | double Ldmax
496          Ldmin = Ldmax/Lddyn;
497          logLddyn = log(Lddyn);
498          Ldavg = sqrt(Ldmax*Ldmin);
499 <        i = (tms->hbrmin-MINBRT)/HISTEP;
499 >        i = HISTI(tms->hbrmin);
500          brt0 = MINBRT + HISTEP/2 + i*HISTEP;
501 <        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
501 >        histlen = HISTI(tms->hbrmax) + 1 - i;
502                                          /* histogram total and mean */
503          histot = 0; sum = 0;
504          j = brt0 + histlen*HISTEP;
505          for (i = histlen; i--; ) {
506                  histot += tms->histo[i];
507 <                sum += (j -= HISTEP) * tms->histo[i];
507 >                sum += (double)(j -= HISTEP) * tms->histo[i];
508          }
509          threshold = histot*0.005 + .5;
510 <        if (threshold < 4)
510 >        if (!histot)
511                  returnErr(TM_E_TMFAIL);
512          Lwavg = tmLuminance( (double)sum / histot );
511                                        /* allocate space for mapping */
512        if (!tmNewMap(tms))
513                returnErr(TM_E_NOMEM);
513                                          /* use linear tone mapping? */
514 <        if (tms->flags & TM_F_LINEAR)
514 >        if (tms->flags & TM_F_LINEAR || threshold < 4 ||
515 >                        tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn)
516                  goto linearmap;
517                                          /* clamp histogram */
518          histo = (int *)malloc(histlen*sizeof(int));
# Line 553 | Line 553 | double Ldmax
553                          goto linearmap;
554                  }
555          } while (trimmings > threshold);
556 +                                        /* allocate space for mapping */
557 +        if (!tmNewMap(tms))
558 +                returnErr(TM_E_NOMEM);
559                                          /* assign tone-mapping */
560          for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
561                  j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
# Line 630 | Line 633 | TMstruct       *tms
633                  return(NULL);
634          *tmnew = *tms;          /* copy everything */
635          if (tmnew->histo != NULL) {     /* duplicate histogram */
636 <                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
634 <                                (tmnew->hbrmin-MINBRT)/HISTEP;
636 >                len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
637                  tmnew->histo = (int *)malloc(len*sizeof(int));
638                  if (tmnew->histo != NULL)
639                          for (i = len; i--; )

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines