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.17 by greg, Fri Jan 7 21:41:06 2005 UTC vs.
Revision 3.23 by greg, Wed May 10 15:21:58 2006 UTC

# Line 22 | Line 22 | static const char      RCSid[] = "$Id$";
22   struct tmPackage        *tmPkg[TM_MAXPKG];
23   int     tmNumPkgs = 0;                  /* number of registered packages */
24  
25 +                                        /* luminance->brightness lookup */
26 + static TMbright         *tmFloat2BrtLUT;
27  
28 + #define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15]
29 +
30 +
31   TMstruct *
32   tmInit(                                 /* initialize new tone mapping */
33   int     flags,
# Line 68 | Line 73 | double gamval
73                          tmnew->inpsf = WHTEFFICACY;
74          tmnew->cmat[0][1] = tmnew->cmat[0][2] = tmnew->cmat[1][0] =
75          tmnew->cmat[1][2] = tmnew->cmat[2][0] = tmnew->cmat[2][1] = 0.;
76 +        tmnew->inpdat = NULL;
77          tmnew->hbrmin = 10; tmnew->hbrmax = -10;
78          tmnew->histo = NULL;
79          tmnew->mbrmin = 10; tmnew->mbrmax = -10;
# Line 86 | Line 92 | int
92   tmSetSpace(                     /* set input color space for conversions */
93   TMstruct        *tms,
94   RGBPRIMP        pri,
95 < double  sf
95 > double  sf,
96 > MEM_PTR dat
97   )
98   {
99          static const char funcName[] = "tmSetSpace";
# Line 97 | Line 104 | double sf
104          if (sf <= 1e-12)
105                  returnErr(TM_E_ILLEGAL);
106                                                  /* check if no change */
107 <        if (pri == tms->inppri && FEQ(sf, tms->inpsf))
107 >        if (pri == tms->inppri && FEQ(sf, tms->inpsf) && dat == tms->inpdat)
108                  returnOK;
109          tms->inppri = pri;                      /* let's set it */
110          tms->inpsf = sf;
111 +        tms->inpdat = dat;
112  
113          if (tms->flags & TM_F_BW) {             /* color doesn't matter */
114                  tms->monpri = tms->inppri;              /* eliminate xform */
# Line 146 | Line 154 | double sf
154  
155  
156   void
157 < tmClearHisto(                   /* clear current histogram */
157 > tmClearHisto(                           /* clear current histogram */
158   TMstruct        *tms
159   )
160   {
# Line 157 | Line 165 | TMstruct       *tms
165   }
166  
167  
168 + TMbright
169 + tmCvLuminance(                          /* convert a single luminance */
170 + double  lum
171 + )
172 + {
173 +        double  d;
174 +
175 + #ifdef isfinite
176 +        if (!isfinite(lum) || lum <= TM_NOLUM)
177 + #else
178 +        if (lum <= TM_NOLUM)
179 + #endif
180 +                return(TM_NOBRT);
181 +        d = TM_BRTSCALE*log(lum);
182 +        if (d > 0.)
183 +                return((TMbright)(d+.5));
184 +        return((TMbright)(d-.5));
185 + }
186 +
187 +
188   int
189 + tmCvLums(                               /* convert luminances using lookup */
190 + TMbright        *ls,
191 + float           *scan,
192 + int             len
193 + )
194 + {
195 +        if (tmFloat2BrtLUT == NULL) {   /* initialize lookup table */
196 +                int32   i;
197 +                tmFloat2BrtLUT = (TMbright *)malloc(sizeof(TMbright)*0x10000);
198 +                if (tmFloat2BrtLUT == NULL)
199 +                        return(TM_E_NOMEM);
200 +                for (i = 0; i < 0x10000; i++) {
201 +                        int32   l = (i<<1 | 1) << 14;
202 + #ifndef isfinite
203 +                        if ((l & 0x7f800000) == 0x7f800000)
204 +                                tmFloat2BrtLUT[i] = TM_NOBRT;
205 +                        else
206 + #endif
207 +                        tmFloat2BrtLUT[i] = tmCvLuminance(*(float *)&l);
208 +                }
209 +        }
210 +        if (len <= 0)
211 +                return(TM_E_OK);
212 +        if ((ls == NULL) | (scan == NULL))
213 +                return(TM_E_ILLEGAL);
214 +        while (len--) {
215 +                if (*scan <= TM_NOLUM) {
216 +                        *ls++ = TM_NOBRT;
217 +                        ++scan;
218 +                        continue;
219 +                }
220 +                *ls++ = tmCvLumLUfp(scan++);
221 +        }
222 +        return(TM_E_OK);
223 + }
224 +
225 +
226 + int
227 + tmCvGrays(                              /* convert float gray values */
228 + TMstruct        *tms,
229 + TMbright        *ls,
230 + float           *scan,
231 + int             len
232 + )
233 + {
234 +        static const char funcName[] = "tmCvGrays";
235 +        double  d;
236 +        int     i;
237 +
238 +        if (tms == NULL)
239 +                returnErr(TM_E_TMINVAL);
240 +        if ((ls == NULL) | (scan == NULL) | (len < 0))
241 +                returnErr(TM_E_ILLEGAL);
242 +        for (i = len; i--; ) {
243 +                float   lum = tms->inpsf * scan[i];
244 +                if (lum <= TM_NOLUM)
245 +                        ls[i] = TM_NOBRT;
246 +                else
247 +                        ls[i] = tmCvLumLUfp(&lum);
248 +        }
249 +        returnOK;
250 + }
251 +
252 +
253 + int
254   tmCvColors(                             /* convert float colors */
255   TMstruct        *tms,
256   TMbright        *ls,
# Line 168 | Line 261 | int    len
261   {
262          static const char funcName[] = "tmCvColors";
263          static COLOR    csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
264 +        static BYTE     gamtab[1024];
265 +        static double   curgam = .0;
266          COLOR   cmon;
267 <        double  lum, slum;
173 <        double  d;
267 >        float   lum, slum, d;
268          int     i;
269  
270          if (tms == NULL)
271                  returnErr(TM_E_TMINVAL);
272          if ((ls == NULL) | (scan == NULL) | (len < 0))
273                  returnErr(TM_E_ILLEGAL);
274 +        tmCvLums(NULL, NULL, 0);                        /* initialize */
275 +        if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
276 +                curgam = tms->mongam;                   /* (re)build table */
277 +                for (i = 1024; i--; )
278 +                        gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam));
279 +        }
280          for (i = len; i--; ) {
281                  if (tmNeedMatrix(tms)) {                /* get monitor RGB */
282                          colortrans(cmon, tms->cmat, scan[i]);
# Line 185 | Line 285 | int    len
285                          cmon[GRN] = tms->inpsf*scan[i][GRN];
286                          cmon[BLU] = tms->inpsf*scan[i][BLU];
287                  }
288 + #ifdef isfinite
289 +                if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f;
290 +                if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f;
291 +                if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f;
292 + #else
293 +                if (cmon[RED] < .0f) cmon[RED] = .0f;
294 +                if (cmon[GRN] < .0f) cmon[GRN] = .0f;
295 +                if (cmon[BLU] < .0f) cmon[BLU] = .0f;
296 + #endif
297                                                          /* world luminance */
298                  lum =   tms->clf[RED]*cmon[RED] +
299                          tms->clf[GRN]*cmon[GRN] +
300                          tms->clf[BLU]*cmon[BLU] ;
301 <                                                        /* check range */
302 <                if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite))
303 <                        lum =   tms->clf[RED]*cmon[RED] +
304 <                                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;
200 <                } else {
201 <                        d = TM_BRTSCALE*log(lum);       /* encode it */
202 <                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
203 <                }
301 >                if (lum <= TM_NOLUM)                    /* convert brightness */
302 >                        ls[i] = TM_NOBRT;
303 >                else
304 >                        ls[i] = tmCvLumLUfp(&lum);
305                  if (cs == TM_NOCHROM)                   /* no color? */
306                          continue;
307                  if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
# Line 214 | Line 315 | int    len
315                                                          cmon[BLU] = d*lum;
316                                  else
317                                          scalecolor(cmon, d);
318 <                                d = (1.-d)*slum;
318 >                                d = (1.f-d)*slum;
319                                  cmon[RED] += d;
320                                  cmon[GRN] += d;
321                                  cmon[BLU] += d;
# Line 223 | Line 324 | int    len
324                          cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
325                  }
326                  d = tms->clf[RED]*cmon[RED]/lum;
327 <                cs[3*i  ] = d>=.999 ? 255 :
227 <                                (int)(256.*pow(d, 1./tms->mongam));
327 >                cs[3*i  ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
328                  d = tms->clf[GRN]*cmon[GRN]/lum;
329 <                cs[3*i+1] = d>=.999 ? 255 :
230 <                                (int)(256.*pow(d, 1./tms->mongam));
329 >                cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
330                  d = tms->clf[BLU]*cmon[BLU]/lum;
331 <                cs[3*i+2] = d>=.999 ? 255 :
233 <                                (int)(256.*pow(d, 1./tms->mongam));
331 >                cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
332          }
235        returnOK;
236 }
237
238
239 int
240 tmCvGrays(                              /* convert float gray values */
241 TMstruct        *tms,
242 TMbright        *ls,
243 float   *scan,
244 int     len
245 )
246 {
247        static const char funcName[] = "tmCvGrays";
248        double  d;
249        int     i;
250
251        if (tms == NULL)
252                returnErr(TM_E_TMINVAL);
253        if ((ls == NULL) | (scan == NULL) | (len < 0))
254                returnErr(TM_E_ILLEGAL);
255        for (i = len; i--; )
256                if (scan[i] <= TM_NOLUM) {
257                        ls[i] = TM_NOBRT;               /* bogus value */
258                } else {
259                        d = TM_BRTSCALE*log(scan[i]);   /* encode it */
260                        ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5);
261                }
333          returnOK;
334   }
335  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines