ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.c
Revision: 3.35
Committed: Mon Mar 7 20:49:19 2011 UTC (13 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.34: +2 -4 lines
Log Message:
Minor improvement in round-off method

File Contents

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