ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.c
(Generate patch)

Comparing ray/src/common/tonemap.c (file contents):
Revision 3.16 by greg, Fri Jan 7 20:33:02 2005 UTC vs.
Revision 3.33 by greg, Mon Feb 9 20:23:51 2009 UTC

# Line 22 | Line 22 | static const char      RCSid[] = "$Id$";
22   struct tmPackage        *tmPkg[TM_MAXPKG];
23   int     tmNumPkgs = 0;                  /* number of registered packages */
24  
25 < int     tmLastError;                    /* last error incurred by library */
26 < char    *tmLastFunction;                /* error-generating function name */
25 >                                        /* luminance->brightness lookup */
26 > static TMbright         *tmFloat2BrtLUT = NULL;
27  
28 + #define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15]
29  
30 +
31   TMstruct *
32   tmInit(                                 /* initialize new tone mapping */
33   int     flags,
# Line 51 | Line 53 | double gamval
53                  tmnew->clf[GRN] = rgb2xyzmat[1][1];
54                  tmnew->clf[BLU] = rgb2xyzmat[1][2];
55          } else {
56 <                comprgb2xyzWBmat(cmat, tmnew->monpri=monpri);
56 >                comprgb2xyzmat(cmat, tmnew->monpri=monpri);
57                  tmnew->clf[RED] = cmat[1][0];
58                  tmnew->clf[GRN] = cmat[1][1];
59                  tmnew->clf[BLU] = cmat[1][2];
# Line 71 | Line 73 | double gamval
73                          tmnew->inpsf = WHTEFFICACY;
74          tmnew->cmat[0][1] = tmnew->cmat[0][2] = tmnew->cmat[1][0] =
75          tmnew->cmat[1][2] = tmnew->cmat[2][0] = tmnew->cmat[2][1] = 0.;
76 +        tmnew->inpdat = NULL;
77          tmnew->hbrmin = 10; tmnew->hbrmax = -10;
78          tmnew->histo = NULL;
79          tmnew->mbrmin = 10; tmnew->mbrmax = -10;
# Line 78 | Line 81 | double gamval
81                                                  /* zero private data */
82          for (i = TM_MAXPKG; i--; )
83                  tmnew->pd[i] = NULL;
84 +        tmnew->lastError = TM_E_OK;
85 +        tmnew->lastFunc = "NoErr";
86                                                  /* return new TMstruct */
87          return(tmnew);
88   }
# Line 87 | Line 92 | int
92   tmSetSpace(                     /* set input color space for conversions */
93   TMstruct        *tms,
94   RGBPRIMP        pri,
95 < double  sf
95 > double  sf,
96 > MEM_PTR dat
97   )
98   {
99 <        static char     funcName[] = "tmSetSpace";
99 >        static const char funcName[] = "tmSetSpace";
100          int     i, j;
101                                                  /* error check */
102          if (tms == NULL)
# Line 98 | Line 104 | double sf
104          if (sf <= 1e-12)
105                  returnErr(TM_E_ILLEGAL);
106                                                  /* check if no change */
107 <        if (pri == tms->inppri && FEQ(sf, tms->inpsf))
107 >        if (pri == tms->inppri && FEQ(sf, tms->inpsf) && dat == tms->inpdat)
108                  returnOK;
109          tms->inppri = pri;                      /* let's set it */
110          tms->inpsf = sf;
111 +        tms->inpdat = dat;
112  
113          if (tms->flags & TM_F_BW) {             /* color doesn't matter */
114                  tms->monpri = tms->inppri;              /* eliminate xform */
# Line 109 | Line 116 | double sf
116                          tms->clf[CIEX] = tms->clf[CIEZ] = 0.;
117                          tms->clf[CIEY] = 1.;
118                  } else {
119 <                        comprgb2xyzWBmat(tms->cmat, tms->monpri);
119 >                        comprgb2xyzmat(tms->cmat, tms->monpri);
120                          tms->clf[RED] = tms->cmat[1][0];
121                          tms->clf[GRN] = tms->cmat[1][1];
122                          tms->clf[BLU] = tms->cmat[1][2];
# Line 147 | Line 154 | double sf
154  
155  
156   void
157 < tmClearHisto(                   /* clear current histogram */
157 > tmClearHisto(                           /* clear current histogram */
158   TMstruct        *tms
159   )
160   {
# Line 158 | Line 165 | TMstruct       *tms
165   }
166  
167  
168 + TMbright
169 + tmCvLuminance(                          /* convert a single luminance */
170 + double  lum
171 + )
172 + {
173 +        double  d;
174 +
175 + #ifdef isfinite
176 +        if (!isfinite(lum) || lum <= TM_NOLUM)
177 + #else
178 +        if (lum <= TM_NOLUM)
179 + #endif
180 +                return(TM_NOBRT);
181 +        d = TM_BRTSCALE*log(lum);
182 +        if (d > 0.)
183 +                return((TMbright)(d+.5));
184 +        return((TMbright)(d-.5));
185 + }
186 +
187 +
188   int
189 + tmCvLums(                               /* convert luminances using lookup */
190 + TMbright        *ls,
191 + float           *scan,
192 + int             len
193 + )
194 + {
195 +        if (tmFloat2BrtLUT == NULL) {   /* initialize lookup table */
196 +                int32   i;
197 +                tmFloat2BrtLUT = (TMbright *)malloc(sizeof(TMbright)*0x10000);
198 +                if (tmFloat2BrtLUT == NULL)
199 +                        return(TM_E_NOMEM);
200 +                for (i = 0; i < 0x10000; i++) {
201 +                        int32   l = (i<<1 | 1) << 14;
202 + #ifndef isfinite
203 +                        if ((l & 0x7f800000) == 0x7f800000)
204 +                                tmFloat2BrtLUT[i] = TM_NOBRT;
205 +                        else
206 + #endif
207 +                        tmFloat2BrtLUT[i] = tmCvLuminance(*(float *)&l);
208 +                }
209 +        }
210 +        if (len <= 0)
211 +                return(TM_E_OK);
212 +        if ((ls == NULL) | (scan == NULL))
213 +                return(TM_E_ILLEGAL);
214 +        while (len--) {
215 +                if (*scan <= TM_NOLUM) {
216 +                        *ls++ = TM_NOBRT;
217 +                        ++scan;
218 +                        continue;
219 +                }
220 +                *ls++ = tmCvLumLUfp(scan++);
221 +        }
222 +        return(TM_E_OK);
223 + }
224 +
225 +
226 + int
227 + tmCvGrays(                              /* convert float gray values */
228 + TMstruct        *tms,
229 + TMbright        *ls,
230 + float           *scan,
231 + int             len
232 + )
233 + {
234 +        static const char funcName[] = "tmCvGrays";
235 +        int     i;
236 +
237 +        if (tms == NULL)
238 +                returnErr(TM_E_TMINVAL);
239 +        if ((ls == NULL) | (scan == NULL) | (len < 0))
240 +                returnErr(TM_E_ILLEGAL);
241 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
242 +                tmCvLums(NULL, NULL, 0);
243 +        for (i = len; i--; ) {
244 +                float   lum = tms->inpsf * scan[i];
245 +                if (lum <= TM_NOLUM)
246 +                        ls[i] = TM_NOBRT;
247 +                else
248 +                        ls[i] = tmCvLumLUfp(&lum);
249 +        }
250 +        returnOK;
251 + }
252 +
253 +
254 + int
255   tmCvColors(                             /* convert float colors */
256   TMstruct        *tms,
257   TMbright        *ls,
# Line 167 | Line 260 | COLOR  *scan,
260   int     len
261   )
262   {
263 <        static char     funcName[] = "tmCvColors";
264 <        static COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
263 >        static const char funcName[] = "tmCvColors";
264 >        static BYTE     gamtab[1024];
265 >        static double   curgam = .0;
266          COLOR   cmon;
267 <        double  lum, slum;
174 <        double  d;
267 >        float   lum, slum, d;
268          int     i;
269  
270          if (tms == NULL)
271                  returnErr(TM_E_TMINVAL);
272          if ((ls == NULL) | (scan == NULL) | (len < 0))
273                  returnErr(TM_E_ILLEGAL);
274 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
275 +                tmCvLums(NULL, NULL, 0);
276 +        if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
277 +                curgam = tms->mongam;                   /* (re)build table */
278 +                for (i = 1024; i--; )
279 +                        gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam));
280 +        }
281          for (i = len; i--; ) {
282                  if (tmNeedMatrix(tms)) {                /* get monitor RGB */
283                          colortrans(cmon, tms->cmat, scan[i]);
# Line 186 | Line 286 | int    len
286                          cmon[GRN] = tms->inpsf*scan[i][GRN];
287                          cmon[BLU] = tms->inpsf*scan[i][BLU];
288                  }
289 + #ifdef isfinite
290 +                if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f;
291 +                if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f;
292 +                if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f;
293 + #else
294 +                if (cmon[RED] < .0f) cmon[RED] = .0f;
295 +                if (cmon[GRN] < .0f) cmon[GRN] = .0f;
296 +                if (cmon[BLU] < .0f) cmon[BLU] = .0f;
297 + #endif
298                                                          /* world luminance */
299                  lum =   tms->clf[RED]*cmon[RED] +
300                          tms->clf[GRN]*cmon[GRN] +
301                          tms->clf[BLU]*cmon[BLU] ;
302 <                                                        /* check range */
303 <                if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
304 <                        lum =   tms->clf[RED]*cmon[RED] +
305 <                                tms->clf[GRN]*cmon[GRN] +
306 <                                tms->clf[BLU]*cmon[BLU] ;
198 <                if (lum < MINLUM) {
199 <                        ls[i] = MINBRT-1;               /* bogus value */
200 <                        lum = MINLUM;
201 <                } else {
202 <                        d = TM_BRTSCALE*log(lum);       /* encode it */
203 <                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
204 <                }
302 >                if (lum <= TM_NOLUM) {                  /* convert brightness */
303 >                        lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM;
304 >                        ls[i] = TM_NOBRT;
305 >                } else
306 >                        ls[i] = tmCvLumLUfp(&lum);
307                  if (cs == TM_NOCHROM)                   /* no color? */
308                          continue;
309                  if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
310                          slum = scotlum(cmon);           /* mesopic adj. */
311 <                        if (lum < LMESLOWER)
311 >                        if (lum < LMESLOWER) {
312                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
313 <                        else {
313 >                        } else {
314                                  d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
315                                  if (tms->flags & TM_F_BW)
316                                          cmon[RED] = cmon[GRN] =
317                                                          cmon[BLU] = d*lum;
318                                  else
319                                          scalecolor(cmon, d);
320 <                                d = (1.-d)*slum;
320 >                                d = (1.f-d)*slum;
321                                  cmon[RED] += d;
322                                  cmon[GRN] += d;
323                                  cmon[BLU] += d;
# Line 224 | Line 326 | int    len
326                          cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
327                  }
328                  d = tms->clf[RED]*cmon[RED]/lum;
329 <                cs[3*i  ] = d>=.999 ? 255 :
228 <                                (int)(256.*pow(d, 1./tms->mongam));
329 >                cs[3*i  ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
330                  d = tms->clf[GRN]*cmon[GRN]/lum;
331 <                cs[3*i+1] = d>=.999 ? 255 :
231 <                                (int)(256.*pow(d, 1./tms->mongam));
331 >                cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
332                  d = tms->clf[BLU]*cmon[BLU]/lum;
333 <                cs[3*i+2] = d>=.999 ? 255 :
234 <                                (int)(256.*pow(d, 1./tms->mongam));
333 >                cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
334          }
335          returnOK;
336   }
337  
338  
339   int
241 tmCvGrays(                              /* convert float gray values */
242 TMstruct        *tms,
243 TMbright        *ls,
244 float   *scan,
245 int     len
246 )
247 {
248        static char     funcName[] = "tmCvGrays";
249        double  d;
250        int     i;
251
252        if (tms == NULL)
253                returnErr(TM_E_TMINVAL);
254        if ((ls == NULL) | (scan == NULL) | (len < 0))
255                returnErr(TM_E_ILLEGAL);
256        for (i = len; i--; )
257                if (scan[i] <= TM_NOLUM) {
258                        ls[i] = TM_NOBRT;               /* bogus value */
259                } else {
260                        d = TM_BRTSCALE*log(scan[i]);   /* encode it */
261                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
262                }
263        returnOK;
264 }
265
266
267 int
340   tmAddHisto(                             /* add values to histogram */
341   TMstruct        *tms,
342   TMbright        *ls,
# Line 272 | Line 344 | int    len,
344   int     wt
345   )
346   {
347 <        static char     funcName[] = "tmAddHisto";
347 >        static const char funcName[] = "tmAddHisto";
348          int     oldorig=0, oldlen, horig, hlen;
349          int     i, j;
350  
# Line 291 | Line 363 | int    wt
363                  tms->hbrmin = tms->hbrmax = ls[i];
364                  oldlen = 0;
365          } else {
366 <                oldorig = (tms->hbrmin-MINBRT)/HISTEP;
367 <                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
366 >                oldorig = HISTI(tms->hbrmin);
367 >                oldlen = HISTI(tms->hbrmax) + 1 - oldorig;
368          }
369          for (i = len; i--; ) {
370                  if ((j = ls[i]) < MINBRT)
# Line 302 | Line 374 | int    wt
374                  else if (j > tms->hbrmax)
375                          tms->hbrmax = j;
376          }
377 <        horig = (tms->hbrmin-MINBRT)/HISTEP;
378 <        hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
377 >        horig = HISTI(tms->hbrmin);
378 >        hlen = HISTI(tms->hbrmax) + 1 - horig;
379          if (hlen > oldlen) {                    /* (re)allocate histogram */
380                  int     *newhist = (int *)calloc(hlen, sizeof(int));
381                  if (newhist == NULL)
# Line 319 | Line 391 | int    wt
391                  returnOK;
392          for (i = len; i--; )                    /* add in new counts */
393                  if (ls[i] >= MINBRT)
394 <                        tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
394 >                        tms->histo[ HISTI(ls[i]) - horig ] += wt;
395          returnOK;
396   }
397  
# Line 347 | Line 419 | double La
419   }
420  
421  
350 static int
351 tmNewMap(                       /* allocate new tone-mapping array */
352 TMstruct        *tms
353 )
354 {
355        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
356                                        (tms->hbrmax - tms->hbrmin)) {
357                free((MEM_PTR)tms->lumap);
358                tms->lumap = NULL;
359        }
360        tms->mbrmin = tms->hbrmin;
361        tms->mbrmax = tms->hbrmax;
362        if (tms->mbrmin > tms->mbrmax)
363                return 0;
364        if (tms->lumap == NULL)
365                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
366                                        (tms->mbrmax-tms->mbrmin+1));
367        return(tms->lumap != NULL);
368 }
369
370
422   int
423   tmFixedMapping(                 /* compute fixed, linear tone-mapping */
424   TMstruct        *tms,
# Line 375 | Line 426 | double expmult,
426   double  gamval
427   )
428   {
429 <        static char     funcName[] = "tmFixedMapping";
429 >        static const char funcName[] = "tmFixedMapping";
430          double          d;
431          int     i;
432          
# Line 386 | Line 437 | double gamval
437          if (gamval < MINGAM)
438                  gamval = tms->mongam;
439          d = log(expmult/tms->inpsf);
440 <        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
441 <                tms->lumap[i] = 256. * exp(
440 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
441 >                double  val = 256. * exp(
442                          ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
443 <                        / gamval );
443 >                        / gamval);
444 >                tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val;
445 >        }
446          returnOK;
447   }
448  
# Line 402 | Line 455 | double Lddyn,
455   double  Ldmax
456   )
457   {
458 <        static char     funcName[] = "tmComputeMapping";
458 >        static const char funcName[] = "tmComputeMapping";
459          int     *histo;
460          float   *cumf;
461          int     brt0, histlen, threshold, ceiling, trimmings;
# Line 422 | Line 475 | double Ldmax
475          Ldmin = Ldmax/Lddyn;
476          logLddyn = log(Lddyn);
477          Ldavg = sqrt(Ldmax*Ldmin);
478 <        i = (tms->hbrmin-MINBRT)/HISTEP;
479 <        brt0 = MINBRT + HISTEP/2 + i*HISTEP;
480 <        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
478 >        i = HISTI(tms->hbrmin);
479 >        brt0 = HISTV(i);
480 >        histlen = HISTI(tms->hbrmax) + 1 - i;
481                                          /* histogram total and mean */
482          histot = 0; sum = 0;
483          j = brt0 + histlen*HISTEP;
484          for (i = histlen; i--; ) {
485                  histot += tms->histo[i];
486 <                sum += (j -= HISTEP) * tms->histo[i];
486 >                sum += (double)(j -= HISTEP) * tms->histo[i];
487          }
488          threshold = histot*0.005 + .5;
489 <        if (threshold < 4)
489 >        if (!histot)
490                  returnErr(TM_E_TMFAIL);
491          Lwavg = tmLuminance( (double)sum / histot );
439                                        /* allocate space for mapping */
440        if (!tmNewMap(tms))
441                returnErr(TM_E_NOMEM);
492                                          /* use linear tone mapping? */
493 <        if (tms->flags & TM_F_LINEAR)
493 >        if (tms->flags & TM_F_LINEAR || threshold < 4 ||
494 >                        tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn)
495                  goto linearmap;
496                                          /* clamp histogram */
497          histo = (int *)malloc(histlen*sizeof(int));
# Line 481 | Line 532 | double Ldmax
532                          goto linearmap;
533                  }
534          } while (trimmings > threshold);
535 +                                        /* allocate space for mapping */
536 +        if (!tmNewMap(tms))
537 +                returnErr(TM_E_NOMEM);
538                                          /* assign tone-mapping */
539          for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
540                  j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
# Line 510 | Line 564 | BYTE   *cs,
564   int     len
565   )
566   {
567 <        static char     funcName[] = "tmMapPixels";
567 >        static const char funcName[] = "tmMapPixels";
568          int32   li, pv;
569  
570          if (tms == NULL || tms->lumap == NULL)
# Line 540 | Line 594 | int    len
594   }
595  
596  
543
544
597   TMstruct *
598   tmDup(                          /* duplicate top tone mapping */
599   TMstruct        *tms
# Line 558 | Line 610 | TMstruct       *tms
610                  return(NULL);
611          *tmnew = *tms;          /* copy everything */
612          if (tmnew->histo != NULL) {     /* duplicate histogram */
613 <                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
562 <                                (tmnew->hbrmin-MINBRT)/HISTEP;
613 >                len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
614                  tmnew->histo = (int *)malloc(len*sizeof(int));
615                  if (tmnew->histo != NULL)
616                          for (i = len; i--; )
# Line 621 | Line 672 | tmMkMesofact()                         /* build mesopic lookup factor table
672  
673  
674   int
675 + tmNewMap(                       /* allocate new tone-mapping array */
676 + TMstruct        *tms
677 + )
678 + {
679 +        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
680 +                                        (tms->hbrmax - tms->hbrmin)) {
681 +                free((MEM_PTR)tms->lumap);
682 +                tms->lumap = NULL;
683 +        }
684 +        tms->mbrmin = tms->hbrmin;
685 +        tms->mbrmax = tms->hbrmax;
686 +        if (tms->mbrmin > tms->mbrmax)
687 +                return 0;
688 +        if (tms->lumap == NULL)
689 +                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
690 +                                        (tms->mbrmax-tms->mbrmin+1));
691 +        return(tms->lumap != NULL);
692 + }
693 +
694 +
695 + int
696   tmErrorReturn(                          /* error return (with message) */
697 < char    *func,
697 > const char      *func,
698   TMstruct        *tms,
699   int     err
700   )
701   {
702 <        tmLastFunction = func;
703 <        tmLastError = err;
704 <        if (tms != NULL && tms->flags & TM_F_NOSTDERR)
705 <                return(err);
702 >        if (tms != NULL) {
703 >                tms->lastFunc = func;
704 >                tms->lastError = err;
705 >                if (tms->flags & TM_F_NOSTDERR)
706 >                        return(err);
707 >        }
708          fputs(func, stderr);
709          fputs(": ", stderr);
710          fputs(tmErrorMessage[err], stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines