ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tonemap.c
Revision: 3.21
Committed: Wed Jan 18 00:24:38 2006 UTC (18 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.20: +2 -2 lines
Log Message:
Bug fix in last change

File Contents

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