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.2 by greg, Tue Apr 15 20:04:43 1997 UTC vs.
Revision 3.12 by greg, Tue Jul 15 00:30:09 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Tone mapping functions.
6   * See tonemap.h for detailed function descriptions.
7 + * Added von Kries white-balance calculations 10/01 (GW).
8 + *
9 + * Externals declared in tonemap.h
10   */
11  
12 + #include "copyright.h"
13 +
14   #include        <stdio.h>
15   #include        <math.h>
16   #include        "tmprivat.h"
# Line 18 | Line 20 | static char SCCSid[] = "$SunId$ LBL";
20  
21   struct tmStruct *tmTop = NULL;          /* current tone mapping stack */
22  
23 +                                        /* our list of conversion packages */
24 + struct tmPackage        *tmPkg[TM_MAXPKG];
25 + int     tmNumPkgs = 0;                  /* number of registered packages */
26  
27 < int
28 < tmErrorReturn(func, err)                /* error return (with message) */
24 < char    *func;
25 < int     err;
26 < {
27 <        if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR)
28 <                return(err);
29 <        fputs(func, stderr);
30 <        fputs(": ", stderr);
31 <        fputs(tmErrorMessage[err], stderr);
32 <        fputs("!\n", stderr);
33 <        return(err);
34 < }
27 > int     tmLastError;                    /* last error incurred by library */
28 > char    *tmLastFunction;                /* error-generating function name */
29  
30  
31   struct tmStruct *
# Line 40 | Line 34 | int    flags;
34   RGBPRIMP        monpri;
35   double  gamval;
36   {
43        static char     funcName[] = "tmInit";
37          COLORMAT        cmat;
38          register struct tmStruct        *tmnew;
39          register int    i;
# Line 57 | Line 50 | double gamval;
50                  tmnew->clf[GRN] = rgb2xyzmat[1][1];
51                  tmnew->clf[BLU] = rgb2xyzmat[1][2];
52          } else {
53 <                comprgb2xyzmat(cmat, tmnew->monpri=monpri);
53 >                comprgb2xyzWBmat(cmat, tmnew->monpri=monpri);
54                  tmnew->clf[RED] = cmat[1][0];
55                  tmnew->clf[GRN] = cmat[1][1];
56                  tmnew->clf[BLU] = cmat[1][2];
57          }
65        tmnew->clfb[RED] = 256.*tmnew->clf[RED] + .5;
66        tmnew->clfb[GRN] = 256.*tmnew->clf[GRN] + .5;
67        tmnew->clfb[BLU] = 256.*tmnew->clf[BLU] + .5;
68        tmnew->clfb[EXP] = COLXS;
58                                                  /* set gamma value */
59          if (gamval < MINGAM)
60                  tmnew->mongam = DEFGAM;
61          else
62                  tmnew->mongam = gamval;
63 <        for (i = TM_GAMTSZ; i--; )
64 <                tmnew->gamb[i] = 256.*pow((i+.5)/TM_GAMTSZ, 1./tmnew->mongam);
63 >                                                /* set color divisors */
64 >        for (i = 0; i < 3; i++)
65 >                tmnew->cdiv[i] = 256.*pow(tmnew->clf[i], 1./tmnew->mongam);
66 >
67                                                  /* set input transform */
68          tmnew->inppri = tmnew->monpri;
69          tmnew->cmat[0][0] = tmnew->cmat[1][1] = tmnew->cmat[2][2] =
70                          tmnew->inpsf = WHTEFFICACY;
80        tmnew->inpsfb = TM_BRTSCALE*log(tmnew->inpsf) + .5;
71          tmnew->cmat[0][1] = tmnew->cmat[0][2] = tmnew->cmat[1][0] =
72          tmnew->cmat[1][2] = tmnew->cmat[2][0] = tmnew->cmat[2][1] = 0.;
73 <        tmnew->flags &= ~TM_F_NEEDMAT;
84 <        tmnew->brmin = tmnew->brmax = 0;
73 >        tmnew->hbrmin = 10; tmnew->hbrmax = -10;
74          tmnew->histo = NULL;
75 +        tmnew->mbrmin = 10; tmnew->mbrmax = -10;
76          tmnew->lumap = NULL;
77 <        tmnew->tmprev = NULL;
78 <
79 <        tmnew->flags |= TM_F_INITED;
80 <                                                /* make it current */
77 >                                                /* zero private data */
78 >        for (i = TM_MAXPKG; i--; )
79 >                tmnew->pd[i] = NULL;
80 >                                                /* make tmnew current */
81          tmnew->tmprev = tmTop;
82          return(tmTop = tmnew);
83   }
# Line 110 | Line 100 | double sf;
100                  returnOK;
101          tmTop->inppri = pri;                    /* let's set it */
102          tmTop->inpsf = sf;
113        tmTop->inpsfb = TM_BRTSCALE*log(sf) + (sf>=1. ? .5 : -.5);
103  
104          if (tmTop->flags & TM_F_BW) {           /* color doesn't matter */
105                  tmTop->monpri = tmTop->inppri;          /* eliminate xform */
106                  if (tmTop->inppri == TM_XYZPRIM) {
107                          tmTop->clf[CIEX] = tmTop->clf[CIEZ] = 0.;
108                          tmTop->clf[CIEY] = 1.;
120                        tmTop->clfb[CIEX] = tmTop->clfb[CIEZ] = 0;
121                        tmTop->clfb[CIEY] = 255;
109                  } else {
110 <                        comprgb2xyzmat(tmTop->cmat, tmTop->monpri);
110 >                        comprgb2xyzWBmat(tmTop->cmat, tmTop->monpri);
111                          tmTop->clf[RED] = tmTop->cmat[1][0];
112                          tmTop->clf[GRN] = tmTop->cmat[1][1];
113                          tmTop->clf[BLU] = tmTop->cmat[1][2];
# Line 131 | Line 118 | double sf;
118                  tmTop->cmat[1][2] = tmTop->cmat[2][0] = tmTop->cmat[2][1] = 0.;
119  
120          } else if (tmTop->inppri == TM_XYZPRIM) /* input is XYZ */
121 <                compxyz2rgbmat(tmTop->cmat, tmTop->monpri);
121 >                compxyz2rgbWBmat(tmTop->cmat, tmTop->monpri);
122  
123          else {                                  /* input is RGB */
124                  if (tmTop->inppri != tmTop->monpri &&
125                                  PRIMEQ(tmTop->inppri, tmTop->monpri))
126                          tmTop->inppri = tmTop->monpri;  /* no xform */
127 <                comprgb2rgbmat(tmTop->cmat, tmTop->inppri, tmTop->monpri);
127 >                comprgb2rgbWBmat(tmTop->cmat, tmTop->inppri, tmTop->monpri);
128          }
129          for (i = 0; i < 3; i++)
130                  for (j = 0; j < 3; j++)
131                          tmTop->cmat[i][j] *= tmTop->inpsf;
132 <        if (tmTop->inppri != tmTop->monpri)
133 <                tmTop->flags |= TM_F_NEEDMAT;
134 <        else
135 <                tmTop->flags &= ~TM_F_NEEDMAT;
132 >                                                /* set color divisors */
133 >        for (i = 0; i < 3; i++)
134 >                if (tmTop->clf[i] > .001)
135 >                        tmTop->cdiv[i] =
136 >                                256.*pow(tmTop->clf[i], 1./tmTop->mongam);
137 >                else
138 >                        tmTop->cdiv[i] = 1;
139 >                                                /* notify packages */
140 >        for (i = tmNumPkgs; i--; )
141 >                if (tmTop->pd[i] != NULL && tmPkg[i]->NewSpace != NULL)
142 >                        (*tmPkg[i]->NewSpace)(tmTop);
143          returnOK;
144   }
145  
# Line 155 | Line 149 | tmClearHisto()                 /* clear current histogram */
149   {
150          if (tmTop == NULL || tmTop->histo == NULL)
151                  return;
152 <        free((char *)tmTop->histo);
152 >        free((MEM_PTR)tmTop->histo);
153          tmTop->histo = NULL;
154   }
155  
# Line 168 | Line 162 | COLOR  *scan;
162   int     len;
163   {
164          static char     funcName[] = "tmCvColors";
165 +        static COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
166          COLOR   cmon;
167          double  lum, slum;
168          register double d;
# Line 175 | Line 170 | int    len;
170  
171          if (tmTop == NULL)
172                  returnErr(TM_E_TMINVAL);
173 <        if (ls == NULL | scan == NULL | len <= 0)
173 >        if (ls == NULL | scan == NULL | len < 0)
174                  returnErr(TM_E_ILLEGAL);
175          for (i = len; i--; ) {
176 <                if (tmTop->flags & TM_F_NEEDMAT)        /* get monitor RGB */
176 >                if (tmNeedMatrix(tmTop))                /* get monitor RGB */
177                          colortrans(cmon, tmTop->cmat, scan[i]);
178                  else {
179                          cmon[RED] = tmTop->inpsf*scan[i][RED];
# Line 190 | Line 185 | int    len;
185                          tmTop->clf[GRN]*cmon[GRN] +
186                          tmTop->clf[BLU]*cmon[BLU] ;
187                                                          /* check range */
188 <                if (clipgamut(cmon, lum, CGAMUT_LOWER, cblack, cwhite))
188 >                if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
189                          lum =   tmTop->clf[RED]*cmon[RED] +
190                                  tmTop->clf[GRN]*cmon[GRN] +
191                                  tmTop->clf[BLU]*cmon[BLU] ;
# Line 223 | Line 218 | int    len;
218                          cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
219                  }
220                  d = tmTop->clf[RED]*cmon[RED]/lum;
221 <                /* cs[3*i  ] = d>.999 ? 255 : 256.*pow(d, 1./tmTop->mongam); */
222 <                cs[3*i  ] = d>.999 ? 255 : tmTop->gamb[(int)(d*TM_GAMTSZ)];
221 >                cs[3*i  ] = d>=.999 ? 255 :
222 >                                (int)(256.*pow(d, 1./tmTop->mongam));
223                  d = tmTop->clf[GRN]*cmon[GRN]/lum;
224 <                /* cs[3*i+1] = d>.999 ? 255 : 256.*pow(d, 1./tmTop->mongam); */
225 <                cs[3*i+1] = d>.999 ? 255 : tmTop->gamb[(int)(d*TM_GAMTSZ)];
224 >                cs[3*i+1] = d>=.999 ? 255 :
225 >                                (int)(256.*pow(d, 1./tmTop->mongam));
226                  d = tmTop->clf[BLU]*cmon[BLU]/lum;
227 <                /* cs[3*i+2] = d>.999 ? 255 : 256.*pow(d, 1./tmTop->mongam); */
228 <                cs[3*i+2] = d>.999 ? 255 : tmTop->gamb[(int)(d*TM_GAMTSZ)];
227 >                cs[3*i+2] = d>=.999 ? 255 :
228 >                                (int)(256.*pow(d, 1./tmTop->mongam));
229          }
230          returnOK;
231   }
232  
233  
234   int
235 + tmCvGrays(ls, scan, len)                /* convert float gray values */
236 + TMbright        *ls;
237 + float   *scan;
238 + int     len;
239 + {
240 +        static char     funcName[] = "tmCvGrays";
241 +        register double d;
242 +        register int    i;
243 +
244 +        if (tmTop == NULL)
245 +                returnErr(TM_E_TMINVAL);
246 +        if (ls == NULL | scan == NULL | len < 0)
247 +                returnErr(TM_E_ILLEGAL);
248 +        for (i = len; i--; )
249 +                if (scan[i] <= TM_NOLUM) {
250 +                        ls[i] = TM_NOBRT;               /* bogus value */
251 +                } else {
252 +                        d = TM_BRTSCALE*log(scan[i]);   /* encode it */
253 +                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
254 +                }
255 +        returnOK;
256 + }
257 +
258 +
259 + int
260   tmAddHisto(ls, len, wt)                 /* add values to histogram */
261   register TMbright       *ls;
262   int     len;
263   int     wt;
264   {
265          static char     funcName[] = "tmAddHisto";
266 <        int     sum, oldorig, oldlen, horig, hlen;
266 >        int     oldorig=0, oldlen, horig, hlen;
267          register int    i, j;
268  
249        if (len <= 0)
250                returnErr(TM_E_ILLEGAL);
269          if (tmTop == NULL)
270                  returnErr(TM_E_TMINVAL);
271 +        if (len < 0)
272 +                returnErr(TM_E_ILLEGAL);
273 +        if (len == 0)
274 +                returnOK;
275                                                  /* first, grow limits */
276          if (tmTop->histo == NULL) {
277                  for (i = len; i-- && ls[i] < MINBRT; )
278                          ;
279                  if (i < 0)
280                          returnOK;
281 <                tmTop->brmin = tmTop->brmax = ls[i];
281 >                tmTop->hbrmin = tmTop->hbrmax = ls[i];
282                  oldlen = 0;
283          } else {
284 <                oldorig = (tmTop->brmin-MINBRT)/HISTEP;
285 <                oldlen = (tmTop->brmax-MINBRT)/HISTEP + 1 - oldorig;
284 >                oldorig = (tmTop->hbrmin-MINBRT)/HISTEP;
285 >                oldlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
286          }
287          for (i = len; i--; ) {
288                  if ((j = ls[i]) < MINBRT)
289                          continue;
290 <                if (j < tmTop->brmin)
291 <                        tmTop->brmin = j;
292 <                else if (j > tmTop->brmax)
293 <                        tmTop->brmax = j;
290 >                if (j < tmTop->hbrmin)
291 >                        tmTop->hbrmin = j;
292 >                else if (j > tmTop->hbrmax)
293 >                        tmTop->hbrmax = j;
294          }
295 <        horig = (tmTop->brmin-MINBRT)/HISTEP;
296 <        hlen = (tmTop->brmax-MINBRT)/HISTEP + 1 - horig;
295 >        horig = (tmTop->hbrmin-MINBRT)/HISTEP;
296 >        hlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - horig;
297          if (hlen > oldlen) {                    /* (re)allocate histogram */
298                  register int    *newhist = (int *)calloc(hlen, sizeof(int));
299                  if (newhist == NULL)
# Line 279 | Line 301 | int    wt;
301                  if (oldlen) {                   /* copy and free old */
302                          for (i = oldlen, j = i+oldorig-horig; i; )
303                                  newhist[--j] = tmTop->histo[--i];
304 <                        free((char *)tmTop->histo);
304 >                        free((MEM_PTR)tmTop->histo);
305                  }
306                  tmTop->histo = newhist;
285                if (tmTop->lumap != NULL) {     /* invalid tone map */
286                        free((char *)tmTop->lumap);
287                        tmTop->lumap = NULL;
288                }
307          }
308          if (wt == 0)
309                  returnOK;
# Line 318 | Line 336 | double La;
336   }
337  
338  
339 + static int
340 + tmNewMap()
341 + {
342 +        if (tmTop->lumap != NULL && (tmTop->mbrmax - tmTop->mbrmin) !=
343 +                                        (tmTop->hbrmax - tmTop->hbrmin)) {
344 +                free((MEM_PTR)tmTop->lumap);
345 +                tmTop->lumap = NULL;
346 +        }
347 +        tmTop->mbrmin = tmTop->hbrmin;
348 +        tmTop->mbrmax = tmTop->hbrmax;
349 +        if (tmTop->mbrmin > tmTop->mbrmax)
350 +                return 0;
351 +        if (tmTop->lumap == NULL)
352 +                tmTop->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
353 +                                        (tmTop->mbrmax-tmTop->mbrmin+1));
354 +        return(tmTop->lumap != NULL);
355 + }
356 +
357 +
358   int
359 + tmFixedMapping(expmult, gamval)
360 + double  expmult;
361 + double  gamval;
362 + {
363 +        static char     funcName[] = "tmFixedMapping";
364 +        double          d;
365 +        register int    i;
366 +        
367 +        if (!tmNewMap())
368 +                returnErr(TM_E_NOMEM);
369 +        if (expmult <= .0)
370 +                expmult = 1.;
371 +        if (gamval < MINGAM)
372 +                gamval = tmTop->mongam;
373 +        d = log(expmult/tmTop->inpsf);
374 +        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; )
375 +                tmTop->lumap[i] = 256. * exp(
376 +                        ( d + (tmTop->mbrmin+i)*(1./TM_BRTSCALE) )
377 +                        / gamval );
378 +        returnOK;
379 + }
380 +
381 +
382 + int
383   tmComputeMapping(gamval, Lddyn, Ldmax)
384   double  gamval;
385   double  Lddyn;
# Line 327 | Line 388 | double Ldmax;
388          static char     funcName[] = "tmComputeMapping";
389          int     *histo;
390          float   *cumf;
391 <        int     brt0, histlen, histot, threshold, ceiling, trimmings;
391 >        int     brt0, histlen, threshold, ceiling, trimmings;
392          double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
393 <        int4    sum;
393 >        int32   histot;
394 >        double  sum;
395          register double d;
396          register int    i, j;
397  
# Line 343 | Line 405 | double Ldmax;
405          Ldmin = Ldmax/Lddyn;
406          logLddyn = log(Lddyn);
407          Ldavg = sqrt(Ldmax*Ldmin);
408 <        i = (tmTop->brmin-MINBRT)/HISTEP;
408 >        i = (tmTop->hbrmin-MINBRT)/HISTEP;
409          brt0 = MINBRT + HISTEP/2 + i*HISTEP;
410 <        histlen = (tmTop->brmax-MINBRT)/HISTEP + 1 - i;
410 >        histlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - i;
411                                          /* histogram total and mean */
412          histot = 0; sum = 0;
413          j = brt0 + histlen*HISTEP;
# Line 357 | Line 419 | double Ldmax;
419          if (threshold < 4)
420                  returnErr(TM_E_TMFAIL);
421          Lwavg = tmLuminance( (double)sum / histot );
422 <        if (!(tmTop->flags & TM_F_LINEAR)) {    /* clamp histogram */
423 <                histo = (int *)malloc(histlen*sizeof(int));
424 <                cumf = (float *)malloc((histlen+1)*sizeof(float));
425 <                if (histo == NULL | cumf == NULL)
426 <                        returnErr(TM_E_NOMEM);
427 <                for (i = histlen; i--; )        /* make malleable copy */
428 <                        histo[i] = tmTop->histo[i];
429 <                do {                            /* iterate to solution */
430 <                        sum = 0;                /* cumulative probability */
431 <                        for (i = 0; i < histlen; i++) {
432 <                                cumf[i] = (double)sum/histot;
433 <                                sum += histo[i];
422 >                                        /* allocate space for mapping */
423 >        if (!tmNewMap())
424 >                returnErr(TM_E_NOMEM);
425 >                                        /* use linear tone mapping? */
426 >        if (tmTop->flags & TM_F_LINEAR)
427 >                goto linearmap;
428 >                                        /* clamp histogram */
429 >        histo = (int *)malloc(histlen*sizeof(int));
430 >        cumf = (float *)malloc((histlen+2)*sizeof(float));
431 >        if (histo == NULL | cumf == NULL)
432 >                returnErr(TM_E_NOMEM);
433 >        cumf[histlen+1] = 1.;           /* guard for assignment code */
434 >        for (i = histlen; i--; )        /* make malleable copy */
435 >                histo[i] = tmTop->histo[i];
436 >        do {                            /* iterate to solution */
437 >                sum = 0;                /* cumulative probability */
438 >                for (i = 0; i < histlen; i++) {
439 >                        cumf[i] = (double)sum/histot;
440 >                        sum += histo[i];
441 >                }
442 >                cumf[histlen] = 1.;
443 >                Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) /
444 >                        ((double)histlen*TM_BRTSCALE) / logLddyn;
445 >                ceiling = Tr + 1.;
446 >                trimmings = 0;          /* clip to envelope */
447 >                for (i = histlen; i--; ) {
448 >                        if (tmTop->flags & TM_F_HCONTR) {
449 >                                Lw = tmLuminance(brt0 + i*HISTEP);
450 >                                Ld = Ldmin * exp( logLddyn *
451 >                                        .5*(cumf[i]+cumf[i+1]) );
452 >                                ceiling = Tr * (htcontrs(Ld) * Lw) /
453 >                                        (htcontrs(Lw) * Ld) + 1.;
454                          }
455 <                        cumf[i] = 1.;
456 <                        Tr = histot * (double)(tmTop->brmax - tmTop->brmin) /
457 <                                ((double)histlen*TM_BRTSCALE) / logLddyn;
376 <                        ceiling = Tr + 1.;
377 <                        trimmings = 0;                  /* clip to envelope */
378 <                        for (i = histlen; i--; ) {
379 <                                if (tmTop->flags & TM_F_HCONTR) {
380 <                                        Lw = tmLuminance(brt0 + i*HISTEP);
381 <                                        Ld = Ldmin * exp( logLddyn *
382 <                                                .5*(cumf[i]+cumf[i+1]) );
383 <                                        ceiling = Tr * (htcontrs(Ld) * Lw) /
384 <                                                (htcontrs(Lw) * Ld) + 1.;
385 <                                }
386 <                                if (histo[i] > ceiling) {
387 <                                        trimmings += histo[i] - ceiling;
388 <                                        histo[i] = ceiling;
389 <                                }
455 >                        if (histo[i] > ceiling) {
456 >                                trimmings += histo[i] - ceiling;
457 >                                histo[i] = ceiling;
458                          }
391                } while ((histot -= trimmings) > threshold &&
392                                                trimmings > threshold);
393        }
394                                                /* allocate luminance map */
395        if (tmTop->lumap == NULL) {
396                tmTop->lumap = (unsigned short *)malloc(
397                        (tmTop->brmax-tmTop->brmin+1)*sizeof(unsigned short) );
398                if (tmTop->lumap == NULL)
399                        returnErr(TM_E_NOMEM);
400        }
401        if (tmTop->flags & TM_F_LINEAR || histot <= threshold) {
402                                                /* linear tone mapping */
403                if (tmTop->flags & TM_F_HCONTR)
404                        d = htcontrs(Ldavg) / htcontrs(Lwavg);
405                else
406                        d = Ldavg / Lwavg;
407                d = log(d/Ldmax);
408                for (i = tmTop->brmax-tmTop->brmin+1; i--; )
409                        tmTop->lumap[i] = 256. * exp(
410                                ( d + (tmTop->brmin+i)/(double)TM_BRTSCALE )
411                                / gamval );
412        } else {
413                                                /* histogram adjustment */
414                for (i = tmTop->brmax-tmTop->brmin+1; i--; ) {
415                        j = d = (double)i/(tmTop->brmax-tmTop->brmin)*histlen;
416                        d -= (double)j;
417                        Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
418                        d = (Ld - Ldmin)/(Ldmax - Ldmin);
419                        tmTop->lumap[i] = 256.*pow(d, 1./gamval);
459                  }
460 +                                        /* check if we're out of data */
461 +                if ((histot -= trimmings) <= threshold) {
462 +                        free((MEM_PTR)histo);
463 +                        free((MEM_PTR)cumf);
464 +                        goto linearmap;
465 +                }
466 +        } while (trimmings > threshold);
467 +                                        /* assign tone-mapping */
468 +        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) {
469 +                j = d = (double)i/(tmTop->mbrmax-tmTop->mbrmin)*histlen;
470 +                d -= (double)j;
471 +                Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
472 +                d = (Ld - Ldmin)/(Ldmax - Ldmin);
473 +                tmTop->lumap[i] = 256.*pow(d, 1./gamval);
474          }
475 <        if (!(tmTop->flags & TM_F_LINEAR)) {
476 <                free((char *)histo);
424 <                free((char *)cumf);
425 <        }
475 >        free((MEM_PTR)histo);           /* clean up and return */
476 >        free((MEM_PTR)cumf);
477          returnOK;
478 + linearmap:                              /* linear tone-mapping */
479 +        if (tmTop->flags & TM_F_HCONTR)
480 +                d = htcontrs(Ldavg) / htcontrs(Lwavg);
481 +        else
482 +                d = Ldavg / Lwavg;
483 +        return(tmFixedMapping(tmTop->inpsf*d/Ldmax, gamval));
484   }
485  
486  
# Line 435 | Line 492 | register BYTE  *cs;
492   int     len;
493   {
494          static char     funcName[] = "tmMapPixels";
495 <        int     rdiv, gdiv, bdiv;
439 <        register int    li, pv;
495 >        register int32  li, pv;
496  
497          if (tmTop == NULL || tmTop->lumap == NULL)
498                  returnErr(TM_E_TMINVAL);
499 <        if (ps == NULL | ls == NULL | len <= 0)
499 >        if (ps == NULL | ls == NULL | len < 0)
500                  returnErr(TM_E_ILLEGAL);
445        rdiv = tmTop->gamb[((int4)TM_GAMTSZ*tmTop->clfb[RED])>>8];
446        gdiv = tmTop->gamb[((int4)TM_GAMTSZ*tmTop->clfb[GRN])>>8];
447        bdiv = tmTop->gamb[((int4)TM_GAMTSZ*tmTop->clfb[BLU])>>8];
501          while (len--) {
502 <                if ((li = *ls++) < tmTop->brmin)
503 <                        li = tmTop->brmin;
504 <                else if (li > tmTop->brmax)
505 <                        li = tmTop->brmax;
506 <                li = tmTop->lumap[li - tmTop->brmin];
502 >                if ((li = *ls++) < tmTop->mbrmin) {
503 >                        li = 0;
504 >                } else {
505 >                        if (li > tmTop->mbrmax)
506 >                                li = tmTop->mbrmax;
507 >                        li = tmTop->lumap[li - tmTop->mbrmin];
508 >                }
509                  if (cs == TM_NOCHROM)
510                          *ps++ = li>255 ? 255 : li;
511                  else {
512 <                        pv = *cs++ * li / rdiv;
512 >                        pv = *cs++ * li / tmTop->cdiv[RED];
513                          *ps++ = pv>255 ? 255 : pv;
514 <                        pv = *cs++ * li / gdiv;
514 >                        pv = *cs++ * li / tmTop->cdiv[GRN];
515                          *ps++ = pv>255 ? 255 : pv;
516 <                        pv = *cs++ * li / bdiv;
516 >                        pv = *cs++ * li / tmTop->cdiv[BLU];
517                          *ps++ = pv>255 ? 255 : pv;
518                  }
519          }
# Line 500 | Line 555 | register struct tmStruct       *tms;
555   }
556  
557  
558 + struct tmStruct *
559 + tmDup()                         /* duplicate top tone mapping */
560 + {
561 +        int     len;
562 +        register int    i;
563 +        register struct tmStruct        *tmnew;
564 +
565 +        if (tmTop == NULL)              /* anything to duplicate? */
566 +                return(NULL);
567 +        tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct));
568 +        if (tmnew == NULL)
569 +                return(NULL);
570 +        *tmnew = *tmTop;                /* copy everything */
571 +        if (tmnew->histo != NULL) {     /* duplicate histogram */
572 +                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
573 +                                (tmnew->hbrmin-MINBRT)/HISTEP;
574 +                tmnew->histo = (int *)malloc(len*sizeof(int));
575 +                if (tmnew->histo != NULL)
576 +                        for (i = len; i--; )
577 +                                tmnew->histo[i] = tmTop->histo[i];
578 +        }
579 +        if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */
580 +                len = tmnew->mbrmax-tmnew->mbrmin+1;
581 +                tmnew->lumap = (unsigned short *)malloc(
582 +                                                len*sizeof(unsigned short) );
583 +                if (tmnew->lumap != NULL)
584 +                        for (i = len; i--; )
585 +                                tmnew->lumap[i] = tmTop->lumap[i];
586 +        }
587 +                                        /* clear package data */
588 +        for (i = tmNumPkgs; i--; )
589 +                tmnew->pd[i] = NULL;
590 +        tmnew->tmprev = tmTop;          /* make copy current */
591 +        return(tmTop = tmnew);
592 + }
593 +
594 +
595   int
596   tmPush(tms)                     /* push tone mapping on top of stack */
597   register struct tmStruct        *tms;
598   {
599          static char     funcName[] = "tmPush";
600                                          /* check validity */
601 <        if (tms == NULL || !(tms->flags & TM_F_INITED))
601 >        if (tms == NULL)
602                  returnErr(TM_E_ILLEGAL);
603          if (tms == tmTop)               /* check necessity */
604                  returnOK;
# Line 523 | Line 615 | void
615   tmDone(tms)                     /* done with tone mapping -- destroy it */
616   register struct tmStruct        *tms;
617   {
618 +        register int    i;
619                                          /* NULL arg. is equiv. to tmTop */
620          if (tms == NULL && (tms = tmTop) == NULL)
621                  return;
# Line 530 | Line 623 | register struct tmStruct       *tms;
623          (void)tmPull(tms);
624                                          /* free tables */
625          if (tms->histo != NULL)
626 <                free((char *)tms->histo);
626 >                free((MEM_PTR)tms->histo);
627          if (tms->lumap != NULL)
628 <                free((char *)tms->lumap);
629 <        tms->flags = 0;
630 <        free((char *)tms);              /* free basic structure */
628 >                free((MEM_PTR)tms->lumap);
629 >                                        /* free private data */
630 >        for (i = tmNumPkgs; i--; )
631 >                if (tms->pd[i] != NULL)
632 >                        (*tmPkg[i]->Free)(tms->pd[i]);
633 >        free((MEM_PTR)tms);             /* free basic structure */
634 > }
635 >
636 > /******************** Shared but Private library routines *********************/
637 >
638 > BYTE    tmMesofact[BMESUPPER-BMESLOWER];
639 >
640 > void
641 > tmMkMesofact()                          /* build mesopic lookup factor table */
642 > {
643 >        register int    i;
644 >
645 >        if (tmMesofact[BMESUPPER-BMESLOWER-1])
646 >                return;
647 >
648 >        for (i = BMESLOWER; i < BMESUPPER; i++)
649 >                tmMesofact[i-BMESLOWER] = 256. *
650 >                                (tmLuminance(i) - LMESLOWER) /
651 >                                (LMESUPPER - LMESLOWER);
652 > }
653 >
654 >
655 > int
656 > tmErrorReturn(func, err)                /* error return (with message) */
657 > char    *func;
658 > int     err;
659 > {
660 >        tmLastFunction = func;
661 >        tmLastError = err;
662 >        if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR)
663 >                return(err);
664 >        fputs(func, stderr);
665 >        fputs(": ", stderr);
666 >        fputs(tmErrorMessage[err], stderr);
667 >        fputs("!\n", stderr);
668 >        return(err);
669   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines