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.22 by greg, Thu Mar 23 22:01:43 2006 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  
82 int     tmLastError;                    /* last error incurred by library */
83 char    *tmLastFunction;                /* error-generating function name */
25  
26 <
27 < struct tmStruct *
28 < tmInit(flags, monpri, gamval)           /* initialize new tone mapping */
29 < int     flags;
30 < RGBPRIMP        monpri;
31 < double  gamval;
26 > TMstruct *
27 > tmInit(                                 /* initialize new tone mapping */
28 > int     flags,
29 > RGBPRIMP        monpri,
30 > double  gamval
31 > )
32   {
33          COLORMAT        cmat;
34 <        register struct tmStruct        *tmnew;
35 <        register int    i;
34 >        TMstruct        *tmnew;
35 >        int     i;
36                                                  /* allocate structure */
37 <        tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct));
37 >        tmnew = (TMstruct *)malloc(sizeof(TMstruct));
38          if (tmnew == NULL)
39                  return(NULL);
40  
41          tmnew->flags = flags & ~TM_F_UNIMPL;
42 +        if (tmnew->flags & TM_F_BW)
43 +                tmnew->flags &= ~TM_F_MESOPIC;
44                                                  /* set monitor transform */
45          if (monpri == NULL || monpri == stdprims || tmnew->flags & TM_F_BW) {
46                  tmnew->monpri = stdprims;
# Line 125 | Line 68 | double gamval;
68                          tmnew->inpsf = WHTEFFICACY;
69          tmnew->cmat[0][1] = tmnew->cmat[0][2] = tmnew->cmat[1][0] =
70          tmnew->cmat[1][2] = tmnew->cmat[2][0] = tmnew->cmat[2][1] = 0.;
71 +        tmnew->inpdat = NULL;
72          tmnew->hbrmin = 10; tmnew->hbrmax = -10;
73          tmnew->histo = NULL;
74          tmnew->mbrmin = 10; tmnew->mbrmax = -10;
# Line 132 | Line 76 | double gamval;
76                                                  /* zero private data */
77          for (i = TM_MAXPKG; i--; )
78                  tmnew->pd[i] = NULL;
79 <                                                /* make tmnew current */
80 <        tmnew->tmprev = tmTop;
81 <        return(tmTop = tmnew);
79 >        tmnew->lastError = TM_E_OK;
80 >        tmnew->lastFunc = "NoErr";
81 >                                                /* return new TMstruct */
82 >        return(tmnew);
83   }
84  
85  
86   int
87 < tmSetSpace(pri, sf)             /* set input color space for conversions */
88 < RGBPRIMP        pri;
89 < double  sf;
87 > tmSetSpace(                     /* set input color space for conversions */
88 > TMstruct        *tms,
89 > RGBPRIMP        pri,
90 > double  sf,
91 > MEM_PTR dat
92 > )
93   {
94 <        static char     funcName[] = "tmSetSpace";
95 <        register int    i, j;
94 >        static const char funcName[] = "tmSetSpace";
95 >        int     i, j;
96                                                  /* error check */
97 <        if (tmTop == NULL)
97 >        if (tms == NULL)
98                  returnErr(TM_E_TMINVAL);
99          if (sf <= 1e-12)
100                  returnErr(TM_E_ILLEGAL);
101                                                  /* check if no change */
102 <        if (pri == tmTop->inppri && FEQ(sf, tmTop->inpsf))
102 >        if (pri == tms->inppri && FEQ(sf, tms->inpsf) && dat == tms->inpdat)
103                  returnOK;
104 <        tmTop->inppri = pri;                    /* let's set it */
105 <        tmTop->inpsf = sf;
104 >        tms->inppri = pri;                      /* let's set it */
105 >        tms->inpsf = sf;
106 >        tms->inpdat = dat;
107  
108 <        if (tmTop->flags & TM_F_BW) {           /* color doesn't matter */
109 <                tmTop->monpri = tmTop->inppri;          /* eliminate xform */
110 <                if (tmTop->inppri == TM_XYZPRIM) {
111 <                        tmTop->clf[CIEX] = tmTop->clf[CIEZ] = 0.;
112 <                        tmTop->clf[CIEY] = 1.;
108 >        if (tms->flags & TM_F_BW) {             /* color doesn't matter */
109 >                tms->monpri = tms->inppri;              /* eliminate xform */
110 >                if (tms->inppri == TM_XYZPRIM) {
111 >                        tms->clf[CIEX] = tms->clf[CIEZ] = 0.;
112 >                        tms->clf[CIEY] = 1.;
113                  } else {
114 <                        comprgb2xyzWBmat(tmTop->cmat, tmTop->monpri);
115 <                        tmTop->clf[RED] = tmTop->cmat[1][0];
116 <                        tmTop->clf[GRN] = tmTop->cmat[1][1];
117 <                        tmTop->clf[BLU] = tmTop->cmat[1][2];
114 >                        comprgb2xyzWBmat(tms->cmat, tms->monpri);
115 >                        tms->clf[RED] = tms->cmat[1][0];
116 >                        tms->clf[GRN] = tms->cmat[1][1];
117 >                        tms->clf[BLU] = tms->cmat[1][2];
118                  }
119 <                tmTop->cmat[0][0] = tmTop->cmat[1][1] = tmTop->cmat[2][2] =
120 <                                tmTop->inpsf;
121 <                tmTop->cmat[0][1] = tmTop->cmat[0][2] = tmTop->cmat[1][0] =
122 <                tmTop->cmat[1][2] = tmTop->cmat[2][0] = tmTop->cmat[2][1] = 0.;
119 >                tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] =
120 >                                tms->inpsf;
121 >                tms->cmat[0][1] = tms->cmat[0][2] = tms->cmat[1][0] =
122 >                tms->cmat[1][2] = tms->cmat[2][0] = tms->cmat[2][1] = 0.;
123  
124 <        } else if (tmTop->inppri == TM_XYZPRIM) /* input is XYZ */
125 <                compxyz2rgbWBmat(tmTop->cmat, tmTop->monpri);
124 >        } else if (tms->inppri == TM_XYZPRIM)   /* input is XYZ */
125 >                compxyz2rgbWBmat(tms->cmat, tms->monpri);
126  
127          else {                                  /* input is RGB */
128 <                if (tmTop->inppri != tmTop->monpri &&
129 <                                PRIMEQ(tmTop->inppri, tmTop->monpri))
130 <                        tmTop->inppri = tmTop->monpri;  /* no xform */
131 <                comprgb2rgbWBmat(tmTop->cmat, tmTop->inppri, tmTop->monpri);
128 >                if (tms->inppri != tms->monpri &&
129 >                                PRIMEQ(tms->inppri, tms->monpri))
130 >                        tms->inppri = tms->monpri;      /* no xform */
131 >                comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri);
132          }
133          for (i = 0; i < 3; i++)
134                  for (j = 0; j < 3; j++)
135 <                        tmTop->cmat[i][j] *= tmTop->inpsf;
135 >                        tms->cmat[i][j] *= tms->inpsf;
136                                                  /* set color divisors */
137          for (i = 0; i < 3; i++)
138 <                if (tmTop->clf[i] > .001)
139 <                        tmTop->cdiv[i] =
140 <                                256.*pow(tmTop->clf[i], 1./tmTop->mongam);
138 >                if (tms->clf[i] > .001)
139 >                        tms->cdiv[i] =
140 >                                256.*pow(tms->clf[i], 1./tms->mongam);
141                  else
142 <                        tmTop->cdiv[i] = 1;
142 >                        tms->cdiv[i] = 1;
143                                                  /* notify packages */
144          for (i = tmNumPkgs; i--; )
145 <                if (tmTop->pd[i] != NULL && tmPkg[i]->NewSpace != NULL)
146 <                        (*tmPkg[i]->NewSpace)(tmTop);
145 >                if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL)
146 >                        (*tmPkg[i]->NewSpace)(tms);
147          returnOK;
148   }
149  
150  
151   void
152 < tmClearHisto()                  /* clear current histogram */
152 > tmClearHisto(                   /* clear current histogram */
153 > TMstruct        *tms
154 > )
155   {
156 <        if (tmTop == NULL || tmTop->histo == NULL)
156 >        if (tms == NULL || tms->histo == NULL)
157                  return;
158 <        free((MEM_PTR)tmTop->histo);
159 <        tmTop->histo = NULL;
158 >        free((MEM_PTR)tms->histo);
159 >        tms->histo = NULL;
160   }
161  
162  
163   int
164 < tmCvColors(ls, cs, scan, len)           /* convert float colors */
165 < TMbright        *ls;
166 < BYTE    *cs;
167 < COLOR   *scan;
168 < int     len;
164 > tmCvColors(                             /* convert float colors */
165 > TMstruct        *tms,
166 > TMbright        *ls,
167 > BYTE    *cs,
168 > COLOR   *scan,
169 > int     len
170 > )
171   {
172 <        static char     funcName[] = "tmCvColors";
173 <        static COLOR    csmall = {1e-6, 1e-6, 1e-6};
172 >        static const char funcName[] = "tmCvColors";
173 >        static COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
174 >        static BYTE     gamtab[1024];
175 >        static double   curgam = .0;
176          COLOR   cmon;
177          double  lum, slum;
178 <        register double d;
179 <        register int    i;
178 >        double  d;
179 >        int     i;
180  
181 <        if (tmTop == NULL)
181 >        if (tms == NULL)
182                  returnErr(TM_E_TMINVAL);
183 <        if (ls == NULL | scan == NULL | len < 0)
183 >        if ((ls == NULL) | (scan == NULL) | (len < 0))
184                  returnErr(TM_E_ILLEGAL);
185 +        if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
186 +                curgam = tms->mongam;                   /* (re)build table */
187 +                for (i = 1024; i--; )
188 +                        gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam));
189 +        }
190          for (i = len; i--; ) {
191 <                if (tmNeedMatrix(tmTop))                /* get monitor RGB */
192 <                        colortrans(cmon, tmTop->cmat, scan[i]);
193 <                else {
194 <                        cmon[RED] = tmTop->inpsf*scan[i][RED];
195 <                        cmon[GRN] = tmTop->inpsf*scan[i][GRN];
196 <                        cmon[BLU] = tmTop->inpsf*scan[i][BLU];
191 >                if (tmNeedMatrix(tms)) {                /* get monitor RGB */
192 >                        colortrans(cmon, tms->cmat, scan[i]);
193 >                } else {
194 >                        cmon[RED] = tms->inpsf*scan[i][RED];
195 >                        cmon[GRN] = tms->inpsf*scan[i][GRN];
196 >                        cmon[BLU] = tms->inpsf*scan[i][BLU];
197                  }
198 + #ifdef isnan
199 +                if (isnan(cmon[RED]) || isinf(cmon[RED])) cmon[RED] = .0f;
200 +                if (isnan(cmon[GRN]) || isinf(cmon[GRN])) cmon[GRN] = .0f;
201 +                if (isnan(cmon[BLU]) || isinf(cmon[BLU])) cmon[BLU] = .0f;
202 + #endif
203                                                          /* world luminance */
204 <                lum =   tmTop->clf[RED]*cmon[RED] +
205 <                        tmTop->clf[GRN]*cmon[GRN] +
206 <                        tmTop->clf[BLU]*cmon[BLU] ;
204 >                lum =   tms->clf[RED]*cmon[RED] +
205 >                        tms->clf[GRN]*cmon[GRN] +
206 >                        tms->clf[BLU]*cmon[BLU] ;
207                                                          /* check range */
208                  if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
209 <                        lum =   tmTop->clf[RED]*cmon[RED] +
210 <                                tmTop->clf[GRN]*cmon[GRN] +
211 <                                tmTop->clf[BLU]*cmon[BLU] ;
209 >                        lum =   tms->clf[RED]*cmon[RED] +
210 >                                tms->clf[GRN]*cmon[GRN] +
211 >                                tms->clf[BLU]*cmon[BLU] ;
212                  if (lum < MINLUM) {
213                          ls[i] = MINBRT-1;               /* bogus value */
214                          lum = MINLUM;
# Line 253 | Line 218 | int    len;
218                  }
219                  if (cs == TM_NOCHROM)                   /* no color? */
220                          continue;
221 <                if (tmTop->flags & TM_F_MESOPIC && lum < LMESUPPER) {
221 >                if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
222                          slum = scotlum(cmon);           /* mesopic adj. */
223                          if (lum < LMESLOWER)
224                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
225                          else {
226                                  d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
227 <                                if (tmTop->flags & TM_F_BW)
227 >                                if (tms->flags & TM_F_BW)
228                                          cmon[RED] = cmon[GRN] =
229                                                          cmon[BLU] = d*lum;
230                                  else
# Line 269 | Line 234 | int    len;
234                                  cmon[GRN] += d;
235                                  cmon[BLU] += d;
236                          }
237 <                } else if (tmTop->flags & TM_F_BW) {
237 >                } else if (tms->flags & TM_F_BW) {
238                          cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
239                  }
240 <                d = tmTop->clf[RED]*cmon[RED]/lum;
241 <                cs[3*i  ] = d>=.999 ? 255 :
242 <                                (int)(256.*pow(d, 1./tmTop->mongam));
243 <                d = tmTop->clf[GRN]*cmon[GRN]/lum;
244 <                cs[3*i+1] = d>=.999 ? 255 :
245 <                                (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));
240 >                d = tms->clf[RED]*cmon[RED]/lum;
241 >                cs[3*i  ] = d>=.999 ? 255 : gamtab[(int)(1024.*d)];
242 >                d = tms->clf[GRN]*cmon[GRN]/lum;
243 >                cs[3*i+1] = d>=.999 ? 255 : gamtab[(int)(1024.*d)];
244 >                d = tms->clf[BLU]*cmon[BLU]/lum;
245 >                cs[3*i+2] = d>=.999 ? 255 : gamtab[(int)(1024.*d)];
246          }
247          returnOK;
248   }
249  
250  
251 + TMbright
252 + tmCvLuminance(                          /* convert a single luminance */
253 + double  lum
254 + )
255 + {
256 +        double  d;
257 +
258 +        if (lum <= TM_NOLUM)
259 +                return(TM_NOBRT);
260 +        d = TM_BRTSCALE*log(lum);
261 +        if (d > 0.)
262 +                return((TMbright)(d+.5));
263 +        return((TMbright)(d-.5));
264 + }
265 +
266 +
267   int
268 < tmCvGrays(ls, scan, len)                /* convert float gray values */
269 < TMbright        *ls;
270 < float   *scan;
271 < int     len;
268 > tmCvGrays(                              /* convert float gray values */
269 > TMstruct        *tms,
270 > TMbright        *ls,
271 > float   *scan,
272 > int     len
273 > )
274   {
275 <        static char     funcName[] = "tmCvGrays";
276 <        register double d;
277 <        register int    i;
275 >        static const char funcName[] = "tmCvGrays";
276 >        double  d;
277 >        int     i;
278  
279 <        if (tmTop == NULL)
279 >        if (tms == NULL)
280                  returnErr(TM_E_TMINVAL);
281 <        if (ls == NULL | scan == NULL | len < 0)
281 >        if ((ls == NULL) | (scan == NULL) | (len < 0))
282                  returnErr(TM_E_ILLEGAL);
283          for (i = len; i--; )
284                  if (scan[i] <= TM_NOLUM) {
# Line 312 | Line 292 | int    len;
292  
293  
294   int
295 < tmAddHisto(ls, len, wt)                 /* add values to histogram */
296 < register TMbright       *ls;
297 < int     len;
298 < int     wt;
295 > tmAddHisto(                             /* add values to histogram */
296 > TMstruct        *tms,
297 > TMbright        *ls,
298 > int     len,
299 > int     wt
300 > )
301   {
302 <        static char     funcName[] = "tmAddHisto";
302 >        static const char funcName[] = "tmAddHisto";
303          int     oldorig=0, oldlen, horig, hlen;
304 <        register int    i, j;
304 >        int     i, j;
305  
306 <        if (tmTop == NULL)
306 >        if (tms == NULL)
307                  returnErr(TM_E_TMINVAL);
308          if (len < 0)
309                  returnErr(TM_E_ILLEGAL);
310          if (len == 0)
311                  returnOK;
312                                                  /* first, grow limits */
313 <        if (tmTop->histo == NULL) {
313 >        if (tms->histo == NULL) {
314                  for (i = len; i-- && ls[i] < MINBRT; )
315                          ;
316                  if (i < 0)
317                          returnOK;
318 <                tmTop->hbrmin = tmTop->hbrmax = ls[i];
318 >                tms->hbrmin = tms->hbrmax = ls[i];
319                  oldlen = 0;
320          } else {
321 <                oldorig = (tmTop->hbrmin-MINBRT)/HISTEP;
322 <                oldlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
321 >                oldorig = (tms->hbrmin-MINBRT)/HISTEP;
322 >                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
323          }
324          for (i = len; i--; ) {
325                  if ((j = ls[i]) < MINBRT)
326                          continue;
327 <                if (j < tmTop->hbrmin)
328 <                        tmTop->hbrmin = j;
329 <                else if (j > tmTop->hbrmax)
330 <                        tmTop->hbrmax = j;
327 >                if (j < tms->hbrmin)
328 >                        tms->hbrmin = j;
329 >                else if (j > tms->hbrmax)
330 >                        tms->hbrmax = j;
331          }
332 <        horig = (tmTop->hbrmin-MINBRT)/HISTEP;
333 <        hlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - horig;
332 >        horig = (tms->hbrmin-MINBRT)/HISTEP;
333 >        hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
334          if (hlen > oldlen) {                    /* (re)allocate histogram */
335 <                register int    *newhist = (int *)calloc(hlen, sizeof(int));
335 >                int     *newhist = (int *)calloc(hlen, sizeof(int));
336                  if (newhist == NULL)
337                          returnErr(TM_E_NOMEM);
338                  if (oldlen) {                   /* copy and free old */
339                          for (i = oldlen, j = i+oldorig-horig; i; )
340 <                                newhist[--j] = tmTop->histo[--i];
341 <                        free((MEM_PTR)tmTop->histo);
340 >                                newhist[--j] = tms->histo[--i];
341 >                        free((MEM_PTR)tms->histo);
342                  }
343 <                tmTop->histo = newhist;
343 >                tms->histo = newhist;
344          }
345          if (wt == 0)
346                  returnOK;
347          for (i = len; i--; )                    /* add in new counts */
348                  if (ls[i] >= MINBRT)
349 <                        tmTop->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
349 >                        tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
350          returnOK;
351   }
352  
353  
354   static double
355 < htcontrs(La)            /* human threshold contrast sensitivity, dL(La) */
356 < double  La;
355 > htcontrs(               /* human threshold contrast sensitivity, dL(La) */
356 > double  La
357 > )
358   {
359          double  l10La, l10dL;
360                                  /* formula taken from Ferwerda et al. [SG96] */
# Line 392 | Line 375 | double La;
375  
376  
377   static int
378 < tmNewMap()
378 > tmNewMap(                       /* allocate new tone-mapping array */
379 > TMstruct        *tms
380 > )
381   {
382 <        if (tmTop->lumap != NULL && (tmTop->mbrmax - tmTop->mbrmin) !=
383 <                                        (tmTop->hbrmax - tmTop->hbrmin)) {
384 <                free((MEM_PTR)tmTop->lumap);
385 <                tmTop->lumap = NULL;
382 >        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
383 >                                        (tms->hbrmax - tms->hbrmin)) {
384 >                free((MEM_PTR)tms->lumap);
385 >                tms->lumap = NULL;
386          }
387 <        tmTop->mbrmin = tmTop->hbrmin;
388 <        tmTop->mbrmax = tmTop->hbrmax;
389 <        if (tmTop->mbrmin > tmTop->mbrmax)
387 >        tms->mbrmin = tms->hbrmin;
388 >        tms->mbrmax = tms->hbrmax;
389 >        if (tms->mbrmin > tms->mbrmax)
390                  return 0;
391 <        if (tmTop->lumap == NULL)
392 <                tmTop->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
393 <                                        (tmTop->mbrmax-tmTop->mbrmin+1));
394 <        return(tmTop->lumap != NULL);
391 >        if (tms->lumap == NULL)
392 >                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
393 >                                        (tms->mbrmax-tms->mbrmin+1));
394 >        return(tms->lumap != NULL);
395   }
396  
397  
398   int
399 < tmFixedMapping(expmult, gamval)
400 < double  expmult;
401 < double  gamval;
399 > tmFixedMapping(                 /* compute fixed, linear tone-mapping */
400 > TMstruct        *tms,
401 > double  expmult,
402 > double  gamval
403 > )
404   {
405 <        static char     funcName[] = "tmFixedMapping";
405 >        static const char funcName[] = "tmFixedMapping";
406          double          d;
407 <        register int    i;
407 >        int     i;
408          
409 <        if (!tmNewMap())
409 >        if (!tmNewMap(tms))
410                  returnErr(TM_E_NOMEM);
411          if (expmult <= .0)
412                  expmult = 1.;
413          if (gamval < MINGAM)
414 <                gamval = tmTop->mongam;
415 <        d = log(expmult/tmTop->inpsf);
416 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; )
417 <                tmTop->lumap[i] = 256. * exp(
418 <                        ( d + (tmTop->mbrmin+i)*(1./TM_BRTSCALE) )
414 >                gamval = tms->mongam;
415 >        d = log(expmult/tms->inpsf);
416 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
417 >                tms->lumap[i] = 256. * exp(
418 >                        ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
419                          / gamval );
420          returnOK;
421   }
422  
423  
424   int
425 < tmComputeMapping(gamval, Lddyn, Ldmax)
426 < double  gamval;
427 < double  Lddyn;
428 < double  Ldmax;
425 > tmComputeMapping(                       /* compute histogram tone-mapping */
426 > TMstruct        *tms,
427 > double  gamval,
428 > double  Lddyn,
429 > double  Ldmax
430 > )
431   {
432 <        static char     funcName[] = "tmComputeMapping";
432 >        static const char funcName[] = "tmComputeMapping";
433          int     *histo;
434          float   *cumf;
435          int     brt0, histlen, threshold, ceiling, trimmings;
436          double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
437 <        int4    histot;
437 >        int32   histot;
438          double  sum;
439 <        register double d;
440 <        register int    i, j;
439 >        double  d;
440 >        int     i, j;
441  
442 <        if (tmTop == NULL || tmTop->histo == NULL)
442 >        if (tms == NULL || tms->histo == NULL)
443                  returnErr(TM_E_TMINVAL);
444                                          /* check arguments */
445          if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
446          if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
447 <        if (gamval < MINGAM) gamval = tmTop->mongam;
447 >        if (gamval < MINGAM) gamval = tms->mongam;
448                                          /* compute handy values */
449          Ldmin = Ldmax/Lddyn;
450          logLddyn = log(Lddyn);
451          Ldavg = sqrt(Ldmax*Ldmin);
452 <        i = (tmTop->hbrmin-MINBRT)/HISTEP;
452 >        i = (tms->hbrmin-MINBRT)/HISTEP;
453          brt0 = MINBRT + HISTEP/2 + i*HISTEP;
454 <        histlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - i;
454 >        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
455                                          /* histogram total and mean */
456          histot = 0; sum = 0;
457          j = brt0 + histlen*HISTEP;
458          for (i = histlen; i--; ) {
459 <                histot += tmTop->histo[i];
460 <                sum += (j -= HISTEP) * tmTop->histo[i];
459 >                histot += tms->histo[i];
460 >                sum += (j -= HISTEP) * tms->histo[i];
461          }
462 <        threshold = histot*.025 + .5;
462 >        threshold = histot*0.005 + .5;
463          if (threshold < 4)
464                  returnErr(TM_E_TMFAIL);
465          Lwavg = tmLuminance( (double)sum / histot );
466                                          /* allocate space for mapping */
467 <        if (!tmNewMap())
467 >        if (!tmNewMap(tms))
468                  returnErr(TM_E_NOMEM);
469                                          /* use linear tone mapping? */
470 <        if (tmTop->flags & TM_F_LINEAR)
470 >        if (tms->flags & TM_F_LINEAR)
471                  goto linearmap;
472                                          /* clamp histogram */
473          histo = (int *)malloc(histlen*sizeof(int));
474          cumf = (float *)malloc((histlen+2)*sizeof(float));
475 <        if (histo == NULL | cumf == NULL)
475 >        if ((histo == NULL) | (cumf == NULL))
476                  returnErr(TM_E_NOMEM);
477          cumf[histlen+1] = 1.;           /* guard for assignment code */
478          for (i = histlen; i--; )        /* make malleable copy */
479 <                histo[i] = tmTop->histo[i];
479 >                histo[i] = tms->histo[i];
480          do {                            /* iterate to solution */
481                  sum = 0;                /* cumulative probability */
482                  for (i = 0; i < histlen; i++) {
# Line 495 | Line 484 | double Ldmax;
484                          sum += histo[i];
485                  }
486                  cumf[histlen] = 1.;
487 <                Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) /
487 >                Tr = histot * (double)(tms->hbrmax - tms->hbrmin) /
488                          ((double)histlen*TM_BRTSCALE) / logLddyn;
489                  ceiling = Tr + 1.;
490                  trimmings = 0;          /* clip to envelope */
491                  for (i = histlen; i--; ) {
492 <                        if (tmTop->flags & TM_F_HCONTR) {
492 >                        if (tms->flags & TM_F_HCONTR) {
493                                  Lw = tmLuminance(brt0 + i*HISTEP);
494                                  Ld = Ldmin * exp( logLddyn *
495                                          .5*(cumf[i]+cumf[i+1]) );
# Line 520 | Line 509 | double Ldmax;
509                  }
510          } while (trimmings > threshold);
511                                          /* assign tone-mapping */
512 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) {
513 <                j = d = (double)i/(tmTop->mbrmax-tmTop->mbrmin)*histlen;
512 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
513 >                j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
514                  d -= (double)j;
515                  Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
516                  d = (Ld - Ldmin)/(Ldmax - Ldmin);
517 <                tmTop->lumap[i] = 256.*pow(d, 1./gamval);
517 >                tms->lumap[i] = 256.*pow(d, 1./gamval);
518          }
519          free((MEM_PTR)histo);           /* clean up and return */
520          free((MEM_PTR)cumf);
521          returnOK;
522   linearmap:                              /* linear tone-mapping */
523 <        if (tmTop->flags & TM_F_HCONTR)
523 >        if (tms->flags & TM_F_HCONTR)
524                  d = htcontrs(Ldavg) / htcontrs(Lwavg);
525          else
526                  d = Ldavg / Lwavg;
527 <        return(tmFixedMapping(tmTop->inpsf*d/Ldmax, gamval));
527 >        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
528   }
529  
530  
531   int
532 < tmMapPixels(ps, ls, cs, len)
533 < register BYTE   *ps;
534 < TMbright        *ls;
535 < register BYTE   *cs;
536 < int     len;
532 > tmMapPixels(                    /* apply tone-mapping to pixel(s) */
533 > TMstruct        *tms,
534 > BYTE    *ps,
535 > TMbright        *ls,
536 > BYTE    *cs,
537 > int     len
538 > )
539   {
540 <        static char     funcName[] = "tmMapPixels";
541 <        register int4   li, pv;
540 >        static const char funcName[] = "tmMapPixels";
541 >        int32   li, pv;
542  
543 <        if (tmTop == NULL || tmTop->lumap == NULL)
543 >        if (tms == NULL || tms->lumap == NULL)
544                  returnErr(TM_E_TMINVAL);
545 <        if (ps == NULL | ls == NULL | len < 0)
545 >        if ((ps == NULL) | (ls == NULL) | (len < 0))
546                  returnErr(TM_E_ILLEGAL);
547          while (len--) {
548 <                if ((li = *ls++) < tmTop->mbrmin) {
548 >                if ((li = *ls++) < tms->mbrmin) {
549                          li = 0;
550                  } else {
551 <                        if (li > tmTop->mbrmax)
552 <                                li = tmTop->mbrmax;
553 <                        li = tmTop->lumap[li - tmTop->mbrmin];
551 >                        if (li > tms->mbrmax)
552 >                                li = tms->mbrmax;
553 >                        li = tms->lumap[li - tms->mbrmin];
554                  }
555                  if (cs == TM_NOCHROM)
556                          *ps++ = li>255 ? 255 : li;
557                  else {
558 <                        pv = *cs++ * li / tmTop->cdiv[RED];
558 >                        pv = *cs++ * li / tms->cdiv[RED];
559                          *ps++ = pv>255 ? 255 : pv;
560 <                        pv = *cs++ * li / tmTop->cdiv[GRN];
560 >                        pv = *cs++ * li / tms->cdiv[GRN];
561                          *ps++ = pv>255 ? 255 : pv;
562 <                        pv = *cs++ * li / tmTop->cdiv[BLU];
562 >                        pv = *cs++ * li / tms->cdiv[BLU];
563                          *ps++ = pv>255 ? 255 : pv;
564                  }
565          }
# Line 576 | Line 567 | int    len;
567   }
568  
569  
579 struct tmStruct *
580 tmPop()                         /* pop top tone mapping off stack */
581 {
582        register struct tmStruct        *tms;
570  
584        if ((tms = tmTop) != NULL)
585                tmTop = tms->tmprev;
586        return(tms);
587 }
571  
572 <
573 < int
574 < tmPull(tms)                     /* pull a tone mapping from stack */
575 < register struct tmStruct        *tms;
572 > TMstruct *
573 > tmDup(                          /* duplicate top tone mapping */
574 > TMstruct        *tms
575 > )
576   {
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 {
577          int     len;
578 <        register int    i;
579 <        register struct tmStruct        *tmnew;
578 >        int     i;
579 >        TMstruct        *tmnew;
580  
581 <        if (tmTop == NULL)              /* anything to duplicate? */
581 >        if (tms == NULL)                /* anything to duplicate? */
582                  return(NULL);
583 <        tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct));
583 >        tmnew = (TMstruct *)malloc(sizeof(TMstruct));
584          if (tmnew == NULL)
585                  return(NULL);
586 <        *tmnew = *tmTop;                /* copy everything */
586 >        *tmnew = *tms;          /* copy everything */
587          if (tmnew->histo != NULL) {     /* duplicate histogram */
588                  len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
589                                  (tmnew->hbrmin-MINBRT)/HISTEP;
590                  tmnew->histo = (int *)malloc(len*sizeof(int));
591                  if (tmnew->histo != NULL)
592                          for (i = len; i--; )
593 <                                tmnew->histo[i] = tmTop->histo[i];
593 >                                tmnew->histo[i] = tms->histo[i];
594          }
595          if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */
596                  len = tmnew->mbrmax-tmnew->mbrmin+1;
# Line 637 | Line 598 | tmDup()                                /* duplicate top tone mapping */
598                                                  len*sizeof(unsigned short) );
599                  if (tmnew->lumap != NULL)
600                          for (i = len; i--; )
601 <                                tmnew->lumap[i] = tmTop->lumap[i];
601 >                                tmnew->lumap[i] = tms->lumap[i];
602          }
603                                          /* clear package data */
604          for (i = tmNumPkgs; i--; )
605                  tmnew->pd[i] = NULL;
606 <        tmnew->tmprev = tmTop;          /* make copy current */
607 <        return(tmTop = tmnew);
606 >                                        /* return copy */
607 >        return(tmnew);
608   }
609  
610  
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
611   void
612   tmDone(tms)                     /* done with tone mapping -- destroy it */
613 < register struct tmStruct        *tms;
613 > TMstruct        *tms;
614   {
615 <        register int    i;
616 <                                        /* NULL arg. is equiv. to tmTop */
617 <        if (tms == NULL && (tms = tmTop) == NULL)
615 >        int     i;
616 >                                        /* NULL arg. is equiv. to tms */
617 >        if (tms == NULL)
618                  return;
677                                        /* take out of stack if present */
678        (void)tmPull(tms);
619                                          /* free tables */
620          if (tms->histo != NULL)
621                  free((MEM_PTR)tms->histo);
# Line 695 | Line 635 | BYTE   tmMesofact[BMESUPPER-BMESLOWER];
635   void
636   tmMkMesofact()                          /* build mesopic lookup factor table */
637   {
638 <        register int    i;
638 >        int     i;
639  
640          if (tmMesofact[BMESUPPER-BMESLOWER-1])
641                  return;
# Line 708 | Line 648 | tmMkMesofact()                         /* build mesopic lookup factor table
648  
649  
650   int
651 < tmErrorReturn(func, err)                /* error return (with message) */
652 < char    *func;
653 < int     err;
651 > tmErrorReturn(                          /* error return (with message) */
652 > const char      *func,
653 > TMstruct        *tms,
654 > int     err
655 > )
656   {
657 <        tmLastFunction = func;
658 <        tmLastError = err;
659 <        if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR)
660 <                return(err);
657 >        if (tms != NULL) {
658 >                tms->lastFunc = func;
659 >                tms->lastError = err;
660 >                if (tms->flags & TM_F_NOSTDERR)
661 >                        return(err);
662 >        }
663          fputs(func, stderr);
664          fputs(": ", stderr);
665          fputs(tmErrorMessage[err], stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines