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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines