--- ray/src/common/tonemap.c 2006/08/09 16:26:54 3.28 +++ ray/src/common/tonemap.c 2009/02/09 20:23:51 3.33 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: tonemap.c,v 3.28 2006/08/09 16:26:54 greg Exp $"; +static const char RCSid[] = "$Id: tonemap.c,v 3.33 2009/02/09 20:23:51 greg Exp $"; #endif /* * Tone mapping functions. @@ -363,8 +363,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) @@ -374,8 +374,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) @@ -391,7 +391,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; } @@ -419,27 +419,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, @@ -496,25 +475,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)); @@ -555,6 +532,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; @@ -614,8 +594,6 @@ int len } - - TMstruct * tmDup( /* duplicate top tone mapping */ TMstruct *tms @@ -632,8 +610,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--; ) @@ -691,6 +668,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); }