ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.c
Revision: 3.23
Committed: Wed May 10 15:21:58 2006 UTC (18 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.22: +110 -67 lines
Log Message:
Made COLOR conversion faster with lookup and fixed scale bug in tmCvGrays()

File Contents

# User Rev Content
1 greg 3.1 #ifndef lint
2 greg 3.23 static const char RCSid[] = "$Id: tonemap.c,v 3.22 2006/03/23 22:01:43 greg Exp $";
3 greg 3.1 #endif
4     /*
5     * Tone mapping functions.
6     * See tonemap.h for detailed function descriptions.
7 greg 3.9 * Added von Kries white-balance calculations 10/01 (GW).
8     *
9     * Externals declared in tonemap.h
10     */
11    
12 greg 3.10 #include "copyright.h"
13 greg 3.1
14     #include <stdio.h>
15     #include <math.h>
16     #include "tmprivat.h"
17     #include "tmerrmsg.h"
18    
19     #define exp10(x) exp(M_LN10*(x))
20    
21 greg 3.4 /* our list of conversion packages */
22     struct tmPackage *tmPkg[TM_MAXPKG];
23     int tmNumPkgs = 0; /* number of registered packages */
24 greg 3.1
25 greg 3.23 /* luminance->brightness lookup */
26     static TMbright *tmFloat2BrtLUT;
27    
28     #define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15]
29    
30 gregl 3.5
31 greg 3.16 TMstruct *
32     tmInit( /* initialize new tone mapping */
33     int flags,
34     RGBPRIMP monpri,
35     double gamval
36     )
37 greg 3.1 {
38     COLORMAT cmat;
39 greg 3.16 TMstruct *tmnew;
40     int i;
41 greg 3.1 /* allocate structure */
42 greg 3.16 tmnew = (TMstruct *)malloc(sizeof(TMstruct));
43 greg 3.1 if (tmnew == NULL)
44     return(NULL);
45    
46     tmnew->flags = flags & ~TM_F_UNIMPL;
47 greg 3.15 if (tmnew->flags & TM_F_BW)
48     tmnew->flags &= ~TM_F_MESOPIC;
49 greg 3.1 /* set monitor transform */
50     if (monpri == NULL || monpri == stdprims || tmnew->flags & TM_F_BW) {
51     tmnew->monpri = stdprims;
52     tmnew->clf[RED] = rgb2xyzmat[1][0];
53     tmnew->clf[GRN] = rgb2xyzmat[1][1];
54     tmnew->clf[BLU] = rgb2xyzmat[1][2];
55     } else {
56 greg 3.9 comprgb2xyzWBmat(cmat, tmnew->monpri=monpri);
57 greg 3.1 tmnew->clf[RED] = cmat[1][0];
58     tmnew->clf[GRN] = cmat[1][1];
59     tmnew->clf[BLU] = cmat[1][2];
60     }
61     /* set gamma value */
62     if (gamval < MINGAM)
63     tmnew->mongam = DEFGAM;
64     else
65     tmnew->mongam = gamval;
66 greg 3.4 /* set color divisors */
67     for (i = 0; i < 3; i++)
68     tmnew->cdiv[i] = 256.*pow(tmnew->clf[i], 1./tmnew->mongam);
69    
70 greg 3.1 /* set input transform */
71     tmnew->inppri = tmnew->monpri;
72     tmnew->cmat[0][0] = tmnew->cmat[1][1] = tmnew->cmat[2][2] =
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 greg 3.18 tmnew->inpdat = NULL;
77 greg 3.9 tmnew->hbrmin = 10; tmnew->hbrmax = -10;
78 greg 3.1 tmnew->histo = NULL;
79 greg 3.9 tmnew->mbrmin = 10; tmnew->mbrmax = -10;
80 greg 3.1 tmnew->lumap = NULL;
81 greg 3.4 /* zero private data */
82     for (i = TM_MAXPKG; i--; )
83     tmnew->pd[i] = NULL;
84 greg 3.17 tmnew->lastError = TM_E_OK;
85     tmnew->lastFunc = "NoErr";
86 greg 3.16 /* return new TMstruct */
87     return(tmnew);
88 greg 3.1 }
89    
90    
91     int
92 greg 3.16 tmSetSpace( /* set input color space for conversions */
93     TMstruct *tms,
94     RGBPRIMP pri,
95 greg 3.18 double sf,
96     MEM_PTR dat
97 greg 3.16 )
98 greg 3.1 {
99 greg 3.17 static const char funcName[] = "tmSetSpace";
100 greg 3.16 int i, j;
101 greg 3.1 /* error check */
102 greg 3.16 if (tms == NULL)
103 greg 3.1 returnErr(TM_E_TMINVAL);
104     if (sf <= 1e-12)
105     returnErr(TM_E_ILLEGAL);
106     /* check if no change */
107 greg 3.18 if (pri == tms->inppri && FEQ(sf, tms->inpsf) && dat == tms->inpdat)
108 greg 3.1 returnOK;
109 greg 3.16 tms->inppri = pri; /* let's set it */
110     tms->inpsf = sf;
111 greg 3.18 tms->inpdat = dat;
112 greg 3.1
113 greg 3.16 if (tms->flags & TM_F_BW) { /* color doesn't matter */
114     tms->monpri = tms->inppri; /* eliminate xform */
115     if (tms->inppri == TM_XYZPRIM) {
116     tms->clf[CIEX] = tms->clf[CIEZ] = 0.;
117     tms->clf[CIEY] = 1.;
118 greg 3.1 } else {
119 greg 3.16 comprgb2xyzWBmat(tms->cmat, tms->monpri);
120     tms->clf[RED] = tms->cmat[1][0];
121     tms->clf[GRN] = tms->cmat[1][1];
122     tms->clf[BLU] = tms->cmat[1][2];
123     }
124     tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] =
125     tms->inpsf;
126     tms->cmat[0][1] = tms->cmat[0][2] = tms->cmat[1][0] =
127     tms->cmat[1][2] = tms->cmat[2][0] = tms->cmat[2][1] = 0.;
128 greg 3.1
129 greg 3.16 } else if (tms->inppri == TM_XYZPRIM) /* input is XYZ */
130     compxyz2rgbWBmat(tms->cmat, tms->monpri);
131 greg 3.1
132     else { /* input is RGB */
133 greg 3.16 if (tms->inppri != tms->monpri &&
134     PRIMEQ(tms->inppri, tms->monpri))
135     tms->inppri = tms->monpri; /* no xform */
136     comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri);
137 greg 3.1 }
138     for (i = 0; i < 3; i++)
139     for (j = 0; j < 3; j++)
140 greg 3.16 tms->cmat[i][j] *= tms->inpsf;
141 greg 3.4 /* set color divisors */
142     for (i = 0; i < 3; i++)
143 greg 3.16 if (tms->clf[i] > .001)
144     tms->cdiv[i] =
145     256.*pow(tms->clf[i], 1./tms->mongam);
146 greg 3.4 else
147 greg 3.16 tms->cdiv[i] = 1;
148 greg 3.4 /* notify packages */
149     for (i = tmNumPkgs; i--; )
150 greg 3.16 if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL)
151     (*tmPkg[i]->NewSpace)(tms);
152 greg 3.1 returnOK;
153     }
154    
155    
156     void
157 greg 3.23 tmClearHisto( /* clear current histogram */
158 greg 3.16 TMstruct *tms
159     )
160 greg 3.1 {
161 greg 3.16 if (tms == NULL || tms->histo == NULL)
162 greg 3.1 return;
163 greg 3.16 free((MEM_PTR)tms->histo);
164     tms->histo = NULL;
165 greg 3.1 }
166    
167    
168 greg 3.23 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 greg 3.1 int
254 greg 3.16 tmCvColors( /* convert float colors */
255     TMstruct *tms,
256     TMbright *ls,
257     BYTE *cs,
258     COLOR *scan,
259     int len
260     )
261 greg 3.1 {
262 greg 3.17 static const char funcName[] = "tmCvColors";
263 greg 3.12 static COLOR csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM};
264 greg 3.20 static BYTE gamtab[1024];
265     static double curgam = .0;
266 greg 3.1 COLOR cmon;
267 greg 3.23 float lum, slum, d;
268 greg 3.16 int i;
269 greg 3.1
270 greg 3.16 if (tms == NULL)
271 greg 3.1 returnErr(TM_E_TMINVAL);
272 schorsch 3.13 if ((ls == NULL) | (scan == NULL) | (len < 0))
273 greg 3.1 returnErr(TM_E_ILLEGAL);
274 greg 3.23 tmCvLums(NULL, NULL, 0); /* initialize */
275 greg 3.21 if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) {
276 greg 3.20 curgam = tms->mongam; /* (re)build table */
277     for (i = 1024; i--; )
278     gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam));
279     }
280 greg 3.1 for (i = len; i--; ) {
281 greg 3.16 if (tmNeedMatrix(tms)) { /* get monitor RGB */
282     colortrans(cmon, tms->cmat, scan[i]);
283 greg 3.15 } else {
284 greg 3.16 cmon[RED] = tms->inpsf*scan[i][RED];
285     cmon[GRN] = tms->inpsf*scan[i][GRN];
286     cmon[BLU] = tms->inpsf*scan[i][BLU];
287 greg 3.1 }
288 greg 3.23 #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 greg 3.22 #endif
297 greg 3.1 /* world luminance */
298 greg 3.16 lum = tms->clf[RED]*cmon[RED] +
299     tms->clf[GRN]*cmon[GRN] +
300     tms->clf[BLU]*cmon[BLU] ;
301 greg 3.23 if (lum <= TM_NOLUM) /* convert brightness */
302     ls[i] = TM_NOBRT;
303     else
304     ls[i] = tmCvLumLUfp(&lum);
305 greg 3.1 if (cs == TM_NOCHROM) /* no color? */
306     continue;
307 greg 3.16 if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) {
308 greg 3.1 slum = scotlum(cmon); /* mesopic adj. */
309     if (lum < LMESLOWER)
310     cmon[RED] = cmon[GRN] = cmon[BLU] = slum;
311     else {
312     d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER);
313 greg 3.16 if (tms->flags & TM_F_BW)
314 greg 3.1 cmon[RED] = cmon[GRN] =
315     cmon[BLU] = d*lum;
316     else
317     scalecolor(cmon, d);
318 greg 3.23 d = (1.f-d)*slum;
319 greg 3.1 cmon[RED] += d;
320     cmon[GRN] += d;
321     cmon[BLU] += d;
322     }
323 greg 3.16 } else if (tms->flags & TM_F_BW) {
324 greg 3.1 cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
325     }
326 greg 3.16 d = tms->clf[RED]*cmon[RED]/lum;
327 greg 3.23 cs[3*i ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
328 greg 3.16 d = tms->clf[GRN]*cmon[GRN]/lum;
329 greg 3.23 cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
330 greg 3.16 d = tms->clf[BLU]*cmon[BLU]/lum;
331 greg 3.23 cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
332 greg 3.1 }
333     returnOK;
334     }
335    
336    
337 greg 3.9 int
338 greg 3.16 tmAddHisto( /* add values to histogram */
339     TMstruct *tms,
340     TMbright *ls,
341     int len,
342     int wt
343     )
344 greg 3.1 {
345 greg 3.17 static const char funcName[] = "tmAddHisto";
346 greg 3.9 int oldorig=0, oldlen, horig, hlen;
347 greg 3.16 int i, j;
348 greg 3.1
349 greg 3.16 if (tms == NULL)
350 greg 3.1 returnErr(TM_E_TMINVAL);
351 gwlarson 3.8 if (len < 0)
352     returnErr(TM_E_ILLEGAL);
353     if (len == 0)
354     returnOK;
355 greg 3.1 /* first, grow limits */
356 greg 3.16 if (tms->histo == NULL) {
357 greg 3.1 for (i = len; i-- && ls[i] < MINBRT; )
358     ;
359     if (i < 0)
360     returnOK;
361 greg 3.16 tms->hbrmin = tms->hbrmax = ls[i];
362 greg 3.1 oldlen = 0;
363     } else {
364 greg 3.16 oldorig = (tms->hbrmin-MINBRT)/HISTEP;
365     oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
366 greg 3.1 }
367     for (i = len; i--; ) {
368     if ((j = ls[i]) < MINBRT)
369     continue;
370 greg 3.16 if (j < tms->hbrmin)
371     tms->hbrmin = j;
372     else if (j > tms->hbrmax)
373     tms->hbrmax = j;
374 greg 3.1 }
375 greg 3.16 horig = (tms->hbrmin-MINBRT)/HISTEP;
376     hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
377 greg 3.1 if (hlen > oldlen) { /* (re)allocate histogram */
378 greg 3.16 int *newhist = (int *)calloc(hlen, sizeof(int));
379 greg 3.1 if (newhist == NULL)
380     returnErr(TM_E_NOMEM);
381     if (oldlen) { /* copy and free old */
382 greg 3.2 for (i = oldlen, j = i+oldorig-horig; i; )
383 greg 3.16 newhist[--j] = tms->histo[--i];
384     free((MEM_PTR)tms->histo);
385 greg 3.1 }
386 greg 3.16 tms->histo = newhist;
387 greg 3.1 }
388     if (wt == 0)
389     returnOK;
390     for (i = len; i--; ) /* add in new counts */
391     if (ls[i] >= MINBRT)
392 greg 3.16 tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
393 greg 3.1 returnOK;
394     }
395    
396    
397     static double
398 greg 3.16 htcontrs( /* human threshold contrast sensitivity, dL(La) */
399     double La
400     )
401 greg 3.1 {
402     double l10La, l10dL;
403     /* formula taken from Ferwerda et al. [SG96] */
404     if (La < 1.148e-4)
405     return(1.38e-3);
406     l10La = log10(La);
407     if (l10La < -1.44) /* rod response regime */
408     l10dL = pow(.405*l10La + 1.6, 2.18) - 2.86;
409     else if (l10La < -.0184)
410     l10dL = l10La - .395;
411     else if (l10La < 1.9) /* cone response regime */
412     l10dL = pow(.249*l10La + .65, 2.7) - .72;
413     else
414     l10dL = l10La - 1.255;
415    
416     return(exp10(l10dL));
417     }
418    
419    
420 greg 3.9 static int
421 greg 3.16 tmNewMap( /* allocate new tone-mapping array */
422     TMstruct *tms
423     )
424 greg 3.9 {
425 greg 3.16 if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) !=
426     (tms->hbrmax - tms->hbrmin)) {
427     free((MEM_PTR)tms->lumap);
428     tms->lumap = NULL;
429     }
430     tms->mbrmin = tms->hbrmin;
431     tms->mbrmax = tms->hbrmax;
432     if (tms->mbrmin > tms->mbrmax)
433 greg 3.9 return 0;
434 greg 3.16 if (tms->lumap == NULL)
435     tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)*
436     (tms->mbrmax-tms->mbrmin+1));
437     return(tms->lumap != NULL);
438 greg 3.9 }
439    
440    
441     int
442 greg 3.16 tmFixedMapping( /* compute fixed, linear tone-mapping */
443     TMstruct *tms,
444     double expmult,
445     double gamval
446     )
447 greg 3.9 {
448 greg 3.17 static const char funcName[] = "tmFixedMapping";
449 greg 3.9 double d;
450 greg 3.16 int i;
451 greg 3.9
452 greg 3.16 if (!tmNewMap(tms))
453 greg 3.9 returnErr(TM_E_NOMEM);
454     if (expmult <= .0)
455     expmult = 1.;
456     if (gamval < MINGAM)
457 greg 3.16 gamval = tms->mongam;
458     d = log(expmult/tms->inpsf);
459     for (i = tms->mbrmax-tms->mbrmin+1; i--; )
460     tms->lumap[i] = 256. * exp(
461     ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) )
462 greg 3.9 / gamval );
463     returnOK;
464     }
465    
466    
467 greg 3.1 int
468 greg 3.16 tmComputeMapping( /* compute histogram tone-mapping */
469     TMstruct *tms,
470     double gamval,
471     double Lddyn,
472     double Ldmax
473     )
474 greg 3.1 {
475 greg 3.17 static const char funcName[] = "tmComputeMapping";
476 greg 3.1 int *histo;
477     float *cumf;
478 greg 3.9 int brt0, histlen, threshold, ceiling, trimmings;
479 greg 3.1 double logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
480 greg 3.11 int32 histot;
481 greg 3.9 double sum;
482 greg 3.16 double d;
483     int i, j;
484 greg 3.1
485 greg 3.16 if (tms == NULL || tms->histo == NULL)
486 greg 3.1 returnErr(TM_E_TMINVAL);
487     /* check arguments */
488     if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
489     if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
490 greg 3.16 if (gamval < MINGAM) gamval = tms->mongam;
491 greg 3.1 /* compute handy values */
492     Ldmin = Ldmax/Lddyn;
493     logLddyn = log(Lddyn);
494     Ldavg = sqrt(Ldmax*Ldmin);
495 greg 3.16 i = (tms->hbrmin-MINBRT)/HISTEP;
496 greg 3.1 brt0 = MINBRT + HISTEP/2 + i*HISTEP;
497 greg 3.16 histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
498 greg 3.1 /* histogram total and mean */
499     histot = 0; sum = 0;
500     j = brt0 + histlen*HISTEP;
501     for (i = histlen; i--; ) {
502 greg 3.16 histot += tms->histo[i];
503     sum += (j -= HISTEP) * tms->histo[i];
504 greg 3.1 }
505 greg 3.14 threshold = histot*0.005 + .5;
506 greg 3.1 if (threshold < 4)
507     returnErr(TM_E_TMFAIL);
508     Lwavg = tmLuminance( (double)sum / histot );
509 greg 3.9 /* allocate space for mapping */
510 greg 3.16 if (!tmNewMap(tms))
511 greg 3.9 returnErr(TM_E_NOMEM);
512     /* use linear tone mapping? */
513 greg 3.16 if (tms->flags & TM_F_LINEAR)
514 greg 3.9 goto linearmap;
515     /* clamp histogram */
516     histo = (int *)malloc(histlen*sizeof(int));
517     cumf = (float *)malloc((histlen+2)*sizeof(float));
518 schorsch 3.13 if ((histo == NULL) | (cumf == NULL))
519 greg 3.9 returnErr(TM_E_NOMEM);
520     cumf[histlen+1] = 1.; /* guard for assignment code */
521     for (i = histlen; i--; ) /* make malleable copy */
522 greg 3.16 histo[i] = tms->histo[i];
523 greg 3.9 do { /* iterate to solution */
524     sum = 0; /* cumulative probability */
525     for (i = 0; i < histlen; i++) {
526     cumf[i] = (double)sum/histot;
527     sum += histo[i];
528     }
529     cumf[histlen] = 1.;
530 greg 3.16 Tr = histot * (double)(tms->hbrmax - tms->hbrmin) /
531 greg 3.9 ((double)histlen*TM_BRTSCALE) / logLddyn;
532     ceiling = Tr + 1.;
533     trimmings = 0; /* clip to envelope */
534     for (i = histlen; i--; ) {
535 greg 3.16 if (tms->flags & TM_F_HCONTR) {
536 greg 3.9 Lw = tmLuminance(brt0 + i*HISTEP);
537     Ld = Ldmin * exp( logLddyn *
538     .5*(cumf[i]+cumf[i+1]) );
539     ceiling = Tr * (htcontrs(Ld) * Lw) /
540     (htcontrs(Lw) * Ld) + 1.;
541 greg 3.2 }
542 greg 3.9 if (histo[i] > ceiling) {
543     trimmings += histo[i] - ceiling;
544     histo[i] = ceiling;
545 greg 3.1 }
546     }
547 greg 3.9 /* check if we're out of data */
548     if ((histot -= trimmings) <= threshold) {
549     free((MEM_PTR)histo);
550     free((MEM_PTR)cumf);
551     goto linearmap;
552     }
553     } while (trimmings > threshold);
554     /* assign tone-mapping */
555 greg 3.16 for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
556     j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
557 greg 3.9 d -= (double)j;
558     Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
559     d = (Ld - Ldmin)/(Ldmax - Ldmin);
560 greg 3.16 tms->lumap[i] = 256.*pow(d, 1./gamval);
561 greg 3.1 }
562 greg 3.9 free((MEM_PTR)histo); /* clean up and return */
563     free((MEM_PTR)cumf);
564 greg 3.1 returnOK;
565 greg 3.9 linearmap: /* linear tone-mapping */
566 greg 3.16 if (tms->flags & TM_F_HCONTR)
567 greg 3.9 d = htcontrs(Ldavg) / htcontrs(Lwavg);
568     else
569     d = Ldavg / Lwavg;
570 greg 3.16 return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
571 greg 3.1 }
572    
573    
574     int
575 greg 3.16 tmMapPixels( /* apply tone-mapping to pixel(s) */
576     TMstruct *tms,
577     BYTE *ps,
578     TMbright *ls,
579     BYTE *cs,
580     int len
581     )
582 greg 3.1 {
583 greg 3.17 static const char funcName[] = "tmMapPixels";
584 greg 3.16 int32 li, pv;
585 greg 3.1
586 greg 3.16 if (tms == NULL || tms->lumap == NULL)
587 greg 3.1 returnErr(TM_E_TMINVAL);
588 schorsch 3.13 if ((ps == NULL) | (ls == NULL) | (len < 0))
589 greg 3.1 returnErr(TM_E_ILLEGAL);
590     while (len--) {
591 greg 3.16 if ((li = *ls++) < tms->mbrmin) {
592 greg 3.9 li = 0;
593     } else {
594 greg 3.16 if (li > tms->mbrmax)
595     li = tms->mbrmax;
596     li = tms->lumap[li - tms->mbrmin];
597 greg 3.9 }
598 greg 3.1 if (cs == TM_NOCHROM)
599     *ps++ = li>255 ? 255 : li;
600     else {
601 greg 3.16 pv = *cs++ * li / tms->cdiv[RED];
602 greg 3.1 *ps++ = pv>255 ? 255 : pv;
603 greg 3.16 pv = *cs++ * li / tms->cdiv[GRN];
604 greg 3.1 *ps++ = pv>255 ? 255 : pv;
605 greg 3.16 pv = *cs++ * li / tms->cdiv[BLU];
606 greg 3.1 *ps++ = pv>255 ? 255 : pv;
607     }
608     }
609     returnOK;
610     }
611    
612    
613    
614    
615 greg 3.16 TMstruct *
616     tmDup( /* duplicate top tone mapping */
617     TMstruct *tms
618     )
619 greg 3.3 {
620     int len;
621 greg 3.16 int i;
622     TMstruct *tmnew;
623 greg 3.3
624 greg 3.16 if (tms == NULL) /* anything to duplicate? */
625 greg 3.3 return(NULL);
626 greg 3.16 tmnew = (TMstruct *)malloc(sizeof(TMstruct));
627 greg 3.3 if (tmnew == NULL)
628     return(NULL);
629 greg 3.16 *tmnew = *tms; /* copy everything */
630 greg 3.3 if (tmnew->histo != NULL) { /* duplicate histogram */
631 gregl 3.6 len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
632     (tmnew->hbrmin-MINBRT)/HISTEP;
633 greg 3.3 tmnew->histo = (int *)malloc(len*sizeof(int));
634     if (tmnew->histo != NULL)
635     for (i = len; i--; )
636 greg 3.16 tmnew->histo[i] = tms->histo[i];
637 greg 3.3 }
638     if (tmnew->lumap != NULL) { /* duplicate luminance mapping */
639 gregl 3.6 len = tmnew->mbrmax-tmnew->mbrmin+1;
640 greg 3.3 tmnew->lumap = (unsigned short *)malloc(
641     len*sizeof(unsigned short) );
642     if (tmnew->lumap != NULL)
643     for (i = len; i--; )
644 greg 3.16 tmnew->lumap[i] = tms->lumap[i];
645 greg 3.3 }
646 greg 3.4 /* clear package data */
647     for (i = tmNumPkgs; i--; )
648     tmnew->pd[i] = NULL;
649 greg 3.16 /* return copy */
650     return(tmnew);
651 greg 3.1 }
652    
653    
654     void
655     tmDone(tms) /* done with tone mapping -- destroy it */
656 greg 3.16 TMstruct *tms;
657 greg 3.1 {
658 greg 3.16 int i;
659     /* NULL arg. is equiv. to tms */
660     if (tms == NULL)
661 greg 3.1 return;
662     /* free tables */
663     if (tms->histo != NULL)
664 greg 3.4 free((MEM_PTR)tms->histo);
665 greg 3.1 if (tms->lumap != NULL)
666 greg 3.4 free((MEM_PTR)tms->lumap);
667     /* free private data */
668     for (i = tmNumPkgs; i--; )
669     if (tms->pd[i] != NULL)
670     (*tmPkg[i]->Free)(tms->pd[i]);
671     free((MEM_PTR)tms); /* free basic structure */
672 greg 3.9 }
673    
674     /******************** Shared but Private library routines *********************/
675    
676     BYTE tmMesofact[BMESUPPER-BMESLOWER];
677    
678     void
679     tmMkMesofact() /* build mesopic lookup factor table */
680     {
681 greg 3.16 int i;
682 greg 3.9
683     if (tmMesofact[BMESUPPER-BMESLOWER-1])
684     return;
685    
686     for (i = BMESLOWER; i < BMESUPPER; i++)
687     tmMesofact[i-BMESLOWER] = 256. *
688     (tmLuminance(i) - LMESLOWER) /
689     (LMESUPPER - LMESLOWER);
690     }
691    
692    
693     int
694 greg 3.16 tmErrorReturn( /* error return (with message) */
695 greg 3.17 const char *func,
696 greg 3.16 TMstruct *tms,
697     int err
698     )
699 greg 3.9 {
700 greg 3.17 if (tms != NULL) {
701     tms->lastFunc = func;
702     tms->lastError = err;
703     if (tms->flags & TM_F_NOSTDERR)
704     return(err);
705     }
706 greg 3.9 fputs(func, stderr);
707     fputs(": ", stderr);
708     fputs(tmErrorMessage[err], stderr);
709     fputs("!\n", stderr);
710     return(err);
711 greg 3.1 }