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.45 by greg, Wed Feb 24 20:35:22 2021 UTC vs.
Revision 3.46 by greg, Tue Mar 2 20:09:14 2021 UTC

# Line 123 | Line 123 | MEM_PTR        dat
123                          tms->clf[GRN] = tms->cmat[1][1];
124                          tms->clf[BLU] = tms->cmat[1][2];
125                  }
126 <                tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] =
127 <                                tms->inpsf;
126 >                tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] = 1.;
127                  tms->cmat[0][1] = tms->cmat[0][2] = tms->cmat[1][0] =
128                  tms->cmat[1][2] = tms->cmat[2][0] = tms->cmat[2][1] = 0.;
129  
130 <        } else if (tms->inppri == TM_XYZPRIM)   /* input is XYZ */
130 >        } else if (tms->inppri == TM_XYZPRIM) {         /* input is XYZ */
131                  compxyz2rgbWBmat(tms->cmat, tms->monpri);
132  
133 <        else {                                  /* input is RGB */
133 >        } else {                                        /* input is RGB */
134                  if (tms->inppri != tms->monpri &&
135                                  PRIMEQ(tms->inppri, tms->monpri))
136                          tms->inppri = tms->monpri;      /* no xform */
# Line 160 | Line 159 | TMstruct       *tms
159   {
160          if (tms == NULL || tms->histo == NULL)
161                  return;
162 <        free((MEM_PTR)tms->histo);
162 >        free(tms->histo);
163          tms->histo = NULL;
164   }
165  
# Line 379 | Line 378 | int    wt
378                  if (newhist == NULL)
379                          returnErr(TM_E_NOMEM);
380                  if (oldlen) {                   /* copy and free old */
381 <                        memcpy((MEM_PTR)(newhist+(oldorig-horig)),
382 <                                (MEM_PTR)tms->histo, oldlen*sizeof(HIST_TYP));
383 <                        free((MEM_PTR)tms->histo);
381 >                        memcpy(newhist+(oldorig-horig),
382 >                                        tms->histo, oldlen*sizeof(HIST_TYP));
383 >                        free(tms->histo);
384                  }
385                  tms->histo = newhist;
386          }
# Line 421 | Line 420 | int
420   tmFixedMapping(                 /* compute fixed, linear tone-mapping */
421   TMstruct        *tms,
422   double  expmult,
423 < double  gamval
423 > double  gamval,
424 > double  Lddyn
425   )
426   {
427          static const char funcName[] = "tmFixedMapping";
428 <        double          d;
428 >        int     maxV = (1L<<(8*sizeof(TMAP_TYP))) - 1;
429 >        double  minD;
430          int     i;
431          
432          if (!tmNewMap(tms))
433                  returnErr(TM_E_NOMEM);
434 <        if (expmult <= .0)
435 <                expmult = 1.;
436 <        if (gamval < MINGAM)
437 <                gamval = tms->mongam;
438 <        d = log(expmult/tms->inpsf);
434 >                                        /* check arguments */
435 >        if (expmult <= .0) expmult = 1.;
436 >        if (gamval < MINGAM) gamval = tms->mongam;
437 >        if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
438 >        minD = 1./Lddyn;
439          for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
440 <                double  val = TM_BRES * exp(
441 <                        ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
442 <                        / gamval);
443 <                tms->lumap[i] = val;
444 <                if (sizeof(TMAP_TYP) == 2 && val >= 0xffff)
445 <                        tms->lumap[i] = 0xffff;
440 >                double  d;
441 >                d = expmult/tms->inpsf * tmLuminance(tms->mbrmin + i);
442 >                if (d <= minD)
443 >                        break;          /* map initialized to zeroes */
444 >                d = (d - minD)/(1. - minD);
445 >                d = TM_BRES*pow(d, 1./gamval);
446 >                tms->lumap[i] = (d >= maxV) ? maxV : (int)d;
447          }
448          returnOK;
449   }
# Line 499 | Line 501 | double Ldmax
501                  returnErr(TM_E_NOMEM);
502          cumf[histlen+1] = 1.;           /* guard for assignment code */
503                                          /* make malleable copy */
504 <        memcpy((MEM_PTR)histo, (MEM_PTR)tms->histo, histlen*sizeof(HIST_TYP));
504 >        memcpy(histo, tms->histo, histlen*sizeof(HIST_TYP));
505          do {                            /* iterate to solution */
506                  sum = 0;                /* cumulative probability */
507                  for (i = 0; i < histlen; i++) {
# Line 526 | Line 528 | double Ldmax
528                  }
529                                          /* check if we're out of data */
530                  if ((histot -= trimmings) <= threshold) {
531 <                        free((MEM_PTR)histo);
532 <                        free((MEM_PTR)cumf);
531 >                        free(histo);
532 >                        free(cumf);
533                          goto linearmap;
534                  }
535          } while (trimmings > threshold);
# Line 542 | Line 544 | double Ldmax
544                  d = (Ld - Ldmin)/(Ldmax - Ldmin);
545                  tms->lumap[i] = TM_BRES*pow(d, 1./gamval);
546          }
547 <        free((MEM_PTR)histo);           /* clean up and return */
548 <        free((MEM_PTR)cumf);
547 >        free(histo);                    /* clean up and return */
548 >        free(cumf);
549          returnOK;
550   linearmap:                              /* linear tone-mapping */
551          if (tms->flags & TM_F_HCONTR)
552                  d = htcontrs(sqrt(Ldmax*Ldmin)) / htcontrs(Lwavg);
553          else
554                  d = Ldmax / tmLuminance(tms->hbrmax);
555 <        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
555 >        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval, Lddyn));
556   }
557  
558  
# Line 618 | Line 620 | TMstruct       *tms
620                  len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
621                  tmnew->histo = (HIST_TYP *)malloc(len*sizeof(HIST_TYP));
622                  if (tmnew->histo != NULL)
623 <                        memcpy((MEM_PTR)tmnew->histo, (MEM_PTR)tms->histo,
622 <                                        len*sizeof(HIST_TYP));
623 >                        memcpy(tmnew->histo, tms->histo, len*sizeof(HIST_TYP));
624          }
625          if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */
626                  len = tmnew->mbrmax-tmnew->mbrmin+1;
627                  tmnew->lumap = (TMAP_TYP *)malloc(len*sizeof(TMAP_TYP));
628                  if (tmnew->lumap != NULL)
629 <                        memcpy((MEM_PTR)tmnew->lumap, (MEM_PTR)tms->lumap,
629 <                                        len*sizeof(TMAP_TYP));
629 >                        memcpy(tmnew->lumap, tms->lumap, len*sizeof(TMAP_TYP));
630          }
631                                          /* clear package data */
632          for (i = tmNumPkgs; i--; )
# Line 646 | Line 646 | TMstruct       *tms;
646                  return;
647                                          /* free tables */
648          if (tms->histo != NULL)
649 <                free((MEM_PTR)tms->histo);
649 >                free(tms->histo);
650          if (tms->lumap != NULL)
651 <                free((MEM_PTR)tms->lumap);
651 >                free(tms->lumap);
652                                          /* free private data */
653          for (i = tmNumPkgs; i--; )
654                  if (tms->pd[i] != NULL)
655                          (*tmPkg[i]->Free)(tms->pd[i]);
656 <        free((MEM_PTR)tms);             /* free basic structure */
656 >        free(tms);                      /* free basic structure */
657   }
658  
659   /******************** Shared but Private library routines *********************/
# Line 682 | Line 682 | TMstruct       *tms
682   {
683          if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
684                                          (tms->hbrmax - tms->hbrmin)) {
685 <                free((MEM_PTR)tms->lumap);
685 >                free(tms->lumap);
686                  tms->lumap = NULL;
687          }
688          tms->mbrmin = tms->hbrmin;
# Line 690 | Line 690 | TMstruct       *tms
690          if (tms->mbrmin > tms->mbrmax)
691                  return 0;
692          if (tms->lumap == NULL)
693 <                tms->lumap = (TMAP_TYP *)malloc(sizeof(TMAP_TYP)*
694 <                                        (tms->mbrmax-tms->mbrmin+1));
693 >                tms->lumap = (TMAP_TYP *)calloc(tms->mbrmax-tms->mbrmin+1,
694 >                                                sizeof(TMAP_TYP));
695 >        else
696 >                memset(tms->lumap, 0, (tms->mbrmax-tms->mbrmin+1)*sizeof(TMAP_TYP));
697 >
698          return(tms->lumap != NULL);
699   }
700  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines