1 |
– |
/* Copyright (c) 1997 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Tone mapping functions. |
6 |
|
* See tonemap.h for detailed function descriptions. |
7 |
+ |
* Added von Kries white-balance calculations 10/01 (GW). |
8 |
+ |
* |
9 |
+ |
* Externals declared in tonemap.h |
10 |
|
*/ |
11 |
|
|
12 |
+ |
#include "copyright.h" |
13 |
+ |
|
14 |
|
#include <stdio.h> |
15 |
+ |
#include <stdlib.h> |
16 |
|
#include <math.h> |
17 |
+ |
#include <string.h> |
18 |
|
#include "tmprivat.h" |
19 |
|
#include "tmerrmsg.h" |
20 |
|
|
21 |
|
#define exp10(x) exp(M_LN10*(x)) |
22 |
|
|
19 |
– |
struct tmStruct *tmTop = NULL; /* current tone mapping stack */ |
20 |
– |
|
23 |
|
/* our list of conversion packages */ |
24 |
|
struct tmPackage *tmPkg[TM_MAXPKG]; |
25 |
|
int tmNumPkgs = 0; /* number of registered packages */ |
26 |
|
|
27 |
< |
int tmLastError; /* last error incurred by library */ |
28 |
< |
char *tmLastFunction; /* error-generating function name */ |
27 |
> |
/* luminance->brightness lookup */ |
28 |
> |
static TMbright *tmFloat2BrtLUT = NULL; |
29 |
|
|
30 |
+ |
#define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15] |
31 |
|
|
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); |
39 |
– |
fputs(": ", stderr); |
40 |
– |
fputs(tmErrorMessage[err], stderr); |
41 |
– |
fputs("!\n", stderr); |
42 |
– |
return(err); |
43 |
– |
} |
32 |
|
|
33 |
< |
|
34 |
< |
struct tmStruct * |
35 |
< |
tmInit(flags, monpri, gamval) /* initialize new tone mapping */ |
36 |
< |
int flags; |
37 |
< |
RGBPRIMP monpri; |
38 |
< |
double gamval; |
33 |
> |
TMstruct * |
34 |
> |
tmInit( /* initialize new tone mapping */ |
35 |
> |
int flags, |
36 |
> |
RGBPRIMP monpri, |
37 |
> |
double gamval |
38 |
> |
) |
39 |
|
{ |
52 |
– |
static char funcName[] = "tmInit"; |
40 |
|
COLORMAT cmat; |
41 |
< |
register struct tmStruct *tmnew; |
42 |
< |
register int i; |
41 |
> |
TMstruct *tmnew; |
42 |
> |
int i; |
43 |
|
/* allocate structure */ |
44 |
< |
tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct)); |
44 |
> |
tmnew = (TMstruct *)malloc(sizeof(TMstruct)); |
45 |
|
if (tmnew == NULL) |
46 |
|
return(NULL); |
47 |
|
|
48 |
|
tmnew->flags = flags & ~TM_F_UNIMPL; |
49 |
+ |
if (tmnew->flags & TM_F_BW) |
50 |
+ |
tmnew->flags &= ~TM_F_MESOPIC; |
51 |
|
/* set monitor transform */ |
52 |
|
if (monpri == NULL || monpri == stdprims || tmnew->flags & TM_F_BW) { |
53 |
|
tmnew->monpri = stdprims; |
67 |
|
tmnew->mongam = gamval; |
68 |
|
/* set color divisors */ |
69 |
|
for (i = 0; i < 3; i++) |
70 |
< |
tmnew->cdiv[i] = 256.*pow(tmnew->clf[i], 1./tmnew->mongam); |
70 |
> |
tmnew->cdiv[i] = TM_BRES*pow(tmnew->clf[i], 1./tmnew->mongam); |
71 |
|
|
72 |
|
/* set input transform */ |
73 |
|
tmnew->inppri = tmnew->monpri; |
75 |
|
tmnew->inpsf = WHTEFFICACY; |
76 |
|
tmnew->cmat[0][1] = tmnew->cmat[0][2] = tmnew->cmat[1][0] = |
77 |
|
tmnew->cmat[1][2] = tmnew->cmat[2][0] = tmnew->cmat[2][1] = 0.; |
78 |
< |
tmnew->hbrmin = tmnew->hbrmax = 0; |
78 |
> |
tmnew->inpdat = NULL; |
79 |
> |
tmnew->hbrmin = 10; tmnew->hbrmax = -10; |
80 |
|
tmnew->histo = NULL; |
81 |
< |
tmnew->mbrmin = tmnew->mbrmax = 0; |
81 |
> |
tmnew->mbrmin = 10; tmnew->mbrmax = -10; |
82 |
|
tmnew->lumap = NULL; |
83 |
|
/* zero private data */ |
84 |
|
for (i = TM_MAXPKG; i--; ) |
85 |
|
tmnew->pd[i] = NULL; |
86 |
< |
/* make tmnew current */ |
87 |
< |
tmnew->tmprev = tmTop; |
88 |
< |
return(tmTop = tmnew); |
86 |
> |
tmnew->lastError = TM_E_OK; |
87 |
> |
tmnew->lastFunc = "NoErr"; |
88 |
> |
/* return new TMstruct */ |
89 |
> |
return(tmnew); |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
int |
94 |
< |
tmSetSpace(pri, sf) /* set input color space for conversions */ |
95 |
< |
RGBPRIMP pri; |
96 |
< |
double sf; |
94 |
> |
tmSetSpace( /* set input color space for conversions */ |
95 |
> |
TMstruct *tms, |
96 |
> |
RGBPRIMP pri, |
97 |
> |
double sf, |
98 |
> |
MEM_PTR dat |
99 |
> |
) |
100 |
|
{ |
101 |
< |
static char funcName[] = "tmSetSpace"; |
102 |
< |
register int i, j; |
101 |
> |
static const char funcName[] = "tmSetSpace"; |
102 |
> |
int i, j; |
103 |
|
/* error check */ |
104 |
< |
if (tmTop == NULL) |
104 |
> |
if (tms == NULL) |
105 |
|
returnErr(TM_E_TMINVAL); |
106 |
|
if (sf <= 1e-12) |
107 |
|
returnErr(TM_E_ILLEGAL); |
108 |
|
/* check if no change */ |
109 |
< |
if (pri == tmTop->inppri && FEQ(sf, tmTop->inpsf)) |
109 |
> |
if (pri == tms->inppri && FEQ(sf, tms->inpsf) && dat == tms->inpdat) |
110 |
|
returnOK; |
111 |
< |
tmTop->inppri = pri; /* let's set it */ |
112 |
< |
tmTop->inpsf = sf; |
111 |
> |
tms->inppri = pri; /* let's set it */ |
112 |
> |
tms->inpsf = sf; |
113 |
> |
tms->inpdat = dat; |
114 |
|
|
115 |
< |
if (tmTop->flags & TM_F_BW) { /* color doesn't matter */ |
116 |
< |
tmTop->monpri = tmTop->inppri; /* eliminate xform */ |
117 |
< |
if (tmTop->inppri == TM_XYZPRIM) { |
118 |
< |
tmTop->clf[CIEX] = tmTop->clf[CIEZ] = 0.; |
119 |
< |
tmTop->clf[CIEY] = 1.; |
115 |
> |
if (tms->flags & TM_F_BW) { /* color doesn't matter */ |
116 |
> |
tms->monpri = tms->inppri; /* eliminate xform */ |
117 |
> |
if (tms->inppri == TM_XYZPRIM) { |
118 |
> |
tms->clf[CIEX] = tms->clf[CIEZ] = 0.; |
119 |
> |
tms->clf[CIEY] = 1.; |
120 |
|
} else { |
121 |
< |
comprgb2xyzmat(tmTop->cmat, tmTop->monpri); |
122 |
< |
tmTop->clf[RED] = tmTop->cmat[1][0]; |
123 |
< |
tmTop->clf[GRN] = tmTop->cmat[1][1]; |
124 |
< |
tmTop->clf[BLU] = tmTop->cmat[1][2]; |
121 |
> |
comprgb2xyzmat(tms->cmat, tms->monpri); |
122 |
> |
tms->clf[RED] = tms->cmat[1][0]; |
123 |
> |
tms->clf[GRN] = tms->cmat[1][1]; |
124 |
> |
tms->clf[BLU] = tms->cmat[1][2]; |
125 |
|
} |
126 |
< |
tmTop->cmat[0][0] = tmTop->cmat[1][1] = tmTop->cmat[2][2] = |
127 |
< |
tmTop->inpsf; |
128 |
< |
tmTop->cmat[0][1] = tmTop->cmat[0][2] = tmTop->cmat[1][0] = |
134 |
< |
tmTop->cmat[1][2] = tmTop->cmat[2][0] = tmTop->cmat[2][1] = 0.; |
126 |
> |
tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] = 1.; |
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 |
< |
compxyz2rgbmat(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 |
< |
comprgb2rgbmat(tmTop->cmat, tmTop->inppri, tmTop->monpri); |
133 |
> |
} else { /* input is RGB */ |
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] = |
152 |
< |
256.*pow(tmTop->clf[i], 1./tmTop->mongam); |
153 |
< |
else |
154 |
< |
tmTop->cdiv[i] = 1; |
145 |
> |
tms->cdiv[i] = TM_BRES*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(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; |
185 |
< |
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]); |
194 |
< |
else { |
195 |
< |
cmon[RED] = tmTop->inpsf*scan[i][RED]; |
196 |
< |
cmon[GRN] = tmTop->inpsf*scan[i][GRN]; |
197 |
< |
cmon[BLU] = tmTop->inpsf*scan[i][BLU]; |
198 |
< |
} |
199 |
< |
/* world luminance */ |
200 |
< |
lum = tmTop->clf[RED]*cmon[RED] + |
201 |
< |
tmTop->clf[GRN]*cmon[GRN] + |
202 |
< |
tmTop->clf[BLU]*cmon[BLU] ; |
203 |
< |
/* check range */ |
204 |
< |
if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite)) |
205 |
< |
lum = tmTop->clf[RED]*cmon[RED] + |
206 |
< |
tmTop->clf[GRN]*cmon[GRN] + |
207 |
< |
tmTop->clf[BLU]*cmon[BLU] ; |
208 |
< |
if (lum < MINLUM) { |
209 |
< |
ls[i] = MINBRT-1; /* bogus value */ |
210 |
< |
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)); |
242 |
< |
d = tmTop->clf[BLU]*cmon[BLU]/lum; |
243 |
< |
cs[3*i+2] = d>=.999 ? 255 : |
244 |
< |
(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 |
< |
tmAddHisto(ls, len, wt) /* add values to histogram */ |
338 |
< |
register TMbright *ls; |
339 |
< |
int len; |
340 |
< |
int wt; |
337 |
> |
tmAddHisto( /* add values to histogram */ |
338 |
> |
TMstruct *tms, |
339 |
> |
TMbright *ls, |
340 |
> |
int len, |
341 |
> |
int wt |
342 |
> |
) |
343 |
|
{ |
344 |
< |
static char funcName[] = "tmAddHisto"; |
345 |
< |
int oldorig, oldlen, horig, hlen; |
346 |
< |
register int i, j; |
344 |
> |
static const char funcName[] = "tmAddHisto"; |
345 |
> |
int oldorig=0, oldlen, horig, hlen; |
346 |
> |
int i, j; |
347 |
|
|
348 |
< |
if (len <= 0) |
261 |
< |
returnErr(TM_E_ILLEGAL); |
262 |
< |
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 |
> |
HIST_TYP *newhist = (HIST_TYP *)calloc(hlen, sizeof(HIST_TYP)); |
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); |
381 |
> |
memcpy(newhist+(oldorig-horig), |
382 |
> |
tms->histo, oldlen*sizeof(HIST_TYP)); |
383 |
> |
free(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] */ |
417 |
|
|
418 |
|
|
419 |
|
int |
420 |
< |
tmComputeMapping(gamval, Lddyn, Ldmax) |
421 |
< |
double gamval; |
422 |
< |
double Lddyn; |
423 |
< |
double Ldmax; |
420 |
> |
tmFixedMapping( /* compute fixed, linear tone-mapping */ |
421 |
> |
TMstruct *tms, |
422 |
> |
double expmult, |
423 |
> |
double gamval, |
424 |
> |
double Lddyn |
425 |
> |
) |
426 |
|
{ |
427 |
< |
static char funcName[] = "tmComputeMapping"; |
428 |
< |
int *histo; |
427 |
> |
static const char funcName[] = "tmFixedMapping"; |
428 |
> |
int maxV = (1L<<(8*sizeof(TMAP_TYP))) - 1; |
429 |
> |
double minD; |
430 |
> |
int i; |
431 |
> |
|
432 |
> |
if (!tmNewMap(tms)) |
433 |
> |
returnErr(TM_E_NOMEM); |
434 |
> |
/* check arguments */ |
435 |
> |
if (expmult <= .0) expmult = 1.; |
436 |
> |
if (gamval < MINGAM) gamval = tms->mongam; |
437 |
> |
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; |
438 |
> |
minD = 1./Lddyn; |
439 |
> |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
440 |
> |
double d; |
441 |
> |
d = expmult/tms->inpsf * tmLuminance(tms->mbrmin + i); |
442 |
> |
if (d <= minD) |
443 |
> |
break; /* map initialized to zeroes */ |
444 |
> |
d = (d - minD)/(1. - minD); |
445 |
> |
d = TM_BRES*pow(d, 1./gamval); |
446 |
> |
tms->lumap[i] = (d >= maxV) ? maxV : (int)d; |
447 |
> |
} |
448 |
> |
returnOK; |
449 |
> |
} |
450 |
> |
|
451 |
> |
|
452 |
> |
int |
453 |
> |
tmComputeMapping( /* compute histogram tone-mapping */ |
454 |
> |
TMstruct *tms, |
455 |
> |
double gamval, |
456 |
> |
double Lddyn, |
457 |
> |
double Ldmax |
458 |
> |
) |
459 |
> |
{ |
460 |
> |
static const char funcName[] = "tmComputeMapping"; |
461 |
> |
HIST_TYP *histo; |
462 |
|
float *cumf; |
463 |
< |
int brt0, histlen, histot, threshold, ceiling, trimmings; |
464 |
< |
double logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld; |
465 |
< |
int4 sum; |
466 |
< |
register double d; |
467 |
< |
register int i, j; |
463 |
> |
int brt0, histlen; |
464 |
> |
HIST_TYP threshold, ceiling, trimmings, histot; |
465 |
> |
double logLddyn, Ldmin, Lwavg, Tr, Lw, Ld; |
466 |
> |
double sum; |
467 |
> |
double d; |
468 |
> |
int i, j; |
469 |
|
|
470 |
< |
if (tmTop == NULL || tmTop->histo == NULL) |
470 |
> |
if (tms == NULL || tms->histo == NULL) |
471 |
|
returnErr(TM_E_TMINVAL); |
472 |
|
/* check arguments */ |
473 |
|
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; |
474 |
|
if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX; |
475 |
< |
if (gamval < MINGAM) gamval = tmTop->mongam; |
475 |
> |
if (gamval < MINGAM) gamval = tms->mongam; |
476 |
|
/* compute handy values */ |
477 |
|
Ldmin = Ldmax/Lddyn; |
478 |
|
logLddyn = log(Lddyn); |
479 |
< |
Ldavg = sqrt(Ldmax*Ldmin); |
480 |
< |
i = (tmTop->hbrmin-MINBRT)/HISTEP; |
481 |
< |
brt0 = MINBRT + HISTEP/2 + i*HISTEP; |
355 |
< |
histlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - i; |
479 |
> |
i = HISTI(tms->hbrmin); |
480 |
> |
brt0 = HISTV(i); |
481 |
> |
histlen = HISTI(tms->hbrmax) + 1 - i; |
482 |
|
/* histogram total and mean */ |
483 |
|
histot = 0; sum = 0; |
484 |
|
j = brt0 + histlen*HISTEP; |
485 |
|
for (i = histlen; i--; ) { |
486 |
< |
histot += tmTop->histo[i]; |
487 |
< |
sum += (j -= HISTEP) * tmTop->histo[i]; |
486 |
> |
histot += tms->histo[i]; |
487 |
> |
sum += (double)(j -= HISTEP) * tms->histo[i]; |
488 |
|
} |
489 |
< |
threshold = histot*.025 + .5; |
490 |
< |
if (threshold < 4) |
489 |
> |
threshold = histot*0.002 + .5; |
490 |
> |
if (!histot) |
491 |
|
returnErr(TM_E_TMFAIL); |
492 |
|
Lwavg = tmLuminance( (double)sum / histot ); |
493 |
< |
if (!(tmTop->flags & TM_F_LINEAR)) { /* clamp histogram */ |
494 |
< |
histo = (int *)malloc(histlen*sizeof(int)); |
495 |
< |
cumf = (float *)malloc((histlen+1)*sizeof(float)); |
496 |
< |
if (histo == NULL | cumf == NULL) |
497 |
< |
returnErr(TM_E_NOMEM); |
498 |
< |
for (i = histlen; i--; ) /* make malleable copy */ |
499 |
< |
histo[i] = tmTop->histo[i]; |
500 |
< |
do { /* iterate to solution */ |
501 |
< |
sum = 0; /* cumulative probability */ |
502 |
< |
for (i = 0; i < histlen; i++) { |
503 |
< |
cumf[i] = (double)sum/histot; |
504 |
< |
sum += histo[i]; |
493 |
> |
/* use linear tone mapping? */ |
494 |
> |
if (tms->flags & TM_F_LINEAR || threshold < 4 || |
495 |
> |
tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) |
496 |
> |
goto linearmap; |
497 |
> |
/* clamp histogram */ |
498 |
> |
histo = (HIST_TYP *)malloc(histlen*sizeof(HIST_TYP)); |
499 |
> |
cumf = (float *)malloc((histlen+2)*sizeof(float)); |
500 |
> |
if ((histo == NULL) | (cumf == NULL)) |
501 |
> |
returnErr(TM_E_NOMEM); |
502 |
> |
cumf[histlen+1] = 1.; /* guard for assignment code */ |
503 |
> |
/* make malleable copy */ |
504 |
> |
memcpy(histo, tms->histo, histlen*sizeof(HIST_TYP)); |
505 |
> |
do { /* iterate to solution */ |
506 |
> |
sum = 0; /* cumulative probability */ |
507 |
> |
for (i = 0; i < histlen; i++) { |
508 |
> |
cumf[i] = sum/histot; |
509 |
> |
sum += (double)histo[i]; |
510 |
> |
} |
511 |
> |
cumf[histlen] = 1.; |
512 |
> |
Tr = histot * (double)(tms->hbrmax - tms->hbrmin) / |
513 |
> |
((double)TM_BRTSCALE*histlen*logLddyn); |
514 |
> |
ceiling = Tr + 1.; |
515 |
> |
trimmings = 0; /* clip to envelope */ |
516 |
> |
for (i = histlen; i--; ) { |
517 |
> |
if (tms->flags & TM_F_HCONTR) { |
518 |
> |
Lw = tmLuminance(brt0 + i*HISTEP); |
519 |
> |
Ld = Ldmin * exp( logLddyn * |
520 |
> |
.5*(cumf[i]+cumf[i+1]) ); |
521 |
> |
ceiling = Tr * (htcontrs(Ld) * Lw) / |
522 |
> |
(htcontrs(Lw) * Ld) + 1.; |
523 |
|
} |
524 |
< |
cumf[i] = 1.; |
525 |
< |
Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) / |
526 |
< |
((double)histlen*TM_BRTSCALE) / logLddyn; |
383 |
< |
ceiling = Tr + 1.; |
384 |
< |
trimmings = 0; /* clip to envelope */ |
385 |
< |
for (i = histlen; i--; ) { |
386 |
< |
if (tmTop->flags & TM_F_HCONTR) { |
387 |
< |
Lw = tmLuminance(brt0 + i*HISTEP); |
388 |
< |
Ld = Ldmin * exp( logLddyn * |
389 |
< |
.5*(cumf[i]+cumf[i+1]) ); |
390 |
< |
ceiling = Tr * (htcontrs(Ld) * Lw) / |
391 |
< |
(htcontrs(Lw) * Ld) + 1.; |
392 |
< |
} |
393 |
< |
if (histo[i] > ceiling) { |
394 |
< |
trimmings += histo[i] - ceiling; |
395 |
< |
histo[i] = ceiling; |
396 |
< |
} |
524 |
> |
if (histo[i] > ceiling) { |
525 |
> |
trimmings += histo[i] - ceiling; |
526 |
> |
histo[i] = ceiling; |
527 |
|
} |
398 |
– |
} while ((histot -= trimmings) > threshold && |
399 |
– |
trimmings > threshold); |
400 |
– |
} |
401 |
– |
/* allocate luminance map */ |
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) |
413 |
– |
d = htcontrs(Ldavg) / htcontrs(Lwavg); |
414 |
– |
else |
415 |
– |
d = Ldavg / Lwavg; |
416 |
– |
d = log(d/Ldmax); |
417 |
– |
for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) |
418 |
– |
tmTop->lumap[i] = 256. * exp( |
419 |
– |
( d + (tmTop->mbrmin+i)/(double)TM_BRTSCALE ) |
420 |
– |
/ gamval ); |
421 |
– |
} else { |
422 |
– |
/* histogram adjustment */ |
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); |
428 |
– |
tmTop->lumap[i] = 256.*pow(d, 1./gamval); |
528 |
|
} |
529 |
+ |
/* check if we're out of data */ |
530 |
+ |
if ((histot -= trimmings) <= threshold) { |
531 |
+ |
free(histo); |
532 |
+ |
free(cumf); |
533 |
+ |
goto linearmap; |
534 |
+ |
} |
535 |
+ |
} while (trimmings > threshold); |
536 |
+ |
/* allocate space for mapping */ |
537 |
+ |
if (!tmNewMap(tms)) |
538 |
+ |
returnErr(TM_E_NOMEM); |
539 |
+ |
/* assign tone-mapping */ |
540 |
+ |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
541 |
+ |
j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen; |
542 |
+ |
d -= (double)j; |
543 |
+ |
Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1])); |
544 |
+ |
d = (Ld - Ldmin)/(Ldmax - Ldmin); |
545 |
+ |
tms->lumap[i] = TM_BRES*pow(d, 1./gamval); |
546 |
|
} |
547 |
< |
if (!(tmTop->flags & TM_F_LINEAR)) { |
548 |
< |
free((MEM_PTR)histo); |
433 |
< |
free((MEM_PTR)cumf); |
434 |
< |
} |
547 |
> |
free(histo); /* clean up and return */ |
548 |
> |
free(cumf); |
549 |
|
returnOK; |
550 |
+ |
linearmap: /* linear tone-mapping */ |
551 |
+ |
if (tms->flags & TM_F_HCONTR) |
552 |
+ |
d = htcontrs(sqrt(Ldmax*Ldmin)) / htcontrs(Lwavg); |
553 |
+ |
else |
554 |
+ |
d = Ldmax / tmLuminance(tms->hbrmax); |
555 |
+ |
return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval, Lddyn)); |
556 |
|
} |
557 |
|
|
558 |
|
|
559 |
|
int |
560 |
< |
tmMapPixels(ps, ls, cs, len) |
561 |
< |
register BYTE *ps; |
562 |
< |
TMbright *ls; |
563 |
< |
register BYTE *cs; |
564 |
< |
int len; |
560 |
> |
tmMapPixels( /* apply tone-mapping to pixel(s) */ |
561 |
> |
TMstruct *tms, |
562 |
> |
uby8 *ps, |
563 |
> |
TMbright *ls, |
564 |
> |
uby8 *cs, |
565 |
> |
int len |
566 |
> |
) |
567 |
|
{ |
568 |
< |
static char funcName[] = "tmMapPixels"; |
569 |
< |
register int4 li, pv; |
568 |
> |
static const char funcName[] = "tmMapPixels"; |
569 |
> |
TMbright lv; |
570 |
> |
TMAP_TYP li; |
571 |
> |
int pv; |
572 |
|
|
573 |
< |
if (tmTop == NULL || tmTop->lumap == NULL) |
573 |
> |
if (tms == NULL || tms->lumap == NULL) |
574 |
|
returnErr(TM_E_TMINVAL); |
575 |
< |
if (ps == NULL | ls == NULL | len <= 0) |
575 |
> |
if ((ps == NULL) | (ls == NULL) | (len < 0)) |
576 |
|
returnErr(TM_E_ILLEGAL); |
577 |
|
while (len--) { |
578 |
< |
if ((li = *ls++) < tmTop->mbrmin) |
579 |
< |
li = tmTop->mbrmin; |
580 |
< |
else if (li > tmTop->mbrmax) |
581 |
< |
li = tmTop->mbrmax; |
582 |
< |
li = tmTop->lumap[li - tmTop->mbrmin]; |
583 |
< |
if (cs == TM_NOCHROM) |
584 |
< |
*ps++ = li>255 ? 255 : li; |
585 |
< |
else { |
586 |
< |
pv = *cs++ * li / tmTop->cdiv[RED]; |
578 |
> |
if ((lv = *ls++) < tms->mbrmin) { |
579 |
> |
li = 0; |
580 |
> |
} else { |
581 |
> |
if (lv > tms->mbrmax) |
582 |
> |
lv = tms->mbrmax; |
583 |
> |
li = tms->lumap[lv - tms->mbrmin]; |
584 |
> |
} |
585 |
> |
if (cs == TM_NOCHROM) { |
586 |
> |
#if !(TM_BRES & 0xff) |
587 |
> |
*ps++ = li>=TM_BRES ? 255 : li/(TM_BRES>>8); |
588 |
> |
#else |
589 |
> |
*ps++ = li>=TM_BRES ? 255 : (li<<8)/TM_BRES; |
590 |
> |
#endif |
591 |
> |
} else { |
592 |
> |
pv = *cs++ * li / tms->cdiv[RED]; |
593 |
|
*ps++ = pv>255 ? 255 : pv; |
594 |
< |
pv = *cs++ * li / tmTop->cdiv[GRN]; |
594 |
> |
pv = *cs++ * li / tms->cdiv[GRN]; |
595 |
|
*ps++ = pv>255 ? 255 : pv; |
596 |
< |
pv = *cs++ * li / tmTop->cdiv[BLU]; |
596 |
> |
pv = *cs++ * li / tms->cdiv[BLU]; |
597 |
|
*ps++ = pv>255 ? 255 : pv; |
598 |
|
} |
599 |
|
} |
601 |
|
} |
602 |
|
|
603 |
|
|
604 |
< |
struct tmStruct * |
605 |
< |
tmPop() /* pop top tone mapping off stack */ |
604 |
> |
TMstruct * |
605 |
> |
tmDup( /* duplicate tone mapping */ |
606 |
> |
TMstruct *tms |
607 |
> |
) |
608 |
|
{ |
477 |
– |
register struct tmStruct *tms; |
478 |
– |
|
479 |
– |
if ((tms = tmTop) != NULL) |
480 |
– |
tmTop = tms->tmprev; |
481 |
– |
return(tms); |
482 |
– |
} |
483 |
– |
|
484 |
– |
|
485 |
– |
int |
486 |
– |
tmPull(tms) /* pull a tone mapping from stack */ |
487 |
– |
register struct tmStruct *tms; |
488 |
– |
{ |
489 |
– |
register struct tmStruct *tms2; |
490 |
– |
/* special cases first */ |
491 |
– |
if (tms == NULL | tmTop == NULL) |
492 |
– |
return(0); |
493 |
– |
if (tms == tmTop) { |
494 |
– |
tmTop = tms->tmprev; |
495 |
– |
tms->tmprev = NULL; |
496 |
– |
return(1); |
497 |
– |
} |
498 |
– |
for (tms2 = tmTop; tms2->tmprev != NULL; tms2 = tms2->tmprev) |
499 |
– |
if (tms == tms2->tmprev) { /* remove it */ |
500 |
– |
tms2->tmprev = tms->tmprev; |
501 |
– |
tms->tmprev = NULL; |
502 |
– |
return(1); |
503 |
– |
} |
504 |
– |
return(0); /* not found on stack */ |
505 |
– |
} |
506 |
– |
|
507 |
– |
|
508 |
– |
struct tmStruct * |
509 |
– |
tmDup() /* duplicate top tone mapping */ |
510 |
– |
{ |
609 |
|
int len; |
610 |
< |
register int i; |
611 |
< |
register struct tmStruct *tmnew; |
610 |
> |
int i; |
611 |
> |
TMstruct *tmnew; |
612 |
|
|
613 |
< |
if (tmTop == NULL) /* anything to duplicate? */ |
613 |
> |
if (tms == NULL) /* anything to duplicate? */ |
614 |
|
return(NULL); |
615 |
< |
tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct)); |
615 |
> |
tmnew = (TMstruct *)malloc(sizeof(TMstruct)); |
616 |
|
if (tmnew == NULL) |
617 |
|
return(NULL); |
618 |
< |
*tmnew = *tmTop; /* copy everything */ |
618 |
> |
*tmnew = *tms; /* copy everything */ |
619 |
|
if (tmnew->histo != NULL) { /* duplicate histogram */ |
620 |
< |
len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 - |
621 |
< |
(tmnew->hbrmin-MINBRT)/HISTEP; |
524 |
< |
tmnew->histo = (int *)malloc(len*sizeof(int)); |
620 |
> |
len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); |
621 |
> |
tmnew->histo = (HIST_TYP *)malloc(len*sizeof(HIST_TYP)); |
622 |
|
if (tmnew->histo != NULL) |
623 |
< |
for (i = len; i--; ) |
527 |
< |
tmnew->histo[i] = tmTop->histo[i]; |
623 |
> |
memcpy(tmnew->histo, tms->histo, len*sizeof(HIST_TYP)); |
624 |
|
} |
625 |
|
if (tmnew->lumap != NULL) { /* duplicate luminance mapping */ |
626 |
|
len = tmnew->mbrmax-tmnew->mbrmin+1; |
627 |
< |
tmnew->lumap = (unsigned short *)malloc( |
532 |
< |
len*sizeof(unsigned short) ); |
627 |
> |
tmnew->lumap = (TMAP_TYP *)malloc(len*sizeof(TMAP_TYP)); |
628 |
|
if (tmnew->lumap != NULL) |
629 |
< |
for (i = len; i--; ) |
535 |
< |
tmnew->lumap[i] = tmTop->lumap[i]; |
629 |
> |
memcpy(tmnew->lumap, tms->lumap, len*sizeof(TMAP_TYP)); |
630 |
|
} |
631 |
|
/* clear package data */ |
632 |
|
for (i = tmNumPkgs; i--; ) |
633 |
|
tmnew->pd[i] = NULL; |
634 |
< |
tmnew->tmprev = tmTop; /* make copy current */ |
635 |
< |
return(tmTop = tmnew); |
634 |
> |
/* return copy */ |
635 |
> |
return(tmnew); |
636 |
|
} |
637 |
|
|
638 |
|
|
545 |
– |
int |
546 |
– |
tmPush(tms) /* push tone mapping on top of stack */ |
547 |
– |
register struct tmStruct *tms; |
548 |
– |
{ |
549 |
– |
static char funcName[] = "tmPush"; |
550 |
– |
/* check validity */ |
551 |
– |
if (tms == NULL) |
552 |
– |
returnErr(TM_E_ILLEGAL); |
553 |
– |
if (tms == tmTop) /* check necessity */ |
554 |
– |
returnOK; |
555 |
– |
/* pull if already in stack */ |
556 |
– |
(void)tmPull(tms); |
557 |
– |
/* push it on top */ |
558 |
– |
tms->tmprev = tmTop; |
559 |
– |
tmTop = tms; |
560 |
– |
returnOK; |
561 |
– |
} |
562 |
– |
|
563 |
– |
|
639 |
|
void |
640 |
|
tmDone(tms) /* done with tone mapping -- destroy it */ |
641 |
< |
register struct tmStruct *tms; |
641 |
> |
TMstruct *tms; |
642 |
|
{ |
643 |
< |
register int i; |
644 |
< |
/* NULL arg. is equiv. to tmTop */ |
645 |
< |
if (tms == NULL && (tms = tmTop) == NULL) |
643 |
> |
int i; |
644 |
> |
/* NULL arg. is equiv. to tms */ |
645 |
> |
if (tms == NULL) |
646 |
|
return; |
572 |
– |
/* take out of stack if present */ |
573 |
– |
(void)tmPull(tms); |
647 |
|
/* free tables */ |
648 |
|
if (tms->histo != NULL) |
649 |
< |
free((MEM_PTR)tms->histo); |
649 |
> |
free(tms->histo); |
650 |
|
if (tms->lumap != NULL) |
651 |
< |
free((MEM_PTR)tms->lumap); |
651 |
> |
free(tms->lumap); |
652 |
|
/* free private data */ |
653 |
|
for (i = tmNumPkgs; i--; ) |
654 |
|
if (tms->pd[i] != NULL) |
655 |
|
(*tmPkg[i]->Free)(tms->pd[i]); |
656 |
< |
free((MEM_PTR)tms); /* free basic structure */ |
656 |
> |
free(tms); /* free basic structure */ |
657 |
> |
} |
658 |
> |
|
659 |
> |
/******************** Shared but Private library routines *********************/ |
660 |
> |
|
661 |
> |
uby8 tmMesofact[BMESUPPER-BMESLOWER]; |
662 |
> |
|
663 |
> |
void |
664 |
> |
tmMkMesofact() /* build mesopic lookup factor table */ |
665 |
> |
{ |
666 |
> |
int i; |
667 |
> |
|
668 |
> |
if (tmMesofact[BMESUPPER-BMESLOWER-1]) |
669 |
> |
return; |
670 |
> |
|
671 |
> |
for (i = BMESLOWER; i < BMESUPPER; i++) |
672 |
> |
tmMesofact[i-BMESLOWER] = 256. * |
673 |
> |
(tmLuminance(i) - LMESLOWER) / |
674 |
> |
(LMESUPPER - LMESLOWER); |
675 |
> |
} |
676 |
> |
|
677 |
> |
|
678 |
> |
int |
679 |
> |
tmNewMap( /* allocate new tone-mapping array */ |
680 |
> |
TMstruct *tms |
681 |
> |
) |
682 |
> |
{ |
683 |
> |
if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != |
684 |
> |
(tms->hbrmax - tms->hbrmin)) { |
685 |
> |
free(tms->lumap); |
686 |
> |
tms->lumap = NULL; |
687 |
> |
} |
688 |
> |
tms->mbrmin = tms->hbrmin; |
689 |
> |
tms->mbrmax = tms->hbrmax; |
690 |
> |
if (tms->mbrmin > tms->mbrmax) |
691 |
> |
return 0; |
692 |
> |
if (tms->lumap == NULL) |
693 |
> |
tms->lumap = (TMAP_TYP *)calloc(tms->mbrmax-tms->mbrmin+1, |
694 |
> |
sizeof(TMAP_TYP)); |
695 |
> |
else |
696 |
> |
memset(tms->lumap, 0, (tms->mbrmax-tms->mbrmin+1)*sizeof(TMAP_TYP)); |
697 |
> |
|
698 |
> |
return(tms->lumap != NULL); |
699 |
> |
} |
700 |
> |
|
701 |
> |
|
702 |
> |
int |
703 |
> |
tmErrorReturn( /* error return (with message) */ |
704 |
> |
const char *func, |
705 |
> |
TMstruct *tms, |
706 |
> |
int err |
707 |
> |
) |
708 |
> |
{ |
709 |
> |
if (tms != NULL) { |
710 |
> |
tms->lastFunc = func; |
711 |
> |
tms->lastError = err; |
712 |
> |
if (tms->flags & TM_F_NOSTDERR) |
713 |
> |
return(err); |
714 |
> |
} |
715 |
> |
fputs(func, stderr); |
716 |
> |
fputs(": ", stderr); |
717 |
> |
fputs(tmErrorMessage[err], stderr); |
718 |
> |
fputs("!\n", stderr); |
719 |
> |
return(err); |
720 |
|
} |