12 |
|
#include "copyright.h" |
13 |
|
|
14 |
|
#include <stdio.h> |
15 |
+ |
#include <stdlib.h> |
16 |
|
#include <math.h> |
17 |
|
#include "tmprivat.h" |
18 |
|
#include "tmerrmsg.h" |
364 |
|
tms->hbrmin = tms->hbrmax = ls[i]; |
365 |
|
oldlen = 0; |
366 |
|
} else { |
367 |
< |
oldorig = (tms->hbrmin-MINBRT)/HISTEP; |
368 |
< |
oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig; |
367 |
> |
oldorig = HISTI(tms->hbrmin); |
368 |
> |
oldlen = HISTI(tms->hbrmax) + 1 - oldorig; |
369 |
|
} |
370 |
|
for (i = len; i--; ) { |
371 |
|
if ((j = ls[i]) < MINBRT) |
375 |
|
else if (j > tms->hbrmax) |
376 |
|
tms->hbrmax = j; |
377 |
|
} |
378 |
< |
horig = (tms->hbrmin-MINBRT)/HISTEP; |
379 |
< |
hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig; |
378 |
> |
horig = HISTI(tms->hbrmin); |
379 |
> |
hlen = HISTI(tms->hbrmax) + 1 - horig; |
380 |
|
if (hlen > oldlen) { /* (re)allocate histogram */ |
381 |
|
int *newhist = (int *)calloc(hlen, sizeof(int)); |
382 |
|
if (newhist == NULL) |
392 |
|
returnOK; |
393 |
|
for (i = len; i--; ) /* add in new counts */ |
394 |
|
if (ls[i] >= MINBRT) |
395 |
< |
tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt; |
395 |
> |
tms->histo[ HISTI(ls[i]) - horig ] += wt; |
396 |
|
returnOK; |
397 |
|
} |
398 |
|
|
420 |
|
} |
421 |
|
|
422 |
|
|
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 |
– |
|
423 |
|
int |
424 |
|
tmFixedMapping( /* compute fixed, linear tone-mapping */ |
425 |
|
TMstruct *tms, |
476 |
|
Ldmin = Ldmax/Lddyn; |
477 |
|
logLddyn = log(Lddyn); |
478 |
|
Ldavg = sqrt(Ldmax*Ldmin); |
479 |
< |
i = (tms->hbrmin-MINBRT)/HISTEP; |
480 |
< |
brt0 = MINBRT + HISTEP/2 + i*HISTEP; |
481 |
< |
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 (!histot) |
491 |
|
returnErr(TM_E_TMFAIL); |
492 |
|
Lwavg = tmLuminance( (double)sum / histot ); |
493 |
|
/* use linear tone mapping? */ |
494 |
< |
if (tms->flags & TM_F_LINEAR || threshold < 4) |
494 |
> |
if (tms->flags & TM_F_LINEAR || threshold < 4 || |
495 |
> |
tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) |
496 |
|
goto linearmap; |
516 |
– |
/* allocate space for mapping */ |
517 |
– |
if (!tmNewMap(tms)) |
518 |
– |
returnErr(TM_E_NOMEM); |
497 |
|
/* clamp histogram */ |
498 |
|
histo = (int *)malloc(histlen*sizeof(int)); |
499 |
|
cumf = (float *)malloc((histlen+2)*sizeof(float)); |
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; |
595 |
|
} |
596 |
|
|
597 |
|
|
617 |
– |
|
618 |
– |
|
598 |
|
TMstruct * |
599 |
|
tmDup( /* duplicate top tone mapping */ |
600 |
|
TMstruct *tms |
611 |
|
return(NULL); |
612 |
|
*tmnew = *tms; /* copy everything */ |
613 |
|
if (tmnew->histo != NULL) { /* duplicate histogram */ |
614 |
< |
len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 - |
636 |
< |
(tmnew->hbrmin-MINBRT)/HISTEP; |
614 |
> |
len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); |
615 |
|
tmnew->histo = (int *)malloc(len*sizeof(int)); |
616 |
|
if (tmnew->histo != NULL) |
617 |
|
for (i = len; i--; ) |
669 |
|
tmMesofact[i-BMESLOWER] = 256. * |
670 |
|
(tmLuminance(i) - LMESLOWER) / |
671 |
|
(LMESUPPER - LMESLOWER); |
672 |
+ |
} |
673 |
+ |
|
674 |
+ |
|
675 |
+ |
int |
676 |
+ |
tmNewMap( /* allocate new tone-mapping array */ |
677 |
+ |
TMstruct *tms |
678 |
+ |
) |
679 |
+ |
{ |
680 |
+ |
if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != |
681 |
+ |
(tms->hbrmax - tms->hbrmin)) { |
682 |
+ |
free((MEM_PTR)tms->lumap); |
683 |
+ |
tms->lumap = NULL; |
684 |
+ |
} |
685 |
+ |
tms->mbrmin = tms->hbrmin; |
686 |
+ |
tms->mbrmax = tms->hbrmax; |
687 |
+ |
if (tms->mbrmin > tms->mbrmax) |
688 |
+ |
return 0; |
689 |
+ |
if (tms->lumap == NULL) |
690 |
+ |
tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)* |
691 |
+ |
(tms->mbrmax-tms->mbrmin+1)); |
692 |
+ |
return(tms->lumap != NULL); |
693 |
|
} |
694 |
|
|
695 |
|
|