123 |
|
tms->clf[GRN] = tms->cmat[1][1]; |
124 |
|
tms->clf[BLU] = tms->cmat[1][2]; |
125 |
|
} |
126 |
< |
tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] = |
127 |
< |
tms->inpsf; |
126 |
> |
tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] = 1.; |
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 */ |
130 |
> |
} else if (tms->inppri == TM_XYZPRIM) { /* input is XYZ */ |
131 |
|
compxyz2rgbWBmat(tms->cmat, tms->monpri); |
132 |
|
|
133 |
< |
else { /* input is RGB */ |
133 |
> |
} else { /* input is RGB */ |
134 |
|
if (tms->inppri != tms->monpri && |
135 |
|
PRIMEQ(tms->inppri, tms->monpri)) |
136 |
|
tms->inppri = tms->monpri; /* no xform */ |
159 |
|
{ |
160 |
|
if (tms == NULL || tms->histo == NULL) |
161 |
|
return; |
162 |
< |
free((MEM_PTR)tms->histo); |
162 |
> |
free(tms->histo); |
163 |
> |
tms->hbrmin = 10; tms->hbrmax = -10; |
164 |
|
tms->histo = NULL; |
165 |
|
} |
166 |
|
|
344 |
|
{ |
345 |
|
static const char funcName[] = "tmAddHisto"; |
346 |
|
int oldorig=0, oldlen, horig, hlen; |
347 |
< |
int i, j; |
347 |
> |
int i; |
348 |
|
|
349 |
|
if (tms == NULL) |
350 |
|
returnErr(TM_E_TMINVAL); |
365 |
|
oldlen = HISTI(tms->hbrmax) + 1 - oldorig; |
366 |
|
} |
367 |
|
for (i = len; i--; ) { |
368 |
< |
if ((j = ls[i]) < MINBRT) |
368 |
> |
if (ls[i] < MINBRT) |
369 |
|
continue; |
370 |
< |
if (j < tms->hbrmin) |
371 |
< |
tms->hbrmin = j; |
372 |
< |
else if (j > tms->hbrmax) |
373 |
< |
tms->hbrmax = j; |
370 |
> |
if (ls[i] < tms->hbrmin) |
371 |
> |
tms->hbrmin = ls[i]; |
372 |
> |
else if (ls[i] > tms->hbrmax) |
373 |
> |
tms->hbrmax = ls[i]; |
374 |
|
} |
375 |
|
horig = HISTI(tms->hbrmin); |
376 |
|
hlen = HISTI(tms->hbrmax) + 1 - horig; |
379 |
|
if (newhist == NULL) |
380 |
|
returnErr(TM_E_NOMEM); |
381 |
|
if (oldlen) { /* copy and free old */ |
382 |
< |
memcpy((MEM_PTR)(newhist+(oldorig-horig)), |
383 |
< |
(MEM_PTR)tms->histo, oldlen*sizeof(HIST_TYP)); |
384 |
< |
free((MEM_PTR)tms->histo); |
382 |
> |
memcpy(newhist+(oldorig-horig), |
383 |
> |
tms->histo, oldlen*sizeof(HIST_TYP)); |
384 |
> |
free(tms->histo); |
385 |
|
} |
386 |
|
tms->histo = newhist; |
387 |
|
} |
421 |
|
tmFixedMapping( /* compute fixed, linear tone-mapping */ |
422 |
|
TMstruct *tms, |
423 |
|
double expmult, |
424 |
< |
double gamval |
424 |
> |
double gamval, |
425 |
> |
double Lddyn |
426 |
|
) |
427 |
|
{ |
428 |
|
static const char funcName[] = "tmFixedMapping"; |
429 |
< |
double d; |
430 |
< |
int i; |
429 |
> |
const int maxV = (1L<<(8*sizeof(TMAP_TYP))) - 1; |
430 |
> |
double minD; |
431 |
> |
int i; |
432 |
|
|
433 |
|
if (!tmNewMap(tms)) |
434 |
|
returnErr(TM_E_NOMEM); |
435 |
< |
if (expmult <= .0) |
436 |
< |
expmult = 1.; |
437 |
< |
if (gamval < MINGAM) |
438 |
< |
gamval = tms->mongam; |
439 |
< |
d = log(expmult/tms->inpsf); |
435 |
> |
/* check arguments */ |
436 |
> |
if (expmult <= .0) expmult = 1.; |
437 |
> |
if (gamval < MINGAM) gamval = tms->mongam; |
438 |
> |
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; |
439 |
> |
minD = 1./Lddyn; |
440 |
|
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
441 |
< |
double val = TM_BRES * exp( |
442 |
< |
( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) |
443 |
< |
/ gamval); |
444 |
< |
tms->lumap[i] = val; |
445 |
< |
if (sizeof(TMAP_TYP) == 2 && val >= 0xffff) |
446 |
< |
tms->lumap[i] = 0xffff; |
441 |
> |
double d; |
442 |
> |
d = expmult/tms->inpsf * tmLuminance(tms->mbrmin + i); |
443 |
> |
if (d >= 2.*minD) |
444 |
> |
d -= minD; |
445 |
> |
else /* soft black crushing */ |
446 |
> |
d *= d/(4.*minD); |
447 |
> |
d /= 1. - minD; |
448 |
> |
d = TM_BRES*pow(d, 1./gamval); |
449 |
> |
tms->lumap[i] = (d > maxV) ? maxV : (int)d; |
450 |
|
} |
451 |
|
returnOK; |
452 |
|
} |
489 |
|
histot += tms->histo[i]; |
490 |
|
sum += (double)(j -= HISTEP) * tms->histo[i]; |
491 |
|
} |
487 |
– |
threshold = histot*0.002 + .5; |
492 |
|
if (!histot) |
493 |
|
returnErr(TM_E_TMFAIL); |
494 |
+ |
threshold = histot/500 + 1; |
495 |
|
Lwavg = tmLuminance( (double)sum / histot ); |
496 |
|
/* use linear tone mapping? */ |
497 |
< |
if (tms->flags & TM_F_LINEAR || threshold < 4 || |
497 |
> |
if (tms->flags & TM_F_LINEAR || |
498 |
|
tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) |
499 |
|
goto linearmap; |
500 |
|
/* clamp histogram */ |
504 |
|
returnErr(TM_E_NOMEM); |
505 |
|
cumf[histlen+1] = 1.; /* guard for assignment code */ |
506 |
|
/* make malleable copy */ |
507 |
< |
memcpy((MEM_PTR)histo, (MEM_PTR)tms->histo, histlen*sizeof(HIST_TYP)); |
507 |
> |
memcpy(histo, tms->histo, histlen*sizeof(HIST_TYP)); |
508 |
|
do { /* iterate to solution */ |
509 |
|
sum = 0; /* cumulative probability */ |
510 |
|
for (i = 0; i < histlen; i++) { |
531 |
|
} |
532 |
|
/* check if we're out of data */ |
533 |
|
if ((histot -= trimmings) <= threshold) { |
534 |
< |
free((MEM_PTR)histo); |
535 |
< |
free((MEM_PTR)cumf); |
534 |
> |
free(histo); |
535 |
> |
free(cumf); |
536 |
|
goto linearmap; |
537 |
|
} |
538 |
< |
} while (trimmings > threshold); |
538 |
> |
} while (40*trimmings > histot); |
539 |
|
/* allocate space for mapping */ |
540 |
|
if (!tmNewMap(tms)) |
541 |
|
returnErr(TM_E_NOMEM); |
547 |
|
d = (Ld - Ldmin)/(Ldmax - Ldmin); |
548 |
|
tms->lumap[i] = TM_BRES*pow(d, 1./gamval); |
549 |
|
} |
550 |
< |
free((MEM_PTR)histo); /* clean up and return */ |
551 |
< |
free((MEM_PTR)cumf); |
550 |
> |
free(histo); /* clean up and return */ |
551 |
> |
free(cumf); |
552 |
|
returnOK; |
553 |
|
linearmap: /* linear tone-mapping */ |
554 |
|
if (tms->flags & TM_F_HCONTR) |
555 |
|
d = htcontrs(sqrt(Ldmax*Ldmin)) / htcontrs(Lwavg); |
556 |
|
else |
557 |
|
d = Ldmax / tmLuminance(tms->hbrmax); |
558 |
< |
return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval)); |
558 |
> |
return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval, Lddyn)); |
559 |
|
} |
560 |
|
|
561 |
|
|
623 |
|
len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); |
624 |
|
tmnew->histo = (HIST_TYP *)malloc(len*sizeof(HIST_TYP)); |
625 |
|
if (tmnew->histo != NULL) |
626 |
< |
memcpy((MEM_PTR)tmnew->histo, (MEM_PTR)tms->histo, |
622 |
< |
len*sizeof(HIST_TYP)); |
626 |
> |
memcpy(tmnew->histo, tms->histo, len*sizeof(HIST_TYP)); |
627 |
|
} |
628 |
|
if (tmnew->lumap != NULL) { /* duplicate luminance mapping */ |
629 |
|
len = tmnew->mbrmax-tmnew->mbrmin+1; |
630 |
|
tmnew->lumap = (TMAP_TYP *)malloc(len*sizeof(TMAP_TYP)); |
631 |
|
if (tmnew->lumap != NULL) |
632 |
< |
memcpy((MEM_PTR)tmnew->lumap, (MEM_PTR)tms->lumap, |
629 |
< |
len*sizeof(TMAP_TYP)); |
632 |
> |
memcpy(tmnew->lumap, tms->lumap, len*sizeof(TMAP_TYP)); |
633 |
|
} |
634 |
|
/* clear package data */ |
635 |
|
for (i = tmNumPkgs; i--; ) |
649 |
|
return; |
650 |
|
/* free tables */ |
651 |
|
if (tms->histo != NULL) |
652 |
< |
free((MEM_PTR)tms->histo); |
652 |
> |
free(tms->histo); |
653 |
|
if (tms->lumap != NULL) |
654 |
< |
free((MEM_PTR)tms->lumap); |
654 |
> |
free(tms->lumap); |
655 |
|
/* free private data */ |
656 |
|
for (i = tmNumPkgs; i--; ) |
657 |
|
if (tms->pd[i] != NULL) |
658 |
|
(*tmPkg[i]->Free)(tms->pd[i]); |
659 |
< |
free((MEM_PTR)tms); /* free basic structure */ |
659 |
> |
free(tms); /* free basic structure */ |
660 |
|
} |
661 |
|
|
662 |
|
/******************** Shared but Private library routines *********************/ |
685 |
|
{ |
686 |
|
if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != |
687 |
|
(tms->hbrmax - tms->hbrmin)) { |
688 |
< |
free((MEM_PTR)tms->lumap); |
688 |
> |
free(tms->lumap); |
689 |
|
tms->lumap = NULL; |
690 |
|
} |
691 |
|
tms->mbrmin = tms->hbrmin; |
692 |
|
tms->mbrmax = tms->hbrmax; |
693 |
|
if (tms->mbrmin > tms->mbrmax) |
694 |
< |
return 0; |
694 |
> |
return(0); |
695 |
|
if (tms->lumap == NULL) |
696 |
< |
tms->lumap = (TMAP_TYP *)malloc(sizeof(TMAP_TYP)* |
697 |
< |
(tms->mbrmax-tms->mbrmin+1)); |
696 |
> |
tms->lumap = (TMAP_TYP *)calloc(tms->mbrmax-tms->mbrmin+1, |
697 |
> |
sizeof(TMAP_TYP)); |
698 |
> |
else |
699 |
> |
memset(tms->lumap, 0, (tms->mbrmax-tms->mbrmin+1)*sizeof(TMAP_TYP)); |
700 |
> |
|
701 |
|
return(tms->lumap != NULL); |
702 |
|
} |
703 |
|
|