22 |
|
struct tmPackage *tmPkg[TM_MAXPKG]; |
23 |
|
int tmNumPkgs = 0; /* number of registered packages */ |
24 |
|
|
25 |
+ |
/* luminance->brightness lookup */ |
26 |
+ |
static TMbright *tmFloat2BrtLUT = NULL; |
27 |
|
|
28 |
+ |
#define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15] |
29 |
+ |
|
30 |
+ |
|
31 |
|
TMstruct * |
32 |
|
tmInit( /* initialize new tone mapping */ |
33 |
|
int flags, |
53 |
|
tmnew->clf[GRN] = rgb2xyzmat[1][1]; |
54 |
|
tmnew->clf[BLU] = rgb2xyzmat[1][2]; |
55 |
|
} else { |
56 |
< |
comprgb2xyzWBmat(cmat, tmnew->monpri=monpri); |
56 |
> |
comprgb2xyzmat(cmat, tmnew->monpri=monpri); |
57 |
|
tmnew->clf[RED] = cmat[1][0]; |
58 |
|
tmnew->clf[GRN] = cmat[1][1]; |
59 |
|
tmnew->clf[BLU] = cmat[1][2]; |
116 |
|
tms->clf[CIEX] = tms->clf[CIEZ] = 0.; |
117 |
|
tms->clf[CIEY] = 1.; |
118 |
|
} else { |
119 |
< |
comprgb2xyzWBmat(tms->cmat, tms->monpri); |
119 |
> |
comprgb2xyzmat(tms->cmat, tms->monpri); |
120 |
|
tms->clf[RED] = tms->cmat[1][0]; |
121 |
|
tms->clf[GRN] = tms->cmat[1][1]; |
122 |
|
tms->clf[BLU] = tms->cmat[1][2]; |
154 |
|
|
155 |
|
|
156 |
|
void |
157 |
< |
tmClearHisto( /* clear current histogram */ |
157 |
> |
tmClearHisto( /* clear current histogram */ |
158 |
|
TMstruct *tms |
159 |
|
) |
160 |
|
{ |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
+ |
TMbright |
169 |
+ |
tmCvLuminance( /* convert a single luminance */ |
170 |
+ |
double lum |
171 |
+ |
) |
172 |
+ |
{ |
173 |
+ |
double d; |
174 |
+ |
|
175 |
+ |
#ifdef isfinite |
176 |
+ |
if (!isfinite(lum) || lum <= TM_NOLUM) |
177 |
+ |
#else |
178 |
+ |
if (lum <= TM_NOLUM) |
179 |
+ |
#endif |
180 |
+ |
return(TM_NOBRT); |
181 |
+ |
d = TM_BRTSCALE*log(lum); |
182 |
+ |
if (d > 0.) |
183 |
+ |
return((TMbright)(d+.5)); |
184 |
+ |
return((TMbright)(d-.5)); |
185 |
+ |
} |
186 |
+ |
|
187 |
+ |
|
188 |
|
int |
189 |
+ |
tmCvLums( /* convert luminances using lookup */ |
190 |
+ |
TMbright *ls, |
191 |
+ |
float *scan, |
192 |
+ |
int len |
193 |
+ |
) |
194 |
+ |
{ |
195 |
+ |
if (tmFloat2BrtLUT == NULL) { /* initialize lookup table */ |
196 |
+ |
int32 i; |
197 |
+ |
tmFloat2BrtLUT = (TMbright *)malloc(sizeof(TMbright)*0x10000); |
198 |
+ |
if (tmFloat2BrtLUT == NULL) |
199 |
+ |
return(TM_E_NOMEM); |
200 |
+ |
for (i = 0; i < 0x10000; i++) { |
201 |
+ |
int32 l = (i<<1 | 1) << 14; |
202 |
+ |
#ifndef isfinite |
203 |
+ |
if ((l & 0x7f800000) == 0x7f800000) |
204 |
+ |
tmFloat2BrtLUT[i] = TM_NOBRT; |
205 |
+ |
else |
206 |
+ |
#endif |
207 |
+ |
tmFloat2BrtLUT[i] = tmCvLuminance(*(float *)&l); |
208 |
+ |
} |
209 |
+ |
} |
210 |
+ |
if (len <= 0) |
211 |
+ |
return(TM_E_OK); |
212 |
+ |
if ((ls == NULL) | (scan == NULL)) |
213 |
+ |
return(TM_E_ILLEGAL); |
214 |
+ |
while (len--) { |
215 |
+ |
if (*scan <= TM_NOLUM) { |
216 |
+ |
*ls++ = TM_NOBRT; |
217 |
+ |
++scan; |
218 |
+ |
continue; |
219 |
+ |
} |
220 |
+ |
*ls++ = tmCvLumLUfp(scan++); |
221 |
+ |
} |
222 |
+ |
return(TM_E_OK); |
223 |
+ |
} |
224 |
+ |
|
225 |
+ |
|
226 |
+ |
int |
227 |
+ |
tmCvGrays( /* convert float gray values */ |
228 |
+ |
TMstruct *tms, |
229 |
+ |
TMbright *ls, |
230 |
+ |
float *scan, |
231 |
+ |
int len |
232 |
+ |
) |
233 |
+ |
{ |
234 |
+ |
static const char funcName[] = "tmCvGrays"; |
235 |
+ |
int i; |
236 |
+ |
|
237 |
+ |
if (tms == NULL) |
238 |
+ |
returnErr(TM_E_TMINVAL); |
239 |
+ |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
240 |
+ |
returnErr(TM_E_ILLEGAL); |
241 |
+ |
if (tmFloat2BrtLUT == NULL) /* initialize */ |
242 |
+ |
tmCvLums(NULL, NULL, 0); |
243 |
+ |
for (i = len; i--; ) { |
244 |
+ |
float lum = tms->inpsf * scan[i]; |
245 |
+ |
if (lum <= TM_NOLUM) |
246 |
+ |
ls[i] = TM_NOBRT; |
247 |
+ |
else |
248 |
+ |
ls[i] = tmCvLumLUfp(&lum); |
249 |
+ |
} |
250 |
+ |
returnOK; |
251 |
+ |
} |
252 |
+ |
|
253 |
+ |
|
254 |
+ |
int |
255 |
|
tmCvColors( /* convert float colors */ |
256 |
|
TMstruct *tms, |
257 |
|
TMbright *ls, |
261 |
|
) |
262 |
|
{ |
263 |
|
static const char funcName[] = "tmCvColors"; |
173 |
– |
static COLOR csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM}; |
264 |
|
static BYTE gamtab[1024]; |
265 |
|
static double curgam = .0; |
266 |
|
COLOR cmon; |
267 |
< |
double lum, slum; |
178 |
< |
double d; |
267 |
> |
float lum, slum, d; |
268 |
|
int i; |
269 |
|
|
270 |
|
if (tms == NULL) |
271 |
|
returnErr(TM_E_TMINVAL); |
272 |
|
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
273 |
|
returnErr(TM_E_ILLEGAL); |
274 |
< |
if (cs != TM_NOCHROM && fabs(curgam - tms->mongam) < .02) { |
274 |
> |
if (tmFloat2BrtLUT == NULL) /* initialize */ |
275 |
> |
tmCvLums(NULL, NULL, 0); |
276 |
> |
if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) { |
277 |
|
curgam = tms->mongam; /* (re)build table */ |
278 |
|
for (i = 1024; i--; ) |
279 |
|
gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam)); |
286 |
|
cmon[GRN] = tms->inpsf*scan[i][GRN]; |
287 |
|
cmon[BLU] = tms->inpsf*scan[i][BLU]; |
288 |
|
} |
289 |
+ |
#ifdef isfinite |
290 |
+ |
if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f; |
291 |
+ |
if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f; |
292 |
+ |
if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f; |
293 |
+ |
#else |
294 |
+ |
if (cmon[RED] < .0f) cmon[RED] = .0f; |
295 |
+ |
if (cmon[GRN] < .0f) cmon[GRN] = .0f; |
296 |
+ |
if (cmon[BLU] < .0f) cmon[BLU] = .0f; |
297 |
+ |
#endif |
298 |
|
/* world luminance */ |
299 |
|
lum = tms->clf[RED]*cmon[RED] + |
300 |
|
tms->clf[GRN]*cmon[GRN] + |
301 |
|
tms->clf[BLU]*cmon[BLU] ; |
302 |
< |
/* check range */ |
303 |
< |
if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite)) |
304 |
< |
lum = tms->clf[RED]*cmon[RED] + |
305 |
< |
tms->clf[GRN]*cmon[GRN] + |
306 |
< |
tms->clf[BLU]*cmon[BLU] ; |
207 |
< |
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 |
< |
} |
302 |
> |
if (lum <= TM_NOLUM) { /* convert brightness */ |
303 |
> |
lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM; |
304 |
> |
ls[i] = TM_NOBRT; |
305 |
> |
} else |
306 |
> |
ls[i] = tmCvLumLUfp(&lum); |
307 |
|
if (cs == TM_NOCHROM) /* no color? */ |
308 |
|
continue; |
309 |
|
if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) { |
310 |
|
slum = scotlum(cmon); /* mesopic adj. */ |
311 |
< |
if (lum < LMESLOWER) |
311 |
> |
if (lum < LMESLOWER) { |
312 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = slum; |
313 |
< |
else { |
313 |
> |
} else { |
314 |
|
d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER); |
315 |
|
if (tms->flags & TM_F_BW) |
316 |
|
cmon[RED] = cmon[GRN] = |
317 |
|
cmon[BLU] = d*lum; |
318 |
|
else |
319 |
|
scalecolor(cmon, d); |
320 |
< |
d = (1.-d)*slum; |
320 |
> |
d = (1.f-d)*slum; |
321 |
|
cmon[RED] += d; |
322 |
|
cmon[GRN] += d; |
323 |
|
cmon[BLU] += d; |
326 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = lum; |
327 |
|
} |
328 |
|
d = tms->clf[RED]*cmon[RED]/lum; |
329 |
< |
cs[3*i ] = d>=.999 ? 255 : gamtab[(int)(1024.*d)]; |
329 |
> |
cs[3*i ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
330 |
|
d = tms->clf[GRN]*cmon[GRN]/lum; |
331 |
< |
cs[3*i+1] = d>=.999 ? 255 : gamtab[(int)(1024.*d)]; |
331 |
> |
cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
332 |
|
d = tms->clf[BLU]*cmon[BLU]/lum; |
333 |
< |
cs[3*i+2] = d>=.999 ? 255 : gamtab[(int)(1024.*d)]; |
333 |
> |
cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
334 |
|
} |
335 |
|
returnOK; |
336 |
|
} |
337 |
|
|
338 |
|
|
246 |
– |
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 |
– |
|
339 |
|
int |
263 |
– |
tmCvGrays( /* convert float gray values */ |
264 |
– |
TMstruct *tms, |
265 |
– |
TMbright *ls, |
266 |
– |
float *scan, |
267 |
– |
int len |
268 |
– |
) |
269 |
– |
{ |
270 |
– |
static const char funcName[] = "tmCvGrays"; |
271 |
– |
double d; |
272 |
– |
int i; |
273 |
– |
|
274 |
– |
if (tms == NULL) |
275 |
– |
returnErr(TM_E_TMINVAL); |
276 |
– |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
277 |
– |
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 |
340 |
|
tmAddHisto( /* add values to histogram */ |
341 |
|
TMstruct *tms, |
342 |
|
TMbright *ls, |
458 |
|
if (gamval < MINGAM) |
459 |
|
gamval = tms->mongam; |
460 |
|
d = log(expmult/tms->inpsf); |
461 |
< |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) |
462 |
< |
tms->lumap[i] = 256. * exp( |
461 |
> |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
462 |
> |
double val = 256. * exp( |
463 |
|
( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) |
464 |
< |
/ gamval ); |
464 |
> |
/ gamval); |
465 |
> |
tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val; |
466 |
> |
} |
467 |
|
returnOK; |
468 |
|
} |
469 |
|
|
504 |
|
j = brt0 + histlen*HISTEP; |
505 |
|
for (i = histlen; i--; ) { |
506 |
|
histot += tms->histo[i]; |
507 |
< |
sum += (j -= HISTEP) * tms->histo[i]; |
507 |
> |
sum += (double)(j -= HISTEP) * tms->histo[i]; |
508 |
|
} |
509 |
|
threshold = histot*0.005 + .5; |
510 |
< |
if (threshold < 4) |
510 |
> |
if (!histot) |
511 |
|
returnErr(TM_E_TMFAIL); |
512 |
|
Lwavg = tmLuminance( (double)sum / histot ); |
461 |
– |
/* allocate space for mapping */ |
462 |
– |
if (!tmNewMap(tms)) |
463 |
– |
returnErr(TM_E_NOMEM); |
513 |
|
/* use linear tone mapping? */ |
514 |
< |
if (tms->flags & TM_F_LINEAR) |
514 |
> |
if (tms->flags & TM_F_LINEAR || threshold < 4 || |
515 |
> |
tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) |
516 |
|
goto linearmap; |
517 |
|
/* clamp histogram */ |
518 |
|
histo = (int *)malloc(histlen*sizeof(int)); |
553 |
|
goto linearmap; |
554 |
|
} |
555 |
|
} while (trimmings > threshold); |
556 |
+ |
/* allocate space for mapping */ |
557 |
+ |
if (!tmNewMap(tms)) |
558 |
+ |
returnErr(TM_E_NOMEM); |
559 |
|
/* assign tone-mapping */ |
560 |
|
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
561 |
|
j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen; |