24 |
|
char *func; |
25 |
|
int err; |
26 |
|
{ |
27 |
< |
if (tmTop != NULL && tmTop->flags & TM_F_NOERRS) |
27 |
> |
if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR) |
28 |
|
return(err); |
29 |
|
fputs(func, stderr); |
30 |
|
fputs(": ", stderr); |
277 |
|
if (newhist == NULL) |
278 |
|
returnErr(TM_E_NOMEM); |
279 |
|
if (oldlen) { /* copy and free old */ |
280 |
< |
for (i = oldlen, j = i+oldorig-horig; i--; ) |
281 |
< |
newhist[--j] = tmTop->histo[i]; |
280 |
> |
for (i = oldlen, j = i+oldorig-horig; i; ) |
281 |
> |
newhist[--j] = tmTop->histo[--i]; |
282 |
|
free((char *)tmTop->histo); |
283 |
|
} |
284 |
|
tmTop->histo = newhist; |
346 |
|
i = (tmTop->brmin-MINBRT)/HISTEP; |
347 |
|
brt0 = MINBRT + HISTEP/2 + i*HISTEP; |
348 |
|
histlen = (tmTop->brmax-MINBRT)/HISTEP + 1 - i; |
349 |
– |
/* allocate temporary tables */ |
350 |
– |
histo = (int *)malloc(histlen*sizeof(int)); |
351 |
– |
cumf = (float *)malloc((histlen+1)*sizeof(float)); |
352 |
– |
if (histo == NULL | cumf == NULL) |
353 |
– |
returnErr(TM_E_NOMEM); |
349 |
|
/* histogram total and mean */ |
350 |
|
histot = 0; sum = 0; |
351 |
|
j = brt0 + histlen*HISTEP; |
352 |
|
for (i = histlen; i--; ) { |
353 |
< |
histot += (histo[i] = tmTop->histo[i]); |
354 |
< |
sum += (j -= HISTEP) * histo[i]; |
353 |
> |
histot += tmTop->histo[i]; |
354 |
> |
sum += (j -= HISTEP) * tmTop->histo[i]; |
355 |
|
} |
356 |
|
threshold = histot*.025 + .5; |
357 |
|
if (threshold < 4) |
358 |
|
returnErr(TM_E_TMFAIL); |
359 |
|
Lwavg = tmLuminance( (double)sum / histot ); |
360 |
< |
do { /* iterate to solution */ |
361 |
< |
sum = 0; /* compute cumulative probability */ |
362 |
< |
for (i = 0; i < histlen; i++) { |
363 |
< |
cumf[i] = (double)sum/histot; |
364 |
< |
sum += histo[i]; |
365 |
< |
} |
366 |
< |
cumf[i] = 1.; |
367 |
< |
Tr = histot * (double)(tmTop->brmax - tmTop->brmin) / |
360 |
> |
if (!(tmTop->flags & TM_F_LINEAR)) { /* clamp histogram */ |
361 |
> |
histo = (int *)malloc(histlen*sizeof(int)); |
362 |
> |
cumf = (float *)malloc((histlen+1)*sizeof(float)); |
363 |
> |
if (histo == NULL | cumf == NULL) |
364 |
> |
returnErr(TM_E_NOMEM); |
365 |
> |
for (i = histlen; i--; ) /* make malleable copy */ |
366 |
> |
histo[i] = tmTop->histo[i]; |
367 |
> |
do { /* iterate to solution */ |
368 |
> |
sum = 0; /* cumulative probability */ |
369 |
> |
for (i = 0; i < histlen; i++) { |
370 |
> |
cumf[i] = (double)sum/histot; |
371 |
> |
sum += histo[i]; |
372 |
> |
} |
373 |
> |
cumf[i] = 1.; |
374 |
> |
Tr = histot * (double)(tmTop->brmax - tmTop->brmin) / |
375 |
|
((double)histlen*TM_BRTSCALE) / logLddyn; |
376 |
< |
ceiling = Tr + 1.; |
377 |
< |
trimmings = 0; /* clip to envelope */ |
378 |
< |
for (i = histlen; i--; ) { |
379 |
< |
if (tmTop->flags & TM_F_HCONTR) { |
380 |
< |
Lw = tmLuminance(brt0 + i*HISTEP); |
381 |
< |
Ld = Ldmin * exp( logLddyn * |
376 |
> |
ceiling = Tr + 1.; |
377 |
> |
trimmings = 0; /* clip to envelope */ |
378 |
> |
for (i = histlen; i--; ) { |
379 |
> |
if (tmTop->flags & TM_F_HCONTR) { |
380 |
> |
Lw = tmLuminance(brt0 + i*HISTEP); |
381 |
> |
Ld = Ldmin * exp( logLddyn * |
382 |
|
.5*(cumf[i]+cumf[i+1]) ); |
383 |
< |
ceiling = Tr * (htcontrs(Ld) * Lw) / |
383 |
> |
ceiling = Tr * (htcontrs(Ld) * Lw) / |
384 |
|
(htcontrs(Lw) * Ld) + 1.; |
385 |
+ |
} |
386 |
+ |
if (histo[i] > ceiling) { |
387 |
+ |
trimmings += histo[i] - ceiling; |
388 |
+ |
histo[i] = ceiling; |
389 |
+ |
} |
390 |
|
} |
391 |
< |
if (histo[i] > ceiling) { |
392 |
< |
trimmings += histo[i] - ceiling; |
393 |
< |
histo[i] = ceiling; |
394 |
< |
} |
395 |
< |
} |
389 |
< |
} while ((histot -= trimmings) > threshold && trimmings > threshold); |
390 |
< |
|
391 |
< |
if (tmTop->lumap == NULL) { /* allocate luminance map */ |
391 |
> |
} while ((histot -= trimmings) > threshold && |
392 |
> |
trimmings > threshold); |
393 |
> |
} |
394 |
> |
/* allocate luminance map */ |
395 |
> |
if (tmTop->lumap == NULL) { |
396 |
|
tmTop->lumap = (unsigned short *)malloc( |
397 |
|
(tmTop->brmax-tmTop->brmin+1)*sizeof(unsigned short) ); |
398 |
|
if (tmTop->lumap == NULL) |
419 |
|
tmTop->lumap[i] = 256.*pow(d, 1./gamval); |
420 |
|
} |
421 |
|
} |
422 |
< |
free((char *)histo); |
423 |
< |
free((char *)cumf); |
422 |
> |
if (!(tmTop->flags & TM_F_LINEAR)) { |
423 |
> |
free((char *)histo); |
424 |
> |
free((char *)cumf); |
425 |
> |
} |
426 |
|
returnOK; |
427 |
|
} |
428 |
|
|