--- ray/src/common/tonemap.c 2021/01/07 02:13:49 3.40 +++ ray/src/common/tonemap.c 2021/02/24 03:48:52 3.44 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: tonemap.c,v 3.40 2021/01/07 02:13:49 greg Exp $"; +static const char RCSid[] = "$Id: tonemap.c,v 3.44 2021/02/24 03:48:52 greg Exp $"; #endif /* * Tone mapping functions. @@ -439,7 +439,9 @@ double gamval double val = TM_BRES * exp( ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) / gamval); - tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val; + tms->lumap[i] = val; + if (sizeof(TMAP_TYP) == 2 && val >= 0xffff) + tms->lumap[i] = 0xffff; } returnOK; } @@ -458,7 +460,7 @@ double Ldmax float *cumf; int brt0, histlen; HIST_TYP threshold, ceiling, trimmings, histot; - double logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld; + double logLddyn, Ldmin, Lwavg, Tr, Lw, Ld; double sum; double d; int i, j; @@ -472,7 +474,6 @@ double Ldmax /* compute handy values */ Ldmin = Ldmax/Lddyn; logLddyn = log(Lddyn); - Ldavg = sqrt(Ldmax*Ldmin); i = HISTI(tms->hbrmin); brt0 = HISTV(i); histlen = HISTI(tms->hbrmax) + 1 - i; @@ -497,8 +498,8 @@ double Ldmax if ((histo == NULL) | (cumf == NULL)) returnErr(TM_E_NOMEM); cumf[histlen+1] = 1.; /* guard for assignment code */ - for (i = histlen; i--; ) /* make malleable copy */ - histo[i] = tms->histo[i]; + /* make malleable copy */ + memcpy((MEM_PTR)histo, (MEM_PTR)tms->histo, histlen*sizeof(HIST_TYP)); do { /* iterate to solution */ sum = 0; /* cumulative probability */ for (i = 0; i < histlen; i++) { @@ -546,9 +547,9 @@ double Ldmax returnOK; linearmap: /* linear tone-mapping */ if (tms->flags & TM_F_HCONTR) - d = htcontrs(Ldavg) / htcontrs(Lwavg); + d = htcontrs(sqrt(Ldmax*Ldmin)) / htcontrs(Lwavg); else - d = Ldavg / Lwavg; + d = Ldmax / tmLuminance(tms->hbrmax); return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval)); } @@ -563,23 +564,29 @@ int len ) { static const char funcName[] = "tmMapPixels"; - int32 li, pv; + TMbright lv; + TMAP_TYP li; + int pv; if (tms == NULL || tms->lumap == NULL) returnErr(TM_E_TMINVAL); if ((ps == NULL) | (ls == NULL) | (len < 0)) returnErr(TM_E_ILLEGAL); while (len--) { - if ((li = *ls++) < tms->mbrmin) { + if ((lv = *ls++) < tms->mbrmin) { li = 0; } else { - if (li > tms->mbrmax) - li = tms->mbrmax; - li = tms->lumap[li - tms->mbrmin]; + if (lv > tms->mbrmax) + lv = tms->mbrmax; + li = tms->lumap[lv - tms->mbrmin]; } - if (cs == TM_NOCHROM) - *ps++ = li>255 ? 255 : li; - else { + if (cs == TM_NOCHROM) { +#if !(TM_BRES & 0xff) + *ps++ = li>=TM_BRES ? 255 : li/(TM_BRES>>8); +#else + *ps++ = li>=TM_BRES ? 255 : (li<<8)/TM_BRES; +#endif + } else { pv = *cs++ * li / tms->cdiv[RED]; *ps++ = pv>255 ? 255 : pv; pv = *cs++ * li / tms->cdiv[GRN]; @@ -593,7 +600,7 @@ int len TMstruct * -tmDup( /* duplicate top tone mapping */ +tmDup( /* duplicate tone mapping */ TMstruct *tms ) {