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.15 by greg, Thu Jul 22 17:48:52 2004 UTC vs.
Revision 3.17 by greg, Fri Jan 7 21:41:06 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  
# Line 79 | Line 75 | double gamval;
75                                                  /* zero private data */
76          for (i = TM_MAXPKG; i--; )
77                  tmnew->pd[i] = NULL;
78 <                                                /* make tmnew current */
79 <        tmnew->tmprev = tmTop;
80 <        return(tmTop = tmnew);
78 >        tmnew->lastError = TM_E_OK;
79 >        tmnew->lastFunc = "NoErr";
80 >                                                /* return new TMstruct */
81 >        return(tmnew);
82   }
83  
84  
85   int
86 < tmSetSpace(pri, sf)             /* set input color space for conversions */
87 < RGBPRIMP        pri;
88 < double  sf;
86 > tmSetSpace(                     /* set input color space for conversions */
87 > TMstruct        *tms,
88 > RGBPRIMP        pri,
89 > double  sf
90 > )
91   {
92 <        static char     funcName[] = "tmSetSpace";
93 <        register int    i, j;
92 >        static const char funcName[] = "tmSetSpace";
93 >        int     i, j;
94                                                  /* error check */
95 <        if (tmTop == NULL)
95 >        if (tms == NULL)
96                  returnErr(TM_E_TMINVAL);
97          if (sf <= 1e-12)
98                  returnErr(TM_E_ILLEGAL);
99                                                  /* check if no change */
100 <        if (pri == tmTop->inppri && FEQ(sf, tmTop->inpsf))
100 >        if (pri == tms->inppri && FEQ(sf, tms->inpsf))
101                  returnOK;
102 <        tmTop->inppri = pri;                    /* let's set it */
103 <        tmTop->inpsf = sf;
102 >        tms->inppri = pri;                      /* let's set it */
103 >        tms->inpsf = sf;
104  
105 <        if (tmTop->flags & TM_F_BW) {           /* color doesn't matter */
106 <                tmTop->monpri = tmTop->inppri;          /* eliminate xform */
107 <                if (tmTop->inppri == TM_XYZPRIM) {
108 <                        tmTop->clf[CIEX] = tmTop->clf[CIEZ] = 0.;
109 <                        tmTop->clf[CIEY] = 1.;
105 >        if (tms->flags & TM_F_BW) {             /* color doesn't matter */
106 >                tms->monpri = tms->inppri;              /* eliminate xform */
107 >                if (tms->inppri == TM_XYZPRIM) {
108 >                        tms->clf[CIEX] = tms->clf[CIEZ] = 0.;
109 >                        tms->clf[CIEY] = 1.;
110                  } else {
111 <                        comprgb2xyzWBmat(tmTop->cmat, tmTop->monpri);
112 <                        tmTop->clf[RED] = tmTop->cmat[1][0];
113 <                        tmTop->clf[GRN] = tmTop->cmat[1][1];
114 <                        tmTop->clf[BLU] = tmTop->cmat[1][2];
111 >                        comprgb2xyzWBmat(tms->cmat, tms->monpri);
112 >                        tms->clf[RED] = tms->cmat[1][0];
113 >                        tms->clf[GRN] = tms->cmat[1][1];
114 >                        tms->clf[BLU] = tms->cmat[1][2];
115                  }
116 <                tmTop->cmat[0][0] = tmTop->cmat[1][1] = tmTop->cmat[2][2] =
117 <                                tmTop->inpsf;
118 <                tmTop->cmat[0][1] = tmTop->cmat[0][2] = tmTop->cmat[1][0] =
119 <                tmTop->cmat[1][2] = tmTop->cmat[2][0] = tmTop->cmat[2][1] = 0.;
116 >                tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] =
117 >                                tms->inpsf;
118 >                tms->cmat[0][1] = tms->cmat[0][2] = tms->cmat[1][0] =
119 >                tms->cmat[1][2] = tms->cmat[2][0] = tms->cmat[2][1] = 0.;
120  
121 <        } else if (tmTop->inppri == TM_XYZPRIM) /* input is XYZ */
122 <                compxyz2rgbWBmat(tmTop->cmat, tmTop->monpri);
121 >        } else if (tms->inppri == TM_XYZPRIM)   /* input is XYZ */
122 >                compxyz2rgbWBmat(tms->cmat, tms->monpri);
123  
124          else {                                  /* input is RGB */
125 <                if (tmTop->inppri != tmTop->monpri &&
126 <                                PRIMEQ(tmTop->inppri, tmTop->monpri))
127 <                        tmTop->inppri = tmTop->monpri;  /* no xform */
128 <                comprgb2rgbWBmat(tmTop->cmat, tmTop->inppri, tmTop->monpri);
125 >                if (tms->inppri != tms->monpri &&
126 >                                PRIMEQ(tms->inppri, tms->monpri))
127 >                        tms->inppri = tms->monpri;      /* no xform */
128 >                comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri);
129          }
130          for (i = 0; i < 3; i++)
131                  for (j = 0; j < 3; j++)
132 <                        tmTop->cmat[i][j] *= tmTop->inpsf;
132 >                        tms->cmat[i][j] *= tms->inpsf;
133                                                  /* set color divisors */
134          for (i = 0; i < 3; i++)
135 <                if (tmTop->clf[i] > .001)
136 <                        tmTop->cdiv[i] =
137 <                                256.*pow(tmTop->clf[i], 1./tmTop->mongam);
135 >                if (tms->clf[i] > .001)
136 >                        tms->cdiv[i] =
137 >                                256.*pow(tms->clf[i], 1./tms->mongam);
138                  else
139 <                        tmTop->cdiv[i] = 1;
139 >                        tms->cdiv[i] = 1;
140                                                  /* notify packages */
141          for (i = tmNumPkgs; i--; )
142 <                if (tmTop->pd[i] != NULL && tmPkg[i]->NewSpace != NULL)
143 <                        (*tmPkg[i]->NewSpace)(tmTop);
142 >                if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL)
143 >                        (*tmPkg[i]->NewSpace)(tms);
144          returnOK;
145   }
146  
147  
148   void
149 < tmClearHisto()                  /* clear current histogram */
149 > tmClearHisto(                   /* clear current histogram */
150 > TMstruct        *tms
151 > )
152   {
153 <        if (tmTop == NULL || tmTop->histo == NULL)
153 >        if (tms == NULL || tms->histo == NULL)
154                  return;
155 <        free((MEM_PTR)tmTop->histo);
156 <        tmTop->histo = NULL;
155 >        free((MEM_PTR)tms->histo);
156 >        tms->histo = NULL;
157   }
158  
159  
160   int
161 < tmCvColors(ls, cs, scan, len)           /* convert float colors */
162 < TMbright        *ls;
163 < BYTE    *cs;
164 < COLOR   *scan;
165 < int     len;
161 > tmCvColors(                             /* convert float colors */
162 > TMstruct        *tms,
163 > TMbright        *ls,
164 > BYTE    *cs,
165 > COLOR   *scan,
166 > int     len
167 > )
168   {
169 <        static char     funcName[] = "tmCvColors";
169 >        static const char funcName[] = "tmCvColors";
170          static COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
171          COLOR   cmon;
172          double  lum, slum;
173 <        register double d;
174 <        register int    i;
173 >        double  d;
174 >        int     i;
175  
176 <        if (tmTop == NULL)
176 >        if (tms == NULL)
177                  returnErr(TM_E_TMINVAL);
178          if ((ls == NULL) | (scan == NULL) | (len < 0))
179                  returnErr(TM_E_ILLEGAL);
180          for (i = len; i--; ) {
181 <                if (tmNeedMatrix(tmTop)) {              /* get monitor RGB */
182 <                        colortrans(cmon, tmTop->cmat, scan[i]);
181 >                if (tmNeedMatrix(tms)) {                /* get monitor RGB */
182 >                        colortrans(cmon, tms->cmat, scan[i]);
183                  } else {
184 <                        cmon[RED] = tmTop->inpsf*scan[i][RED];
185 <                        cmon[GRN] = tmTop->inpsf*scan[i][GRN];
186 <                        cmon[BLU] = tmTop->inpsf*scan[i][BLU];
184 >                        cmon[RED] = tms->inpsf*scan[i][RED];
185 >                        cmon[GRN] = tms->inpsf*scan[i][GRN];
186 >                        cmon[BLU] = tms->inpsf*scan[i][BLU];
187                  }
188                                                          /* world luminance */
189 <                lum =   tmTop->clf[RED]*cmon[RED] +
190 <                        tmTop->clf[GRN]*cmon[GRN] +
191 <                        tmTop->clf[BLU]*cmon[BLU] ;
189 >                lum =   tms->clf[RED]*cmon[RED] +
190 >                        tms->clf[GRN]*cmon[GRN] +
191 >                        tms->clf[BLU]*cmon[BLU] ;
192                                                          /* check range */
193                  if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
194 <                        lum =   tmTop->clf[RED]*cmon[RED] +
195 <                                tmTop->clf[GRN]*cmon[GRN] +
196 <                                tmTop->clf[BLU]*cmon[BLU] ;
194 >                        lum =   tms->clf[RED]*cmon[RED] +
195 >                                tms->clf[GRN]*cmon[GRN] +
196 >                                tms->clf[BLU]*cmon[BLU] ;
197                  if (lum < MINLUM) {
198                          ls[i] = MINBRT-1;               /* bogus value */
199                          lum = MINLUM;
# Line 200 | Line 203 | int    len;
203                  }
204                  if (cs == TM_NOCHROM)                   /* no color? */
205                          continue;
206 <                if (tmTop->flags & TM_F_MESOPIC && lum < LMESUPPER) {
206 >                if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
207                          slum = scotlum(cmon);           /* mesopic adj. */
208                          if (lum < LMESLOWER)
209                                  cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
210                          else {
211                                  d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
212 <                                if (tmTop->flags & TM_F_BW)
212 >                                if (tms->flags & TM_F_BW)
213                                          cmon[RED] = cmon[GRN] =
214                                                          cmon[BLU] = d*lum;
215                                  else
# Line 216 | Line 219 | int    len;
219                                  cmon[GRN] += d;
220                                  cmon[BLU] += d;
221                          }
222 <                } else if (tmTop->flags & TM_F_BW) {
222 >                } else if (tms->flags & TM_F_BW) {
223                          cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
224                  }
225 <                d = tmTop->clf[RED]*cmon[RED]/lum;
225 >                d = tms->clf[RED]*cmon[RED]/lum;
226                  cs[3*i  ] = d>=.999 ? 255 :
227 <                                (int)(256.*pow(d, 1./tmTop->mongam));
228 <                d = tmTop->clf[GRN]*cmon[GRN]/lum;
227 >                                (int)(256.*pow(d, 1./tms->mongam));
228 >                d = tms->clf[GRN]*cmon[GRN]/lum;
229                  cs[3*i+1] = d>=.999 ? 255 :
230 <                                (int)(256.*pow(d, 1./tmTop->mongam));
231 <                d = tmTop->clf[BLU]*cmon[BLU]/lum;
230 >                                (int)(256.*pow(d, 1./tms->mongam));
231 >                d = tms->clf[BLU]*cmon[BLU]/lum;
232                  cs[3*i+2] = d>=.999 ? 255 :
233 <                                (int)(256.*pow(d, 1./tmTop->mongam));
233 >                                (int)(256.*pow(d, 1./tms->mongam));
234          }
235          returnOK;
236   }
237  
238  
239   int
240 < tmCvGrays(ls, scan, len)                /* convert float gray values */
241 < TMbright        *ls;
242 < float   *scan;
243 < int     len;
240 > tmCvGrays(                              /* convert float gray values */
241 > TMstruct        *tms,
242 > TMbright        *ls,
243 > float   *scan,
244 > int     len
245 > )
246   {
247 <        static char     funcName[] = "tmCvGrays";
248 <        register double d;
249 <        register int    i;
247 >        static const char funcName[] = "tmCvGrays";
248 >        double  d;
249 >        int     i;
250  
251 <        if (tmTop == NULL)
251 >        if (tms == NULL)
252                  returnErr(TM_E_TMINVAL);
253          if ((ls == NULL) | (scan == NULL) | (len < 0))
254                  returnErr(TM_E_ILLEGAL);
# Line 259 | Line 264 | int    len;
264  
265  
266   int
267 < tmAddHisto(ls, len, wt)                 /* add values to histogram */
268 < register TMbright       *ls;
269 < int     len;
270 < int     wt;
267 > tmAddHisto(                             /* add values to histogram */
268 > TMstruct        *tms,
269 > TMbright        *ls,
270 > int     len,
271 > int     wt
272 > )
273   {
274 <        static char     funcName[] = "tmAddHisto";
274 >        static const char funcName[] = "tmAddHisto";
275          int     oldorig=0, oldlen, horig, hlen;
276 <        register int    i, j;
276 >        int     i, j;
277  
278 <        if (tmTop == NULL)
278 >        if (tms == NULL)
279                  returnErr(TM_E_TMINVAL);
280          if (len < 0)
281                  returnErr(TM_E_ILLEGAL);
282          if (len == 0)
283                  returnOK;
284                                                  /* first, grow limits */
285 <        if (tmTop->histo == NULL) {
285 >        if (tms->histo == NULL) {
286                  for (i = len; i-- && ls[i] < MINBRT; )
287                          ;
288                  if (i < 0)
289                          returnOK;
290 <                tmTop->hbrmin = tmTop->hbrmax = ls[i];
290 >                tms->hbrmin = tms->hbrmax = ls[i];
291                  oldlen = 0;
292          } else {
293 <                oldorig = (tmTop->hbrmin-MINBRT)/HISTEP;
294 <                oldlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
293 >                oldorig = (tms->hbrmin-MINBRT)/HISTEP;
294 >                oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
295          }
296          for (i = len; i--; ) {
297                  if ((j = ls[i]) < MINBRT)
298                          continue;
299 <                if (j < tmTop->hbrmin)
300 <                        tmTop->hbrmin = j;
301 <                else if (j > tmTop->hbrmax)
302 <                        tmTop->hbrmax = j;
299 >                if (j < tms->hbrmin)
300 >                        tms->hbrmin = j;
301 >                else if (j > tms->hbrmax)
302 >                        tms->hbrmax = j;
303          }
304 <        horig = (tmTop->hbrmin-MINBRT)/HISTEP;
305 <        hlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - horig;
304 >        horig = (tms->hbrmin-MINBRT)/HISTEP;
305 >        hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
306          if (hlen > oldlen) {                    /* (re)allocate histogram */
307 <                register int    *newhist = (int *)calloc(hlen, sizeof(int));
307 >                int     *newhist = (int *)calloc(hlen, sizeof(int));
308                  if (newhist == NULL)
309                          returnErr(TM_E_NOMEM);
310                  if (oldlen) {                   /* copy and free old */
311                          for (i = oldlen, j = i+oldorig-horig; i; )
312 <                                newhist[--j] = tmTop->histo[--i];
313 <                        free((MEM_PTR)tmTop->histo);
312 >                                newhist[--j] = tms->histo[--i];
313 >                        free((MEM_PTR)tms->histo);
314                  }
315 <                tmTop->histo = newhist;
315 >                tms->histo = newhist;
316          }
317          if (wt == 0)
318                  returnOK;
319          for (i = len; i--; )                    /* add in new counts */
320                  if (ls[i] >= MINBRT)
321 <                        tmTop->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
321 >                        tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
322          returnOK;
323   }
324  
325  
326   static double
327 < htcontrs(La)            /* human threshold contrast sensitivity, dL(La) */
328 < double  La;
327 > htcontrs(               /* human threshold contrast sensitivity, dL(La) */
328 > double  La
329 > )
330   {
331          double  l10La, l10dL;
332                                  /* formula taken from Ferwerda et al. [SG96] */
# Line 339 | Line 347 | double La;
347  
348  
349   static int
350 < tmNewMap()
350 > tmNewMap(                       /* allocate new tone-mapping array */
351 > TMstruct        *tms
352 > )
353   {
354 <        if (tmTop->lumap != NULL && (tmTop->mbrmax - tmTop->mbrmin) !=
355 <                                        (tmTop->hbrmax - tmTop->hbrmin)) {
356 <                free((MEM_PTR)tmTop->lumap);
357 <                tmTop->lumap = NULL;
354 >        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
355 >                                        (tms->hbrmax - tms->hbrmin)) {
356 >                free((MEM_PTR)tms->lumap);
357 >                tms->lumap = NULL;
358          }
359 <        tmTop->mbrmin = tmTop->hbrmin;
360 <        tmTop->mbrmax = tmTop->hbrmax;
361 <        if (tmTop->mbrmin > tmTop->mbrmax)
359 >        tms->mbrmin = tms->hbrmin;
360 >        tms->mbrmax = tms->hbrmax;
361 >        if (tms->mbrmin > tms->mbrmax)
362                  return 0;
363 <        if (tmTop->lumap == NULL)
364 <                tmTop->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
365 <                                        (tmTop->mbrmax-tmTop->mbrmin+1));
366 <        return(tmTop->lumap != NULL);
363 >        if (tms->lumap == NULL)
364 >                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
365 >                                        (tms->mbrmax-tms->mbrmin+1));
366 >        return(tms->lumap != NULL);
367   }
368  
369  
370   int
371 < tmFixedMapping(expmult, gamval)
372 < double  expmult;
373 < double  gamval;
371 > tmFixedMapping(                 /* compute fixed, linear tone-mapping */
372 > TMstruct        *tms,
373 > double  expmult,
374 > double  gamval
375 > )
376   {
377 <        static char     funcName[] = "tmFixedMapping";
377 >        static const char funcName[] = "tmFixedMapping";
378          double          d;
379 <        register int    i;
379 >        int     i;
380          
381 <        if (!tmNewMap())
381 >        if (!tmNewMap(tms))
382                  returnErr(TM_E_NOMEM);
383          if (expmult <= .0)
384                  expmult = 1.;
385          if (gamval < MINGAM)
386 <                gamval = tmTop->mongam;
387 <        d = log(expmult/tmTop->inpsf);
388 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; )
389 <                tmTop->lumap[i] = 256. * exp(
390 <                        ( d + (tmTop->mbrmin+i)*(1./TM_BRTSCALE) )
386 >                gamval = tms->mongam;
387 >        d = log(expmult/tms->inpsf);
388 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; )
389 >                tms->lumap[i] = 256. * exp(
390 >                        ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
391                          / gamval );
392          returnOK;
393   }
394  
395  
396   int
397 < tmComputeMapping(gamval, Lddyn, Ldmax)
398 < double  gamval;
399 < double  Lddyn;
400 < double  Ldmax;
397 > tmComputeMapping(                       /* compute histogram tone-mapping */
398 > TMstruct        *tms,
399 > double  gamval,
400 > double  Lddyn,
401 > double  Ldmax
402 > )
403   {
404 <        static char     funcName[] = "tmComputeMapping";
404 >        static const char funcName[] = "tmComputeMapping";
405          int     *histo;
406          float   *cumf;
407          int     brt0, histlen, threshold, ceiling, trimmings;
408          double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
409          int32   histot;
410          double  sum;
411 <        register double d;
412 <        register int    i, j;
411 >        double  d;
412 >        int     i, j;
413  
414 <        if (tmTop == NULL || tmTop->histo == NULL)
414 >        if (tms == NULL || tms->histo == NULL)
415                  returnErr(TM_E_TMINVAL);
416                                          /* check arguments */
417          if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
418          if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
419 <        if (gamval < MINGAM) gamval = tmTop->mongam;
419 >        if (gamval < MINGAM) gamval = tms->mongam;
420                                          /* compute handy values */
421          Ldmin = Ldmax/Lddyn;
422          logLddyn = log(Lddyn);
423          Ldavg = sqrt(Ldmax*Ldmin);
424 <        i = (tmTop->hbrmin-MINBRT)/HISTEP;
424 >        i = (tms->hbrmin-MINBRT)/HISTEP;
425          brt0 = MINBRT + HISTEP/2 + i*HISTEP;
426 <        histlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - i;
426 >        histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
427                                          /* histogram total and mean */
428          histot = 0; sum = 0;
429          j = brt0 + histlen*HISTEP;
430          for (i = histlen; i--; ) {
431 <                histot += tmTop->histo[i];
432 <                sum += (j -= HISTEP) * tmTop->histo[i];
431 >                histot += tms->histo[i];
432 >                sum += (j -= HISTEP) * tms->histo[i];
433          }
434          threshold = histot*0.005 + .5;
435          if (threshold < 4)
436                  returnErr(TM_E_TMFAIL);
437          Lwavg = tmLuminance( (double)sum / histot );
438                                          /* allocate space for mapping */
439 <        if (!tmNewMap())
439 >        if (!tmNewMap(tms))
440                  returnErr(TM_E_NOMEM);
441                                          /* use linear tone mapping? */
442 <        if (tmTop->flags & TM_F_LINEAR)
442 >        if (tms->flags & TM_F_LINEAR)
443                  goto linearmap;
444                                          /* clamp histogram */
445          histo = (int *)malloc(histlen*sizeof(int));
# Line 434 | Line 448 | double Ldmax;
448                  returnErr(TM_E_NOMEM);
449          cumf[histlen+1] = 1.;           /* guard for assignment code */
450          for (i = histlen; i--; )        /* make malleable copy */
451 <                histo[i] = tmTop->histo[i];
451 >                histo[i] = tms->histo[i];
452          do {                            /* iterate to solution */
453                  sum = 0;                /* cumulative probability */
454                  for (i = 0; i < histlen; i++) {
# Line 442 | Line 456 | double Ldmax;
456                          sum += histo[i];
457                  }
458                  cumf[histlen] = 1.;
459 <                Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) /
459 >                Tr = histot * (double)(tms->hbrmax - tms->hbrmin) /
460                          ((double)histlen*TM_BRTSCALE) / logLddyn;
461                  ceiling = Tr + 1.;
462                  trimmings = 0;          /* clip to envelope */
463                  for (i = histlen; i--; ) {
464 <                        if (tmTop->flags & TM_F_HCONTR) {
464 >                        if (tms->flags & TM_F_HCONTR) {
465                                  Lw = tmLuminance(brt0 + i*HISTEP);
466                                  Ld = Ldmin * exp( logLddyn *
467                                          .5*(cumf[i]+cumf[i+1]) );
# Line 467 | Line 481 | double Ldmax;
481                  }
482          } while (trimmings > threshold);
483                                          /* assign tone-mapping */
484 <        for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) {
485 <                j = d = (double)i/(tmTop->mbrmax-tmTop->mbrmin)*histlen;
484 >        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
485 >                j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
486                  d -= (double)j;
487                  Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
488                  d = (Ld - Ldmin)/(Ldmax - Ldmin);
489 <                tmTop->lumap[i] = 256.*pow(d, 1./gamval);
489 >                tms->lumap[i] = 256.*pow(d, 1./gamval);
490          }
491          free((MEM_PTR)histo);           /* clean up and return */
492          free((MEM_PTR)cumf);
493          returnOK;
494   linearmap:                              /* linear tone-mapping */
495 <        if (tmTop->flags & TM_F_HCONTR)
495 >        if (tms->flags & TM_F_HCONTR)
496                  d = htcontrs(Ldavg) / htcontrs(Lwavg);
497          else
498                  d = Ldavg / Lwavg;
499 <        return(tmFixedMapping(tmTop->inpsf*d/Ldmax, gamval));
499 >        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
500   }
501  
502  
503   int
504 < tmMapPixels(ps, ls, cs, len)
505 < register BYTE   *ps;
506 < TMbright        *ls;
507 < register BYTE   *cs;
508 < int     len;
504 > tmMapPixels(                    /* apply tone-mapping to pixel(s) */
505 > TMstruct        *tms,
506 > BYTE    *ps,
507 > TMbright        *ls,
508 > BYTE    *cs,
509 > int     len
510 > )
511   {
512 <        static char     funcName[] = "tmMapPixels";
513 <        register int32  li, pv;
512 >        static const char funcName[] = "tmMapPixels";
513 >        int32   li, pv;
514  
515 <        if (tmTop == NULL || tmTop->lumap == NULL)
515 >        if (tms == NULL || tms->lumap == NULL)
516                  returnErr(TM_E_TMINVAL);
517          if ((ps == NULL) | (ls == NULL) | (len < 0))
518                  returnErr(TM_E_ILLEGAL);
519          while (len--) {
520 <                if ((li = *ls++) < tmTop->mbrmin) {
520 >                if ((li = *ls++) < tms->mbrmin) {
521                          li = 0;
522                  } else {
523 <                        if (li > tmTop->mbrmax)
524 <                                li = tmTop->mbrmax;
525 <                        li = tmTop->lumap[li - tmTop->mbrmin];
523 >                        if (li > tms->mbrmax)
524 >                                li = tms->mbrmax;
525 >                        li = tms->lumap[li - tms->mbrmin];
526                  }
527                  if (cs == TM_NOCHROM)
528                          *ps++ = li>255 ? 255 : li;
529                  else {
530 <                        pv = *cs++ * li / tmTop->cdiv[RED];
530 >                        pv = *cs++ * li / tms->cdiv[RED];
531                          *ps++ = pv>255 ? 255 : pv;
532 <                        pv = *cs++ * li / tmTop->cdiv[GRN];
532 >                        pv = *cs++ * li / tms->cdiv[GRN];
533                          *ps++ = pv>255 ? 255 : pv;
534 <                        pv = *cs++ * li / tmTop->cdiv[BLU];
534 >                        pv = *cs++ * li / tms->cdiv[BLU];
535                          *ps++ = pv>255 ? 255 : pv;
536                  }
537          }
# Line 523 | Line 539 | int    len;
539   }
540  
541  
526 struct tmStruct *
527 tmPop()                         /* pop top tone mapping off stack */
528 {
529        register struct tmStruct        *tms;
542  
531        if ((tms = tmTop) != NULL)
532                tmTop = tms->tmprev;
533        return(tms);
534 }
543  
544 <
545 < int
546 < tmPull(tms)                     /* pull a tone mapping from stack */
547 < register struct tmStruct        *tms;
544 > TMstruct *
545 > tmDup(                          /* duplicate top tone mapping */
546 > TMstruct        *tms
547 > )
548   {
541        register struct tmStruct        *tms2;
542                                        /* special cases first */
543        if ((tms == NULL) | (tmTop == NULL))
544                return(0);
545        if (tms == tmTop) {
546                tmTop = tms->tmprev;
547                tms->tmprev = NULL;
548                return(1);
549        }
550        for (tms2 = tmTop; tms2->tmprev != NULL; tms2 = tms2->tmprev)
551                if (tms == tms2->tmprev) {      /* remove it */
552                        tms2->tmprev = tms->tmprev;
553                        tms->tmprev = NULL;
554                        return(1);
555                }
556        return(0);                      /* not found on stack */
557 }
558
559
560 struct tmStruct *
561 tmDup()                         /* duplicate top tone mapping */
562 {
549          int     len;
550 <        register int    i;
551 <        register struct tmStruct        *tmnew;
550 >        int     i;
551 >        TMstruct        *tmnew;
552  
553 <        if (tmTop == NULL)              /* anything to duplicate? */
553 >        if (tms == NULL)                /* anything to duplicate? */
554                  return(NULL);
555 <        tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct));
555 >        tmnew = (TMstruct *)malloc(sizeof(TMstruct));
556          if (tmnew == NULL)
557                  return(NULL);
558 <        *tmnew = *tmTop;                /* copy everything */
558 >        *tmnew = *tms;          /* copy everything */
559          if (tmnew->histo != NULL) {     /* duplicate histogram */
560                  len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
561                                  (tmnew->hbrmin-MINBRT)/HISTEP;
562                  tmnew->histo = (int *)malloc(len*sizeof(int));
563                  if (tmnew->histo != NULL)
564                          for (i = len; i--; )
565 <                                tmnew->histo[i] = tmTop->histo[i];
565 >                                tmnew->histo[i] = tms->histo[i];
566          }
567          if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */
568                  len = tmnew->mbrmax-tmnew->mbrmin+1;
# Line 584 | Line 570 | tmDup()                                /* duplicate top tone mapping */
570                                                  len*sizeof(unsigned short) );
571                  if (tmnew->lumap != NULL)
572                          for (i = len; i--; )
573 <                                tmnew->lumap[i] = tmTop->lumap[i];
573 >                                tmnew->lumap[i] = tms->lumap[i];
574          }
575                                          /* clear package data */
576          for (i = tmNumPkgs; i--; )
577                  tmnew->pd[i] = NULL;
578 <        tmnew->tmprev = tmTop;          /* make copy current */
579 <        return(tmTop = tmnew);
578 >                                        /* return copy */
579 >        return(tmnew);
580   }
581  
582  
597 int
598 tmPush(tms)                     /* push tone mapping on top of stack */
599 register struct tmStruct        *tms;
600 {
601        static char     funcName[] = "tmPush";
602                                        /* check validity */
603        if (tms == NULL)
604                returnErr(TM_E_ILLEGAL);
605        if (tms == tmTop)               /* check necessity */
606                returnOK;
607                                        /* pull if already in stack */
608        (void)tmPull(tms);
609                                        /* push it on top */
610        tms->tmprev = tmTop;
611        tmTop = tms;
612        returnOK;
613 }
614
615
583   void
584   tmDone(tms)                     /* done with tone mapping -- destroy it */
585 < register struct tmStruct        *tms;
585 > TMstruct        *tms;
586   {
587 <        register int    i;
588 <                                        /* NULL arg. is equiv. to tmTop */
589 <        if (tms == NULL && (tms = tmTop) == NULL)
587 >        int     i;
588 >                                        /* NULL arg. is equiv. to tms */
589 >        if (tms == NULL)
590                  return;
624                                        /* take out of stack if present */
625        (void)tmPull(tms);
591                                          /* free tables */
592          if (tms->histo != NULL)
593                  free((MEM_PTR)tms->histo);
# Line 642 | Line 607 | BYTE   tmMesofact[BMESUPPER-BMESLOWER];
607   void
608   tmMkMesofact()                          /* build mesopic lookup factor table */
609   {
610 <        register int    i;
610 >        int     i;
611  
612          if (tmMesofact[BMESUPPER-BMESLOWER-1])
613                  return;
# Line 655 | Line 620 | tmMkMesofact()                         /* build mesopic lookup factor table
620  
621  
622   int
623 < tmErrorReturn(func, err)                /* error return (with message) */
624 < char    *func;
625 < int     err;
623 > tmErrorReturn(                          /* error return (with message) */
624 > const char      *func,
625 > TMstruct        *tms,
626 > int     err
627 > )
628   {
629 <        tmLastFunction = func;
630 <        tmLastError = err;
631 <        if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR)
632 <                return(err);
629 >        if (tms != NULL) {
630 >                tms->lastFunc = func;
631 >                tms->lastError = err;
632 >                if (tms->flags & TM_F_NOSTDERR)
633 >                        return(err);
634 >        }
635          fputs(func, stderr);
636          fputs(": ", stderr);
637          fputs(tmErrorMessage[err], stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines