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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines