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 */ |
25 |
> |
/* luminance->brightness lookup */ |
26 |
> |
static TMbright *tmFloat2BrtLUT = NULL; |
27 |
|
|
28 |
+ |
#define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15] |
29 |
|
|
30 |
+ |
|
31 |
|
TMstruct * |
32 |
|
tmInit( /* initialize new tone mapping */ |
33 |
|
int flags, |
73 |
|
tmnew->inpsf = WHTEFFICACY; |
74 |
|
tmnew->cmat[0][1] = tmnew->cmat[0][2] = tmnew->cmat[1][0] = |
75 |
|
tmnew->cmat[1][2] = tmnew->cmat[2][0] = tmnew->cmat[2][1] = 0.; |
76 |
+ |
tmnew->inpdat = NULL; |
77 |
|
tmnew->hbrmin = 10; tmnew->hbrmax = -10; |
78 |
|
tmnew->histo = NULL; |
79 |
|
tmnew->mbrmin = 10; tmnew->mbrmax = -10; |
81 |
|
/* zero private data */ |
82 |
|
for (i = TM_MAXPKG; i--; ) |
83 |
|
tmnew->pd[i] = NULL; |
84 |
+ |
tmnew->lastError = TM_E_OK; |
85 |
+ |
tmnew->lastFunc = "NoErr"; |
86 |
|
/* return new TMstruct */ |
87 |
|
return(tmnew); |
88 |
|
} |
92 |
|
tmSetSpace( /* set input color space for conversions */ |
93 |
|
TMstruct *tms, |
94 |
|
RGBPRIMP pri, |
95 |
< |
double sf |
95 |
> |
double sf, |
96 |
> |
MEM_PTR dat |
97 |
|
) |
98 |
|
{ |
99 |
< |
static char funcName[] = "tmSetSpace"; |
99 |
> |
static const char funcName[] = "tmSetSpace"; |
100 |
|
int i, j; |
101 |
|
/* error check */ |
102 |
|
if (tms == NULL) |
104 |
|
if (sf <= 1e-12) |
105 |
|
returnErr(TM_E_ILLEGAL); |
106 |
|
/* check if no change */ |
107 |
< |
if (pri == tms->inppri && FEQ(sf, tms->inpsf)) |
107 |
> |
if (pri == tms->inppri && FEQ(sf, tms->inpsf) && dat == tms->inpdat) |
108 |
|
returnOK; |
109 |
|
tms->inppri = pri; /* let's set it */ |
110 |
|
tms->inpsf = sf; |
111 |
+ |
tms->inpdat = dat; |
112 |
|
|
113 |
|
if (tms->flags & TM_F_BW) { /* color doesn't matter */ |
114 |
|
tms->monpri = tms->inppri; /* eliminate xform */ |
154 |
|
|
155 |
|
|
156 |
|
void |
157 |
< |
tmClearHisto( /* clear current histogram */ |
157 |
> |
tmClearHisto( /* clear current histogram */ |
158 |
|
TMstruct *tms |
159 |
|
) |
160 |
|
{ |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
+ |
TMbright |
169 |
+ |
tmCvLuminance( /* convert a single luminance */ |
170 |
+ |
double lum |
171 |
+ |
) |
172 |
+ |
{ |
173 |
+ |
double d; |
174 |
+ |
|
175 |
+ |
#ifdef isfinite |
176 |
+ |
if (!isfinite(lum) || lum <= TM_NOLUM) |
177 |
+ |
#else |
178 |
+ |
if (lum <= TM_NOLUM) |
179 |
+ |
#endif |
180 |
+ |
return(TM_NOBRT); |
181 |
+ |
d = TM_BRTSCALE*log(lum); |
182 |
+ |
if (d > 0.) |
183 |
+ |
return((TMbright)(d+.5)); |
184 |
+ |
return((TMbright)(d-.5)); |
185 |
+ |
} |
186 |
+ |
|
187 |
+ |
|
188 |
|
int |
189 |
+ |
tmCvLums( /* convert luminances using lookup */ |
190 |
+ |
TMbright *ls, |
191 |
+ |
float *scan, |
192 |
+ |
int len |
193 |
+ |
) |
194 |
+ |
{ |
195 |
+ |
if (tmFloat2BrtLUT == NULL) { /* initialize lookup table */ |
196 |
+ |
int32 i; |
197 |
+ |
tmFloat2BrtLUT = (TMbright *)malloc(sizeof(TMbright)*0x10000); |
198 |
+ |
if (tmFloat2BrtLUT == NULL) |
199 |
+ |
return(TM_E_NOMEM); |
200 |
+ |
for (i = 0; i < 0x10000; i++) { |
201 |
+ |
int32 l = (i<<1 | 1) << 14; |
202 |
+ |
#ifndef isfinite |
203 |
+ |
if ((l & 0x7f800000) == 0x7f800000) |
204 |
+ |
tmFloat2BrtLUT[i] = TM_NOBRT; |
205 |
+ |
else |
206 |
+ |
#endif |
207 |
+ |
tmFloat2BrtLUT[i] = tmCvLuminance(*(float *)&l); |
208 |
+ |
} |
209 |
+ |
} |
210 |
+ |
if (len <= 0) |
211 |
+ |
return(TM_E_OK); |
212 |
+ |
if ((ls == NULL) | (scan == NULL)) |
213 |
+ |
return(TM_E_ILLEGAL); |
214 |
+ |
while (len--) { |
215 |
+ |
if (*scan <= TM_NOLUM) { |
216 |
+ |
*ls++ = TM_NOBRT; |
217 |
+ |
++scan; |
218 |
+ |
continue; |
219 |
+ |
} |
220 |
+ |
*ls++ = tmCvLumLUfp(scan++); |
221 |
+ |
} |
222 |
+ |
return(TM_E_OK); |
223 |
+ |
} |
224 |
+ |
|
225 |
+ |
|
226 |
+ |
int |
227 |
+ |
tmCvGrays( /* convert float gray values */ |
228 |
+ |
TMstruct *tms, |
229 |
+ |
TMbright *ls, |
230 |
+ |
float *scan, |
231 |
+ |
int len |
232 |
+ |
) |
233 |
+ |
{ |
234 |
+ |
static const char funcName[] = "tmCvGrays"; |
235 |
+ |
double d; |
236 |
+ |
int i; |
237 |
+ |
|
238 |
+ |
if (tms == NULL) |
239 |
+ |
returnErr(TM_E_TMINVAL); |
240 |
+ |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
241 |
+ |
returnErr(TM_E_ILLEGAL); |
242 |
+ |
if (tmFloat2BrtLUT == NULL) /* initialize */ |
243 |
+ |
tmCvLums(NULL, NULL, 0); |
244 |
+ |
for (i = len; i--; ) { |
245 |
+ |
float lum = tms->inpsf * scan[i]; |
246 |
+ |
if (lum <= TM_NOLUM) |
247 |
+ |
ls[i] = TM_NOBRT; |
248 |
+ |
else |
249 |
+ |
ls[i] = tmCvLumLUfp(&lum); |
250 |
+ |
} |
251 |
+ |
returnOK; |
252 |
+ |
} |
253 |
+ |
|
254 |
+ |
|
255 |
+ |
int |
256 |
|
tmCvColors( /* convert float colors */ |
257 |
|
TMstruct *tms, |
258 |
|
TMbright *ls, |
261 |
|
int len |
262 |
|
) |
263 |
|
{ |
264 |
< |
static char funcName[] = "tmCvColors"; |
264 |
> |
static const char funcName[] = "tmCvColors"; |
265 |
|
static COLOR csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM}; |
266 |
+ |
static BYTE gamtab[1024]; |
267 |
+ |
static double curgam = .0; |
268 |
|
COLOR cmon; |
269 |
< |
double lum, slum; |
174 |
< |
double d; |
269 |
> |
float lum, slum, d; |
270 |
|
int i; |
271 |
|
|
272 |
|
if (tms == NULL) |
273 |
|
returnErr(TM_E_TMINVAL); |
274 |
|
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
275 |
|
returnErr(TM_E_ILLEGAL); |
276 |
+ |
if (tmFloat2BrtLUT == NULL) /* initialize */ |
277 |
+ |
tmCvLums(NULL, NULL, 0); |
278 |
+ |
if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) { |
279 |
+ |
curgam = tms->mongam; /* (re)build table */ |
280 |
+ |
for (i = 1024; i--; ) |
281 |
+ |
gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam)); |
282 |
+ |
} |
283 |
|
for (i = len; i--; ) { |
284 |
|
if (tmNeedMatrix(tms)) { /* get monitor RGB */ |
285 |
|
colortrans(cmon, tms->cmat, scan[i]); |
288 |
|
cmon[GRN] = tms->inpsf*scan[i][GRN]; |
289 |
|
cmon[BLU] = tms->inpsf*scan[i][BLU]; |
290 |
|
} |
291 |
+ |
#ifdef isfinite |
292 |
+ |
if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f; |
293 |
+ |
if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f; |
294 |
+ |
if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f; |
295 |
+ |
#else |
296 |
+ |
if (cmon[RED] < .0f) cmon[RED] = .0f; |
297 |
+ |
if (cmon[GRN] < .0f) cmon[GRN] = .0f; |
298 |
+ |
if (cmon[BLU] < .0f) cmon[BLU] = .0f; |
299 |
+ |
#endif |
300 |
|
/* world luminance */ |
301 |
|
lum = tms->clf[RED]*cmon[RED] + |
302 |
|
tms->clf[GRN]*cmon[GRN] + |
303 |
|
tms->clf[BLU]*cmon[BLU] ; |
304 |
< |
/* check range */ |
305 |
< |
if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite)) |
306 |
< |
lum = tms->clf[RED]*cmon[RED] + |
307 |
< |
tms->clf[GRN]*cmon[GRN] + |
197 |
< |
tms->clf[BLU]*cmon[BLU] ; |
198 |
< |
if (lum < MINLUM) { |
199 |
< |
ls[i] = MINBRT-1; /* bogus value */ |
200 |
< |
lum = MINLUM; |
201 |
< |
} else { |
202 |
< |
d = TM_BRTSCALE*log(lum); /* encode it */ |
203 |
< |
ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5); |
204 |
< |
} |
304 |
> |
if (lum <= TM_NOLUM) /* convert brightness */ |
305 |
> |
ls[i] = TM_NOBRT; |
306 |
> |
else |
307 |
> |
ls[i] = tmCvLumLUfp(&lum); |
308 |
|
if (cs == TM_NOCHROM) /* no color? */ |
309 |
|
continue; |
310 |
|
if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) { |
318 |
|
cmon[BLU] = d*lum; |
319 |
|
else |
320 |
|
scalecolor(cmon, d); |
321 |
< |
d = (1.-d)*slum; |
321 |
> |
d = (1.f-d)*slum; |
322 |
|
cmon[RED] += d; |
323 |
|
cmon[GRN] += d; |
324 |
|
cmon[BLU] += d; |
327 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = lum; |
328 |
|
} |
329 |
|
d = tms->clf[RED]*cmon[RED]/lum; |
330 |
< |
cs[3*i ] = d>=.999 ? 255 : |
228 |
< |
(int)(256.*pow(d, 1./tms->mongam)); |
330 |
> |
cs[3*i ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
331 |
|
d = tms->clf[GRN]*cmon[GRN]/lum; |
332 |
< |
cs[3*i+1] = d>=.999 ? 255 : |
231 |
< |
(int)(256.*pow(d, 1./tms->mongam)); |
332 |
> |
cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
333 |
|
d = tms->clf[BLU]*cmon[BLU]/lum; |
334 |
< |
cs[3*i+2] = d>=.999 ? 255 : |
234 |
< |
(int)(256.*pow(d, 1./tms->mongam)); |
334 |
> |
cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
335 |
|
} |
336 |
|
returnOK; |
337 |
|
} |
338 |
|
|
339 |
|
|
340 |
|
int |
241 |
– |
tmCvGrays( /* convert float gray values */ |
242 |
– |
TMstruct *tms, |
243 |
– |
TMbright *ls, |
244 |
– |
float *scan, |
245 |
– |
int len |
246 |
– |
) |
247 |
– |
{ |
248 |
– |
static char funcName[] = "tmCvGrays"; |
249 |
– |
double d; |
250 |
– |
int i; |
251 |
– |
|
252 |
– |
if (tms == NULL) |
253 |
– |
returnErr(TM_E_TMINVAL); |
254 |
– |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
255 |
– |
returnErr(TM_E_ILLEGAL); |
256 |
– |
for (i = len; i--; ) |
257 |
– |
if (scan[i] <= TM_NOLUM) { |
258 |
– |
ls[i] = TM_NOBRT; /* bogus value */ |
259 |
– |
} else { |
260 |
– |
d = TM_BRTSCALE*log(scan[i]); /* encode it */ |
261 |
– |
ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5); |
262 |
– |
} |
263 |
– |
returnOK; |
264 |
– |
} |
265 |
– |
|
266 |
– |
|
267 |
– |
int |
341 |
|
tmAddHisto( /* add values to histogram */ |
342 |
|
TMstruct *tms, |
343 |
|
TMbright *ls, |
345 |
|
int wt |
346 |
|
) |
347 |
|
{ |
348 |
< |
static char funcName[] = "tmAddHisto"; |
348 |
> |
static const char funcName[] = "tmAddHisto"; |
349 |
|
int oldorig=0, oldlen, horig, hlen; |
350 |
|
int i, j; |
351 |
|
|
448 |
|
double gamval |
449 |
|
) |
450 |
|
{ |
451 |
< |
static char funcName[] = "tmFixedMapping"; |
451 |
> |
static const char funcName[] = "tmFixedMapping"; |
452 |
|
double d; |
453 |
|
int i; |
454 |
|
|
475 |
|
double Ldmax |
476 |
|
) |
477 |
|
{ |
478 |
< |
static char funcName[] = "tmComputeMapping"; |
478 |
> |
static const char funcName[] = "tmComputeMapping"; |
479 |
|
int *histo; |
480 |
|
float *cumf; |
481 |
|
int brt0, histlen, threshold, ceiling, trimmings; |
583 |
|
int len |
584 |
|
) |
585 |
|
{ |
586 |
< |
static char funcName[] = "tmMapPixels"; |
586 |
> |
static const char funcName[] = "tmMapPixels"; |
587 |
|
int32 li, pv; |
588 |
|
|
589 |
|
if (tms == NULL || tms->lumap == NULL) |
695 |
|
|
696 |
|
int |
697 |
|
tmErrorReturn( /* error return (with message) */ |
698 |
< |
char *func, |
698 |
> |
const char *func, |
699 |
|
TMstruct *tms, |
700 |
|
int err |
701 |
|
) |
702 |
|
{ |
703 |
< |
tmLastFunction = func; |
704 |
< |
tmLastError = err; |
705 |
< |
if (tms != NULL && tms->flags & TM_F_NOSTDERR) |
706 |
< |
return(err); |
703 |
> |
if (tms != NULL) { |
704 |
> |
tms->lastFunc = func; |
705 |
> |
tms->lastError = err; |
706 |
> |
if (tms->flags & TM_F_NOSTDERR) |
707 |
> |
return(err); |
708 |
> |
} |
709 |
|
fputs(func, stderr); |
710 |
|
fputs(": ", stderr); |
711 |
|
fputs(tmErrorMessage[err], stderr); |