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.41 by greg, Thu Jan 7 19:13:57 2021 UTC vs.
Revision 3.55 by greg, Thu Nov 21 17:15:54 2024 UTC

# Line 75 | Line 75 | double gamval
75                          tmnew->inpsf = WHTEFFICACY;
76          tmnew->cmat[0][1] = tmnew->cmat[0][2] = tmnew->cmat[1][0] =
77          tmnew->cmat[1][2] = tmnew->cmat[2][0] = tmnew->cmat[2][1] = 0.;
78        tmnew->inpdat = NULL;
78          tmnew->hbrmin = 10; tmnew->hbrmax = -10;
79          tmnew->histo = NULL;
80          tmnew->mbrmin = 10; tmnew->mbrmax = -10;
# Line 94 | Line 93 | int
93   tmSetSpace(                     /* set input color space for conversions */
94   TMstruct        *tms,
95   RGBPRIMP        pri,
96 < double  sf,
98 < MEM_PTR dat
96 > double  sf
97   )
98   {
99          static const char funcName[] = "tmSetSpace";
# Line 106 | Line 104 | MEM_PTR        dat
104          if (sf <= 1e-12)
105                  returnErr(TM_E_ILLEGAL);
106                                                  /* check if no change */
107 <        if (pri == tms->inppri && FEQ(sf, tms->inpsf) && dat == tms->inpdat)
107 >        if (pri == tms->inppri && FEQ(sf, tms->inpsf))
108                  returnOK;
109          tms->inppri = pri;                      /* let's set it */
110          tms->inpsf = sf;
113        tms->inpdat = dat;
111  
112          if (tms->flags & TM_F_BW) {             /* color doesn't matter */
113                  tms->monpri = tms->inppri;              /* eliminate xform */
# Line 123 | Line 120 | MEM_PTR        dat
120                          tms->clf[GRN] = tms->cmat[1][1];
121                          tms->clf[BLU] = tms->cmat[1][2];
122                  }
123 <                tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] =
127 <                                tms->inpsf;
123 >                tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] = 1.;
124                  tms->cmat[0][1] = tms->cmat[0][2] = tms->cmat[1][0] =
125                  tms->cmat[1][2] = tms->cmat[2][0] = tms->cmat[2][1] = 0.;
126  
127 <        } else if (tms->inppri == TM_XYZPRIM)   /* input is XYZ */
127 >        } else if (tms->inppri == TM_XYZPRIM) {         /* input is XYZ */
128                  compxyz2rgbWBmat(tms->cmat, tms->monpri);
129  
130 <        else {                                  /* input is RGB */
130 >        } else {                                        /* input is RGB */
131                  if (tms->inppri != tms->monpri &&
132                                  PRIMEQ(tms->inppri, tms->monpri))
133                          tms->inppri = tms->monpri;      /* no xform */
# Line 160 | Line 156 | TMstruct       *tms
156   {
157          if (tms == NULL || tms->histo == NULL)
158                  return;
159 <        free((MEM_PTR)tms->histo);
159 >        free(tms->histo);
160 >        tms->hbrmin = 10; tms->hbrmax = -10;
161          tms->histo = NULL;
162   }
163  
# Line 321 | Line 318 | int    len
318                                  cmon[BLU] += d;
319                          }
320                  } else if (tms->flags & TM_F_BW) {
321 <                        cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
321 >                        int     j = 3;
322 >                        while (j--) cs[3*i+j] = tms->cdiv[j]/(TM_BRES>>8);
323 >                        continue;
324                  }
325                  d = tms->clf[RED]*cmon[RED]/lum;
326                  cs[3*i  ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
# Line 344 | Line 343 | int    wt
343   {
344          static const char funcName[] = "tmAddHisto";
345          int     oldorig=0, oldlen, horig, hlen;
346 <        int     i, j;
346 >        int     i;
347  
348          if (tms == NULL)
349                  returnErr(TM_E_TMINVAL);
# Line 365 | Line 364 | int    wt
364                  oldlen = HISTI(tms->hbrmax) + 1 - oldorig;
365          }
366          for (i = len; i--; ) {
367 <                if ((j = ls[i]) < MINBRT)
367 >                if (ls[i] < MINBRT)
368                          continue;
369 <                if (j < tms->hbrmin)
370 <                        tms->hbrmin = j;
371 <                else if (j > tms->hbrmax)
372 <                        tms->hbrmax = j;
369 >                if (ls[i] < tms->hbrmin)
370 >                        tms->hbrmin = ls[i];
371 >                else if (ls[i] > tms->hbrmax)
372 >                        tms->hbrmax = ls[i];
373          }
374          horig = HISTI(tms->hbrmin);
375          hlen = HISTI(tms->hbrmax) + 1 - horig;
# 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;
429 <        int     i;
428 >        const 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 >= 2.*minD)
443 >                        d -= minD;
444 >                else                    /* soft black crushing */
445 >                        d *= d/(4.*minD);
446 >                d /= 1. - minD;
447 >                d = TM_BRES*pow(d, 1./gamval);
448 >                tms->lumap[i] = (d > maxV) ? maxV : (int)d;
449          }
450          returnOK;
451   }
# Line 460 | Line 464 | double Ldmax
464          float   *cumf;
465          int     brt0, histlen;
466          HIST_TYP        threshold, ceiling, trimmings, histot;
467 <        double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
467 >        double  logLddyn, Ldmin, Lwavg, Tr, Lw, Ld;
468          double  sum;
469          double  d;
470          int     i, j;
# Line 474 | Line 478 | double Ldmax
478                                          /* compute handy values */
479          Ldmin = Ldmax/Lddyn;
480          logLddyn = log(Lddyn);
477        Ldavg = sqrt(Ldmax*Ldmin);
481          i = HISTI(tms->hbrmin);
482          brt0 = HISTV(i);
483          histlen = HISTI(tms->hbrmax) + 1 - i;
# Line 485 | Line 488 | double Ldmax
488                  histot += tms->histo[i];
489                  sum += (double)(j -= HISTEP) * tms->histo[i];
490          }
488        threshold = histot*0.005 + .5;
491          if (!histot)
492                  returnErr(TM_E_TMFAIL);
493 +        threshold = histot/500 + 1;
494          Lwavg = tmLuminance( (double)sum / histot );
495                                          /* use linear tone mapping? */
496 <        if (tms->flags & TM_F_LINEAR || threshold < 4 ||
496 >        if (tms->flags & TM_F_LINEAR ||
497                          tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn)
498                  goto linearmap;
499                                          /* clamp histogram */
# Line 499 | Line 502 | double Ldmax
502          if ((histo == NULL) | (cumf == NULL))
503                  returnErr(TM_E_NOMEM);
504          cumf[histlen+1] = 1.;           /* guard for assignment code */
505 <        for (i = histlen; i--; )        /* make malleable copy */
506 <                histo[i] = tms->histo[i];
505 >                                        /* make malleable copy */
506 >        memcpy(histo, tms->histo, histlen*sizeof(HIST_TYP));
507          do {                            /* iterate to solution */
508                  sum = 0;                /* cumulative probability */
509                  for (i = 0; i < histlen; i++) {
510 <                        cumf[i] = (double)sum/histot;
511 <                        sum += histo[i];
510 >                        cumf[i] = sum/histot;
511 >                        sum += (double)histo[i];
512                  }
513                  cumf[histlen] = 1.;
514                  Tr = histot * (double)(tms->hbrmax - tms->hbrmin) /
515 <                        ((double)histlen*TM_BRTSCALE) / logLddyn;
515 >                                ((double)TM_BRTSCALE*histlen*logLddyn);
516                  ceiling = Tr + 1.;
517                  trimmings = 0;          /* clip to envelope */
518                  for (i = histlen; i--; ) {
# Line 527 | Line 530 | double Ldmax
530                  }
531                                          /* check if we're out of data */
532                  if ((histot -= trimmings) <= threshold) {
533 <                        free((MEM_PTR)histo);
534 <                        free((MEM_PTR)cumf);
533 >                        free(histo);
534 >                        free(cumf);
535                          goto linearmap;
536                  }
537 <        } while (trimmings > threshold);
537 >        } while (40*trimmings > histot);
538                                          /* allocate space for mapping */
539          if (!tmNewMap(tms))
540                  returnErr(TM_E_NOMEM);
# Line 543 | Line 546 | double Ldmax
546                  d = (Ld - Ldmin)/(Ldmax - Ldmin);
547                  tms->lumap[i] = TM_BRES*pow(d, 1./gamval);
548          }
549 <        free((MEM_PTR)histo);           /* clean up and return */
550 <        free((MEM_PTR)cumf);
549 >        free(histo);                    /* clean up and return */
550 >        free(cumf);
551          returnOK;
552   linearmap:                              /* linear tone-mapping */
553          if (tms->flags & TM_F_HCONTR)
554 <                d = htcontrs(Ldavg) / htcontrs(Lwavg);
554 >                d = htcontrs(sqrt(Ldmax*Ldmin)) / htcontrs(Lwavg);
555          else
556 <                d = Ldavg / Lwavg;
557 <        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
556 >                d = Ldmax / tmLuminance(tms->hbrmax);
557 >        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval, Lddyn));
558   }
559  
560  
# Line 581 | Line 584 | int    len
584                                  lv = tms->mbrmax;
585                          li = tms->lumap[lv - tms->mbrmin];
586                  }
587 <                if (cs == TM_NOCHROM)
588 <                        *ps++ = li>=TM_BRES ? 255 : (int)(256*li/TM_BRES);
589 <                else {
587 >                if (cs == TM_NOCHROM) {
588 > #if !(TM_BRES & 0xff)
589 >                        *ps++ = li>=TM_BRES ? 255 : li/(TM_BRES>>8);
590 > #else
591 >                        *ps++ = li>=TM_BRES ? 255 : (li<<8)/TM_BRES;
592 > #endif
593 >                } else {
594                          pv = *cs++ * li / tms->cdiv[RED];
595                          *ps++ = pv>255 ? 255 : pv;
596                          pv = *cs++ * li / tms->cdiv[GRN];
# Line 597 | Line 604 | int    len
604  
605  
606   TMstruct *
607 < tmDup(                          /* duplicate top tone mapping */
607 > tmDup(                          /* duplicate tone mapping */
608   TMstruct        *tms
609   )
610   {
# Line 615 | Line 622 | TMstruct       *tms
622                  len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
623                  tmnew->histo = (HIST_TYP *)malloc(len*sizeof(HIST_TYP));
624                  if (tmnew->histo != NULL)
625 <                        memcpy((MEM_PTR)tmnew->histo, (MEM_PTR)tms->histo,
619 <                                        len*sizeof(HIST_TYP));
625 >                        memcpy(tmnew->histo, tms->histo, len*sizeof(HIST_TYP));
626          }
627          if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */
628                  len = tmnew->mbrmax-tmnew->mbrmin+1;
629                  tmnew->lumap = (TMAP_TYP *)malloc(len*sizeof(TMAP_TYP));
630                  if (tmnew->lumap != NULL)
631 <                        memcpy((MEM_PTR)tmnew->lumap, (MEM_PTR)tms->lumap,
626 <                                        len*sizeof(TMAP_TYP));
631 >                        memcpy(tmnew->lumap, tms->lumap, len*sizeof(TMAP_TYP));
632          }
633                                          /* clear package data */
634          for (i = tmNumPkgs; i--; )
# Line 643 | Line 648 | TMstruct       *tms;
648                  return;
649                                          /* free tables */
650          if (tms->histo != NULL)
651 <                free((MEM_PTR)tms->histo);
651 >                free(tms->histo);
652          if (tms->lumap != NULL)
653 <                free((MEM_PTR)tms->lumap);
653 >                free(tms->lumap);
654                                          /* free private data */
655          for (i = tmNumPkgs; i--; )
656                  if (tms->pd[i] != NULL)
657                          (*tmPkg[i]->Free)(tms->pd[i]);
658 <        free((MEM_PTR)tms);             /* free basic structure */
658 >        free(tms);                      /* free basic structure */
659   }
660  
661   /******************** Shared but Private library routines *********************/
# Line 679 | Line 684 | TMstruct       *tms
684   {
685          if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
686                                          (tms->hbrmax - tms->hbrmin)) {
687 <                free((MEM_PTR)tms->lumap);
687 >                free(tms->lumap);
688                  tms->lumap = NULL;
689          }
690          tms->mbrmin = tms->hbrmin;
691          tms->mbrmax = tms->hbrmax;
692          if (tms->mbrmin > tms->mbrmax)
693 <                return 0;
693 >                return(0);
694          if (tms->lumap == NULL)
695 <                tms->lumap = (TMAP_TYP *)malloc(sizeof(TMAP_TYP)*
696 <                                        (tms->mbrmax-tms->mbrmin+1));
695 >                tms->lumap = (TMAP_TYP *)calloc(tms->mbrmax-tms->mbrmin+1,
696 >                                                sizeof(TMAP_TYP));
697 >        else
698 >                memset(tms->lumap, 0, (tms->mbrmax-tms->mbrmin+1)*sizeof(TMAP_TYP));
699 >
700          return(tms->lumap != NULL);
701   }
702  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines