--- ray/src/common/tonemap.c 2008/07/03 03:35:10 3.32 +++ ray/src/common/tonemap.c 2021/04/07 21:13:52 3.47 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: tonemap.c,v 3.32 2008/07/03 03:35:10 greg Exp $"; +static const char RCSid[] = "$Id: tonemap.c,v 3.47 2021/04/07 21:13:52 greg Exp $"; #endif /* * Tone mapping functions. @@ -12,7 +12,9 @@ static const char RCSid[] = "$Id: tonemap.c,v 3.32 200 #include "copyright.h" #include +#include #include +#include #include "tmprivat.h" #include "tmerrmsg.h" @@ -65,7 +67,7 @@ double gamval tmnew->mongam = gamval; /* set color divisors */ for (i = 0; i < 3; i++) - tmnew->cdiv[i] = 256.*pow(tmnew->clf[i], 1./tmnew->mongam); + tmnew->cdiv[i] = TM_BRES*pow(tmnew->clf[i], 1./tmnew->mongam); /* set input transform */ tmnew->inppri = tmnew->monpri; @@ -121,30 +123,27 @@ MEM_PTR dat tms->clf[GRN] = tms->cmat[1][1]; tms->clf[BLU] = tms->cmat[1][2]; } - tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] = - tms->inpsf; + tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] = 1.; tms->cmat[0][1] = tms->cmat[0][2] = tms->cmat[1][0] = tms->cmat[1][2] = tms->cmat[2][0] = tms->cmat[2][1] = 0.; - } else if (tms->inppri == TM_XYZPRIM) /* input is XYZ */ + } else if (tms->inppri == TM_XYZPRIM) { /* input is XYZ */ compxyz2rgbWBmat(tms->cmat, tms->monpri); - else { /* input is RGB */ + } else { /* input is RGB */ if (tms->inppri != tms->monpri && PRIMEQ(tms->inppri, tms->monpri)) tms->inppri = tms->monpri; /* no xform */ - comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri); + if (!comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri)) + returnErr(TM_E_ILLEGAL); } for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) tms->cmat[i][j] *= tms->inpsf; /* set color divisors */ for (i = 0; i < 3; i++) - if (tms->clf[i] > .001) - tms->cdiv[i] = - 256.*pow(tms->clf[i], 1./tms->mongam); - else - tms->cdiv[i] = 1; + tms->cdiv[i] = TM_BRES*pow(tms->clf[i] < .001 ? .001 : + tms->clf[i], 1./tms->mongam); /* notify packages */ for (i = tmNumPkgs; i--; ) if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL) @@ -160,7 +159,7 @@ TMstruct *tms { if (tms == NULL || tms->histo == NULL) return; - free((MEM_PTR)tms->histo); + free(tms->histo); tms->histo = NULL; } @@ -179,9 +178,7 @@ double lum #endif return(TM_NOBRT); d = TM_BRTSCALE*log(lum); - if (d > 0.) - return((TMbright)(d+.5)); - return((TMbright)(d-.5)); + return((TMbright)(d + .5 - (d < 0.))); } @@ -255,13 +252,13 @@ int tmCvColors( /* convert float colors */ TMstruct *tms, TMbright *ls, -BYTE *cs, +uby8 *cs, COLOR *scan, int len ) { static const char funcName[] = "tmCvColors"; - static BYTE gamtab[1024]; + static uby8 gamtab[1024]; static double curgam = .0; COLOR cmon; float lum, slum, d; @@ -377,13 +374,13 @@ int wt horig = HISTI(tms->hbrmin); hlen = HISTI(tms->hbrmax) + 1 - horig; if (hlen > oldlen) { /* (re)allocate histogram */ - int *newhist = (int *)calloc(hlen, sizeof(int)); + HIST_TYP *newhist = (HIST_TYP *)calloc(hlen, sizeof(HIST_TYP)); if (newhist == NULL) returnErr(TM_E_NOMEM); if (oldlen) { /* copy and free old */ - for (i = oldlen, j = i+oldorig-horig; i; ) - newhist[--j] = tms->histo[--i]; - free((MEM_PTR)tms->histo); + memcpy(newhist+(oldorig-horig), + tms->histo, oldlen*sizeof(HIST_TYP)); + free(tms->histo); } tms->histo = newhist; } @@ -419,50 +416,34 @@ double La } -static int -tmNewMap( /* allocate new tone-mapping array */ -TMstruct *tms -) -{ - if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != - (tms->hbrmax - tms->hbrmin)) { - free((MEM_PTR)tms->lumap); - tms->lumap = NULL; - } - tms->mbrmin = tms->hbrmin; - tms->mbrmax = tms->hbrmax; - if (tms->mbrmin > tms->mbrmax) - return 0; - if (tms->lumap == NULL) - tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)* - (tms->mbrmax-tms->mbrmin+1)); - return(tms->lumap != NULL); -} - - int tmFixedMapping( /* compute fixed, linear tone-mapping */ TMstruct *tms, double expmult, -double gamval +double gamval, +double Lddyn ) { static const char funcName[] = "tmFixedMapping"; - double d; - int i; + const int maxV = (1L<<(8*sizeof(TMAP_TYP))) - 1; + double minD; + int i; if (!tmNewMap(tms)) returnErr(TM_E_NOMEM); - if (expmult <= .0) - expmult = 1.; - if (gamval < MINGAM) - gamval = tms->mongam; - d = log(expmult/tms->inpsf); + /* check arguments */ + if (expmult <= .0) expmult = 1.; + if (gamval < MINGAM) gamval = tms->mongam; + if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; + minD = 1./Lddyn; for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { - double val = 256. * exp( - ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) - / gamval); - tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val; + double d; + d = expmult/tms->inpsf * tmLuminance(tms->mbrmin + i); + if (d <= minD) + break; /* map initialized to zeroes */ + d = (d - minD)/(1. - minD); + d = TM_BRES*pow(d, 1./gamval); + tms->lumap[i] = (d > maxV) ? maxV : (int)d; } returnOK; } @@ -477,11 +458,11 @@ double Ldmax ) { static const char funcName[] = "tmComputeMapping"; - int *histo; + HIST_TYP *histo; float *cumf; - int brt0, histlen, threshold, ceiling, trimmings; - double logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld; - int32 histot; + int brt0, histlen; + HIST_TYP threshold, ceiling, trimmings, histot; + double logLddyn, Ldmin, Lwavg, Tr, Lw, Ld; double sum; double d; int i, j; @@ -495,7 +476,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; @@ -506,7 +486,7 @@ double Ldmax histot += tms->histo[i]; sum += (double)(j -= HISTEP) * tms->histo[i]; } - threshold = histot*0.005 + .5; + threshold = histot*0.002 + .5; if (!histot) returnErr(TM_E_TMFAIL); Lwavg = tmLuminance( (double)sum / histot ); @@ -515,22 +495,22 @@ double Ldmax tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) goto linearmap; /* clamp histogram */ - histo = (int *)malloc(histlen*sizeof(int)); + histo = (HIST_TYP *)malloc(histlen*sizeof(HIST_TYP)); cumf = (float *)malloc((histlen+2)*sizeof(float)); 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(histo, tms->histo, histlen*sizeof(HIST_TYP)); do { /* iterate to solution */ sum = 0; /* cumulative probability */ for (i = 0; i < histlen; i++) { - cumf[i] = (double)sum/histot; - sum += histo[i]; + cumf[i] = sum/histot; + sum += (double)histo[i]; } cumf[histlen] = 1.; Tr = histot * (double)(tms->hbrmax - tms->hbrmin) / - ((double)histlen*TM_BRTSCALE) / logLddyn; + ((double)TM_BRTSCALE*histlen*logLddyn); ceiling = Tr + 1.; trimmings = 0; /* clip to envelope */ for (i = histlen; i--; ) { @@ -548,8 +528,8 @@ double Ldmax } /* check if we're out of data */ if ((histot -= trimmings) <= threshold) { - free((MEM_PTR)histo); - free((MEM_PTR)cumf); + free(histo); + free(cumf); goto linearmap; } } while (trimmings > threshold); @@ -562,47 +542,53 @@ double Ldmax d -= (double)j; Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1])); d = (Ld - Ldmin)/(Ldmax - Ldmin); - tms->lumap[i] = 256.*pow(d, 1./gamval); + tms->lumap[i] = TM_BRES*pow(d, 1./gamval); } - free((MEM_PTR)histo); /* clean up and return */ - free((MEM_PTR)cumf); + free(histo); /* clean up and return */ + free(cumf); 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; - return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval)); + d = Ldmax / tmLuminance(tms->hbrmax); + return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval, Lddyn)); } int tmMapPixels( /* apply tone-mapping to pixel(s) */ TMstruct *tms, -BYTE *ps, +uby8 *ps, TMbright *ls, -BYTE *cs, +uby8 *cs, 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]; @@ -615,10 +601,8 @@ int len } - - TMstruct * -tmDup( /* duplicate top tone mapping */ +tmDup( /* duplicate tone mapping */ TMstruct *tms ) { @@ -634,18 +618,15 @@ TMstruct *tms *tmnew = *tms; /* copy everything */ if (tmnew->histo != NULL) { /* duplicate histogram */ len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); - tmnew->histo = (int *)malloc(len*sizeof(int)); + tmnew->histo = (HIST_TYP *)malloc(len*sizeof(HIST_TYP)); if (tmnew->histo != NULL) - for (i = len; i--; ) - tmnew->histo[i] = tms->histo[i]; + memcpy(tmnew->histo, tms->histo, len*sizeof(HIST_TYP)); } if (tmnew->lumap != NULL) { /* duplicate luminance mapping */ len = tmnew->mbrmax-tmnew->mbrmin+1; - tmnew->lumap = (unsigned short *)malloc( - len*sizeof(unsigned short) ); + tmnew->lumap = (TMAP_TYP *)malloc(len*sizeof(TMAP_TYP)); if (tmnew->lumap != NULL) - for (i = len; i--; ) - tmnew->lumap[i] = tms->lumap[i]; + memcpy(tmnew->lumap, tms->lumap, len*sizeof(TMAP_TYP)); } /* clear package data */ for (i = tmNumPkgs; i--; ) @@ -665,19 +646,19 @@ TMstruct *tms; return; /* free tables */ if (tms->histo != NULL) - free((MEM_PTR)tms->histo); + free(tms->histo); if (tms->lumap != NULL) - free((MEM_PTR)tms->lumap); + free(tms->lumap); /* free private data */ for (i = tmNumPkgs; i--; ) if (tms->pd[i] != NULL) (*tmPkg[i]->Free)(tms->pd[i]); - free((MEM_PTR)tms); /* free basic structure */ + free(tms); /* free basic structure */ } /******************** Shared but Private library routines *********************/ -BYTE tmMesofact[BMESUPPER-BMESLOWER]; +uby8 tmMesofact[BMESUPPER-BMESLOWER]; void tmMkMesofact() /* build mesopic lookup factor table */ @@ -691,6 +672,30 @@ tmMkMesofact() /* build mesopic lookup factor table tmMesofact[i-BMESLOWER] = 256. * (tmLuminance(i) - LMESLOWER) / (LMESUPPER - LMESLOWER); +} + + +int +tmNewMap( /* allocate new tone-mapping array */ +TMstruct *tms +) +{ + if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != + (tms->hbrmax - tms->hbrmin)) { + free(tms->lumap); + tms->lumap = NULL; + } + tms->mbrmin = tms->hbrmin; + tms->mbrmax = tms->hbrmax; + if (tms->mbrmin > tms->mbrmax) + return(0); + if (tms->lumap == NULL) + tms->lumap = (TMAP_TYP *)calloc(tms->mbrmax-tms->mbrmin+1, + sizeof(TMAP_TYP)); + else + memset(tms->lumap, 0, (tms->mbrmax-tms->mbrmin+1)*sizeof(TMAP_TYP)); + + return(tms->lumap != NULL); }