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.14 by greg, Thu Sep 18 20:18:19 2003 UTC vs.
Revision 3.18 by greg, Fri Jan 7 22:05:30 2005 UTC

# Line 18 | Line 18 | static const char      RCSid[] = "$Id$";
18  
19   #define exp10(x)        exp(M_LN10*(x))
20  
21 struct tmStruct *tmTop = NULL;          /* current tone mapping stack */
22
21                                          /* our list of conversion packages */
22   struct tmPackage        *tmPkg[TM_MAXPKG];
23   int     tmNumPkgs = 0;                  /* number of registered packages */
24  
27 int     tmLastError;                    /* last error incurred by library */
28 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 70 | 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 77 | 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";
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))
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 198 | 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 214 | 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   int
243 < tmCvGrays(ls, scan, len)                /* convert float gray values */
244 < TMbright        *ls;
245 < float   *scan;
246 < int     len;
243 > tmCvGrays(                              /* convert float gray values */
244 > TMstruct        *tms,
245 > TMbright        *ls,
246 > float   *scan,
247 > int     len
248 > )
249   {
250 <        static char     funcName[] = "tmCvGrays";
251 <        register double d;
252 <        register int    i;
250 >        static const char funcName[] = "tmCvGrays";
251 >        double  d;
252 >        int     i;
253  
254 <        if (tmTop == NULL)
254 >        if (tms == NULL)
255                  returnErr(TM_E_TMINVAL);
256          if ((ls == NULL) | (scan == NULL) | (len < 0))
257                  returnErr(TM_E_ILLEGAL);
# Line 257 | Line 267 | int    len;
267  
268  
269   int
270 < tmAddHisto(ls, len, wt)                 /* add values to histogram */
271 < register TMbright       *ls;
272 < int     len;
273 < int     wt;
270 > tmAddHisto(                             /* add values to histogram */
271 > TMstruct        *tms,
272 > TMbright        *ls,
273 > int     len,
274 > int     wt
275 > )
276   {
277 <        static char     funcName[] = "tmAddHisto";
277 >        static const char funcName[] = "tmAddHisto";
278          int     oldorig=0, oldlen, horig, hlen;
279 <        register int    i, j;
279 >        int     i, j;
280  
281 <        if (tmTop == NULL)
281 >        if (tms == NULL)
282                  returnErr(TM_E_TMINVAL);
283          if (len < 0)
284                  returnErr(TM_E_ILLEGAL);
285          if (len == 0)
286                  returnOK;
287                                                  /* first, grow limits */
288 <        if (tmTop->histo == NULL) {
288 >        if (tms->histo == NULL) {
289                  for (i = len; i-- && ls[i] < MINBRT; )
290                          ;
291                  if (i < 0)
292                          returnOK;
293 <                tmTop->hbrmin = tmTop->hbrmax = ls[i];
293 >                tms->hbrmin = tms->hbrmax = ls[i];
294                  oldlen = 0;
295          } else {
296 <                oldorig = (tmTop->hbrmin-MINBRT)/HISTEP;
297 <                oldlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
296 >                oldorig = (tms->hbrmin-MINBRT)/HISTEP;
297 >                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
298          }
299          for (i = len; i--; ) {
300                  if ((j = ls[i]) < MINBRT)
301                          continue;
302 <                if (j < tmTop->hbrmin)
303 <                        tmTop->hbrmin = j;
304 <                else if (j > tmTop->hbrmax)
305 <                        tmTop->hbrmax = j;
302 >                if (j < tms->hbrmin)
303 >                        tms->hbrmin = j;
304 >                else if (j > tms->hbrmax)
305 >                        tms->hbrmax = j;
306          }
307 <        horig = (tmTop->hbrmin-MINBRT)/HISTEP;
308 <        hlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - horig;
307 >        horig = (tms->hbrmin-MINBRT)/HISTEP;
308 >        hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
309          if (hlen > oldlen) {                    /* (re)allocate histogram */
310 <                register int    *newhist = (int *)calloc(hlen, sizeof(int));
310 >                int     *newhist = (int *)calloc(hlen, sizeof(int));
311                  if (newhist == NULL)
312                          returnErr(TM_E_NOMEM);
313                  if (oldlen) {                   /* copy and free old */
314                          for (i = oldlen, j = i+oldorig-horig; i; )
315 <                                newhist[--j] = tmTop->histo[--i];
316 <                        free((MEM_PTR)tmTop->histo);
315 >                                newhist[--j] = tms->histo[--i];
316 >                        free((MEM_PTR)tms->histo);
317                  }
318 <                tmTop->histo = newhist;
318 >                tms->histo = newhist;
319          }
320          if (wt == 0)
321                  returnOK;
322          for (i = len; i--; )                    /* add in new counts */
323                  if (ls[i] >= MINBRT)
324 <                        tmTop->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
324 >                        tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
325          returnOK;
326   }
327  
328  
329   static double
330 < htcontrs(La)            /* human threshold contrast sensitivity, dL(La) */
331 < double  La;
330 > htcontrs(               /* human threshold contrast sensitivity, dL(La) */
331 > double  La
332 > )
333   {
334          double  l10La, l10dL;
335                                  /* formula taken from Ferwerda et al. [SG96] */
# Line 337 | Line 350 | double La;
350  
351  
352   static int
353 < tmNewMap()
353 > tmNewMap(                       /* allocate new tone-mapping array */
354 > TMstruct        *tms
355 > )
356   {
357 <        if (tmTop->lumap != NULL && (tmTop->mbrmax - tmTop->mbrmin) !=
358 <                                        (tmTop->hbrmax - tmTop->hbrmin)) {
359 <                free((MEM_PTR)tmTop->lumap);
360 <                tmTop->lumap = NULL;
357 >        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
358 >                                        (tms->hbrmax - tms->hbrmin)) {
359 >                free((MEM_PTR)tms->lumap);
360 >                tms->lumap = NULL;
361          }
362 <        tmTop->mbrmin = tmTop->hbrmin;
363 <        tmTop->mbrmax = tmTop->hbrmax;
364 <        if (tmTop->mbrmin > tmTop->mbrmax)
362 >        tms->mbrmin = tms->hbrmin;
363 >        tms->mbrmax = tms->hbrmax;
364 >        if (tms->mbrmin > tms->mbrmax)
365                  return 0;
366 <        if (tmTop->lumap == NULL)
367 <                tmTop->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
368 <                                        (tmTop->mbrmax-tmTop->mbrmin+1));
369 <        return(tmTop->lumap != NULL);
366 >        if (tms->lumap == NULL)
367 >                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
368 >                                        (tms->mbrmax-tms->mbrmin+1));
369 >        return(tms->lumap != NULL);
370   }
371  
372  
373   int
374 < tmFixedMapping(expmult, gamval)
375 < double  expmult;
376 < double  gamval;
374 > tmFixedMapping(                 /* compute fixed, linear tone-mapping */
375 > TMstruct        *tms,
376 > double  expmult,
377 > double  gamval
378 > )
379   {
380 <        static char     funcName[] = "tmFixedMapping";
380 >        static const char funcName[] = "tmFixedMapping";
381          double          d;
382 <        register int    i;
382 >        int     i;
383          
384 <        if (!tmNewMap())
384 >        if (!tmNewMap(tms))
385                  returnErr(TM_E_NOMEM);
386          if (expmult <= .0)
387                  expmult = 1.;
388          if (gamval < MINGAM)
389 <                gamval = tmTop->mongam;
390 <        d = log(expmult/tmTop->inpsf);
391 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; )
392 <                tmTop->lumap[i] = 256. * exp(
393 <                        ( d + (tmTop->mbrmin+i)*(1./TM_BRTSCALE) )
389 >                gamval = tms->mongam;
390 >        d = log(expmult/tms->inpsf);
391 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
392 >                tms->lumap[i] = 256. * exp(
393 >                        ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
394                          / gamval );
395          returnOK;
396   }
397  
398  
399   int
400 < tmComputeMapping(gamval, Lddyn, Ldmax)
401 < double  gamval;
402 < double  Lddyn;
403 < double  Ldmax;
400 > tmComputeMapping(                       /* compute histogram tone-mapping */
401 > TMstruct        *tms,
402 > double  gamval,
403 > double  Lddyn,
404 > double  Ldmax
405 > )
406   {
407 <        static char     funcName[] = "tmComputeMapping";
407 >        static const char funcName[] = "tmComputeMapping";
408          int     *histo;
409          float   *cumf;
410          int     brt0, histlen, threshold, ceiling, trimmings;
411          double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
412          int32   histot;
413          double  sum;
414 <        register double d;
415 <        register int    i, j;
414 >        double  d;
415 >        int     i, j;
416  
417 <        if (tmTop == NULL || tmTop->histo == NULL)
417 >        if (tms == NULL || tms->histo == NULL)
418                  returnErr(TM_E_TMINVAL);
419                                          /* check arguments */
420          if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
421          if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
422 <        if (gamval < MINGAM) gamval = tmTop->mongam;
422 >        if (gamval < MINGAM) gamval = tms->mongam;
423                                          /* compute handy values */
424          Ldmin = Ldmax/Lddyn;
425          logLddyn = log(Lddyn);
426          Ldavg = sqrt(Ldmax*Ldmin);
427 <        i = (tmTop->hbrmin-MINBRT)/HISTEP;
427 >        i = (tms->hbrmin-MINBRT)/HISTEP;
428          brt0 = MINBRT + HISTEP/2 + i*HISTEP;
429 <        histlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - i;
429 >        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
430                                          /* histogram total and mean */
431          histot = 0; sum = 0;
432          j = brt0 + histlen*HISTEP;
433          for (i = histlen; i--; ) {
434 <                histot += tmTop->histo[i];
435 <                sum += (j -= HISTEP) * tmTop->histo[i];
434 >                histot += tms->histo[i];
435 >                sum += (j -= HISTEP) * tms->histo[i];
436          }
437          threshold = histot*0.005 + .5;
438          if (threshold < 4)
439                  returnErr(TM_E_TMFAIL);
440          Lwavg = tmLuminance( (double)sum / histot );
441                                          /* allocate space for mapping */
442 <        if (!tmNewMap())
442 >        if (!tmNewMap(tms))
443                  returnErr(TM_E_NOMEM);
444                                          /* use linear tone mapping? */
445 <        if (tmTop->flags & TM_F_LINEAR)
445 >        if (tms->flags & TM_F_LINEAR)
446                  goto linearmap;
447                                          /* clamp histogram */
448          histo = (int *)malloc(histlen*sizeof(int));
# Line 432 | Line 451 | double Ldmax;
451                  returnErr(TM_E_NOMEM);
452          cumf[histlen+1] = 1.;           /* guard for assignment code */
453          for (i = histlen; i--; )        /* make malleable copy */
454 <                histo[i] = tmTop->histo[i];
454 >                histo[i] = tms->histo[i];
455          do {                            /* iterate to solution */
456                  sum = 0;                /* cumulative probability */
457                  for (i = 0; i < histlen; i++) {
# Line 440 | Line 459 | double Ldmax;
459                          sum += histo[i];
460                  }
461                  cumf[histlen] = 1.;
462 <                Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) /
462 >                Tr = histot * (double)(tms->hbrmax - tms->hbrmin) /
463                          ((double)histlen*TM_BRTSCALE) / logLddyn;
464                  ceiling = Tr + 1.;
465                  trimmings = 0;          /* clip to envelope */
466                  for (i = histlen; i--; ) {
467 <                        if (tmTop->flags & TM_F_HCONTR) {
467 >                        if (tms->flags & TM_F_HCONTR) {
468                                  Lw = tmLuminance(brt0 + i*HISTEP);
469                                  Ld = Ldmin * exp( logLddyn *
470                                          .5*(cumf[i]+cumf[i+1]) );
# Line 465 | Line 484 | double Ldmax;
484                  }
485          } while (trimmings > threshold);
486                                          /* assign tone-mapping */
487 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) {
488 <                j = d = (double)i/(tmTop->mbrmax-tmTop->mbrmin)*histlen;
487 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
488 >                j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
489                  d -= (double)j;
490                  Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
491                  d = (Ld - Ldmin)/(Ldmax - Ldmin);
492 <                tmTop->lumap[i] = 256.*pow(d, 1./gamval);
492 >                tms->lumap[i] = 256.*pow(d, 1./gamval);
493          }
494          free((MEM_PTR)histo);           /* clean up and return */
495          free((MEM_PTR)cumf);
496          returnOK;
497   linearmap:                              /* linear tone-mapping */
498 <        if (tmTop->flags & TM_F_HCONTR)
498 >        if (tms->flags & TM_F_HCONTR)
499                  d = htcontrs(Ldavg) / htcontrs(Lwavg);
500          else
501                  d = Ldavg / Lwavg;
502 <        return(tmFixedMapping(tmTop->inpsf*d/Ldmax, gamval));
502 >        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
503   }
504  
505  
506   int
507 < tmMapPixels(ps, ls, cs, len)
508 < register BYTE   *ps;
509 < TMbright        *ls;
510 < register BYTE   *cs;
511 < int     len;
507 > tmMapPixels(                    /* apply tone-mapping to pixel(s) */
508 > TMstruct        *tms,
509 > BYTE    *ps,
510 > TMbright        *ls,
511 > BYTE    *cs,
512 > int     len
513 > )
514   {
515 <        static char     funcName[] = "tmMapPixels";
516 <        register int32  li, pv;
515 >        static const char funcName[] = "tmMapPixels";
516 >        int32   li, pv;
517  
518 <        if (tmTop == NULL || tmTop->lumap == NULL)
518 >        if (tms == NULL || tms->lumap == NULL)
519                  returnErr(TM_E_TMINVAL);
520          if ((ps == NULL) | (ls == NULL) | (len < 0))
521                  returnErr(TM_E_ILLEGAL);
522          while (len--) {
523 <                if ((li = *ls++) < tmTop->mbrmin) {
523 >                if ((li = *ls++) < tms->mbrmin) {
524                          li = 0;
525                  } else {
526 <                        if (li > tmTop->mbrmax)
527 <                                li = tmTop->mbrmax;
528 <                        li = tmTop->lumap[li - tmTop->mbrmin];
526 >                        if (li > tms->mbrmax)
527 >                                li = tms->mbrmax;
528 >                        li = tms->lumap[li - tms->mbrmin];
529                  }
530                  if (cs == TM_NOCHROM)
531                          *ps++ = li>255 ? 255 : li;
532                  else {
533 <                        pv = *cs++ * li / tmTop->cdiv[RED];
533 >                        pv = *cs++ * li / tms->cdiv[RED];
534                          *ps++ = pv>255 ? 255 : pv;
535 <                        pv = *cs++ * li / tmTop->cdiv[GRN];
535 >                        pv = *cs++ * li / tms->cdiv[GRN];
536                          *ps++ = pv>255 ? 255 : pv;
537 <                        pv = *cs++ * li / tmTop->cdiv[BLU];
537 >                        pv = *cs++ * li / tms->cdiv[BLU];
538                          *ps++ = pv>255 ? 255 : pv;
539                  }
540          }
# Line 521 | Line 542 | int    len;
542   }
543  
544  
524 struct tmStruct *
525 tmPop()                         /* pop top tone mapping off stack */
526 {
527        register struct tmStruct        *tms;
545  
529        if ((tms = tmTop) != NULL)
530                tmTop = tms->tmprev;
531        return(tms);
532 }
546  
547 <
548 < int
549 < tmPull(tms)                     /* pull a tone mapping from stack */
550 < register struct tmStruct        *tms;
547 > TMstruct *
548 > tmDup(                          /* duplicate top tone mapping */
549 > TMstruct        *tms
550 > )
551   {
539        register struct tmStruct        *tms2;
540                                        /* special cases first */
541        if ((tms == NULL) | (tmTop == NULL))
542                return(0);
543        if (tms == tmTop) {
544                tmTop = tms->tmprev;
545                tms->tmprev = NULL;
546                return(1);
547        }
548        for (tms2 = tmTop; tms2->tmprev != NULL; tms2 = tms2->tmprev)
549                if (tms == tms2->tmprev) {      /* remove it */
550                        tms2->tmprev = tms->tmprev;
551                        tms->tmprev = NULL;
552                        return(1);
553                }
554        return(0);                      /* not found on stack */
555 }
556
557
558 struct tmStruct *
559 tmDup()                         /* duplicate top tone mapping */
560 {
552          int     len;
553 <        register int    i;
554 <        register struct tmStruct        *tmnew;
553 >        int     i;
554 >        TMstruct        *tmnew;
555  
556 <        if (tmTop == NULL)              /* anything to duplicate? */
556 >        if (tms == NULL)                /* anything to duplicate? */
557                  return(NULL);
558 <        tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct));
558 >        tmnew = (TMstruct *)malloc(sizeof(TMstruct));
559          if (tmnew == NULL)
560                  return(NULL);
561 <        *tmnew = *tmTop;                /* copy everything */
561 >        *tmnew = *tms;          /* copy everything */
562          if (tmnew->histo != NULL) {     /* duplicate histogram */
563                  len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
564                                  (tmnew->hbrmin-MINBRT)/HISTEP;
565                  tmnew->histo = (int *)malloc(len*sizeof(int));
566                  if (tmnew->histo != NULL)
567                          for (i = len; i--; )
568 <                                tmnew->histo[i] = tmTop->histo[i];
568 >                                tmnew->histo[i] = tms->histo[i];
569          }
570          if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */
571                  len = tmnew->mbrmax-tmnew->mbrmin+1;
# Line 582 | Line 573 | tmDup()                                /* duplicate top tone mapping */
573                                                  len*sizeof(unsigned short) );
574                  if (tmnew->lumap != NULL)
575                          for (i = len; i--; )
576 <                                tmnew->lumap[i] = tmTop->lumap[i];
576 >                                tmnew->lumap[i] = tms->lumap[i];
577          }
578                                          /* clear package data */
579          for (i = tmNumPkgs; i--; )
580                  tmnew->pd[i] = NULL;
581 <        tmnew->tmprev = tmTop;          /* make copy current */
582 <        return(tmTop = tmnew);
581 >                                        /* return copy */
582 >        return(tmnew);
583   }
584  
585  
595 int
596 tmPush(tms)                     /* push tone mapping on top of stack */
597 register struct tmStruct        *tms;
598 {
599        static char     funcName[] = "tmPush";
600                                        /* check validity */
601        if (tms == NULL)
602                returnErr(TM_E_ILLEGAL);
603        if (tms == tmTop)               /* check necessity */
604                returnOK;
605                                        /* pull if already in stack */
606        (void)tmPull(tms);
607                                        /* push it on top */
608        tms->tmprev = tmTop;
609        tmTop = tms;
610        returnOK;
611 }
612
613
586   void
587   tmDone(tms)                     /* done with tone mapping -- destroy it */
588 < register struct tmStruct        *tms;
588 > TMstruct        *tms;
589   {
590 <        register int    i;
591 <                                        /* NULL arg. is equiv. to tmTop */
592 <        if (tms == NULL && (tms = tmTop) == NULL)
590 >        int     i;
591 >                                        /* NULL arg. is equiv. to tms */
592 >        if (tms == NULL)
593                  return;
622                                        /* take out of stack if present */
623        (void)tmPull(tms);
594                                          /* free tables */
595          if (tms->histo != NULL)
596                  free((MEM_PTR)tms->histo);
# Line 640 | Line 610 | BYTE   tmMesofact[BMESUPPER-BMESLOWER];
610   void
611   tmMkMesofact()                          /* build mesopic lookup factor table */
612   {
613 <        register int    i;
613 >        int     i;
614  
615          if (tmMesofact[BMESUPPER-BMESLOWER-1])
616                  return;
# Line 653 | Line 623 | tmMkMesofact()                         /* build mesopic lookup factor table
623  
624  
625   int
626 < tmErrorReturn(func, err)                /* error return (with message) */
627 < char    *func;
628 < int     err;
626 > tmErrorReturn(                          /* error return (with message) */
627 > const char      *func,
628 > TMstruct        *tms,
629 > int     err
630 > )
631   {
632 <        tmLastFunction = func;
633 <        tmLastError = err;
634 <        if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR)
635 <                return(err);
632 >        if (tms != NULL) {
633 >                tms->lastFunc = func;
634 >                tms->lastError = err;
635 >                if (tms->flags & TM_F_NOSTDERR)
636 >                        return(err);
637 >        }
638          fputs(func, stderr);
639          fputs(": ", stderr);
640          fputs(tmErrorMessage[err], stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines