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.19 by greg, Tue Nov 15 06:53:00 2005 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          COLOR   cmon;
175          double  lum, slum;
176 <        register double d;
177 <        register int    i;
176 >        double  d;
177 >        int     i;
178  
179 <        if (tmTop == NULL)
179 >        if (tms == NULL)
180                  returnErr(TM_E_TMINVAL);
181 <        if (ls == NULL | scan == NULL | len < 0)
181 >        if ((ls == NULL) | (scan == NULL) | (len < 0))
182                  returnErr(TM_E_ILLEGAL);
183          for (i = len; i--; ) {
184 <                if (tmNeedMatrix(tmTop))                /* get monitor RGB */
185 <                        colortrans(cmon, tmTop->cmat, scan[i]);
186 <                else {
187 <                        cmon[RED] = tmTop->inpsf*scan[i][RED];
188 <                        cmon[GRN] = tmTop->inpsf*scan[i][GRN];
189 <                        cmon[BLU] = tmTop->inpsf*scan[i][BLU];
184 >                if (tmNeedMatrix(tms)) {                /* get monitor RGB */
185 >                        colortrans(cmon, tms->cmat, scan[i]);
186 >                } else {
187 >                        cmon[RED] = tms->inpsf*scan[i][RED];
188 >                        cmon[GRN] = tms->inpsf*scan[i][GRN];
189 >                        cmon[BLU] = tms->inpsf*scan[i][BLU];
190                  }
191                                                          /* world luminance */
192 <                lum =   tmTop->clf[RED]*cmon[RED] +
193 <                        tmTop->clf[GRN]*cmon[GRN] +
194 <                        tmTop->clf[BLU]*cmon[BLU] ;
192 >                lum =   tms->clf[RED]*cmon[RED] +
193 >                        tms->clf[GRN]*cmon[GRN] +
194 >                        tms->clf[BLU]*cmon[BLU] ;
195                                                          /* check range */
196                  if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
197 <                        lum =   tmTop->clf[RED]*cmon[RED] +
198 <                                tmTop->clf[GRN]*cmon[GRN] +
199 <                                tmTop->clf[BLU]*cmon[BLU] ;
197 >                        lum =   tms->clf[RED]*cmon[RED] +
198 >                                tms->clf[GRN]*cmon[GRN] +
199 >                                tms->clf[BLU]*cmon[BLU] ;
200                  if (lum < MINLUM) {
201                          ls[i] = MINBRT-1;               /* bogus value */
202                          lum = MINLUM;
# Line 253 | Line 206 | int    len;
206                  }
207                  if (cs == TM_NOCHROM)                   /* no color? */
208                          continue;
209 <                if (tmTop->flags & TM_F_MESOPIC && lum < LMESUPPER) {
209 >                if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
210                          slum = scotlum(cmon);           /* mesopic adj. */
211                          if (lum < LMESLOWER)
212                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
213                          else {
214                                  d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
215 <                                if (tmTop->flags & TM_F_BW)
215 >                                if (tms->flags & TM_F_BW)
216                                          cmon[RED] = cmon[GRN] =
217                                                          cmon[BLU] = d*lum;
218                                  else
# Line 269 | Line 222 | int    len;
222                                  cmon[GRN] += d;
223                                  cmon[BLU] += d;
224                          }
225 <                } else if (tmTop->flags & TM_F_BW) {
225 >                } else if (tms->flags & TM_F_BW) {
226                          cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
227                  }
228 <                d = tmTop->clf[RED]*cmon[RED]/lum;
228 >                d = tms->clf[RED]*cmon[RED]/lum;
229                  cs[3*i  ] = d>=.999 ? 255 :
230 <                                (int)(256.*pow(d, 1./tmTop->mongam));
231 <                d = tmTop->clf[GRN]*cmon[GRN]/lum;
230 >                                (int)(256.*pow(d, 1./tms->mongam));
231 >                d = tms->clf[GRN]*cmon[GRN]/lum;
232                  cs[3*i+1] = d>=.999 ? 255 :
233 <                                (int)(256.*pow(d, 1./tmTop->mongam));
234 <                d = tmTop->clf[BLU]*cmon[BLU]/lum;
233 >                                (int)(256.*pow(d, 1./tms->mongam));
234 >                d = tms->clf[BLU]*cmon[BLU]/lum;
235                  cs[3*i+2] = d>=.999 ? 255 :
236 <                                (int)(256.*pow(d, 1./tmTop->mongam));
236 >                                (int)(256.*pow(d, 1./tms->mongam));
237          }
238          returnOK;
239   }
240  
241  
242 + TMbright
243 + tmCvLuminance(                          /* convert a single luminance */
244 + double  lum
245 + )
246 + {
247 +        double  d;
248 +
249 +        if (lum <= TM_NOLUM)
250 +                return(TM_NOBRT);
251 +        d = TM_BRTSCALE*log(lum);
252 +        if (d > 0.)
253 +                return((TMbright)(d+.5));
254 +        return((TMbright)(d-.5));
255 + }
256 +
257 +
258   int
259 < tmCvGrays(ls, scan, len)                /* convert float gray values */
260 < TMbright        *ls;
261 < float   *scan;
262 < int     len;
259 > tmCvGrays(                              /* convert float gray values */
260 > TMstruct        *tms,
261 > TMbright        *ls,
262 > float   *scan,
263 > int     len
264 > )
265   {
266 <        static char     funcName[] = "tmCvGrays";
267 <        register double d;
268 <        register int    i;
266 >        static const char funcName[] = "tmCvGrays";
267 >        double  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          for (i = len; i--; )
275                  if (scan[i] <= TM_NOLUM) {
# Line 312 | Line 283 | int    len;
283  
284  
285   int
286 < tmAddHisto(ls, len, wt)                 /* add values to histogram */
287 < register TMbright       *ls;
288 < int     len;
289 < int     wt;
286 > tmAddHisto(                             /* add values to histogram */
287 > TMstruct        *tms,
288 > TMbright        *ls,
289 > int     len,
290 > int     wt
291 > )
292   {
293 <        static char     funcName[] = "tmAddHisto";
293 >        static const char funcName[] = "tmAddHisto";
294          int     oldorig=0, oldlen, horig, hlen;
295 <        register int    i, j;
295 >        int     i, j;
296  
297 <        if (tmTop == NULL)
297 >        if (tms == NULL)
298                  returnErr(TM_E_TMINVAL);
299          if (len < 0)
300                  returnErr(TM_E_ILLEGAL);
301          if (len == 0)
302                  returnOK;
303                                                  /* first, grow limits */
304 <        if (tmTop->histo == NULL) {
304 >        if (tms->histo == NULL) {
305                  for (i = len; i-- && ls[i] < MINBRT; )
306                          ;
307                  if (i < 0)
308                          returnOK;
309 <                tmTop->hbrmin = tmTop->hbrmax = ls[i];
309 >                tms->hbrmin = tms->hbrmax = ls[i];
310                  oldlen = 0;
311          } else {
312 <                oldorig = (tmTop->hbrmin-MINBRT)/HISTEP;
313 <                oldlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
312 >                oldorig = (tms->hbrmin-MINBRT)/HISTEP;
313 >                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
314          }
315          for (i = len; i--; ) {
316                  if ((j = ls[i]) < MINBRT)
317                          continue;
318 <                if (j < tmTop->hbrmin)
319 <                        tmTop->hbrmin = j;
320 <                else if (j > tmTop->hbrmax)
321 <                        tmTop->hbrmax = j;
318 >                if (j < tms->hbrmin)
319 >                        tms->hbrmin = j;
320 >                else if (j > tms->hbrmax)
321 >                        tms->hbrmax = j;
322          }
323 <        horig = (tmTop->hbrmin-MINBRT)/HISTEP;
324 <        hlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - horig;
323 >        horig = (tms->hbrmin-MINBRT)/HISTEP;
324 >        hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
325          if (hlen > oldlen) {                    /* (re)allocate histogram */
326 <                register int    *newhist = (int *)calloc(hlen, sizeof(int));
326 >                int     *newhist = (int *)calloc(hlen, sizeof(int));
327                  if (newhist == NULL)
328                          returnErr(TM_E_NOMEM);
329                  if (oldlen) {                   /* copy and free old */
330                          for (i = oldlen, j = i+oldorig-horig; i; )
331 <                                newhist[--j] = tmTop->histo[--i];
332 <                        free((MEM_PTR)tmTop->histo);
331 >                                newhist[--j] = tms->histo[--i];
332 >                        free((MEM_PTR)tms->histo);
333                  }
334 <                tmTop->histo = newhist;
334 >                tms->histo = newhist;
335          }
336          if (wt == 0)
337                  returnOK;
338          for (i = len; i--; )                    /* add in new counts */
339                  if (ls[i] >= MINBRT)
340 <                        tmTop->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
340 >                        tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
341          returnOK;
342   }
343  
344  
345   static double
346 < htcontrs(La)            /* human threshold contrast sensitivity, dL(La) */
347 < double  La;
346 > htcontrs(               /* human threshold contrast sensitivity, dL(La) */
347 > double  La
348 > )
349   {
350          double  l10La, l10dL;
351                                  /* formula taken from Ferwerda et al. [SG96] */
# Line 392 | Line 366 | double La;
366  
367  
368   static int
369 < tmNewMap()
369 > tmNewMap(                       /* allocate new tone-mapping array */
370 > TMstruct        *tms
371 > )
372   {
373 <        if (tmTop->lumap != NULL && (tmTop->mbrmax - tmTop->mbrmin) !=
374 <                                        (tmTop->hbrmax - tmTop->hbrmin)) {
375 <                free((MEM_PTR)tmTop->lumap);
376 <                tmTop->lumap = NULL;
373 >        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
374 >                                        (tms->hbrmax - tms->hbrmin)) {
375 >                free((MEM_PTR)tms->lumap);
376 >                tms->lumap = NULL;
377          }
378 <        tmTop->mbrmin = tmTop->hbrmin;
379 <        tmTop->mbrmax = tmTop->hbrmax;
380 <        if (tmTop->mbrmin > tmTop->mbrmax)
378 >        tms->mbrmin = tms->hbrmin;
379 >        tms->mbrmax = tms->hbrmax;
380 >        if (tms->mbrmin > tms->mbrmax)
381                  return 0;
382 <        if (tmTop->lumap == NULL)
383 <                tmTop->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
384 <                                        (tmTop->mbrmax-tmTop->mbrmin+1));
385 <        return(tmTop->lumap != NULL);
382 >        if (tms->lumap == NULL)
383 >                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
384 >                                        (tms->mbrmax-tms->mbrmin+1));
385 >        return(tms->lumap != NULL);
386   }
387  
388  
389   int
390 < tmFixedMapping(expmult, gamval)
391 < double  expmult;
392 < double  gamval;
390 > tmFixedMapping(                 /* compute fixed, linear tone-mapping */
391 > TMstruct        *tms,
392 > double  expmult,
393 > double  gamval
394 > )
395   {
396 <        static char     funcName[] = "tmFixedMapping";
396 >        static const char funcName[] = "tmFixedMapping";
397          double          d;
398 <        register int    i;
398 >        int     i;
399          
400 <        if (!tmNewMap())
400 >        if (!tmNewMap(tms))
401                  returnErr(TM_E_NOMEM);
402          if (expmult <= .0)
403                  expmult = 1.;
404          if (gamval < MINGAM)
405 <                gamval = tmTop->mongam;
406 <        d = log(expmult/tmTop->inpsf);
407 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; )
408 <                tmTop->lumap[i] = 256. * exp(
409 <                        ( d + (tmTop->mbrmin+i)*(1./TM_BRTSCALE) )
405 >                gamval = tms->mongam;
406 >        d = log(expmult/tms->inpsf);
407 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
408 >                tms->lumap[i] = 256. * exp(
409 >                        ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
410                          / gamval );
411          returnOK;
412   }
413  
414  
415   int
416 < tmComputeMapping(gamval, Lddyn, Ldmax)
417 < double  gamval;
418 < double  Lddyn;
419 < double  Ldmax;
416 > tmComputeMapping(                       /* compute histogram tone-mapping */
417 > TMstruct        *tms,
418 > double  gamval,
419 > double  Lddyn,
420 > double  Ldmax
421 > )
422   {
423 <        static char     funcName[] = "tmComputeMapping";
423 >        static const char funcName[] = "tmComputeMapping";
424          int     *histo;
425          float   *cumf;
426          int     brt0, histlen, threshold, ceiling, trimmings;
427          double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
428 <        int4    histot;
428 >        int32   histot;
429          double  sum;
430 <        register double d;
431 <        register int    i, j;
430 >        double  d;
431 >        int     i, j;
432  
433 <        if (tmTop == NULL || tmTop->histo == NULL)
433 >        if (tms == NULL || tms->histo == NULL)
434                  returnErr(TM_E_TMINVAL);
435                                          /* check arguments */
436          if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
437          if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
438 <        if (gamval < MINGAM) gamval = tmTop->mongam;
438 >        if (gamval < MINGAM) gamval = tms->mongam;
439                                          /* compute handy values */
440          Ldmin = Ldmax/Lddyn;
441          logLddyn = log(Lddyn);
442          Ldavg = sqrt(Ldmax*Ldmin);
443 <        i = (tmTop->hbrmin-MINBRT)/HISTEP;
443 >        i = (tms->hbrmin-MINBRT)/HISTEP;
444          brt0 = MINBRT + HISTEP/2 + i*HISTEP;
445 <        histlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - i;
445 >        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
446                                          /* histogram total and mean */
447          histot = 0; sum = 0;
448          j = brt0 + histlen*HISTEP;
449          for (i = histlen; i--; ) {
450 <                histot += tmTop->histo[i];
451 <                sum += (j -= HISTEP) * tmTop->histo[i];
450 >                histot += tms->histo[i];
451 >                sum += (j -= HISTEP) * tms->histo[i];
452          }
453 <        threshold = histot*.025 + .5;
453 >        threshold = histot*0.005 + .5;
454          if (threshold < 4)
455                  returnErr(TM_E_TMFAIL);
456          Lwavg = tmLuminance( (double)sum / histot );
457                                          /* allocate space for mapping */
458 <        if (!tmNewMap())
458 >        if (!tmNewMap(tms))
459                  returnErr(TM_E_NOMEM);
460                                          /* use linear tone mapping? */
461 <        if (tmTop->flags & TM_F_LINEAR)
461 >        if (tms->flags & TM_F_LINEAR)
462                  goto linearmap;
463                                          /* clamp histogram */
464          histo = (int *)malloc(histlen*sizeof(int));
465          cumf = (float *)malloc((histlen+2)*sizeof(float));
466 <        if (histo == NULL | cumf == NULL)
466 >        if ((histo == NULL) | (cumf == NULL))
467                  returnErr(TM_E_NOMEM);
468          cumf[histlen+1] = 1.;           /* guard for assignment code */
469          for (i = histlen; i--; )        /* make malleable copy */
470 <                histo[i] = tmTop->histo[i];
470 >                histo[i] = tms->histo[i];
471          do {                            /* iterate to solution */
472                  sum = 0;                /* cumulative probability */
473                  for (i = 0; i < histlen; i++) {
# Line 495 | Line 475 | double Ldmax;
475                          sum += histo[i];
476                  }
477                  cumf[histlen] = 1.;
478 <                Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) /
478 >                Tr = histot * (double)(tms->hbrmax - tms->hbrmin) /
479                          ((double)histlen*TM_BRTSCALE) / logLddyn;
480                  ceiling = Tr + 1.;
481                  trimmings = 0;          /* clip to envelope */
482                  for (i = histlen; i--; ) {
483 <                        if (tmTop->flags & TM_F_HCONTR) {
483 >                        if (tms->flags & TM_F_HCONTR) {
484                                  Lw = tmLuminance(brt0 + i*HISTEP);
485                                  Ld = Ldmin * exp( logLddyn *
486                                          .5*(cumf[i]+cumf[i+1]) );
# Line 520 | Line 500 | double Ldmax;
500                  }
501          } while (trimmings > threshold);
502                                          /* assign tone-mapping */
503 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) {
504 <                j = d = (double)i/(tmTop->mbrmax-tmTop->mbrmin)*histlen;
503 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
504 >                j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
505                  d -= (double)j;
506                  Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
507                  d = (Ld - Ldmin)/(Ldmax - Ldmin);
508 <                tmTop->lumap[i] = 256.*pow(d, 1./gamval);
508 >                tms->lumap[i] = 256.*pow(d, 1./gamval);
509          }
510          free((MEM_PTR)histo);           /* clean up and return */
511          free((MEM_PTR)cumf);
512          returnOK;
513   linearmap:                              /* linear tone-mapping */
514 <        if (tmTop->flags & TM_F_HCONTR)
514 >        if (tms->flags & TM_F_HCONTR)
515                  d = htcontrs(Ldavg) / htcontrs(Lwavg);
516          else
517                  d = Ldavg / Lwavg;
518 <        return(tmFixedMapping(tmTop->inpsf*d/Ldmax, gamval));
518 >        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
519   }
520  
521  
522   int
523 < tmMapPixels(ps, ls, cs, len)
524 < register BYTE   *ps;
525 < TMbright        *ls;
526 < register BYTE   *cs;
527 < int     len;
523 > tmMapPixels(                    /* apply tone-mapping to pixel(s) */
524 > TMstruct        *tms,
525 > BYTE    *ps,
526 > TMbright        *ls,
527 > BYTE    *cs,
528 > int     len
529 > )
530   {
531 <        static char     funcName[] = "tmMapPixels";
532 <        register int4   li, pv;
531 >        static const char funcName[] = "tmMapPixels";
532 >        int32   li, pv;
533  
534 <        if (tmTop == NULL || tmTop->lumap == NULL)
534 >        if (tms == NULL || tms->lumap == NULL)
535                  returnErr(TM_E_TMINVAL);
536 <        if (ps == NULL | ls == NULL | len < 0)
536 >        if ((ps == NULL) | (ls == NULL) | (len < 0))
537                  returnErr(TM_E_ILLEGAL);
538          while (len--) {
539 <                if ((li = *ls++) < tmTop->mbrmin) {
539 >                if ((li = *ls++) < tms->mbrmin) {
540                          li = 0;
541                  } else {
542 <                        if (li > tmTop->mbrmax)
543 <                                li = tmTop->mbrmax;
544 <                        li = tmTop->lumap[li - tmTop->mbrmin];
542 >                        if (li > tms->mbrmax)
543 >                                li = tms->mbrmax;
544 >                        li = tms->lumap[li - tms->mbrmin];
545                  }
546                  if (cs == TM_NOCHROM)
547                          *ps++ = li>255 ? 255 : li;
548                  else {
549 <                        pv = *cs++ * li / tmTop->cdiv[RED];
549 >                        pv = *cs++ * li / tms->cdiv[RED];
550                          *ps++ = pv>255 ? 255 : pv;
551 <                        pv = *cs++ * li / tmTop->cdiv[GRN];
551 >                        pv = *cs++ * li / tms->cdiv[GRN];
552                          *ps++ = pv>255 ? 255 : pv;
553 <                        pv = *cs++ * li / tmTop->cdiv[BLU];
553 >                        pv = *cs++ * li / tms->cdiv[BLU];
554                          *ps++ = pv>255 ? 255 : pv;
555                  }
556          }
# Line 576 | Line 558 | int    len;
558   }
559  
560  
579 struct tmStruct *
580 tmPop()                         /* pop top tone mapping off stack */
581 {
582        register struct tmStruct        *tms;
561  
584        if ((tms = tmTop) != NULL)
585                tmTop = tms->tmprev;
586        return(tms);
587 }
562  
563 <
564 < int
565 < tmPull(tms)                     /* pull a tone mapping from stack */
566 < register struct tmStruct        *tms;
563 > TMstruct *
564 > tmDup(                          /* duplicate top tone mapping */
565 > TMstruct        *tms
566 > )
567   {
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 {
568          int     len;
569 <        register int    i;
570 <        register struct tmStruct        *tmnew;
569 >        int     i;
570 >        TMstruct        *tmnew;
571  
572 <        if (tmTop == NULL)              /* anything to duplicate? */
572 >        if (tms == NULL)                /* anything to duplicate? */
573                  return(NULL);
574 <        tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct));
574 >        tmnew = (TMstruct *)malloc(sizeof(TMstruct));
575          if (tmnew == NULL)
576                  return(NULL);
577 <        *tmnew = *tmTop;                /* copy everything */
577 >        *tmnew = *tms;          /* copy everything */
578          if (tmnew->histo != NULL) {     /* duplicate histogram */
579                  len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
580                                  (tmnew->hbrmin-MINBRT)/HISTEP;
581                  tmnew->histo = (int *)malloc(len*sizeof(int));
582                  if (tmnew->histo != NULL)
583                          for (i = len; i--; )
584 <                                tmnew->histo[i] = tmTop->histo[i];
584 >                                tmnew->histo[i] = tms->histo[i];
585          }
586          if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */
587                  len = tmnew->mbrmax-tmnew->mbrmin+1;
# Line 637 | Line 589 | tmDup()                                /* duplicate top tone mapping */
589                                                  len*sizeof(unsigned short) );
590                  if (tmnew->lumap != NULL)
591                          for (i = len; i--; )
592 <                                tmnew->lumap[i] = tmTop->lumap[i];
592 >                                tmnew->lumap[i] = tms->lumap[i];
593          }
594                                          /* clear package data */
595          for (i = tmNumPkgs; i--; )
596                  tmnew->pd[i] = NULL;
597 <        tmnew->tmprev = tmTop;          /* make copy current */
598 <        return(tmTop = tmnew);
597 >                                        /* return copy */
598 >        return(tmnew);
599   }
600  
601  
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
602   void
603   tmDone(tms)                     /* done with tone mapping -- destroy it */
604 < register struct tmStruct        *tms;
604 > TMstruct        *tms;
605   {
606 <        register int    i;
607 <                                        /* NULL arg. is equiv. to tmTop */
608 <        if (tms == NULL && (tms = tmTop) == NULL)
606 >        int     i;
607 >                                        /* NULL arg. is equiv. to tms */
608 >        if (tms == NULL)
609                  return;
677                                        /* take out of stack if present */
678        (void)tmPull(tms);
610                                          /* free tables */
611          if (tms->histo != NULL)
612                  free((MEM_PTR)tms->histo);
# Line 695 | Line 626 | BYTE   tmMesofact[BMESUPPER-BMESLOWER];
626   void
627   tmMkMesofact()                          /* build mesopic lookup factor table */
628   {
629 <        register int    i;
629 >        int     i;
630  
631          if (tmMesofact[BMESUPPER-BMESLOWER-1])
632                  return;
# Line 708 | Line 639 | tmMkMesofact()                         /* build mesopic lookup factor table
639  
640  
641   int
642 < tmErrorReturn(func, err)                /* error return (with message) */
643 < char    *func;
644 < int     err;
642 > tmErrorReturn(                          /* error return (with message) */
643 > const char      *func,
644 > TMstruct        *tms,
645 > int     err
646 > )
647   {
648 <        tmLastFunction = func;
649 <        tmLastError = err;
650 <        if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR)
651 <                return(err);
648 >        if (tms != NULL) {
649 >                tms->lastFunc = func;
650 >                tms->lastError = err;
651 >                if (tms->flags & TM_F_NOSTDERR)
652 >                        return(err);
653 >        }
654          fputs(func, stderr);
655          fputs(": ", stderr);
656          fputs(tmErrorMessage[err], stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines