12 |
|
#include "copyright.h" |
13 |
|
|
14 |
|
#include <stdio.h> |
15 |
+ |
#include <stdlib.h> |
16 |
|
#include <math.h> |
17 |
+ |
#include <string.h> |
18 |
|
#include "tmprivat.h" |
19 |
|
#include "tmerrmsg.h" |
20 |
|
|
67 |
|
tmnew->mongam = gamval; |
68 |
|
/* set color divisors */ |
69 |
|
for (i = 0; i < 3; i++) |
70 |
< |
tmnew->cdiv[i] = 256.*pow(tmnew->clf[i], 1./tmnew->mongam); |
70 |
> |
tmnew->cdiv[i] = TM_BRES*pow(tmnew->clf[i], 1./tmnew->mongam); |
71 |
|
|
72 |
|
/* set input transform */ |
73 |
|
tmnew->inppri = tmnew->monpri; |
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] = |
125 |
< |
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 */ |
137 |
< |
comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri); |
137 |
> |
if (!comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri)) |
138 |
> |
returnErr(TM_E_ILLEGAL); |
139 |
|
} |
140 |
|
for (i = 0; i < 3; i++) |
141 |
|
for (j = 0; j < 3; j++) |
142 |
|
tms->cmat[i][j] *= tms->inpsf; |
143 |
|
/* set color divisors */ |
144 |
|
for (i = 0; i < 3; i++) |
145 |
< |
if (tms->clf[i] > .001) |
146 |
< |
tms->cdiv[i] = |
145 |
< |
256.*pow(tms->clf[i], 1./tms->mongam); |
146 |
< |
else |
147 |
< |
tms->cdiv[i] = 1; |
145 |
> |
tms->cdiv[i] = TM_BRES*pow(tms->clf[i] < .001 ? .001 : |
146 |
> |
tms->clf[i], 1./tms->mongam); |
147 |
|
/* notify packages */ |
148 |
|
for (i = tmNumPkgs; i--; ) |
149 |
|
if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL) |
159 |
|
{ |
160 |
|
if (tms == NULL || tms->histo == NULL) |
161 |
|
return; |
162 |
< |
free((MEM_PTR)tms->histo); |
162 |
> |
free(tms->histo); |
163 |
|
tms->histo = NULL; |
164 |
|
} |
165 |
|
|
178 |
|
#endif |
179 |
|
return(TM_NOBRT); |
180 |
|
d = TM_BRTSCALE*log(lum); |
181 |
< |
if (d > 0.) |
183 |
< |
return((TMbright)(d+.5)); |
184 |
< |
return((TMbright)(d-.5)); |
181 |
> |
return((TMbright)(d + .5 - (d < 0.))); |
182 |
|
} |
183 |
|
|
184 |
|
|
252 |
|
tmCvColors( /* convert float colors */ |
253 |
|
TMstruct *tms, |
254 |
|
TMbright *ls, |
255 |
< |
BYTE *cs, |
255 |
> |
uby8 *cs, |
256 |
|
COLOR *scan, |
257 |
|
int len |
258 |
|
) |
259 |
|
{ |
260 |
|
static const char funcName[] = "tmCvColors"; |
261 |
< |
static BYTE gamtab[1024]; |
261 |
> |
static uby8 gamtab[1024]; |
262 |
|
static double curgam = .0; |
263 |
|
COLOR cmon; |
264 |
|
float lum, slum, d; |
374 |
|
horig = HISTI(tms->hbrmin); |
375 |
|
hlen = HISTI(tms->hbrmax) + 1 - horig; |
376 |
|
if (hlen > oldlen) { /* (re)allocate histogram */ |
377 |
< |
int *newhist = (int *)calloc(hlen, sizeof(int)); |
377 |
> |
HIST_TYP *newhist = (HIST_TYP *)calloc(hlen, sizeof(HIST_TYP)); |
378 |
|
if (newhist == NULL) |
379 |
|
returnErr(TM_E_NOMEM); |
380 |
|
if (oldlen) { /* copy and free old */ |
381 |
< |
for (i = oldlen, j = i+oldorig-horig; i; ) |
382 |
< |
newhist[--j] = tms->histo[--i]; |
383 |
< |
free((MEM_PTR)tms->histo); |
381 |
> |
memcpy(newhist+(oldorig-horig), |
382 |
> |
tms->histo, oldlen*sizeof(HIST_TYP)); |
383 |
> |
free(tms->histo); |
384 |
|
} |
385 |
|
tms->histo = newhist; |
386 |
|
} |
420 |
|
tmFixedMapping( /* compute fixed, linear tone-mapping */ |
421 |
|
TMstruct *tms, |
422 |
|
double expmult, |
423 |
< |
double gamval |
423 |
> |
double gamval, |
424 |
> |
double Lddyn |
425 |
|
) |
426 |
|
{ |
427 |
|
static const char funcName[] = "tmFixedMapping"; |
428 |
< |
double d; |
429 |
< |
int i; |
428 |
> |
const int maxV = (1L<<(8*sizeof(TMAP_TYP))) - 1; |
429 |
> |
double minD; |
430 |
> |
int i; |
431 |
|
|
432 |
|
if (!tmNewMap(tms)) |
433 |
|
returnErr(TM_E_NOMEM); |
434 |
< |
if (expmult <= .0) |
435 |
< |
expmult = 1.; |
436 |
< |
if (gamval < MINGAM) |
437 |
< |
gamval = tms->mongam; |
438 |
< |
d = log(expmult/tms->inpsf); |
434 |
> |
/* check arguments */ |
435 |
> |
if (expmult <= .0) expmult = 1.; |
436 |
> |
if (gamval < MINGAM) gamval = tms->mongam; |
437 |
> |
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; |
438 |
> |
minD = 1./Lddyn; |
439 |
|
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
440 |
< |
double val = 256. * exp( |
441 |
< |
( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) |
442 |
< |
/ gamval); |
443 |
< |
tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val; |
440 |
> |
double d; |
441 |
> |
d = expmult/tms->inpsf * tmLuminance(tms->mbrmin + i); |
442 |
> |
if (d >= 2.*minD) |
443 |
> |
d -= minD; |
444 |
> |
else /* soft black crushing */ |
445 |
> |
d *= d/(4.*minD); |
446 |
> |
d /= 1. - minD; |
447 |
> |
d = TM_BRES*pow(d, 1./gamval); |
448 |
> |
tms->lumap[i] = (d > maxV) ? maxV : (int)d; |
449 |
|
} |
450 |
|
returnOK; |
451 |
|
} |
460 |
|
) |
461 |
|
{ |
462 |
|
static const char funcName[] = "tmComputeMapping"; |
463 |
< |
int *histo; |
463 |
> |
HIST_TYP *histo; |
464 |
|
float *cumf; |
465 |
< |
int brt0, histlen, threshold, ceiling, trimmings; |
466 |
< |
double logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld; |
467 |
< |
int32 histot; |
465 |
> |
int brt0, histlen; |
466 |
> |
HIST_TYP threshold, ceiling, trimmings, histot; |
467 |
> |
double logLddyn, Ldmin, Lwavg, Tr, Lw, Ld; |
468 |
|
double sum; |
469 |
|
double d; |
470 |
|
int i, j; |
478 |
|
/* compute handy values */ |
479 |
|
Ldmin = Ldmax/Lddyn; |
480 |
|
logLddyn = log(Lddyn); |
477 |
– |
Ldavg = sqrt(Ldmax*Ldmin); |
481 |
|
i = HISTI(tms->hbrmin); |
482 |
|
brt0 = HISTV(i); |
483 |
|
histlen = HISTI(tms->hbrmax) + 1 - i; |
488 |
|
histot += tms->histo[i]; |
489 |
|
sum += (double)(j -= HISTEP) * tms->histo[i]; |
490 |
|
} |
488 |
– |
threshold = histot*0.005 + .5; |
491 |
|
if (!histot) |
492 |
|
returnErr(TM_E_TMFAIL); |
493 |
+ |
threshold = histot*0.025 + 1.; |
494 |
|
Lwavg = tmLuminance( (double)sum / histot ); |
495 |
|
/* use linear tone mapping? */ |
496 |
|
if (tms->flags & TM_F_LINEAR || threshold < 4 || |
497 |
|
tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) |
498 |
|
goto linearmap; |
499 |
|
/* clamp histogram */ |
500 |
< |
histo = (int *)malloc(histlen*sizeof(int)); |
500 |
> |
histo = (HIST_TYP *)malloc(histlen*sizeof(HIST_TYP)); |
501 |
|
cumf = (float *)malloc((histlen+2)*sizeof(float)); |
502 |
|
if ((histo == NULL) | (cumf == NULL)) |
503 |
|
returnErr(TM_E_NOMEM); |
504 |
|
cumf[histlen+1] = 1.; /* guard for assignment code */ |
505 |
< |
for (i = histlen; i--; ) /* make malleable copy */ |
506 |
< |
histo[i] = tms->histo[i]; |
505 |
> |
/* make malleable copy */ |
506 |
> |
memcpy(histo, tms->histo, histlen*sizeof(HIST_TYP)); |
507 |
|
do { /* iterate to solution */ |
508 |
|
sum = 0; /* cumulative probability */ |
509 |
|
for (i = 0; i < histlen; i++) { |
510 |
< |
cumf[i] = (double)sum/histot; |
511 |
< |
sum += histo[i]; |
510 |
> |
cumf[i] = sum/histot; |
511 |
> |
sum += (double)histo[i]; |
512 |
|
} |
513 |
|
cumf[histlen] = 1.; |
514 |
|
Tr = histot * (double)(tms->hbrmax - tms->hbrmin) / |
515 |
< |
((double)histlen*TM_BRTSCALE) / logLddyn; |
515 |
> |
((double)TM_BRTSCALE*histlen*logLddyn); |
516 |
|
ceiling = Tr + 1.; |
517 |
|
trimmings = 0; /* clip to envelope */ |
518 |
|
for (i = histlen; i--; ) { |
530 |
|
} |
531 |
|
/* check if we're out of data */ |
532 |
|
if ((histot -= trimmings) <= threshold) { |
533 |
< |
free((MEM_PTR)histo); |
534 |
< |
free((MEM_PTR)cumf); |
533 |
> |
free(histo); |
534 |
> |
free(cumf); |
535 |
|
goto linearmap; |
536 |
|
} |
537 |
< |
} while (trimmings > threshold); |
537 |
> |
threshold = 0.025*histot + 1.; |
538 |
> |
|
539 |
> |
} while ((threshold > 4) & (trimmings > threshold)); |
540 |
|
/* allocate space for mapping */ |
541 |
|
if (!tmNewMap(tms)) |
542 |
|
returnErr(TM_E_NOMEM); |
546 |
|
d -= (double)j; |
547 |
|
Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1])); |
548 |
|
d = (Ld - Ldmin)/(Ldmax - Ldmin); |
549 |
< |
tms->lumap[i] = 256.*pow(d, 1./gamval); |
549 |
> |
tms->lumap[i] = TM_BRES*pow(d, 1./gamval); |
550 |
|
} |
551 |
< |
free((MEM_PTR)histo); /* clean up and return */ |
552 |
< |
free((MEM_PTR)cumf); |
551 |
> |
free(histo); /* clean up and return */ |
552 |
> |
free(cumf); |
553 |
|
returnOK; |
554 |
|
linearmap: /* linear tone-mapping */ |
555 |
|
if (tms->flags & TM_F_HCONTR) |
556 |
< |
d = htcontrs(Ldavg) / htcontrs(Lwavg); |
556 |
> |
d = htcontrs(sqrt(Ldmax*Ldmin)) / htcontrs(Lwavg); |
557 |
|
else |
558 |
< |
d = Ldavg / Lwavg; |
559 |
< |
return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval)); |
558 |
> |
d = Ldmax / tmLuminance(tms->hbrmax); |
559 |
> |
return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval, Lddyn)); |
560 |
|
} |
561 |
|
|
562 |
|
|
563 |
|
int |
564 |
|
tmMapPixels( /* apply tone-mapping to pixel(s) */ |
565 |
|
TMstruct *tms, |
566 |
< |
BYTE *ps, |
566 |
> |
uby8 *ps, |
567 |
|
TMbright *ls, |
568 |
< |
BYTE *cs, |
568 |
> |
uby8 *cs, |
569 |
|
int len |
570 |
|
) |
571 |
|
{ |
572 |
|
static const char funcName[] = "tmMapPixels"; |
573 |
< |
int32 li, pv; |
573 |
> |
TMbright lv; |
574 |
> |
TMAP_TYP li; |
575 |
> |
int pv; |
576 |
|
|
577 |
|
if (tms == NULL || tms->lumap == NULL) |
578 |
|
returnErr(TM_E_TMINVAL); |
579 |
|
if ((ps == NULL) | (ls == NULL) | (len < 0)) |
580 |
|
returnErr(TM_E_ILLEGAL); |
581 |
|
while (len--) { |
582 |
< |
if ((li = *ls++) < tms->mbrmin) { |
582 |
> |
if ((lv = *ls++) < tms->mbrmin) { |
583 |
|
li = 0; |
584 |
|
} else { |
585 |
< |
if (li > tms->mbrmax) |
586 |
< |
li = tms->mbrmax; |
587 |
< |
li = tms->lumap[li - tms->mbrmin]; |
585 |
> |
if (lv > tms->mbrmax) |
586 |
> |
lv = tms->mbrmax; |
587 |
> |
li = tms->lumap[lv - tms->mbrmin]; |
588 |
|
} |
589 |
< |
if (cs == TM_NOCHROM) |
590 |
< |
*ps++ = li>255 ? 255 : li; |
591 |
< |
else { |
589 |
> |
if (cs == TM_NOCHROM) { |
590 |
> |
#if !(TM_BRES & 0xff) |
591 |
> |
*ps++ = li>=TM_BRES ? 255 : li/(TM_BRES>>8); |
592 |
> |
#else |
593 |
> |
*ps++ = li>=TM_BRES ? 255 : (li<<8)/TM_BRES; |
594 |
> |
#endif |
595 |
> |
} else { |
596 |
|
pv = *cs++ * li / tms->cdiv[RED]; |
597 |
|
*ps++ = pv>255 ? 255 : pv; |
598 |
|
pv = *cs++ * li / tms->cdiv[GRN]; |
606 |
|
|
607 |
|
|
608 |
|
TMstruct * |
609 |
< |
tmDup( /* duplicate top tone mapping */ |
609 |
> |
tmDup( /* duplicate tone mapping */ |
610 |
|
TMstruct *tms |
611 |
|
) |
612 |
|
{ |
622 |
|
*tmnew = *tms; /* copy everything */ |
623 |
|
if (tmnew->histo != NULL) { /* duplicate histogram */ |
624 |
|
len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); |
625 |
< |
tmnew->histo = (int *)malloc(len*sizeof(int)); |
625 |
> |
tmnew->histo = (HIST_TYP *)malloc(len*sizeof(HIST_TYP)); |
626 |
|
if (tmnew->histo != NULL) |
627 |
< |
for (i = len; i--; ) |
617 |
< |
tmnew->histo[i] = tms->histo[i]; |
627 |
> |
memcpy(tmnew->histo, tms->histo, len*sizeof(HIST_TYP)); |
628 |
|
} |
629 |
|
if (tmnew->lumap != NULL) { /* duplicate luminance mapping */ |
630 |
|
len = tmnew->mbrmax-tmnew->mbrmin+1; |
631 |
< |
tmnew->lumap = (unsigned short *)malloc( |
622 |
< |
len*sizeof(unsigned short) ); |
631 |
> |
tmnew->lumap = (TMAP_TYP *)malloc(len*sizeof(TMAP_TYP)); |
632 |
|
if (tmnew->lumap != NULL) |
633 |
< |
for (i = len; i--; ) |
625 |
< |
tmnew->lumap[i] = tms->lumap[i]; |
633 |
> |
memcpy(tmnew->lumap, tms->lumap, len*sizeof(TMAP_TYP)); |
634 |
|
} |
635 |
|
/* clear package data */ |
636 |
|
for (i = tmNumPkgs; i--; ) |
650 |
|
return; |
651 |
|
/* free tables */ |
652 |
|
if (tms->histo != NULL) |
653 |
< |
free((MEM_PTR)tms->histo); |
653 |
> |
free(tms->histo); |
654 |
|
if (tms->lumap != NULL) |
655 |
< |
free((MEM_PTR)tms->lumap); |
655 |
> |
free(tms->lumap); |
656 |
|
/* free private data */ |
657 |
|
for (i = tmNumPkgs; i--; ) |
658 |
|
if (tms->pd[i] != NULL) |
659 |
|
(*tmPkg[i]->Free)(tms->pd[i]); |
660 |
< |
free((MEM_PTR)tms); /* free basic structure */ |
660 |
> |
free(tms); /* free basic structure */ |
661 |
|
} |
662 |
|
|
663 |
|
/******************** Shared but Private library routines *********************/ |
664 |
|
|
665 |
< |
BYTE tmMesofact[BMESUPPER-BMESLOWER]; |
665 |
> |
uby8 tmMesofact[BMESUPPER-BMESLOWER]; |
666 |
|
|
667 |
|
void |
668 |
|
tmMkMesofact() /* build mesopic lookup factor table */ |
686 |
|
{ |
687 |
|
if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != |
688 |
|
(tms->hbrmax - tms->hbrmin)) { |
689 |
< |
free((MEM_PTR)tms->lumap); |
689 |
> |
free(tms->lumap); |
690 |
|
tms->lumap = NULL; |
691 |
|
} |
692 |
|
tms->mbrmin = tms->hbrmin; |
693 |
|
tms->mbrmax = tms->hbrmax; |
694 |
|
if (tms->mbrmin > tms->mbrmax) |
695 |
< |
return 0; |
695 |
> |
return(0); |
696 |
|
if (tms->lumap == NULL) |
697 |
< |
tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)* |
698 |
< |
(tms->mbrmax-tms->mbrmin+1)); |
697 |
> |
tms->lumap = (TMAP_TYP *)calloc(tms->mbrmax-tms->mbrmin+1, |
698 |
> |
sizeof(TMAP_TYP)); |
699 |
> |
else |
700 |
> |
memset(tms->lumap, 0, (tms->mbrmax-tms->mbrmin+1)*sizeof(TMAP_TYP)); |
701 |
> |
|
702 |
|
return(tms->lumap != NULL); |
703 |
|
} |
704 |
|
|