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.24 by greg, Wed May 10 15:34:17 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 = NULL;
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 >        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 COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
266 >        static BYTE     gamtab[1024];
267 >        static double   curgam = .0;
268          COLOR   cmon;
269 <        double  lum, slum;
270 <        register double d;
174 <        register int    i;
269 >        float   lum, slum, d;
270 >        int     i;
271  
272 <        if (tmTop == NULL)
272 >        if (tms == NULL)
273                  returnErr(TM_E_TMINVAL);
274 <        if (ls == NULL | scan == NULL | len <= 0)
274 >        if ((ls == NULL) | (scan == NULL) | (len < 0))
275                  returnErr(TM_E_ILLEGAL);
276 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
277 +                tmCvLums(NULL, NULL, 0);
278 +        if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
279 +                curgam = tms->mongam;                   /* (re)build table */
280 +                for (i = 1024; i--; )
281 +                        gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam));
282 +        }
283          for (i = len; i--; ) {
284 <                if (tmTop->flags & TM_F_NEEDMAT)        /* get monitor RGB */
285 <                        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;
284 >                if (tmNeedMatrix(tms)) {                /* get monitor RGB */
285 >                        colortrans(cmon, tms->cmat, scan[i]);
286                  } else {
287 <                        d = TM_BRTSCALE*log(lum);       /* encode it */
288 <                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
287 >                        cmon[RED] = tms->inpsf*scan[i][RED];
288 >                        cmon[GRN] = tms->inpsf*scan[i][GRN];
289 >                        cmon[BLU] = tms->inpsf*scan[i][BLU];
290                  }
291 + #ifdef isfinite
292 +                if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f;
293 +                if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f;
294 +                if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f;
295 + #else
296 +                if (cmon[RED] < .0f) cmon[RED] = .0f;
297 +                if (cmon[GRN] < .0f) cmon[GRN] = .0f;
298 +                if (cmon[BLU] < .0f) cmon[BLU] = .0f;
299 + #endif
300 +                                                        /* world luminance */
301 +                lum =   tms->clf[RED]*cmon[RED] +
302 +                        tms->clf[GRN]*cmon[GRN] +
303 +                        tms->clf[BLU]*cmon[BLU] ;
304 +                if (lum <= TM_NOLUM)                    /* convert brightness */
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)
313                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
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 : 256.*pow(d, 1./tmTop->mongam); */
331 <                cs[3*i  ] = d>.999 ? 255 : tmTop->gamb[(int)(d*TM_GAMTSZ)];
332 <                d = tmTop->clf[GRN]*cmon[GRN]/lum;
333 <                /* cs[3*i+1] = d>.999 ? 255 : 256.*pow(d, 1./tmTop->mongam); */
334 <                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)];
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)
250 <                returnErr(TM_E_ILLEGAL);
251 <        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 = (tms->hbrmin-MINBRT)/HISTEP;
368 >                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 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 = (tms->hbrmin-MINBRT)/HISTEP;
379 >        hlen = (tms->hbrmax-MINBRT)/HISTEP + 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((char *)tmTop->histo);
386 >                                newhist[--j] = tms->histo[--i];
387 >                        free((MEM_PTR)tms->histo);
388                  }
389 <                tmTop->histo = newhist;
285 <                if (tmTop->lumap != NULL) {     /* invalid tone map */
286 <                        free((char *)tmTop->lumap);
287 <                        tmTop->lumap = NULL;
288 <                }
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[ (ls[i]-MINBRT)/HISTEP - 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 318 | Line 420 | double La;
420   }
421  
422  
423 + static int
424 + tmNewMap(                       /* allocate new tone-mapping array */
425 + TMstruct        *tms
426 + )
427 + {
428 +        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
429 +                                        (tms->hbrmax - tms->hbrmin)) {
430 +                free((MEM_PTR)tms->lumap);
431 +                tms->lumap = NULL;
432 +        }
433 +        tms->mbrmin = tms->hbrmin;
434 +        tms->mbrmax = tms->hbrmax;
435 +        if (tms->mbrmin > tms->mbrmax)
436 +                return 0;
437 +        if (tms->lumap == NULL)
438 +                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
439 +                                        (tms->mbrmax-tms->mbrmin+1));
440 +        return(tms->lumap != NULL);
441 + }
442 +
443 +
444   int
445 < tmComputeMapping(gamval, Lddyn, Ldmax)
446 < double  gamval;
447 < double  Lddyn;
448 < double  Ldmax;
445 > tmFixedMapping(                 /* compute fixed, linear tone-mapping */
446 > TMstruct        *tms,
447 > double  expmult,
448 > double  gamval
449 > )
450   {
451 <        static char     funcName[] = "tmComputeMapping";
451 >        static const char funcName[] = "tmFixedMapping";
452 >        double          d;
453 >        int     i;
454 >        
455 >        if (!tmNewMap(tms))
456 >                returnErr(TM_E_NOMEM);
457 >        if (expmult <= .0)
458 >                expmult = 1.;
459 >        if (gamval < MINGAM)
460 >                gamval = tms->mongam;
461 >        d = log(expmult/tms->inpsf);
462 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
463 >                tms->lumap[i] = 256. * exp(
464 >                        ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
465 >                        / gamval );
466 >        returnOK;
467 > }
468 >
469 >
470 > int
471 > tmComputeMapping(                       /* compute histogram tone-mapping */
472 > TMstruct        *tms,
473 > double  gamval,
474 > double  Lddyn,
475 > double  Ldmax
476 > )
477 > {
478 >        static const char funcName[] = "tmComputeMapping";
479          int     *histo;
480          float   *cumf;
481 <        int     brt0, histlen, histot, threshold, ceiling, trimmings;
481 >        int     brt0, histlen, threshold, ceiling, trimmings;
482          double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
483 <        int4    sum;
484 <        register double d;
485 <        register int    i, j;
483 >        int32   histot;
484 >        double  sum;
485 >        double  d;
486 >        int     i, j;
487  
488 <        if (tmTop == NULL || tmTop->histo == NULL)
488 >        if (tms == NULL || tms->histo == NULL)
489                  returnErr(TM_E_TMINVAL);
490                                          /* check arguments */
491          if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
492          if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
493 <        if (gamval < MINGAM) gamval = tmTop->mongam;
493 >        if (gamval < MINGAM) gamval = tms->mongam;
494                                          /* compute handy values */
495          Ldmin = Ldmax/Lddyn;
496          logLddyn = log(Lddyn);
497          Ldavg = sqrt(Ldmax*Ldmin);
498 <        i = (tmTop->brmin-MINBRT)/HISTEP;
498 >        i = (tms->hbrmin-MINBRT)/HISTEP;
499          brt0 = MINBRT + HISTEP/2 + i*HISTEP;
500 <        histlen = (tmTop->brmax-MINBRT)/HISTEP + 1 - i;
500 >        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
501                                          /* histogram total and mean */
502          histot = 0; sum = 0;
503          j = brt0 + histlen*HISTEP;
504          for (i = histlen; i--; ) {
505 <                histot += tmTop->histo[i];
506 <                sum += (j -= HISTEP) * tmTop->histo[i];
505 >                histot += tms->histo[i];
506 >                sum += (j -= HISTEP) * tms->histo[i];
507          }
508 <        threshold = histot*.025 + .5;
508 >        threshold = histot*0.005 + .5;
509          if (threshold < 4)
510                  returnErr(TM_E_TMFAIL);
511          Lwavg = tmLuminance( (double)sum / histot );
512 <        if (!(tmTop->flags & TM_F_LINEAR)) {    /* clamp histogram */
513 <                histo = (int *)malloc(histlen*sizeof(int));
514 <                cumf = (float *)malloc((histlen+1)*sizeof(float));
515 <                if (histo == NULL | cumf == NULL)
516 <                        returnErr(TM_E_NOMEM);
517 <                for (i = histlen; i--; )        /* make malleable copy */
518 <                        histo[i] = tmTop->histo[i];
519 <                do {                            /* iterate to solution */
520 <                        sum = 0;                /* cumulative probability */
521 <                        for (i = 0; i < histlen; i++) {
522 <                                cumf[i] = (double)sum/histot;
523 <                                sum += histo[i];
512 >                                        /* allocate space for mapping */
513 >        if (!tmNewMap(tms))
514 >                returnErr(TM_E_NOMEM);
515 >                                        /* use linear tone mapping? */
516 >        if (tms->flags & TM_F_LINEAR)
517 >                goto linearmap;
518 >                                        /* clamp histogram */
519 >        histo = (int *)malloc(histlen*sizeof(int));
520 >        cumf = (float *)malloc((histlen+2)*sizeof(float));
521 >        if ((histo == NULL) | (cumf == NULL))
522 >                returnErr(TM_E_NOMEM);
523 >        cumf[histlen+1] = 1.;           /* guard for assignment code */
524 >        for (i = histlen; i--; )        /* make malleable copy */
525 >                histo[i] = tms->histo[i];
526 >        do {                            /* iterate to solution */
527 >                sum = 0;                /* cumulative probability */
528 >                for (i = 0; i < histlen; i++) {
529 >                        cumf[i] = (double)sum/histot;
530 >                        sum += histo[i];
531 >                }
532 >                cumf[histlen] = 1.;
533 >                Tr = histot * (double)(tms->hbrmax - tms->hbrmin) /
534 >                        ((double)histlen*TM_BRTSCALE) / logLddyn;
535 >                ceiling = Tr + 1.;
536 >                trimmings = 0;          /* clip to envelope */
537 >                for (i = histlen; i--; ) {
538 >                        if (tms->flags & TM_F_HCONTR) {
539 >                                Lw = tmLuminance(brt0 + i*HISTEP);
540 >                                Ld = Ldmin * exp( logLddyn *
541 >                                        .5*(cumf[i]+cumf[i+1]) );
542 >                                ceiling = Tr * (htcontrs(Ld) * Lw) /
543 >                                        (htcontrs(Lw) * Ld) + 1.;
544                          }
545 <                        cumf[i] = 1.;
546 <                        Tr = histot * (double)(tmTop->brmax - tmTop->brmin) /
547 <                                ((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 <                                }
545 >                        if (histo[i] > ceiling) {
546 >                                trimmings += histo[i] - ceiling;
547 >                                histo[i] = ceiling;
548                          }
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);
549                  }
550 +                                        /* check if we're out of data */
551 +                if ((histot -= trimmings) <= threshold) {
552 +                        free((MEM_PTR)histo);
553 +                        free((MEM_PTR)cumf);
554 +                        goto linearmap;
555 +                }
556 +        } while (trimmings > threshold);
557 +                                        /* assign tone-mapping */
558 +        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
559 +                j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
560 +                d -= (double)j;
561 +                Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
562 +                d = (Ld - Ldmin)/(Ldmax - Ldmin);
563 +                tms->lumap[i] = 256.*pow(d, 1./gamval);
564          }
565 <        if (!(tmTop->flags & TM_F_LINEAR)) {
566 <                free((char *)histo);
424 <                free((char *)cumf);
425 <        }
565 >        free((MEM_PTR)histo);           /* clean up and return */
566 >        free((MEM_PTR)cumf);
567          returnOK;
568 + linearmap:                              /* linear tone-mapping */
569 +        if (tms->flags & TM_F_HCONTR)
570 +                d = htcontrs(Ldavg) / htcontrs(Lwavg);
571 +        else
572 +                d = Ldavg / Lwavg;
573 +        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
574   }
575  
576  
577   int
578 < tmMapPixels(ps, ls, cs, len)
579 < register BYTE   *ps;
580 < TMbright        *ls;
581 < register BYTE   *cs;
582 < int     len;
578 > tmMapPixels(                    /* apply tone-mapping to pixel(s) */
579 > TMstruct        *tms,
580 > BYTE    *ps,
581 > TMbright        *ls,
582 > BYTE    *cs,
583 > int     len
584 > )
585   {
586 <        static char     funcName[] = "tmMapPixels";
587 <        int     rdiv, gdiv, bdiv;
439 <        register int    li, pv;
586 >        static const char funcName[] = "tmMapPixels";
587 >        int32   li, pv;
588  
589 <        if (tmTop == NULL || tmTop->lumap == NULL)
589 >        if (tms == NULL || tms->lumap == NULL)
590                  returnErr(TM_E_TMINVAL);
591 <        if (ps == NULL | ls == NULL | len <= 0)
591 >        if ((ps == NULL) | (ls == NULL) | (len < 0))
592                  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];
593          while (len--) {
594 <                if ((li = *ls++) < tmTop->brmin)
595 <                        li = tmTop->brmin;
596 <                else if (li > tmTop->brmax)
597 <                        li = tmTop->brmax;
598 <                li = tmTop->lumap[li - tmTop->brmin];
594 >                if ((li = *ls++) < tms->mbrmin) {
595 >                        li = 0;
596 >                } else {
597 >                        if (li > tms->mbrmax)
598 >                                li = tms->mbrmax;
599 >                        li = tms->lumap[li - tms->mbrmin];
600 >                }
601                  if (cs == TM_NOCHROM)
602                          *ps++ = li>255 ? 255 : li;
603                  else {
604 <                        pv = *cs++ * li / rdiv;
604 >                        pv = *cs++ * li / tms->cdiv[RED];
605                          *ps++ = pv>255 ? 255 : pv;
606 <                        pv = *cs++ * li / gdiv;
606 >                        pv = *cs++ * li / tms->cdiv[GRN];
607                          *ps++ = pv>255 ? 255 : pv;
608 <                        pv = *cs++ * li / bdiv;
608 >                        pv = *cs++ * li / tms->cdiv[BLU];
609                          *ps++ = pv>255 ? 255 : pv;
610                  }
611          }
# Line 466 | Line 613 | int    len;
613   }
614  
615  
469 struct tmStruct *
470 tmPop()                         /* pop top tone mapping off stack */
471 {
472        register struct tmStruct        *tms;
616  
474        if ((tms = tmTop) != NULL)
475                tmTop = tms->tmprev;
476        return(tms);
477 }
617  
618 <
619 < int
620 < tmPull(tms)                     /* pull a tone mapping from stack */
621 < register struct tmStruct        *tms;
618 > TMstruct *
619 > tmDup(                          /* duplicate top tone mapping */
620 > TMstruct        *tms
621 > )
622   {
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 {
623          int     len;
624 <        register int    i;
625 <        register struct tmStruct        *tmnew;
624 >        int     i;
625 >        TMstruct        *tmnew;
626  
627 <        if (tmTop == NULL)              /* anything to duplicate? */
627 >        if (tms == NULL)                /* anything to duplicate? */
628                  return(NULL);
629 <        tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct));
629 >        tmnew = (TMstruct *)malloc(sizeof(TMstruct));
630          if (tmnew == NULL)
631                  return(NULL);
632 <        *tmnew = *tmTop;                /* copy everything */
632 >        *tmnew = *tms;          /* copy everything */
633          if (tmnew->histo != NULL) {     /* duplicate histogram */
634 <                len = (tmnew->brmax-MINBRT)/HISTEP + 1 -
635 <                                (tmnew->brmin-MINBRT)/HISTEP;
634 >                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
635 >                                (tmnew->hbrmin-MINBRT)/HISTEP;
636                  tmnew->histo = (int *)malloc(len*sizeof(int));
637                  if (tmnew->histo != NULL)
638                          for (i = len; i--; )
639 <                                tmnew->histo[i] = tmTop->histo[i];
639 >                                tmnew->histo[i] = tms->histo[i];
640          }
641          if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */
642 <                len = tmnew->brmax-tmnew->brmin+1;
642 >                len = tmnew->mbrmax-tmnew->mbrmin+1;
643                  tmnew->lumap = (unsigned short *)malloc(
644                                                  len*sizeof(unsigned short) );
645                  if (tmnew->lumap != NULL)
646                          for (i = len; i--; )
647 <                                tmnew->lumap[i] = tmTop->lumap[i];
647 >                                tmnew->lumap[i] = tms->lumap[i];
648          }
649 <        tmnew->tmprev = tmTop;          /* make copy current */
650 <        return(tmTop = tmnew);
649 >                                        /* clear package data */
650 >        for (i = tmNumPkgs; i--; )
651 >                tmnew->pd[i] = NULL;
652 >                                        /* return copy */
653 >        return(tmnew);
654   }
655  
656  
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
657   void
658   tmDone(tms)                     /* done with tone mapping -- destroy it */
659 < register struct tmStruct        *tms;
659 > TMstruct        *tms;
660   {
661 <                                        /* NULL arg. is equiv. to tmTop */
662 <        if (tms == NULL && (tms = tmTop) == NULL)
661 >        int     i;
662 >                                        /* NULL arg. is equiv. to tms */
663 >        if (tms == NULL)
664                  return;
563                                        /* take out of stack if present */
564        (void)tmPull(tms);
665                                          /* free tables */
666          if (tms->histo != NULL)
667 <                free((char *)tms->histo);
667 >                free((MEM_PTR)tms->histo);
668          if (tms->lumap != NULL)
669 <                free((char *)tms->lumap);
670 <        tms->flags = 0;
671 <        free((char *)tms);              /* free basic structure */
669 >                free((MEM_PTR)tms->lumap);
670 >                                        /* free private data */
671 >        for (i = tmNumPkgs; i--; )
672 >                if (tms->pd[i] != NULL)
673 >                        (*tmPkg[i]->Free)(tms->pd[i]);
674 >        free((MEM_PTR)tms);             /* free basic structure */
675 > }
676 >
677 > /******************** Shared but Private library routines *********************/
678 >
679 > BYTE    tmMesofact[BMESUPPER-BMESLOWER];
680 >
681 > void
682 > tmMkMesofact()                          /* build mesopic lookup factor table */
683 > {
684 >        int     i;
685 >
686 >        if (tmMesofact[BMESUPPER-BMESLOWER-1])
687 >                return;
688 >
689 >        for (i = BMESLOWER; i < BMESUPPER; i++)
690 >                tmMesofact[i-BMESLOWER] = 256. *
691 >                                (tmLuminance(i) - LMESLOWER) /
692 >                                (LMESUPPER - LMESLOWER);
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