1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: tonemap.c,v 3.33 2009/02/09 20:23:51 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 |
if (d > 0.) |
184 |
return((TMbright)(d+.5)); |
185 |
return((TMbright)(d-.5)); |
186 |
} |
187 |
|
188 |
|
189 |
int |
190 |
tmCvLums( /* convert luminances using lookup */ |
191 |
TMbright *ls, |
192 |
float *scan, |
193 |
int len |
194 |
) |
195 |
{ |
196 |
if (tmFloat2BrtLUT == NULL) { /* initialize lookup table */ |
197 |
int32 i; |
198 |
tmFloat2BrtLUT = (TMbright *)malloc(sizeof(TMbright)*0x10000); |
199 |
if (tmFloat2BrtLUT == NULL) |
200 |
return(TM_E_NOMEM); |
201 |
for (i = 0; i < 0x10000; i++) { |
202 |
int32 l = (i<<1 | 1) << 14; |
203 |
#ifndef isfinite |
204 |
if ((l & 0x7f800000) == 0x7f800000) |
205 |
tmFloat2BrtLUT[i] = TM_NOBRT; |
206 |
else |
207 |
#endif |
208 |
tmFloat2BrtLUT[i] = tmCvLuminance(*(float *)&l); |
209 |
} |
210 |
} |
211 |
if (len <= 0) |
212 |
return(TM_E_OK); |
213 |
if ((ls == NULL) | (scan == NULL)) |
214 |
return(TM_E_ILLEGAL); |
215 |
while (len--) { |
216 |
if (*scan <= TM_NOLUM) { |
217 |
*ls++ = TM_NOBRT; |
218 |
++scan; |
219 |
continue; |
220 |
} |
221 |
*ls++ = tmCvLumLUfp(scan++); |
222 |
} |
223 |
return(TM_E_OK); |
224 |
} |
225 |
|
226 |
|
227 |
int |
228 |
tmCvGrays( /* convert float gray values */ |
229 |
TMstruct *tms, |
230 |
TMbright *ls, |
231 |
float *scan, |
232 |
int len |
233 |
) |
234 |
{ |
235 |
static const char funcName[] = "tmCvGrays"; |
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 |
if (tmFloat2BrtLUT == NULL) /* initialize */ |
243 |
tmCvLums(NULL, NULL, 0); |
244 |
for (i = len; i--; ) { |
245 |
float lum = tms->inpsf * scan[i]; |
246 |
if (lum <= TM_NOLUM) |
247 |
ls[i] = TM_NOBRT; |
248 |
else |
249 |
ls[i] = tmCvLumLUfp(&lum); |
250 |
} |
251 |
returnOK; |
252 |
} |
253 |
|
254 |
|
255 |
int |
256 |
tmCvColors( /* convert float colors */ |
257 |
TMstruct *tms, |
258 |
TMbright *ls, |
259 |
BYTE *cs, |
260 |
COLOR *scan, |
261 |
int len |
262 |
) |
263 |
{ |
264 |
static const char funcName[] = "tmCvColors"; |
265 |
static BYTE gamtab[1024]; |
266 |
static double curgam = .0; |
267 |
COLOR cmon; |
268 |
float lum, slum, d; |
269 |
int i; |
270 |
|
271 |
if (tms == NULL) |
272 |
returnErr(TM_E_TMINVAL); |
273 |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
274 |
returnErr(TM_E_ILLEGAL); |
275 |
if (tmFloat2BrtLUT == NULL) /* initialize */ |
276 |
tmCvLums(NULL, NULL, 0); |
277 |
if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) { |
278 |
curgam = tms->mongam; /* (re)build table */ |
279 |
for (i = 1024; i--; ) |
280 |
gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam)); |
281 |
} |
282 |
for (i = len; i--; ) { |
283 |
if (tmNeedMatrix(tms)) { /* get monitor RGB */ |
284 |
colortrans(cmon, tms->cmat, scan[i]); |
285 |
} else { |
286 |
cmon[RED] = tms->inpsf*scan[i][RED]; |
287 |
cmon[GRN] = tms->inpsf*scan[i][GRN]; |
288 |
cmon[BLU] = tms->inpsf*scan[i][BLU]; |
289 |
} |
290 |
#ifdef isfinite |
291 |
if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f; |
292 |
if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f; |
293 |
if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f; |
294 |
#else |
295 |
if (cmon[RED] < .0f) cmon[RED] = .0f; |
296 |
if (cmon[GRN] < .0f) cmon[GRN] = .0f; |
297 |
if (cmon[BLU] < .0f) cmon[BLU] = .0f; |
298 |
#endif |
299 |
/* world luminance */ |
300 |
lum = tms->clf[RED]*cmon[RED] + |
301 |
tms->clf[GRN]*cmon[GRN] + |
302 |
tms->clf[BLU]*cmon[BLU] ; |
303 |
if (lum <= TM_NOLUM) { /* convert brightness */ |
304 |
lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM; |
305 |
ls[i] = TM_NOBRT; |
306 |
} else |
307 |
ls[i] = tmCvLumLUfp(&lum); |
308 |
if (cs == TM_NOCHROM) /* no color? */ |
309 |
continue; |
310 |
if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) { |
311 |
slum = scotlum(cmon); /* mesopic adj. */ |
312 |
if (lum < LMESLOWER) { |
313 |
cmon[RED] = cmon[GRN] = cmon[BLU] = slum; |
314 |
} else { |
315 |
d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER); |
316 |
if (tms->flags & TM_F_BW) |
317 |
cmon[RED] = cmon[GRN] = |
318 |
cmon[BLU] = d*lum; |
319 |
else |
320 |
scalecolor(cmon, d); |
321 |
d = (1.f-d)*slum; |
322 |
cmon[RED] += d; |
323 |
cmon[GRN] += d; |
324 |
cmon[BLU] += d; |
325 |
} |
326 |
} else if (tms->flags & TM_F_BW) { |
327 |
cmon[RED] = cmon[GRN] = cmon[BLU] = lum; |
328 |
} |
329 |
d = tms->clf[RED]*cmon[RED]/lum; |
330 |
cs[3*i ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
331 |
d = tms->clf[GRN]*cmon[GRN]/lum; |
332 |
cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
333 |
d = tms->clf[BLU]*cmon[BLU]/lum; |
334 |
cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
335 |
} |
336 |
returnOK; |
337 |
} |
338 |
|
339 |
|
340 |
int |
341 |
tmAddHisto( /* add values to histogram */ |
342 |
TMstruct *tms, |
343 |
TMbright *ls, |
344 |
int len, |
345 |
int wt |
346 |
) |
347 |
{ |
348 |
static const char funcName[] = "tmAddHisto"; |
349 |
int oldorig=0, oldlen, horig, hlen; |
350 |
int i, j; |
351 |
|
352 |
if (tms == NULL) |
353 |
returnErr(TM_E_TMINVAL); |
354 |
if (len < 0) |
355 |
returnErr(TM_E_ILLEGAL); |
356 |
if (len == 0) |
357 |
returnOK; |
358 |
/* first, grow limits */ |
359 |
if (tms->histo == NULL) { |
360 |
for (i = len; i-- && ls[i] < MINBRT; ) |
361 |
; |
362 |
if (i < 0) |
363 |
returnOK; |
364 |
tms->hbrmin = tms->hbrmax = ls[i]; |
365 |
oldlen = 0; |
366 |
} else { |
367 |
oldorig = HISTI(tms->hbrmin); |
368 |
oldlen = HISTI(tms->hbrmax) + 1 - oldorig; |
369 |
} |
370 |
for (i = len; i--; ) { |
371 |
if ((j = ls[i]) < MINBRT) |
372 |
continue; |
373 |
if (j < tms->hbrmin) |
374 |
tms->hbrmin = j; |
375 |
else if (j > tms->hbrmax) |
376 |
tms->hbrmax = j; |
377 |
} |
378 |
horig = HISTI(tms->hbrmin); |
379 |
hlen = HISTI(tms->hbrmax) + 1 - horig; |
380 |
if (hlen > oldlen) { /* (re)allocate histogram */ |
381 |
int *newhist = (int *)calloc(hlen, sizeof(int)); |
382 |
if (newhist == NULL) |
383 |
returnErr(TM_E_NOMEM); |
384 |
if (oldlen) { /* copy and free old */ |
385 |
for (i = oldlen, j = i+oldorig-horig; i; ) |
386 |
newhist[--j] = tms->histo[--i]; |
387 |
free((MEM_PTR)tms->histo); |
388 |
} |
389 |
tms->histo = newhist; |
390 |
} |
391 |
if (wt == 0) |
392 |
returnOK; |
393 |
for (i = len; i--; ) /* add in new counts */ |
394 |
if (ls[i] >= MINBRT) |
395 |
tms->histo[ HISTI(ls[i]) - horig ] += wt; |
396 |
returnOK; |
397 |
} |
398 |
|
399 |
|
400 |
static double |
401 |
htcontrs( /* human threshold contrast sensitivity, dL(La) */ |
402 |
double La |
403 |
) |
404 |
{ |
405 |
double l10La, l10dL; |
406 |
/* formula taken from Ferwerda et al. [SG96] */ |
407 |
if (La < 1.148e-4) |
408 |
return(1.38e-3); |
409 |
l10La = log10(La); |
410 |
if (l10La < -1.44) /* rod response regime */ |
411 |
l10dL = pow(.405*l10La + 1.6, 2.18) - 2.86; |
412 |
else if (l10La < -.0184) |
413 |
l10dL = l10La - .395; |
414 |
else if (l10La < 1.9) /* cone response regime */ |
415 |
l10dL = pow(.249*l10La + .65, 2.7) - .72; |
416 |
else |
417 |
l10dL = l10La - 1.255; |
418 |
|
419 |
return(exp10(l10dL)); |
420 |
} |
421 |
|
422 |
|
423 |
int |
424 |
tmFixedMapping( /* compute fixed, linear tone-mapping */ |
425 |
TMstruct *tms, |
426 |
double expmult, |
427 |
double gamval |
428 |
) |
429 |
{ |
430 |
static const char funcName[] = "tmFixedMapping"; |
431 |
double d; |
432 |
int i; |
433 |
|
434 |
if (!tmNewMap(tms)) |
435 |
returnErr(TM_E_NOMEM); |
436 |
if (expmult <= .0) |
437 |
expmult = 1.; |
438 |
if (gamval < MINGAM) |
439 |
gamval = tms->mongam; |
440 |
d = log(expmult/tms->inpsf); |
441 |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
442 |
double val = 256. * exp( |
443 |
( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) |
444 |
/ gamval); |
445 |
tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val; |
446 |
} |
447 |
returnOK; |
448 |
} |
449 |
|
450 |
|
451 |
int |
452 |
tmComputeMapping( /* compute histogram tone-mapping */ |
453 |
TMstruct *tms, |
454 |
double gamval, |
455 |
double Lddyn, |
456 |
double Ldmax |
457 |
) |
458 |
{ |
459 |
static const char funcName[] = "tmComputeMapping"; |
460 |
int *histo; |
461 |
float *cumf; |
462 |
int brt0, histlen, threshold, ceiling, trimmings; |
463 |
double logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld; |
464 |
int32 histot; |
465 |
double sum; |
466 |
double d; |
467 |
int i, j; |
468 |
|
469 |
if (tms == NULL || tms->histo == NULL) |
470 |
returnErr(TM_E_TMINVAL); |
471 |
/* check arguments */ |
472 |
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; |
473 |
if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX; |
474 |
if (gamval < MINGAM) gamval = tms->mongam; |
475 |
/* compute handy values */ |
476 |
Ldmin = Ldmax/Lddyn; |
477 |
logLddyn = log(Lddyn); |
478 |
Ldavg = sqrt(Ldmax*Ldmin); |
479 |
i = HISTI(tms->hbrmin); |
480 |
brt0 = HISTV(i); |
481 |
histlen = HISTI(tms->hbrmax) + 1 - i; |
482 |
/* histogram total and mean */ |
483 |
histot = 0; sum = 0; |
484 |
j = brt0 + histlen*HISTEP; |
485 |
for (i = histlen; i--; ) { |
486 |
histot += tms->histo[i]; |
487 |
sum += (double)(j -= HISTEP) * tms->histo[i]; |
488 |
} |
489 |
threshold = histot*0.005 + .5; |
490 |
if (!histot) |
491 |
returnErr(TM_E_TMFAIL); |
492 |
Lwavg = tmLuminance( (double)sum / histot ); |
493 |
/* use linear tone mapping? */ |
494 |
if (tms->flags & TM_F_LINEAR || threshold < 4 || |
495 |
tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) |
496 |
goto linearmap; |
497 |
/* clamp histogram */ |
498 |
histo = (int *)malloc(histlen*sizeof(int)); |
499 |
cumf = (float *)malloc((histlen+2)*sizeof(float)); |
500 |
if ((histo == NULL) | (cumf == NULL)) |
501 |
returnErr(TM_E_NOMEM); |
502 |
cumf[histlen+1] = 1.; /* guard for assignment code */ |
503 |
for (i = histlen; i--; ) /* make malleable copy */ |
504 |
histo[i] = tms->histo[i]; |
505 |
do { /* iterate to solution */ |
506 |
sum = 0; /* cumulative probability */ |
507 |
for (i = 0; i < histlen; i++) { |
508 |
cumf[i] = (double)sum/histot; |
509 |
sum += histo[i]; |
510 |
} |
511 |
cumf[histlen] = 1.; |
512 |
Tr = histot * (double)(tms->hbrmax - tms->hbrmin) / |
513 |
((double)histlen*TM_BRTSCALE) / logLddyn; |
514 |
ceiling = Tr + 1.; |
515 |
trimmings = 0; /* clip to envelope */ |
516 |
for (i = histlen; i--; ) { |
517 |
if (tms->flags & TM_F_HCONTR) { |
518 |
Lw = tmLuminance(brt0 + i*HISTEP); |
519 |
Ld = Ldmin * exp( logLddyn * |
520 |
.5*(cumf[i]+cumf[i+1]) ); |
521 |
ceiling = Tr * (htcontrs(Ld) * Lw) / |
522 |
(htcontrs(Lw) * Ld) + 1.; |
523 |
} |
524 |
if (histo[i] > ceiling) { |
525 |
trimmings += histo[i] - ceiling; |
526 |
histo[i] = ceiling; |
527 |
} |
528 |
} |
529 |
/* check if we're out of data */ |
530 |
if ((histot -= trimmings) <= threshold) { |
531 |
free((MEM_PTR)histo); |
532 |
free((MEM_PTR)cumf); |
533 |
goto linearmap; |
534 |
} |
535 |
} while (trimmings > threshold); |
536 |
/* allocate space for mapping */ |
537 |
if (!tmNewMap(tms)) |
538 |
returnErr(TM_E_NOMEM); |
539 |
/* assign tone-mapping */ |
540 |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
541 |
j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen; |
542 |
d -= (double)j; |
543 |
Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1])); |
544 |
d = (Ld - Ldmin)/(Ldmax - Ldmin); |
545 |
tms->lumap[i] = 256.*pow(d, 1./gamval); |
546 |
} |
547 |
free((MEM_PTR)histo); /* clean up and return */ |
548 |
free((MEM_PTR)cumf); |
549 |
returnOK; |
550 |
linearmap: /* linear tone-mapping */ |
551 |
if (tms->flags & TM_F_HCONTR) |
552 |
d = htcontrs(Ldavg) / htcontrs(Lwavg); |
553 |
else |
554 |
d = Ldavg / Lwavg; |
555 |
return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval)); |
556 |
} |
557 |
|
558 |
|
559 |
int |
560 |
tmMapPixels( /* apply tone-mapping to pixel(s) */ |
561 |
TMstruct *tms, |
562 |
BYTE *ps, |
563 |
TMbright *ls, |
564 |
BYTE *cs, |
565 |
int len |
566 |
) |
567 |
{ |
568 |
static const char funcName[] = "tmMapPixels"; |
569 |
int32 li, pv; |
570 |
|
571 |
if (tms == NULL || tms->lumap == NULL) |
572 |
returnErr(TM_E_TMINVAL); |
573 |
if ((ps == NULL) | (ls == NULL) | (len < 0)) |
574 |
returnErr(TM_E_ILLEGAL); |
575 |
while (len--) { |
576 |
if ((li = *ls++) < tms->mbrmin) { |
577 |
li = 0; |
578 |
} else { |
579 |
if (li > tms->mbrmax) |
580 |
li = tms->mbrmax; |
581 |
li = tms->lumap[li - tms->mbrmin]; |
582 |
} |
583 |
if (cs == TM_NOCHROM) |
584 |
*ps++ = li>255 ? 255 : li; |
585 |
else { |
586 |
pv = *cs++ * li / tms->cdiv[RED]; |
587 |
*ps++ = pv>255 ? 255 : pv; |
588 |
pv = *cs++ * li / tms->cdiv[GRN]; |
589 |
*ps++ = pv>255 ? 255 : pv; |
590 |
pv = *cs++ * li / tms->cdiv[BLU]; |
591 |
*ps++ = pv>255 ? 255 : pv; |
592 |
} |
593 |
} |
594 |
returnOK; |
595 |
} |
596 |
|
597 |
|
598 |
TMstruct * |
599 |
tmDup( /* duplicate top tone mapping */ |
600 |
TMstruct *tms |
601 |
) |
602 |
{ |
603 |
int len; |
604 |
int i; |
605 |
TMstruct *tmnew; |
606 |
|
607 |
if (tms == NULL) /* anything to duplicate? */ |
608 |
return(NULL); |
609 |
tmnew = (TMstruct *)malloc(sizeof(TMstruct)); |
610 |
if (tmnew == NULL) |
611 |
return(NULL); |
612 |
*tmnew = *tms; /* copy everything */ |
613 |
if (tmnew->histo != NULL) { /* duplicate histogram */ |
614 |
len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); |
615 |
tmnew->histo = (int *)malloc(len*sizeof(int)); |
616 |
if (tmnew->histo != NULL) |
617 |
for (i = len; i--; ) |
618 |
tmnew->histo[i] = tms->histo[i]; |
619 |
} |
620 |
if (tmnew->lumap != NULL) { /* duplicate luminance mapping */ |
621 |
len = tmnew->mbrmax-tmnew->mbrmin+1; |
622 |
tmnew->lumap = (unsigned short *)malloc( |
623 |
len*sizeof(unsigned short) ); |
624 |
if (tmnew->lumap != NULL) |
625 |
for (i = len; i--; ) |
626 |
tmnew->lumap[i] = tms->lumap[i]; |
627 |
} |
628 |
/* clear package data */ |
629 |
for (i = tmNumPkgs; i--; ) |
630 |
tmnew->pd[i] = NULL; |
631 |
/* return copy */ |
632 |
return(tmnew); |
633 |
} |
634 |
|
635 |
|
636 |
void |
637 |
tmDone(tms) /* done with tone mapping -- destroy it */ |
638 |
TMstruct *tms; |
639 |
{ |
640 |
int i; |
641 |
/* NULL arg. is equiv. to tms */ |
642 |
if (tms == NULL) |
643 |
return; |
644 |
/* free tables */ |
645 |
if (tms->histo != NULL) |
646 |
free((MEM_PTR)tms->histo); |
647 |
if (tms->lumap != NULL) |
648 |
free((MEM_PTR)tms->lumap); |
649 |
/* free private data */ |
650 |
for (i = tmNumPkgs; i--; ) |
651 |
if (tms->pd[i] != NULL) |
652 |
(*tmPkg[i]->Free)(tms->pd[i]); |
653 |
free((MEM_PTR)tms); /* free basic structure */ |
654 |
} |
655 |
|
656 |
/******************** Shared but Private library routines *********************/ |
657 |
|
658 |
BYTE tmMesofact[BMESUPPER-BMESLOWER]; |
659 |
|
660 |
void |
661 |
tmMkMesofact() /* build mesopic lookup factor table */ |
662 |
{ |
663 |
int i; |
664 |
|
665 |
if (tmMesofact[BMESUPPER-BMESLOWER-1]) |
666 |
return; |
667 |
|
668 |
for (i = BMESLOWER; i < BMESUPPER; i++) |
669 |
tmMesofact[i-BMESLOWER] = 256. * |
670 |
(tmLuminance(i) - LMESLOWER) / |
671 |
(LMESUPPER - LMESLOWER); |
672 |
} |
673 |
|
674 |
|
675 |
int |
676 |
tmNewMap( /* allocate new tone-mapping array */ |
677 |
TMstruct *tms |
678 |
) |
679 |
{ |
680 |
if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != |
681 |
(tms->hbrmax - tms->hbrmin)) { |
682 |
free((MEM_PTR)tms->lumap); |
683 |
tms->lumap = NULL; |
684 |
} |
685 |
tms->mbrmin = tms->hbrmin; |
686 |
tms->mbrmax = tms->hbrmax; |
687 |
if (tms->mbrmin > tms->mbrmax) |
688 |
return 0; |
689 |
if (tms->lumap == NULL) |
690 |
tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)* |
691 |
(tms->mbrmax-tms->mbrmin+1)); |
692 |
return(tms->lumap != NULL); |
693 |
} |
694 |
|
695 |
|
696 |
int |
697 |
tmErrorReturn( /* error return (with message) */ |
698 |
const char *func, |
699 |
TMstruct *tms, |
700 |
int err |
701 |
) |
702 |
{ |
703 |
if (tms != NULL) { |
704 |
tms->lastFunc = func; |
705 |
tms->lastError = err; |
706 |
if (tms->flags & TM_F_NOSTDERR) |
707 |
return(err); |
708 |
} |
709 |
fputs(func, stderr); |
710 |
fputs(": ", stderr); |
711 |
fputs(tmErrorMessage[err], stderr); |
712 |
fputs("!\n", stderr); |
713 |
return(err); |
714 |
} |