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" |
19 |
|
|
20 |
|
#define exp10(x) exp(M_LN10*(x)) |
21 |
|
|
21 |
– |
struct tmStruct *tmTop = NULL; /* current tone mapping stack */ |
22 |
– |
|
22 |
|
/* our list of conversion packages */ |
23 |
|
struct tmPackage *tmPkg[TM_MAXPKG]; |
24 |
|
int tmNumPkgs = 0; /* number of registered packages */ |
25 |
|
|
26 |
< |
int tmLastError; /* last error incurred by library */ |
27 |
< |
char *tmLastFunction; /* error-generating function name */ |
26 |
> |
/* luminance->brightness lookup */ |
27 |
> |
static TMbright *tmFloat2BrtLUT = NULL; |
28 |
|
|
29 |
+ |
#define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15] |
30 |
|
|
31 |
< |
struct tmStruct * |
32 |
< |
tmInit(flags, monpri, gamval) /* initialize new tone mapping */ |
33 |
< |
int flags; |
34 |
< |
RGBPRIMP monpri; |
35 |
< |
double gamval; |
31 |
> |
|
32 |
> |
TMstruct * |
33 |
> |
tmInit( /* initialize new tone mapping */ |
34 |
> |
int flags, |
35 |
> |
RGBPRIMP monpri, |
36 |
> |
double gamval |
37 |
> |
) |
38 |
|
{ |
39 |
|
COLORMAT cmat; |
40 |
< |
register struct tmStruct *tmnew; |
41 |
< |
register int i; |
40 |
> |
TMstruct *tmnew; |
41 |
> |
int i; |
42 |
|
/* allocate structure */ |
43 |
< |
tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct)); |
43 |
> |
tmnew = (TMstruct *)malloc(sizeof(TMstruct)); |
44 |
|
if (tmnew == NULL) |
45 |
|
return(NULL); |
46 |
|
|
47 |
|
tmnew->flags = flags & ~TM_F_UNIMPL; |
48 |
+ |
if (tmnew->flags & TM_F_BW) |
49 |
+ |
tmnew->flags &= ~TM_F_MESOPIC; |
50 |
|
/* set monitor transform */ |
51 |
|
if (monpri == NULL || monpri == stdprims || tmnew->flags & TM_F_BW) { |
52 |
|
tmnew->monpri = stdprims; |
54 |
|
tmnew->clf[GRN] = rgb2xyzmat[1][1]; |
55 |
|
tmnew->clf[BLU] = rgb2xyzmat[1][2]; |
56 |
|
} else { |
57 |
< |
comprgb2xyzWBmat(cmat, tmnew->monpri=monpri); |
57 |
> |
comprgb2xyzmat(cmat, tmnew->monpri=monpri); |
58 |
|
tmnew->clf[RED] = cmat[1][0]; |
59 |
|
tmnew->clf[GRN] = cmat[1][1]; |
60 |
|
tmnew->clf[BLU] = cmat[1][2]; |
74 |
|
tmnew->inpsf = WHTEFFICACY; |
75 |
|
tmnew->cmat[0][1] = tmnew->cmat[0][2] = tmnew->cmat[1][0] = |
76 |
|
tmnew->cmat[1][2] = tmnew->cmat[2][0] = tmnew->cmat[2][1] = 0.; |
77 |
+ |
tmnew->inpdat = NULL; |
78 |
|
tmnew->hbrmin = 10; tmnew->hbrmax = -10; |
79 |
|
tmnew->histo = NULL; |
80 |
|
tmnew->mbrmin = 10; tmnew->mbrmax = -10; |
82 |
|
/* zero private data */ |
83 |
|
for (i = TM_MAXPKG; i--; ) |
84 |
|
tmnew->pd[i] = NULL; |
85 |
< |
/* make tmnew current */ |
86 |
< |
tmnew->tmprev = tmTop; |
87 |
< |
return(tmTop = tmnew); |
85 |
> |
tmnew->lastError = TM_E_OK; |
86 |
> |
tmnew->lastFunc = "NoErr"; |
87 |
> |
/* return new TMstruct */ |
88 |
> |
return(tmnew); |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
|
int |
93 |
< |
tmSetSpace(pri, sf) /* set input color space for conversions */ |
94 |
< |
RGBPRIMP pri; |
95 |
< |
double sf; |
93 |
> |
tmSetSpace( /* set input color space for conversions */ |
94 |
> |
TMstruct *tms, |
95 |
> |
RGBPRIMP pri, |
96 |
> |
double sf, |
97 |
> |
MEM_PTR dat |
98 |
> |
) |
99 |
|
{ |
100 |
< |
static char funcName[] = "tmSetSpace"; |
101 |
< |
register int i, j; |
100 |
> |
static const char funcName[] = "tmSetSpace"; |
101 |
> |
int i, j; |
102 |
|
/* error check */ |
103 |
< |
if (tmTop == NULL) |
103 |
> |
if (tms == NULL) |
104 |
|
returnErr(TM_E_TMINVAL); |
105 |
|
if (sf <= 1e-12) |
106 |
|
returnErr(TM_E_ILLEGAL); |
107 |
|
/* check if no change */ |
108 |
< |
if (pri == tmTop->inppri && FEQ(sf, tmTop->inpsf)) |
108 |
> |
if (pri == tms->inppri && FEQ(sf, tms->inpsf) && dat == tms->inpdat) |
109 |
|
returnOK; |
110 |
< |
tmTop->inppri = pri; /* let's set it */ |
111 |
< |
tmTop->inpsf = sf; |
110 |
> |
tms->inppri = pri; /* let's set it */ |
111 |
> |
tms->inpsf = sf; |
112 |
> |
tms->inpdat = dat; |
113 |
|
|
114 |
< |
if (tmTop->flags & TM_F_BW) { /* color doesn't matter */ |
115 |
< |
tmTop->monpri = tmTop->inppri; /* eliminate xform */ |
116 |
< |
if (tmTop->inppri == TM_XYZPRIM) { |
117 |
< |
tmTop->clf[CIEX] = tmTop->clf[CIEZ] = 0.; |
118 |
< |
tmTop->clf[CIEY] = 1.; |
114 |
> |
if (tms->flags & TM_F_BW) { /* color doesn't matter */ |
115 |
> |
tms->monpri = tms->inppri; /* eliminate xform */ |
116 |
> |
if (tms->inppri == TM_XYZPRIM) { |
117 |
> |
tms->clf[CIEX] = tms->clf[CIEZ] = 0.; |
118 |
> |
tms->clf[CIEY] = 1.; |
119 |
|
} else { |
120 |
< |
comprgb2xyzWBmat(tmTop->cmat, tmTop->monpri); |
121 |
< |
tmTop->clf[RED] = tmTop->cmat[1][0]; |
122 |
< |
tmTop->clf[GRN] = tmTop->cmat[1][1]; |
123 |
< |
tmTop->clf[BLU] = tmTop->cmat[1][2]; |
120 |
> |
comprgb2xyzmat(tms->cmat, tms->monpri); |
121 |
> |
tms->clf[RED] = tms->cmat[1][0]; |
122 |
> |
tms->clf[GRN] = tms->cmat[1][1]; |
123 |
> |
tms->clf[BLU] = tms->cmat[1][2]; |
124 |
|
} |
125 |
< |
tmTop->cmat[0][0] = tmTop->cmat[1][1] = tmTop->cmat[2][2] = |
126 |
< |
tmTop->inpsf; |
127 |
< |
tmTop->cmat[0][1] = tmTop->cmat[0][2] = tmTop->cmat[1][0] = |
128 |
< |
tmTop->cmat[1][2] = tmTop->cmat[2][0] = tmTop->cmat[2][1] = 0.; |
125 |
> |
tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] = |
126 |
> |
tms->inpsf; |
127 |
> |
tms->cmat[0][1] = tms->cmat[0][2] = tms->cmat[1][0] = |
128 |
> |
tms->cmat[1][2] = tms->cmat[2][0] = tms->cmat[2][1] = 0.; |
129 |
|
|
130 |
< |
} else if (tmTop->inppri == TM_XYZPRIM) /* input is XYZ */ |
131 |
< |
compxyz2rgbWBmat(tmTop->cmat, tmTop->monpri); |
130 |
> |
} else if (tms->inppri == TM_XYZPRIM) /* input is XYZ */ |
131 |
> |
compxyz2rgbWBmat(tms->cmat, tms->monpri); |
132 |
|
|
133 |
|
else { /* input is RGB */ |
134 |
< |
if (tmTop->inppri != tmTop->monpri && |
135 |
< |
PRIMEQ(tmTop->inppri, tmTop->monpri)) |
136 |
< |
tmTop->inppri = tmTop->monpri; /* no xform */ |
137 |
< |
comprgb2rgbWBmat(tmTop->cmat, tmTop->inppri, tmTop->monpri); |
134 |
> |
if (tms->inppri != tms->monpri && |
135 |
> |
PRIMEQ(tms->inppri, tms->monpri)) |
136 |
> |
tms->inppri = tms->monpri; /* no xform */ |
137 |
> |
if (!comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri)) |
138 |
> |
returnErr(TM_E_ILLEGAL); |
139 |
|
} |
140 |
|
for (i = 0; i < 3; i++) |
141 |
|
for (j = 0; j < 3; j++) |
142 |
< |
tmTop->cmat[i][j] *= tmTop->inpsf; |
142 |
> |
tms->cmat[i][j] *= tms->inpsf; |
143 |
|
/* set color divisors */ |
144 |
|
for (i = 0; i < 3; i++) |
145 |
< |
if (tmTop->clf[i] > .001) |
146 |
< |
tmTop->cdiv[i] = |
136 |
< |
256.*pow(tmTop->clf[i], 1./tmTop->mongam); |
137 |
< |
else |
138 |
< |
tmTop->cdiv[i] = 1; |
145 |
> |
tms->cdiv[i] = 256.*pow(tms->clf[i] < .001 ? .001 : |
146 |
> |
tms->clf[i], 1./tms->mongam); |
147 |
|
/* notify packages */ |
148 |
|
for (i = tmNumPkgs; i--; ) |
149 |
< |
if (tmTop->pd[i] != NULL && tmPkg[i]->NewSpace != NULL) |
150 |
< |
(*tmPkg[i]->NewSpace)(tmTop); |
149 |
> |
if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL) |
150 |
> |
(*tmPkg[i]->NewSpace)(tms); |
151 |
|
returnOK; |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
void |
156 |
< |
tmClearHisto() /* clear current histogram */ |
156 |
> |
tmClearHisto( /* clear current histogram */ |
157 |
> |
TMstruct *tms |
158 |
> |
) |
159 |
|
{ |
160 |
< |
if (tmTop == NULL || tmTop->histo == NULL) |
160 |
> |
if (tms == NULL || tms->histo == NULL) |
161 |
|
return; |
162 |
< |
free((MEM_PTR)tmTop->histo); |
163 |
< |
tmTop->histo = NULL; |
162 |
> |
free((MEM_PTR)tms->histo); |
163 |
> |
tms->histo = NULL; |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
+ |
TMbright |
168 |
+ |
tmCvLuminance( /* convert a single luminance */ |
169 |
+ |
double lum |
170 |
+ |
) |
171 |
+ |
{ |
172 |
+ |
double d; |
173 |
+ |
|
174 |
+ |
#ifdef isfinite |
175 |
+ |
if (!isfinite(lum) || lum <= TM_NOLUM) |
176 |
+ |
#else |
177 |
+ |
if (lum <= TM_NOLUM) |
178 |
+ |
#endif |
179 |
+ |
return(TM_NOBRT); |
180 |
+ |
d = TM_BRTSCALE*log(lum); |
181 |
+ |
return((TMbright)(d + .5 - (d < 0.))); |
182 |
+ |
} |
183 |
+ |
|
184 |
+ |
|
185 |
|
int |
186 |
< |
tmCvColors(ls, cs, scan, len) /* convert float colors */ |
187 |
< |
TMbright *ls; |
188 |
< |
BYTE *cs; |
189 |
< |
COLOR *scan; |
190 |
< |
int len; |
186 |
> |
tmCvLums( /* convert luminances using lookup */ |
187 |
> |
TMbright *ls, |
188 |
> |
float *scan, |
189 |
> |
int len |
190 |
> |
) |
191 |
|
{ |
192 |
< |
static char funcName[] = "tmCvColors"; |
193 |
< |
static COLOR csmall = {1e-6, 1e-6, 1e-6}; |
192 |
> |
if (tmFloat2BrtLUT == NULL) { /* initialize lookup table */ |
193 |
> |
int32 i; |
194 |
> |
tmFloat2BrtLUT = (TMbright *)malloc(sizeof(TMbright)*0x10000); |
195 |
> |
if (tmFloat2BrtLUT == NULL) |
196 |
> |
return(TM_E_NOMEM); |
197 |
> |
for (i = 0; i < 0x10000; i++) { |
198 |
> |
int32 l = (i<<1 | 1) << 14; |
199 |
> |
#ifndef isfinite |
200 |
> |
if ((l & 0x7f800000) == 0x7f800000) |
201 |
> |
tmFloat2BrtLUT[i] = TM_NOBRT; |
202 |
> |
else |
203 |
> |
#endif |
204 |
> |
tmFloat2BrtLUT[i] = tmCvLuminance(*(float *)&l); |
205 |
> |
} |
206 |
> |
} |
207 |
> |
if (len <= 0) |
208 |
> |
return(TM_E_OK); |
209 |
> |
if ((ls == NULL) | (scan == NULL)) |
210 |
> |
return(TM_E_ILLEGAL); |
211 |
> |
while (len--) { |
212 |
> |
if (*scan <= TM_NOLUM) { |
213 |
> |
*ls++ = TM_NOBRT; |
214 |
> |
++scan; |
215 |
> |
continue; |
216 |
> |
} |
217 |
> |
*ls++ = tmCvLumLUfp(scan++); |
218 |
> |
} |
219 |
> |
return(TM_E_OK); |
220 |
> |
} |
221 |
> |
|
222 |
> |
|
223 |
> |
int |
224 |
> |
tmCvGrays( /* convert float gray values */ |
225 |
> |
TMstruct *tms, |
226 |
> |
TMbright *ls, |
227 |
> |
float *scan, |
228 |
> |
int len |
229 |
> |
) |
230 |
> |
{ |
231 |
> |
static const char funcName[] = "tmCvGrays"; |
232 |
> |
int i; |
233 |
> |
|
234 |
> |
if (tms == NULL) |
235 |
> |
returnErr(TM_E_TMINVAL); |
236 |
> |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
237 |
> |
returnErr(TM_E_ILLEGAL); |
238 |
> |
if (tmFloat2BrtLUT == NULL) /* initialize */ |
239 |
> |
tmCvLums(NULL, NULL, 0); |
240 |
> |
for (i = len; i--; ) { |
241 |
> |
float lum = tms->inpsf * scan[i]; |
242 |
> |
if (lum <= TM_NOLUM) |
243 |
> |
ls[i] = TM_NOBRT; |
244 |
> |
else |
245 |
> |
ls[i] = tmCvLumLUfp(&lum); |
246 |
> |
} |
247 |
> |
returnOK; |
248 |
> |
} |
249 |
> |
|
250 |
> |
|
251 |
> |
int |
252 |
> |
tmCvColors( /* convert float colors */ |
253 |
> |
TMstruct *tms, |
254 |
> |
TMbright *ls, |
255 |
> |
uby8 *cs, |
256 |
> |
COLOR *scan, |
257 |
> |
int len |
258 |
> |
) |
259 |
> |
{ |
260 |
> |
static const char funcName[] = "tmCvColors"; |
261 |
> |
static uby8 gamtab[1024]; |
262 |
> |
static double curgam = .0; |
263 |
|
COLOR cmon; |
264 |
< |
double lum, slum; |
265 |
< |
register double d; |
169 |
< |
register int i; |
264 |
> |
float lum, slum, d; |
265 |
> |
int i; |
266 |
|
|
267 |
< |
if (tmTop == NULL) |
267 |
> |
if (tms == NULL) |
268 |
|
returnErr(TM_E_TMINVAL); |
269 |
< |
if (ls == NULL | scan == NULL | len < 0) |
269 |
> |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
270 |
|
returnErr(TM_E_ILLEGAL); |
271 |
+ |
if (tmFloat2BrtLUT == NULL) /* initialize */ |
272 |
+ |
tmCvLums(NULL, NULL, 0); |
273 |
+ |
if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) { |
274 |
+ |
curgam = tms->mongam; /* (re)build table */ |
275 |
+ |
for (i = 1024; i--; ) |
276 |
+ |
gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam)); |
277 |
+ |
} |
278 |
|
for (i = len; i--; ) { |
279 |
< |
if (tmNeedMatrix(tmTop)) /* get monitor RGB */ |
280 |
< |
colortrans(cmon, tmTop->cmat, scan[i]); |
178 |
< |
else { |
179 |
< |
cmon[RED] = tmTop->inpsf*scan[i][RED]; |
180 |
< |
cmon[GRN] = tmTop->inpsf*scan[i][GRN]; |
181 |
< |
cmon[BLU] = tmTop->inpsf*scan[i][BLU]; |
182 |
< |
} |
183 |
< |
/* world luminance */ |
184 |
< |
lum = tmTop->clf[RED]*cmon[RED] + |
185 |
< |
tmTop->clf[GRN]*cmon[GRN] + |
186 |
< |
tmTop->clf[BLU]*cmon[BLU] ; |
187 |
< |
/* check range */ |
188 |
< |
if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite)) |
189 |
< |
lum = tmTop->clf[RED]*cmon[RED] + |
190 |
< |
tmTop->clf[GRN]*cmon[GRN] + |
191 |
< |
tmTop->clf[BLU]*cmon[BLU] ; |
192 |
< |
if (lum < MINLUM) { |
193 |
< |
ls[i] = MINBRT-1; /* bogus value */ |
194 |
< |
lum = MINLUM; |
279 |
> |
if (tmNeedMatrix(tms)) { /* get monitor RGB */ |
280 |
> |
colortrans(cmon, tms->cmat, scan[i]); |
281 |
|
} else { |
282 |
< |
d = TM_BRTSCALE*log(lum); /* encode it */ |
283 |
< |
ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5); |
282 |
> |
cmon[RED] = tms->inpsf*scan[i][RED]; |
283 |
> |
cmon[GRN] = tms->inpsf*scan[i][GRN]; |
284 |
> |
cmon[BLU] = tms->inpsf*scan[i][BLU]; |
285 |
|
} |
286 |
+ |
#ifdef isfinite |
287 |
+ |
if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f; |
288 |
+ |
if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f; |
289 |
+ |
if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f; |
290 |
+ |
#else |
291 |
+ |
if (cmon[RED] < .0f) cmon[RED] = .0f; |
292 |
+ |
if (cmon[GRN] < .0f) cmon[GRN] = .0f; |
293 |
+ |
if (cmon[BLU] < .0f) cmon[BLU] = .0f; |
294 |
+ |
#endif |
295 |
+ |
/* world luminance */ |
296 |
+ |
lum = tms->clf[RED]*cmon[RED] + |
297 |
+ |
tms->clf[GRN]*cmon[GRN] + |
298 |
+ |
tms->clf[BLU]*cmon[BLU] ; |
299 |
+ |
if (lum <= TM_NOLUM) { /* convert brightness */ |
300 |
+ |
lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM; |
301 |
+ |
ls[i] = TM_NOBRT; |
302 |
+ |
} else |
303 |
+ |
ls[i] = tmCvLumLUfp(&lum); |
304 |
|
if (cs == TM_NOCHROM) /* no color? */ |
305 |
|
continue; |
306 |
< |
if (tmTop->flags & TM_F_MESOPIC && lum < LMESUPPER) { |
306 |
> |
if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) { |
307 |
|
slum = scotlum(cmon); /* mesopic adj. */ |
308 |
< |
if (lum < LMESLOWER) |
308 |
> |
if (lum < LMESLOWER) { |
309 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = slum; |
310 |
< |
else { |
310 |
> |
} else { |
311 |
|
d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER); |
312 |
< |
if (tmTop->flags & TM_F_BW) |
312 |
> |
if (tms->flags & TM_F_BW) |
313 |
|
cmon[RED] = cmon[GRN] = |
314 |
|
cmon[BLU] = d*lum; |
315 |
|
else |
316 |
|
scalecolor(cmon, d); |
317 |
< |
d = (1.-d)*slum; |
317 |
> |
d = (1.f-d)*slum; |
318 |
|
cmon[RED] += d; |
319 |
|
cmon[GRN] += d; |
320 |
|
cmon[BLU] += d; |
321 |
|
} |
322 |
< |
} else if (tmTop->flags & TM_F_BW) { |
322 |
> |
} else if (tms->flags & TM_F_BW) { |
323 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = lum; |
324 |
|
} |
325 |
< |
d = tmTop->clf[RED]*cmon[RED]/lum; |
326 |
< |
cs[3*i ] = d>=.999 ? 255 : |
327 |
< |
(int)(256.*pow(d, 1./tmTop->mongam)); |
328 |
< |
d = tmTop->clf[GRN]*cmon[GRN]/lum; |
329 |
< |
cs[3*i+1] = d>=.999 ? 255 : |
330 |
< |
(int)(256.*pow(d, 1./tmTop->mongam)); |
226 |
< |
d = tmTop->clf[BLU]*cmon[BLU]/lum; |
227 |
< |
cs[3*i+2] = d>=.999 ? 255 : |
228 |
< |
(int)(256.*pow(d, 1./tmTop->mongam)); |
325 |
> |
d = tms->clf[RED]*cmon[RED]/lum; |
326 |
> |
cs[3*i ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
327 |
> |
d = tms->clf[GRN]*cmon[GRN]/lum; |
328 |
> |
cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
329 |
> |
d = tms->clf[BLU]*cmon[BLU]/lum; |
330 |
> |
cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; |
331 |
|
} |
332 |
|
returnOK; |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
|
int |
337 |
< |
tmCvGrays(ls, scan, len) /* convert float gray values */ |
338 |
< |
TMbright *ls; |
339 |
< |
float *scan; |
340 |
< |
int len; |
337 |
> |
tmAddHisto( /* add values to histogram */ |
338 |
> |
TMstruct *tms, |
339 |
> |
TMbright *ls, |
340 |
> |
int len, |
341 |
> |
int wt |
342 |
> |
) |
343 |
|
{ |
344 |
< |
static char funcName[] = "tmCvGrays"; |
241 |
< |
register double d; |
242 |
< |
register int i; |
243 |
< |
|
244 |
< |
if (tmTop == NULL) |
245 |
< |
returnErr(TM_E_TMINVAL); |
246 |
< |
if (ls == NULL | scan == NULL | len < 0) |
247 |
< |
returnErr(TM_E_ILLEGAL); |
248 |
< |
for (i = len; i--; ) |
249 |
< |
if (scan[i] <= TM_NOLUM) { |
250 |
< |
ls[i] = TM_NOBRT; /* bogus value */ |
251 |
< |
} else { |
252 |
< |
d = TM_BRTSCALE*log(scan[i]); /* encode it */ |
253 |
< |
ls[i] = d>0. ? (int)(d+.5) : (int)(d-.5); |
254 |
< |
} |
255 |
< |
returnOK; |
256 |
< |
} |
257 |
< |
|
258 |
< |
|
259 |
< |
int |
260 |
< |
tmAddHisto(ls, len, wt) /* add values to histogram */ |
261 |
< |
register TMbright *ls; |
262 |
< |
int len; |
263 |
< |
int wt; |
264 |
< |
{ |
265 |
< |
static char funcName[] = "tmAddHisto"; |
344 |
> |
static const char funcName[] = "tmAddHisto"; |
345 |
|
int oldorig=0, oldlen, horig, hlen; |
346 |
< |
register int i, j; |
346 |
> |
int i, j; |
347 |
|
|
348 |
< |
if (tmTop == NULL) |
348 |
> |
if (tms == NULL) |
349 |
|
returnErr(TM_E_TMINVAL); |
350 |
|
if (len < 0) |
351 |
|
returnErr(TM_E_ILLEGAL); |
352 |
|
if (len == 0) |
353 |
|
returnOK; |
354 |
|
/* first, grow limits */ |
355 |
< |
if (tmTop->histo == NULL) { |
355 |
> |
if (tms->histo == NULL) { |
356 |
|
for (i = len; i-- && ls[i] < MINBRT; ) |
357 |
|
; |
358 |
|
if (i < 0) |
359 |
|
returnOK; |
360 |
< |
tmTop->hbrmin = tmTop->hbrmax = ls[i]; |
360 |
> |
tms->hbrmin = tms->hbrmax = ls[i]; |
361 |
|
oldlen = 0; |
362 |
|
} else { |
363 |
< |
oldorig = (tmTop->hbrmin-MINBRT)/HISTEP; |
364 |
< |
oldlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - oldorig; |
363 |
> |
oldorig = HISTI(tms->hbrmin); |
364 |
> |
oldlen = HISTI(tms->hbrmax) + 1 - oldorig; |
365 |
|
} |
366 |
|
for (i = len; i--; ) { |
367 |
|
if ((j = ls[i]) < MINBRT) |
368 |
|
continue; |
369 |
< |
if (j < tmTop->hbrmin) |
370 |
< |
tmTop->hbrmin = j; |
371 |
< |
else if (j > tmTop->hbrmax) |
372 |
< |
tmTop->hbrmax = j; |
369 |
> |
if (j < tms->hbrmin) |
370 |
> |
tms->hbrmin = j; |
371 |
> |
else if (j > tms->hbrmax) |
372 |
> |
tms->hbrmax = j; |
373 |
|
} |
374 |
< |
horig = (tmTop->hbrmin-MINBRT)/HISTEP; |
375 |
< |
hlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - horig; |
374 |
> |
horig = HISTI(tms->hbrmin); |
375 |
> |
hlen = HISTI(tms->hbrmax) + 1 - horig; |
376 |
|
if (hlen > oldlen) { /* (re)allocate histogram */ |
377 |
< |
register int *newhist = (int *)calloc(hlen, sizeof(int)); |
377 |
> |
int *newhist = (int *)calloc(hlen, sizeof(int)); |
378 |
|
if (newhist == NULL) |
379 |
|
returnErr(TM_E_NOMEM); |
380 |
|
if (oldlen) { /* copy and free old */ |
381 |
|
for (i = oldlen, j = i+oldorig-horig; i; ) |
382 |
< |
newhist[--j] = tmTop->histo[--i]; |
383 |
< |
free((MEM_PTR)tmTop->histo); |
382 |
> |
newhist[--j] = tms->histo[--i]; |
383 |
> |
free((MEM_PTR)tms->histo); |
384 |
|
} |
385 |
< |
tmTop->histo = newhist; |
385 |
> |
tms->histo = newhist; |
386 |
|
} |
387 |
|
if (wt == 0) |
388 |
|
returnOK; |
389 |
|
for (i = len; i--; ) /* add in new counts */ |
390 |
|
if (ls[i] >= MINBRT) |
391 |
< |
tmTop->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt; |
391 |
> |
tms->histo[ HISTI(ls[i]) - horig ] += wt; |
392 |
|
returnOK; |
393 |
|
} |
394 |
|
|
395 |
|
|
396 |
|
static double |
397 |
< |
htcontrs(La) /* human threshold contrast sensitivity, dL(La) */ |
398 |
< |
double La; |
397 |
> |
htcontrs( /* human threshold contrast sensitivity, dL(La) */ |
398 |
> |
double La |
399 |
> |
) |
400 |
|
{ |
401 |
|
double l10La, l10dL; |
402 |
|
/* formula taken from Ferwerda et al. [SG96] */ |
416 |
|
} |
417 |
|
|
418 |
|
|
339 |
– |
static int |
340 |
– |
tmNewMap() |
341 |
– |
{ |
342 |
– |
if (tmTop->lumap != NULL && (tmTop->mbrmax - tmTop->mbrmin) != |
343 |
– |
(tmTop->hbrmax - tmTop->hbrmin)) { |
344 |
– |
free((MEM_PTR)tmTop->lumap); |
345 |
– |
tmTop->lumap = NULL; |
346 |
– |
} |
347 |
– |
tmTop->mbrmin = tmTop->hbrmin; |
348 |
– |
tmTop->mbrmax = tmTop->hbrmax; |
349 |
– |
if (tmTop->mbrmin > tmTop->mbrmax) |
350 |
– |
return 0; |
351 |
– |
if (tmTop->lumap == NULL) |
352 |
– |
tmTop->lumap = (unsigned short *)malloc(sizeof(unsigned short)* |
353 |
– |
(tmTop->mbrmax-tmTop->mbrmin+1)); |
354 |
– |
return(tmTop->lumap != NULL); |
355 |
– |
} |
356 |
– |
|
357 |
– |
|
419 |
|
int |
420 |
< |
tmFixedMapping(expmult, gamval) |
421 |
< |
double expmult; |
422 |
< |
double gamval; |
420 |
> |
tmFixedMapping( /* compute fixed, linear tone-mapping */ |
421 |
> |
TMstruct *tms, |
422 |
> |
double expmult, |
423 |
> |
double gamval |
424 |
> |
) |
425 |
|
{ |
426 |
< |
static char funcName[] = "tmFixedMapping"; |
426 |
> |
static const char funcName[] = "tmFixedMapping"; |
427 |
|
double d; |
428 |
< |
register int i; |
428 |
> |
int i; |
429 |
|
|
430 |
< |
if (!tmNewMap()) |
430 |
> |
if (!tmNewMap(tms)) |
431 |
|
returnErr(TM_E_NOMEM); |
432 |
|
if (expmult <= .0) |
433 |
|
expmult = 1.; |
434 |
|
if (gamval < MINGAM) |
435 |
< |
gamval = tmTop->mongam; |
436 |
< |
d = log(expmult/tmTop->inpsf); |
437 |
< |
for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) |
438 |
< |
tmTop->lumap[i] = 256. * exp( |
439 |
< |
( d + (tmTop->mbrmin+i)*(1./TM_BRTSCALE) ) |
440 |
< |
/ gamval ); |
435 |
> |
gamval = tms->mongam; |
436 |
> |
d = log(expmult/tms->inpsf); |
437 |
> |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
438 |
> |
double val = 256. * exp( |
439 |
> |
( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) |
440 |
> |
/ gamval); |
441 |
> |
tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val; |
442 |
> |
} |
443 |
|
returnOK; |
444 |
|
} |
445 |
|
|
446 |
|
|
447 |
|
int |
448 |
< |
tmComputeMapping(gamval, Lddyn, Ldmax) |
449 |
< |
double gamval; |
450 |
< |
double Lddyn; |
451 |
< |
double Ldmax; |
448 |
> |
tmComputeMapping( /* compute histogram tone-mapping */ |
449 |
> |
TMstruct *tms, |
450 |
> |
double gamval, |
451 |
> |
double Lddyn, |
452 |
> |
double Ldmax |
453 |
> |
) |
454 |
|
{ |
455 |
< |
static char funcName[] = "tmComputeMapping"; |
455 |
> |
static const char funcName[] = "tmComputeMapping"; |
456 |
|
int *histo; |
457 |
|
float *cumf; |
458 |
|
int brt0, histlen, threshold, ceiling, trimmings; |
459 |
|
double logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld; |
460 |
|
int32 histot; |
461 |
|
double sum; |
462 |
< |
register double d; |
463 |
< |
register int i, j; |
462 |
> |
double d; |
463 |
> |
int i, j; |
464 |
|
|
465 |
< |
if (tmTop == NULL || tmTop->histo == NULL) |
465 |
> |
if (tms == NULL || tms->histo == NULL) |
466 |
|
returnErr(TM_E_TMINVAL); |
467 |
|
/* check arguments */ |
468 |
|
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; |
469 |
|
if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX; |
470 |
< |
if (gamval < MINGAM) gamval = tmTop->mongam; |
470 |
> |
if (gamval < MINGAM) gamval = tms->mongam; |
471 |
|
/* compute handy values */ |
472 |
|
Ldmin = Ldmax/Lddyn; |
473 |
|
logLddyn = log(Lddyn); |
474 |
|
Ldavg = sqrt(Ldmax*Ldmin); |
475 |
< |
i = (tmTop->hbrmin-MINBRT)/HISTEP; |
476 |
< |
brt0 = MINBRT + HISTEP/2 + i*HISTEP; |
477 |
< |
histlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - i; |
475 |
> |
i = HISTI(tms->hbrmin); |
476 |
> |
brt0 = HISTV(i); |
477 |
> |
histlen = HISTI(tms->hbrmax) + 1 - i; |
478 |
|
/* histogram total and mean */ |
479 |
|
histot = 0; sum = 0; |
480 |
|
j = brt0 + histlen*HISTEP; |
481 |
|
for (i = histlen; i--; ) { |
482 |
< |
histot += tmTop->histo[i]; |
483 |
< |
sum += (j -= HISTEP) * tmTop->histo[i]; |
482 |
> |
histot += tms->histo[i]; |
483 |
> |
sum += (double)(j -= HISTEP) * tms->histo[i]; |
484 |
|
} |
485 |
< |
threshold = histot*.025 + .5; |
486 |
< |
if (threshold < 4) |
485 |
> |
threshold = histot*0.005 + .5; |
486 |
> |
if (!histot) |
487 |
|
returnErr(TM_E_TMFAIL); |
488 |
|
Lwavg = tmLuminance( (double)sum / histot ); |
422 |
– |
/* allocate space for mapping */ |
423 |
– |
if (!tmNewMap()) |
424 |
– |
returnErr(TM_E_NOMEM); |
489 |
|
/* use linear tone mapping? */ |
490 |
< |
if (tmTop->flags & TM_F_LINEAR) |
490 |
> |
if (tms->flags & TM_F_LINEAR || threshold < 4 || |
491 |
> |
tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) |
492 |
|
goto linearmap; |
493 |
|
/* clamp histogram */ |
494 |
|
histo = (int *)malloc(histlen*sizeof(int)); |
495 |
|
cumf = (float *)malloc((histlen+2)*sizeof(float)); |
496 |
< |
if (histo == NULL | cumf == NULL) |
496 |
> |
if ((histo == NULL) | (cumf == NULL)) |
497 |
|
returnErr(TM_E_NOMEM); |
498 |
|
cumf[histlen+1] = 1.; /* guard for assignment code */ |
499 |
|
for (i = histlen; i--; ) /* make malleable copy */ |
500 |
< |
histo[i] = tmTop->histo[i]; |
500 |
> |
histo[i] = tms->histo[i]; |
501 |
|
do { /* iterate to solution */ |
502 |
|
sum = 0; /* cumulative probability */ |
503 |
|
for (i = 0; i < histlen; i++) { |
505 |
|
sum += histo[i]; |
506 |
|
} |
507 |
|
cumf[histlen] = 1.; |
508 |
< |
Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) / |
508 |
> |
Tr = histot * (double)(tms->hbrmax - tms->hbrmin) / |
509 |
|
((double)histlen*TM_BRTSCALE) / logLddyn; |
510 |
|
ceiling = Tr + 1.; |
511 |
|
trimmings = 0; /* clip to envelope */ |
512 |
|
for (i = histlen; i--; ) { |
513 |
< |
if (tmTop->flags & TM_F_HCONTR) { |
513 |
> |
if (tms->flags & TM_F_HCONTR) { |
514 |
|
Lw = tmLuminance(brt0 + i*HISTEP); |
515 |
|
Ld = Ldmin * exp( logLddyn * |
516 |
|
.5*(cumf[i]+cumf[i+1]) ); |
529 |
|
goto linearmap; |
530 |
|
} |
531 |
|
} while (trimmings > threshold); |
532 |
+ |
/* allocate space for mapping */ |
533 |
+ |
if (!tmNewMap(tms)) |
534 |
+ |
returnErr(TM_E_NOMEM); |
535 |
|
/* assign tone-mapping */ |
536 |
< |
for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) { |
537 |
< |
j = d = (double)i/(tmTop->mbrmax-tmTop->mbrmin)*histlen; |
536 |
> |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
537 |
> |
j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen; |
538 |
|
d -= (double)j; |
539 |
|
Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1])); |
540 |
|
d = (Ld - Ldmin)/(Ldmax - Ldmin); |
541 |
< |
tmTop->lumap[i] = 256.*pow(d, 1./gamval); |
541 |
> |
tms->lumap[i] = 256.*pow(d, 1./gamval); |
542 |
|
} |
543 |
|
free((MEM_PTR)histo); /* clean up and return */ |
544 |
|
free((MEM_PTR)cumf); |
545 |
|
returnOK; |
546 |
|
linearmap: /* linear tone-mapping */ |
547 |
< |
if (tmTop->flags & TM_F_HCONTR) |
547 |
> |
if (tms->flags & TM_F_HCONTR) |
548 |
|
d = htcontrs(Ldavg) / htcontrs(Lwavg); |
549 |
|
else |
550 |
|
d = Ldavg / Lwavg; |
551 |
< |
return(tmFixedMapping(tmTop->inpsf*d/Ldmax, gamval)); |
551 |
> |
return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval)); |
552 |
|
} |
553 |
|
|
554 |
|
|
555 |
|
int |
556 |
< |
tmMapPixels(ps, ls, cs, len) |
557 |
< |
register BYTE *ps; |
558 |
< |
TMbright *ls; |
559 |
< |
register BYTE *cs; |
560 |
< |
int len; |
556 |
> |
tmMapPixels( /* apply tone-mapping to pixel(s) */ |
557 |
> |
TMstruct *tms, |
558 |
> |
uby8 *ps, |
559 |
> |
TMbright *ls, |
560 |
> |
uby8 *cs, |
561 |
> |
int len |
562 |
> |
) |
563 |
|
{ |
564 |
< |
static char funcName[] = "tmMapPixels"; |
565 |
< |
register int32 li, pv; |
564 |
> |
static const char funcName[] = "tmMapPixels"; |
565 |
> |
int32 li, pv; |
566 |
|
|
567 |
< |
if (tmTop == NULL || tmTop->lumap == NULL) |
567 |
> |
if (tms == NULL || tms->lumap == NULL) |
568 |
|
returnErr(TM_E_TMINVAL); |
569 |
< |
if (ps == NULL | ls == NULL | len < 0) |
569 |
> |
if ((ps == NULL) | (ls == NULL) | (len < 0)) |
570 |
|
returnErr(TM_E_ILLEGAL); |
571 |
|
while (len--) { |
572 |
< |
if ((li = *ls++) < tmTop->mbrmin) { |
572 |
> |
if ((li = *ls++) < tms->mbrmin) { |
573 |
|
li = 0; |
574 |
|
} else { |
575 |
< |
if (li > tmTop->mbrmax) |
576 |
< |
li = tmTop->mbrmax; |
577 |
< |
li = tmTop->lumap[li - tmTop->mbrmin]; |
575 |
> |
if (li > tms->mbrmax) |
576 |
> |
li = tms->mbrmax; |
577 |
> |
li = tms->lumap[li - tms->mbrmin]; |
578 |
|
} |
579 |
|
if (cs == TM_NOCHROM) |
580 |
|
*ps++ = li>255 ? 255 : li; |
581 |
|
else { |
582 |
< |
pv = *cs++ * li / tmTop->cdiv[RED]; |
582 |
> |
pv = *cs++ * li / tms->cdiv[RED]; |
583 |
|
*ps++ = pv>255 ? 255 : pv; |
584 |
< |
pv = *cs++ * li / tmTop->cdiv[GRN]; |
584 |
> |
pv = *cs++ * li / tms->cdiv[GRN]; |
585 |
|
*ps++ = pv>255 ? 255 : pv; |
586 |
< |
pv = *cs++ * li / tmTop->cdiv[BLU]; |
586 |
> |
pv = *cs++ * li / tms->cdiv[BLU]; |
587 |
|
*ps++ = pv>255 ? 255 : pv; |
588 |
|
} |
589 |
|
} |
591 |
|
} |
592 |
|
|
593 |
|
|
594 |
< |
struct tmStruct * |
595 |
< |
tmPop() /* pop top tone mapping off stack */ |
594 |
> |
TMstruct * |
595 |
> |
tmDup( /* duplicate top tone mapping */ |
596 |
> |
TMstruct *tms |
597 |
> |
) |
598 |
|
{ |
527 |
– |
register struct tmStruct *tms; |
528 |
– |
|
529 |
– |
if ((tms = tmTop) != NULL) |
530 |
– |
tmTop = tms->tmprev; |
531 |
– |
return(tms); |
532 |
– |
} |
533 |
– |
|
534 |
– |
|
535 |
– |
int |
536 |
– |
tmPull(tms) /* pull a tone mapping from stack */ |
537 |
– |
register struct tmStruct *tms; |
538 |
– |
{ |
539 |
– |
register struct tmStruct *tms2; |
540 |
– |
/* special cases first */ |
541 |
– |
if (tms == NULL | tmTop == NULL) |
542 |
– |
return(0); |
543 |
– |
if (tms == tmTop) { |
544 |
– |
tmTop = tms->tmprev; |
545 |
– |
tms->tmprev = NULL; |
546 |
– |
return(1); |
547 |
– |
} |
548 |
– |
for (tms2 = tmTop; tms2->tmprev != NULL; tms2 = tms2->tmprev) |
549 |
– |
if (tms == tms2->tmprev) { /* remove it */ |
550 |
– |
tms2->tmprev = tms->tmprev; |
551 |
– |
tms->tmprev = NULL; |
552 |
– |
return(1); |
553 |
– |
} |
554 |
– |
return(0); /* not found on stack */ |
555 |
– |
} |
556 |
– |
|
557 |
– |
|
558 |
– |
struct tmStruct * |
559 |
– |
tmDup() /* duplicate top tone mapping */ |
560 |
– |
{ |
599 |
|
int len; |
600 |
< |
register int i; |
601 |
< |
register struct tmStruct *tmnew; |
600 |
> |
int i; |
601 |
> |
TMstruct *tmnew; |
602 |
|
|
603 |
< |
if (tmTop == NULL) /* anything to duplicate? */ |
603 |
> |
if (tms == NULL) /* anything to duplicate? */ |
604 |
|
return(NULL); |
605 |
< |
tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct)); |
605 |
> |
tmnew = (TMstruct *)malloc(sizeof(TMstruct)); |
606 |
|
if (tmnew == NULL) |
607 |
|
return(NULL); |
608 |
< |
*tmnew = *tmTop; /* copy everything */ |
608 |
> |
*tmnew = *tms; /* copy everything */ |
609 |
|
if (tmnew->histo != NULL) { /* duplicate histogram */ |
610 |
< |
len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 - |
573 |
< |
(tmnew->hbrmin-MINBRT)/HISTEP; |
610 |
> |
len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); |
611 |
|
tmnew->histo = (int *)malloc(len*sizeof(int)); |
612 |
|
if (tmnew->histo != NULL) |
613 |
|
for (i = len; i--; ) |
614 |
< |
tmnew->histo[i] = tmTop->histo[i]; |
614 |
> |
tmnew->histo[i] = tms->histo[i]; |
615 |
|
} |
616 |
|
if (tmnew->lumap != NULL) { /* duplicate luminance mapping */ |
617 |
|
len = tmnew->mbrmax-tmnew->mbrmin+1; |
619 |
|
len*sizeof(unsigned short) ); |
620 |
|
if (tmnew->lumap != NULL) |
621 |
|
for (i = len; i--; ) |
622 |
< |
tmnew->lumap[i] = tmTop->lumap[i]; |
622 |
> |
tmnew->lumap[i] = tms->lumap[i]; |
623 |
|
} |
624 |
|
/* clear package data */ |
625 |
|
for (i = tmNumPkgs; i--; ) |
626 |
|
tmnew->pd[i] = NULL; |
627 |
< |
tmnew->tmprev = tmTop; /* make copy current */ |
628 |
< |
return(tmTop = tmnew); |
627 |
> |
/* return copy */ |
628 |
> |
return(tmnew); |
629 |
|
} |
630 |
|
|
631 |
|
|
595 |
– |
int |
596 |
– |
tmPush(tms) /* push tone mapping on top of stack */ |
597 |
– |
register struct tmStruct *tms; |
598 |
– |
{ |
599 |
– |
static char funcName[] = "tmPush"; |
600 |
– |
/* check validity */ |
601 |
– |
if (tms == NULL) |
602 |
– |
returnErr(TM_E_ILLEGAL); |
603 |
– |
if (tms == tmTop) /* check necessity */ |
604 |
– |
returnOK; |
605 |
– |
/* pull if already in stack */ |
606 |
– |
(void)tmPull(tms); |
607 |
– |
/* push it on top */ |
608 |
– |
tms->tmprev = tmTop; |
609 |
– |
tmTop = tms; |
610 |
– |
returnOK; |
611 |
– |
} |
612 |
– |
|
613 |
– |
|
632 |
|
void |
633 |
|
tmDone(tms) /* done with tone mapping -- destroy it */ |
634 |
< |
register struct tmStruct *tms; |
634 |
> |
TMstruct *tms; |
635 |
|
{ |
636 |
< |
register int i; |
637 |
< |
/* NULL arg. is equiv. to tmTop */ |
638 |
< |
if (tms == NULL && (tms = tmTop) == NULL) |
636 |
> |
int i; |
637 |
> |
/* NULL arg. is equiv. to tms */ |
638 |
> |
if (tms == NULL) |
639 |
|
return; |
622 |
– |
/* take out of stack if present */ |
623 |
– |
(void)tmPull(tms); |
640 |
|
/* free tables */ |
641 |
|
if (tms->histo != NULL) |
642 |
|
free((MEM_PTR)tms->histo); |
651 |
|
|
652 |
|
/******************** Shared but Private library routines *********************/ |
653 |
|
|
654 |
< |
BYTE tmMesofact[BMESUPPER-BMESLOWER]; |
654 |
> |
uby8 tmMesofact[BMESUPPER-BMESLOWER]; |
655 |
|
|
656 |
|
void |
657 |
|
tmMkMesofact() /* build mesopic lookup factor table */ |
658 |
|
{ |
659 |
< |
register int i; |
659 |
> |
int i; |
660 |
|
|
661 |
|
if (tmMesofact[BMESUPPER-BMESLOWER-1]) |
662 |
|
return; |
669 |
|
|
670 |
|
|
671 |
|
int |
672 |
< |
tmErrorReturn(func, err) /* error return (with message) */ |
673 |
< |
char *func; |
674 |
< |
int err; |
672 |
> |
tmNewMap( /* allocate new tone-mapping array */ |
673 |
> |
TMstruct *tms |
674 |
> |
) |
675 |
|
{ |
676 |
< |
tmLastFunction = func; |
677 |
< |
tmLastError = err; |
678 |
< |
if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR) |
679 |
< |
return(err); |
676 |
> |
if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != |
677 |
> |
(tms->hbrmax - tms->hbrmin)) { |
678 |
> |
free((MEM_PTR)tms->lumap); |
679 |
> |
tms->lumap = NULL; |
680 |
> |
} |
681 |
> |
tms->mbrmin = tms->hbrmin; |
682 |
> |
tms->mbrmax = tms->hbrmax; |
683 |
> |
if (tms->mbrmin > tms->mbrmax) |
684 |
> |
return 0; |
685 |
> |
if (tms->lumap == NULL) |
686 |
> |
tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)* |
687 |
> |
(tms->mbrmax-tms->mbrmin+1)); |
688 |
> |
return(tms->lumap != NULL); |
689 |
> |
} |
690 |
> |
|
691 |
> |
|
692 |
> |
int |
693 |
> |
tmErrorReturn( /* error return (with message) */ |
694 |
> |
const char *func, |
695 |
> |
TMstruct *tms, |
696 |
> |
int err |
697 |
> |
) |
698 |
> |
{ |
699 |
> |
if (tms != NULL) { |
700 |
> |
tms->lastFunc = func; |
701 |
> |
tms->lastError = err; |
702 |
> |
if (tms->flags & TM_F_NOSTDERR) |
703 |
> |
return(err); |
704 |
> |
} |
705 |
|
fputs(func, stderr); |
706 |
|
fputs(": ", stderr); |
707 |
|
fputs(tmErrorMessage[err], stderr); |