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.9 by greg, Sat Feb 22 02:07:22 2003 UTC vs.
Revision 3.33 by greg, Mon Feb 9 20:23:51 2009 UTC

# Line 9 | Line 9 | static const char      RCSid[] = "$Id$";
9   * Externals declared in tonemap.h
10   */
11  
12 < /* ====================================================================
13 < * The Radiance Software License, Version 1.0
14 < *
15 < * Copyright (c) 1990 - 2002 The Regents of the University of California,
16 < * through Lawrence Berkeley National Laboratory.   All rights reserved.
17 < *
18 < * Redistribution and use in source and binary forms, with or without
19 < * modification, are permitted provided that the following conditions
20 < * are met:
21 < *
22 < * 1. Redistributions of source code must retain the above copyright
23 < *         notice, this list of conditions and the following disclaimer.
24 < *
25 < * 2. Redistributions in binary form must reproduce the above copyright
26 < *       notice, this list of conditions and the following disclaimer in
27 < *       the documentation and/or other materials provided with the
28 < *       distribution.
29 < *
30 < * 3. The end-user documentation included with the redistribution,
31 < *           if any, must include the following acknowledgment:
32 < *             "This product includes Radiance software
33 < *                 (http://radsite.lbl.gov/)
34 < *                 developed by the Lawrence Berkeley National Laboratory
35 < *               (http://www.lbl.gov/)."
36 < *       Alternately, this acknowledgment may appear in the software itself,
37 < *       if and wherever such third-party acknowledgments normally appear.
38 < *
39 < * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
40 < *       and "The Regents of the University of California" must
41 < *       not be used to endorse or promote products derived from this
42 < *       software without prior written permission. For written
43 < *       permission, please contact [email protected].
44 < *
45 < * 5. Products derived from this software may not be called "Radiance",
46 < *       nor may "Radiance" appear in their name, without prior written
47 < *       permission of Lawrence Berkeley National Laboratory.
48 < *
49 < * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
50 < * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 < * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 < * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
53 < * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54 < * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55 < * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
56 < * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57 < * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58 < * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
59 < * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 < * SUCH DAMAGE.
61 < * ====================================================================
62 < *
63 < * This software consists of voluntary contributions made by many
64 < * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
65 < * information on Lawrence Berkeley National Laboratory, please see
66 < * <http://www.lbl.gov/>.
67 < */
12 > #include "copyright.h"
13  
14   #include        <stdio.h>
15   #include        <math.h>
# Line 73 | Line 18 | static const char      RCSid[] = "$Id$";
18  
19   #define exp10(x)        exp(M_LN10*(x))
20  
76 struct tmStruct *tmTop = NULL;          /* current tone mapping stack */
77
21                                          /* our list of conversion packages */
22   struct tmPackage        *tmPkg[TM_MAXPKG];
23   int     tmNumPkgs = 0;                  /* number of registered packages */
24  
25 < int     tmLastError;                    /* last error incurred by library */
26 < char    *tmLastFunction;                /* error-generating function name */
25 >                                        /* luminance->brightness lookup */
26 > static TMbright         *tmFloat2BrtLUT = NULL;
27  
28 + #define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15]
29  
30 < struct tmStruct *
31 < tmInit(flags, monpri, gamval)           /* initialize new tone mapping */
32 < int     flags;
33 < RGBPRIMP        monpri;
34 < double  gamval;
30 >
31 > TMstruct *
32 > tmInit(                                 /* initialize new tone mapping */
33 > int     flags,
34 > RGBPRIMP        monpri,
35 > double  gamval
36 > )
37   {
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 105 | Line 53 | double gamval;
53                  tmnew->clf[GRN] = rgb2xyzmat[1][1];
54                  tmnew->clf[BLU] = rgb2xyzmat[1][2];
55          } else {
56 <                comprgb2xyzWBmat(cmat, tmnew->monpri=monpri);
56 >                comprgb2xyzmat(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];
# Line 125 | Line 73 | double gamval;
73                          tmnew->inpsf = WHTEFFICACY;
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->inpdat = NULL;
77          tmnew->hbrmin = 10; tmnew->hbrmax = -10;
78          tmnew->histo = NULL;
79          tmnew->mbrmin = 10; tmnew->mbrmax = -10;
# Line 132 | Line 81 | double gamval;
81                                                  /* zero private data */
82          for (i = TM_MAXPKG; i--; )
83                  tmnew->pd[i] = NULL;
84 <                                                /* make tmnew current */
85 <        tmnew->tmprev = tmTop;
86 <        return(tmTop = tmnew);
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;
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.;
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 <                        comprgb2xyzWBmat(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 >                        comprgb2xyzmat(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 <                compxyz2rgbWBmat(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 <                comprgb2rgbWBmat(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;
140 >                        tms->cmat[i][j] *= tms->inpsf;
141                                                  /* set color divisors */
142          for (i = 0; i < 3; i++)
143 <                if (tmTop->clf[i] > .001)
144 <                        tmTop->cdiv[i] =
145 <                                256.*pow(tmTop->clf[i], 1./tmTop->mongam);
143 >                if (tms->clf[i] > .001)
144 >                        tms->cdiv[i] =
145 >                                256.*pow(tms->clf[i], 1./tms->mongam);
146                  else
147 <                        tmTop->cdiv[i] = 1;
147 >                        tms->cdiv[i] = 1;
148                                                  /* notify packages */
149          for (i = tmNumPkgs; i--; )
150 <                if (tmTop->pd[i] != NULL && tmPkg[i]->NewSpace != NULL)
151 <                        (*tmPkg[i]->NewSpace)(tmTop);
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((MEM_PTR)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";
196 <        static COLOR    csmall = {1e-6, 1e-6, 1e-6};
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 >        int     i;
236 >
237 >        if (tms == NULL)
238 >                returnErr(TM_E_TMINVAL);
239 >        if ((ls == NULL) | (scan == NULL) | (len < 0))
240 >                returnErr(TM_E_ILLEGAL);
241 >        if (tmFloat2BrtLUT == NULL)                     /* initialize */
242 >                tmCvLums(NULL, NULL, 0);
243 >        for (i = len; i--; ) {
244 >                float   lum = tms->inpsf * scan[i];
245 >                if (lum <= TM_NOLUM)
246 >                        ls[i] = TM_NOBRT;
247 >                else
248 >                        ls[i] = tmCvLumLUfp(&lum);
249 >        }
250 >        returnOK;
251 > }
252 >
253 >
254 > int
255 > tmCvColors(                             /* convert float colors */
256 > TMstruct        *tms,
257 > TMbright        *ls,
258 > BYTE    *cs,
259 > COLOR   *scan,
260 > int     len
261 > )
262 > {
263 >        static const char funcName[] = "tmCvColors";
264 >        static BYTE     gamtab[1024];
265 >        static double   curgam = .0;
266          COLOR   cmon;
267 <        double  lum, slum;
268 <        register double d;
224 <        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 +        if (tmFloat2BrtLUT == NULL)                     /* initialize */
275 +                tmCvLums(NULL, NULL, 0);
276 +        if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
277 +                curgam = tms->mongam;                   /* (re)build table */
278 +                for (i = 1024; i--; )
279 +                        gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam));
280 +        }
281          for (i = len; i--; ) {
282 <                if (tmNeedMatrix(tmTop))                /* get monitor RGB */
283 <                        colortrans(cmon, tmTop->cmat, scan[i]);
233 <                else {
234 <                        cmon[RED] = tmTop->inpsf*scan[i][RED];
235 <                        cmon[GRN] = tmTop->inpsf*scan[i][GRN];
236 <                        cmon[BLU] = tmTop->inpsf*scan[i][BLU];
237 <                }
238 <                                                        /* world luminance */
239 <                lum =   tmTop->clf[RED]*cmon[RED] +
240 <                        tmTop->clf[GRN]*cmon[GRN] +
241 <                        tmTop->clf[BLU]*cmon[BLU] ;
242 <                                                        /* check range */
243 <                if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
244 <                        lum =   tmTop->clf[RED]*cmon[RED] +
245 <                                tmTop->clf[GRN]*cmon[GRN] +
246 <                                tmTop->clf[BLU]*cmon[BLU] ;
247 <                if (lum < MINLUM) {
248 <                        ls[i] = MINBRT-1;               /* bogus value */
249 <                        lum = MINLUM;
282 >                if (tmNeedMatrix(tms)) {                /* get monitor RGB */
283 >                        colortrans(cmon, tms->cmat, scan[i]);
284                  } else {
285 <                        d = TM_BRTSCALE*log(lum);       /* encode it */
286 <                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
285 >                        cmon[RED] = tms->inpsf*scan[i][RED];
286 >                        cmon[GRN] = tms->inpsf*scan[i][GRN];
287 >                        cmon[BLU] = tms->inpsf*scan[i][BLU];
288                  }
289 + #ifdef isfinite
290 +                if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f;
291 +                if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f;
292 +                if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f;
293 + #else
294 +                if (cmon[RED] < .0f) cmon[RED] = .0f;
295 +                if (cmon[GRN] < .0f) cmon[GRN] = .0f;
296 +                if (cmon[BLU] < .0f) cmon[BLU] = .0f;
297 + #endif
298 +                                                        /* world luminance */
299 +                lum =   tms->clf[RED]*cmon[RED] +
300 +                        tms->clf[GRN]*cmon[GRN] +
301 +                        tms->clf[BLU]*cmon[BLU] ;
302 +                if (lum <= TM_NOLUM) {                  /* convert brightness */
303 +                        lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM;
304 +                        ls[i] = TM_NOBRT;
305 +                } else
306 +                        ls[i] = tmCvLumLUfp(&lum);
307                  if (cs == TM_NOCHROM)                   /* no color? */
308                          continue;
309 <                if (tmTop->flags & TM_F_MESOPIC && lum < LMESUPPER) {
309 >                if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
310                          slum = scotlum(cmon);           /* mesopic adj. */
311 <                        if (lum < LMESLOWER)
311 >                        if (lum < LMESLOWER) {
312                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
313 <                        else {
313 >                        } else {
314                                  d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
315 <                                if (tmTop->flags & TM_F_BW)
315 >                                if (tms->flags & TM_F_BW)
316                                          cmon[RED] = cmon[GRN] =
317                                                          cmon[BLU] = d*lum;
318                                  else
319                                          scalecolor(cmon, d);
320 <                                d = (1.-d)*slum;
320 >                                d = (1.f-d)*slum;
321                                  cmon[RED] += d;
322                                  cmon[GRN] += d;
323                                  cmon[BLU] += d;
324                          }
325 <                } else if (tmTop->flags & TM_F_BW) {
325 >                } else if (tms->flags & TM_F_BW) {
326                          cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
327                  }
328 <                d = tmTop->clf[RED]*cmon[RED]/lum;
329 <                cs[3*i  ] = d>=.999 ? 255 :
330 <                                (int)(256.*pow(d, 1./tmTop->mongam));
331 <                d = tmTop->clf[GRN]*cmon[GRN]/lum;
332 <                cs[3*i+1] = d>=.999 ? 255 :
333 <                                (int)(256.*pow(d, 1./tmTop->mongam));
281 <                d = tmTop->clf[BLU]*cmon[BLU]/lum;
282 <                cs[3*i+2] = d>=.999 ? 255 :
283 <                                (int)(256.*pow(d, 1./tmTop->mongam));
328 >                d = tms->clf[RED]*cmon[RED]/lum;
329 >                cs[3*i  ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
330 >                d = tms->clf[GRN]*cmon[GRN]/lum;
331 >                cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
332 >                d = tms->clf[BLU]*cmon[BLU]/lum;
333 >                cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
334          }
335          returnOK;
336   }
337  
338  
339   int
340 < tmCvGrays(ls, scan, len)                /* convert float gray values */
341 < TMbright        *ls;
342 < float   *scan;
343 < int     len;
340 > tmAddHisto(                             /* add values to histogram */
341 > TMstruct        *tms,
342 > TMbright        *ls,
343 > int     len,
344 > int     wt
345 > )
346   {
347 <        static char     funcName[] = "tmCvGrays";
296 <        register double d;
297 <        register int    i;
298 <
299 <        if (tmTop == NULL)
300 <                returnErr(TM_E_TMINVAL);
301 <        if (ls == NULL | scan == NULL | len < 0)
302 <                returnErr(TM_E_ILLEGAL);
303 <        for (i = len; i--; )
304 <                if (scan[i] <= TM_NOLUM) {
305 <                        ls[i] = TM_NOBRT;               /* bogus value */
306 <                } else {
307 <                        d = TM_BRTSCALE*log(scan[i]);   /* encode it */
308 <                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
309 <                }
310 <        returnOK;
311 < }
312 <
313 <
314 < int
315 < tmAddHisto(ls, len, wt)                 /* add values to histogram */
316 < register TMbright       *ls;
317 < int     len;
318 < int     wt;
319 < {
320 <        static char     funcName[] = "tmAddHisto";
347 >        static const char funcName[] = "tmAddHisto";
348          int     oldorig=0, oldlen, horig, hlen;
349 <        register int    i, j;
349 >        int     i, j;
350  
351 <        if (tmTop == NULL)
351 >        if (tms == NULL)
352                  returnErr(TM_E_TMINVAL);
353          if (len < 0)
354                  returnErr(TM_E_ILLEGAL);
355          if (len == 0)
356                  returnOK;
357                                                  /* first, grow limits */
358 <        if (tmTop->histo == NULL) {
358 >        if (tms->histo == NULL) {
359                  for (i = len; i-- && ls[i] < MINBRT; )
360                          ;
361                  if (i < 0)
362                          returnOK;
363 <                tmTop->hbrmin = tmTop->hbrmax = ls[i];
363 >                tms->hbrmin = tms->hbrmax = ls[i];
364                  oldlen = 0;
365          } else {
366 <                oldorig = (tmTop->hbrmin-MINBRT)/HISTEP;
367 <                oldlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
366 >                oldorig = HISTI(tms->hbrmin);
367 >                oldlen = HISTI(tms->hbrmax) + 1 - oldorig;
368          }
369          for (i = len; i--; ) {
370                  if ((j = ls[i]) < MINBRT)
371                          continue;
372 <                if (j < tmTop->hbrmin)
373 <                        tmTop->hbrmin = j;
374 <                else if (j > tmTop->hbrmax)
375 <                        tmTop->hbrmax = j;
372 >                if (j < tms->hbrmin)
373 >                        tms->hbrmin = j;
374 >                else if (j > tms->hbrmax)
375 >                        tms->hbrmax = j;
376          }
377 <        horig = (tmTop->hbrmin-MINBRT)/HISTEP;
378 <        hlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - horig;
377 >        horig = HISTI(tms->hbrmin);
378 >        hlen = HISTI(tms->hbrmax) + 1 - horig;
379          if (hlen > oldlen) {                    /* (re)allocate histogram */
380 <                register int    *newhist = (int *)calloc(hlen, sizeof(int));
380 >                int     *newhist = (int *)calloc(hlen, sizeof(int));
381                  if (newhist == NULL)
382                          returnErr(TM_E_NOMEM);
383                  if (oldlen) {                   /* copy and free old */
384                          for (i = oldlen, j = i+oldorig-horig; i; )
385 <                                newhist[--j] = tmTop->histo[--i];
386 <                        free((MEM_PTR)tmTop->histo);
385 >                                newhist[--j] = tms->histo[--i];
386 >                        free((MEM_PTR)tms->histo);
387                  }
388 <                tmTop->histo = newhist;
388 >                tms->histo = newhist;
389          }
390          if (wt == 0)
391                  returnOK;
392          for (i = len; i--; )                    /* add in new counts */
393                  if (ls[i] >= MINBRT)
394 <                        tmTop->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
394 >                        tms->histo[ HISTI(ls[i]) - horig ] += wt;
395          returnOK;
396   }
397  
398  
399   static double
400 < htcontrs(La)            /* human threshold contrast sensitivity, dL(La) */
401 < double  La;
400 > htcontrs(               /* human threshold contrast sensitivity, dL(La) */
401 > double  La
402 > )
403   {
404          double  l10La, l10dL;
405                                  /* formula taken from Ferwerda et al. [SG96] */
# Line 391 | Line 419 | double La;
419   }
420  
421  
394 static int
395 tmNewMap()
396 {
397        if (tmTop->lumap != NULL && (tmTop->mbrmax - tmTop->mbrmin) !=
398                                        (tmTop->hbrmax - tmTop->hbrmin)) {
399                free((MEM_PTR)tmTop->lumap);
400                tmTop->lumap = NULL;
401        }
402        tmTop->mbrmin = tmTop->hbrmin;
403        tmTop->mbrmax = tmTop->hbrmax;
404        if (tmTop->mbrmin > tmTop->mbrmax)
405                return 0;
406        if (tmTop->lumap == NULL)
407                tmTop->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
408                                        (tmTop->mbrmax-tmTop->mbrmin+1));
409        return(tmTop->lumap != NULL);
410 }
411
412
422   int
423 < tmFixedMapping(expmult, gamval)
424 < double  expmult;
425 < double  gamval;
423 > tmFixedMapping(                 /* compute fixed, linear tone-mapping */
424 > TMstruct        *tms,
425 > double  expmult,
426 > double  gamval
427 > )
428   {
429 <        static char     funcName[] = "tmFixedMapping";
429 >        static const char funcName[] = "tmFixedMapping";
430          double          d;
431 <        register int    i;
431 >        int     i;
432          
433 <        if (!tmNewMap())
433 >        if (!tmNewMap(tms))
434                  returnErr(TM_E_NOMEM);
435          if (expmult <= .0)
436                  expmult = 1.;
437          if (gamval < MINGAM)
438 <                gamval = tmTop->mongam;
439 <        d = log(expmult/tmTop->inpsf);
440 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; )
441 <                tmTop->lumap[i] = 256. * exp(
442 <                        ( d + (tmTop->mbrmin+i)*(1./TM_BRTSCALE) )
443 <                        / gamval );
438 >                gamval = tms->mongam;
439 >        d = log(expmult/tms->inpsf);
440 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
441 >                double  val = 256. * exp(
442 >                        ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
443 >                        / gamval);
444 >                tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val;
445 >        }
446          returnOK;
447   }
448  
449  
450   int
451 < tmComputeMapping(gamval, Lddyn, Ldmax)
452 < double  gamval;
453 < double  Lddyn;
454 < double  Ldmax;
451 > tmComputeMapping(                       /* compute histogram tone-mapping */
452 > TMstruct        *tms,
453 > double  gamval,
454 > double  Lddyn,
455 > double  Ldmax
456 > )
457   {
458 <        static char     funcName[] = "tmComputeMapping";
458 >        static const char funcName[] = "tmComputeMapping";
459          int     *histo;
460          float   *cumf;
461          int     brt0, histlen, threshold, ceiling, trimmings;
462          double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
463 <        int4    histot;
463 >        int32   histot;
464          double  sum;
465 <        register double d;
466 <        register int    i, j;
465 >        double  d;
466 >        int     i, j;
467  
468 <        if (tmTop == NULL || tmTop->histo == NULL)
468 >        if (tms == NULL || tms->histo == NULL)
469                  returnErr(TM_E_TMINVAL);
470                                          /* check arguments */
471          if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
472          if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
473 <        if (gamval < MINGAM) gamval = tmTop->mongam;
473 >        if (gamval < MINGAM) gamval = tms->mongam;
474                                          /* compute handy values */
475          Ldmin = Ldmax/Lddyn;
476          logLddyn = log(Lddyn);
477          Ldavg = sqrt(Ldmax*Ldmin);
478 <        i = (tmTop->hbrmin-MINBRT)/HISTEP;
479 <        brt0 = MINBRT + HISTEP/2 + i*HISTEP;
480 <        histlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - i;
478 >        i = HISTI(tms->hbrmin);
479 >        brt0 = HISTV(i);
480 >        histlen = HISTI(tms->hbrmax) + 1 - i;
481                                          /* histogram total and mean */
482          histot = 0; sum = 0;
483          j = brt0 + histlen*HISTEP;
484          for (i = histlen; i--; ) {
485 <                histot += tmTop->histo[i];
486 <                sum += (j -= HISTEP) * tmTop->histo[i];
485 >                histot += tms->histo[i];
486 >                sum += (double)(j -= HISTEP) * tms->histo[i];
487          }
488 <        threshold = histot*.025 + .5;
489 <        if (threshold < 4)
488 >        threshold = histot*0.005 + .5;
489 >        if (!histot)
490                  returnErr(TM_E_TMFAIL);
491          Lwavg = tmLuminance( (double)sum / histot );
477                                        /* allocate space for mapping */
478        if (!tmNewMap())
479                returnErr(TM_E_NOMEM);
492                                          /* use linear tone mapping? */
493 <        if (tmTop->flags & TM_F_LINEAR)
493 >        if (tms->flags & TM_F_LINEAR || threshold < 4 ||
494 >                        tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn)
495                  goto linearmap;
496                                          /* clamp histogram */
497          histo = (int *)malloc(histlen*sizeof(int));
498          cumf = (float *)malloc((histlen+2)*sizeof(float));
499 <        if (histo == NULL | cumf == NULL)
499 >        if ((histo == NULL) | (cumf == NULL))
500                  returnErr(TM_E_NOMEM);
501          cumf[histlen+1] = 1.;           /* guard for assignment code */
502          for (i = histlen; i--; )        /* make malleable copy */
503 <                histo[i] = tmTop->histo[i];
503 >                histo[i] = tms->histo[i];
504          do {                            /* iterate to solution */
505                  sum = 0;                /* cumulative probability */
506                  for (i = 0; i < histlen; i++) {
# Line 495 | Line 508 | double Ldmax;
508                          sum += histo[i];
509                  }
510                  cumf[histlen] = 1.;
511 <                Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) /
511 >                Tr = histot * (double)(tms->hbrmax - tms->hbrmin) /
512                          ((double)histlen*TM_BRTSCALE) / logLddyn;
513                  ceiling = Tr + 1.;
514                  trimmings = 0;          /* clip to envelope */
515                  for (i = histlen; i--; ) {
516 <                        if (tmTop->flags & TM_F_HCONTR) {
516 >                        if (tms->flags & TM_F_HCONTR) {
517                                  Lw = tmLuminance(brt0 + i*HISTEP);
518                                  Ld = Ldmin * exp( logLddyn *
519                                          .5*(cumf[i]+cumf[i+1]) );
# Line 519 | Line 532 | double Ldmax;
532                          goto linearmap;
533                  }
534          } while (trimmings > threshold);
535 +                                        /* allocate space for mapping */
536 +        if (!tmNewMap(tms))
537 +                returnErr(TM_E_NOMEM);
538                                          /* assign tone-mapping */
539 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) {
540 <                j = d = (double)i/(tmTop->mbrmax-tmTop->mbrmin)*histlen;
539 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
540 >                j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
541                  d -= (double)j;
542                  Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
543                  d = (Ld - Ldmin)/(Ldmax - Ldmin);
544 <                tmTop->lumap[i] = 256.*pow(d, 1./gamval);
544 >                tms->lumap[i] = 256.*pow(d, 1./gamval);
545          }
546          free((MEM_PTR)histo);           /* clean up and return */
547          free((MEM_PTR)cumf);
548          returnOK;
549   linearmap:                              /* linear tone-mapping */
550 <        if (tmTop->flags & TM_F_HCONTR)
550 >        if (tms->flags & TM_F_HCONTR)
551                  d = htcontrs(Ldavg) / htcontrs(Lwavg);
552          else
553                  d = Ldavg / Lwavg;
554 <        return(tmFixedMapping(tmTop->inpsf*d/Ldmax, gamval));
554 >        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
555   }
556  
557  
558   int
559 < tmMapPixels(ps, ls, cs, len)
560 < register BYTE   *ps;
561 < TMbright        *ls;
562 < register BYTE   *cs;
563 < int     len;
559 > tmMapPixels(                    /* apply tone-mapping to pixel(s) */
560 > TMstruct        *tms,
561 > BYTE    *ps,
562 > TMbright        *ls,
563 > BYTE    *cs,
564 > int     len
565 > )
566   {
567 <        static char     funcName[] = "tmMapPixels";
568 <        register int4   li, pv;
567 >        static const char funcName[] = "tmMapPixels";
568 >        int32   li, pv;
569  
570 <        if (tmTop == NULL || tmTop->lumap == NULL)
570 >        if (tms == NULL || tms->lumap == NULL)
571                  returnErr(TM_E_TMINVAL);
572 <        if (ps == NULL | ls == NULL | len < 0)
572 >        if ((ps == NULL) | (ls == NULL) | (len < 0))
573                  returnErr(TM_E_ILLEGAL);
574          while (len--) {
575 <                if ((li = *ls++) < tmTop->mbrmin) {
575 >                if ((li = *ls++) < tms->mbrmin) {
576                          li = 0;
577                  } else {
578 <                        if (li > tmTop->mbrmax)
579 <                                li = tmTop->mbrmax;
580 <                        li = tmTop->lumap[li - tmTop->mbrmin];
578 >                        if (li > tms->mbrmax)
579 >                                li = tms->mbrmax;
580 >                        li = tms->lumap[li - tms->mbrmin];
581                  }
582                  if (cs == TM_NOCHROM)
583                          *ps++ = li>255 ? 255 : li;
584                  else {
585 <                        pv = *cs++ * li / tmTop->cdiv[RED];
585 >                        pv = *cs++ * li / tms->cdiv[RED];
586                          *ps++ = pv>255 ? 255 : pv;
587 <                        pv = *cs++ * li / tmTop->cdiv[GRN];
587 >                        pv = *cs++ * li / tms->cdiv[GRN];
588                          *ps++ = pv>255 ? 255 : pv;
589 <                        pv = *cs++ * li / tmTop->cdiv[BLU];
589 >                        pv = *cs++ * li / tms->cdiv[BLU];
590                          *ps++ = pv>255 ? 255 : pv;
591                  }
592          }
# Line 576 | Line 594 | int    len;
594   }
595  
596  
597 < struct tmStruct *
598 < tmPop()                         /* pop top tone mapping off stack */
597 > TMstruct *
598 > tmDup(                          /* duplicate top tone mapping */
599 > TMstruct        *tms
600 > )
601   {
582        register struct tmStruct        *tms;
583
584        if ((tms = tmTop) != NULL)
585                tmTop = tms->tmprev;
586        return(tms);
587 }
588
589
590 int
591 tmPull(tms)                     /* pull a tone mapping from stack */
592 register struct tmStruct        *tms;
593 {
594        register struct tmStruct        *tms2;
595                                        /* special cases first */
596        if (tms == NULL | tmTop == NULL)
597                return(0);
598        if (tms == tmTop) {
599                tmTop = tms->tmprev;
600                tms->tmprev = NULL;
601                return(1);
602        }
603        for (tms2 = tmTop; tms2->tmprev != NULL; tms2 = tms2->tmprev)
604                if (tms == tms2->tmprev) {      /* remove it */
605                        tms2->tmprev = tms->tmprev;
606                        tms->tmprev = NULL;
607                        return(1);
608                }
609        return(0);                      /* not found on stack */
610 }
611
612
613 struct tmStruct *
614 tmDup()                         /* duplicate top tone mapping */
615 {
602          int     len;
603 <        register int    i;
604 <        register struct tmStruct        *tmnew;
603 >        int     i;
604 >        TMstruct        *tmnew;
605  
606 <        if (tmTop == NULL)              /* anything to duplicate? */
606 >        if (tms == NULL)                /* anything to duplicate? */
607                  return(NULL);
608 <        tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct));
608 >        tmnew = (TMstruct *)malloc(sizeof(TMstruct));
609          if (tmnew == NULL)
610                  return(NULL);
611 <        *tmnew = *tmTop;                /* copy everything */
611 >        *tmnew = *tms;          /* copy everything */
612          if (tmnew->histo != NULL) {     /* duplicate histogram */
613 <                len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
628 <                                (tmnew->hbrmin-MINBRT)/HISTEP;
613 >                len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin);
614                  tmnew->histo = (int *)malloc(len*sizeof(int));
615                  if (tmnew->histo != NULL)
616                          for (i = len; i--; )
617 <                                tmnew->histo[i] = tmTop->histo[i];
617 >                                tmnew->histo[i] = tms->histo[i];
618          }
619          if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */
620                  len = tmnew->mbrmax-tmnew->mbrmin+1;
# Line 637 | Line 622 | tmDup()                                /* duplicate top tone mapping */
622                                                  len*sizeof(unsigned short) );
623                  if (tmnew->lumap != NULL)
624                          for (i = len; i--; )
625 <                                tmnew->lumap[i] = tmTop->lumap[i];
625 >                                tmnew->lumap[i] = tms->lumap[i];
626          }
627                                          /* clear package data */
628          for (i = tmNumPkgs; i--; )
629                  tmnew->pd[i] = NULL;
630 <        tmnew->tmprev = tmTop;          /* make copy current */
631 <        return(tmTop = tmnew);
630 >                                        /* return copy */
631 >        return(tmnew);
632   }
633  
634  
650 int
651 tmPush(tms)                     /* push tone mapping on top of stack */
652 register struct tmStruct        *tms;
653 {
654        static char     funcName[] = "tmPush";
655                                        /* check validity */
656        if (tms == NULL)
657                returnErr(TM_E_ILLEGAL);
658        if (tms == tmTop)               /* check necessity */
659                returnOK;
660                                        /* pull if already in stack */
661        (void)tmPull(tms);
662                                        /* push it on top */
663        tms->tmprev = tmTop;
664        tmTop = tms;
665        returnOK;
666 }
667
668
635   void
636   tmDone(tms)                     /* done with tone mapping -- destroy it */
637 < register struct tmStruct        *tms;
637 > TMstruct        *tms;
638   {
639 <        register int    i;
640 <                                        /* NULL arg. is equiv. to tmTop */
641 <        if (tms == NULL && (tms = tmTop) == NULL)
639 >        int     i;
640 >                                        /* NULL arg. is equiv. to tms */
641 >        if (tms == NULL)
642                  return;
677                                        /* take out of stack if present */
678        (void)tmPull(tms);
643                                          /* free tables */
644          if (tms->histo != NULL)
645                  free((MEM_PTR)tms->histo);
# Line 695 | Line 659 | BYTE   tmMesofact[BMESUPPER-BMESLOWER];
659   void
660   tmMkMesofact()                          /* build mesopic lookup factor table */
661   {
662 <        register int    i;
662 >        int     i;
663  
664          if (tmMesofact[BMESUPPER-BMESLOWER-1])
665                  return;
# Line 708 | Line 672 | tmMkMesofact()                         /* build mesopic lookup factor table
672  
673  
674   int
675 < tmErrorReturn(func, err)                /* error return (with message) */
676 < char    *func;
677 < int     err;
675 > tmNewMap(                       /* allocate new tone-mapping array */
676 > TMstruct        *tms
677 > )
678   {
679 <        tmLastFunction = func;
680 <        tmLastError = err;
681 <        if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR)
682 <                return(err);
679 >        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
680 >                                        (tms->hbrmax - tms->hbrmin)) {
681 >                free((MEM_PTR)tms->lumap);
682 >                tms->lumap = NULL;
683 >        }
684 >        tms->mbrmin = tms->hbrmin;
685 >        tms->mbrmax = tms->hbrmax;
686 >        if (tms->mbrmin > tms->mbrmax)
687 >                return 0;
688 >        if (tms->lumap == NULL)
689 >                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
690 >                                        (tms->mbrmax-tms->mbrmin+1));
691 >        return(tms->lumap != NULL);
692 > }
693 >
694 >
695 > int
696 > tmErrorReturn(                          /* error return (with message) */
697 > const char      *func,
698 > TMstruct        *tms,
699 > int     err
700 > )
701 > {
702 >        if (tms != NULL) {
703 >                tms->lastFunc = func;
704 >                tms->lastError = err;
705 >                if (tms->flags & TM_F_NOSTDERR)
706 >                        return(err);
707 >        }
708          fputs(func, stderr);
709          fputs(": ", stderr);
710          fputs(tmErrorMessage[err], stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines