--- ray/src/common/tonemap.c 2006/05/10 15:34:17 3.24 +++ ray/src/common/tonemap.c 2011/04/22 14:35:54 3.36 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: tonemap.c,v 3.24 2006/05/10 15:34:17 greg Exp $"; +static const char RCSid[] = "$Id: tonemap.c,v 3.36 2011/04/22 14:35:54 greg Exp $"; #endif /* * Tone mapping functions. @@ -12,6 +12,7 @@ static const char RCSid[] = "$Id: tonemap.c,v 3.24 200 #include "copyright.h" #include +#include #include #include "tmprivat.h" #include "tmerrmsg.h" @@ -53,7 +54,7 @@ double gamval tmnew->clf[GRN] = rgb2xyzmat[1][1]; tmnew->clf[BLU] = rgb2xyzmat[1][2]; } else { - comprgb2xyzWBmat(cmat, tmnew->monpri=monpri); + comprgb2xyzmat(cmat, tmnew->monpri=monpri); tmnew->clf[RED] = cmat[1][0]; tmnew->clf[GRN] = cmat[1][1]; tmnew->clf[BLU] = cmat[1][2]; @@ -116,7 +117,7 @@ MEM_PTR dat tms->clf[CIEX] = tms->clf[CIEZ] = 0.; tms->clf[CIEY] = 1.; } else { - comprgb2xyzWBmat(tms->cmat, tms->monpri); + comprgb2xyzmat(tms->cmat, tms->monpri); tms->clf[RED] = tms->cmat[1][0]; tms->clf[GRN] = tms->cmat[1][1]; tms->clf[BLU] = tms->cmat[1][2]; @@ -133,18 +134,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] = 256.*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 +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.))); } @@ -232,7 +229,6 @@ int len ) { static const char funcName[] = "tmCvGrays"; - double d; int i; if (tms == NULL) @@ -262,7 +258,6 @@ int len ) { static const char funcName[] = "tmCvColors"; - static COLOR csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM}; static BYTE gamtab[1024]; static double curgam = .0; COLOR cmon; @@ -301,17 +296,18 @@ int len lum = tms->clf[RED]*cmon[RED] + tms->clf[GRN]*cmon[GRN] + tms->clf[BLU]*cmon[BLU] ; - if (lum <= TM_NOLUM) /* convert brightness */ + if (lum <= TM_NOLUM) { /* convert brightness */ + lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM; ls[i] = TM_NOBRT; - else + } else ls[i] = tmCvLumLUfp(&lum); if (cs == TM_NOCHROM) /* no color? */ continue; if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) { slum = scotlum(cmon); /* mesopic adj. */ - if (lum < LMESLOWER) + if (lum < LMESLOWER) { cmon[RED] = cmon[GRN] = cmon[BLU] = slum; - else { + } else { d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER); if (tms->flags & TM_F_BW) cmon[RED] = cmon[GRN] = @@ -364,8 +360,8 @@ int wt tms->hbrmin = tms->hbrmax = ls[i]; oldlen = 0; } else { - oldorig = (tms->hbrmin-MINBRT)/HISTEP; - oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig; + oldorig = HISTI(tms->hbrmin); + oldlen = HISTI(tms->hbrmax) + 1 - oldorig; } for (i = len; i--; ) { if ((j = ls[i]) < MINBRT) @@ -375,8 +371,8 @@ int wt else if (j > tms->hbrmax) tms->hbrmax = j; } - horig = (tms->hbrmin-MINBRT)/HISTEP; - hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig; + horig = HISTI(tms->hbrmin); + hlen = HISTI(tms->hbrmax) + 1 - horig; if (hlen > oldlen) { /* (re)allocate histogram */ int *newhist = (int *)calloc(hlen, sizeof(int)); if (newhist == NULL) @@ -392,7 +388,7 @@ int wt returnOK; for (i = len; i--; ) /* add in new counts */ if (ls[i] >= MINBRT) - tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt; + tms->histo[ HISTI(ls[i]) - horig ] += wt; returnOK; } @@ -420,27 +416,6 @@ 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, @@ -459,10 +434,12 @@ double gamval if (gamval < MINGAM) gamval = tms->mongam; d = log(expmult/tms->inpsf); - for (i = tms->mbrmax-tms->mbrmin+1; i--; ) - tms->lumap[i] = 256. * exp( + for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { + double val = 256. * exp( ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) - / gamval ); + / gamval); + tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val; + } returnOK; } @@ -495,25 +472,23 @@ double Ldmax Ldmin = Ldmax/Lddyn; logLddyn = log(Lddyn); Ldavg = sqrt(Ldmax*Ldmin); - i = (tms->hbrmin-MINBRT)/HISTEP; - brt0 = MINBRT + HISTEP/2 + i*HISTEP; - histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i; + i = HISTI(tms->hbrmin); + brt0 = HISTV(i); + histlen = HISTI(tms->hbrmax) + 1 - i; /* histogram total and mean */ histot = 0; sum = 0; j = brt0 + histlen*HISTEP; for (i = histlen; i--; ) { histot += tms->histo[i]; - sum += (j -= HISTEP) * tms->histo[i]; + sum += (double)(j -= HISTEP) * tms->histo[i]; } threshold = histot*0.005 + .5; - if (threshold < 4) + if (!histot) returnErr(TM_E_TMFAIL); Lwavg = tmLuminance( (double)sum / histot ); - /* allocate space for mapping */ - if (!tmNewMap(tms)) - returnErr(TM_E_NOMEM); /* use linear tone mapping? */ - if (tms->flags & TM_F_LINEAR) + if (tms->flags & TM_F_LINEAR || threshold < 4 || + tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) goto linearmap; /* clamp histogram */ histo = (int *)malloc(histlen*sizeof(int)); @@ -554,6 +529,9 @@ double Ldmax goto linearmap; } } while (trimmings > threshold); + /* allocate space for mapping */ + if (!tmNewMap(tms)) + returnErr(TM_E_NOMEM); /* assign tone-mapping */ for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen; @@ -613,8 +591,6 @@ int len } - - TMstruct * tmDup( /* duplicate top tone mapping */ TMstruct *tms @@ -631,8 +607,7 @@ TMstruct *tms return(NULL); *tmnew = *tms; /* copy everything */ if (tmnew->histo != NULL) { /* duplicate histogram */ - len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 - - (tmnew->hbrmin-MINBRT)/HISTEP; + len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); tmnew->histo = (int *)malloc(len*sizeof(int)); if (tmnew->histo != NULL) for (i = len; i--; ) @@ -690,6 +665,27 @@ 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((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); }