--- ray/src/common/tonemap.c 2009/02/09 20:23:51 3.33 +++ ray/src/common/tonemap.c 2021/01/07 20:34:45 3.42 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: tonemap.c,v 3.33 2009/02/09 20:23:51 greg Exp $"; +static const char RCSid[] = "$Id: tonemap.c,v 3.42 2021/01/07 20:34:45 greg Exp $"; #endif /* * Tone mapping functions. @@ -12,7 +12,9 @@ static const char RCSid[] = "$Id: tonemap.c,v 3.33 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; @@ -133,18 +135,16 @@ MEM_PTR dat 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) @@ -179,9 +179,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 +253,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,12 +375,12 @@ 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]; + memcpy((MEM_PTR)(newhist+(oldorig-horig)), + (MEM_PTR)tms->histo, oldlen*sizeof(HIST_TYP)); free((MEM_PTR)tms->histo); } tms->histo = newhist; @@ -438,10 +436,12 @@ double gamval gamval = tms->mongam; d = log(expmult/tms->inpsf); for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { - double val = 256. * exp( + 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; } @@ -456,11 +456,11 @@ double Ldmax ) { static const char funcName[] = "tmComputeMapping"; - int *histo; + HIST_TYP *histo; float *cumf; - int brt0, histlen, threshold, ceiling, trimmings; + int brt0, histlen; + HIST_TYP threshold, ceiling, trimmings, histot; double logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld; - int32 histot; double sum; double d; int i, j; @@ -494,13 +494,13 @@ 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((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++) { @@ -541,7 +541,7 @@ 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); @@ -558,29 +558,31 @@ linearmap: /* linear tone-mapping */ 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; + *ps++ = li>=TM_BRES ? 255 : (int)(256*li/TM_BRES); else { pv = *cs++ * li / tms->cdiv[RED]; *ps++ = pv>255 ? 255 : pv; @@ -595,7 +597,7 @@ int len TMstruct * -tmDup( /* duplicate top tone mapping */ +tmDup( /* duplicate tone mapping */ TMstruct *tms ) { @@ -611,18 +613,17 @@ 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((MEM_PTR)tmnew->histo, (MEM_PTR)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((MEM_PTR)tmnew->lumap, (MEM_PTR)tms->lumap, + len*sizeof(TMAP_TYP)); } /* clear package data */ for (i = tmNumPkgs; i--; ) @@ -654,7 +655,7 @@ TMstruct *tms; /******************** Shared but Private library routines *********************/ -BYTE tmMesofact[BMESUPPER-BMESLOWER]; +uby8 tmMesofact[BMESUPPER-BMESLOWER]; void tmMkMesofact() /* build mesopic lookup factor table */ @@ -686,7 +687,7 @@ TMstruct *tms if (tms->mbrmin > tms->mbrmax) return 0; if (tms->lumap == NULL) - tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)* + tms->lumap = (TMAP_TYP *)malloc(sizeof(TMAP_TYP)* (tms->mbrmax-tms->mbrmin+1)); return(tms->lumap != NULL); }