--- ray/src/common/tonemap.c 2021/02/05 01:07:28 3.43 +++ ray/src/common/tonemap.c 2021/05/04 21:50:54 3.51 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: tonemap.c,v 3.43 2021/02/05 01:07:28 greg Exp $"; +static const char RCSid[] = "$Id: tonemap.c,v 3.51 2021/05/04 21:50:54 greg Exp $"; #endif /* * Tone mapping functions. @@ -123,15 +123,14 @@ 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 */ @@ -160,7 +159,8 @@ TMstruct *tms { if (tms == NULL || tms->histo == NULL) return; - free((MEM_PTR)tms->histo); + free(tms->histo); + tms->hbrmin = 10; tms->hbrmax = -10; tms->histo = NULL; } @@ -344,7 +344,7 @@ int wt { static const char funcName[] = "tmAddHisto"; int oldorig=0, oldlen, horig, hlen; - int i, j; + int i; if (tms == NULL) returnErr(TM_E_TMINVAL); @@ -365,12 +365,12 @@ int wt oldlen = HISTI(tms->hbrmax) + 1 - oldorig; } for (i = len; i--; ) { - if ((j = ls[i]) < MINBRT) + if (ls[i] < MINBRT) continue; - if (j < tms->hbrmin) - tms->hbrmin = j; - else if (j > tms->hbrmax) - tms->hbrmax = j; + if (ls[i] < tms->hbrmin) + tms->hbrmin = ls[i]; + else if (ls[i] > tms->hbrmax) + tms->hbrmax = ls[i]; } horig = HISTI(tms->hbrmin); hlen = HISTI(tms->hbrmax) + 1 - horig; @@ -379,9 +379,9 @@ int wt if (newhist == NULL) returnErr(TM_E_NOMEM); if (oldlen) { /* copy and free old */ - memcpy((MEM_PTR)(newhist+(oldorig-horig)), - (MEM_PTR)tms->histo, oldlen*sizeof(HIST_TYP)); - free((MEM_PTR)tms->histo); + memcpy(newhist+(oldorig-horig), + tms->histo, oldlen*sizeof(HIST_TYP)); + free(tms->histo); } tms->histo = newhist; } @@ -421,27 +421,32 @@ 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 = TM_BRES * exp( - ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) - / gamval); - tms->lumap[i] = val; - if (sizeof(TMAP_TYP) == 2 && val >= 0xffff) - tms->lumap[i] = 0xffff; + double d; + d = expmult/tms->inpsf * tmLuminance(tms->mbrmin + i); + if (d >= 2.*minD) + d -= minD; + else /* soft black crushing */ + d *= d/(4.*minD); + d /= 1. - minD; + d = TM_BRES*pow(d, 1./gamval); + tms->lumap[i] = (d > maxV) ? maxV : (int)d; } returnOK; } @@ -460,7 +465,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; @@ -474,7 +479,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; @@ -485,9 +489,9 @@ double Ldmax histot += tms->histo[i]; sum += (double)(j -= HISTEP) * tms->histo[i]; } - threshold = histot*0.005 + .5; if (!histot) returnErr(TM_E_TMFAIL); + threshold = histot/40 + 1; Lwavg = tmLuminance( (double)sum / histot ); /* use linear tone mapping? */ if (tms->flags & TM_F_LINEAR || threshold < 4 || @@ -500,16 +504,16 @@ double Ldmax returnErr(TM_E_NOMEM); cumf[histlen+1] = 1.; /* guard for assignment code */ /* make malleable copy */ - memcpy((MEM_PTR)histo, (MEM_PTR)tms->histo, histlen*sizeof(HIST_TYP)); + 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--; ) { @@ -527,11 +531,11 @@ 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); + } while (40*trimmings > histot); /* allocate space for mapping */ if (!tmNewMap(tms)) returnErr(TM_E_NOMEM); @@ -543,15 +547,15 @@ double Ldmax d = (Ld - Ldmin)/(Ldmax - Ldmin); 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)); } @@ -619,15 +623,13 @@ TMstruct *tms len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); tmnew->histo = (HIST_TYP *)malloc(len*sizeof(HIST_TYP)); if (tmnew->histo != NULL) - memcpy((MEM_PTR)tmnew->histo, (MEM_PTR)tms->histo, - len*sizeof(HIST_TYP)); + memcpy(tmnew->histo, tms->histo, len*sizeof(HIST_TYP)); } if (tmnew->lumap != NULL) { /* duplicate luminance mapping */ len = tmnew->mbrmax-tmnew->mbrmin+1; tmnew->lumap = (TMAP_TYP *)malloc(len*sizeof(TMAP_TYP)); if (tmnew->lumap != NULL) - memcpy((MEM_PTR)tmnew->lumap, (MEM_PTR)tms->lumap, - len*sizeof(TMAP_TYP)); + memcpy(tmnew->lumap, tms->lumap, len*sizeof(TMAP_TYP)); } /* clear package data */ for (i = tmNumPkgs; i--; ) @@ -647,14 +649,14 @@ 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 *********************/ @@ -683,16 +685,19 @@ TMstruct *tms { if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != (tms->hbrmax - tms->hbrmin)) { - free((MEM_PTR)tms->lumap); + free(tms->lumap); tms->lumap = NULL; } tms->mbrmin = tms->hbrmin; tms->mbrmax = tms->hbrmax; if (tms->mbrmin > tms->mbrmax) - return 0; + return(0); if (tms->lumap == NULL) - tms->lumap = (TMAP_TYP *)malloc(sizeof(TMAP_TYP)* - (tms->mbrmax-tms->mbrmin+1)); + 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); }