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.34 by greg, Mon Feb 9 20:48:08 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines