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.22 by greg, Thu Mar 23 22:01:43 2006 UTC vs.
Revision 3.50 by greg, Tue Apr 13 02:42:31 2021 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        <string.h>
18   #include        "tmprivat.h"
19   #include        "tmerrmsg.h"
20  
# Line 22 | Line 24 | static const char      RCSid[] = "$Id$";
24   struct tmPackage        *tmPkg[TM_MAXPKG];
25   int     tmNumPkgs = 0;                  /* number of registered packages */
26  
27 +                                        /* luminance->brightness lookup */
28 + static TMbright         *tmFloat2BrtLUT = NULL;
29  
30 + #define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15]
31 +
32 +
33   TMstruct *
34   tmInit(                                 /* initialize new tone mapping */
35   int     flags,
# Line 48 | Line 55 | double gamval
55                  tmnew->clf[GRN] = rgb2xyzmat[1][1];
56                  tmnew->clf[BLU] = rgb2xyzmat[1][2];
57          } else {
58 <                comprgb2xyzWBmat(cmat, tmnew->monpri=monpri);
58 >                comprgb2xyzmat(cmat, tmnew->monpri=monpri);
59                  tmnew->clf[RED] = cmat[1][0];
60                  tmnew->clf[GRN] = cmat[1][1];
61                  tmnew->clf[BLU] = cmat[1][2];
# Line 60 | Line 67 | double gamval
67                  tmnew->mongam = gamval;
68                                                  /* set color divisors */
69          for (i = 0; i < 3; i++)
70 <                tmnew->cdiv[i] = 256.*pow(tmnew->clf[i], 1./tmnew->mongam);
70 >                tmnew->cdiv[i] = TM_BRES*pow(tmnew->clf[i], 1./tmnew->mongam);
71  
72                                                  /* set input transform */
73          tmnew->inppri = tmnew->monpri;
# Line 111 | Line 118 | MEM_PTR        dat
118                          tms->clf[CIEX] = tms->clf[CIEZ] = 0.;
119                          tms->clf[CIEY] = 1.;
120                  } else {
121 <                        comprgb2xyzWBmat(tms->cmat, tms->monpri);
121 >                        comprgb2xyzmat(tms->cmat, tms->monpri);
122                          tms->clf[RED] = tms->cmat[1][0];
123                          tms->clf[GRN] = tms->cmat[1][1];
124                          tms->clf[BLU] = tms->cmat[1][2];
125                  }
126 <                tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] =
120 <                                tms->inpsf;
126 >                tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] = 1.;
127                  tms->cmat[0][1] = tms->cmat[0][2] = tms->cmat[1][0] =
128                  tms->cmat[1][2] = tms->cmat[2][0] = tms->cmat[2][1] = 0.;
129  
130 <        } else if (tms->inppri == TM_XYZPRIM)   /* input is XYZ */
130 >        } else if (tms->inppri == TM_XYZPRIM) {         /* input is XYZ */
131                  compxyz2rgbWBmat(tms->cmat, tms->monpri);
132  
133 <        else {                                  /* input is RGB */
133 >        } else {                                        /* input is RGB */
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] =
140 <                                256.*pow(tms->clf[i], 1./tms->mongam);
141 <                else
142 <                        tms->cdiv[i] = 1;
145 >                tms->cdiv[i] = TM_BRES*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 149 | Line 153 | MEM_PTR        dat
153  
154  
155   void
156 < tmClearHisto(                   /* clear current histogram */
156 > tmClearHisto(                           /* clear current histogram */
157   TMstruct        *tms
158   )
159   {
160          if (tms == NULL || tms->histo == NULL)
161                  return;
162 <        free((MEM_PTR)tms->histo);
162 >        free(tms->histo);
163          tms->histo = NULL;
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,
255 < BYTE    *cs,
255 > uby8    *cs,
256   COLOR   *scan,
257   int     len
258   )
259   {
260          static const char funcName[] = "tmCvColors";
261 <        static COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
174 <        static BYTE     gamtab[1024];
261 >        static uby8     gamtab[1024];
262          static double   curgam = .0;
263          COLOR   cmon;
264 <        double  lum, slum;
178 <        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--; )
# Line 195 | Line 283 | int    len
283                          cmon[GRN] = tms->inpsf*scan[i][GRN];
284                          cmon[BLU] = tms->inpsf*scan[i][BLU];
285                  }
286 < #ifdef isnan
287 <                if (isnan(cmon[RED]) || isinf(cmon[RED])) cmon[RED] = .0f;
288 <                if (isnan(cmon[GRN]) || isinf(cmon[GRN])) cmon[GRN] = .0f;
289 <                if (isnan(cmon[BLU]) || isinf(cmon[BLU])) cmon[BLU] = .0f;
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] ;
212 <                if (lum < MINLUM) {
213 <                        ls[i] = MINBRT-1;               /* bogus value */
214 <                        lum = MINLUM;
215 <                } else {
216 <                        d = TM_BRTSCALE*log(lum);       /* encode it */
217 <                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
218 <                }
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 238 | 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 : gamtab[(int)(1024.*d)];
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 : gamtab[(int)(1024.*d)];
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 : gamtab[(int)(1024.*d)];
330 >                cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
331          }
332          returnOK;
333   }
334  
335  
251 TMbright
252 tmCvLuminance(                          /* convert a single luminance */
253 double  lum
254 )
255 {
256        double  d;
257
258        if (lum <= TM_NOLUM)
259                return(TM_NOBRT);
260        d = TM_BRTSCALE*log(lum);
261        if (d > 0.)
262                return((TMbright)(d+.5));
263        return((TMbright)(d-.5));
264 }
265
266
336   int
268 tmCvGrays(                              /* convert float gray values */
269 TMstruct        *tms,
270 TMbright        *ls,
271 float   *scan,
272 int     len
273 )
274 {
275        static const char funcName[] = "tmCvGrays";
276        double  d;
277        int     i;
278
279        if (tms == NULL)
280                returnErr(TM_E_TMINVAL);
281        if ((ls == NULL) | (scan == NULL) | (len < 0))
282                returnErr(TM_E_ILLEGAL);
283        for (i = len; i--; )
284                if (scan[i] <= TM_NOLUM) {
285                        ls[i] = TM_NOBRT;               /* bogus value */
286                } else {
287                        d = TM_BRTSCALE*log(scan[i]);   /* encode it */
288                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
289                }
290        returnOK;
291 }
292
293
294 int
337   tmAddHisto(                             /* add values to histogram */
338   TMstruct        *tms,
339   TMbright        *ls,
# Line 318 | 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 329 | 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));
377 >                HIST_TYP  *newhist = (HIST_TYP *)calloc(hlen, sizeof(HIST_TYP));
378                  if (newhist == NULL)
379                          returnErr(TM_E_NOMEM);
380                  if (oldlen) {                   /* copy and free old */
381 <                        for (i = oldlen, j = i+oldorig-horig; i; )
382 <                                newhist[--j] = tms->histo[--i];
383 <                        free((MEM_PTR)tms->histo);
381 >                        memcpy(newhist+(oldorig-horig),
382 >                                        tms->histo, oldlen*sizeof(HIST_TYP));
383 >                        free(tms->histo);
384                  }
385                  tms->histo = newhist;
386          }
# Line 346 | 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 374 | Line 416 | double La
416   }
417  
418  
377 static int
378 tmNewMap(                       /* allocate new tone-mapping array */
379 TMstruct        *tms
380 )
381 {
382        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
383                                        (tms->hbrmax - tms->hbrmin)) {
384                free((MEM_PTR)tms->lumap);
385                tms->lumap = NULL;
386        }
387        tms->mbrmin = tms->hbrmin;
388        tms->mbrmax = tms->hbrmax;
389        if (tms->mbrmin > tms->mbrmax)
390                return 0;
391        if (tms->lumap == NULL)
392                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
393                                        (tms->mbrmax-tms->mbrmin+1));
394        return(tms->lumap != NULL);
395 }
396
397
419   int
420   tmFixedMapping(                 /* compute fixed, linear tone-mapping */
421   TMstruct        *tms,
422   double  expmult,
423 < double  gamval
423 > double  gamval,
424 > double  Lddyn
425   )
426   {
427          static const char funcName[] = "tmFixedMapping";
428 <        double          d;
429 <        int     i;
428 >        const int       maxV = (1L<<(8*sizeof(TMAP_TYP))) - 1;
429 >        double          minD;
430 >        int             i;
431          
432          if (!tmNewMap(tms))
433                  returnErr(TM_E_NOMEM);
434 <        if (expmult <= .0)
435 <                expmult = 1.;
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(
441 <                        ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
442 <                        / gamval );
434 >                                        /* check arguments */
435 >        if (expmult <= .0) expmult = 1.;
436 >        if (gamval < MINGAM) gamval = tms->mongam;
437 >        if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
438 >        minD = 1./Lddyn;
439 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
440 >                double  d;
441 >                d = expmult/tms->inpsf * tmLuminance(tms->mbrmin + i);
442 >                if (d >= 2.*minD)
443 >                        d -= minD;
444 >                else                    /* soft black crushing */
445 >                        d *= d/(4.*minD);
446 >                d /= 1. - minD;
447 >                d = TM_BRES*pow(d, 1./gamval);
448 >                tms->lumap[i] = (d > maxV) ? maxV : (int)d;
449 >        }
450          returnOK;
451   }
452  
# Line 430 | Line 460 | double Ldmax
460   )
461   {
462          static const char funcName[] = "tmComputeMapping";
463 <        int     *histo;
463 >        HIST_TYP        *histo;
464          float   *cumf;
465 <        int     brt0, histlen, threshold, ceiling, trimmings;
466 <        double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
467 <        int32   histot;
465 >        int     brt0, histlen;
466 >        HIST_TYP        threshold, ceiling, trimmings, histot;
467 >        double  logLddyn, Ldmin, Lwavg, Tr, Lw, Ld;
468          double  sum;
469          double  d;
470          int     i, j;
# Line 448 | Line 478 | double Ldmax
478                                          /* compute handy values */
479          Ldmin = Ldmax/Lddyn;
480          logLddyn = log(Lddyn);
481 <        Ldavg = sqrt(Ldmax*Ldmin);
482 <        i = (tms->hbrmin-MINBRT)/HISTEP;
483 <        brt0 = MINBRT + HISTEP/2 + i*HISTEP;
454 <        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
481 >        i = HISTI(tms->hbrmin);
482 >        brt0 = HISTV(i);
483 >        histlen = HISTI(tms->hbrmax) + 1 - i;
484                                          /* histogram total and mean */
485          histot = 0; sum = 0;
486          j = brt0 + histlen*HISTEP;
487          for (i = histlen; i--; ) {
488                  histot += tms->histo[i];
489 <                sum += (j -= HISTEP) * tms->histo[i];
489 >                sum += (double)(j -= HISTEP) * tms->histo[i];
490          }
491 <        threshold = histot*0.005 + .5;
463 <        if (threshold < 4)
491 >        if (!histot)
492                  returnErr(TM_E_TMFAIL);
493 +        threshold = histot/40 + 1;
494          Lwavg = tmLuminance( (double)sum / histot );
466                                        /* allocate space for mapping */
467        if (!tmNewMap(tms))
468                returnErr(TM_E_NOMEM);
495                                          /* use linear tone mapping? */
496 <        if (tms->flags & TM_F_LINEAR)
496 >        if (tms->flags & TM_F_LINEAR || threshold < 4 ||
497 >                        tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn)
498                  goto linearmap;
499                                          /* clamp histogram */
500 <        histo = (int *)malloc(histlen*sizeof(int));
500 >        histo = (HIST_TYP *)malloc(histlen*sizeof(HIST_TYP));
501          cumf = (float *)malloc((histlen+2)*sizeof(float));
502          if ((histo == NULL) | (cumf == NULL))
503                  returnErr(TM_E_NOMEM);
504          cumf[histlen+1] = 1.;           /* guard for assignment code */
505 <        for (i = histlen; i--; )        /* make malleable copy */
506 <                histo[i] = tms->histo[i];
505 >                                        /* make malleable copy */
506 >        memcpy(histo, tms->histo, histlen*sizeof(HIST_TYP));
507          do {                            /* iterate to solution */
508                  sum = 0;                /* cumulative probability */
509                  for (i = 0; i < histlen; i++) {
510 <                        cumf[i] = (double)sum/histot;
511 <                        sum += histo[i];
510 >                        cumf[i] = sum/histot;
511 >                        sum += (double)histo[i];
512                  }
513                  cumf[histlen] = 1.;
514                  Tr = histot * (double)(tms->hbrmax - tms->hbrmin) /
515 <                        ((double)histlen*TM_BRTSCALE) / logLddyn;
515 >                                ((double)TM_BRTSCALE*histlen*logLddyn);
516                  ceiling = Tr + 1.;
517                  trimmings = 0;          /* clip to envelope */
518                  for (i = histlen; i--; ) {
# Line 503 | Line 530 | double Ldmax
530                  }
531                                          /* check if we're out of data */
532                  if ((histot -= trimmings) <= threshold) {
533 <                        free((MEM_PTR)histo);
534 <                        free((MEM_PTR)cumf);
533 >                        free(histo);
534 >                        free(cumf);
535                          goto linearmap;
536                  }
537 <        } while (trimmings > threshold);
537 >        } while (40*trimmings > histot);
538 >                                        /* allocate space for mapping */
539 >        if (!tmNewMap(tms))
540 >                returnErr(TM_E_NOMEM);
541                                          /* assign tone-mapping */
542          for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
543                  j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
544                  d -= (double)j;
545                  Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
546                  d = (Ld - Ldmin)/(Ldmax - Ldmin);
547 <                tms->lumap[i] = 256.*pow(d, 1./gamval);
547 >                tms->lumap[i] = TM_BRES*pow(d, 1./gamval);
548          }
549 <        free((MEM_PTR)histo);           /* clean up and return */
550 <        free((MEM_PTR)cumf);
549 >        free(histo);                    /* clean up and return */
550 >        free(cumf);
551          returnOK;
552   linearmap:                              /* linear tone-mapping */
553          if (tms->flags & TM_F_HCONTR)
554 <                d = htcontrs(Ldavg) / htcontrs(Lwavg);
554 >                d = htcontrs(sqrt(Ldmax*Ldmin)) / htcontrs(Lwavg);
555          else
556 <                d = Ldavg / Lwavg;
557 <        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
556 >                d = Ldmax / tmLuminance(tms->hbrmax);
557 >        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval, Lddyn));
558   }
559  
560  
561   int
562   tmMapPixels(                    /* apply tone-mapping to pixel(s) */
563   TMstruct        *tms,
564 < BYTE    *ps,
564 > uby8    *ps,
565   TMbright        *ls,
566 < BYTE    *cs,
566 > uby8    *cs,
567   int     len
568   )
569   {
570          static const char funcName[] = "tmMapPixels";
571 <        int32   li, pv;
571 >        TMbright        lv;
572 >        TMAP_TYP        li;
573 >        int             pv;
574  
575          if (tms == NULL || tms->lumap == NULL)
576                  returnErr(TM_E_TMINVAL);
577          if ((ps == NULL) | (ls == NULL) | (len < 0))
578                  returnErr(TM_E_ILLEGAL);
579          while (len--) {
580 <                if ((li = *ls++) < tms->mbrmin) {
580 >                if ((lv = *ls++) < tms->mbrmin) {
581                          li = 0;
582                  } else {
583 <                        if (li > tms->mbrmax)
584 <                                li = tms->mbrmax;
585 <                        li = tms->lumap[li - tms->mbrmin];
583 >                        if (lv > tms->mbrmax)
584 >                                lv = tms->mbrmax;
585 >                        li = tms->lumap[lv - tms->mbrmin];
586                  }
587 <                if (cs == TM_NOCHROM)
588 <                        *ps++ = li>255 ? 255 : li;
589 <                else {
587 >                if (cs == TM_NOCHROM) {
588 > #if !(TM_BRES & 0xff)
589 >                        *ps++ = li>=TM_BRES ? 255 : li/(TM_BRES>>8);
590 > #else
591 >                        *ps++ = li>=TM_BRES ? 255 : (li<<8)/TM_BRES;
592 > #endif
593 >                } else {
594                          pv = *cs++ * li / tms->cdiv[RED];
595                          *ps++ = pv>255 ? 255 : pv;
596                          pv = *cs++ * li / tms->cdiv[GRN];
# Line 567 | Line 603 | int    len
603   }
604  
605  
570
571
606   TMstruct *
607 < tmDup(                          /* duplicate top tone mapping */
607 > tmDup(                          /* duplicate tone mapping */
608   TMstruct        *tms
609   )
610   {
# Line 585 | Line 619 | TMstruct       *tms
619                  return(NULL);
620          *tmnew = *tms;          /* copy everything */
621          if (tmnew->histo != NULL) {     /* duplicate histogram */
622 <                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
623 <                                (tmnew->hbrmin-MINBRT)/HISTEP;
590 <                tmnew->histo = (int *)malloc(len*sizeof(int));
622 >                len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
623 >                tmnew->histo = (HIST_TYP *)malloc(len*sizeof(HIST_TYP));
624                  if (tmnew->histo != NULL)
625 <                        for (i = len; i--; )
593 <                                tmnew->histo[i] = tms->histo[i];
625 >                        memcpy(tmnew->histo, tms->histo, len*sizeof(HIST_TYP));
626          }
627          if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */
628                  len = tmnew->mbrmax-tmnew->mbrmin+1;
629 <                tmnew->lumap = (unsigned short *)malloc(
598 <                                                len*sizeof(unsigned short) );
629 >                tmnew->lumap = (TMAP_TYP *)malloc(len*sizeof(TMAP_TYP));
630                  if (tmnew->lumap != NULL)
631 <                        for (i = len; i--; )
601 <                                tmnew->lumap[i] = tms->lumap[i];
631 >                        memcpy(tmnew->lumap, tms->lumap, len*sizeof(TMAP_TYP));
632          }
633                                          /* clear package data */
634          for (i = tmNumPkgs; i--; )
# Line 618 | Line 648 | TMstruct       *tms;
648                  return;
649                                          /* free tables */
650          if (tms->histo != NULL)
651 <                free((MEM_PTR)tms->histo);
651 >                free(tms->histo);
652          if (tms->lumap != NULL)
653 <                free((MEM_PTR)tms->lumap);
653 >                free(tms->lumap);
654                                          /* free private data */
655          for (i = tmNumPkgs; i--; )
656                  if (tms->pd[i] != NULL)
657                          (*tmPkg[i]->Free)(tms->pd[i]);
658 <        free((MEM_PTR)tms);             /* free basic structure */
658 >        free(tms);                      /* free basic structure */
659   }
660  
661   /******************** Shared but Private library routines *********************/
662  
663 < BYTE    tmMesofact[BMESUPPER-BMESLOWER];
663 > uby8    tmMesofact[BMESUPPER-BMESLOWER];
664  
665   void
666   tmMkMesofact()                          /* build mesopic lookup factor table */
# Line 644 | Line 674 | tmMkMesofact()                         /* build mesopic lookup factor table
674                  tmMesofact[i-BMESLOWER] = 256. *
675                                  (tmLuminance(i) - LMESLOWER) /
676                                  (LMESUPPER - LMESLOWER);
677 + }
678 +
679 +
680 + int
681 + tmNewMap(                       /* allocate new tone-mapping array */
682 + TMstruct        *tms
683 + )
684 + {
685 +        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
686 +                                        (tms->hbrmax - tms->hbrmin)) {
687 +                free(tms->lumap);
688 +                tms->lumap = NULL;
689 +        }
690 +        tms->mbrmin = tms->hbrmin;
691 +        tms->mbrmax = tms->hbrmax;
692 +        if (tms->mbrmin > tms->mbrmax)
693 +                return(0);
694 +        if (tms->lumap == NULL)
695 +                tms->lumap = (TMAP_TYP *)calloc(tms->mbrmax-tms->mbrmin+1,
696 +                                                sizeof(TMAP_TYP));
697 +        else
698 +                memset(tms->lumap, 0, (tms->mbrmax-tms->mbrmin+1)*sizeof(TMAP_TYP));
699 +
700 +        return(tms->lumap != NULL);
701   }
702  
703  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines