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.35 by greg, Mon Mar 7 20:49:19 2011 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 +        return((TMbright)(d + .5 - (d < 0.)));
184 + }
185 +
186 +
187   int
188 + tmCvLums(                               /* convert luminances using lookup */
189 + TMbright        *ls,
190 + float           *scan,
191 + int             len
192 + )
193 + {
194 +        if (tmFloat2BrtLUT == NULL) {   /* initialize lookup table */
195 +                int32   i;
196 +                tmFloat2BrtLUT = (TMbright *)malloc(sizeof(TMbright)*0x10000);
197 +                if (tmFloat2BrtLUT == NULL)
198 +                        return(TM_E_NOMEM);
199 +                for (i = 0; i < 0x10000; i++) {
200 +                        int32   l = (i<<1 | 1) << 14;
201 + #ifndef isfinite
202 +                        if ((l & 0x7f800000) == 0x7f800000)
203 +                                tmFloat2BrtLUT[i] = TM_NOBRT;
204 +                        else
205 + #endif
206 +                        tmFloat2BrtLUT[i] = tmCvLuminance(*(float *)&l);
207 +                }
208 +        }
209 +        if (len <= 0)
210 +                return(TM_E_OK);
211 +        if ((ls == NULL) | (scan == NULL))
212 +                return(TM_E_ILLEGAL);
213 +        while (len--) {
214 +                if (*scan <= TM_NOLUM) {
215 +                        *ls++ = TM_NOBRT;
216 +                        ++scan;
217 +                        continue;
218 +                }
219 +                *ls++ = tmCvLumLUfp(scan++);
220 +        }
221 +        return(TM_E_OK);
222 + }
223 +
224 +
225 + int
226 + tmCvGrays(                              /* convert float gray values */
227 + TMstruct        *tms,
228 + TMbright        *ls,
229 + float           *scan,
230 + int             len
231 + )
232 + {
233 +        static const char funcName[] = "tmCvGrays";
234 +        int     i;
235 +
236 +        if (tms == NULL)
237 +                returnErr(TM_E_TMINVAL);
238 +        if ((ls == NULL) | (scan == NULL) | (len < 0))
239 +                returnErr(TM_E_ILLEGAL);
240 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
241 +                tmCvLums(NULL, NULL, 0);
242 +        for (i = len; i--; ) {
243 +                float   lum = tms->inpsf * scan[i];
244 +                if (lum <= TM_NOLUM)
245 +                        ls[i] = TM_NOBRT;
246 +                else
247 +                        ls[i] = tmCvLumLUfp(&lum);
248 +        }
249 +        returnOK;
250 + }
251 +
252 +
253 + int
254   tmCvColors(                             /* convert float colors */
255   TMstruct        *tms,
256   TMbright        *ls,
# Line 167 | Line 259 | COLOR  *scan,
259   int     len
260   )
261   {
262 <        static char     funcName[] = "tmCvColors";
263 <        static COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
262 >        static const char funcName[] = "tmCvColors";
263 >        static BYTE     gamtab[1024];
264 >        static double   curgam = .0;
265          COLOR   cmon;
266 <        double  lum, slum;
174 <        double  d;
266 >        float   lum, slum, d;
267          int     i;
268  
269          if (tms == NULL)
270                  returnErr(TM_E_TMINVAL);
271          if ((ls == NULL) | (scan == NULL) | (len < 0))
272                  returnErr(TM_E_ILLEGAL);
273 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
274 +                tmCvLums(NULL, NULL, 0);
275 +        if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
276 +                curgam = tms->mongam;                   /* (re)build table */
277 +                for (i = 1024; i--; )
278 +                        gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam));
279 +        }
280          for (i = len; i--; ) {
281                  if (tmNeedMatrix(tms)) {                /* get monitor RGB */
282                          colortrans(cmon, tms->cmat, scan[i]);
# Line 186 | Line 285 | int    len
285                          cmon[GRN] = tms->inpsf*scan[i][GRN];
286                          cmon[BLU] = tms->inpsf*scan[i][BLU];
287                  }
288 + #ifdef isfinite
289 +                if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f;
290 +                if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f;
291 +                if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f;
292 + #else
293 +                if (cmon[RED] < .0f) cmon[RED] = .0f;
294 +                if (cmon[GRN] < .0f) cmon[GRN] = .0f;
295 +                if (cmon[BLU] < .0f) cmon[BLU] = .0f;
296 + #endif
297                                                          /* world luminance */
298                  lum =   tms->clf[RED]*cmon[RED] +
299                          tms->clf[GRN]*cmon[GRN] +
300                          tms->clf[BLU]*cmon[BLU] ;
301 <                                                        /* check range */
302 <                if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
303 <                        lum =   tms->clf[RED]*cmon[RED] +
304 <                                tms->clf[GRN]*cmon[GRN] +
305 <                                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 <                }
301 >                if (lum <= TM_NOLUM) {                  /* convert brightness */
302 >                        lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM;
303 >                        ls[i] = TM_NOBRT;
304 >                } else
305 >                        ls[i] = tmCvLumLUfp(&lum);
306                  if (cs == TM_NOCHROM)                   /* no color? */
307                          continue;
308                  if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
309                          slum = scotlum(cmon);           /* mesopic adj. */
310 <                        if (lum < LMESLOWER)
310 >                        if (lum < LMESLOWER) {
311                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
312 <                        else {
312 >                        } else {
313                                  d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
314                                  if (tms->flags & TM_F_BW)
315                                          cmon[RED] = cmon[GRN] =
316                                                          cmon[BLU] = d*lum;
317                                  else
318                                          scalecolor(cmon, d);
319 <                                d = (1.-d)*slum;
319 >                                d = (1.f-d)*slum;
320                                  cmon[RED] += d;
321                                  cmon[GRN] += d;
322                                  cmon[BLU] += d;
# Line 224 | Line 325 | int    len
325                          cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
326                  }
327                  d = tms->clf[RED]*cmon[RED]/lum;
328 <                cs[3*i  ] = d>=.999 ? 255 :
228 <                                (int)(256.*pow(d, 1./tms->mongam));
328 >                cs[3*i  ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
329                  d = tms->clf[GRN]*cmon[GRN]/lum;
330 <                cs[3*i+1] = d>=.999 ? 255 :
231 <                                (int)(256.*pow(d, 1./tms->mongam));
330 >                cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
331                  d = tms->clf[BLU]*cmon[BLU]/lum;
332 <                cs[3*i+2] = d>=.999 ? 255 :
234 <                                (int)(256.*pow(d, 1./tms->mongam));
332 >                cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
333          }
334          returnOK;
335   }
336  
337  
338   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
339   tmAddHisto(                             /* add values to histogram */
340   TMstruct        *tms,
341   TMbright        *ls,
# Line 272 | Line 343 | int    len,
343   int     wt
344   )
345   {
346 <        static char     funcName[] = "tmAddHisto";
346 >        static const char funcName[] = "tmAddHisto";
347          int     oldorig=0, oldlen, horig, hlen;
348          int     i, j;
349  
# Line 291 | Line 362 | int    wt
362                  tms->hbrmin = tms->hbrmax = ls[i];
363                  oldlen = 0;
364          } else {
365 <                oldorig = (tms->hbrmin-MINBRT)/HISTEP;
366 <                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
365 >                oldorig = HISTI(tms->hbrmin);
366 >                oldlen = HISTI(tms->hbrmax) + 1 - oldorig;
367          }
368          for (i = len; i--; ) {
369                  if ((j = ls[i]) < MINBRT)
# Line 302 | Line 373 | int    wt
373                  else if (j > tms->hbrmax)
374                          tms->hbrmax = j;
375          }
376 <        horig = (tms->hbrmin-MINBRT)/HISTEP;
377 <        hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
376 >        horig = HISTI(tms->hbrmin);
377 >        hlen = HISTI(tms->hbrmax) + 1 - horig;
378          if (hlen > oldlen) {                    /* (re)allocate histogram */
379                  int     *newhist = (int *)calloc(hlen, sizeof(int));
380                  if (newhist == NULL)
# Line 319 | Line 390 | int    wt
390                  returnOK;
391          for (i = len; i--; )                    /* add in new counts */
392                  if (ls[i] >= MINBRT)
393 <                        tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
393 >                        tms->histo[ HISTI(ls[i]) - horig ] += wt;
394          returnOK;
395   }
396  
# Line 347 | Line 418 | double La
418   }
419  
420  
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
421   int
422   tmFixedMapping(                 /* compute fixed, linear tone-mapping */
423   TMstruct        *tms,
# Line 375 | Line 425 | double expmult,
425   double  gamval
426   )
427   {
428 <        static char     funcName[] = "tmFixedMapping";
428 >        static const char funcName[] = "tmFixedMapping";
429          double          d;
430          int     i;
431          
# Line 386 | Line 436 | double gamval
436          if (gamval < MINGAM)
437                  gamval = tms->mongam;
438          d = log(expmult/tms->inpsf);
439 <        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
440 <                tms->lumap[i] = 256. * exp(
439 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
440 >                double  val = 256. * exp(
441                          ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
442 <                        / gamval );
442 >                        / gamval);
443 >                tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val;
444 >        }
445          returnOK;
446   }
447  
# Line 402 | Line 454 | double Lddyn,
454   double  Ldmax
455   )
456   {
457 <        static char     funcName[] = "tmComputeMapping";
457 >        static const char funcName[] = "tmComputeMapping";
458          int     *histo;
459          float   *cumf;
460          int     brt0, histlen, threshold, ceiling, trimmings;
# Line 422 | Line 474 | double Ldmax
474          Ldmin = Ldmax/Lddyn;
475          logLddyn = log(Lddyn);
476          Ldavg = sqrt(Ldmax*Ldmin);
477 <        i = (tms->hbrmin-MINBRT)/HISTEP;
478 <        brt0 = MINBRT + HISTEP/2 + i*HISTEP;
479 <        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
477 >        i = HISTI(tms->hbrmin);
478 >        brt0 = HISTV(i);
479 >        histlen = HISTI(tms->hbrmax) + 1 - i;
480                                          /* histogram total and mean */
481          histot = 0; sum = 0;
482          j = brt0 + histlen*HISTEP;
483          for (i = histlen; i--; ) {
484                  histot += tms->histo[i];
485 <                sum += (j -= HISTEP) * tms->histo[i];
485 >                sum += (double)(j -= HISTEP) * tms->histo[i];
486          }
487          threshold = histot*0.005 + .5;
488 <        if (threshold < 4)
488 >        if (!histot)
489                  returnErr(TM_E_TMFAIL);
490          Lwavg = tmLuminance( (double)sum / histot );
439                                        /* allocate space for mapping */
440        if (!tmNewMap(tms))
441                returnErr(TM_E_NOMEM);
491                                          /* use linear tone mapping? */
492 <        if (tms->flags & TM_F_LINEAR)
492 >        if (tms->flags & TM_F_LINEAR || threshold < 4 ||
493 >                        tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn)
494                  goto linearmap;
495                                          /* clamp histogram */
496          histo = (int *)malloc(histlen*sizeof(int));
# Line 481 | Line 531 | double Ldmax
531                          goto linearmap;
532                  }
533          } while (trimmings > threshold);
534 +                                        /* allocate space for mapping */
535 +        if (!tmNewMap(tms))
536 +                returnErr(TM_E_NOMEM);
537                                          /* assign tone-mapping */
538          for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
539                  j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
# Line 510 | Line 563 | BYTE   *cs,
563   int     len
564   )
565   {
566 <        static char     funcName[] = "tmMapPixels";
566 >        static const char funcName[] = "tmMapPixels";
567          int32   li, pv;
568  
569          if (tms == NULL || tms->lumap == NULL)
# Line 540 | Line 593 | int    len
593   }
594  
595  
543
544
596   TMstruct *
597   tmDup(                          /* duplicate top tone mapping */
598   TMstruct        *tms
# Line 558 | Line 609 | TMstruct       *tms
609                  return(NULL);
610          *tmnew = *tms;          /* copy everything */
611          if (tmnew->histo != NULL) {     /* duplicate histogram */
612 <                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
562 <                                (tmnew->hbrmin-MINBRT)/HISTEP;
612 >                len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
613                  tmnew->histo = (int *)malloc(len*sizeof(int));
614                  if (tmnew->histo != NULL)
615                          for (i = len; i--; )
# Line 621 | Line 671 | tmMkMesofact()                         /* build mesopic lookup factor table
671  
672  
673   int
674 + tmNewMap(                       /* allocate new tone-mapping array */
675 + TMstruct        *tms
676 + )
677 + {
678 +        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
679 +                                        (tms->hbrmax - tms->hbrmin)) {
680 +                free((MEM_PTR)tms->lumap);
681 +                tms->lumap = NULL;
682 +        }
683 +        tms->mbrmin = tms->hbrmin;
684 +        tms->mbrmax = tms->hbrmax;
685 +        if (tms->mbrmin > tms->mbrmax)
686 +                return 0;
687 +        if (tms->lumap == NULL)
688 +                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
689 +                                        (tms->mbrmax-tms->mbrmin+1));
690 +        return(tms->lumap != NULL);
691 + }
692 +
693 +
694 + int
695   tmErrorReturn(                          /* error return (with message) */
696 < char    *func,
696 > const char      *func,
697   TMstruct        *tms,
698   int     err
699   )
700   {
701 <        tmLastFunction = func;
702 <        tmLastError = err;
703 <        if (tms != NULL && tms->flags & TM_F_NOSTDERR)
704 <                return(err);
701 >        if (tms != NULL) {
702 >                tms->lastFunc = func;
703 >                tms->lastError = err;
704 >                if (tms->flags & TM_F_NOSTDERR)
705 >                        return(err);
706 >        }
707          fputs(func, stderr);
708          fputs(": ", stderr);
709          fputs(tmErrorMessage[err], stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines