1 |
greg |
3.1 |
#ifndef lint |
2 |
greg |
3.35 |
static const char RCSid[] = "$Id: tonemap.c,v 3.34 2009/02/09 20:48:08 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 |
greg |
3.34 |
#include <stdlib.h> |
16 |
greg |
3.1 |
#include <math.h> |
17 |
|
|
#include "tmprivat.h" |
18 |
|
|
#include "tmerrmsg.h" |
19 |
|
|
|
20 |
|
|
#define exp10(x) exp(M_LN10*(x)) |
21 |
|
|
|
22 |
greg |
3.4 |
/* our list of conversion packages */ |
23 |
|
|
struct tmPackage *tmPkg[TM_MAXPKG]; |
24 |
|
|
int tmNumPkgs = 0; /* number of registered packages */ |
25 |
greg |
3.1 |
|
26 |
greg |
3.23 |
/* luminance->brightness lookup */ |
27 |
greg |
3.24 |
static TMbright *tmFloat2BrtLUT = NULL; |
28 |
greg |
3.23 |
|
29 |
|
|
#define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15] |
30 |
|
|
|
31 |
gregl |
3.5 |
|
32 |
greg |
3.16 |
TMstruct * |
33 |
|
|
tmInit( /* initialize new tone mapping */ |
34 |
|
|
int flags, |
35 |
|
|
RGBPRIMP monpri, |
36 |
|
|
double gamval |
37 |
|
|
) |
38 |
greg |
3.1 |
{ |
39 |
|
|
COLORMAT cmat; |
40 |
greg |
3.16 |
TMstruct *tmnew; |
41 |
|
|
int i; |
42 |
greg |
3.1 |
/* allocate structure */ |
43 |
greg |
3.16 |
tmnew = (TMstruct *)malloc(sizeof(TMstruct)); |
44 |
greg |
3.1 |
if (tmnew == NULL) |
45 |
|
|
return(NULL); |
46 |
|
|
|
47 |
|
|
tmnew->flags = flags & ~TM_F_UNIMPL; |
48 |
greg |
3.15 |
if (tmnew->flags & TM_F_BW) |
49 |
|
|
tmnew->flags &= ~TM_F_MESOPIC; |
50 |
greg |
3.1 |
/* 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 |
greg |
3.26 |
comprgb2xyzmat(cmat, tmnew->monpri=monpri); |
58 |
greg |
3.1 |
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 |
greg |
3.4 |
/* set color divisors */ |
68 |
|
|
for (i = 0; i < 3; i++) |
69 |
|
|
tmnew->cdiv[i] = 256.*pow(tmnew->clf[i], 1./tmnew->mongam); |
70 |
|
|
|
71 |
greg |
3.1 |
/* 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 |
greg |
3.18 |
tmnew->inpdat = NULL; |
78 |
greg |
3.9 |
tmnew->hbrmin = 10; tmnew->hbrmax = -10; |
79 |
greg |
3.1 |
tmnew->histo = NULL; |
80 |
greg |
3.9 |
tmnew->mbrmin = 10; tmnew->mbrmax = -10; |
81 |
greg |
3.1 |
tmnew->lumap = NULL; |
82 |
greg |
3.4 |
/* zero private data */ |
83 |
|
|
for (i = TM_MAXPKG; i--; ) |
84 |
|
|
tmnew->pd[i] = NULL; |
85 |
greg |
3.17 |
tmnew->lastError = TM_E_OK; |
86 |
|
|
tmnew->lastFunc = "NoErr"; |
87 |
greg |
3.16 |
/* return new TMstruct */ |
88 |
|
|
return(tmnew); |
89 |
greg |
3.1 |
} |
90 |
|
|
|
91 |
|
|
|
92 |
|
|
int |
93 |
greg |
3.16 |
tmSetSpace( /* set input color space for conversions */ |
94 |
|
|
TMstruct *tms, |
95 |
|
|
RGBPRIMP pri, |
96 |
greg |
3.18 |
double sf, |
97 |
|
|
MEM_PTR dat |
98 |
greg |
3.16 |
) |
99 |
greg |
3.1 |
{ |
100 |
greg |
3.17 |
static const char funcName[] = "tmSetSpace"; |
101 |
greg |
3.16 |
int i, j; |
102 |
greg |
3.1 |
/* error check */ |
103 |
greg |
3.16 |
if (tms == NULL) |
104 |
greg |
3.1 |
returnErr(TM_E_TMINVAL); |
105 |
|
|
if (sf <= 1e-12) |
106 |
|
|
returnErr(TM_E_ILLEGAL); |
107 |
|
|
/* check if no change */ |
108 |
greg |
3.18 |
if (pri == tms->inppri && FEQ(sf, tms->inpsf) && dat == tms->inpdat) |
109 |
greg |
3.1 |
returnOK; |
110 |
greg |
3.16 |
tms->inppri = pri; /* let's set it */ |
111 |
|
|
tms->inpsf = sf; |
112 |
greg |
3.18 |
tms->inpdat = dat; |
113 |
greg |
3.1 |
|
114 |
greg |
3.16 |
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 |
greg |
3.1 |
} else { |
120 |
greg |
3.26 |
comprgb2xyzmat(tms->cmat, tms->monpri); |
121 |
greg |
3.16 |
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 |
greg |
3.1 |
|
130 |
greg |
3.16 |
} else if (tms->inppri == TM_XYZPRIM) /* input is XYZ */ |
131 |
|
|
compxyz2rgbWBmat(tms->cmat, tms->monpri); |
132 |
greg |
3.1 |
|
133 |
|
|
else { /* input is RGB */ |
134 |
greg |
3.16 |
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 |
greg |
3.1 |
} |
139 |
|
|
for (i = 0; i < 3; i++) |
140 |
|
|
for (j = 0; j < 3; j++) |
141 |
greg |
3.16 |
tms->cmat[i][j] *= tms->inpsf; |
142 |
greg |
3.4 |
/* set color divisors */ |
143 |
|
|
for (i = 0; i < 3; i++) |
144 |
greg |
3.16 |
if (tms->clf[i] > .001) |
145 |
|
|
tms->cdiv[i] = |
146 |
|
|
256.*pow(tms->clf[i], 1./tms->mongam); |
147 |
greg |
3.4 |
else |
148 |
greg |
3.16 |
tms->cdiv[i] = 1; |
149 |
greg |
3.4 |
/* notify packages */ |
150 |
|
|
for (i = tmNumPkgs; i--; ) |
151 |
greg |
3.16 |
if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL) |
152 |
|
|
(*tmPkg[i]->NewSpace)(tms); |
153 |
greg |
3.1 |
returnOK; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
|
157 |
|
|
void |
158 |
greg |
3.23 |
tmClearHisto( /* clear current histogram */ |
159 |
greg |
3.16 |
TMstruct *tms |
160 |
|
|
) |
161 |
greg |
3.1 |
{ |
162 |
greg |
3.16 |
if (tms == NULL || tms->histo == NULL) |
163 |
greg |
3.1 |
return; |
164 |
greg |
3.16 |
free((MEM_PTR)tms->histo); |
165 |
|
|
tms->histo = NULL; |
166 |
greg |
3.1 |
} |
167 |
|
|
|
168 |
|
|
|
169 |
greg |
3.23 |
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 |
greg |
3.35 |
return((TMbright)(d + .5 - (d < 0.))); |
184 |
greg |
3.23 |
} |
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 |
greg |
3.24 |
if (tmFloat2BrtLUT == NULL) /* initialize */ |
241 |
|
|
tmCvLums(NULL, NULL, 0); |
242 |
greg |
3.23 |
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 |
greg |
3.1 |
int |
254 |
greg |
3.16 |
tmCvColors( /* convert float colors */ |
255 |
|
|
TMstruct *tms, |
256 |
|
|
TMbright *ls, |
257 |
|
|
BYTE *cs, |
258 |
|
|
COLOR *scan, |
259 |
|
|
int len |
260 |
|
|
) |
261 |
greg |
3.1 |
{ |
262 |
greg |
3.17 |
static const char funcName[] = "tmCvColors"; |
263 |
greg |
3.20 |
static BYTE gamtab[1024]; |
264 |
|
|
static double curgam = .0; |
265 |
greg |
3.1 |
COLOR cmon; |
266 |
greg |
3.23 |
float lum, slum, d; |
267 |
greg |
3.16 |
int i; |
268 |
greg |
3.1 |
|
269 |
greg |
3.16 |
if (tms == NULL) |
270 |
greg |
3.1 |
returnErr(TM_E_TMINVAL); |
271 |
schorsch |
3.13 |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
272 |
greg |
3.1 |
returnErr(TM_E_ILLEGAL); |
273 |
greg |
3.24 |
if (tmFloat2BrtLUT == NULL) /* initialize */ |
274 |
|
|
tmCvLums(NULL, NULL, 0); |
275 |
greg |
3.21 |
if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) { |
276 |
greg |
3.20 |
curgam = tms->mongam; /* (re)build table */ |
277 |
|
|
for (i = 1024; i--; ) |
278 |
|
|
gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam)); |
279 |
|
|
} |
280 |
greg |
3.1 |
for (i = len; i--; ) { |
281 |
greg |
3.16 |
if (tmNeedMatrix(tms)) { /* get monitor RGB */ |
282 |
|
|
colortrans(cmon, tms->cmat, scan[i]); |
283 |
greg |
3.15 |
} else { |
284 |
greg |
3.16 |
cmon[RED] = tms->inpsf*scan[i][RED]; |
285 |
|
|
cmon[GRN] = tms->inpsf*scan[i][GRN]; |
286 |
|
|
cmon[BLU] = tms->inpsf*scan[i][BLU]; |
287 |
greg |
3.1 |
} |
288 |
greg |
3.23 |
#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 |
greg |
3.22 |
#endif |
297 |
greg |
3.1 |
/* world luminance */ |
298 |
greg |
3.16 |
lum = tms->clf[RED]*cmon[RED] + |
299 |
|
|
tms->clf[GRN]*cmon[GRN] + |
300 |
|
|
tms->clf[BLU]*cmon[BLU] ; |
301 |
greg |
3.25 |
if (lum <= TM_NOLUM) { /* convert brightness */ |
302 |
|
|
lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM; |
303 |
greg |
3.23 |
ls[i] = TM_NOBRT; |
304 |
greg |
3.25 |
} else |
305 |
greg |
3.23 |
ls[i] = tmCvLumLUfp(&lum); |
306 |
greg |
3.1 |
if (cs == TM_NOCHROM) /* no color? */ |
307 |
|
|
continue; |
308 |
greg |
3.16 |
if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) { |
309 |
greg |
3.1 |
slum = scotlum(cmon); /* mesopic adj. */ |
310 |
greg |
3.25 |
if (lum < LMESLOWER) { |
311 |
greg |
3.1 |
cmon[RED] = cmon[GRN] = cmon[BLU] = slum; |
312 |
greg |
3.25 |
} else { |
313 |
greg |
3.1 |
d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER); |
314 |
greg |
3.16 |
if (tms->flags & TM_F_BW) |
315 |
greg |
3.1 |
cmon[RED] = cmon[GRN] = |
316 |
|
|
cmon[BLU] = d*lum; |
317 |
|
|
else |
318 |
|
|
scalecolor(cmon, d); |
319 |
greg |
3.23 |
d = (1.f-d)*slum; |
320 |
greg |
3.1 |
cmon[RED] += d; |
321 |
|
|
cmon[GRN] += d; |
322 |
|
|
cmon[BLU] += d; |
323 |
|
|
} |
324 |
greg |
3.16 |
} else if (tms->flags & TM_F_BW) { |
325 |
greg |
3.1 |
cmon[RED] = cmon[GRN] = cmon[BLU] = lum; |
326 |
|
|
} |
327 |
greg |
3.16 |
d = tms->clf[RED]*cmon[RED]/lum; |
328 |
greg |
3.23 |
cs[3*i ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
329 |
greg |
3.16 |
d = tms->clf[GRN]*cmon[GRN]/lum; |
330 |
greg |
3.23 |
cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
331 |
greg |
3.16 |
d = tms->clf[BLU]*cmon[BLU]/lum; |
332 |
greg |
3.23 |
cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
333 |
greg |
3.1 |
} |
334 |
|
|
returnOK; |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
|
338 |
greg |
3.9 |
int |
339 |
greg |
3.16 |
tmAddHisto( /* add values to histogram */ |
340 |
|
|
TMstruct *tms, |
341 |
|
|
TMbright *ls, |
342 |
|
|
int len, |
343 |
|
|
int wt |
344 |
|
|
) |
345 |
greg |
3.1 |
{ |
346 |
greg |
3.17 |
static const char funcName[] = "tmAddHisto"; |
347 |
greg |
3.9 |
int oldorig=0, oldlen, horig, hlen; |
348 |
greg |
3.16 |
int i, j; |
349 |
greg |
3.1 |
|
350 |
greg |
3.16 |
if (tms == NULL) |
351 |
greg |
3.1 |
returnErr(TM_E_TMINVAL); |
352 |
gwlarson |
3.8 |
if (len < 0) |
353 |
|
|
returnErr(TM_E_ILLEGAL); |
354 |
|
|
if (len == 0) |
355 |
|
|
returnOK; |
356 |
greg |
3.1 |
/* first, grow limits */ |
357 |
greg |
3.16 |
if (tms->histo == NULL) { |
358 |
greg |
3.1 |
for (i = len; i-- && ls[i] < MINBRT; ) |
359 |
|
|
; |
360 |
|
|
if (i < 0) |
361 |
|
|
returnOK; |
362 |
greg |
3.16 |
tms->hbrmin = tms->hbrmax = ls[i]; |
363 |
greg |
3.1 |
oldlen = 0; |
364 |
|
|
} else { |
365 |
greg |
3.31 |
oldorig = HISTI(tms->hbrmin); |
366 |
|
|
oldlen = HISTI(tms->hbrmax) + 1 - oldorig; |
367 |
greg |
3.1 |
} |
368 |
|
|
for (i = len; i--; ) { |
369 |
|
|
if ((j = ls[i]) < MINBRT) |
370 |
|
|
continue; |
371 |
greg |
3.16 |
if (j < tms->hbrmin) |
372 |
|
|
tms->hbrmin = j; |
373 |
|
|
else if (j > tms->hbrmax) |
374 |
|
|
tms->hbrmax = j; |
375 |
greg |
3.1 |
} |
376 |
greg |
3.31 |
horig = HISTI(tms->hbrmin); |
377 |
|
|
hlen = HISTI(tms->hbrmax) + 1 - horig; |
378 |
greg |
3.1 |
if (hlen > oldlen) { /* (re)allocate histogram */ |
379 |
greg |
3.16 |
int *newhist = (int *)calloc(hlen, sizeof(int)); |
380 |
greg |
3.1 |
if (newhist == NULL) |
381 |
|
|
returnErr(TM_E_NOMEM); |
382 |
|
|
if (oldlen) { /* copy and free old */ |
383 |
greg |
3.2 |
for (i = oldlen, j = i+oldorig-horig; i; ) |
384 |
greg |
3.16 |
newhist[--j] = tms->histo[--i]; |
385 |
|
|
free((MEM_PTR)tms->histo); |
386 |
greg |
3.1 |
} |
387 |
greg |
3.16 |
tms->histo = newhist; |
388 |
greg |
3.1 |
} |
389 |
|
|
if (wt == 0) |
390 |
|
|
returnOK; |
391 |
|
|
for (i = len; i--; ) /* add in new counts */ |
392 |
|
|
if (ls[i] >= MINBRT) |
393 |
greg |
3.31 |
tms->histo[ HISTI(ls[i]) - horig ] += wt; |
394 |
greg |
3.1 |
returnOK; |
395 |
|
|
} |
396 |
|
|
|
397 |
|
|
|
398 |
|
|
static double |
399 |
greg |
3.16 |
htcontrs( /* human threshold contrast sensitivity, dL(La) */ |
400 |
|
|
double La |
401 |
|
|
) |
402 |
greg |
3.1 |
{ |
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 |
greg |
3.9 |
int |
422 |
greg |
3.16 |
tmFixedMapping( /* compute fixed, linear tone-mapping */ |
423 |
|
|
TMstruct *tms, |
424 |
|
|
double expmult, |
425 |
|
|
double gamval |
426 |
|
|
) |
427 |
greg |
3.9 |
{ |
428 |
greg |
3.17 |
static const char funcName[] = "tmFixedMapping"; |
429 |
greg |
3.9 |
double d; |
430 |
greg |
3.16 |
int i; |
431 |
greg |
3.9 |
|
432 |
greg |
3.16 |
if (!tmNewMap(tms)) |
433 |
greg |
3.9 |
returnErr(TM_E_NOMEM); |
434 |
|
|
if (expmult <= .0) |
435 |
|
|
expmult = 1.; |
436 |
|
|
if (gamval < MINGAM) |
437 |
greg |
3.16 |
gamval = tms->mongam; |
438 |
|
|
d = log(expmult/tms->inpsf); |
439 |
greg |
3.28 |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
440 |
|
|
double val = 256. * exp( |
441 |
greg |
3.16 |
( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) |
442 |
greg |
3.28 |
/ gamval); |
443 |
|
|
tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val; |
444 |
|
|
} |
445 |
greg |
3.9 |
returnOK; |
446 |
|
|
} |
447 |
|
|
|
448 |
|
|
|
449 |
greg |
3.1 |
int |
450 |
greg |
3.16 |
tmComputeMapping( /* compute histogram tone-mapping */ |
451 |
|
|
TMstruct *tms, |
452 |
|
|
double gamval, |
453 |
|
|
double Lddyn, |
454 |
|
|
double Ldmax |
455 |
|
|
) |
456 |
greg |
3.1 |
{ |
457 |
greg |
3.17 |
static const char funcName[] = "tmComputeMapping"; |
458 |
greg |
3.1 |
int *histo; |
459 |
|
|
float *cumf; |
460 |
greg |
3.9 |
int brt0, histlen, threshold, ceiling, trimmings; |
461 |
greg |
3.1 |
double logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld; |
462 |
greg |
3.11 |
int32 histot; |
463 |
greg |
3.9 |
double sum; |
464 |
greg |
3.16 |
double d; |
465 |
|
|
int i, j; |
466 |
greg |
3.1 |
|
467 |
greg |
3.16 |
if (tms == NULL || tms->histo == NULL) |
468 |
greg |
3.1 |
returnErr(TM_E_TMINVAL); |
469 |
|
|
/* check arguments */ |
470 |
|
|
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; |
471 |
|
|
if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX; |
472 |
greg |
3.16 |
if (gamval < MINGAM) gamval = tms->mongam; |
473 |
greg |
3.1 |
/* compute handy values */ |
474 |
|
|
Ldmin = Ldmax/Lddyn; |
475 |
|
|
logLddyn = log(Lddyn); |
476 |
|
|
Ldavg = sqrt(Ldmax*Ldmin); |
477 |
greg |
3.31 |
i = HISTI(tms->hbrmin); |
478 |
greg |
3.32 |
brt0 = HISTV(i); |
479 |
greg |
3.31 |
histlen = HISTI(tms->hbrmax) + 1 - i; |
480 |
greg |
3.1 |
/* histogram total and mean */ |
481 |
|
|
histot = 0; sum = 0; |
482 |
|
|
j = brt0 + histlen*HISTEP; |
483 |
|
|
for (i = histlen; i--; ) { |
484 |
greg |
3.16 |
histot += tms->histo[i]; |
485 |
greg |
3.30 |
sum += (double)(j -= HISTEP) * tms->histo[i]; |
486 |
greg |
3.1 |
} |
487 |
greg |
3.14 |
threshold = histot*0.005 + .5; |
488 |
greg |
3.29 |
if (!histot) |
489 |
greg |
3.1 |
returnErr(TM_E_TMFAIL); |
490 |
|
|
Lwavg = tmLuminance( (double)sum / histot ); |
491 |
greg |
3.29 |
/* use linear tone mapping? */ |
492 |
greg |
3.30 |
if (tms->flags & TM_F_LINEAR || threshold < 4 || |
493 |
|
|
tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) |
494 |
greg |
3.29 |
goto linearmap; |
495 |
greg |
3.9 |
/* clamp histogram */ |
496 |
|
|
histo = (int *)malloc(histlen*sizeof(int)); |
497 |
|
|
cumf = (float *)malloc((histlen+2)*sizeof(float)); |
498 |
schorsch |
3.13 |
if ((histo == NULL) | (cumf == NULL)) |
499 |
greg |
3.9 |
returnErr(TM_E_NOMEM); |
500 |
|
|
cumf[histlen+1] = 1.; /* guard for assignment code */ |
501 |
|
|
for (i = histlen; i--; ) /* make malleable copy */ |
502 |
greg |
3.16 |
histo[i] = tms->histo[i]; |
503 |
greg |
3.9 |
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 |
greg |
3.16 |
Tr = histot * (double)(tms->hbrmax - tms->hbrmin) / |
511 |
greg |
3.9 |
((double)histlen*TM_BRTSCALE) / logLddyn; |
512 |
|
|
ceiling = Tr + 1.; |
513 |
|
|
trimmings = 0; /* clip to envelope */ |
514 |
|
|
for (i = histlen; i--; ) { |
515 |
greg |
3.16 |
if (tms->flags & TM_F_HCONTR) { |
516 |
greg |
3.9 |
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 |
greg |
3.2 |
} |
522 |
greg |
3.9 |
if (histo[i] > ceiling) { |
523 |
|
|
trimmings += histo[i] - ceiling; |
524 |
|
|
histo[i] = ceiling; |
525 |
greg |
3.1 |
} |
526 |
|
|
} |
527 |
greg |
3.9 |
/* 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 |
greg |
3.30 |
/* allocate space for mapping */ |
535 |
|
|
if (!tmNewMap(tms)) |
536 |
|
|
returnErr(TM_E_NOMEM); |
537 |
greg |
3.9 |
/* assign tone-mapping */ |
538 |
greg |
3.16 |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
539 |
|
|
j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen; |
540 |
greg |
3.9 |
d -= (double)j; |
541 |
|
|
Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1])); |
542 |
|
|
d = (Ld - Ldmin)/(Ldmax - Ldmin); |
543 |
greg |
3.16 |
tms->lumap[i] = 256.*pow(d, 1./gamval); |
544 |
greg |
3.1 |
} |
545 |
greg |
3.9 |
free((MEM_PTR)histo); /* clean up and return */ |
546 |
|
|
free((MEM_PTR)cumf); |
547 |
greg |
3.1 |
returnOK; |
548 |
greg |
3.9 |
linearmap: /* linear tone-mapping */ |
549 |
greg |
3.16 |
if (tms->flags & TM_F_HCONTR) |
550 |
greg |
3.9 |
d = htcontrs(Ldavg) / htcontrs(Lwavg); |
551 |
|
|
else |
552 |
|
|
d = Ldavg / Lwavg; |
553 |
greg |
3.16 |
return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval)); |
554 |
greg |
3.1 |
} |
555 |
|
|
|
556 |
|
|
|
557 |
|
|
int |
558 |
greg |
3.16 |
tmMapPixels( /* apply tone-mapping to pixel(s) */ |
559 |
|
|
TMstruct *tms, |
560 |
|
|
BYTE *ps, |
561 |
|
|
TMbright *ls, |
562 |
|
|
BYTE *cs, |
563 |
|
|
int len |
564 |
|
|
) |
565 |
greg |
3.1 |
{ |
566 |
greg |
3.17 |
static const char funcName[] = "tmMapPixels"; |
567 |
greg |
3.16 |
int32 li, pv; |
568 |
greg |
3.1 |
|
569 |
greg |
3.16 |
if (tms == NULL || tms->lumap == NULL) |
570 |
greg |
3.1 |
returnErr(TM_E_TMINVAL); |
571 |
schorsch |
3.13 |
if ((ps == NULL) | (ls == NULL) | (len < 0)) |
572 |
greg |
3.1 |
returnErr(TM_E_ILLEGAL); |
573 |
|
|
while (len--) { |
574 |
greg |
3.16 |
if ((li = *ls++) < tms->mbrmin) { |
575 |
greg |
3.9 |
li = 0; |
576 |
|
|
} else { |
577 |
greg |
3.16 |
if (li > tms->mbrmax) |
578 |
|
|
li = tms->mbrmax; |
579 |
|
|
li = tms->lumap[li - tms->mbrmin]; |
580 |
greg |
3.9 |
} |
581 |
greg |
3.1 |
if (cs == TM_NOCHROM) |
582 |
|
|
*ps++ = li>255 ? 255 : li; |
583 |
|
|
else { |
584 |
greg |
3.16 |
pv = *cs++ * li / tms->cdiv[RED]; |
585 |
greg |
3.1 |
*ps++ = pv>255 ? 255 : pv; |
586 |
greg |
3.16 |
pv = *cs++ * li / tms->cdiv[GRN]; |
587 |
greg |
3.1 |
*ps++ = pv>255 ? 255 : pv; |
588 |
greg |
3.16 |
pv = *cs++ * li / tms->cdiv[BLU]; |
589 |
greg |
3.1 |
*ps++ = pv>255 ? 255 : pv; |
590 |
|
|
} |
591 |
|
|
} |
592 |
|
|
returnOK; |
593 |
|
|
} |
594 |
|
|
|
595 |
|
|
|
596 |
greg |
3.16 |
TMstruct * |
597 |
|
|
tmDup( /* duplicate top tone mapping */ |
598 |
|
|
TMstruct *tms |
599 |
|
|
) |
600 |
greg |
3.3 |
{ |
601 |
|
|
int len; |
602 |
greg |
3.16 |
int i; |
603 |
|
|
TMstruct *tmnew; |
604 |
greg |
3.3 |
|
605 |
greg |
3.16 |
if (tms == NULL) /* anything to duplicate? */ |
606 |
greg |
3.3 |
return(NULL); |
607 |
greg |
3.16 |
tmnew = (TMstruct *)malloc(sizeof(TMstruct)); |
608 |
greg |
3.3 |
if (tmnew == NULL) |
609 |
|
|
return(NULL); |
610 |
greg |
3.16 |
*tmnew = *tms; /* copy everything */ |
611 |
greg |
3.3 |
if (tmnew->histo != NULL) { /* duplicate histogram */ |
612 |
greg |
3.31 |
len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); |
613 |
greg |
3.3 |
tmnew->histo = (int *)malloc(len*sizeof(int)); |
614 |
|
|
if (tmnew->histo != NULL) |
615 |
|
|
for (i = len; i--; ) |
616 |
greg |
3.16 |
tmnew->histo[i] = tms->histo[i]; |
617 |
greg |
3.3 |
} |
618 |
|
|
if (tmnew->lumap != NULL) { /* duplicate luminance mapping */ |
619 |
gregl |
3.6 |
len = tmnew->mbrmax-tmnew->mbrmin+1; |
620 |
greg |
3.3 |
tmnew->lumap = (unsigned short *)malloc( |
621 |
|
|
len*sizeof(unsigned short) ); |
622 |
|
|
if (tmnew->lumap != NULL) |
623 |
|
|
for (i = len; i--; ) |
624 |
greg |
3.16 |
tmnew->lumap[i] = tms->lumap[i]; |
625 |
greg |
3.3 |
} |
626 |
greg |
3.4 |
/* clear package data */ |
627 |
|
|
for (i = tmNumPkgs; i--; ) |
628 |
|
|
tmnew->pd[i] = NULL; |
629 |
greg |
3.16 |
/* return copy */ |
630 |
|
|
return(tmnew); |
631 |
greg |
3.1 |
} |
632 |
|
|
|
633 |
|
|
|
634 |
|
|
void |
635 |
|
|
tmDone(tms) /* done with tone mapping -- destroy it */ |
636 |
greg |
3.16 |
TMstruct *tms; |
637 |
greg |
3.1 |
{ |
638 |
greg |
3.16 |
int i; |
639 |
|
|
/* NULL arg. is equiv. to tms */ |
640 |
|
|
if (tms == NULL) |
641 |
greg |
3.1 |
return; |
642 |
|
|
/* free tables */ |
643 |
|
|
if (tms->histo != NULL) |
644 |
greg |
3.4 |
free((MEM_PTR)tms->histo); |
645 |
greg |
3.1 |
if (tms->lumap != NULL) |
646 |
greg |
3.4 |
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 |
greg |
3.9 |
} |
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 |
greg |
3.16 |
int i; |
662 |
greg |
3.9 |
|
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 |
greg |
3.33 |
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 |
greg |
3.16 |
tmErrorReturn( /* error return (with message) */ |
696 |
greg |
3.17 |
const char *func, |
697 |
greg |
3.16 |
TMstruct *tms, |
698 |
|
|
int err |
699 |
|
|
) |
700 |
greg |
3.9 |
{ |
701 |
greg |
3.17 |
if (tms != NULL) { |
702 |
|
|
tms->lastFunc = func; |
703 |
|
|
tms->lastError = err; |
704 |
|
|
if (tms->flags & TM_F_NOSTDERR) |
705 |
|
|
return(err); |
706 |
|
|
} |
707 |
greg |
3.9 |
fputs(func, stderr); |
708 |
|
|
fputs(": ", stderr); |
709 |
|
|
fputs(tmErrorMessage[err], stderr); |
710 |
|
|
fputs("!\n", stderr); |
711 |
|
|
return(err); |
712 |
greg |
3.1 |
} |