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; |
360 |
|
tms->hbrmin = tms->hbrmax = ls[i]; |
361 |
|
oldlen = 0; |
362 |
|
} else { |
363 |
< |
oldorig = (tms->hbrmin-MINBRT)/HISTEP; |
364 |
< |
oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig; |
363 |
> |
oldorig = HISTI(tms->hbrmin); |
364 |
> |
oldlen = HISTI(tms->hbrmax) + 1 - oldorig; |
365 |
|
} |
366 |
|
for (i = len; i--; ) { |
367 |
|
if ((j = ls[i]) < MINBRT) |
371 |
|
else if (j > tms->hbrmax) |
372 |
|
tms->hbrmax = j; |
373 |
|
} |
374 |
< |
horig = (tms->hbrmin-MINBRT)/HISTEP; |
375 |
< |
hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig; |
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 |
|
} |
388 |
|
returnOK; |
389 |
|
for (i = len; i--; ) /* add in new counts */ |
390 |
|
if (ls[i] >= MINBRT) |
391 |
< |
tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt; |
391 |
> |
tms->histo[ HISTI(ls[i]) - horig ] += wt; |
392 |
|
returnOK; |
393 |
|
} |
394 |
|
|
416 |
|
} |
417 |
|
|
418 |
|
|
422 |
– |
static int |
423 |
– |
tmNewMap( /* allocate new tone-mapping array */ |
424 |
– |
TMstruct *tms |
425 |
– |
) |
426 |
– |
{ |
427 |
– |
if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != |
428 |
– |
(tms->hbrmax - tms->hbrmin)) { |
429 |
– |
free((MEM_PTR)tms->lumap); |
430 |
– |
tms->lumap = NULL; |
431 |
– |
} |
432 |
– |
tms->mbrmin = tms->hbrmin; |
433 |
– |
tms->mbrmax = tms->hbrmax; |
434 |
– |
if (tms->mbrmin > tms->mbrmax) |
435 |
– |
return 0; |
436 |
– |
if (tms->lumap == NULL) |
437 |
– |
tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)* |
438 |
– |
(tms->mbrmax-tms->mbrmin+1)); |
439 |
– |
return(tms->lumap != NULL); |
440 |
– |
} |
441 |
– |
|
442 |
– |
|
419 |
|
int |
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; |
428 |
> |
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); |
439 |
< |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) |
440 |
< |
tms->lumap[i] = 256. * exp( |
441 |
< |
( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) |
442 |
< |
/ gamval ); |
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 d; |
441 |
> |
d = expmult/tms->inpsf * tmLuminance(tms->mbrmin + i); |
442 |
> |
if (d <= minD) |
443 |
> |
break; /* map initialized to zeroes */ |
444 |
> |
d = (d - minD)/(1. - minD); |
445 |
> |
d = TM_BRES*pow(d, 1./gamval); |
446 |
> |
tms->lumap[i] = (d >= maxV) ? maxV : (int)d; |
447 |
> |
} |
448 |
|
returnOK; |
449 |
|
} |
450 |
|
|
458 |
|
) |
459 |
|
{ |
460 |
|
static const char funcName[] = "tmComputeMapping"; |
461 |
< |
int *histo; |
461 |
> |
HIST_TYP *histo; |
462 |
|
float *cumf; |
463 |
< |
int brt0, histlen, threshold, ceiling, trimmings; |
464 |
< |
double logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld; |
465 |
< |
int32 histot; |
463 |
> |
int brt0, histlen; |
464 |
> |
HIST_TYP threshold, ceiling, trimmings, histot; |
465 |
> |
double logLddyn, Ldmin, Lwavg, Tr, Lw, Ld; |
466 |
|
double sum; |
467 |
|
double d; |
468 |
|
int i, j; |
476 |
|
/* compute handy values */ |
477 |
|
Ldmin = Ldmax/Lddyn; |
478 |
|
logLddyn = log(Lddyn); |
479 |
< |
Ldavg = sqrt(Ldmax*Ldmin); |
480 |
< |
i = (tms->hbrmin-MINBRT)/HISTEP; |
481 |
< |
brt0 = MINBRT + HISTEP/2 + i*HISTEP; |
499 |
< |
histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i; |
479 |
> |
i = HISTI(tms->hbrmin); |
480 |
> |
brt0 = HISTV(i); |
481 |
> |
histlen = HISTI(tms->hbrmax) + 1 - i; |
482 |
|
/* histogram total and mean */ |
483 |
|
histot = 0; sum = 0; |
484 |
|
j = brt0 + histlen*HISTEP; |
485 |
|
for (i = histlen; i--; ) { |
486 |
|
histot += tms->histo[i]; |
487 |
< |
sum += (j -= HISTEP) * tms->histo[i]; |
487 |
> |
sum += (double)(j -= HISTEP) * tms->histo[i]; |
488 |
|
} |
489 |
< |
threshold = histot*0.005 + .5; |
490 |
< |
if (threshold < 4) |
489 |
> |
threshold = histot*0.002 + .5; |
490 |
> |
if (!histot) |
491 |
|
returnErr(TM_E_TMFAIL); |
492 |
|
Lwavg = tmLuminance( (double)sum / histot ); |
511 |
– |
/* allocate space for mapping */ |
512 |
– |
if (!tmNewMap(tms)) |
513 |
– |
returnErr(TM_E_NOMEM); |
493 |
|
/* use linear tone mapping? */ |
494 |
< |
if (tms->flags & TM_F_LINEAR) |
494 |
> |
if (tms->flags & TM_F_LINEAR || threshold < 4 || |
495 |
> |
tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) |
496 |
|
goto linearmap; |
497 |
|
/* clamp histogram */ |
498 |
< |
histo = (int *)malloc(histlen*sizeof(int)); |
498 |
> |
histo = (HIST_TYP *)malloc(histlen*sizeof(HIST_TYP)); |
499 |
|
cumf = (float *)malloc((histlen+2)*sizeof(float)); |
500 |
|
if ((histo == NULL) | (cumf == NULL)) |
501 |
|
returnErr(TM_E_NOMEM); |
502 |
|
cumf[histlen+1] = 1.; /* guard for assignment code */ |
503 |
< |
for (i = histlen; i--; ) /* make malleable copy */ |
504 |
< |
histo[i] = tms->histo[i]; |
503 |
> |
/* make malleable copy */ |
504 |
> |
memcpy(histo, tms->histo, histlen*sizeof(HIST_TYP)); |
505 |
|
do { /* iterate to solution */ |
506 |
|
sum = 0; /* cumulative probability */ |
507 |
|
for (i = 0; i < histlen; i++) { |
508 |
< |
cumf[i] = (double)sum/histot; |
509 |
< |
sum += histo[i]; |
508 |
> |
cumf[i] = sum/histot; |
509 |
> |
sum += (double)histo[i]; |
510 |
|
} |
511 |
|
cumf[histlen] = 1.; |
512 |
|
Tr = histot * (double)(tms->hbrmax - tms->hbrmin) / |
513 |
< |
((double)histlen*TM_BRTSCALE) / logLddyn; |
513 |
> |
((double)TM_BRTSCALE*histlen*logLddyn); |
514 |
|
ceiling = Tr + 1.; |
515 |
|
trimmings = 0; /* clip to envelope */ |
516 |
|
for (i = histlen; i--; ) { |
528 |
|
} |
529 |
|
/* check if we're out of data */ |
530 |
|
if ((histot -= trimmings) <= threshold) { |
531 |
< |
free((MEM_PTR)histo); |
532 |
< |
free((MEM_PTR)cumf); |
531 |
> |
free(histo); |
532 |
> |
free(cumf); |
533 |
|
goto linearmap; |
534 |
|
} |
535 |
|
} while (trimmings > threshold); |
536 |
+ |
/* allocate space for mapping */ |
537 |
+ |
if (!tmNewMap(tms)) |
538 |
+ |
returnErr(TM_E_NOMEM); |
539 |
|
/* assign tone-mapping */ |
540 |
|
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
541 |
|
j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen; |
542 |
|
d -= (double)j; |
543 |
|
Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1])); |
544 |
|
d = (Ld - Ldmin)/(Ldmax - Ldmin); |
545 |
< |
tms->lumap[i] = 256.*pow(d, 1./gamval); |
545 |
> |
tms->lumap[i] = TM_BRES*pow(d, 1./gamval); |
546 |
|
} |
547 |
< |
free((MEM_PTR)histo); /* clean up and return */ |
548 |
< |
free((MEM_PTR)cumf); |
547 |
> |
free(histo); /* clean up and return */ |
548 |
> |
free(cumf); |
549 |
|
returnOK; |
550 |
|
linearmap: /* linear tone-mapping */ |
551 |
|
if (tms->flags & TM_F_HCONTR) |
552 |
< |
d = htcontrs(Ldavg) / htcontrs(Lwavg); |
552 |
> |
d = htcontrs(sqrt(Ldmax*Ldmin)) / htcontrs(Lwavg); |
553 |
|
else |
554 |
< |
d = Ldavg / Lwavg; |
555 |
< |
return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval)); |
554 |
> |
d = Ldmax / tmLuminance(tms->hbrmax); |
555 |
> |
return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval, Lddyn)); |
556 |
|
} |
557 |
|
|
558 |
|
|
559 |
|
int |
560 |
|
tmMapPixels( /* apply tone-mapping to pixel(s) */ |
561 |
|
TMstruct *tms, |
562 |
< |
BYTE *ps, |
562 |
> |
uby8 *ps, |
563 |
|
TMbright *ls, |
564 |
< |
BYTE *cs, |
564 |
> |
uby8 *cs, |
565 |
|
int len |
566 |
|
) |
567 |
|
{ |
568 |
|
static const char funcName[] = "tmMapPixels"; |
569 |
< |
int32 li, pv; |
569 |
> |
TMbright lv; |
570 |
> |
TMAP_TYP li; |
571 |
> |
int pv; |
572 |
|
|
573 |
|
if (tms == NULL || tms->lumap == NULL) |
574 |
|
returnErr(TM_E_TMINVAL); |
575 |
|
if ((ps == NULL) | (ls == NULL) | (len < 0)) |
576 |
|
returnErr(TM_E_ILLEGAL); |
577 |
|
while (len--) { |
578 |
< |
if ((li = *ls++) < tms->mbrmin) { |
578 |
> |
if ((lv = *ls++) < tms->mbrmin) { |
579 |
|
li = 0; |
580 |
|
} else { |
581 |
< |
if (li > tms->mbrmax) |
582 |
< |
li = tms->mbrmax; |
583 |
< |
li = tms->lumap[li - tms->mbrmin]; |
581 |
> |
if (lv > tms->mbrmax) |
582 |
> |
lv = tms->mbrmax; |
583 |
> |
li = tms->lumap[lv - tms->mbrmin]; |
584 |
|
} |
585 |
< |
if (cs == TM_NOCHROM) |
586 |
< |
*ps++ = li>255 ? 255 : li; |
587 |
< |
else { |
585 |
> |
if (cs == TM_NOCHROM) { |
586 |
> |
#if !(TM_BRES & 0xff) |
587 |
> |
*ps++ = li>=TM_BRES ? 255 : li/(TM_BRES>>8); |
588 |
> |
#else |
589 |
> |
*ps++ = li>=TM_BRES ? 255 : (li<<8)/TM_BRES; |
590 |
> |
#endif |
591 |
> |
} else { |
592 |
|
pv = *cs++ * li / tms->cdiv[RED]; |
593 |
|
*ps++ = pv>255 ? 255 : pv; |
594 |
|
pv = *cs++ * li / tms->cdiv[GRN]; |
601 |
|
} |
602 |
|
|
603 |
|
|
615 |
– |
|
616 |
– |
|
604 |
|
TMstruct * |
605 |
< |
tmDup( /* duplicate top tone mapping */ |
605 |
> |
tmDup( /* duplicate tone mapping */ |
606 |
|
TMstruct *tms |
607 |
|
) |
608 |
|
{ |
617 |
|
return(NULL); |
618 |
|
*tmnew = *tms; /* copy everything */ |
619 |
|
if (tmnew->histo != NULL) { /* duplicate histogram */ |
620 |
< |
len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 - |
621 |
< |
(tmnew->hbrmin-MINBRT)/HISTEP; |
635 |
< |
tmnew->histo = (int *)malloc(len*sizeof(int)); |
620 |
> |
len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); |
621 |
> |
tmnew->histo = (HIST_TYP *)malloc(len*sizeof(HIST_TYP)); |
622 |
|
if (tmnew->histo != NULL) |
623 |
< |
for (i = len; i--; ) |
638 |
< |
tmnew->histo[i] = tms->histo[i]; |
623 |
> |
memcpy(tmnew->histo, tms->histo, len*sizeof(HIST_TYP)); |
624 |
|
} |
625 |
|
if (tmnew->lumap != NULL) { /* duplicate luminance mapping */ |
626 |
|
len = tmnew->mbrmax-tmnew->mbrmin+1; |
627 |
< |
tmnew->lumap = (unsigned short *)malloc( |
643 |
< |
len*sizeof(unsigned short) ); |
627 |
> |
tmnew->lumap = (TMAP_TYP *)malloc(len*sizeof(TMAP_TYP)); |
628 |
|
if (tmnew->lumap != NULL) |
629 |
< |
for (i = len; i--; ) |
646 |
< |
tmnew->lumap[i] = tms->lumap[i]; |
629 |
> |
memcpy(tmnew->lumap, tms->lumap, len*sizeof(TMAP_TYP)); |
630 |
|
} |
631 |
|
/* clear package data */ |
632 |
|
for (i = tmNumPkgs; i--; ) |
646 |
|
return; |
647 |
|
/* free tables */ |
648 |
|
if (tms->histo != NULL) |
649 |
< |
free((MEM_PTR)tms->histo); |
649 |
> |
free(tms->histo); |
650 |
|
if (tms->lumap != NULL) |
651 |
< |
free((MEM_PTR)tms->lumap); |
651 |
> |
free(tms->lumap); |
652 |
|
/* free private data */ |
653 |
|
for (i = tmNumPkgs; i--; ) |
654 |
|
if (tms->pd[i] != NULL) |
655 |
|
(*tmPkg[i]->Free)(tms->pd[i]); |
656 |
< |
free((MEM_PTR)tms); /* free basic structure */ |
656 |
> |
free(tms); /* free basic structure */ |
657 |
|
} |
658 |
|
|
659 |
|
/******************** Shared but Private library routines *********************/ |
660 |
|
|
661 |
< |
BYTE tmMesofact[BMESUPPER-BMESLOWER]; |
661 |
> |
uby8 tmMesofact[BMESUPPER-BMESLOWER]; |
662 |
|
|
663 |
|
void |
664 |
|
tmMkMesofact() /* build mesopic lookup factor table */ |
672 |
|
tmMesofact[i-BMESLOWER] = 256. * |
673 |
|
(tmLuminance(i) - LMESLOWER) / |
674 |
|
(LMESUPPER - LMESLOWER); |
675 |
+ |
} |
676 |
+ |
|
677 |
+ |
|
678 |
+ |
int |
679 |
+ |
tmNewMap( /* allocate new tone-mapping array */ |
680 |
+ |
TMstruct *tms |
681 |
+ |
) |
682 |
+ |
{ |
683 |
+ |
if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != |
684 |
+ |
(tms->hbrmax - tms->hbrmin)) { |
685 |
+ |
free(tms->lumap); |
686 |
+ |
tms->lumap = NULL; |
687 |
+ |
} |
688 |
+ |
tms->mbrmin = tms->hbrmin; |
689 |
+ |
tms->mbrmax = tms->hbrmax; |
690 |
+ |
if (tms->mbrmin > tms->mbrmax) |
691 |
+ |
return 0; |
692 |
+ |
if (tms->lumap == NULL) |
693 |
+ |
tms->lumap = (TMAP_TYP *)calloc(tms->mbrmax-tms->mbrmin+1, |
694 |
+ |
sizeof(TMAP_TYP)); |
695 |
+ |
else |
696 |
+ |
memset(tms->lumap, 0, (tms->mbrmax-tms->mbrmin+1)*sizeof(TMAP_TYP)); |
697 |
+ |
|
698 |
+ |
return(tms->lumap != NULL); |
699 |
|
} |
700 |
|
|
701 |
|
|