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.6 by gregl, Mon Nov 17 14:02:12 1997 UTC vs.
Revision 3.35 by greg, Mon Mar 7 20:49:19 2011 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Tone mapping functions.
6   * See tonemap.h for detailed function descriptions.
7 + * Added von Kries white-balance calculations 10/01 (GW).
8 + *
9 + * Externals declared in tonemap.h
10   */
11  
12 + #include "copyright.h"
13 +
14   #include        <stdio.h>
15 + #include        <stdlib.h>
16   #include        <math.h>
17   #include        "tmprivat.h"
18   #include        "tmerrmsg.h"
19  
20   #define exp10(x)        exp(M_LN10*(x))
21  
19 struct tmStruct *tmTop = NULL;          /* current tone mapping stack */
20
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  
29 int
30 tmErrorReturn(func, err)                /* error return (with message) */
31 char    *func;
32 int     err;
33 {
34        tmLastFunction = func;
35        tmLastError = err;
36        if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR)
37                return(err);
38        fputs(func, stderr);
39        fputs(": ", stderr);
40        fputs(tmErrorMessage[err], stderr);
41        fputs("!\n", stderr);
42        return(err);
43 }
31  
32 <
33 < struct tmStruct *
34 < tmInit(flags, monpri, gamval)           /* initialize new tone mapping */
35 < int     flags;
36 < RGBPRIMP        monpri;
37 < double  gamval;
32 > TMstruct *
33 > tmInit(                                 /* initialize new tone mapping */
34 > int     flags,
35 > RGBPRIMP        monpri,
36 > double  gamval
37 > )
38   {
52        static char     funcName[] = "tmInit";
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 86 | 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->hbrmin = tmnew->hbrmax = 0;
77 >        tmnew->inpdat = NULL;
78 >        tmnew->hbrmin = 10; tmnew->hbrmax = -10;
79          tmnew->histo = NULL;
80 <        tmnew->mbrmin = tmnew->mbrmax = 0;
80 >        tmnew->mbrmin = 10; tmnew->mbrmax = -10;
81          tmnew->lumap = NULL;
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 <                        comprgb2xyzmat(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 <                compxyz2rgbmat(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 <                comprgb2rgbmat(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;
185 <        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]);
194 <                else {
195 <                        cmon[RED] = tmTop->inpsf*scan[i][RED];
196 <                        cmon[GRN] = tmTop->inpsf*scan[i][GRN];
197 <                        cmon[BLU] = tmTop->inpsf*scan[i][BLU];
198 <                }
199 <                                                        /* world luminance */
200 <                lum =   tmTop->clf[RED]*cmon[RED] +
201 <                        tmTop->clf[GRN]*cmon[GRN] +
202 <                        tmTop->clf[BLU]*cmon[BLU] ;
203 <                                                        /* check range */
204 <                if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
205 <                        lum =   tmTop->clf[RED]*cmon[RED] +
206 <                                tmTop->clf[GRN]*cmon[GRN] +
207 <                                tmTop->clf[BLU]*cmon[BLU] ;
208 <                if (lum < MINLUM) {
209 <                        ls[i] = MINBRT-1;               /* bogus value */
210 <                        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));
242 <                d = tmTop->clf[BLU]*cmon[BLU]/lum;
243 <                cs[3*i+2] = d>=.999 ? 255 :
244 <                                (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 < tmAddHisto(ls, len, wt)                 /* add values to histogram */
340 < register TMbright       *ls;
341 < int     len;
342 < int     wt;
339 > tmAddHisto(                             /* add values to histogram */
340 > TMstruct        *tms,
341 > TMbright        *ls,
342 > int     len,
343 > int     wt
344 > )
345   {
346 <        static char     funcName[] = "tmAddHisto";
347 <        int     sum, oldorig, oldlen, horig, hlen;
348 <        register int    i, j;
346 >        static const char funcName[] = "tmAddHisto";
347 >        int     oldorig=0, oldlen, horig, hlen;
348 >        int     i, j;
349  
350 <        if (len <= 0)
261 <                returnErr(TM_E_ILLEGAL);
262 <        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 326 | Line 419 | double La;
419  
420  
421   int
422 < tmComputeMapping(gamval, Lddyn, Ldmax)
423 < double  gamval;
424 < double  Lddyn;
425 < double  Ldmax;
422 > tmFixedMapping(                 /* compute fixed, linear tone-mapping */
423 > TMstruct        *tms,
424 > double  expmult,
425 > double  gamval
426 > )
427   {
428 <        static char     funcName[] = "tmComputeMapping";
428 >        static const char funcName[] = "tmFixedMapping";
429 >        double          d;
430 >        int     i;
431 >        
432 >        if (!tmNewMap(tms))
433 >                returnErr(TM_E_NOMEM);
434 >        if (expmult <= .0)
435 >                expmult = 1.;
436 >        if (gamval < MINGAM)
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(                       /* compute histogram tone-mapping */
451 > TMstruct        *tms,
452 > double  gamval,
453 > double  Lddyn,
454 > double  Ldmax
455 > )
456 > {
457 >        static const char funcName[] = "tmComputeMapping";
458          int     *histo;
459          float   *cumf;
460 <        int     brt0, histlen, histot, threshold, ceiling, trimmings;
460 >        int     brt0, histlen, threshold, ceiling, trimmings;
461          double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
462 <        int4    sum;
463 <        register double d;
464 <        register int    i, j;
462 >        int32   histot;
463 >        double  sum;
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 );
491 <        if (!(tmTop->flags & TM_F_LINEAR)) {    /* clamp histogram */
492 <                histo = (int *)malloc(histlen*sizeof(int));
493 <                cumf = (float *)malloc((histlen+1)*sizeof(float));
494 <                if (histo == NULL | cumf == NULL)
495 <                        returnErr(TM_E_NOMEM);
496 <                for (i = histlen; i--; )        /* make malleable copy */
497 <                        histo[i] = tmTop->histo[i];
498 <                do {                            /* iterate to solution */
499 <                        sum = 0;                /* cumulative probability */
500 <                        for (i = 0; i < histlen; i++) {
501 <                                cumf[i] = (double)sum/histot;
502 <                                sum += histo[i];
491 >                                        /* use linear tone mapping? */
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))
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] = tms->histo[i];
503 >        do {                            /* iterate to solution */
504 >                sum = 0;                /* cumulative probability */
505 >                for (i = 0; i < histlen; i++) {
506 >                        cumf[i] = (double)sum/histot;
507 >                        sum += histo[i];
508 >                }
509 >                cumf[histlen] = 1.;
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 (tms->flags & TM_F_HCONTR) {
516 >                                Lw = tmLuminance(brt0 + i*HISTEP);
517 >                                Ld = Ldmin * exp( logLddyn *
518 >                                        .5*(cumf[i]+cumf[i+1]) );
519 >                                ceiling = Tr * (htcontrs(Ld) * Lw) /
520 >                                        (htcontrs(Lw) * Ld) + 1.;
521                          }
522 <                        cumf[i] = 1.;
523 <                        Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) /
524 <                                ((double)histlen*TM_BRTSCALE) / logLddyn;
383 <                        ceiling = Tr + 1.;
384 <                        trimmings = 0;                  /* clip to envelope */
385 <                        for (i = histlen; i--; ) {
386 <                                if (tmTop->flags & TM_F_HCONTR) {
387 <                                        Lw = tmLuminance(brt0 + i*HISTEP);
388 <                                        Ld = Ldmin * exp( logLddyn *
389 <                                                .5*(cumf[i]+cumf[i+1]) );
390 <                                        ceiling = Tr * (htcontrs(Ld) * Lw) /
391 <                                                (htcontrs(Lw) * Ld) + 1.;
392 <                                }
393 <                                if (histo[i] > ceiling) {
394 <                                        trimmings += histo[i] - ceiling;
395 <                                        histo[i] = ceiling;
396 <                                }
522 >                        if (histo[i] > ceiling) {
523 >                                trimmings += histo[i] - ceiling;
524 >                                histo[i] = ceiling;
525                          }
398                } while ((histot -= trimmings) > threshold &&
399                                                trimmings > threshold);
400        }
401                                                /* allocate luminance map */
402        if (tmTop->lumap != NULL)
403                free((MEM_PTR)tmTop->lumap);
404        tmTop->mbrmin = tmTop->hbrmin;
405        tmTop->mbrmax = tmTop->hbrmax;
406        tmTop->lumap = (unsigned short *)malloc(
407                (tmTop->mbrmax-tmTop->mbrmin+1)*sizeof(unsigned short) );
408        if (tmTop->lumap == NULL)
409                returnErr(TM_E_NOMEM);
410        if (tmTop->flags & TM_F_LINEAR || histot <= threshold) {
411                                                /* linear tone mapping */
412                if (tmTop->flags & TM_F_HCONTR)
413                        d = htcontrs(Ldavg) / htcontrs(Lwavg);
414                else
415                        d = Ldavg / Lwavg;
416                d = log(d/Ldmax);
417                for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; )
418                        tmTop->lumap[i] = 256. * exp(
419                                ( d + (tmTop->mbrmin+i)/(double)TM_BRTSCALE )
420                                / gamval );
421        } else {
422                                                /* histogram adjustment */
423                for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) {
424                        j = d = (double)i/(tmTop->mbrmax-tmTop->mbrmin)*histlen;
425                        d -= (double)j;
426                        Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
427                        d = (Ld - Ldmin)/(Ldmax - Ldmin);
428                        tmTop->lumap[i] = 256.*pow(d, 1./gamval);
526                  }
527 +                                        /* check if we're out of data */
528 +                if ((histot -= trimmings) <= threshold) {
529 +                        free((MEM_PTR)histo);
530 +                        free((MEM_PTR)cumf);
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 = 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 +                tms->lumap[i] = 256.*pow(d, 1./gamval);
544          }
545 <        if (!(tmTop->flags & TM_F_LINEAR)) {
546 <                free((MEM_PTR)histo);
433 <                free((MEM_PTR)cumf);
434 <        }
545 >        free((MEM_PTR)histo);           /* clean up and return */
546 >        free((MEM_PTR)cumf);
547          returnOK;
548 + linearmap:                              /* linear tone-mapping */
549 +        if (tms->flags & TM_F_HCONTR)
550 +                d = htcontrs(Ldavg) / htcontrs(Lwavg);
551 +        else
552 +                d = Ldavg / Lwavg;
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)
575 <                        li = tmTop->mbrmin;
576 <                else if (li > tmTop->mbrmax)
577 <                        li = tmTop->mbrmax;
578 <                li = tmTop->lumap[li - tmTop->mbrmin];
574 >                if ((li = *ls++) < tms->mbrmin) {
575 >                        li = 0;
576 >                } else {
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 471 | 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   {
477        register struct tmStruct        *tms;
478
479        if ((tms = tmTop) != NULL)
480                tmTop = tms->tmprev;
481        return(tms);
482 }
483
484
485 int
486 tmPull(tms)                     /* pull a tone mapping from stack */
487 register struct tmStruct        *tms;
488 {
489        register struct tmStruct        *tms2;
490                                        /* special cases first */
491        if (tms == NULL | tmTop == NULL)
492                return(0);
493        if (tms == tmTop) {
494                tmTop = tms->tmprev;
495                tms->tmprev = NULL;
496                return(1);
497        }
498        for (tms2 = tmTop; tms2->tmprev != NULL; tms2 = tms2->tmprev)
499                if (tms == tms2->tmprev) {      /* remove it */
500                        tms2->tmprev = tms->tmprev;
501                        tms->tmprev = NULL;
502                        return(1);
503                }
504        return(0);                      /* not found on stack */
505 }
506
507
508 struct tmStruct *
509 tmDup()                         /* duplicate top tone mapping */
510 {
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 -
523 <                                (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 532 | 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  
545 int
546 tmPush(tms)                     /* push tone mapping on top of stack */
547 register struct tmStruct        *tms;
548 {
549        static char     funcName[] = "tmPush";
550                                        /* check validity */
551        if (tms == NULL)
552                returnErr(TM_E_ILLEGAL);
553        if (tms == tmTop)               /* check necessity */
554                returnOK;
555                                        /* pull if already in stack */
556        (void)tmPull(tms);
557                                        /* push it on top */
558        tms->tmprev = tmTop;
559        tmTop = tms;
560        returnOK;
561 }
562
563
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;
572                                        /* take out of stack if present */
573        (void)tmPull(tms);
642                                          /* free tables */
643          if (tms->histo != NULL)
644                  free((MEM_PTR)tms->histo);
# Line 581 | Line 649 | register struct tmStruct       *tms;
649                  if (tms->pd[i] != NULL)
650                          (*tmPkg[i]->Free)(tms->pd[i]);
651          free((MEM_PTR)tms);             /* free basic structure */
652 + }
653 +
654 + /******************** Shared but Private library routines *********************/
655 +
656 + BYTE    tmMesofact[BMESUPPER-BMESLOWER];
657 +
658 + void
659 + tmMkMesofact()                          /* build mesopic lookup factor table */
660 + {
661 +        int     i;
662 +
663 +        if (tmMesofact[BMESUPPER-BMESLOWER-1])
664 +                return;
665 +
666 +        for (i = BMESLOWER; i < BMESUPPER; i++)
667 +                tmMesofact[i-BMESLOWER] = 256. *
668 +                                (tmLuminance(i) - LMESLOWER) /
669 +                                (LMESUPPER - LMESLOWER);
670 + }
671 +
672 +
673 + int
674 + tmNewMap(                       /* allocate new tone-mapping array */
675 + TMstruct        *tms
676 + )
677 + {
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);
710 +        fputs("!\n", stderr);
711 +        return(err);
712   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines