232 |
|
) |
233 |
|
{ |
234 |
|
static const char funcName[] = "tmCvGrays"; |
235 |
– |
double d; |
235 |
|
int i; |
236 |
|
|
237 |
|
if (tms == NULL) |
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 |
|
|
419 |
|
} |
420 |
|
|
421 |
|
|
423 |
– |
static int |
424 |
– |
tmNewMap( /* allocate new tone-mapping array */ |
425 |
– |
TMstruct *tms |
426 |
– |
) |
427 |
– |
{ |
428 |
– |
if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != |
429 |
– |
(tms->hbrmax - tms->hbrmin)) { |
430 |
– |
free((MEM_PTR)tms->lumap); |
431 |
– |
tms->lumap = NULL; |
432 |
– |
} |
433 |
– |
tms->mbrmin = tms->hbrmin; |
434 |
– |
tms->mbrmax = tms->hbrmax; |
435 |
– |
if (tms->mbrmin > tms->mbrmax) |
436 |
– |
return 0; |
437 |
– |
if (tms->lumap == NULL) |
438 |
– |
tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)* |
439 |
– |
(tms->mbrmax-tms->mbrmin+1)); |
440 |
– |
return(tms->lumap != NULL); |
441 |
– |
} |
442 |
– |
|
443 |
– |
|
422 |
|
int |
423 |
|
tmFixedMapping( /* compute fixed, linear tone-mapping */ |
424 |
|
TMstruct *tms, |
437 |
|
if (gamval < MINGAM) |
438 |
|
gamval = tms->mongam; |
439 |
|
d = log(expmult/tms->inpsf); |
440 |
< |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) |
441 |
< |
tms->lumap[i] = 256. * exp( |
440 |
> |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
441 |
> |
double val = 256. * exp( |
442 |
|
( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) |
443 |
< |
/ gamval ); |
443 |
> |
/ gamval); |
444 |
> |
tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val; |
445 |
> |
} |
446 |
|
returnOK; |
447 |
|
} |
448 |
|
|
475 |
|
Ldmin = Ldmax/Lddyn; |
476 |
|
logLddyn = log(Lddyn); |
477 |
|
Ldavg = sqrt(Ldmax*Ldmin); |
478 |
< |
i = (tms->hbrmin-MINBRT)/HISTEP; |
479 |
< |
brt0 = MINBRT + HISTEP/2 + i*HISTEP; |
480 |
< |
histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i; |
478 |
> |
i = HISTI(tms->hbrmin); |
479 |
> |
brt0 = HISTV(i); |
480 |
> |
histlen = HISTI(tms->hbrmax) + 1 - i; |
481 |
|
/* histogram total and mean */ |
482 |
|
histot = 0; sum = 0; |
483 |
|
j = brt0 + histlen*HISTEP; |
484 |
|
for (i = histlen; i--; ) { |
485 |
|
histot += tms->histo[i]; |
486 |
< |
sum += (j -= HISTEP) * tms->histo[i]; |
486 |
> |
sum += (double)(j -= HISTEP) * tms->histo[i]; |
487 |
|
} |
488 |
|
threshold = histot*0.005 + .5; |
489 |
< |
if (threshold < 4) |
489 |
> |
if (!histot) |
490 |
|
returnErr(TM_E_TMFAIL); |
491 |
|
Lwavg = tmLuminance( (double)sum / histot ); |
512 |
– |
/* allocate space for mapping */ |
513 |
– |
if (!tmNewMap(tms)) |
514 |
– |
returnErr(TM_E_NOMEM); |
492 |
|
/* use linear tone mapping? */ |
493 |
< |
if (tms->flags & TM_F_LINEAR) |
493 |
> |
if (tms->flags & TM_F_LINEAR || threshold < 4 || |
494 |
> |
tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) |
495 |
|
goto linearmap; |
496 |
|
/* clamp histogram */ |
497 |
|
histo = (int *)malloc(histlen*sizeof(int)); |
532 |
|
goto linearmap; |
533 |
|
} |
534 |
|
} while (trimmings > threshold); |
535 |
+ |
/* allocate space for mapping */ |
536 |
+ |
if (!tmNewMap(tms)) |
537 |
+ |
returnErr(TM_E_NOMEM); |
538 |
|
/* assign tone-mapping */ |
539 |
|
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
540 |
|
j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen; |
594 |
|
} |
595 |
|
|
596 |
|
|
616 |
– |
|
617 |
– |
|
597 |
|
TMstruct * |
598 |
|
tmDup( /* duplicate top tone mapping */ |
599 |
|
TMstruct *tms |
610 |
|
return(NULL); |
611 |
|
*tmnew = *tms; /* copy everything */ |
612 |
|
if (tmnew->histo != NULL) { /* duplicate histogram */ |
613 |
< |
len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 - |
635 |
< |
(tmnew->hbrmin-MINBRT)/HISTEP; |
613 |
> |
len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); |
614 |
|
tmnew->histo = (int *)malloc(len*sizeof(int)); |
615 |
|
if (tmnew->histo != NULL) |
616 |
|
for (i = len; i--; ) |
668 |
|
tmMesofact[i-BMESLOWER] = 256. * |
669 |
|
(tmLuminance(i) - LMESLOWER) / |
670 |
|
(LMESUPPER - LMESLOWER); |
671 |
+ |
} |
672 |
+ |
|
673 |
+ |
|
674 |
+ |
int |
675 |
+ |
tmNewMap( /* allocate new tone-mapping array */ |
676 |
+ |
TMstruct *tms |
677 |
+ |
) |
678 |
+ |
{ |
679 |
+ |
if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != |
680 |
+ |
(tms->hbrmax - tms->hbrmin)) { |
681 |
+ |
free((MEM_PTR)tms->lumap); |
682 |
+ |
tms->lumap = NULL; |
683 |
+ |
} |
684 |
+ |
tms->mbrmin = tms->hbrmin; |
685 |
+ |
tms->mbrmax = tms->hbrmax; |
686 |
+ |
if (tms->mbrmin > tms->mbrmax) |
687 |
+ |
return 0; |
688 |
+ |
if (tms->lumap == NULL) |
689 |
+ |
tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)* |
690 |
+ |
(tms->mbrmax-tms->mbrmin+1)); |
691 |
+ |
return(tms->lumap != NULL); |
692 |
|
} |
693 |
|
|
694 |
|
|