--- ray/src/common/tonemap.c 2006/08/09 16:26:54 3.28 +++ ray/src/common/tonemap.c 2007/11/27 02:42:54 3.30 @@ -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.30 2007/11/27 02:42:54 greg Exp $"; #endif /* * Tone mapping functions. @@ -504,17 +504,15 @@ double Ldmax 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 +553,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;