23 |
|
int tmNumPkgs = 0; /* number of registered packages */ |
24 |
|
|
25 |
|
/* luminance->brightness lookup */ |
26 |
< |
static TMbright *tmFloat2BrtLUT; |
26 |
> |
static TMbright *tmFloat2BrtLUT = NULL; |
27 |
|
|
28 |
|
#define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15] |
29 |
|
|
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]; |
232 |
|
) |
233 |
|
{ |
234 |
|
static const char funcName[] = "tmCvGrays"; |
235 |
– |
double d; |
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) |
261 |
|
) |
262 |
|
{ |
263 |
|
static const char funcName[] = "tmCvColors"; |
263 |
– |
static COLOR csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM}; |
264 |
|
static BYTE gamtab[1024]; |
265 |
|
static double curgam = .0; |
266 |
|
COLOR cmon; |
271 |
|
returnErr(TM_E_TMINVAL); |
272 |
|
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
273 |
|
returnErr(TM_E_ILLEGAL); |
274 |
< |
tmCvLums(NULL, NULL, 0); /* initialize */ |
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--; ) |
299 |
|
lum = tms->clf[RED]*cmon[RED] + |
300 |
|
tms->clf[GRN]*cmon[GRN] + |
301 |
|
tms->clf[BLU]*cmon[BLU] ; |
302 |
< |
if (lum <= TM_NOLUM) /* convert brightness */ |
302 |
> |
if (lum <= TM_NOLUM) { /* convert brightness */ |
303 |
> |
lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM; |
304 |
|
ls[i] = TM_NOBRT; |
305 |
< |
else |
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] = |
363 |
|
tms->hbrmin = tms->hbrmax = ls[i]; |
364 |
|
oldlen = 0; |
365 |
|
} else { |
366 |
< |
oldorig = (tms->hbrmin-MINBRT)/HISTEP; |
367 |
< |
oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig; |
366 |
> |
oldorig = HISTI(tms->hbrmin); |
367 |
> |
oldlen = HISTI(tms->hbrmax) + 1 - oldorig; |
368 |
|
} |
369 |
|
for (i = len; i--; ) { |
370 |
|
if ((j = ls[i]) < MINBRT) |
374 |
|
else if (j > tms->hbrmax) |
375 |
|
tms->hbrmax = j; |
376 |
|
} |
377 |
< |
horig = (tms->hbrmin-MINBRT)/HISTEP; |
378 |
< |
hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig; |
377 |
> |
horig = HISTI(tms->hbrmin); |
378 |
> |
hlen = HISTI(tms->hbrmax) + 1 - horig; |
379 |
|
if (hlen > oldlen) { /* (re)allocate histogram */ |
380 |
|
int *newhist = (int *)calloc(hlen, sizeof(int)); |
381 |
|
if (newhist == NULL) |
391 |
|
returnOK; |
392 |
|
for (i = len; i--; ) /* add in new counts */ |
393 |
|
if (ls[i] >= MINBRT) |
394 |
< |
tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt; |
394 |
> |
tms->histo[ HISTI(ls[i]) - horig ] += wt; |
395 |
|
returnOK; |
396 |
|
} |
397 |
|
|
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 |
|
|
496 |
|
Ldmin = Ldmax/Lddyn; |
497 |
|
logLddyn = log(Lddyn); |
498 |
|
Ldavg = sqrt(Ldmax*Ldmin); |
499 |
< |
i = (tms->hbrmin-MINBRT)/HISTEP; |
499 |
> |
i = HISTI(tms->hbrmin); |
500 |
|
brt0 = MINBRT + HISTEP/2 + i*HISTEP; |
501 |
< |
histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i; |
501 |
> |
histlen = HISTI(tms->hbrmax) + 1 - i; |
502 |
|
/* histogram total and mean */ |
503 |
|
histot = 0; sum = 0; |
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 ); |
509 |
– |
/* allocate space for mapping */ |
510 |
– |
if (!tmNewMap(tms)) |
511 |
– |
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; |
633 |
|
return(NULL); |
634 |
|
*tmnew = *tms; /* copy everything */ |
635 |
|
if (tmnew->histo != NULL) { /* duplicate histogram */ |
636 |
< |
len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 - |
632 |
< |
(tmnew->hbrmin-MINBRT)/HISTEP; |
636 |
> |
len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); |
637 |
|
tmnew->histo = (int *)malloc(len*sizeof(int)); |
638 |
|
if (tmnew->histo != NULL) |
639 |
|
for (i = len; i--; ) |