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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: tonemap.c,v 3.22 2006/03/23 22:01:43 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 <math.h>
16 #include "tmprivat.h"
17 #include "tmerrmsg.h"
18
19 #define exp10(x) exp(M_LN10*(x))
20
21 /* our list of conversion packages */
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,
34 RGBPRIMP monpri,
35 double gamval
36 )
37 {
38 COLORMAT cmat;
39 TMstruct *tmnew;
40 int i;
41 /* allocate structure */
42 tmnew = (TMstruct *)malloc(sizeof(TMstruct));
43 if (tmnew == NULL)
44 return(NULL);
45
46 tmnew->flags = flags & ~TM_F_UNIMPL;
47 if (tmnew->flags & TM_F_BW)
48 tmnew->flags &= ~TM_F_MESOPIC;
49 /* 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 comprgb2xyzWBmat(cmat, tmnew->monpri=monpri);
57 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 /* set color divisors */
67 for (i = 0; i < 3; i++)
68 tmnew->cdiv[i] = 256.*pow(tmnew->clf[i], 1./tmnew->mongam);
69
70 /* 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 tmnew->inpdat = NULL;
77 tmnew->hbrmin = 10; tmnew->hbrmax = -10;
78 tmnew->histo = NULL;
79 tmnew->mbrmin = 10; tmnew->mbrmax = -10;
80 tmnew->lumap = NULL;
81 /* zero private data */
82 for (i = TM_MAXPKG; i--; )
83 tmnew->pd[i] = NULL;
84 tmnew->lastError = TM_E_OK;
85 tmnew->lastFunc = "NoErr";
86 /* return new TMstruct */
87 return(tmnew);
88 }
89
90
91 int
92 tmSetSpace( /* set input color space for conversions */
93 TMstruct *tms,
94 RGBPRIMP pri,
95 double sf,
96 MEM_PTR dat
97 )
98 {
99 static const char funcName[] = "tmSetSpace";
100 int i, j;
101 /* error check */
102 if (tms == NULL)
103 returnErr(TM_E_TMINVAL);
104 if (sf <= 1e-12)
105 returnErr(TM_E_ILLEGAL);
106 /* check if no change */
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 */
115 if (tms->inppri == TM_XYZPRIM) {
116 tms->clf[CIEX] = tms->clf[CIEZ] = 0.;
117 tms->clf[CIEY] = 1.;
118 } else {
119 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
129 } else if (tms->inppri == TM_XYZPRIM) /* input is XYZ */
130 compxyz2rgbWBmat(tms->cmat, tms->monpri);
131
132 else { /* input is RGB */
133 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 }
138 for (i = 0; i < 3; i++)
139 for (j = 0; j < 3; j++)
140 tms->cmat[i][j] *= tms->inpsf;
141 /* set color divisors */
142 for (i = 0; i < 3; i++)
143 if (tms->clf[i] > .001)
144 tms->cdiv[i] =
145 256.*pow(tms->clf[i], 1./tms->mongam);
146 else
147 tms->cdiv[i] = 1;
148 /* notify packages */
149 for (i = tmNumPkgs; i--; )
150 if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL)
151 (*tmPkg[i]->NewSpace)(tms);
152 returnOK;
153 }
154
155
156 void
157 tmClearHisto( /* clear current histogram */
158 TMstruct *tms
159 )
160 {
161 if (tms == NULL || tms->histo == NULL)
162 return;
163 free((MEM_PTR)tms->histo);
164 tms->histo = NULL;
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,
257 BYTE *cs,
258 COLOR *scan,
259 int len
260 )
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 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]);
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 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) {
308 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 if (tms->flags & TM_F_BW)
314 cmon[RED] = cmon[GRN] =
315 cmon[BLU] = d*lum;
316 else
317 scalecolor(cmon, d);
318 d = (1.f-d)*slum;
319 cmon[RED] += d;
320 cmon[GRN] += d;
321 cmon[BLU] += d;
322 }
323 } else if (tms->flags & TM_F_BW) {
324 cmon[RED] = cmon[GRN] = cmon[BLU] = lum;
325 }
326 d = tms->clf[RED]*cmon[RED]/lum;
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>=.999f ? 255 : gamtab[(int)(1024.f*d)];
330 d = tms->clf[BLU]*cmon[BLU]/lum;
331 cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)];
332 }
333 returnOK;
334 }
335
336
337 int
338 tmAddHisto( /* add values to histogram */
339 TMstruct *tms,
340 TMbright *ls,
341 int len,
342 int wt
343 )
344 {
345 static const char funcName[] = "tmAddHisto";
346 int oldorig=0, oldlen, horig, hlen;
347 int i, j;
348
349 if (tms == NULL)
350 returnErr(TM_E_TMINVAL);
351 if (len < 0)
352 returnErr(TM_E_ILLEGAL);
353 if (len == 0)
354 returnOK;
355 /* first, grow limits */
356 if (tms->histo == NULL) {
357 for (i = len; i-- && ls[i] < MINBRT; )
358 ;
359 if (i < 0)
360 returnOK;
361 tms->hbrmin = tms->hbrmax = ls[i];
362 oldlen = 0;
363 } else {
364 oldorig = (tms->hbrmin-MINBRT)/HISTEP;
365 oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig;
366 }
367 for (i = len; i--; ) {
368 if ((j = ls[i]) < MINBRT)
369 continue;
370 if (j < tms->hbrmin)
371 tms->hbrmin = j;
372 else if (j > tms->hbrmax)
373 tms->hbrmax = j;
374 }
375 horig = (tms->hbrmin-MINBRT)/HISTEP;
376 hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig;
377 if (hlen > oldlen) { /* (re)allocate histogram */
378 int *newhist = (int *)calloc(hlen, sizeof(int));
379 if (newhist == NULL)
380 returnErr(TM_E_NOMEM);
381 if (oldlen) { /* copy and free old */
382 for (i = oldlen, j = i+oldorig-horig; i; )
383 newhist[--j] = tms->histo[--i];
384 free((MEM_PTR)tms->histo);
385 }
386 tms->histo = newhist;
387 }
388 if (wt == 0)
389 returnOK;
390 for (i = len; i--; ) /* add in new counts */
391 if (ls[i] >= MINBRT)
392 tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt;
393 returnOK;
394 }
395
396
397 static double
398 htcontrs( /* human threshold contrast sensitivity, dL(La) */
399 double La
400 )
401 {
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 static int
421 tmNewMap( /* allocate new tone-mapping array */
422 TMstruct *tms
423 )
424 {
425 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 return 0;
434 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 }
439
440
441 int
442 tmFixedMapping( /* compute fixed, linear tone-mapping */
443 TMstruct *tms,
444 double expmult,
445 double gamval
446 )
447 {
448 static const char funcName[] = "tmFixedMapping";
449 double d;
450 int i;
451
452 if (!tmNewMap(tms))
453 returnErr(TM_E_NOMEM);
454 if (expmult <= .0)
455 expmult = 1.;
456 if (gamval < MINGAM)
457 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 / gamval );
463 returnOK;
464 }
465
466
467 int
468 tmComputeMapping( /* compute histogram tone-mapping */
469 TMstruct *tms,
470 double gamval,
471 double Lddyn,
472 double Ldmax
473 )
474 {
475 static const char funcName[] = "tmComputeMapping";
476 int *histo;
477 float *cumf;
478 int brt0, histlen, threshold, ceiling, trimmings;
479 double logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld;
480 int32 histot;
481 double sum;
482 double d;
483 int i, j;
484
485 if (tms == NULL || tms->histo == NULL)
486 returnErr(TM_E_TMINVAL);
487 /* check arguments */
488 if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
489 if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
490 if (gamval < MINGAM) gamval = tms->mongam;
491 /* compute handy values */
492 Ldmin = Ldmax/Lddyn;
493 logLddyn = log(Lddyn);
494 Ldavg = sqrt(Ldmax*Ldmin);
495 i = (tms->hbrmin-MINBRT)/HISTEP;
496 brt0 = MINBRT + HISTEP/2 + i*HISTEP;
497 histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i;
498 /* histogram total and mean */
499 histot = 0; sum = 0;
500 j = brt0 + histlen*HISTEP;
501 for (i = histlen; i--; ) {
502 histot += tms->histo[i];
503 sum += (j -= HISTEP) * tms->histo[i];
504 }
505 threshold = histot*0.005 + .5;
506 if (threshold < 4)
507 returnErr(TM_E_TMFAIL);
508 Lwavg = tmLuminance( (double)sum / histot );
509 /* allocate space for mapping */
510 if (!tmNewMap(tms))
511 returnErr(TM_E_NOMEM);
512 /* use linear tone mapping? */
513 if (tms->flags & TM_F_LINEAR)
514 goto linearmap;
515 /* clamp histogram */
516 histo = (int *)malloc(histlen*sizeof(int));
517 cumf = (float *)malloc((histlen+2)*sizeof(float));
518 if ((histo == NULL) | (cumf == NULL))
519 returnErr(TM_E_NOMEM);
520 cumf[histlen+1] = 1.; /* guard for assignment code */
521 for (i = histlen; i--; ) /* make malleable copy */
522 histo[i] = tms->histo[i];
523 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 Tr = histot * (double)(tms->hbrmax - tms->hbrmin) /
531 ((double)histlen*TM_BRTSCALE) / logLddyn;
532 ceiling = Tr + 1.;
533 trimmings = 0; /* clip to envelope */
534 for (i = histlen; i--; ) {
535 if (tms->flags & TM_F_HCONTR) {
536 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 }
542 if (histo[i] > ceiling) {
543 trimmings += histo[i] - ceiling;
544 histo[i] = ceiling;
545 }
546 }
547 /* 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 for (i = tms->mbrmax-tms->mbrmin+1; i--; ) {
556 j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen;
557 d -= (double)j;
558 Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1]));
559 d = (Ld - Ldmin)/(Ldmax - Ldmin);
560 tms->lumap[i] = 256.*pow(d, 1./gamval);
561 }
562 free((MEM_PTR)histo); /* clean up and return */
563 free((MEM_PTR)cumf);
564 returnOK;
565 linearmap: /* linear tone-mapping */
566 if (tms->flags & TM_F_HCONTR)
567 d = htcontrs(Ldavg) / htcontrs(Lwavg);
568 else
569 d = Ldavg / Lwavg;
570 return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval));
571 }
572
573
574 int
575 tmMapPixels( /* apply tone-mapping to pixel(s) */
576 TMstruct *tms,
577 BYTE *ps,
578 TMbright *ls,
579 BYTE *cs,
580 int len
581 )
582 {
583 static const char funcName[] = "tmMapPixels";
584 int32 li, pv;
585
586 if (tms == NULL || tms->lumap == NULL)
587 returnErr(TM_E_TMINVAL);
588 if ((ps == NULL) | (ls == NULL) | (len < 0))
589 returnErr(TM_E_ILLEGAL);
590 while (len--) {
591 if ((li = *ls++) < tms->mbrmin) {
592 li = 0;
593 } else {
594 if (li > tms->mbrmax)
595 li = tms->mbrmax;
596 li = tms->lumap[li - tms->mbrmin];
597 }
598 if (cs == TM_NOCHROM)
599 *ps++ = li>255 ? 255 : li;
600 else {
601 pv = *cs++ * li / tms->cdiv[RED];
602 *ps++ = pv>255 ? 255 : pv;
603 pv = *cs++ * li / tms->cdiv[GRN];
604 *ps++ = pv>255 ? 255 : pv;
605 pv = *cs++ * li / tms->cdiv[BLU];
606 *ps++ = pv>255 ? 255 : pv;
607 }
608 }
609 returnOK;
610 }
611
612
613
614
615 TMstruct *
616 tmDup( /* duplicate top tone mapping */
617 TMstruct *tms
618 )
619 {
620 int len;
621 int i;
622 TMstruct *tmnew;
623
624 if (tms == NULL) /* anything to duplicate? */
625 return(NULL);
626 tmnew = (TMstruct *)malloc(sizeof(TMstruct));
627 if (tmnew == NULL)
628 return(NULL);
629 *tmnew = *tms; /* copy everything */
630 if (tmnew->histo != NULL) { /* duplicate histogram */
631 len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 -
632 (tmnew->hbrmin-MINBRT)/HISTEP;
633 tmnew->histo = (int *)malloc(len*sizeof(int));
634 if (tmnew->histo != NULL)
635 for (i = len; i--; )
636 tmnew->histo[i] = tms->histo[i];
637 }
638 if (tmnew->lumap != NULL) { /* duplicate luminance mapping */
639 len = tmnew->mbrmax-tmnew->mbrmin+1;
640 tmnew->lumap = (unsigned short *)malloc(
641 len*sizeof(unsigned short) );
642 if (tmnew->lumap != NULL)
643 for (i = len; i--; )
644 tmnew->lumap[i] = tms->lumap[i];
645 }
646 /* clear package data */
647 for (i = tmNumPkgs; i--; )
648 tmnew->pd[i] = NULL;
649 /* return copy */
650 return(tmnew);
651 }
652
653
654 void
655 tmDone(tms) /* done with tone mapping -- destroy it */
656 TMstruct *tms;
657 {
658 int i;
659 /* NULL arg. is equiv. to tms */
660 if (tms == NULL)
661 return;
662 /* free tables */
663 if (tms->histo != NULL)
664 free((MEM_PTR)tms->histo);
665 if (tms->lumap != NULL)
666 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 }
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 int i;
682
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 tmErrorReturn( /* error return (with message) */
695 const char *func,
696 TMstruct *tms,
697 int err
698 )
699 {
700 if (tms != NULL) {
701 tms->lastFunc = func;
702 tms->lastError = err;
703 if (tms->flags & TM_F_NOSTDERR)
704 return(err);
705 }
706 fputs(func, stderr);
707 fputs(": ", stderr);
708 fputs(tmErrorMessage[err], stderr);
709 fputs("!\n", stderr);
710 return(err);
711 }