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.10 by greg, Tue Feb 25 02:47:22 2003 UTC vs.
Revision 3.35 by greg, Mon Mar 7 20:49:19 2011 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  
47          tmnew->flags = flags & ~TM_F_UNIMPL;
48 +        if (tmnew->flags & TM_F_BW)
49 +                tmnew->flags &= ~TM_F_MESOPIC;
50                                                  /* set monitor transform */
51          if (monpri == NULL || monpri == stdprims || tmnew->flags & TM_F_BW) {
52                  tmnew->monpri = stdprims;
# Line 50 | 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 70 | 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 77 | 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 +        return((TMbright)(d + .5 - (d < 0.)));
184 + }
185 +
186 +
187   int
188 < tmCvColors(ls, cs, scan, len)           /* convert float colors */
189 < TMbright        *ls;
190 < BYTE    *cs;
191 < COLOR   *scan;
192 < int     len;
188 > tmCvLums(                               /* convert luminances using lookup */
189 > TMbright        *ls,
190 > float           *scan,
191 > int             len
192 > )
193   {
194 <        static char     funcName[] = "tmCvColors";
195 <        static COLOR    csmall = {1e-6, 1e-6, 1e-6};
194 >        if (tmFloat2BrtLUT == NULL) {   /* initialize lookup table */
195 >                int32   i;
196 >                tmFloat2BrtLUT = (TMbright *)malloc(sizeof(TMbright)*0x10000);
197 >                if (tmFloat2BrtLUT == NULL)
198 >                        return(TM_E_NOMEM);
199 >                for (i = 0; i < 0x10000; i++) {
200 >                        int32   l = (i<<1 | 1) << 14;
201 > #ifndef isfinite
202 >                        if ((l & 0x7f800000) == 0x7f800000)
203 >                                tmFloat2BrtLUT[i] = TM_NOBRT;
204 >                        else
205 > #endif
206 >                        tmFloat2BrtLUT[i] = tmCvLuminance(*(float *)&l);
207 >                }
208 >        }
209 >        if (len <= 0)
210 >                return(TM_E_OK);
211 >        if ((ls == NULL) | (scan == NULL))
212 >                return(TM_E_ILLEGAL);
213 >        while (len--) {
214 >                if (*scan <= TM_NOLUM) {
215 >                        *ls++ = TM_NOBRT;
216 >                        ++scan;
217 >                        continue;
218 >                }
219 >                *ls++ = tmCvLumLUfp(scan++);
220 >        }
221 >        return(TM_E_OK);
222 > }
223 >
224 >
225 > int
226 > tmCvGrays(                              /* convert float gray values */
227 > TMstruct        *tms,
228 > TMbright        *ls,
229 > float           *scan,
230 > int             len
231 > )
232 > {
233 >        static const char funcName[] = "tmCvGrays";
234 >        int     i;
235 >
236 >        if (tms == NULL)
237 >                returnErr(TM_E_TMINVAL);
238 >        if ((ls == NULL) | (scan == NULL) | (len < 0))
239 >                returnErr(TM_E_ILLEGAL);
240 >        if (tmFloat2BrtLUT == NULL)                     /* initialize */
241 >                tmCvLums(NULL, NULL, 0);
242 >        for (i = len; i--; ) {
243 >                float   lum = tms->inpsf * scan[i];
244 >                if (lum <= TM_NOLUM)
245 >                        ls[i] = TM_NOBRT;
246 >                else
247 >                        ls[i] = tmCvLumLUfp(&lum);
248 >        }
249 >        returnOK;
250 > }
251 >
252 >
253 > int
254 > tmCvColors(                             /* convert float colors */
255 > TMstruct        *tms,
256 > TMbright        *ls,
257 > BYTE    *cs,
258 > COLOR   *scan,
259 > int     len
260 > )
261 > {
262 >        static const char funcName[] = "tmCvColors";
263 >        static BYTE     gamtab[1024];
264 >        static double   curgam = .0;
265          COLOR   cmon;
266 <        double  lum, slum;
267 <        register double d;
169 <        register int    i;
266 >        float   lum, slum, d;
267 >        int     i;
268  
269 <        if (tmTop == NULL)
269 >        if (tms == NULL)
270                  returnErr(TM_E_TMINVAL);
271 <        if (ls == NULL | scan == NULL | len < 0)
271 >        if ((ls == NULL) | (scan == NULL) | (len < 0))
272                  returnErr(TM_E_ILLEGAL);
273 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
274 +                tmCvLums(NULL, NULL, 0);
275 +        if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
276 +                curgam = tms->mongam;                   /* (re)build table */
277 +                for (i = 1024; i--; )
278 +                        gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam));
279 +        }
280          for (i = len; i--; ) {
281 <                if (tmNeedMatrix(tmTop))                /* get monitor RGB */
282 <                        colortrans(cmon, tmTop->cmat, scan[i]);
178 <                else {
179 <                        cmon[RED] = tmTop->inpsf*scan[i][RED];
180 <                        cmon[GRN] = tmTop->inpsf*scan[i][GRN];
181 <                        cmon[BLU] = tmTop->inpsf*scan[i][BLU];
182 <                }
183 <                                                        /* world luminance */
184 <                lum =   tmTop->clf[RED]*cmon[RED] +
185 <                        tmTop->clf[GRN]*cmon[GRN] +
186 <                        tmTop->clf[BLU]*cmon[BLU] ;
187 <                                                        /* check range */
188 <                if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
189 <                        lum =   tmTop->clf[RED]*cmon[RED] +
190 <                                tmTop->clf[GRN]*cmon[GRN] +
191 <                                tmTop->clf[BLU]*cmon[BLU] ;
192 <                if (lum < MINLUM) {
193 <                        ls[i] = MINBRT-1;               /* bogus value */
194 <                        lum = MINLUM;
281 >                if (tmNeedMatrix(tms)) {                /* get monitor RGB */
282 >                        colortrans(cmon, tms->cmat, scan[i]);
283                  } else {
284 <                        d = TM_BRTSCALE*log(lum);       /* encode it */
285 <                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
284 >                        cmon[RED] = tms->inpsf*scan[i][RED];
285 >                        cmon[GRN] = tms->inpsf*scan[i][GRN];
286 >                        cmon[BLU] = tms->inpsf*scan[i][BLU];
287                  }
288 + #ifdef isfinite
289 +                if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f;
290 +                if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f;
291 +                if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f;
292 + #else
293 +                if (cmon[RED] < .0f) cmon[RED] = .0f;
294 +                if (cmon[GRN] < .0f) cmon[GRN] = .0f;
295 +                if (cmon[BLU] < .0f) cmon[BLU] = .0f;
296 + #endif
297 +                                                        /* world luminance */
298 +                lum =   tms->clf[RED]*cmon[RED] +
299 +                        tms->clf[GRN]*cmon[GRN] +
300 +                        tms->clf[BLU]*cmon[BLU] ;
301 +                if (lum <= TM_NOLUM) {                  /* convert brightness */
302 +                        lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM;
303 +                        ls[i] = TM_NOBRT;
304 +                } else
305 +                        ls[i] = tmCvLumLUfp(&lum);
306                  if (cs == TM_NOCHROM)                   /* no color? */
307                          continue;
308 <                if (tmTop->flags & TM_F_MESOPIC && lum < LMESUPPER) {
308 >                if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
309                          slum = scotlum(cmon);           /* mesopic adj. */
310 <                        if (lum < LMESLOWER)
310 >                        if (lum < LMESLOWER) {
311                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
312 <                        else {
312 >                        } else {
313                                  d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
314 <                                if (tmTop->flags & TM_F_BW)
314 >                                if (tms->flags & TM_F_BW)
315                                          cmon[RED] = cmon[GRN] =
316                                                          cmon[BLU] = d*lum;
317                                  else
318                                          scalecolor(cmon, d);
319 <                                d = (1.-d)*slum;
319 >                                d = (1.f-d)*slum;
320                                  cmon[RED] += d;
321                                  cmon[GRN] += d;
322                                  cmon[BLU] += d;
323                          }
324 <                } else if (tmTop->flags & TM_F_BW) {
324 >                } else if (tms->flags & TM_F_BW) {
325                          cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
326                  }
327 <                d = tmTop->clf[RED]*cmon[RED]/lum;
328 <                cs[3*i  ] = d>=.999 ? 255 :
329 <                                (int)(256.*pow(d, 1./tmTop->mongam));
330 <                d = tmTop->clf[GRN]*cmon[GRN]/lum;
331 <                cs[3*i+1] = d>=.999 ? 255 :
332 <                                (int)(256.*pow(d, 1./tmTop->mongam));
226 <                d = tmTop->clf[BLU]*cmon[BLU]/lum;
227 <                cs[3*i+2] = d>=.999 ? 255 :
228 <                                (int)(256.*pow(d, 1./tmTop->mongam));
327 >                d = tms->clf[RED]*cmon[RED]/lum;
328 >                cs[3*i  ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
329 >                d = tms->clf[GRN]*cmon[GRN]/lum;
330 >                cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
331 >                d = tms->clf[BLU]*cmon[BLU]/lum;
332 >                cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
333          }
334          returnOK;
335   }
336  
337  
338   int
339 < tmCvGrays(ls, scan, len)                /* convert float gray values */
340 < TMbright        *ls;
341 < float   *scan;
342 < int     len;
339 > tmAddHisto(                             /* add values to histogram */
340 > TMstruct        *tms,
341 > TMbright        *ls,
342 > int     len,
343 > int     wt
344 > )
345   {
346 <        static char     funcName[] = "tmCvGrays";
241 <        register double d;
242 <        register int    i;
243 <
244 <        if (tmTop == NULL)
245 <                returnErr(TM_E_TMINVAL);
246 <        if (ls == NULL | scan == NULL | len < 0)
247 <                returnErr(TM_E_ILLEGAL);
248 <        for (i = len; i--; )
249 <                if (scan[i] <= TM_NOLUM) {
250 <                        ls[i] = TM_NOBRT;               /* bogus value */
251 <                } else {
252 <                        d = TM_BRTSCALE*log(scan[i]);   /* encode it */
253 <                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
254 <                }
255 <        returnOK;
256 < }
257 <
258 <
259 < int
260 < tmAddHisto(ls, len, wt)                 /* add values to histogram */
261 < register TMbright       *ls;
262 < int     len;
263 < int     wt;
264 < {
265 <        static char     funcName[] = "tmAddHisto";
346 >        static const char funcName[] = "tmAddHisto";
347          int     oldorig=0, oldlen, horig, hlen;
348 <        register int    i, j;
348 >        int     i, j;
349  
350 <        if (tmTop == NULL)
350 >        if (tms == NULL)
351                  returnErr(TM_E_TMINVAL);
352          if (len < 0)
353                  returnErr(TM_E_ILLEGAL);
354          if (len == 0)
355                  returnOK;
356                                                  /* first, grow limits */
357 <        if (tmTop->histo == NULL) {
357 >        if (tms->histo == NULL) {
358                  for (i = len; i-- && ls[i] < MINBRT; )
359                          ;
360                  if (i < 0)
361                          returnOK;
362 <                tmTop->hbrmin = tmTop->hbrmax = ls[i];
362 >                tms->hbrmin = tms->hbrmax = ls[i];
363                  oldlen = 0;
364          } else {
365 <                oldorig = (tmTop->hbrmin-MINBRT)/HISTEP;
366 <                oldlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
365 >                oldorig = HISTI(tms->hbrmin);
366 >                oldlen = HISTI(tms->hbrmax) + 1 - oldorig;
367          }
368          for (i = len; i--; ) {
369                  if ((j = ls[i]) < MINBRT)
370                          continue;
371 <                if (j < tmTop->hbrmin)
372 <                        tmTop->hbrmin = j;
373 <                else if (j > tmTop->hbrmax)
374 <                        tmTop->hbrmax = j;
371 >                if (j < tms->hbrmin)
372 >                        tms->hbrmin = j;
373 >                else if (j > tms->hbrmax)
374 >                        tms->hbrmax = j;
375          }
376 <        horig = (tmTop->hbrmin-MINBRT)/HISTEP;
377 <        hlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - horig;
376 >        horig = HISTI(tms->hbrmin);
377 >        hlen = HISTI(tms->hbrmax) + 1 - horig;
378          if (hlen > oldlen) {                    /* (re)allocate histogram */
379 <                register int    *newhist = (int *)calloc(hlen, sizeof(int));
379 >                int     *newhist = (int *)calloc(hlen, sizeof(int));
380                  if (newhist == NULL)
381                          returnErr(TM_E_NOMEM);
382                  if (oldlen) {                   /* copy and free old */
383                          for (i = oldlen, j = i+oldorig-horig; i; )
384 <                                newhist[--j] = tmTop->histo[--i];
385 <                        free((MEM_PTR)tmTop->histo);
384 >                                newhist[--j] = tms->histo[--i];
385 >                        free((MEM_PTR)tms->histo);
386                  }
387 <                tmTop->histo = newhist;
387 >                tms->histo = newhist;
388          }
389          if (wt == 0)
390                  returnOK;
391          for (i = len; i--; )                    /* add in new counts */
392                  if (ls[i] >= MINBRT)
393 <                        tmTop->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
393 >                        tms->histo[ HISTI(ls[i]) - horig ] += wt;
394          returnOK;
395   }
396  
397  
398   static double
399 < htcontrs(La)            /* human threshold contrast sensitivity, dL(La) */
400 < double  La;
399 > htcontrs(               /* human threshold contrast sensitivity, dL(La) */
400 > double  La
401 > )
402   {
403          double  l10La, l10dL;
404                                  /* formula taken from Ferwerda et al. [SG96] */
# Line 336 | Line 418 | double La;
418   }
419  
420  
339 static int
340 tmNewMap()
341 {
342        if (tmTop->lumap != NULL && (tmTop->mbrmax - tmTop->mbrmin) !=
343                                        (tmTop->hbrmax - tmTop->hbrmin)) {
344                free((MEM_PTR)tmTop->lumap);
345                tmTop->lumap = NULL;
346        }
347        tmTop->mbrmin = tmTop->hbrmin;
348        tmTop->mbrmax = tmTop->hbrmax;
349        if (tmTop->mbrmin > tmTop->mbrmax)
350                return 0;
351        if (tmTop->lumap == NULL)
352                tmTop->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
353                                        (tmTop->mbrmax-tmTop->mbrmin+1));
354        return(tmTop->lumap != NULL);
355 }
356
357
421   int
422 < tmFixedMapping(expmult, gamval)
423 < double  expmult;
424 < double  gamval;
422 > tmFixedMapping(                 /* compute fixed, linear tone-mapping */
423 > TMstruct        *tms,
424 > double  expmult,
425 > double  gamval
426 > )
427   {
428 <        static char     funcName[] = "tmFixedMapping";
428 >        static const char funcName[] = "tmFixedMapping";
429          double          d;
430 <        register int    i;
430 >        int     i;
431          
432 <        if (!tmNewMap())
432 >        if (!tmNewMap(tms))
433                  returnErr(TM_E_NOMEM);
434          if (expmult <= .0)
435                  expmult = 1.;
436          if (gamval < MINGAM)
437 <                gamval = tmTop->mongam;
438 <        d = log(expmult/tmTop->inpsf);
439 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; )
440 <                tmTop->lumap[i] = 256. * exp(
441 <                        ( d + (tmTop->mbrmin+i)*(1./TM_BRTSCALE) )
442 <                        / gamval );
437 >                gamval = tms->mongam;
438 >        d = log(expmult/tms->inpsf);
439 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
440 >                double  val = 256. * exp(
441 >                        ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
442 >                        / gamval);
443 >                tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val;
444 >        }
445          returnOK;
446   }
447  
448  
449   int
450 < tmComputeMapping(gamval, Lddyn, Ldmax)
451 < double  gamval;
452 < double  Lddyn;
453 < double  Ldmax;
450 > tmComputeMapping(                       /* compute histogram tone-mapping */
451 > TMstruct        *tms,
452 > double  gamval,
453 > double  Lddyn,
454 > double  Ldmax
455 > )
456   {
457 <        static char     funcName[] = "tmComputeMapping";
457 >        static const char funcName[] = "tmComputeMapping";
458          int     *histo;
459          float   *cumf;
460          int     brt0, histlen, threshold, ceiling, trimmings;
461          double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
462 <        int4    histot;
462 >        int32   histot;
463          double  sum;
464 <        register double d;
465 <        register int    i, j;
464 >        double  d;
465 >        int     i, j;
466  
467 <        if (tmTop == NULL || tmTop->histo == NULL)
467 >        if (tms == NULL || tms->histo == NULL)
468                  returnErr(TM_E_TMINVAL);
469                                          /* check arguments */
470          if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
471          if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
472 <        if (gamval < MINGAM) gamval = tmTop->mongam;
472 >        if (gamval < MINGAM) gamval = tms->mongam;
473                                          /* compute handy values */
474          Ldmin = Ldmax/Lddyn;
475          logLddyn = log(Lddyn);
476          Ldavg = sqrt(Ldmax*Ldmin);
477 <        i = (tmTop->hbrmin-MINBRT)/HISTEP;
478 <        brt0 = MINBRT + HISTEP/2 + i*HISTEP;
479 <        histlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - i;
477 >        i = HISTI(tms->hbrmin);
478 >        brt0 = HISTV(i);
479 >        histlen = HISTI(tms->hbrmax) + 1 - i;
480                                          /* histogram total and mean */
481          histot = 0; sum = 0;
482          j = brt0 + histlen*HISTEP;
483          for (i = histlen; i--; ) {
484 <                histot += tmTop->histo[i];
485 <                sum += (j -= HISTEP) * tmTop->histo[i];
484 >                histot += tms->histo[i];
485 >                sum += (double)(j -= HISTEP) * tms->histo[i];
486          }
487 <        threshold = histot*.025 + .5;
488 <        if (threshold < 4)
487 >        threshold = histot*0.005 + .5;
488 >        if (!histot)
489                  returnErr(TM_E_TMFAIL);
490          Lwavg = tmLuminance( (double)sum / histot );
422                                        /* allocate space for mapping */
423        if (!tmNewMap())
424                returnErr(TM_E_NOMEM);
491                                          /* use linear tone mapping? */
492 <        if (tmTop->flags & TM_F_LINEAR)
492 >        if (tms->flags & TM_F_LINEAR || threshold < 4 ||
493 >                        tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn)
494                  goto linearmap;
495                                          /* clamp histogram */
496          histo = (int *)malloc(histlen*sizeof(int));
497          cumf = (float *)malloc((histlen+2)*sizeof(float));
498 <        if (histo == NULL | cumf == NULL)
498 >        if ((histo == NULL) | (cumf == NULL))
499                  returnErr(TM_E_NOMEM);
500          cumf[histlen+1] = 1.;           /* guard for assignment code */
501          for (i = histlen; i--; )        /* make malleable copy */
502 <                histo[i] = tmTop->histo[i];
502 >                histo[i] = tms->histo[i];
503          do {                            /* iterate to solution */
504                  sum = 0;                /* cumulative probability */
505                  for (i = 0; i < histlen; i++) {
# Line 440 | Line 507 | double Ldmax;
507                          sum += histo[i];
508                  }
509                  cumf[histlen] = 1.;
510 <                Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) /
510 >                Tr = histot * (double)(tms->hbrmax - tms->hbrmin) /
511                          ((double)histlen*TM_BRTSCALE) / logLddyn;
512                  ceiling = Tr + 1.;
513                  trimmings = 0;          /* clip to envelope */
514                  for (i = histlen; i--; ) {
515 <                        if (tmTop->flags & TM_F_HCONTR) {
515 >                        if (tms->flags & TM_F_HCONTR) {
516                                  Lw = tmLuminance(brt0 + i*HISTEP);
517                                  Ld = Ldmin * exp( logLddyn *
518                                          .5*(cumf[i]+cumf[i+1]) );
# Line 464 | Line 531 | double Ldmax;
531                          goto linearmap;
532                  }
533          } while (trimmings > threshold);
534 +                                        /* allocate space for mapping */
535 +        if (!tmNewMap(tms))
536 +                returnErr(TM_E_NOMEM);
537                                          /* assign tone-mapping */
538 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) {
539 <                j = d = (double)i/(tmTop->mbrmax-tmTop->mbrmin)*histlen;
538 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
539 >                j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
540                  d -= (double)j;
541                  Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
542                  d = (Ld - Ldmin)/(Ldmax - Ldmin);
543 <                tmTop->lumap[i] = 256.*pow(d, 1./gamval);
543 >                tms->lumap[i] = 256.*pow(d, 1./gamval);
544          }
545          free((MEM_PTR)histo);           /* clean up and return */
546          free((MEM_PTR)cumf);
547          returnOK;
548   linearmap:                              /* linear tone-mapping */
549 <        if (tmTop->flags & TM_F_HCONTR)
549 >        if (tms->flags & TM_F_HCONTR)
550                  d = htcontrs(Ldavg) / htcontrs(Lwavg);
551          else
552                  d = Ldavg / Lwavg;
553 <        return(tmFixedMapping(tmTop->inpsf*d/Ldmax, gamval));
553 >        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
554   }
555  
556  
557   int
558 < tmMapPixels(ps, ls, cs, len)
559 < register BYTE   *ps;
560 < TMbright        *ls;
561 < register BYTE   *cs;
562 < int     len;
558 > tmMapPixels(                    /* apply tone-mapping to pixel(s) */
559 > TMstruct        *tms,
560 > BYTE    *ps,
561 > TMbright        *ls,
562 > BYTE    *cs,
563 > int     len
564 > )
565   {
566 <        static char     funcName[] = "tmMapPixels";
567 <        register int4   li, pv;
566 >        static const char funcName[] = "tmMapPixels";
567 >        int32   li, pv;
568  
569 <        if (tmTop == NULL || tmTop->lumap == NULL)
569 >        if (tms == NULL || tms->lumap == NULL)
570                  returnErr(TM_E_TMINVAL);
571 <        if (ps == NULL | ls == NULL | len < 0)
571 >        if ((ps == NULL) | (ls == NULL) | (len < 0))
572                  returnErr(TM_E_ILLEGAL);
573          while (len--) {
574 <                if ((li = *ls++) < tmTop->mbrmin) {
574 >                if ((li = *ls++) < tms->mbrmin) {
575                          li = 0;
576                  } else {
577 <                        if (li > tmTop->mbrmax)
578 <                                li = tmTop->mbrmax;
579 <                        li = tmTop->lumap[li - tmTop->mbrmin];
577 >                        if (li > tms->mbrmax)
578 >                                li = tms->mbrmax;
579 >                        li = tms->lumap[li - tms->mbrmin];
580                  }
581                  if (cs == TM_NOCHROM)
582                          *ps++ = li>255 ? 255 : li;
583                  else {
584 <                        pv = *cs++ * li / tmTop->cdiv[RED];
584 >                        pv = *cs++ * li / tms->cdiv[RED];
585                          *ps++ = pv>255 ? 255 : pv;
586 <                        pv = *cs++ * li / tmTop->cdiv[GRN];
586 >                        pv = *cs++ * li / tms->cdiv[GRN];
587                          *ps++ = pv>255 ? 255 : pv;
588 <                        pv = *cs++ * li / tmTop->cdiv[BLU];
588 >                        pv = *cs++ * li / tms->cdiv[BLU];
589                          *ps++ = pv>255 ? 255 : pv;
590                  }
591          }
# Line 521 | Line 593 | int    len;
593   }
594  
595  
596 < struct tmStruct *
597 < tmPop()                         /* pop top tone mapping off stack */
596 > TMstruct *
597 > tmDup(                          /* duplicate top tone mapping */
598 > TMstruct        *tms
599 > )
600   {
527        register struct tmStruct        *tms;
528
529        if ((tms = tmTop) != NULL)
530                tmTop = tms->tmprev;
531        return(tms);
532 }
533
534
535 int
536 tmPull(tms)                     /* pull a tone mapping from stack */
537 register struct tmStruct        *tms;
538 {
539        register struct tmStruct        *tms2;
540                                        /* special cases first */
541        if (tms == NULL | tmTop == NULL)
542                return(0);
543        if (tms == tmTop) {
544                tmTop = tms->tmprev;
545                tms->tmprev = NULL;
546                return(1);
547        }
548        for (tms2 = tmTop; tms2->tmprev != NULL; tms2 = tms2->tmprev)
549                if (tms == tms2->tmprev) {      /* remove it */
550                        tms2->tmprev = tms->tmprev;
551                        tms->tmprev = NULL;
552                        return(1);
553                }
554        return(0);                      /* not found on stack */
555 }
556
557
558 struct tmStruct *
559 tmDup()                         /* duplicate top tone mapping */
560 {
601          int     len;
602 <        register int    i;
603 <        register struct tmStruct        *tmnew;
602 >        int     i;
603 >        TMstruct        *tmnew;
604  
605 <        if (tmTop == NULL)              /* anything to duplicate? */
605 >        if (tms == NULL)                /* anything to duplicate? */
606                  return(NULL);
607 <        tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct));
607 >        tmnew = (TMstruct *)malloc(sizeof(TMstruct));
608          if (tmnew == NULL)
609                  return(NULL);
610 <        *tmnew = *tmTop;                /* copy everything */
610 >        *tmnew = *tms;          /* copy everything */
611          if (tmnew->histo != NULL) {     /* duplicate histogram */
612 <                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
573 <                                (tmnew->hbrmin-MINBRT)/HISTEP;
612 >                len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
613                  tmnew->histo = (int *)malloc(len*sizeof(int));
614                  if (tmnew->histo != NULL)
615                          for (i = len; i--; )
616 <                                tmnew->histo[i] = tmTop->histo[i];
616 >                                tmnew->histo[i] = tms->histo[i];
617          }
618          if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */
619                  len = tmnew->mbrmax-tmnew->mbrmin+1;
# Line 582 | Line 621 | tmDup()                                /* duplicate top tone mapping */
621                                                  len*sizeof(unsigned short) );
622                  if (tmnew->lumap != NULL)
623                          for (i = len; i--; )
624 <                                tmnew->lumap[i] = tmTop->lumap[i];
624 >                                tmnew->lumap[i] = tms->lumap[i];
625          }
626                                          /* clear package data */
627          for (i = tmNumPkgs; i--; )
628                  tmnew->pd[i] = NULL;
629 <        tmnew->tmprev = tmTop;          /* make copy current */
630 <        return(tmTop = tmnew);
629 >                                        /* return copy */
630 >        return(tmnew);
631   }
632  
633  
595 int
596 tmPush(tms)                     /* push tone mapping on top of stack */
597 register struct tmStruct        *tms;
598 {
599        static char     funcName[] = "tmPush";
600                                        /* check validity */
601        if (tms == NULL)
602                returnErr(TM_E_ILLEGAL);
603        if (tms == tmTop)               /* check necessity */
604                returnOK;
605                                        /* pull if already in stack */
606        (void)tmPull(tms);
607                                        /* push it on top */
608        tms->tmprev = tmTop;
609        tmTop = tms;
610        returnOK;
611 }
612
613
634   void
635   tmDone(tms)                     /* done with tone mapping -- destroy it */
636 < register struct tmStruct        *tms;
636 > TMstruct        *tms;
637   {
638 <        register int    i;
639 <                                        /* NULL arg. is equiv. to tmTop */
640 <        if (tms == NULL && (tms = tmTop) == NULL)
638 >        int     i;
639 >                                        /* NULL arg. is equiv. to tms */
640 >        if (tms == NULL)
641                  return;
622                                        /* take out of stack if present */
623        (void)tmPull(tms);
642                                          /* free tables */
643          if (tms->histo != NULL)
644                  free((MEM_PTR)tms->histo);
# Line 640 | Line 658 | BYTE   tmMesofact[BMESUPPER-BMESLOWER];
658   void
659   tmMkMesofact()                          /* build mesopic lookup factor table */
660   {
661 <        register int    i;
661 >        int     i;
662  
663          if (tmMesofact[BMESUPPER-BMESLOWER-1])
664                  return;
# Line 653 | Line 671 | tmMkMesofact()                         /* build mesopic lookup factor table
671  
672  
673   int
674 < tmErrorReturn(func, err)                /* error return (with message) */
675 < char    *func;
676 < int     err;
674 > tmNewMap(                       /* allocate new tone-mapping array */
675 > TMstruct        *tms
676 > )
677   {
678 <        tmLastFunction = func;
679 <        tmLastError = err;
680 <        if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR)
681 <                return(err);
678 >        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
679 >                                        (tms->hbrmax - tms->hbrmin)) {
680 >                free((MEM_PTR)tms->lumap);
681 >                tms->lumap = NULL;
682 >        }
683 >        tms->mbrmin = tms->hbrmin;
684 >        tms->mbrmax = tms->hbrmax;
685 >        if (tms->mbrmin > tms->mbrmax)
686 >                return 0;
687 >        if (tms->lumap == NULL)
688 >                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
689 >                                        (tms->mbrmax-tms->mbrmin+1));
690 >        return(tms->lumap != NULL);
691 > }
692 >
693 >
694 > int
695 > tmErrorReturn(                          /* error return (with message) */
696 > const char      *func,
697 > TMstruct        *tms,
698 > int     err
699 > )
700 > {
701 >        if (tms != NULL) {
702 >                tms->lastFunc = func;
703 >                tms->lastError = err;
704 >                if (tms->flags & TM_F_NOSTDERR)
705 >                        return(err);
706 >        }
707          fputs(func, stderr);
708          fputs(": ", stderr);
709          fputs(tmErrorMessage[err], stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines