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.34 by greg, Mon Feb 9 20:48:08 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines