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.36 by greg, Fri Apr 22 14:35:54 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 126 | Line 134 | double sf
134                  if (tms->inppri != tms->monpri &&
135                                  PRIMEQ(tms->inppri, tms->monpri))
136                          tms->inppri = tms->monpri;      /* no xform */
137 <                comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri);
137 >                if (!comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri))
138 >                        returnErr(TM_E_ILLEGAL);
139          }
140          for (i = 0; i < 3; i++)
141                  for (j = 0; j < 3; j++)
142                          tms->cmat[i][j] *= tms->inpsf;
143                                                  /* set color divisors */
144          for (i = 0; i < 3; i++)
145 <                if (tms->clf[i] > .001)
146 <                        tms->cdiv[i] =
138 <                                256.*pow(tms->clf[i], 1./tms->mongam);
139 <                else
140 <                        tms->cdiv[i] = 1;
145 >                tms->cdiv[i] = 256.*pow(tms->clf[i] < .001 ? .001 :
146 >                                                tms->clf[i], 1./tms->mongam);
147                                                  /* notify packages */
148          for (i = tmNumPkgs; i--; )
149                  if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL)
# Line 147 | Line 153 | double sf
153  
154  
155   void
156 < tmClearHisto(                   /* clear current histogram */
156 > tmClearHisto(                           /* clear current histogram */
157   TMstruct        *tms
158   )
159   {
# Line 158 | Line 164 | TMstruct       *tms
164   }
165  
166  
167 + TMbright
168 + tmCvLuminance(                          /* convert a single luminance */
169 + double  lum
170 + )
171 + {
172 +        double  d;
173 +
174 + #ifdef isfinite
175 +        if (!isfinite(lum) || lum <= TM_NOLUM)
176 + #else
177 +        if (lum <= TM_NOLUM)
178 + #endif
179 +                return(TM_NOBRT);
180 +        d = TM_BRTSCALE*log(lum);
181 +        return((TMbright)(d + .5 - (d < 0.)));
182 + }
183 +
184 +
185   int
186 + tmCvLums(                               /* convert luminances using lookup */
187 + TMbright        *ls,
188 + float           *scan,
189 + int             len
190 + )
191 + {
192 +        if (tmFloat2BrtLUT == NULL) {   /* initialize lookup table */
193 +                int32   i;
194 +                tmFloat2BrtLUT = (TMbright *)malloc(sizeof(TMbright)*0x10000);
195 +                if (tmFloat2BrtLUT == NULL)
196 +                        return(TM_E_NOMEM);
197 +                for (i = 0; i < 0x10000; i++) {
198 +                        int32   l = (i<<1 | 1) << 14;
199 + #ifndef isfinite
200 +                        if ((l & 0x7f800000) == 0x7f800000)
201 +                                tmFloat2BrtLUT[i] = TM_NOBRT;
202 +                        else
203 + #endif
204 +                        tmFloat2BrtLUT[i] = tmCvLuminance(*(float *)&l);
205 +                }
206 +        }
207 +        if (len <= 0)
208 +                return(TM_E_OK);
209 +        if ((ls == NULL) | (scan == NULL))
210 +                return(TM_E_ILLEGAL);
211 +        while (len--) {
212 +                if (*scan <= TM_NOLUM) {
213 +                        *ls++ = TM_NOBRT;
214 +                        ++scan;
215 +                        continue;
216 +                }
217 +                *ls++ = tmCvLumLUfp(scan++);
218 +        }
219 +        return(TM_E_OK);
220 + }
221 +
222 +
223 + int
224 + tmCvGrays(                              /* convert float gray values */
225 + TMstruct        *tms,
226 + TMbright        *ls,
227 + float           *scan,
228 + int             len
229 + )
230 + {
231 +        static const char funcName[] = "tmCvGrays";
232 +        int     i;
233 +
234 +        if (tms == NULL)
235 +                returnErr(TM_E_TMINVAL);
236 +        if ((ls == NULL) | (scan == NULL) | (len < 0))
237 +                returnErr(TM_E_ILLEGAL);
238 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
239 +                tmCvLums(NULL, NULL, 0);
240 +        for (i = len; i--; ) {
241 +                float   lum = tms->inpsf * scan[i];
242 +                if (lum <= TM_NOLUM)
243 +                        ls[i] = TM_NOBRT;
244 +                else
245 +                        ls[i] = tmCvLumLUfp(&lum);
246 +        }
247 +        returnOK;
248 + }
249 +
250 +
251 + int
252   tmCvColors(                             /* convert float colors */
253   TMstruct        *tms,
254   TMbright        *ls,
# Line 167 | Line 257 | COLOR  *scan,
257   int     len
258   )
259   {
260 <        static char     funcName[] = "tmCvColors";
261 <        static COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
260 >        static const char funcName[] = "tmCvColors";
261 >        static BYTE     gamtab[1024];
262 >        static double   curgam = .0;
263          COLOR   cmon;
264 <        double  lum, slum;
174 <        double  d;
264 >        float   lum, slum, d;
265          int     i;
266  
267          if (tms == NULL)
268                  returnErr(TM_E_TMINVAL);
269          if ((ls == NULL) | (scan == NULL) | (len < 0))
270                  returnErr(TM_E_ILLEGAL);
271 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
272 +                tmCvLums(NULL, NULL, 0);
273 +        if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
274 +                curgam = tms->mongam;                   /* (re)build table */
275 +                for (i = 1024; i--; )
276 +                        gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam));
277 +        }
278          for (i = len; i--; ) {
279                  if (tmNeedMatrix(tms)) {                /* get monitor RGB */
280                          colortrans(cmon, tms->cmat, scan[i]);
# Line 186 | Line 283 | int    len
283                          cmon[GRN] = tms->inpsf*scan[i][GRN];
284                          cmon[BLU] = tms->inpsf*scan[i][BLU];
285                  }
286 + #ifdef isfinite
287 +                if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f;
288 +                if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f;
289 +                if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f;
290 + #else
291 +                if (cmon[RED] < .0f) cmon[RED] = .0f;
292 +                if (cmon[GRN] < .0f) cmon[GRN] = .0f;
293 +                if (cmon[BLU] < .0f) cmon[BLU] = .0f;
294 + #endif
295                                                          /* world luminance */
296                  lum =   tms->clf[RED]*cmon[RED] +
297                          tms->clf[GRN]*cmon[GRN] +
298                          tms->clf[BLU]*cmon[BLU] ;
299 <                                                        /* check range */
300 <                if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
301 <                        lum =   tms->clf[RED]*cmon[RED] +
302 <                                tms->clf[GRN]*cmon[GRN] +
303 <                                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 <                }
299 >                if (lum <= TM_NOLUM) {                  /* convert brightness */
300 >                        lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM;
301 >                        ls[i] = TM_NOBRT;
302 >                } else
303 >                        ls[i] = tmCvLumLUfp(&lum);
304                  if (cs == TM_NOCHROM)                   /* no color? */
305                          continue;
306                  if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
307                          slum = scotlum(cmon);           /* mesopic adj. */
308 <                        if (lum < LMESLOWER)
308 >                        if (lum < LMESLOWER) {
309                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
310 <                        else {
310 >                        } else {
311                                  d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
312                                  if (tms->flags & TM_F_BW)
313                                          cmon[RED] = cmon[GRN] =
314                                                          cmon[BLU] = d*lum;
315                                  else
316                                          scalecolor(cmon, d);
317 <                                d = (1.-d)*slum;
317 >                                d = (1.f-d)*slum;
318                                  cmon[RED] += d;
319                                  cmon[GRN] += d;
320                                  cmon[BLU] += d;
# Line 224 | Line 323 | int    len
323                          cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
324                  }
325                  d = tms->clf[RED]*cmon[RED]/lum;
326 <                cs[3*i  ] = d>=.999 ? 255 :
228 <                                (int)(256.*pow(d, 1./tms->mongam));
326 >                cs[3*i  ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
327                  d = tms->clf[GRN]*cmon[GRN]/lum;
328 <                cs[3*i+1] = d>=.999 ? 255 :
231 <                                (int)(256.*pow(d, 1./tms->mongam));
328 >                cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
329                  d = tms->clf[BLU]*cmon[BLU]/lum;
330 <                cs[3*i+2] = d>=.999 ? 255 :
234 <                                (int)(256.*pow(d, 1./tms->mongam));
330 >                cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
331          }
332          returnOK;
333   }
334  
335  
336   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
337   tmAddHisto(                             /* add values to histogram */
338   TMstruct        *tms,
339   TMbright        *ls,
# Line 272 | Line 341 | int    len,
341   int     wt
342   )
343   {
344 <        static char     funcName[] = "tmAddHisto";
344 >        static const char funcName[] = "tmAddHisto";
345          int     oldorig=0, oldlen, horig, hlen;
346          int     i, j;
347  
# Line 291 | Line 360 | int    wt
360                  tms->hbrmin = tms->hbrmax = ls[i];
361                  oldlen = 0;
362          } else {
363 <                oldorig = (tms->hbrmin-MINBRT)/HISTEP;
364 <                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
363 >                oldorig = HISTI(tms->hbrmin);
364 >                oldlen = HISTI(tms->hbrmax) + 1 - oldorig;
365          }
366          for (i = len; i--; ) {
367                  if ((j = ls[i]) < MINBRT)
# Line 302 | Line 371 | int    wt
371                  else if (j > tms->hbrmax)
372                          tms->hbrmax = j;
373          }
374 <        horig = (tms->hbrmin-MINBRT)/HISTEP;
375 <        hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
374 >        horig = HISTI(tms->hbrmin);
375 >        hlen = HISTI(tms->hbrmax) + 1 - horig;
376          if (hlen > oldlen) {                    /* (re)allocate histogram */
377                  int     *newhist = (int *)calloc(hlen, sizeof(int));
378                  if (newhist == NULL)
# Line 319 | Line 388 | int    wt
388                  returnOK;
389          for (i = len; i--; )                    /* add in new counts */
390                  if (ls[i] >= MINBRT)
391 <                        tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
391 >                        tms->histo[ HISTI(ls[i]) - horig ] += wt;
392          returnOK;
393   }
394  
# Line 347 | Line 416 | double La
416   }
417  
418  
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
419   int
420   tmFixedMapping(                 /* compute fixed, linear tone-mapping */
421   TMstruct        *tms,
# Line 375 | Line 423 | double expmult,
423   double  gamval
424   )
425   {
426 <        static char     funcName[] = "tmFixedMapping";
426 >        static const char funcName[] = "tmFixedMapping";
427          double          d;
428          int     i;
429          
# Line 386 | Line 434 | double gamval
434          if (gamval < MINGAM)
435                  gamval = tms->mongam;
436          d = log(expmult/tms->inpsf);
437 <        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
438 <                tms->lumap[i] = 256. * exp(
437 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
438 >                double  val = 256. * exp(
439                          ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
440 <                        / gamval );
440 >                        / gamval);
441 >                tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val;
442 >        }
443          returnOK;
444   }
445  
# Line 402 | Line 452 | double Lddyn,
452   double  Ldmax
453   )
454   {
455 <        static char     funcName[] = "tmComputeMapping";
455 >        static const char funcName[] = "tmComputeMapping";
456          int     *histo;
457          float   *cumf;
458          int     brt0, histlen, threshold, ceiling, trimmings;
# Line 422 | Line 472 | double Ldmax
472          Ldmin = Ldmax/Lddyn;
473          logLddyn = log(Lddyn);
474          Ldavg = sqrt(Ldmax*Ldmin);
475 <        i = (tms->hbrmin-MINBRT)/HISTEP;
476 <        brt0 = MINBRT + HISTEP/2 + i*HISTEP;
477 <        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
475 >        i = HISTI(tms->hbrmin);
476 >        brt0 = HISTV(i);
477 >        histlen = HISTI(tms->hbrmax) + 1 - i;
478                                          /* histogram total and mean */
479          histot = 0; sum = 0;
480          j = brt0 + histlen*HISTEP;
481          for (i = histlen; i--; ) {
482                  histot += tms->histo[i];
483 <                sum += (j -= HISTEP) * tms->histo[i];
483 >                sum += (double)(j -= HISTEP) * tms->histo[i];
484          }
485          threshold = histot*0.005 + .5;
486 <        if (threshold < 4)
486 >        if (!histot)
487                  returnErr(TM_E_TMFAIL);
488          Lwavg = tmLuminance( (double)sum / histot );
439                                        /* allocate space for mapping */
440        if (!tmNewMap(tms))
441                returnErr(TM_E_NOMEM);
489                                          /* use linear tone mapping? */
490 <        if (tms->flags & TM_F_LINEAR)
490 >        if (tms->flags & TM_F_LINEAR || threshold < 4 ||
491 >                        tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn)
492                  goto linearmap;
493                                          /* clamp histogram */
494          histo = (int *)malloc(histlen*sizeof(int));
# Line 481 | Line 529 | double Ldmax
529                          goto linearmap;
530                  }
531          } while (trimmings > threshold);
532 +                                        /* allocate space for mapping */
533 +        if (!tmNewMap(tms))
534 +                returnErr(TM_E_NOMEM);
535                                          /* assign tone-mapping */
536          for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
537                  j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
# Line 510 | Line 561 | BYTE   *cs,
561   int     len
562   )
563   {
564 <        static char     funcName[] = "tmMapPixels";
564 >        static const char funcName[] = "tmMapPixels";
565          int32   li, pv;
566  
567          if (tms == NULL || tms->lumap == NULL)
# Line 540 | Line 591 | int    len
591   }
592  
593  
543
544
594   TMstruct *
595   tmDup(                          /* duplicate top tone mapping */
596   TMstruct        *tms
# Line 558 | Line 607 | TMstruct       *tms
607                  return(NULL);
608          *tmnew = *tms;          /* copy everything */
609          if (tmnew->histo != NULL) {     /* duplicate histogram */
610 <                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
562 <                                (tmnew->hbrmin-MINBRT)/HISTEP;
610 >                len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
611                  tmnew->histo = (int *)malloc(len*sizeof(int));
612                  if (tmnew->histo != NULL)
613                          for (i = len; i--; )
# Line 621 | Line 669 | tmMkMesofact()                         /* build mesopic lookup factor table
669  
670  
671   int
672 + tmNewMap(                       /* allocate new tone-mapping array */
673 + TMstruct        *tms
674 + )
675 + {
676 +        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
677 +                                        (tms->hbrmax - tms->hbrmin)) {
678 +                free((MEM_PTR)tms->lumap);
679 +                tms->lumap = NULL;
680 +        }
681 +        tms->mbrmin = tms->hbrmin;
682 +        tms->mbrmax = tms->hbrmax;
683 +        if (tms->mbrmin > tms->mbrmax)
684 +                return 0;
685 +        if (tms->lumap == NULL)
686 +                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
687 +                                        (tms->mbrmax-tms->mbrmin+1));
688 +        return(tms->lumap != NULL);
689 + }
690 +
691 +
692 + int
693   tmErrorReturn(                          /* error return (with message) */
694 < char    *func,
694 > const char      *func,
695   TMstruct        *tms,
696   int     err
697   )
698   {
699 <        tmLastFunction = func;
700 <        tmLastError = err;
701 <        if (tms != NULL && tms->flags & TM_F_NOSTDERR)
702 <                return(err);
699 >        if (tms != NULL) {
700 >                tms->lastFunc = func;
701 >                tms->lastError = err;
702 >                if (tms->flags & TM_F_NOSTDERR)
703 >                        return(err);
704 >        }
705          fputs(func, stderr);
706          fputs(": ", stderr);
707          fputs(tmErrorMessage[err], stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines