1 |
greg |
3.1 |
#ifndef lint |
2 |
greg |
3.37 |
static const char RCSid[] = "$Id: tmapcolrs.c,v 3.36 2022/01/07 23:01:01 greg Exp $"; |
3 |
greg |
3.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Routines for tone mapping on Radiance RGBE and XYZE pictures. |
6 |
greg |
3.10 |
* |
7 |
|
|
* Externals declared in tonemap.h |
8 |
|
|
*/ |
9 |
|
|
|
10 |
greg |
3.11 |
#include "copyright.h" |
11 |
greg |
3.1 |
|
12 |
greg |
3.26 |
#include <stdlib.h> |
13 |
greg |
3.1 |
#include <math.h> |
14 |
schorsch |
3.16 |
|
15 |
greg |
3.21 |
#ifdef PCOND |
16 |
greg |
3.31 |
#include "paths.h" |
17 |
greg |
3.10 |
#endif |
18 |
greg |
3.27 |
#include "tmprivat.h" |
19 |
greg |
3.33 |
#include "rtio.h" |
20 |
greg |
3.27 |
#include "resolu.h" |
21 |
greg |
3.1 |
|
22 |
greg |
3.28 |
#define GAMTSZ 4096 |
23 |
greg |
3.3 |
|
24 |
|
|
typedef struct { |
25 |
greg |
3.29 |
uby8 gamb[GAMTSZ]; /* gamma lookup table */ |
26 |
greg |
3.21 |
int clfb[3]; /* encoded tm->clf */ |
27 |
|
|
int32 cmatb[3][3]; /* encoded color transform */ |
28 |
greg |
3.3 |
TMbright inpsfb; /* encoded tm->inpsf */ |
29 |
|
|
} COLRDATA; |
30 |
|
|
|
31 |
greg |
3.37 |
static void * colrInit(TMstruct *); |
32 |
greg |
3.17 |
static void colrNewSpace(TMstruct *); |
33 |
schorsch |
3.15 |
static gethfunc headline; |
34 |
|
|
|
35 |
greg |
3.3 |
static struct tmPackage colrPkg = { /* our package functions */ |
36 |
|
|
colrInit, colrNewSpace, free |
37 |
|
|
}; |
38 |
|
|
static int colrReg = -1; /* our package registration number */ |
39 |
|
|
|
40 |
greg |
3.28 |
static TMbright logi[256]; |
41 |
greg |
3.1 |
|
42 |
|
|
|
43 |
|
|
int |
44 |
greg |
3.17 |
tmCvColrs( /* convert RGBE/XYZE colors */ |
45 |
|
|
TMstruct *tms, |
46 |
|
|
TMbright *ls, |
47 |
greg |
3.29 |
uby8 *cs, |
48 |
greg |
3.17 |
COLR *scan, |
49 |
|
|
int len |
50 |
|
|
) |
51 |
greg |
3.1 |
{ |
52 |
greg |
3.18 |
static const char funcName[] = "tmCvColrs"; |
53 |
greg |
3.21 |
int cmon[4]; |
54 |
greg |
3.28 |
COLRDATA *cd; |
55 |
|
|
int i, j, bi; |
56 |
|
|
int32 li, vl; |
57 |
greg |
3.1 |
|
58 |
greg |
3.17 |
if (tms == NULL) |
59 |
greg |
3.1 |
returnErr(TM_E_TMINVAL); |
60 |
schorsch |
3.14 |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
61 |
greg |
3.1 |
returnErr(TM_E_ILLEGAL); |
62 |
gwlarson |
3.6 |
if (colrReg < 0) { /* build tables if necessary */ |
63 |
greg |
3.3 |
colrReg = tmRegPkg(&colrPkg); |
64 |
|
|
if (colrReg < 0) |
65 |
|
|
returnErr(TM_E_CODERR1); |
66 |
greg |
3.1 |
for (i = 256; i--; ) |
67 |
|
|
logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5; |
68 |
greg |
3.10 |
tmMkMesofact(); |
69 |
greg |
3.1 |
} |
70 |
greg |
3.17 |
if ((cd = (COLRDATA *)tmPkgData(tms,colrReg)) == NULL) |
71 |
greg |
3.3 |
returnErr(TM_E_NOMEM); |
72 |
greg |
3.1 |
for (i = len; i--; ) { |
73 |
greg |
3.21 |
if (tmNeedMatrix(tms)) { /* apply color xform */ |
74 |
greg |
3.35 |
bi = 0; |
75 |
greg |
3.21 |
for (j = 3; j--; ) { |
76 |
|
|
vl = cd->cmatb[j][RED]*(int32)scan[i][RED] + |
77 |
|
|
cd->cmatb[j][GRN]*(int32)scan[i][GRN] + |
78 |
|
|
cd->cmatb[j][BLU]*(int32)scan[i][BLU] ; |
79 |
greg |
3.35 |
if (vl < 0) |
80 |
|
|
cmon[j] = vl/(int32)0x10000; |
81 |
|
|
else if ((cmon[j] = vl>>16) > bi) |
82 |
|
|
bi = cmon[j]; |
83 |
greg |
3.21 |
} |
84 |
|
|
cmon[EXP] = scan[i][EXP]; |
85 |
greg |
3.35 |
while (bi >= 256) { /* handle overflow */ |
86 |
|
|
cmon[EXP]++; |
87 |
|
|
for (j = 3; j--; ) cmon[j] >>= 1; |
88 |
|
|
bi >>= 1; |
89 |
|
|
} |
90 |
greg |
3.21 |
} else |
91 |
|
|
copycolr(cmon, scan[i]); |
92 |
greg |
3.1 |
/* world luminance */ |
93 |
greg |
3.28 |
li = cd->clfb[RED]*(int32)cmon[RED] + |
94 |
|
|
cd->clfb[GRN]*(int32)cmon[GRN] + |
95 |
|
|
cd->clfb[BLU]*(int32)cmon[BLU] ; |
96 |
|
|
if (li >= 1L<<(12+8)) li = 255; |
97 |
greg |
3.24 |
else li >>= 12; |
98 |
greg |
3.22 |
bi = BRT2SCALE(cmon[EXP]-COLXS) + cd->inpsfb; |
99 |
|
|
if (li > 0) |
100 |
|
|
bi += logi[li]; |
101 |
|
|
else { |
102 |
|
|
bi += logi[0]; |
103 |
|
|
li = 1; /* avoid /0 */ |
104 |
greg |
3.1 |
} |
105 |
|
|
ls[i] = bi; |
106 |
|
|
if (cs == TM_NOCHROM) /* no color? */ |
107 |
|
|
continue; |
108 |
|
|
/* mesopic adj. */ |
109 |
greg |
3.17 |
if (tms->flags & TM_F_MESOPIC && bi < BMESUPPER) { |
110 |
greg |
3.21 |
int pf, sli = normscot(cmon); |
111 |
|
|
if (bi < BMESLOWER) { |
112 |
greg |
3.1 |
cmon[RED] = cmon[GRN] = cmon[BLU] = sli; |
113 |
greg |
3.21 |
} else { |
114 |
greg |
3.17 |
if (tms->flags & TM_F_BW) |
115 |
gwlarson |
3.8 |
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
116 |
greg |
3.10 |
pf = tmMesofact[bi-BMESLOWER]; |
117 |
greg |
3.1 |
sli *= 256 - pf; |
118 |
greg |
3.21 |
for (j = 3; j--; ) { |
119 |
|
|
cmon[j] = sli + pf*cmon[j]; |
120 |
|
|
if (cmon[j] <= 0) cmon[j] = 0; |
121 |
|
|
else cmon[j] >>= 8; |
122 |
|
|
} |
123 |
greg |
3.1 |
} |
124 |
greg |
3.17 |
} else if (tms->flags & TM_F_BW) { |
125 |
greg |
3.36 |
for (j = 3; j--; ) |
126 |
|
|
cs[3*i+j] = tms->cdiv[j]/(TM_BRES>>8); |
127 |
|
|
continue; |
128 |
greg |
3.21 |
} else { |
129 |
|
|
for (j = 3; j--; ) |
130 |
greg |
3.35 |
cmon[j] *= (cmon[j] > 0); |
131 |
greg |
3.1 |
} |
132 |
greg |
3.28 |
bi = ( (uint32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 12; |
133 |
greg |
3.3 |
cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
134 |
greg |
3.28 |
bi = ( (uint32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 12; |
135 |
greg |
3.3 |
cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
136 |
greg |
3.28 |
bi = ( (uint32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 12; |
137 |
greg |
3.3 |
cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
138 |
greg |
3.1 |
} |
139 |
|
|
returnOK; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
|
143 |
|
|
#define FMTRGB 1 /* Input is RGBE */ |
144 |
|
|
#define FMTCIE 2 /* Input is CIE XYZE */ |
145 |
|
|
#define FMTUNK 3 /* Input format is unspecified */ |
146 |
|
|
#define FMTBAD 4 /* Input is not a recognized format */ |
147 |
|
|
|
148 |
|
|
static struct radhead { |
149 |
|
|
int format; /* FMTRGB, FMTCIE, FMTUNK, FMTBAD */ |
150 |
|
|
double expos; /* input exposure value */ |
151 |
|
|
RGBPRIMP primp; /* input primaries */ |
152 |
|
|
RGBPRIMS mypri; /* custom primaries */ |
153 |
|
|
} rhdefault = {FMTUNK, 1., stdprims, STDPRIMS}; |
154 |
|
|
|
155 |
|
|
|
156 |
|
|
static int |
157 |
schorsch |
3.15 |
headline( /* grok a header line */ |
158 |
greg |
3.28 |
char *s, |
159 |
schorsch |
3.15 |
void *vrh |
160 |
|
|
) |
161 |
greg |
3.1 |
{ |
162 |
greg |
3.32 |
char fmt[MAXFMTLEN]; |
163 |
greg |
3.28 |
struct radhead *rh = vrh; |
164 |
greg |
3.1 |
|
165 |
|
|
if (formatval(fmt, s)) { |
166 |
|
|
if (!strcmp(fmt, COLRFMT)) |
167 |
|
|
rh->format = FMTRGB; |
168 |
|
|
else if (!strcmp(fmt, CIEFMT)) |
169 |
|
|
rh->format = FMTCIE; |
170 |
|
|
else |
171 |
|
|
rh->format = FMTBAD; |
172 |
gwlarson |
3.7 |
return(0); |
173 |
greg |
3.1 |
} |
174 |
|
|
if (isexpos(s)) { |
175 |
|
|
rh->expos *= exposval(s); |
176 |
gwlarson |
3.7 |
return(0); |
177 |
greg |
3.1 |
} |
178 |
|
|
if (isprims(s)) { |
179 |
|
|
primsval(rh->mypri, s); |
180 |
|
|
rh->primp = rh->mypri; |
181 |
gwlarson |
3.7 |
return(0); |
182 |
greg |
3.1 |
} |
183 |
gwlarson |
3.7 |
return(0); |
184 |
greg |
3.1 |
} |
185 |
|
|
|
186 |
|
|
|
187 |
|
|
int |
188 |
greg |
3.17 |
tmLoadPicture( /* convert Radiance picture */ |
189 |
|
|
TMstruct *tms, |
190 |
|
|
TMbright **lpp, |
191 |
greg |
3.29 |
uby8 **cpp, |
192 |
greg |
3.17 |
int *xp, |
193 |
|
|
int *yp, |
194 |
|
|
char *fname, |
195 |
|
|
FILE *fp |
196 |
|
|
) |
197 |
greg |
3.1 |
{ |
198 |
|
|
char *funcName = fname==NULL ? "tmLoadPicture" : fname; |
199 |
|
|
FILE *inpf; |
200 |
|
|
struct radhead info; |
201 |
|
|
int err; |
202 |
|
|
COLR *scanin = NULL; |
203 |
|
|
int i; |
204 |
|
|
/* check arguments */ |
205 |
greg |
3.17 |
if (tms == NULL) |
206 |
greg |
3.1 |
returnErr(TM_E_TMINVAL); |
207 |
schorsch |
3.14 |
if ((lpp == NULL) | (xp == NULL) | (yp == NULL) | |
208 |
|
|
((fname == NULL) & (fp == TM_GETFILE))) |
209 |
greg |
3.1 |
returnErr(TM_E_ILLEGAL); |
210 |
|
|
*xp = *yp = 0; /* error precaution */ |
211 |
greg |
3.20 |
if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "rb")) == NULL) |
212 |
greg |
3.1 |
returnErr(TM_E_BADFILE); |
213 |
gwlarson |
3.6 |
*lpp = NULL; |
214 |
|
|
if (cpp != TM_NOCHROMP) *cpp = NULL; |
215 |
greg |
3.1 |
info = rhdefault; /* get our header */ |
216 |
schorsch |
3.15 |
getheader(inpf, headline, &info); |
217 |
schorsch |
3.14 |
if ((info.format == FMTBAD) | (info.expos <= 0.) || |
218 |
greg |
3.1 |
fgetresolu(xp, yp, inpf) < 0) { |
219 |
|
|
err = TM_E_BADFILE; goto done; |
220 |
|
|
} |
221 |
|
|
if (info.format == FMTUNK) /* assume RGBE format */ |
222 |
|
|
info.format = FMTRGB; |
223 |
|
|
if (info.format == FMTRGB) |
224 |
|
|
info.expos /= WHTEFFICACY; |
225 |
|
|
else if (info.format == FMTCIE) |
226 |
|
|
info.primp = TM_XYZPRIM; |
227 |
|
|
/* prepare library */ |
228 |
greg |
3.19 |
if ((err = tmSetSpace(tms, info.primp, 1./info.expos, NULL)) != TM_E_OK) |
229 |
greg |
3.1 |
goto done; |
230 |
|
|
err = TM_E_NOMEM; /* allocate arrays */ |
231 |
|
|
*lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp); |
232 |
|
|
if (*lpp == NULL) |
233 |
|
|
goto done; |
234 |
|
|
if (cpp != TM_NOCHROMP) { |
235 |
greg |
3.29 |
*cpp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp); |
236 |
greg |
3.1 |
if (*cpp == NULL) |
237 |
|
|
goto done; |
238 |
|
|
} |
239 |
|
|
scanin = (COLR *)malloc(sizeof(COLR) * *xp); |
240 |
|
|
if (scanin == NULL) |
241 |
|
|
goto done; |
242 |
|
|
err = TM_E_BADFILE; /* read & convert scanlines */ |
243 |
|
|
for (i = 0; i < *yp; i++) { |
244 |
|
|
if (freadcolrs(scanin, *xp, inpf) < 0) { |
245 |
|
|
err = TM_E_BADFILE; break; |
246 |
|
|
} |
247 |
greg |
3.17 |
err = tmCvColrs(tms, *lpp + (i * *xp), |
248 |
greg |
3.1 |
cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp + (i * 3 * *xp), |
249 |
|
|
scanin, *xp); |
250 |
|
|
if (err != TM_E_OK) |
251 |
|
|
break; |
252 |
|
|
} |
253 |
|
|
done: /* clean up */ |
254 |
|
|
if (fp == NULL) |
255 |
|
|
fclose(inpf); |
256 |
|
|
if (scanin != NULL) |
257 |
greg |
3.37 |
free(scanin); |
258 |
gwlarson |
3.6 |
if (err != TM_E_OK) { |
259 |
|
|
if (*lpp != NULL) |
260 |
greg |
3.37 |
free(*lpp); |
261 |
gwlarson |
3.6 |
if (cpp != TM_NOCHROMP && *cpp != NULL) |
262 |
greg |
3.37 |
free(*cpp); |
263 |
greg |
3.1 |
returnErr(err); |
264 |
gwlarson |
3.6 |
} |
265 |
greg |
3.1 |
returnOK; |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
|
269 |
gwlarson |
3.9 |
#ifdef PCOND |
270 |
schorsch |
3.13 |
static int /* run pcond to map picture */ |
271 |
greg |
3.1 |
dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname) |
272 |
greg |
3.29 |
uby8 **psp; |
273 |
greg |
3.1 |
int *xp, *yp; |
274 |
|
|
int flags; |
275 |
|
|
RGBPRIMP monpri; |
276 |
|
|
double gamval, Lddyn, Ldmax; |
277 |
|
|
char *fname; |
278 |
|
|
{ |
279 |
|
|
char *funcName = fname; |
280 |
greg |
3.17 |
TMstruct *tms = NULL; |
281 |
greg |
3.10 |
char cmdbuf[1024]; |
282 |
greg |
3.1 |
FILE *infp; |
283 |
greg |
3.28 |
COLR *scan; |
284 |
greg |
3.29 |
uby8 *rp; |
285 |
greg |
3.1 |
int y; |
286 |
greg |
3.28 |
int x; |
287 |
greg |
3.1 |
/* set up gamma correction */ |
288 |
|
|
if (setcolrcor(pow, 1./gamval) < 0) |
289 |
|
|
returnErr(TM_E_NOMEM); |
290 |
|
|
/* create command */ |
291 |
gwlarson |
3.9 |
strcpy(cmdbuf, PCOND); |
292 |
greg |
3.1 |
if (flags & TM_F_HCONTR) |
293 |
gwlarson |
3.9 |
strcat(cmdbuf, " -s"); |
294 |
greg |
3.1 |
if (flags & TM_F_MESOPIC) |
295 |
gwlarson |
3.9 |
strcat(cmdbuf, " -c"); |
296 |
greg |
3.1 |
if (flags & TM_F_LINEAR) |
297 |
gwlarson |
3.9 |
strcat(cmdbuf, " -l"); |
298 |
greg |
3.1 |
if (flags & TM_F_ACUITY) |
299 |
gwlarson |
3.9 |
strcat(cmdbuf, " -a"); |
300 |
greg |
3.1 |
if (flags & TM_F_VEIL) |
301 |
gwlarson |
3.9 |
strcat(cmdbuf, " -v"); |
302 |
greg |
3.1 |
if (flags & TM_F_CWEIGHT) |
303 |
gwlarson |
3.9 |
strcat(cmdbuf, " -w"); |
304 |
|
|
if (monpri != stdprims) |
305 |
|
|
sprintf(cmdbuf+strlen(cmdbuf), " -p %f %f %f %f %f %f %f %f", |
306 |
|
|
monpri[RED][CIEX], monpri[RED][CIEY], |
307 |
|
|
monpri[GRN][CIEX], monpri[GRN][CIEY], |
308 |
|
|
monpri[BLU][CIEX], monpri[BLU][CIEY], |
309 |
|
|
monpri[WHT][CIEX], monpri[WHT][CIEY]); |
310 |
|
|
sprintf(cmdbuf+strlen(cmdbuf), " -d %f -u %f %s", Lddyn, Ldmax, fname); |
311 |
greg |
3.1 |
/* start pcond */ |
312 |
|
|
if ((infp = popen(cmdbuf, "r")) == NULL) |
313 |
|
|
returnErr(TM_E_BADFILE); |
314 |
|
|
/* check picture format and size */ |
315 |
|
|
if (checkheader(infp, COLRFMT, NULL) < 0 || |
316 |
|
|
fgetresolu(xp, yp, infp) < 0) { |
317 |
|
|
pclose(infp); |
318 |
|
|
returnErr(TM_E_BADFILE); |
319 |
|
|
} |
320 |
|
|
/* allocate arrays */ |
321 |
greg |
3.2 |
scan = (COLR *)malloc(sizeof(COLR) * *xp); |
322 |
greg |
3.1 |
if (flags & TM_F_BW) |
323 |
greg |
3.29 |
rp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp); |
324 |
greg |
3.1 |
else |
325 |
greg |
3.29 |
rp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp); |
326 |
schorsch |
3.14 |
if (((*psp = rp) == NULL) | (scan == NULL)) { |
327 |
greg |
3.1 |
pclose(infp); |
328 |
|
|
returnErr(TM_E_NOMEM); |
329 |
|
|
} |
330 |
|
|
/* read and gamma map file */ |
331 |
|
|
for (y = 0; y < *yp; y++) { |
332 |
|
|
if (freadcolrs(scan, *xp, infp) < 0) { |
333 |
|
|
pclose(infp); |
334 |
greg |
3.37 |
free(scan); |
335 |
|
|
free(*psp); |
336 |
greg |
3.2 |
*psp = NULL; |
337 |
greg |
3.1 |
returnErr(TM_E_BADFILE); |
338 |
|
|
} |
339 |
|
|
colrs_gambs(scan, *xp); |
340 |
|
|
if (flags & TM_F_BW) |
341 |
|
|
for (x = 0; x < *xp; x++) |
342 |
|
|
*rp++ = normbright(scan[x]); |
343 |
|
|
else |
344 |
|
|
for (x = 0; x < *xp; x++) { |
345 |
|
|
*rp++ = scan[x][RED]; |
346 |
|
|
*rp++ = scan[x][GRN]; |
347 |
|
|
*rp++ = scan[x][BLU]; |
348 |
|
|
} |
349 |
|
|
} |
350 |
greg |
3.37 |
free(scan); |
351 |
greg |
3.1 |
pclose(infp); |
352 |
|
|
returnOK; |
353 |
|
|
} |
354 |
gwlarson |
3.9 |
#endif |
355 |
greg |
3.1 |
|
356 |
|
|
|
357 |
|
|
int /* map a Radiance picture */ |
358 |
|
|
tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp) |
359 |
greg |
3.29 |
uby8 **psp; |
360 |
greg |
3.1 |
int *xp, *yp; |
361 |
|
|
int flags; |
362 |
|
|
RGBPRIMP monpri; |
363 |
|
|
double gamval, Lddyn, Ldmax; |
364 |
|
|
char *fname; |
365 |
|
|
FILE *fp; |
366 |
|
|
{ |
367 |
|
|
char *funcName = fname==NULL ? "tmMapPicture" : fname; |
368 |
greg |
3.30 |
TMstruct *tms = NULL; |
369 |
greg |
3.29 |
uby8 *cp; |
370 |
greg |
3.1 |
TMbright *lp; |
371 |
|
|
int err; |
372 |
|
|
/* check arguments */ |
373 |
schorsch |
3.14 |
if ((psp == NULL) | (xp == NULL) | (yp == NULL) | (monpri == NULL) | |
374 |
|
|
((fname == NULL) & (fp == TM_GETFILE))) |
375 |
greg |
3.1 |
returnErr(TM_E_ILLEGAL); |
376 |
|
|
/* set defaults */ |
377 |
|
|
if (gamval < MINGAM) gamval = DEFGAM; |
378 |
|
|
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; |
379 |
|
|
if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX; |
380 |
|
|
if (flags & TM_F_BW) monpri = stdprims; |
381 |
gwlarson |
3.9 |
#ifdef PCOND |
382 |
greg |
3.1 |
/* check for pcond run */ |
383 |
|
|
if (fp == TM_GETFILE && flags & TM_F_UNIMPL) |
384 |
|
|
return( dopcond(psp, xp, yp, flags, |
385 |
|
|
monpri, gamval, Lddyn, Ldmax, fname) ); |
386 |
gwlarson |
3.9 |
#endif |
387 |
greg |
3.1 |
/* initialize tone mapping */ |
388 |
greg |
3.17 |
if ((tms = tmInit(flags, monpri, gamval)) == NULL) |
389 |
greg |
3.1 |
returnErr(TM_E_NOMEM); |
390 |
|
|
/* load & convert picture */ |
391 |
greg |
3.17 |
err = tmLoadPicture(tms, &lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp, |
392 |
greg |
3.1 |
xp, yp, fname, fp); |
393 |
|
|
if (err != TM_E_OK) { |
394 |
greg |
3.17 |
tmDone(tms); |
395 |
greg |
3.1 |
return(err); |
396 |
|
|
} |
397 |
|
|
/* allocate space for result */ |
398 |
|
|
if (flags & TM_F_BW) { |
399 |
greg |
3.29 |
*psp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp); |
400 |
greg |
3.2 |
if (*psp == NULL) { |
401 |
greg |
3.37 |
free(lp); |
402 |
greg |
3.17 |
tmDone(tms); |
403 |
greg |
3.1 |
returnErr(TM_E_NOMEM); |
404 |
greg |
3.2 |
} |
405 |
greg |
3.1 |
cp = TM_NOCHROM; |
406 |
|
|
} else |
407 |
|
|
*psp = cp; |
408 |
|
|
/* compute color mapping */ |
409 |
greg |
3.17 |
err = tmAddHisto(tms, lp, *xp * *yp, 1); |
410 |
greg |
3.1 |
if (err != TM_E_OK) |
411 |
|
|
goto done; |
412 |
greg |
3.17 |
err = tmComputeMapping(tms, gamval, Lddyn, Ldmax); |
413 |
greg |
3.1 |
if (err != TM_E_OK) |
414 |
|
|
goto done; |
415 |
|
|
/* map colors */ |
416 |
greg |
3.17 |
err = tmMapPixels(tms, *psp, lp, cp, *xp * *yp); |
417 |
greg |
3.1 |
|
418 |
|
|
done: /* clean up */ |
419 |
greg |
3.37 |
free(lp); |
420 |
greg |
3.17 |
tmDone(tms); |
421 |
greg |
3.1 |
if (err != TM_E_OK) { /* free memory on error */ |
422 |
greg |
3.37 |
free(*psp); |
423 |
greg |
3.1 |
*psp = NULL; |
424 |
|
|
returnErr(err); |
425 |
|
|
} |
426 |
|
|
returnOK; |
427 |
greg |
3.3 |
} |
428 |
|
|
|
429 |
|
|
|
430 |
|
|
static void |
431 |
|
|
colrNewSpace(tms) /* color space changed for tone mapping */ |
432 |
greg |
3.28 |
TMstruct *tms; |
433 |
greg |
3.3 |
{ |
434 |
greg |
3.28 |
COLRDATA *cd; |
435 |
greg |
3.3 |
double d; |
436 |
greg |
3.21 |
int i, j; |
437 |
greg |
3.3 |
|
438 |
|
|
cd = (COLRDATA *)tms->pd[colrReg]; |
439 |
greg |
3.21 |
for (i = 3; i--; ) |
440 |
greg |
3.24 |
cd->clfb[i] = 0x1000*tms->clf[i] + .5; |
441 |
greg |
3.23 |
cd->inpsfb = tmCvLuminance(tms->inpsf); |
442 |
greg |
3.21 |
for (i = 3; i--; ) |
443 |
|
|
for (j = 3; j--; ) { |
444 |
|
|
d = tms->cmat[i][j] / tms->inpsf; |
445 |
|
|
cd->cmatb[i][j] = 0x10000*d + (d<0. ? -.5 : .5); |
446 |
|
|
} |
447 |
greg |
3.3 |
} |
448 |
|
|
|
449 |
|
|
|
450 |
greg |
3.37 |
static void * |
451 |
greg |
3.3 |
colrInit(tms) /* initialize private data for tone mapping */ |
452 |
greg |
3.28 |
TMstruct *tms; |
453 |
greg |
3.3 |
{ |
454 |
greg |
3.28 |
COLRDATA *cd; |
455 |
|
|
int i; |
456 |
greg |
3.3 |
/* allocate our data */ |
457 |
|
|
cd = (COLRDATA *)malloc(sizeof(COLRDATA)); |
458 |
|
|
if (cd == NULL) |
459 |
|
|
return(NULL); |
460 |
greg |
3.37 |
tms->pd[colrReg] = (void *)cd; |
461 |
greg |
3.3 |
/* compute gamma table */ |
462 |
|
|
for (i = GAMTSZ; i--; ) |
463 |
|
|
cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam); |
464 |
|
|
/* compute color and scale factors */ |
465 |
|
|
colrNewSpace(tms); |
466 |
greg |
3.37 |
return((void *)cd); |
467 |
greg |
3.1 |
} |