| 22 |
|
struct tmPackage *tmPkg[TM_MAXPKG]; |
| 23 |
|
int tmNumPkgs = 0; /* number of registered packages */ |
| 24 |
|
|
| 25 |
+ |
int tmLastError; /* last error incurred by library */ |
| 26 |
+ |
char *tmLastFunction; /* error-generating function name */ |
| 27 |
|
|
| 28 |
+ |
|
| 29 |
|
int |
| 30 |
|
tmErrorReturn(func, err) /* error return (with message) */ |
| 31 |
|
char *func; |
| 32 |
|
int err; |
| 33 |
|
{ |
| 34 |
+ |
tmLastFunction = func; |
| 35 |
+ |
tmLastError = err; |
| 36 |
|
if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR) |
| 37 |
|
return(err); |
| 38 |
|
fputs(func, stderr); |
| 86 |
|
tmnew->inpsf = WHTEFFICACY; |
| 87 |
|
tmnew->cmat[0][1] = tmnew->cmat[0][2] = tmnew->cmat[1][0] = |
| 88 |
|
tmnew->cmat[1][2] = tmnew->cmat[2][0] = tmnew->cmat[2][1] = 0.; |
| 89 |
< |
tmnew->brmin = tmnew->brmax = 0; |
| 89 |
> |
tmnew->hbrmin = tmnew->hbrmax = 0; |
| 90 |
|
tmnew->histo = NULL; |
| 91 |
+ |
tmnew->mbrmin = tmnew->mbrmax = 0; |
| 92 |
|
tmnew->lumap = NULL; |
| 93 |
|
/* zero private data */ |
| 94 |
|
for (i = TM_MAXPKG; i--; ) |
| 267 |
|
; |
| 268 |
|
if (i < 0) |
| 269 |
|
returnOK; |
| 270 |
< |
tmTop->brmin = tmTop->brmax = ls[i]; |
| 270 |
> |
tmTop->hbrmin = tmTop->hbrmax = ls[i]; |
| 271 |
|
oldlen = 0; |
| 272 |
|
} else { |
| 273 |
< |
oldorig = (tmTop->brmin-MINBRT)/HISTEP; |
| 274 |
< |
oldlen = (tmTop->brmax-MINBRT)/HISTEP + 1 - oldorig; |
| 273 |
> |
oldorig = (tmTop->hbrmin-MINBRT)/HISTEP; |
| 274 |
> |
oldlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - oldorig; |
| 275 |
|
} |
| 276 |
|
for (i = len; i--; ) { |
| 277 |
|
if ((j = ls[i]) < MINBRT) |
| 278 |
|
continue; |
| 279 |
< |
if (j < tmTop->brmin) |
| 280 |
< |
tmTop->brmin = j; |
| 281 |
< |
else if (j > tmTop->brmax) |
| 282 |
< |
tmTop->brmax = j; |
| 279 |
> |
if (j < tmTop->hbrmin) |
| 280 |
> |
tmTop->hbrmin = j; |
| 281 |
> |
else if (j > tmTop->hbrmax) |
| 282 |
> |
tmTop->hbrmax = j; |
| 283 |
|
} |
| 284 |
< |
horig = (tmTop->brmin-MINBRT)/HISTEP; |
| 285 |
< |
hlen = (tmTop->brmax-MINBRT)/HISTEP + 1 - horig; |
| 284 |
> |
horig = (tmTop->hbrmin-MINBRT)/HISTEP; |
| 285 |
> |
hlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - horig; |
| 286 |
|
if (hlen > oldlen) { /* (re)allocate histogram */ |
| 287 |
|
register int *newhist = (int *)calloc(hlen, sizeof(int)); |
| 288 |
|
if (newhist == NULL) |
| 293 |
|
free((MEM_PTR)tmTop->histo); |
| 294 |
|
} |
| 295 |
|
tmTop->histo = newhist; |
| 290 |
– |
if (tmTop->lumap != NULL) { /* invalid tone map */ |
| 291 |
– |
free((MEM_PTR)tmTop->lumap); |
| 292 |
– |
tmTop->lumap = NULL; |
| 293 |
– |
} |
| 296 |
|
} |
| 297 |
|
if (wt == 0) |
| 298 |
|
returnOK; |
| 350 |
|
Ldmin = Ldmax/Lddyn; |
| 351 |
|
logLddyn = log(Lddyn); |
| 352 |
|
Ldavg = sqrt(Ldmax*Ldmin); |
| 353 |
< |
i = (tmTop->brmin-MINBRT)/HISTEP; |
| 353 |
> |
i = (tmTop->hbrmin-MINBRT)/HISTEP; |
| 354 |
|
brt0 = MINBRT + HISTEP/2 + i*HISTEP; |
| 355 |
< |
histlen = (tmTop->brmax-MINBRT)/HISTEP + 1 - i; |
| 355 |
> |
histlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - i; |
| 356 |
|
/* histogram total and mean */ |
| 357 |
|
histot = 0; sum = 0; |
| 358 |
|
j = brt0 + histlen*HISTEP; |
| 378 |
|
sum += histo[i]; |
| 379 |
|
} |
| 380 |
|
cumf[i] = 1.; |
| 381 |
< |
Tr = histot * (double)(tmTop->brmax - tmTop->brmin) / |
| 381 |
> |
Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) / |
| 382 |
|
((double)histlen*TM_BRTSCALE) / logLddyn; |
| 383 |
|
ceiling = Tr + 1.; |
| 384 |
|
trimmings = 0; /* clip to envelope */ |
| 399 |
|
trimmings > threshold); |
| 400 |
|
} |
| 401 |
|
/* allocate luminance map */ |
| 402 |
< |
if (tmTop->lumap == NULL) { |
| 403 |
< |
tmTop->lumap = (unsigned short *)malloc( |
| 404 |
< |
(tmTop->brmax-tmTop->brmin+1)*sizeof(unsigned short) ); |
| 405 |
< |
if (tmTop->lumap == NULL) |
| 406 |
< |
returnErr(TM_E_NOMEM); |
| 407 |
< |
} |
| 402 |
> |
if (tmTop->lumap != NULL) |
| 403 |
> |
free((MEM_PTR)tmTop->lumap); |
| 404 |
> |
tmTop->mbrmin = tmTop->hbrmin; |
| 405 |
> |
tmTop->mbrmax = tmTop->hbrmax; |
| 406 |
> |
tmTop->lumap = (unsigned short *)malloc( |
| 407 |
> |
(tmTop->mbrmax-tmTop->mbrmin+1)*sizeof(unsigned short) ); |
| 408 |
> |
if (tmTop->lumap == NULL) |
| 409 |
> |
returnErr(TM_E_NOMEM); |
| 410 |
|
if (tmTop->flags & TM_F_LINEAR || histot <= threshold) { |
| 411 |
|
/* linear tone mapping */ |
| 412 |
|
if (tmTop->flags & TM_F_HCONTR) |
| 414 |
|
else |
| 415 |
|
d = Ldavg / Lwavg; |
| 416 |
|
d = log(d/Ldmax); |
| 417 |
< |
for (i = tmTop->brmax-tmTop->brmin+1; i--; ) |
| 417 |
> |
for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) |
| 418 |
|
tmTop->lumap[i] = 256. * exp( |
| 419 |
< |
( d + (tmTop->brmin+i)/(double)TM_BRTSCALE ) |
| 419 |
> |
( d + (tmTop->mbrmin+i)/(double)TM_BRTSCALE ) |
| 420 |
|
/ gamval ); |
| 421 |
|
} else { |
| 422 |
|
/* histogram adjustment */ |
| 423 |
< |
for (i = tmTop->brmax-tmTop->brmin+1; i--; ) { |
| 424 |
< |
j = d = (double)i/(tmTop->brmax-tmTop->brmin)*histlen; |
| 423 |
> |
for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) { |
| 424 |
> |
j = d = (double)i/(tmTop->mbrmax-tmTop->mbrmin)*histlen; |
| 425 |
|
d -= (double)j; |
| 426 |
|
Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1])); |
| 427 |
|
d = (Ld - Ldmin)/(Ldmax - Ldmin); |
| 451 |
|
if (ps == NULL | ls == NULL | len <= 0) |
| 452 |
|
returnErr(TM_E_ILLEGAL); |
| 453 |
|
while (len--) { |
| 454 |
< |
if ((li = *ls++) < tmTop->brmin) |
| 455 |
< |
li = tmTop->brmin; |
| 456 |
< |
else if (li > tmTop->brmax) |
| 457 |
< |
li = tmTop->brmax; |
| 458 |
< |
li = tmTop->lumap[li - tmTop->brmin]; |
| 454 |
> |
if ((li = *ls++) < tmTop->mbrmin) |
| 455 |
> |
li = tmTop->mbrmin; |
| 456 |
> |
else if (li > tmTop->mbrmax) |
| 457 |
> |
li = tmTop->mbrmax; |
| 458 |
> |
li = tmTop->lumap[li - tmTop->mbrmin]; |
| 459 |
|
if (cs == TM_NOCHROM) |
| 460 |
|
*ps++ = li>255 ? 255 : li; |
| 461 |
|
else { |
| 519 |
|
return(NULL); |
| 520 |
|
*tmnew = *tmTop; /* copy everything */ |
| 521 |
|
if (tmnew->histo != NULL) { /* duplicate histogram */ |
| 522 |
< |
len = (tmnew->brmax-MINBRT)/HISTEP + 1 - |
| 523 |
< |
(tmnew->brmin-MINBRT)/HISTEP; |
| 522 |
> |
len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 - |
| 523 |
> |
(tmnew->hbrmin-MINBRT)/HISTEP; |
| 524 |
|
tmnew->histo = (int *)malloc(len*sizeof(int)); |
| 525 |
|
if (tmnew->histo != NULL) |
| 526 |
|
for (i = len; i--; ) |
| 527 |
|
tmnew->histo[i] = tmTop->histo[i]; |
| 528 |
|
} |
| 529 |
|
if (tmnew->lumap != NULL) { /* duplicate luminance mapping */ |
| 530 |
< |
len = tmnew->brmax-tmnew->brmin+1; |
| 530 |
> |
len = tmnew->mbrmax-tmnew->mbrmin+1; |
| 531 |
|
tmnew->lumap = (unsigned short *)malloc( |
| 532 |
|
len*sizeof(unsigned short) ); |
| 533 |
|
if (tmnew->lumap != NULL) |