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.4 by greg, Fri Apr 18 13:59:43 1997 UTC vs.
Revision 3.34 by greg, Mon Feb 9 20:48:08 2009 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 +                                        /* luminance->brightness lookup */
27 + static TMbright         *tmFloat2BrtLUT = NULL;
28  
29 < int
27 < tmErrorReturn(func, err)                /* error return (with message) */
28 < char    *func;
29 < int     err;
30 < {
31 <        if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR)
32 <                return(err);
33 <        fputs(func, stderr);
34 <        fputs(": ", stderr);
35 <        fputs(tmErrorMessage[err], stderr);
36 <        fputs("!\n", stderr);
37 <        return(err);
38 < }
29 > #define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15]
30  
31  
32 < struct tmStruct *
33 < tmInit(flags, monpri, gamval)           /* initialize new tone mapping */
34 < int     flags;
35 < RGBPRIMP        monpri;
36 < double  gamval;
32 > TMstruct *
33 > tmInit(                                 /* initialize new tone mapping */
34 > int     flags,
35 > RGBPRIMP        monpri,
36 > double  gamval
37 > )
38   {
47        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 81 | 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->brmin = tmnew->brmax = 0;
77 >        tmnew->inpdat = NULL;
78 >        tmnew->hbrmin = 10; tmnew->hbrmax = -10;
79          tmnew->histo = NULL;
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 +        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 = {1e-6, 1e-6, 1e-6};
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;
179 <        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)
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]);
188 <                else {
189 <                        cmon[RED] = tmTop->inpsf*scan[i][RED];
190 <                        cmon[GRN] = tmTop->inpsf*scan[i][GRN];
191 <                        cmon[BLU] = tmTop->inpsf*scan[i][BLU];
192 <                }
193 <                                                        /* world luminance */
194 <                lum =   tmTop->clf[RED]*cmon[RED] +
195 <                        tmTop->clf[GRN]*cmon[GRN] +
196 <                        tmTop->clf[BLU]*cmon[BLU] ;
197 <                                                        /* check range */
198 <                if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
199 <                        lum =   tmTop->clf[RED]*cmon[RED] +
200 <                                tmTop->clf[GRN]*cmon[GRN] +
201 <                                tmTop->clf[BLU]*cmon[BLU] ;
202 <                if (lum < MINLUM) {
203 <                        ls[i] = MINBRT-1;               /* bogus value */
204 <                        lum = MINLUM;
283 >                if (tmNeedMatrix(tms)) {                /* get monitor RGB */
284 >                        colortrans(cmon, tms->cmat, scan[i]);
285                  } else {
286 <                        d = TM_BRTSCALE*log(lum);       /* encode it */
287 <                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
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 =   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));
236 <                d = tmTop->clf[BLU]*cmon[BLU]/lum;
237 <                cs[3*i+2] = d>=.999 ? 255 :
238 <                                (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 < tmAddHisto(ls, len, wt)                 /* add values to histogram */
342 < register TMbright       *ls;
343 < int     len;
344 < int     wt;
341 > tmAddHisto(                             /* add values to histogram */
342 > TMstruct        *tms,
343 > TMbright        *ls,
344 > int     len,
345 > int     wt
346 > )
347   {
348 <        static char     funcName[] = "tmAddHisto";
349 <        int     sum, oldorig, oldlen, horig, hlen;
350 <        register int    i, j;
348 >        static const char funcName[] = "tmAddHisto";
349 >        int     oldorig=0, oldlen, horig, hlen;
350 >        int     i, j;
351  
352 <        if (len <= 0)
255 <                returnErr(TM_E_ILLEGAL);
256 <        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->brmin = tmTop->brmax = ls[i];
364 >                tms->hbrmin = tms->hbrmax = ls[i];
365                  oldlen = 0;
366          } else {
367 <                oldorig = (tmTop->brmin-MINBRT)/HISTEP;
368 <                oldlen = (tmTop->brmax-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->brmin)
374 <                        tmTop->brmin = j;
375 <                else if (j > tmTop->brmax)
376 <                        tmTop->brmax = j;
373 >                if (j < tms->hbrmin)
374 >                        tms->hbrmin = j;
375 >                else if (j > tms->hbrmax)
376 >                        tms->hbrmax = j;
377          }
378 <        horig = (tmTop->brmin-MINBRT)/HISTEP;
379 <        hlen = (tmTop->brmax-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;
290 <                if (tmTop->lumap != NULL) {     /* invalid tone map */
291 <                        free((MEM_PTR)tmTop->lumap);
292 <                        tmTop->lumap = NULL;
293 <                }
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 324 | Line 421 | double La;
421  
422  
423   int
424 < tmComputeMapping(gamval, Lddyn, Ldmax)
425 < double  gamval;
426 < double  Lddyn;
427 < double  Ldmax;
424 > tmFixedMapping(                 /* compute fixed, linear tone-mapping */
425 > TMstruct        *tms,
426 > double  expmult,
427 > double  gamval
428 > )
429   {
430 <        static char     funcName[] = "tmComputeMapping";
430 >        static const char funcName[] = "tmFixedMapping";
431 >        double          d;
432 >        int     i;
433 >        
434 >        if (!tmNewMap(tms))
435 >                returnErr(TM_E_NOMEM);
436 >        if (expmult <= .0)
437 >                expmult = 1.;
438 >        if (gamval < MINGAM)
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(                       /* compute histogram tone-mapping */
453 > TMstruct        *tms,
454 > double  gamval,
455 > double  Lddyn,
456 > double  Ldmax
457 > )
458 > {
459 >        static const char funcName[] = "tmComputeMapping";
460          int     *histo;
461          float   *cumf;
462 <        int     brt0, histlen, histot, threshold, ceiling, trimmings;
462 >        int     brt0, histlen, threshold, ceiling, trimmings;
463          double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
464 <        int4    sum;
465 <        register double d;
466 <        register int    i, j;
464 >        int32   histot;
465 >        double  sum;
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->brmin-MINBRT)/HISTEP;
480 <        brt0 = MINBRT + HISTEP/2 + i*HISTEP;
481 <        histlen = (tmTop->brmax-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*.025 + .5;
490 <        if (threshold < 4)
489 >        threshold = histot*0.005 + .5;
490 >        if (!histot)
491                  returnErr(TM_E_TMFAIL);
492          Lwavg = tmLuminance( (double)sum / histot );
493 <        if (!(tmTop->flags & TM_F_LINEAR)) {    /* clamp histogram */
494 <                histo = (int *)malloc(histlen*sizeof(int));
495 <                cumf = (float *)malloc((histlen+1)*sizeof(float));
496 <                if (histo == NULL | cumf == NULL)
497 <                        returnErr(TM_E_NOMEM);
498 <                for (i = histlen; i--; )        /* make malleable copy */
499 <                        histo[i] = tmTop->histo[i];
500 <                do {                            /* iterate to solution */
501 <                        sum = 0;                /* cumulative probability */
502 <                        for (i = 0; i < histlen; i++) {
503 <                                cumf[i] = (double)sum/histot;
504 <                                sum += histo[i];
493 >                                        /* use linear tone mapping? */
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));
499 >        cumf = (float *)malloc((histlen+2)*sizeof(float));
500 >        if ((histo == NULL) | (cumf == NULL))
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] = tms->histo[i];
505 >        do {                            /* iterate to solution */
506 >                sum = 0;                /* cumulative probability */
507 >                for (i = 0; i < histlen; i++) {
508 >                        cumf[i] = (double)sum/histot;
509 >                        sum += histo[i];
510 >                }
511 >                cumf[histlen] = 1.;
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 (tms->flags & TM_F_HCONTR) {
518 >                                Lw = tmLuminance(brt0 + i*HISTEP);
519 >                                Ld = Ldmin * exp( logLddyn *
520 >                                        .5*(cumf[i]+cumf[i+1]) );
521 >                                ceiling = Tr * (htcontrs(Ld) * Lw) /
522 >                                        (htcontrs(Lw) * Ld) + 1.;
523                          }
524 <                        cumf[i] = 1.;
525 <                        Tr = histot * (double)(tmTop->brmax - tmTop->brmin) /
526 <                                ((double)histlen*TM_BRTSCALE) / logLddyn;
381 <                        ceiling = Tr + 1.;
382 <                        trimmings = 0;                  /* clip to envelope */
383 <                        for (i = histlen; i--; ) {
384 <                                if (tmTop->flags & TM_F_HCONTR) {
385 <                                        Lw = tmLuminance(brt0 + i*HISTEP);
386 <                                        Ld = Ldmin * exp( logLddyn *
387 <                                                .5*(cumf[i]+cumf[i+1]) );
388 <                                        ceiling = Tr * (htcontrs(Ld) * Lw) /
389 <                                                (htcontrs(Lw) * Ld) + 1.;
390 <                                }
391 <                                if (histo[i] > ceiling) {
392 <                                        trimmings += histo[i] - ceiling;
393 <                                        histo[i] = ceiling;
394 <                                }
524 >                        if (histo[i] > ceiling) {
525 >                                trimmings += histo[i] - ceiling;
526 >                                histo[i] = ceiling;
527                          }
396                } while ((histot -= trimmings) > threshold &&
397                                                trimmings > threshold);
398        }
399                                                /* allocate luminance map */
400        if (tmTop->lumap == NULL) {
401                tmTop->lumap = (unsigned short *)malloc(
402                        (tmTop->brmax-tmTop->brmin+1)*sizeof(unsigned short) );
403                if (tmTop->lumap == NULL)
404                        returnErr(TM_E_NOMEM);
405        }
406        if (tmTop->flags & TM_F_LINEAR || histot <= threshold) {
407                                                /* linear tone mapping */
408                if (tmTop->flags & TM_F_HCONTR)
409                        d = htcontrs(Ldavg) / htcontrs(Lwavg);
410                else
411                        d = Ldavg / Lwavg;
412                d = log(d/Ldmax);
413                for (i = tmTop->brmax-tmTop->brmin+1; i--; )
414                        tmTop->lumap[i] = 256. * exp(
415                                ( d + (tmTop->brmin+i)/(double)TM_BRTSCALE )
416                                / gamval );
417        } else {
418                                                /* histogram adjustment */
419                for (i = tmTop->brmax-tmTop->brmin+1; i--; ) {
420                        j = d = (double)i/(tmTop->brmax-tmTop->brmin)*histlen;
421                        d -= (double)j;
422                        Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
423                        d = (Ld - Ldmin)/(Ldmax - Ldmin);
424                        tmTop->lumap[i] = 256.*pow(d, 1./gamval);
528                  }
529 +                                        /* check if we're out of data */
530 +                if ((histot -= trimmings) <= threshold) {
531 +                        free((MEM_PTR)histo);
532 +                        free((MEM_PTR)cumf);
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 = 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 +                tms->lumap[i] = 256.*pow(d, 1./gamval);
546          }
547 <        if (!(tmTop->flags & TM_F_LINEAR)) {
548 <                free((MEM_PTR)histo);
429 <                free((MEM_PTR)cumf);
430 <        }
547 >        free((MEM_PTR)histo);           /* clean up and return */
548 >        free((MEM_PTR)cumf);
549          returnOK;
550 + linearmap:                              /* linear tone-mapping */
551 +        if (tms->flags & TM_F_HCONTR)
552 +                d = htcontrs(Ldavg) / htcontrs(Lwavg);
553 +        else
554 +                d = Ldavg / Lwavg;
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 int4   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)
573 >        if ((ps == NULL) | (ls == NULL) | (len < 0))
574                  returnErr(TM_E_ILLEGAL);
575          while (len--) {
576 <                if ((li = *ls++) < tmTop->brmin)
577 <                        li = tmTop->brmin;
578 <                else if (li > tmTop->brmax)
579 <                        li = tmTop->brmax;
580 <                li = tmTop->lumap[li - tmTop->brmin];
576 >                if ((li = *ls++) < tms->mbrmin) {
577 >                        li = 0;
578 >                } else {
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 467 | 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   {
473        register struct tmStruct        *tms;
474
475        if ((tms = tmTop) != NULL)
476                tmTop = tms->tmprev;
477        return(tms);
478 }
479
480
481 int
482 tmPull(tms)                     /* pull a tone mapping from stack */
483 register struct tmStruct        *tms;
484 {
485        register struct tmStruct        *tms2;
486                                        /* special cases first */
487        if (tms == NULL | tmTop == NULL)
488                return(0);
489        if (tms == tmTop) {
490                tmTop = tms->tmprev;
491                tms->tmprev = NULL;
492                return(1);
493        }
494        for (tms2 = tmTop; tms2->tmprev != NULL; tms2 = tms2->tmprev)
495                if (tms == tms2->tmprev) {      /* remove it */
496                        tms2->tmprev = tms->tmprev;
497                        tms->tmprev = NULL;
498                        return(1);
499                }
500        return(0);                      /* not found on stack */
501 }
502
503
504 struct tmStruct *
505 tmDup()                         /* duplicate top tone mapping */
506 {
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->brmax-MINBRT)/HISTEP + 1 -
519 <                                (tmnew->brmin-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->brmax-tmnew->brmin+1;
621 >                len = tmnew->mbrmax-tmnew->mbrmin+1;
622                  tmnew->lumap = (unsigned short *)malloc(
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  
541 int
542 tmPush(tms)                     /* push tone mapping on top of stack */
543 register struct tmStruct        *tms;
544 {
545        static char     funcName[] = "tmPush";
546                                        /* check validity */
547        if (tms == NULL)
548                returnErr(TM_E_ILLEGAL);
549        if (tms == tmTop)               /* check necessity */
550                returnOK;
551                                        /* pull if already in stack */
552        (void)tmPull(tms);
553                                        /* push it on top */
554        tms->tmprev = tmTop;
555        tmTop = tms;
556        returnOK;
557 }
558
559
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;
568                                        /* take out of stack if present */
569        (void)tmPull(tms);
644                                          /* free tables */
645          if (tms->histo != NULL)
646                  free((MEM_PTR)tms->histo);
# Line 577 | Line 651 | register struct tmStruct       *tms;
651                  if (tms->pd[i] != NULL)
652                          (*tmPkg[i]->Free)(tms->pd[i]);
653          free((MEM_PTR)tms);             /* free basic structure */
654 + }
655 +
656 + /******************** Shared but Private library routines *********************/
657 +
658 + BYTE    tmMesofact[BMESUPPER-BMESLOWER];
659 +
660 + void
661 + tmMkMesofact()                          /* build mesopic lookup factor table */
662 + {
663 +        int     i;
664 +
665 +        if (tmMesofact[BMESUPPER-BMESLOWER-1])
666 +                return;
667 +
668 +        for (i = BMESLOWER; i < BMESUPPER; i++)
669 +                tmMesofact[i-BMESLOWER] = 256. *
670 +                                (tmLuminance(i) - LMESLOWER) /
671 +                                (LMESUPPER - LMESLOWER);
672 + }
673 +
674 +
675 + int
676 + tmNewMap(                       /* allocate new tone-mapping array */
677 + TMstruct        *tms
678 + )
679 + {
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);
712 +        fputs("!\n", stderr);
713 +        return(err);
714   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines