9 |
|
* Externals declared in tonemap.h |
10 |
|
*/ |
11 |
|
|
12 |
< |
/* ==================================================================== |
13 |
< |
* The Radiance Software License, Version 1.0 |
14 |
< |
* |
15 |
< |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
16 |
< |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
17 |
< |
* |
18 |
< |
* Redistribution and use in source and binary forms, with or without |
19 |
< |
* modification, are permitted provided that the following conditions |
20 |
< |
* are met: |
21 |
< |
* |
22 |
< |
* 1. Redistributions of source code must retain the above copyright |
23 |
< |
* notice, this list of conditions and the following disclaimer. |
24 |
< |
* |
25 |
< |
* 2. Redistributions in binary form must reproduce the above copyright |
26 |
< |
* notice, this list of conditions and the following disclaimer in |
27 |
< |
* the documentation and/or other materials provided with the |
28 |
< |
* distribution. |
29 |
< |
* |
30 |
< |
* 3. The end-user documentation included with the redistribution, |
31 |
< |
* if any, must include the following acknowledgment: |
32 |
< |
* "This product includes Radiance software |
33 |
< |
* (http://radsite.lbl.gov/) |
34 |
< |
* developed by the Lawrence Berkeley National Laboratory |
35 |
< |
* (http://www.lbl.gov/)." |
36 |
< |
* Alternately, this acknowledgment may appear in the software itself, |
37 |
< |
* if and wherever such third-party acknowledgments normally appear. |
38 |
< |
* |
39 |
< |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
40 |
< |
* and "The Regents of the University of California" must |
41 |
< |
* not be used to endorse or promote products derived from this |
42 |
< |
* software without prior written permission. For written |
43 |
< |
* permission, please contact [email protected]. |
44 |
< |
* |
45 |
< |
* 5. Products derived from this software may not be called "Radiance", |
46 |
< |
* nor may "Radiance" appear in their name, without prior written |
47 |
< |
* permission of Lawrence Berkeley National Laboratory. |
48 |
< |
* |
49 |
< |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
50 |
< |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
51 |
< |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
52 |
< |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
53 |
< |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
54 |
< |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
55 |
< |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
56 |
< |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
57 |
< |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
58 |
< |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
59 |
< |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
60 |
< |
* SUCH DAMAGE. |
61 |
< |
* ==================================================================== |
62 |
< |
* |
63 |
< |
* This software consists of voluntary contributions made by many |
64 |
< |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
65 |
< |
* information on Lawrence Berkeley National Laboratory, please see |
66 |
< |
* <http://www.lbl.gov/>. |
67 |
< |
*/ |
12 |
> |
#include "copyright.h" |
13 |
|
|
14 |
|
#include <stdio.h> |
15 |
|
#include <math.h> |
18 |
|
|
19 |
|
#define exp10(x) exp(M_LN10*(x)) |
20 |
|
|
76 |
– |
struct tmStruct *tmTop = NULL; /* current tone mapping stack */ |
77 |
– |
|
21 |
|
/* our list of conversion packages */ |
22 |
|
struct tmPackage *tmPkg[TM_MAXPKG]; |
23 |
|
int tmNumPkgs = 0; /* number of registered packages */ |
24 |
|
|
82 |
– |
int tmLastError; /* last error incurred by library */ |
83 |
– |
char *tmLastFunction; /* error-generating function name */ |
25 |
|
|
26 |
< |
|
27 |
< |
struct tmStruct * |
28 |
< |
tmInit(flags, monpri, gamval) /* initialize new tone mapping */ |
29 |
< |
int flags; |
30 |
< |
RGBPRIMP monpri; |
31 |
< |
double gamval; |
26 |
> |
TMstruct * |
27 |
> |
tmInit( /* initialize new tone mapping */ |
28 |
> |
int flags, |
29 |
> |
RGBPRIMP monpri, |
30 |
> |
double gamval |
31 |
> |
) |
32 |
|
{ |
33 |
|
COLORMAT cmat; |
34 |
< |
register struct tmStruct *tmnew; |
35 |
< |
register int i; |
34 |
> |
TMstruct *tmnew; |
35 |
> |
int i; |
36 |
|
/* allocate structure */ |
37 |
< |
tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct)); |
37 |
> |
tmnew = (TMstruct *)malloc(sizeof(TMstruct)); |
38 |
|
if (tmnew == NULL) |
39 |
|
return(NULL); |
40 |
|
|
41 |
|
tmnew->flags = flags & ~TM_F_UNIMPL; |
42 |
+ |
if (tmnew->flags & TM_F_BW) |
43 |
+ |
tmnew->flags &= ~TM_F_MESOPIC; |
44 |
|
/* set monitor transform */ |
45 |
|
if (monpri == NULL || monpri == stdprims || tmnew->flags & TM_F_BW) { |
46 |
|
tmnew->monpri = stdprims; |
75 |
|
/* zero private data */ |
76 |
|
for (i = TM_MAXPKG; i--; ) |
77 |
|
tmnew->pd[i] = NULL; |
78 |
< |
/* make tmnew current */ |
79 |
< |
tmnew->tmprev = tmTop; |
80 |
< |
return(tmTop = tmnew); |
78 |
> |
tmnew->lastError = TM_E_OK; |
79 |
> |
tmnew->lastFunc = "NoErr"; |
80 |
> |
/* return new TMstruct */ |
81 |
> |
return(tmnew); |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
int |
86 |
< |
tmSetSpace(pri, sf) /* set input color space for conversions */ |
87 |
< |
RGBPRIMP pri; |
88 |
< |
double sf; |
86 |
> |
tmSetSpace( /* set input color space for conversions */ |
87 |
> |
TMstruct *tms, |
88 |
> |
RGBPRIMP pri, |
89 |
> |
double sf |
90 |
> |
) |
91 |
|
{ |
92 |
< |
static char funcName[] = "tmSetSpace"; |
93 |
< |
register int i, j; |
92 |
> |
static const char funcName[] = "tmSetSpace"; |
93 |
> |
int i, j; |
94 |
|
/* error check */ |
95 |
< |
if (tmTop == NULL) |
95 |
> |
if (tms == NULL) |
96 |
|
returnErr(TM_E_TMINVAL); |
97 |
|
if (sf <= 1e-12) |
98 |
|
returnErr(TM_E_ILLEGAL); |
99 |
|
/* check if no change */ |
100 |
< |
if (pri == tmTop->inppri && FEQ(sf, tmTop->inpsf)) |
100 |
> |
if (pri == tms->inppri && FEQ(sf, tms->inpsf)) |
101 |
|
returnOK; |
102 |
< |
tmTop->inppri = pri; /* let's set it */ |
103 |
< |
tmTop->inpsf = sf; |
102 |
> |
tms->inppri = pri; /* let's set it */ |
103 |
> |
tms->inpsf = sf; |
104 |
|
|
105 |
< |
if (tmTop->flags & TM_F_BW) { /* color doesn't matter */ |
106 |
< |
tmTop->monpri = tmTop->inppri; /* eliminate xform */ |
107 |
< |
if (tmTop->inppri == TM_XYZPRIM) { |
108 |
< |
tmTop->clf[CIEX] = tmTop->clf[CIEZ] = 0.; |
109 |
< |
tmTop->clf[CIEY] = 1.; |
105 |
> |
if (tms->flags & TM_F_BW) { /* color doesn't matter */ |
106 |
> |
tms->monpri = tms->inppri; /* eliminate xform */ |
107 |
> |
if (tms->inppri == TM_XYZPRIM) { |
108 |
> |
tms->clf[CIEX] = tms->clf[CIEZ] = 0.; |
109 |
> |
tms->clf[CIEY] = 1.; |
110 |
|
} else { |
111 |
< |
comprgb2xyzWBmat(tmTop->cmat, tmTop->monpri); |
112 |
< |
tmTop->clf[RED] = tmTop->cmat[1][0]; |
113 |
< |
tmTop->clf[GRN] = tmTop->cmat[1][1]; |
114 |
< |
tmTop->clf[BLU] = tmTop->cmat[1][2]; |
111 |
> |
comprgb2xyzWBmat(tms->cmat, tms->monpri); |
112 |
> |
tms->clf[RED] = tms->cmat[1][0]; |
113 |
> |
tms->clf[GRN] = tms->cmat[1][1]; |
114 |
> |
tms->clf[BLU] = tms->cmat[1][2]; |
115 |
|
} |
116 |
< |
tmTop->cmat[0][0] = tmTop->cmat[1][1] = tmTop->cmat[2][2] = |
117 |
< |
tmTop->inpsf; |
118 |
< |
tmTop->cmat[0][1] = tmTop->cmat[0][2] = tmTop->cmat[1][0] = |
119 |
< |
tmTop->cmat[1][2] = tmTop->cmat[2][0] = tmTop->cmat[2][1] = 0.; |
116 |
> |
tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] = |
117 |
> |
tms->inpsf; |
118 |
> |
tms->cmat[0][1] = tms->cmat[0][2] = tms->cmat[1][0] = |
119 |
> |
tms->cmat[1][2] = tms->cmat[2][0] = tms->cmat[2][1] = 0.; |
120 |
|
|
121 |
< |
} else if (tmTop->inppri == TM_XYZPRIM) /* input is XYZ */ |
122 |
< |
compxyz2rgbWBmat(tmTop->cmat, tmTop->monpri); |
121 |
> |
} else if (tms->inppri == TM_XYZPRIM) /* input is XYZ */ |
122 |
> |
compxyz2rgbWBmat(tms->cmat, tms->monpri); |
123 |
|
|
124 |
|
else { /* input is RGB */ |
125 |
< |
if (tmTop->inppri != tmTop->monpri && |
126 |
< |
PRIMEQ(tmTop->inppri, tmTop->monpri)) |
127 |
< |
tmTop->inppri = tmTop->monpri; /* no xform */ |
128 |
< |
comprgb2rgbWBmat(tmTop->cmat, tmTop->inppri, tmTop->monpri); |
125 |
> |
if (tms->inppri != tms->monpri && |
126 |
> |
PRIMEQ(tms->inppri, tms->monpri)) |
127 |
> |
tms->inppri = tms->monpri; /* no xform */ |
128 |
> |
comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri); |
129 |
|
} |
130 |
|
for (i = 0; i < 3; i++) |
131 |
|
for (j = 0; j < 3; j++) |
132 |
< |
tmTop->cmat[i][j] *= tmTop->inpsf; |
132 |
> |
tms->cmat[i][j] *= tms->inpsf; |
133 |
|
/* set color divisors */ |
134 |
|
for (i = 0; i < 3; i++) |
135 |
< |
if (tmTop->clf[i] > .001) |
136 |
< |
tmTop->cdiv[i] = |
137 |
< |
256.*pow(tmTop->clf[i], 1./tmTop->mongam); |
135 |
> |
if (tms->clf[i] > .001) |
136 |
> |
tms->cdiv[i] = |
137 |
> |
256.*pow(tms->clf[i], 1./tms->mongam); |
138 |
|
else |
139 |
< |
tmTop->cdiv[i] = 1; |
139 |
> |
tms->cdiv[i] = 1; |
140 |
|
/* notify packages */ |
141 |
|
for (i = tmNumPkgs; i--; ) |
142 |
< |
if (tmTop->pd[i] != NULL && tmPkg[i]->NewSpace != NULL) |
143 |
< |
(*tmPkg[i]->NewSpace)(tmTop); |
142 |
> |
if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL) |
143 |
> |
(*tmPkg[i]->NewSpace)(tms); |
144 |
|
returnOK; |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
void |
149 |
< |
tmClearHisto() /* clear current histogram */ |
149 |
> |
tmClearHisto( /* clear current histogram */ |
150 |
> |
TMstruct *tms |
151 |
> |
) |
152 |
|
{ |
153 |
< |
if (tmTop == NULL || tmTop->histo == NULL) |
153 |
> |
if (tms == NULL || tms->histo == NULL) |
154 |
|
return; |
155 |
< |
free((MEM_PTR)tmTop->histo); |
156 |
< |
tmTop->histo = NULL; |
155 |
> |
free((MEM_PTR)tms->histo); |
156 |
> |
tms->histo = NULL; |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
int |
161 |
< |
tmCvColors(ls, cs, scan, len) /* convert float colors */ |
162 |
< |
TMbright *ls; |
163 |
< |
BYTE *cs; |
164 |
< |
COLOR *scan; |
165 |
< |
int len; |
161 |
> |
tmCvColors( /* convert float colors */ |
162 |
> |
TMstruct *tms, |
163 |
> |
TMbright *ls, |
164 |
> |
BYTE *cs, |
165 |
> |
COLOR *scan, |
166 |
> |
int len |
167 |
> |
) |
168 |
|
{ |
169 |
< |
static char funcName[] = "tmCvColors"; |
170 |
< |
static COLOR csmall = {1e-6, 1e-6, 1e-6}; |
169 |
> |
static const char funcName[] = "tmCvColors"; |
170 |
> |
static COLOR csmall = {.5*MINLUM, .5*MINLUM, .5*MINLUM}; |
171 |
|
COLOR cmon; |
172 |
|
double lum, slum; |
173 |
< |
register double d; |
174 |
< |
register int i; |
173 |
> |
double d; |
174 |
> |
int i; |
175 |
|
|
176 |
< |
if (tmTop == NULL) |
176 |
> |
if (tms == NULL) |
177 |
|
returnErr(TM_E_TMINVAL); |
178 |
< |
if (ls == NULL | scan == NULL | len < 0) |
178 |
> |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
179 |
|
returnErr(TM_E_ILLEGAL); |
180 |
|
for (i = len; i--; ) { |
181 |
< |
if (tmNeedMatrix(tmTop)) /* get monitor RGB */ |
182 |
< |
colortrans(cmon, tmTop->cmat, scan[i]); |
183 |
< |
else { |
184 |
< |
cmon[RED] = tmTop->inpsf*scan[i][RED]; |
185 |
< |
cmon[GRN] = tmTop->inpsf*scan[i][GRN]; |
186 |
< |
cmon[BLU] = tmTop->inpsf*scan[i][BLU]; |
181 |
> |
if (tmNeedMatrix(tms)) { /* get monitor RGB */ |
182 |
> |
colortrans(cmon, tms->cmat, scan[i]); |
183 |
> |
} else { |
184 |
> |
cmon[RED] = tms->inpsf*scan[i][RED]; |
185 |
> |
cmon[GRN] = tms->inpsf*scan[i][GRN]; |
186 |
> |
cmon[BLU] = tms->inpsf*scan[i][BLU]; |
187 |
|
} |
188 |
|
/* world luminance */ |
189 |
< |
lum = tmTop->clf[RED]*cmon[RED] + |
190 |
< |
tmTop->clf[GRN]*cmon[GRN] + |
191 |
< |
tmTop->clf[BLU]*cmon[BLU] ; |
189 |
> |
lum = tms->clf[RED]*cmon[RED] + |
190 |
> |
tms->clf[GRN]*cmon[GRN] + |
191 |
> |
tms->clf[BLU]*cmon[BLU] ; |
192 |
|
/* check range */ |
193 |
|
if (clipgamut(cmon, lum, CGAMUT_LOWER, csmall, cwhite)) |
194 |
< |
lum = tmTop->clf[RED]*cmon[RED] + |
195 |
< |
tmTop->clf[GRN]*cmon[GRN] + |
196 |
< |
tmTop->clf[BLU]*cmon[BLU] ; |
194 |
> |
lum = tms->clf[RED]*cmon[RED] + |
195 |
> |
tms->clf[GRN]*cmon[GRN] + |
196 |
> |
tms->clf[BLU]*cmon[BLU] ; |
197 |
|
if (lum < MINLUM) { |
198 |
|
ls[i] = MINBRT-1; /* bogus value */ |
199 |
|
lum = MINLUM; |
203 |
|
} |
204 |
|
if (cs == TM_NOCHROM) /* no color? */ |
205 |
|
continue; |
206 |
< |
if (tmTop->flags & TM_F_MESOPIC && lum < LMESUPPER) { |
206 |
> |
if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) { |
207 |
|
slum = scotlum(cmon); /* mesopic adj. */ |
208 |
|
if (lum < LMESLOWER) |
209 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = slum; |
210 |
|
else { |
211 |
|
d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER); |
212 |
< |
if (tmTop->flags & TM_F_BW) |
212 |
> |
if (tms->flags & TM_F_BW) |
213 |
|
cmon[RED] = cmon[GRN] = |
214 |
|
cmon[BLU] = d*lum; |
215 |
|
else |
219 |
|
cmon[GRN] += d; |
220 |
|
cmon[BLU] += d; |
221 |
|
} |
222 |
< |
} else if (tmTop->flags & TM_F_BW) { |
222 |
> |
} else if (tms->flags & TM_F_BW) { |
223 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = lum; |
224 |
|
} |
225 |
< |
d = tmTop->clf[RED]*cmon[RED]/lum; |
225 |
> |
d = tms->clf[RED]*cmon[RED]/lum; |
226 |
|
cs[3*i ] = d>=.999 ? 255 : |
227 |
< |
(int)(256.*pow(d, 1./tmTop->mongam)); |
228 |
< |
d = tmTop->clf[GRN]*cmon[GRN]/lum; |
227 |
> |
(int)(256.*pow(d, 1./tms->mongam)); |
228 |
> |
d = tms->clf[GRN]*cmon[GRN]/lum; |
229 |
|
cs[3*i+1] = d>=.999 ? 255 : |
230 |
< |
(int)(256.*pow(d, 1./tmTop->mongam)); |
231 |
< |
d = tmTop->clf[BLU]*cmon[BLU]/lum; |
230 |
> |
(int)(256.*pow(d, 1./tms->mongam)); |
231 |
> |
d = tms->clf[BLU]*cmon[BLU]/lum; |
232 |
|
cs[3*i+2] = d>=.999 ? 255 : |
233 |
< |
(int)(256.*pow(d, 1./tmTop->mongam)); |
233 |
> |
(int)(256.*pow(d, 1./tms->mongam)); |
234 |
|
} |
235 |
|
returnOK; |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
|
int |
240 |
< |
tmCvGrays(ls, scan, len) /* convert float gray values */ |
241 |
< |
TMbright *ls; |
242 |
< |
float *scan; |
243 |
< |
int len; |
240 |
> |
tmCvGrays( /* convert float gray values */ |
241 |
> |
TMstruct *tms, |
242 |
> |
TMbright *ls, |
243 |
> |
float *scan, |
244 |
> |
int len |
245 |
> |
) |
246 |
|
{ |
247 |
< |
static char funcName[] = "tmCvGrays"; |
248 |
< |
register double d; |
249 |
< |
register int i; |
247 |
> |
static const char funcName[] = "tmCvGrays"; |
248 |
> |
double d; |
249 |
> |
int i; |
250 |
|
|
251 |
< |
if (tmTop == NULL) |
251 |
> |
if (tms == NULL) |
252 |
|
returnErr(TM_E_TMINVAL); |
253 |
< |
if (ls == NULL | scan == NULL | len < 0) |
253 |
> |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
254 |
|
returnErr(TM_E_ILLEGAL); |
255 |
|
for (i = len; i--; ) |
256 |
|
if (scan[i] <= TM_NOLUM) { |
264 |
|
|
265 |
|
|
266 |
|
int |
267 |
< |
tmAddHisto(ls, len, wt) /* add values to histogram */ |
268 |
< |
register TMbright *ls; |
269 |
< |
int len; |
270 |
< |
int wt; |
267 |
> |
tmAddHisto( /* add values to histogram */ |
268 |
> |
TMstruct *tms, |
269 |
> |
TMbright *ls, |
270 |
> |
int len, |
271 |
> |
int wt |
272 |
> |
) |
273 |
|
{ |
274 |
< |
static char funcName[] = "tmAddHisto"; |
274 |
> |
static const char funcName[] = "tmAddHisto"; |
275 |
|
int oldorig=0, oldlen, horig, hlen; |
276 |
< |
register int i, j; |
276 |
> |
int i, j; |
277 |
|
|
278 |
< |
if (tmTop == NULL) |
278 |
> |
if (tms == NULL) |
279 |
|
returnErr(TM_E_TMINVAL); |
280 |
|
if (len < 0) |
281 |
|
returnErr(TM_E_ILLEGAL); |
282 |
|
if (len == 0) |
283 |
|
returnOK; |
284 |
|
/* first, grow limits */ |
285 |
< |
if (tmTop->histo == NULL) { |
285 |
> |
if (tms->histo == NULL) { |
286 |
|
for (i = len; i-- && ls[i] < MINBRT; ) |
287 |
|
; |
288 |
|
if (i < 0) |
289 |
|
returnOK; |
290 |
< |
tmTop->hbrmin = tmTop->hbrmax = ls[i]; |
290 |
> |
tms->hbrmin = tms->hbrmax = ls[i]; |
291 |
|
oldlen = 0; |
292 |
|
} else { |
293 |
< |
oldorig = (tmTop->hbrmin-MINBRT)/HISTEP; |
294 |
< |
oldlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - oldorig; |
293 |
> |
oldorig = (tms->hbrmin-MINBRT)/HISTEP; |
294 |
> |
oldlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - oldorig; |
295 |
|
} |
296 |
|
for (i = len; i--; ) { |
297 |
|
if ((j = ls[i]) < MINBRT) |
298 |
|
continue; |
299 |
< |
if (j < tmTop->hbrmin) |
300 |
< |
tmTop->hbrmin = j; |
301 |
< |
else if (j > tmTop->hbrmax) |
302 |
< |
tmTop->hbrmax = j; |
299 |
> |
if (j < tms->hbrmin) |
300 |
> |
tms->hbrmin = j; |
301 |
> |
else if (j > tms->hbrmax) |
302 |
> |
tms->hbrmax = j; |
303 |
|
} |
304 |
< |
horig = (tmTop->hbrmin-MINBRT)/HISTEP; |
305 |
< |
hlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - horig; |
304 |
> |
horig = (tms->hbrmin-MINBRT)/HISTEP; |
305 |
> |
hlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - horig; |
306 |
|
if (hlen > oldlen) { /* (re)allocate histogram */ |
307 |
< |
register int *newhist = (int *)calloc(hlen, sizeof(int)); |
307 |
> |
int *newhist = (int *)calloc(hlen, sizeof(int)); |
308 |
|
if (newhist == NULL) |
309 |
|
returnErr(TM_E_NOMEM); |
310 |
|
if (oldlen) { /* copy and free old */ |
311 |
|
for (i = oldlen, j = i+oldorig-horig; i; ) |
312 |
< |
newhist[--j] = tmTop->histo[--i]; |
313 |
< |
free((MEM_PTR)tmTop->histo); |
312 |
> |
newhist[--j] = tms->histo[--i]; |
313 |
> |
free((MEM_PTR)tms->histo); |
314 |
|
} |
315 |
< |
tmTop->histo = newhist; |
315 |
> |
tms->histo = newhist; |
316 |
|
} |
317 |
|
if (wt == 0) |
318 |
|
returnOK; |
319 |
|
for (i = len; i--; ) /* add in new counts */ |
320 |
|
if (ls[i] >= MINBRT) |
321 |
< |
tmTop->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt; |
321 |
> |
tms->histo[ (ls[i]-MINBRT)/HISTEP - horig ] += wt; |
322 |
|
returnOK; |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
|
static double |
327 |
< |
htcontrs(La) /* human threshold contrast sensitivity, dL(La) */ |
328 |
< |
double La; |
327 |
> |
htcontrs( /* human threshold contrast sensitivity, dL(La) */ |
328 |
> |
double La |
329 |
> |
) |
330 |
|
{ |
331 |
|
double l10La, l10dL; |
332 |
|
/* formula taken from Ferwerda et al. [SG96] */ |
347 |
|
|
348 |
|
|
349 |
|
static int |
350 |
< |
tmNewMap() |
350 |
> |
tmNewMap( /* allocate new tone-mapping array */ |
351 |
> |
TMstruct *tms |
352 |
> |
) |
353 |
|
{ |
354 |
< |
if (tmTop->lumap != NULL && (tmTop->mbrmax - tmTop->mbrmin) != |
355 |
< |
(tmTop->hbrmax - tmTop->hbrmin)) { |
356 |
< |
free((MEM_PTR)tmTop->lumap); |
357 |
< |
tmTop->lumap = NULL; |
354 |
> |
if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != |
355 |
> |
(tms->hbrmax - tms->hbrmin)) { |
356 |
> |
free((MEM_PTR)tms->lumap); |
357 |
> |
tms->lumap = NULL; |
358 |
|
} |
359 |
< |
tmTop->mbrmin = tmTop->hbrmin; |
360 |
< |
tmTop->mbrmax = tmTop->hbrmax; |
361 |
< |
if (tmTop->mbrmin > tmTop->mbrmax) |
359 |
> |
tms->mbrmin = tms->hbrmin; |
360 |
> |
tms->mbrmax = tms->hbrmax; |
361 |
> |
if (tms->mbrmin > tms->mbrmax) |
362 |
|
return 0; |
363 |
< |
if (tmTop->lumap == NULL) |
364 |
< |
tmTop->lumap = (unsigned short *)malloc(sizeof(unsigned short)* |
365 |
< |
(tmTop->mbrmax-tmTop->mbrmin+1)); |
366 |
< |
return(tmTop->lumap != NULL); |
363 |
> |
if (tms->lumap == NULL) |
364 |
> |
tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)* |
365 |
> |
(tms->mbrmax-tms->mbrmin+1)); |
366 |
> |
return(tms->lumap != NULL); |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
|
int |
371 |
< |
tmFixedMapping(expmult, gamval) |
372 |
< |
double expmult; |
373 |
< |
double gamval; |
371 |
> |
tmFixedMapping( /* compute fixed, linear tone-mapping */ |
372 |
> |
TMstruct *tms, |
373 |
> |
double expmult, |
374 |
> |
double gamval |
375 |
> |
) |
376 |
|
{ |
377 |
< |
static char funcName[] = "tmFixedMapping"; |
377 |
> |
static const char funcName[] = "tmFixedMapping"; |
378 |
|
double d; |
379 |
< |
register int i; |
379 |
> |
int i; |
380 |
|
|
381 |
< |
if (!tmNewMap()) |
381 |
> |
if (!tmNewMap(tms)) |
382 |
|
returnErr(TM_E_NOMEM); |
383 |
|
if (expmult <= .0) |
384 |
|
expmult = 1.; |
385 |
|
if (gamval < MINGAM) |
386 |
< |
gamval = tmTop->mongam; |
387 |
< |
d = log(expmult/tmTop->inpsf); |
388 |
< |
for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) |
389 |
< |
tmTop->lumap[i] = 256. * exp( |
390 |
< |
( d + (tmTop->mbrmin+i)*(1./TM_BRTSCALE) ) |
386 |
> |
gamval = tms->mongam; |
387 |
> |
d = log(expmult/tms->inpsf); |
388 |
> |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) |
389 |
> |
tms->lumap[i] = 256. * exp( |
390 |
> |
( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) |
391 |
|
/ gamval ); |
392 |
|
returnOK; |
393 |
|
} |
394 |
|
|
395 |
|
|
396 |
|
int |
397 |
< |
tmComputeMapping(gamval, Lddyn, Ldmax) |
398 |
< |
double gamval; |
399 |
< |
double Lddyn; |
400 |
< |
double Ldmax; |
397 |
> |
tmComputeMapping( /* compute histogram tone-mapping */ |
398 |
> |
TMstruct *tms, |
399 |
> |
double gamval, |
400 |
> |
double Lddyn, |
401 |
> |
double Ldmax |
402 |
> |
) |
403 |
|
{ |
404 |
< |
static char funcName[] = "tmComputeMapping"; |
404 |
> |
static const char funcName[] = "tmComputeMapping"; |
405 |
|
int *histo; |
406 |
|
float *cumf; |
407 |
|
int brt0, histlen, threshold, ceiling, trimmings; |
408 |
|
double logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld; |
409 |
< |
int4 histot; |
409 |
> |
int32 histot; |
410 |
|
double sum; |
411 |
< |
register double d; |
412 |
< |
register int i, j; |
411 |
> |
double d; |
412 |
> |
int i, j; |
413 |
|
|
414 |
< |
if (tmTop == NULL || tmTop->histo == NULL) |
414 |
> |
if (tms == NULL || tms->histo == NULL) |
415 |
|
returnErr(TM_E_TMINVAL); |
416 |
|
/* check arguments */ |
417 |
|
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; |
418 |
|
if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX; |
419 |
< |
if (gamval < MINGAM) gamval = tmTop->mongam; |
419 |
> |
if (gamval < MINGAM) gamval = tms->mongam; |
420 |
|
/* compute handy values */ |
421 |
|
Ldmin = Ldmax/Lddyn; |
422 |
|
logLddyn = log(Lddyn); |
423 |
|
Ldavg = sqrt(Ldmax*Ldmin); |
424 |
< |
i = (tmTop->hbrmin-MINBRT)/HISTEP; |
424 |
> |
i = (tms->hbrmin-MINBRT)/HISTEP; |
425 |
|
brt0 = MINBRT + HISTEP/2 + i*HISTEP; |
426 |
< |
histlen = (tmTop->hbrmax-MINBRT)/HISTEP + 1 - i; |
426 |
> |
histlen = (tms->hbrmax-MINBRT)/HISTEP + 1 - i; |
427 |
|
/* histogram total and mean */ |
428 |
|
histot = 0; sum = 0; |
429 |
|
j = brt0 + histlen*HISTEP; |
430 |
|
for (i = histlen; i--; ) { |
431 |
< |
histot += tmTop->histo[i]; |
432 |
< |
sum += (j -= HISTEP) * tmTop->histo[i]; |
431 |
> |
histot += tms->histo[i]; |
432 |
> |
sum += (j -= HISTEP) * tms->histo[i]; |
433 |
|
} |
434 |
< |
threshold = histot*.025 + .5; |
434 |
> |
threshold = histot*0.005 + .5; |
435 |
|
if (threshold < 4) |
436 |
|
returnErr(TM_E_TMFAIL); |
437 |
|
Lwavg = tmLuminance( (double)sum / histot ); |
438 |
|
/* allocate space for mapping */ |
439 |
< |
if (!tmNewMap()) |
439 |
> |
if (!tmNewMap(tms)) |
440 |
|
returnErr(TM_E_NOMEM); |
441 |
|
/* use linear tone mapping? */ |
442 |
< |
if (tmTop->flags & TM_F_LINEAR) |
442 |
> |
if (tms->flags & TM_F_LINEAR) |
443 |
|
goto linearmap; |
444 |
|
/* clamp histogram */ |
445 |
|
histo = (int *)malloc(histlen*sizeof(int)); |
446 |
|
cumf = (float *)malloc((histlen+2)*sizeof(float)); |
447 |
< |
if (histo == NULL | cumf == NULL) |
447 |
> |
if ((histo == NULL) | (cumf == NULL)) |
448 |
|
returnErr(TM_E_NOMEM); |
449 |
|
cumf[histlen+1] = 1.; /* guard for assignment code */ |
450 |
|
for (i = histlen; i--; ) /* make malleable copy */ |
451 |
< |
histo[i] = tmTop->histo[i]; |
451 |
> |
histo[i] = tms->histo[i]; |
452 |
|
do { /* iterate to solution */ |
453 |
|
sum = 0; /* cumulative probability */ |
454 |
|
for (i = 0; i < histlen; i++) { |
456 |
|
sum += histo[i]; |
457 |
|
} |
458 |
|
cumf[histlen] = 1.; |
459 |
< |
Tr = histot * (double)(tmTop->hbrmax - tmTop->hbrmin) / |
459 |
> |
Tr = histot * (double)(tms->hbrmax - tms->hbrmin) / |
460 |
|
((double)histlen*TM_BRTSCALE) / logLddyn; |
461 |
|
ceiling = Tr + 1.; |
462 |
|
trimmings = 0; /* clip to envelope */ |
463 |
|
for (i = histlen; i--; ) { |
464 |
< |
if (tmTop->flags & TM_F_HCONTR) { |
464 |
> |
if (tms->flags & TM_F_HCONTR) { |
465 |
|
Lw = tmLuminance(brt0 + i*HISTEP); |
466 |
|
Ld = Ldmin * exp( logLddyn * |
467 |
|
.5*(cumf[i]+cumf[i+1]) ); |
481 |
|
} |
482 |
|
} while (trimmings > threshold); |
483 |
|
/* assign tone-mapping */ |
484 |
< |
for (i = tmTop->mbrmax-tmTop->mbrmin+1; i--; ) { |
485 |
< |
j = d = (double)i/(tmTop->mbrmax-tmTop->mbrmin)*histlen; |
484 |
> |
for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { |
485 |
> |
j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen; |
486 |
|
d -= (double)j; |
487 |
|
Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1])); |
488 |
|
d = (Ld - Ldmin)/(Ldmax - Ldmin); |
489 |
< |
tmTop->lumap[i] = 256.*pow(d, 1./gamval); |
489 |
> |
tms->lumap[i] = 256.*pow(d, 1./gamval); |
490 |
|
} |
491 |
|
free((MEM_PTR)histo); /* clean up and return */ |
492 |
|
free((MEM_PTR)cumf); |
493 |
|
returnOK; |
494 |
|
linearmap: /* linear tone-mapping */ |
495 |
< |
if (tmTop->flags & TM_F_HCONTR) |
495 |
> |
if (tms->flags & TM_F_HCONTR) |
496 |
|
d = htcontrs(Ldavg) / htcontrs(Lwavg); |
497 |
|
else |
498 |
|
d = Ldavg / Lwavg; |
499 |
< |
return(tmFixedMapping(tmTop->inpsf*d/Ldmax, gamval)); |
499 |
> |
return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval)); |
500 |
|
} |
501 |
|
|
502 |
|
|
503 |
|
int |
504 |
< |
tmMapPixels(ps, ls, cs, len) |
505 |
< |
register BYTE *ps; |
506 |
< |
TMbright *ls; |
507 |
< |
register BYTE *cs; |
508 |
< |
int len; |
504 |
> |
tmMapPixels( /* apply tone-mapping to pixel(s) */ |
505 |
> |
TMstruct *tms, |
506 |
> |
BYTE *ps, |
507 |
> |
TMbright *ls, |
508 |
> |
BYTE *cs, |
509 |
> |
int len |
510 |
> |
) |
511 |
|
{ |
512 |
< |
static char funcName[] = "tmMapPixels"; |
513 |
< |
register int4 li, pv; |
512 |
> |
static const char funcName[] = "tmMapPixels"; |
513 |
> |
int32 li, pv; |
514 |
|
|
515 |
< |
if (tmTop == NULL || tmTop->lumap == NULL) |
515 |
> |
if (tms == NULL || tms->lumap == NULL) |
516 |
|
returnErr(TM_E_TMINVAL); |
517 |
< |
if (ps == NULL | ls == NULL | len < 0) |
517 |
> |
if ((ps == NULL) | (ls == NULL) | (len < 0)) |
518 |
|
returnErr(TM_E_ILLEGAL); |
519 |
|
while (len--) { |
520 |
< |
if ((li = *ls++) < tmTop->mbrmin) { |
520 |
> |
if ((li = *ls++) < tms->mbrmin) { |
521 |
|
li = 0; |
522 |
|
} else { |
523 |
< |
if (li > tmTop->mbrmax) |
524 |
< |
li = tmTop->mbrmax; |
525 |
< |
li = tmTop->lumap[li - tmTop->mbrmin]; |
523 |
> |
if (li > tms->mbrmax) |
524 |
> |
li = tms->mbrmax; |
525 |
> |
li = tms->lumap[li - tms->mbrmin]; |
526 |
|
} |
527 |
|
if (cs == TM_NOCHROM) |
528 |
|
*ps++ = li>255 ? 255 : li; |
529 |
|
else { |
530 |
< |
pv = *cs++ * li / tmTop->cdiv[RED]; |
530 |
> |
pv = *cs++ * li / tms->cdiv[RED]; |
531 |
|
*ps++ = pv>255 ? 255 : pv; |
532 |
< |
pv = *cs++ * li / tmTop->cdiv[GRN]; |
532 |
> |
pv = *cs++ * li / tms->cdiv[GRN]; |
533 |
|
*ps++ = pv>255 ? 255 : pv; |
534 |
< |
pv = *cs++ * li / tmTop->cdiv[BLU]; |
534 |
> |
pv = *cs++ * li / tms->cdiv[BLU]; |
535 |
|
*ps++ = pv>255 ? 255 : pv; |
536 |
|
} |
537 |
|
} |
539 |
|
} |
540 |
|
|
541 |
|
|
579 |
– |
struct tmStruct * |
580 |
– |
tmPop() /* pop top tone mapping off stack */ |
581 |
– |
{ |
582 |
– |
register struct tmStruct *tms; |
542 |
|
|
584 |
– |
if ((tms = tmTop) != NULL) |
585 |
– |
tmTop = tms->tmprev; |
586 |
– |
return(tms); |
587 |
– |
} |
543 |
|
|
544 |
< |
|
545 |
< |
int |
546 |
< |
tmPull(tms) /* pull a tone mapping from stack */ |
547 |
< |
register struct tmStruct *tms; |
544 |
> |
TMstruct * |
545 |
> |
tmDup( /* duplicate top tone mapping */ |
546 |
> |
TMstruct *tms |
547 |
> |
) |
548 |
|
{ |
594 |
– |
register struct tmStruct *tms2; |
595 |
– |
/* special cases first */ |
596 |
– |
if (tms == NULL | tmTop == NULL) |
597 |
– |
return(0); |
598 |
– |
if (tms == tmTop) { |
599 |
– |
tmTop = tms->tmprev; |
600 |
– |
tms->tmprev = NULL; |
601 |
– |
return(1); |
602 |
– |
} |
603 |
– |
for (tms2 = tmTop; tms2->tmprev != NULL; tms2 = tms2->tmprev) |
604 |
– |
if (tms == tms2->tmprev) { /* remove it */ |
605 |
– |
tms2->tmprev = tms->tmprev; |
606 |
– |
tms->tmprev = NULL; |
607 |
– |
return(1); |
608 |
– |
} |
609 |
– |
return(0); /* not found on stack */ |
610 |
– |
} |
611 |
– |
|
612 |
– |
|
613 |
– |
struct tmStruct * |
614 |
– |
tmDup() /* duplicate top tone mapping */ |
615 |
– |
{ |
549 |
|
int len; |
550 |
< |
register int i; |
551 |
< |
register struct tmStruct *tmnew; |
550 |
> |
int i; |
551 |
> |
TMstruct *tmnew; |
552 |
|
|
553 |
< |
if (tmTop == NULL) /* anything to duplicate? */ |
553 |
> |
if (tms == NULL) /* anything to duplicate? */ |
554 |
|
return(NULL); |
555 |
< |
tmnew = (struct tmStruct *)malloc(sizeof(struct tmStruct)); |
555 |
> |
tmnew = (TMstruct *)malloc(sizeof(TMstruct)); |
556 |
|
if (tmnew == NULL) |
557 |
|
return(NULL); |
558 |
< |
*tmnew = *tmTop; /* copy everything */ |
558 |
> |
*tmnew = *tms; /* copy everything */ |
559 |
|
if (tmnew->histo != NULL) { /* duplicate histogram */ |
560 |
|
len = (tmnew->hbrmax-MINBRT)/HISTEP + 1 - |
561 |
|
(tmnew->hbrmin-MINBRT)/HISTEP; |
562 |
|
tmnew->histo = (int *)malloc(len*sizeof(int)); |
563 |
|
if (tmnew->histo != NULL) |
564 |
|
for (i = len; i--; ) |
565 |
< |
tmnew->histo[i] = tmTop->histo[i]; |
565 |
> |
tmnew->histo[i] = tms->histo[i]; |
566 |
|
} |
567 |
|
if (tmnew->lumap != NULL) { /* duplicate luminance mapping */ |
568 |
|
len = tmnew->mbrmax-tmnew->mbrmin+1; |
570 |
|
len*sizeof(unsigned short) ); |
571 |
|
if (tmnew->lumap != NULL) |
572 |
|
for (i = len; i--; ) |
573 |
< |
tmnew->lumap[i] = tmTop->lumap[i]; |
573 |
> |
tmnew->lumap[i] = tms->lumap[i]; |
574 |
|
} |
575 |
|
/* clear package data */ |
576 |
|
for (i = tmNumPkgs; i--; ) |
577 |
|
tmnew->pd[i] = NULL; |
578 |
< |
tmnew->tmprev = tmTop; /* make copy current */ |
579 |
< |
return(tmTop = tmnew); |
578 |
> |
/* return copy */ |
579 |
> |
return(tmnew); |
580 |
|
} |
581 |
|
|
582 |
|
|
650 |
– |
int |
651 |
– |
tmPush(tms) /* push tone mapping on top of stack */ |
652 |
– |
register struct tmStruct *tms; |
653 |
– |
{ |
654 |
– |
static char funcName[] = "tmPush"; |
655 |
– |
/* check validity */ |
656 |
– |
if (tms == NULL) |
657 |
– |
returnErr(TM_E_ILLEGAL); |
658 |
– |
if (tms == tmTop) /* check necessity */ |
659 |
– |
returnOK; |
660 |
– |
/* pull if already in stack */ |
661 |
– |
(void)tmPull(tms); |
662 |
– |
/* push it on top */ |
663 |
– |
tms->tmprev = tmTop; |
664 |
– |
tmTop = tms; |
665 |
– |
returnOK; |
666 |
– |
} |
667 |
– |
|
668 |
– |
|
583 |
|
void |
584 |
|
tmDone(tms) /* done with tone mapping -- destroy it */ |
585 |
< |
register struct tmStruct *tms; |
585 |
> |
TMstruct *tms; |
586 |
|
{ |
587 |
< |
register int i; |
588 |
< |
/* NULL arg. is equiv. to tmTop */ |
589 |
< |
if (tms == NULL && (tms = tmTop) == NULL) |
587 |
> |
int i; |
588 |
> |
/* NULL arg. is equiv. to tms */ |
589 |
> |
if (tms == NULL) |
590 |
|
return; |
677 |
– |
/* take out of stack if present */ |
678 |
– |
(void)tmPull(tms); |
591 |
|
/* free tables */ |
592 |
|
if (tms->histo != NULL) |
593 |
|
free((MEM_PTR)tms->histo); |
607 |
|
void |
608 |
|
tmMkMesofact() /* build mesopic lookup factor table */ |
609 |
|
{ |
610 |
< |
register int i; |
610 |
> |
int i; |
611 |
|
|
612 |
|
if (tmMesofact[BMESUPPER-BMESLOWER-1]) |
613 |
|
return; |
620 |
|
|
621 |
|
|
622 |
|
int |
623 |
< |
tmErrorReturn(func, err) /* error return (with message) */ |
624 |
< |
char *func; |
625 |
< |
int err; |
623 |
> |
tmErrorReturn( /* error return (with message) */ |
624 |
> |
const char *func, |
625 |
> |
TMstruct *tms, |
626 |
> |
int err |
627 |
> |
) |
628 |
|
{ |
629 |
< |
tmLastFunction = func; |
630 |
< |
tmLastError = err; |
631 |
< |
if (tmTop != NULL && tmTop->flags & TM_F_NOSTDERR) |
632 |
< |
return(err); |
629 |
> |
if (tms != NULL) { |
630 |
> |
tms->lastFunc = func; |
631 |
> |
tms->lastError = err; |
632 |
> |
if (tms->flags & TM_F_NOSTDERR) |
633 |
> |
return(err); |
634 |
> |
} |
635 |
|
fputs(func, stderr); |
636 |
|
fputs(": ", stderr); |
637 |
|
fputs(tmErrorMessage[err], stderr); |