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