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.15 by greg, Thu Jul 22 17:48:52 2004 UTC vs.
Revision 3.21 by greg, Wed Jan 18 00:24:38 2006 UTC

# Line 18 | Line 18 | static const char      RCSid[] = "$Id$";
18  
19   #define exp10(x)        exp(M_LN10*(x))
20  
21 struct tmStruct *tmTop = NULL;          /* current tone mapping stack */
22
21                                          /* our list of conversion packages */
22   struct tmPackage        *tmPkg[TM_MAXPKG];
23   int     tmNumPkgs = 0;                  /* number of registered packages */
24  
27 int     tmLastError;                    /* last error incurred by library */
28 char    *tmLastFunction;                /* error-generating function name */
25  
26 <
27 < struct tmStruct *
28 < tmInit(flags, monpri, gamval)           /* initialize new tone mapping */
29 < int     flags;
30 < RGBPRIMP        monpri;
31 < double  gamval;
26 > TMstruct *
27 > tmInit(                                 /* initialize new tone mapping */
28 > int     flags,
29 > RGBPRIMP        monpri,
30 > double  gamval
31 > )
32   {
33          COLORMAT        cmat;
34 <        register struct tmStruct        *tmnew;
35 <        register int    i;
34 >        TMstruct        *tmnew;
35 >        int     i;
36                                                  /* allocate structure */
37 <        tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct));
37 >        tmnew = (TMstruct *)malloc(sizeof(TMstruct));
38          if (tmnew == NULL)
39                  return(NULL);
40  
# Line 72 | Line 68 | double gamval;
68                          tmnew->inpsf = WHTEFFICACY;
69          tmnew->cmat[0][1] = tmnew->cmat[0][2] = tmnew->cmat[1][0] =
70          tmnew->cmat[1][2] = tmnew->cmat[2][0] = tmnew->cmat[2][1] = 0.;
71 +        tmnew->inpdat = NULL;
72          tmnew->hbrmin = 10; tmnew->hbrmax = -10;
73          tmnew->histo = NULL;
74          tmnew->mbrmin = 10; tmnew->mbrmax = -10;
# Line 79 | Line 76 | double gamval;
76                                                  /* zero private data */
77          for (i = TM_MAXPKG; i--; )
78                  tmnew->pd[i] = NULL;
79 <                                                /* make tmnew current */
80 <        tmnew->tmprev = tmTop;
81 <        return(tmTop = tmnew);
79 >        tmnew->lastError = TM_E_OK;
80 >        tmnew->lastFunc = "NoErr";
81 >                                                /* return new TMstruct */
82 >        return(tmnew);
83   }
84  
85  
86   int
87 < tmSetSpace(pri, sf)             /* set input color space for conversions */
88 < RGBPRIMP        pri;
89 < double  sf;
87 > tmSetSpace(                     /* set input color space for conversions */
88 > TMstruct        *tms,
89 > RGBPRIMP        pri,
90 > double  sf,
91 > MEM_PTR dat
92 > )
93   {
94 <        static char     funcName[] = "tmSetSpace";
95 <        register int    i, j;
94 >        static const char funcName[] = "tmSetSpace";
95 >        int     i, j;
96                                                  /* error check */
97 <        if (tmTop == NULL)
97 >        if (tms == NULL)
98                  returnErr(TM_E_TMINVAL);
99          if (sf <= 1e-12)
100                  returnErr(TM_E_ILLEGAL);
101                                                  /* check if no change */
102 <        if (pri == tmTop->inppri && FEQ(sf, tmTop->inpsf))
102 >        if (pri == tms->inppri && FEQ(sf, tms->inpsf) && dat == tms->inpdat)
103                  returnOK;
104 <        tmTop->inppri = pri;                    /* let's set it */
105 <        tmTop->inpsf = sf;
104 >        tms->inppri = pri;                      /* let's set it */
105 >        tms->inpsf = sf;
106 >        tms->inpdat = dat;
107  
108 <        if (tmTop->flags & TM_F_BW) {           /* color doesn't matter */
109 <                tmTop->monpri = tmTop->inppri;          /* eliminate xform */
110 <                if (tmTop->inppri == TM_XYZPRIM) {
111 <                        tmTop->clf[CIEX] = tmTop->clf[CIEZ] = 0.;
112 <                        tmTop->clf[CIEY] = 1.;
108 >        if (tms->flags & TM_F_BW) {             /* color doesn't matter */
109 >                tms->monpri = tms->inppri;              /* eliminate xform */
110 >                if (tms->inppri == TM_XYZPRIM) {
111 >                        tms->clf[CIEX] = tms->clf[CIEZ] = 0.;
112 >                        tms->clf[CIEY] = 1.;
113                  } else {
114 <                        comprgb2xyzWBmat(tmTop->cmat, tmTop->monpri);
115 <                        tmTop->clf[RED] = tmTop->cmat[1][0];
116 <                        tmTop->clf[GRN] = tmTop->cmat[1][1];
117 <                        tmTop->clf[BLU] = tmTop->cmat[1][2];
114 >                        comprgb2xyzWBmat(tms->cmat, tms->monpri);
115 >                        tms->clf[RED] = tms->cmat[1][0];
116 >                        tms->clf[GRN] = tms->cmat[1][1];
117 >                        tms->clf[BLU] = tms->cmat[1][2];
118                  }
119 <                tmTop->cmat[0][0] = tmTop->cmat[1][1] = tmTop->cmat[2][2] =
120 <                                tmTop->inpsf;
121 <                tmTop->cmat[0][1] = tmTop->cmat[0][2] = tmTop->cmat[1][0] =
122 <                tmTop->cmat[1][2] = tmTop->cmat[2][0] = tmTop->cmat[2][1] = 0.;
119 >                tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] =
120 >                                tms->inpsf;
121 >                tms->cmat[0][1] = tms->cmat[0][2] = tms->cmat[1][0] =
122 >                tms->cmat[1][2] = tms->cmat[2][0] = tms->cmat[2][1] = 0.;
123  
124 <        } else if (tmTop->inppri == TM_XYZPRIM) /* input is XYZ */
125 <                compxyz2rgbWBmat(tmTop->cmat, tmTop->monpri);
124 >        } else if (tms->inppri == TM_XYZPRIM)   /* input is XYZ */
125 >                compxyz2rgbWBmat(tms->cmat, tms->monpri);
126  
127          else {                                  /* input is RGB */
128 <                if (tmTop->inppri != tmTop->monpri &&
129 <                                PRIMEQ(tmTop->inppri, tmTop->monpri))
130 <                        tmTop->inppri = tmTop->monpri;  /* no xform */
131 <                comprgb2rgbWBmat(tmTop->cmat, tmTop->inppri, tmTop->monpri);
128 >                if (tms->inppri != tms->monpri &&
129 >                                PRIMEQ(tms->inppri, tms->monpri))
130 >                        tms->inppri = tms->monpri;      /* no xform */
131 >                comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri);
132          }
133          for (i = 0; i < 3; i++)
134                  for (j = 0; j < 3; j++)
135 <                        tmTop->cmat[i][j] *= tmTop->inpsf;
135 >                        tms->cmat[i][j] *= tms->inpsf;
136                                                  /* set color divisors */
137          for (i = 0; i < 3; i++)
138 <                if (tmTop->clf[i] > .001)
139 <                        tmTop->cdiv[i] =
140 <                                256.*pow(tmTop->clf[i], 1./tmTop->mongam);
138 >                if (tms->clf[i] > .001)
139 >                        tms->cdiv[i] =
140 >                                256.*pow(tms->clf[i], 1./tms->mongam);
141                  else
142 <                        tmTop->cdiv[i] = 1;
142 >                        tms->cdiv[i] = 1;
143                                                  /* notify packages */
144          for (i = tmNumPkgs; i--; )
145 <                if (tmTop->pd[i] != NULL && tmPkg[i]->NewSpace != NULL)
146 <                        (*tmPkg[i]->NewSpace)(tmTop);
145 >                if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL)
146 >                        (*tmPkg[i]->NewSpace)(tms);
147          returnOK;
148   }
149  
150  
151   void
152 < tmClearHisto()                  /* clear current histogram */
152 > tmClearHisto(                   /* clear current histogram */
153 > TMstruct        *tms
154 > )
155   {
156 <        if (tmTop == NULL || tmTop->histo == NULL)
156 >        if (tms == NULL || tms->histo == NULL)
157                  return;
158 <        free((MEM_PTR)tmTop->histo);
159 <        tmTop->histo = NULL;
158 >        free((MEM_PTR)tms->histo);
159 >        tms->histo = NULL;
160   }
161  
162  
163   int
164 < tmCvColors(ls, cs, scan, len)           /* convert float colors */
165 < TMbright        *ls;
166 < BYTE    *cs;
167 < COLOR   *scan;
168 < int     len;
164 > tmCvColors(                             /* convert float colors */
165 > TMstruct        *tms,
166 > TMbright        *ls,
167 > BYTE    *cs,
168 > COLOR   *scan,
169 > int     len
170 > )
171   {
172 <        static char     funcName[] = "tmCvColors";
172 >        static const char funcName[] = "tmCvColors";
173          static COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
174 +        static BYTE     gamtab[1024];
175 +        static double   curgam = .0;
176          COLOR   cmon;
177          double  lum, slum;
178 <        register double d;
179 <        register int    i;
178 >        double  d;
179 >        int     i;
180  
181 <        if (tmTop == NULL)
181 >        if (tms == NULL)
182                  returnErr(TM_E_TMINVAL);
183          if ((ls == NULL) | (scan == NULL) | (len < 0))
184                  returnErr(TM_E_ILLEGAL);
185 +        if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
186 +                curgam = tms->mongam;                   /* (re)build table */
187 +                for (i = 1024; i--; )
188 +                        gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam));
189 +        }
190          for (i = len; i--; ) {
191 <                if (tmNeedMatrix(tmTop)) {              /* get monitor RGB */
192 <                        colortrans(cmon, tmTop->cmat, scan[i]);
191 >                if (tmNeedMatrix(tms)) {                /* get monitor RGB */
192 >                        colortrans(cmon, tms->cmat, scan[i]);
193                  } else {
194 <                        cmon[RED] = tmTop->inpsf*scan[i][RED];
195 <                        cmon[GRN] = tmTop->inpsf*scan[i][GRN];
196 <                        cmon[BLU] = tmTop->inpsf*scan[i][BLU];
194 >                        cmon[RED] = tms->inpsf*scan[i][RED];
195 >                        cmon[GRN] = tms->inpsf*scan[i][GRN];
196 >                        cmon[BLU] = tms->inpsf*scan[i][BLU];
197                  }
198                                                          /* world luminance */
199 <                lum =   tmTop->clf[RED]*cmon[RED] +
200 <                        tmTop->clf[GRN]*cmon[GRN] +
201 <                        tmTop->clf[BLU]*cmon[BLU] ;
199 >                lum =   tms->clf[RED]*cmon[RED] +
200 >                        tms->clf[GRN]*cmon[GRN] +
201 >                        tms->clf[BLU]*cmon[BLU] ;
202                                                          /* check range */
203                  if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
204 <                        lum =   tmTop->clf[RED]*cmon[RED] +
205 <                                tmTop->clf[GRN]*cmon[GRN] +
206 <                                tmTop->clf[BLU]*cmon[BLU] ;
204 >                        lum =   tms->clf[RED]*cmon[RED] +
205 >                                tms->clf[GRN]*cmon[GRN] +
206 >                                tms->clf[BLU]*cmon[BLU] ;
207                  if (lum < MINLUM) {
208                          ls[i] = MINBRT-1;               /* bogus value */
209                          lum = MINLUM;
# Line 200 | Line 213 | int    len;
213                  }
214                  if (cs == TM_NOCHROM)                   /* no color? */
215                          continue;
216 <                if (tmTop->flags & TM_F_MESOPIC && lum < LMESUPPER) {
216 >                if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
217                          slum = scotlum(cmon);           /* mesopic adj. */
218                          if (lum < LMESLOWER)
219                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
220                          else {
221                                  d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
222 <                                if (tmTop->flags & TM_F_BW)
222 >                                if (tms->flags & TM_F_BW)
223                                          cmon[RED] = cmon[GRN] =
224                                                          cmon[BLU] = d*lum;
225                                  else
# Line 216 | Line 229 | int    len;
229                                  cmon[GRN] += d;
230                                  cmon[BLU] += d;
231                          }
232 <                } else if (tmTop->flags & TM_F_BW) {
232 >                } else if (tms->flags & TM_F_BW) {
233                          cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
234                  }
235 <                d = tmTop->clf[RED]*cmon[RED]/lum;
236 <                cs[3*i  ] = d>=.999 ? 255 :
237 <                                (int)(256.*pow(d, 1./tmTop->mongam));
238 <                d = tmTop->clf[GRN]*cmon[GRN]/lum;
239 <                cs[3*i+1] = d>=.999 ? 255 :
240 <                                (int)(256.*pow(d, 1./tmTop->mongam));
228 <                d = tmTop->clf[BLU]*cmon[BLU]/lum;
229 <                cs[3*i+2] = d>=.999 ? 255 :
230 <                                (int)(256.*pow(d, 1./tmTop->mongam));
235 >                d = tms->clf[RED]*cmon[RED]/lum;
236 >                cs[3*i  ] = d>=.999 ? 255 : gamtab[(int)(1024.*d)];
237 >                d = tms->clf[GRN]*cmon[GRN]/lum;
238 >                cs[3*i+1] = d>=.999 ? 255 : gamtab[(int)(1024.*d)];
239 >                d = tms->clf[BLU]*cmon[BLU]/lum;
240 >                cs[3*i+2] = d>=.999 ? 255 : gamtab[(int)(1024.*d)];
241          }
242          returnOK;
243   }
244  
245  
246 + TMbright
247 + tmCvLuminance(                          /* convert a single luminance */
248 + double  lum
249 + )
250 + {
251 +        double  d;
252 +
253 +        if (lum <= TM_NOLUM)
254 +                return(TM_NOBRT);
255 +        d = TM_BRTSCALE*log(lum);
256 +        if (d > 0.)
257 +                return((TMbright)(d+.5));
258 +        return((TMbright)(d-.5));
259 + }
260 +
261 +
262   int
263 < tmCvGrays(ls, scan, len)                /* convert float gray values */
264 < TMbright        *ls;
265 < float   *scan;
266 < int     len;
263 > tmCvGrays(                              /* convert float gray values */
264 > TMstruct        *tms,
265 > TMbright        *ls,
266 > float   *scan,
267 > int     len
268 > )
269   {
270 <        static char     funcName[] = "tmCvGrays";
271 <        register double d;
272 <        register int    i;
270 >        static const char funcName[] = "tmCvGrays";
271 >        double  d;
272 >        int     i;
273  
274 <        if (tmTop == NULL)
274 >        if (tms == NULL)
275                  returnErr(TM_E_TMINVAL);
276          if ((ls == NULL) | (scan == NULL) | (len < 0))
277                  returnErr(TM_E_ILLEGAL);
# Line 259 | Line 287 | int    len;
287  
288  
289   int
290 < tmAddHisto(ls, len, wt)                 /* add values to histogram */
291 < register TMbright       *ls;
292 < int     len;
293 < int     wt;
290 > tmAddHisto(                             /* add values to histogram */
291 > TMstruct        *tms,
292 > TMbright        *ls,
293 > int     len,
294 > int     wt
295 > )
296   {
297 <        static char     funcName[] = "tmAddHisto";
297 >        static const char funcName[] = "tmAddHisto";
298          int     oldorig=0, oldlen, horig, hlen;
299 <        register int    i, j;
299 >        int     i, j;
300  
301 <        if (tmTop == NULL)
301 >        if (tms == NULL)
302                  returnErr(TM_E_TMINVAL);
303          if (len < 0)
304                  returnErr(TM_E_ILLEGAL);
305          if (len == 0)
306                  returnOK;
307                                                  /* first, grow limits */
308 <        if (tmTop->histo == NULL) {
308 >        if (tms->histo == NULL) {
309                  for (i = len; i-- && ls[i] < MINBRT; )
310                          ;
311                  if (i < 0)
312                          returnOK;
313 <                tmTop->hbrmin = tmTop->hbrmax = ls[i];
313 >                tms->hbrmin = tms->hbrmax = ls[i];
314                  oldlen = 0;
315          } else {
316 <                oldorig = (tmTop->hbrmin-MINBRT)/HISTEP;
317 <                oldlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
316 >                oldorig = (tms->hbrmin-MINBRT)/HISTEP;
317 >                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
318          }
319          for (i = len; i--; ) {
320                  if ((j = ls[i]) < MINBRT)
321                          continue;
322 <                if (j < tmTop->hbrmin)
323 <                        tmTop->hbrmin = j;
324 <                else if (j > tmTop->hbrmax)
325 <                        tmTop->hbrmax = j;
322 >                if (j < tms->hbrmin)
323 >                        tms->hbrmin = j;
324 >                else if (j > tms->hbrmax)
325 >                        tms->hbrmax = j;
326          }
327 <        horig = (tmTop->hbrmin-MINBRT)/HISTEP;
328 <        hlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - horig;
327 >        horig = (tms->hbrmin-MINBRT)/HISTEP;
328 >        hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
329          if (hlen > oldlen) {                    /* (re)allocate histogram */
330 <                register int    *newhist = (int *)calloc(hlen, sizeof(int));
330 >                int     *newhist = (int *)calloc(hlen, sizeof(int));
331                  if (newhist == NULL)
332                          returnErr(TM_E_NOMEM);
333                  if (oldlen) {                   /* copy and free old */
334                          for (i = oldlen, j = i+oldorig-horig; i; )
335 <                                newhist[--j] = tmTop->histo[--i];
336 <                        free((MEM_PTR)tmTop->histo);
335 >                                newhist[--j] = tms->histo[--i];
336 >                        free((MEM_PTR)tms->histo);
337                  }
338 <                tmTop->histo = newhist;
338 >                tms->histo = newhist;
339          }
340          if (wt == 0)
341                  returnOK;
342          for (i = len; i--; )                    /* add in new counts */
343                  if (ls[i] >= MINBRT)
344 <                        tmTop->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
344 >                        tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
345          returnOK;
346   }
347  
348  
349   static double
350 < htcontrs(La)            /* human threshold contrast sensitivity, dL(La) */
351 < double  La;
350 > htcontrs(               /* human threshold contrast sensitivity, dL(La) */
351 > double  La
352 > )
353   {
354          double  l10La, l10dL;
355                                  /* formula taken from Ferwerda et al. [SG96] */
# Line 339 | Line 370 | double La;
370  
371  
372   static int
373 < tmNewMap()
373 > tmNewMap(                       /* allocate new tone-mapping array */
374 > TMstruct        *tms
375 > )
376   {
377 <        if (tmTop->lumap != NULL && (tmTop->mbrmax - tmTop->mbrmin) !=
378 <                                        (tmTop->hbrmax - tmTop->hbrmin)) {
379 <                free((MEM_PTR)tmTop->lumap);
380 <                tmTop->lumap = NULL;
377 >        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
378 >                                        (tms->hbrmax - tms->hbrmin)) {
379 >                free((MEM_PTR)tms->lumap);
380 >                tms->lumap = NULL;
381          }
382 <        tmTop->mbrmin = tmTop->hbrmin;
383 <        tmTop->mbrmax = tmTop->hbrmax;
384 <        if (tmTop->mbrmin > tmTop->mbrmax)
382 >        tms->mbrmin = tms->hbrmin;
383 >        tms->mbrmax = tms->hbrmax;
384 >        if (tms->mbrmin > tms->mbrmax)
385                  return 0;
386 <        if (tmTop->lumap == NULL)
387 <                tmTop->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
388 <                                        (tmTop->mbrmax-tmTop->mbrmin+1));
389 <        return(tmTop->lumap != NULL);
386 >        if (tms->lumap == NULL)
387 >                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
388 >                                        (tms->mbrmax-tms->mbrmin+1));
389 >        return(tms->lumap != NULL);
390   }
391  
392  
393   int
394 < tmFixedMapping(expmult, gamval)
395 < double  expmult;
396 < double  gamval;
394 > tmFixedMapping(                 /* compute fixed, linear tone-mapping */
395 > TMstruct        *tms,
396 > double  expmult,
397 > double  gamval
398 > )
399   {
400 <        static char     funcName[] = "tmFixedMapping";
400 >        static const char funcName[] = "tmFixedMapping";
401          double          d;
402 <        register int    i;
402 >        int     i;
403          
404 <        if (!tmNewMap())
404 >        if (!tmNewMap(tms))
405                  returnErr(TM_E_NOMEM);
406          if (expmult <= .0)
407                  expmult = 1.;
408          if (gamval < MINGAM)
409 <                gamval = tmTop->mongam;
410 <        d = log(expmult/tmTop->inpsf);
411 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; )
412 <                tmTop->lumap[i] = 256. * exp(
413 <                        ( d + (tmTop->mbrmin+i)*(1./TM_BRTSCALE) )
409 >                gamval = tms->mongam;
410 >        d = log(expmult/tms->inpsf);
411 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
412 >                tms->lumap[i] = 256. * exp(
413 >                        ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
414                          / gamval );
415          returnOK;
416   }
417  
418  
419   int
420 < tmComputeMapping(gamval, Lddyn, Ldmax)
421 < double  gamval;
422 < double  Lddyn;
423 < double  Ldmax;
420 > tmComputeMapping(                       /* compute histogram tone-mapping */
421 > TMstruct        *tms,
422 > double  gamval,
423 > double  Lddyn,
424 > double  Ldmax
425 > )
426   {
427 <        static char     funcName[] = "tmComputeMapping";
427 >        static const char funcName[] = "tmComputeMapping";
428          int     *histo;
429          float   *cumf;
430          int     brt0, histlen, threshold, ceiling, trimmings;
431          double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
432          int32   histot;
433          double  sum;
434 <        register double d;
435 <        register int    i, j;
434 >        double  d;
435 >        int     i, j;
436  
437 <        if (tmTop == NULL || tmTop->histo == NULL)
437 >        if (tms == NULL || tms->histo == NULL)
438                  returnErr(TM_E_TMINVAL);
439                                          /* check arguments */
440          if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
441          if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
442 <        if (gamval < MINGAM) gamval = tmTop->mongam;
442 >        if (gamval < MINGAM) gamval = tms->mongam;
443                                          /* compute handy values */
444          Ldmin = Ldmax/Lddyn;
445          logLddyn = log(Lddyn);
446          Ldavg = sqrt(Ldmax*Ldmin);
447 <        i = (tmTop->hbrmin-MINBRT)/HISTEP;
447 >        i = (tms->hbrmin-MINBRT)/HISTEP;
448          brt0 = MINBRT + HISTEP/2 + i*HISTEP;
449 <        histlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - i;
449 >        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
450                                          /* histogram total and mean */
451          histot = 0; sum = 0;
452          j = brt0 + histlen*HISTEP;
453          for (i = histlen; i--; ) {
454 <                histot += tmTop->histo[i];
455 <                sum += (j -= HISTEP) * tmTop->histo[i];
454 >                histot += tms->histo[i];
455 >                sum += (j -= HISTEP) * tms->histo[i];
456          }
457          threshold = histot*0.005 + .5;
458          if (threshold < 4)
459                  returnErr(TM_E_TMFAIL);
460          Lwavg = tmLuminance( (double)sum / histot );
461                                          /* allocate space for mapping */
462 <        if (!tmNewMap())
462 >        if (!tmNewMap(tms))
463                  returnErr(TM_E_NOMEM);
464                                          /* use linear tone mapping? */
465 <        if (tmTop->flags & TM_F_LINEAR)
465 >        if (tms->flags & TM_F_LINEAR)
466                  goto linearmap;
467                                          /* clamp histogram */
468          histo = (int *)malloc(histlen*sizeof(int));
# Line 434 | Line 471 | double Ldmax;
471                  returnErr(TM_E_NOMEM);
472          cumf[histlen+1] = 1.;           /* guard for assignment code */
473          for (i = histlen; i--; )        /* make malleable copy */
474 <                histo[i] = tmTop->histo[i];
474 >                histo[i] = tms->histo[i];
475          do {                            /* iterate to solution */
476                  sum = 0;                /* cumulative probability */
477                  for (i = 0; i < histlen; i++) {
# Line 442 | Line 479 | double Ldmax;
479                          sum += histo[i];
480                  }
481                  cumf[histlen] = 1.;
482 <                Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) /
482 >                Tr = histot * (double)(tms->hbrmax - tms->hbrmin) /
483                          ((double)histlen*TM_BRTSCALE) / logLddyn;
484                  ceiling = Tr + 1.;
485                  trimmings = 0;          /* clip to envelope */
486                  for (i = histlen; i--; ) {
487 <                        if (tmTop->flags & TM_F_HCONTR) {
487 >                        if (tms->flags & TM_F_HCONTR) {
488                                  Lw = tmLuminance(brt0 + i*HISTEP);
489                                  Ld = Ldmin * exp( logLddyn *
490                                          .5*(cumf[i]+cumf[i+1]) );
# Line 467 | Line 504 | double Ldmax;
504                  }
505          } while (trimmings > threshold);
506                                          /* assign tone-mapping */
507 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) {
508 <                j = d = (double)i/(tmTop->mbrmax-tmTop->mbrmin)*histlen;
507 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
508 >                j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
509                  d -= (double)j;
510                  Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
511                  d = (Ld - Ldmin)/(Ldmax - Ldmin);
512 <                tmTop->lumap[i] = 256.*pow(d, 1./gamval);
512 >                tms->lumap[i] = 256.*pow(d, 1./gamval);
513          }
514          free((MEM_PTR)histo);           /* clean up and return */
515          free((MEM_PTR)cumf);
516          returnOK;
517   linearmap:                              /* linear tone-mapping */
518 <        if (tmTop->flags & TM_F_HCONTR)
518 >        if (tms->flags & TM_F_HCONTR)
519                  d = htcontrs(Ldavg) / htcontrs(Lwavg);
520          else
521                  d = Ldavg / Lwavg;
522 <        return(tmFixedMapping(tmTop->inpsf*d/Ldmax, gamval));
522 >        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
523   }
524  
525  
526   int
527 < tmMapPixels(ps, ls, cs, len)
528 < register BYTE   *ps;
529 < TMbright        *ls;
530 < register BYTE   *cs;
531 < int     len;
527 > tmMapPixels(                    /* apply tone-mapping to pixel(s) */
528 > TMstruct        *tms,
529 > BYTE    *ps,
530 > TMbright        *ls,
531 > BYTE    *cs,
532 > int     len
533 > )
534   {
535 <        static char     funcName[] = "tmMapPixels";
536 <        register int32  li, pv;
535 >        static const char funcName[] = "tmMapPixels";
536 >        int32   li, pv;
537  
538 <        if (tmTop == NULL || tmTop->lumap == NULL)
538 >        if (tms == NULL || tms->lumap == NULL)
539                  returnErr(TM_E_TMINVAL);
540          if ((ps == NULL) | (ls == NULL) | (len < 0))
541                  returnErr(TM_E_ILLEGAL);
542          while (len--) {
543 <                if ((li = *ls++) < tmTop->mbrmin) {
543 >                if ((li = *ls++) < tms->mbrmin) {
544                          li = 0;
545                  } else {
546 <                        if (li > tmTop->mbrmax)
547 <                                li = tmTop->mbrmax;
548 <                        li = tmTop->lumap[li - tmTop->mbrmin];
546 >                        if (li > tms->mbrmax)
547 >                                li = tms->mbrmax;
548 >                        li = tms->lumap[li - tms->mbrmin];
549                  }
550                  if (cs == TM_NOCHROM)
551                          *ps++ = li>255 ? 255 : li;
552                  else {
553 <                        pv = *cs++ * li / tmTop->cdiv[RED];
553 >                        pv = *cs++ * li / tms->cdiv[RED];
554                          *ps++ = pv>255 ? 255 : pv;
555 <                        pv = *cs++ * li / tmTop->cdiv[GRN];
555 >                        pv = *cs++ * li / tms->cdiv[GRN];
556                          *ps++ = pv>255 ? 255 : pv;
557 <                        pv = *cs++ * li / tmTop->cdiv[BLU];
557 >                        pv = *cs++ * li / tms->cdiv[BLU];
558                          *ps++ = pv>255 ? 255 : pv;
559                  }
560          }
# Line 523 | Line 562 | int    len;
562   }
563  
564  
526 struct tmStruct *
527 tmPop()                         /* pop top tone mapping off stack */
528 {
529        register struct tmStruct        *tms;
565  
531        if ((tms = tmTop) != NULL)
532                tmTop = tms->tmprev;
533        return(tms);
534 }
566  
567 <
568 < int
569 < tmPull(tms)                     /* pull a tone mapping from stack */
570 < register struct tmStruct        *tms;
567 > TMstruct *
568 > tmDup(                          /* duplicate top tone mapping */
569 > TMstruct        *tms
570 > )
571   {
541        register struct tmStruct        *tms2;
542                                        /* special cases first */
543        if ((tms == NULL) | (tmTop == NULL))
544                return(0);
545        if (tms == tmTop) {
546                tmTop = tms->tmprev;
547                tms->tmprev = NULL;
548                return(1);
549        }
550        for (tms2 = tmTop; tms2->tmprev != NULL; tms2 = tms2->tmprev)
551                if (tms == tms2->tmprev) {      /* remove it */
552                        tms2->tmprev = tms->tmprev;
553                        tms->tmprev = NULL;
554                        return(1);
555                }
556        return(0);                      /* not found on stack */
557 }
558
559
560 struct tmStruct *
561 tmDup()                         /* duplicate top tone mapping */
562 {
572          int     len;
573 <        register int    i;
574 <        register struct tmStruct        *tmnew;
573 >        int     i;
574 >        TMstruct        *tmnew;
575  
576 <        if (tmTop == NULL)              /* anything to duplicate? */
576 >        if (tms == NULL)                /* anything to duplicate? */
577                  return(NULL);
578 <        tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct));
578 >        tmnew = (TMstruct *)malloc(sizeof(TMstruct));
579          if (tmnew == NULL)
580                  return(NULL);
581 <        *tmnew = *tmTop;                /* copy everything */
581 >        *tmnew = *tms;          /* copy everything */
582          if (tmnew->histo != NULL) {     /* duplicate histogram */
583                  len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
584                                  (tmnew->hbrmin-MINBRT)/HISTEP;
585                  tmnew->histo = (int *)malloc(len*sizeof(int));
586                  if (tmnew->histo != NULL)
587                          for (i = len; i--; )
588 <                                tmnew->histo[i] = tmTop->histo[i];
588 >                                tmnew->histo[i] = tms->histo[i];
589          }
590          if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */
591                  len = tmnew->mbrmax-tmnew->mbrmin+1;
# Line 584 | Line 593 | tmDup()                                /* duplicate top tone mapping */
593                                                  len*sizeof(unsigned short) );
594                  if (tmnew->lumap != NULL)
595                          for (i = len; i--; )
596 <                                tmnew->lumap[i] = tmTop->lumap[i];
596 >                                tmnew->lumap[i] = tms->lumap[i];
597          }
598                                          /* clear package data */
599          for (i = tmNumPkgs; i--; )
600                  tmnew->pd[i] = NULL;
601 <        tmnew->tmprev = tmTop;          /* make copy current */
602 <        return(tmTop = tmnew);
601 >                                        /* return copy */
602 >        return(tmnew);
603   }
604  
605  
597 int
598 tmPush(tms)                     /* push tone mapping on top of stack */
599 register struct tmStruct        *tms;
600 {
601        static char     funcName[] = "tmPush";
602                                        /* check validity */
603        if (tms == NULL)
604                returnErr(TM_E_ILLEGAL);
605        if (tms == tmTop)               /* check necessity */
606                returnOK;
607                                        /* pull if already in stack */
608        (void)tmPull(tms);
609                                        /* push it on top */
610        tms->tmprev = tmTop;
611        tmTop = tms;
612        returnOK;
613 }
614
615
606   void
607   tmDone(tms)                     /* done with tone mapping -- destroy it */
608 < register struct tmStruct        *tms;
608 > TMstruct        *tms;
609   {
610 <        register int    i;
611 <                                        /* NULL arg. is equiv. to tmTop */
612 <        if (tms == NULL && (tms = tmTop) == NULL)
610 >        int     i;
611 >                                        /* NULL arg. is equiv. to tms */
612 >        if (tms == NULL)
613                  return;
624                                        /* take out of stack if present */
625        (void)tmPull(tms);
614                                          /* free tables */
615          if (tms->histo != NULL)
616                  free((MEM_PTR)tms->histo);
# Line 642 | Line 630 | BYTE   tmMesofact[BMESUPPER-BMESLOWER];
630   void
631   tmMkMesofact()                          /* build mesopic lookup factor table */
632   {
633 <        register int    i;
633 >        int     i;
634  
635          if (tmMesofact[BMESUPPER-BMESLOWER-1])
636                  return;
# Line 655 | Line 643 | tmMkMesofact()                         /* build mesopic lookup factor table
643  
644  
645   int
646 < tmErrorReturn(func, err)                /* error return (with message) */
647 < char    *func;
648 < int     err;
646 > tmErrorReturn(                          /* error return (with message) */
647 > const char      *func,
648 > TMstruct        *tms,
649 > int     err
650 > )
651   {
652 <        tmLastFunction = func;
653 <        tmLastError = err;
654 <        if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR)
655 <                return(err);
652 >        if (tms != NULL) {
653 >                tms->lastFunc = func;
654 >                tms->lastError = err;
655 >                if (tms->flags & TM_F_NOSTDERR)
656 >                        return(err);
657 >        }
658          fputs(func, stderr);
659          fputs(": ", stderr);
660          fputs(tmErrorMessage[err], stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines