9 |
|
|
10 |
|
#include "copyright.h" |
11 |
|
|
12 |
< |
#include <stdio.h> |
13 |
< |
#include <string.h> |
12 |
> |
#include <stdlib.h> |
13 |
|
#include <math.h> |
14 |
< |
#include <time.h> |
14 |
> |
|
15 |
> |
#ifdef PCOND |
16 |
> |
#include "paths.h" |
17 |
> |
#endif |
18 |
|
#include "tmprivat.h" |
19 |
+ |
#include "rtio.h" |
20 |
|
#include "resolu.h" |
21 |
|
|
22 |
< |
#ifndef TM_PIC_CTRANS |
20 |
< |
#define TM_PIC_CTRANS 1 /* transform colors? (expensive) */ |
21 |
< |
#endif |
22 |
> |
#define GAMTSZ 4096 |
23 |
|
|
23 |
– |
#define GAMTSZ 1024 |
24 |
– |
|
24 |
|
typedef struct { |
25 |
< |
BYTE gamb[GAMTSZ]; /* gamma lookup table */ |
26 |
< |
COLR clfb; /* encoded tm->clf */ |
25 |
> |
uby8 gamb[GAMTSZ]; /* gamma lookup table */ |
26 |
> |
int clfb[3]; /* encoded tm->clf */ |
27 |
> |
int32 cmatb[3][3]; /* encoded color transform */ |
28 |
|
TMbright inpsfb; /* encoded tm->inpsf */ |
29 |
|
} COLRDATA; |
30 |
|
|
31 |
< |
#ifdef NOPROTO |
32 |
< |
static MEM_PTR colrInit(); |
33 |
< |
static void colrNewSpace(); |
34 |
< |
#else |
35 |
< |
static MEM_PTR colrInit(struct tmStruct *); |
36 |
< |
static void colrNewSpace(struct tmStruct *); |
37 |
< |
#endif |
31 |
> |
static void * colrInit(TMstruct *); |
32 |
> |
static void colrNewSpace(TMstruct *); |
33 |
> |
static gethfunc headline; |
34 |
> |
|
35 |
|
static struct tmPackage colrPkg = { /* our package functions */ |
36 |
|
colrInit, colrNewSpace, free |
37 |
|
}; |
38 |
|
static int colrReg = -1; /* our package registration number */ |
39 |
|
|
40 |
< |
#define LOGISZ 260 |
44 |
< |
static TMbright logi[LOGISZ]; |
40 |
> |
static TMbright logi[256]; |
41 |
|
|
42 |
|
|
43 |
|
int |
44 |
< |
tmCvColrs(ls, cs, scan, len) /* convert RGBE/XYZE colors */ |
45 |
< |
TMbright *ls; |
46 |
< |
BYTE *cs; |
47 |
< |
COLR *scan; |
48 |
< |
int len; |
44 |
> |
tmCvColrs( /* convert RGBE/XYZE colors */ |
45 |
> |
TMstruct *tms, |
46 |
> |
TMbright *ls, |
47 |
> |
uby8 *cs, |
48 |
> |
COLR *scan, |
49 |
> |
int len |
50 |
> |
) |
51 |
|
{ |
52 |
< |
static char funcName[] = "tmCvColrs"; |
53 |
< |
COLR cmon; |
54 |
< |
register COLRDATA *cd; |
55 |
< |
register int i, bi, li; |
52 |
> |
static const char funcName[] = "tmCvColrs"; |
53 |
> |
int cmon[4]; |
54 |
> |
COLRDATA *cd; |
55 |
> |
int i, j, bi; |
56 |
> |
int32 li, vl; |
57 |
|
|
58 |
< |
if (tmTop == NULL) |
58 |
> |
if (tms == NULL) |
59 |
|
returnErr(TM_E_TMINVAL); |
60 |
< |
if (ls == NULL | scan == NULL | len < 0) |
60 |
> |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
61 |
|
returnErr(TM_E_ILLEGAL); |
63 |
– |
#if TM_PIC_CTRANS |
64 |
– |
if (tmNeedMatrix(tmTop)) { /* need floating point */ |
65 |
– |
#else |
66 |
– |
if (tmTop->inppri == TM_XYZPRIM) { /* no way around this */ |
67 |
– |
#endif |
68 |
– |
register COLOR *newscan; |
69 |
– |
newscan = (COLOR *)tempbuffer(len*sizeof(COLOR)); |
70 |
– |
if (newscan == NULL) |
71 |
– |
returnErr(TM_E_NOMEM); |
72 |
– |
for (i = len; i--; ) |
73 |
– |
colr_color(newscan[i], scan[i]); |
74 |
– |
return(tmCvColors(ls, cs, newscan, len)); |
75 |
– |
} |
62 |
|
if (colrReg < 0) { /* build tables if necessary */ |
63 |
|
colrReg = tmRegPkg(&colrPkg); |
64 |
|
if (colrReg < 0) |
65 |
|
returnErr(TM_E_CODERR1); |
66 |
|
for (i = 256; i--; ) |
67 |
|
logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5; |
82 |
– |
for (i = 256; i < LOGISZ; i++) |
83 |
– |
logi[i] = 0; |
68 |
|
tmMkMesofact(); |
69 |
|
} |
70 |
< |
if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL) |
70 |
> |
if ((cd = (COLRDATA *)tmPkgData(tms,colrReg)) == NULL) |
71 |
|
returnErr(TM_E_NOMEM); |
72 |
|
for (i = len; i--; ) { |
73 |
< |
copycolr(cmon, scan[i]); |
73 |
> |
if (tmNeedMatrix(tms)) { /* apply color xform */ |
74 |
> |
bi = 0; |
75 |
> |
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 |
> |
if (vl < 0) |
80 |
> |
cmon[j] = vl/(int32)0x10000; |
81 |
> |
else if ((cmon[j] = vl>>16) > bi) |
82 |
> |
bi = cmon[j]; |
83 |
> |
} |
84 |
> |
cmon[EXP] = scan[i][EXP]; |
85 |
> |
while (bi >= 256) { /* handle overflow */ |
86 |
> |
cmon[EXP]++; |
87 |
> |
for (j = 3; j--; ) cmon[j] >>= 1; |
88 |
> |
bi >>= 1; |
89 |
> |
} |
90 |
> |
} else |
91 |
> |
copycolr(cmon, scan[i]); |
92 |
|
/* world luminance */ |
93 |
< |
li = ( cd->clfb[RED]*cmon[RED] + |
94 |
< |
cd->clfb[GRN]*cmon[GRN] + |
95 |
< |
cd->clfb[BLU]*cmon[BLU] ) >> 8; |
96 |
< |
bi = BRT2SCALE(cmon[EXP]-COLXS) + |
97 |
< |
logi[li] + cd->inpsfb; |
98 |
< |
if (li <= 0) { |
99 |
< |
bi = TM_NOBRT; /* bogus value */ |
100 |
< |
li = 1; /* avoid li==0 */ |
93 |
> |
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 |
> |
else li >>= 12; |
98 |
> |
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 |
|
} |
105 |
|
ls[i] = bi; |
106 |
|
if (cs == TM_NOCHROM) /* no color? */ |
107 |
|
continue; |
108 |
|
/* mesopic adj. */ |
109 |
< |
if (tmTop->flags & TM_F_MESOPIC && bi < BMESUPPER) { |
110 |
< |
register int pf, sli = normscot(cmon); |
111 |
< |
if (bi < BMESLOWER) |
109 |
> |
if (tms->flags & TM_F_MESOPIC && bi < BMESUPPER) { |
110 |
> |
int pf, sli = normscot(cmon); |
111 |
> |
if (bi < BMESLOWER) { |
112 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = sli; |
113 |
< |
else { |
114 |
< |
if (tmTop->flags & TM_F_BW) |
113 |
> |
} else { |
114 |
> |
if (tms->flags & TM_F_BW) |
115 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
116 |
|
pf = tmMesofact[bi-BMESLOWER]; |
117 |
|
sli *= 256 - pf; |
118 |
< |
cmon[RED] = ( sli + pf*cmon[RED] ) >> 8; |
119 |
< |
cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8; |
120 |
< |
cmon[BLU] = ( sli + pf*cmon[BLU] ) >> 8; |
118 |
> |
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 |
|
} |
124 |
< |
} else if (tmTop->flags & TM_F_BW) { |
125 |
< |
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
124 |
> |
} else if (tms->flags & TM_F_BW) { |
125 |
> |
for (j = 3; j--; ) |
126 |
> |
cs[3*i+j] = tms->cdiv[j]/(TM_BRES>>8); |
127 |
> |
continue; |
128 |
> |
} else { |
129 |
> |
for (j = 3; j--; ) |
130 |
> |
cmon[j] *= (cmon[j] > 0); |
131 |
|
} |
132 |
< |
bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; |
132 |
> |
bi = ( (uint32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 12; |
133 |
|
cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
134 |
< |
bi = ( (int32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; |
134 |
> |
bi = ( (uint32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 12; |
135 |
|
cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
136 |
< |
bi = ( (int32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; |
136 |
> |
bi = ( (uint32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 12; |
137 |
|
cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
138 |
|
} |
139 |
|
returnOK; |
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 */ |
145 |
> |
#define FMTSPEC 3 /* Input is N-component spectral data */ |
146 |
> |
#define FMTUNK 0 /* Input format is unspecified */ |
147 |
> |
#define FMTBAD (-1) /* Input is not a recognized format */ |
148 |
|
|
149 |
|
static struct radhead { |
150 |
|
int format; /* FMTRGB, FMTCIE, FMTUNK, FMTBAD */ |
151 |
|
double expos; /* input exposure value */ |
152 |
|
RGBPRIMP primp; /* input primaries */ |
153 |
|
RGBPRIMS mypri; /* custom primaries */ |
154 |
< |
} rhdefault = {FMTUNK, 1., stdprims, STDPRIMS}; |
154 |
> |
int ncs; /* number of color samples */ |
155 |
> |
float wpt[4]; /* spectral partition */ |
156 |
> |
} rhdefault = {FMTUNK, 1., stdprims, STDPRIMS, 3, {0,0,0,0}}; |
157 |
|
|
158 |
|
|
159 |
|
static int |
160 |
< |
headline(s, rh) /* grok a header line */ |
161 |
< |
register char *s; |
162 |
< |
register struct radhead *rh; |
160 |
> |
headline( /* grok a header line */ |
161 |
> |
char *s, |
162 |
> |
void *vrh |
163 |
> |
) |
164 |
|
{ |
165 |
< |
char fmt[32]; |
165 |
> |
char fmt[MAXFMTLEN]; |
166 |
> |
struct radhead *rh = vrh; |
167 |
|
|
168 |
|
if (formatval(fmt, s)) { |
169 |
|
if (!strcmp(fmt, COLRFMT)) |
170 |
|
rh->format = FMTRGB; |
171 |
|
else if (!strcmp(fmt, CIEFMT)) |
172 |
|
rh->format = FMTCIE; |
173 |
+ |
else if (!strcmp(fmt, SPECFMT)) |
174 |
+ |
rh->format = FMTSPEC; |
175 |
|
else |
176 |
|
rh->format = FMTBAD; |
177 |
|
return(0); |
185 |
|
rh->primp = rh->mypri; |
186 |
|
return(0); |
187 |
|
} |
188 |
+ |
if (isncomp(s)) { |
189 |
+ |
rh->ncs = ncompval(s); |
190 |
+ |
return(0); |
191 |
+ |
} |
192 |
+ |
if (iswlsplit(s)) { |
193 |
+ |
wlsplitval(rh->wpt, s); |
194 |
+ |
return(0); |
195 |
+ |
} |
196 |
|
return(0); |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
int |
201 |
< |
tmLoadPicture(lpp, cpp, xp, yp, fname, fp) /* convert Radiance picture */ |
202 |
< |
TMbright **lpp; |
203 |
< |
BYTE **cpp; |
204 |
< |
int *xp, *yp; |
205 |
< |
char *fname; |
206 |
< |
FILE *fp; |
201 |
> |
tmLoadPicture( /* convert Radiance picture */ |
202 |
> |
TMstruct *tms, |
203 |
> |
TMbright **lpp, |
204 |
> |
uby8 **cpp, |
205 |
> |
int *xp, |
206 |
> |
int *yp, |
207 |
> |
char *fname, |
208 |
> |
FILE *fp |
209 |
> |
) |
210 |
|
{ |
211 |
|
char *funcName = fname==NULL ? "tmLoadPicture" : fname; |
212 |
|
FILE *inpf; |
215 |
|
COLR *scanin = NULL; |
216 |
|
int i; |
217 |
|
/* check arguments */ |
218 |
< |
if (tmTop == NULL) |
218 |
> |
if (tms == NULL) |
219 |
|
returnErr(TM_E_TMINVAL); |
220 |
< |
if (lpp == NULL | xp == NULL | yp == NULL | |
221 |
< |
(fname == NULL & fp == TM_GETFILE)) |
220 |
> |
if ((lpp == NULL) | (xp == NULL) | (yp == NULL) | |
221 |
> |
((fname == NULL) & (fp == TM_GETFILE))) |
222 |
|
returnErr(TM_E_ILLEGAL); |
223 |
|
*xp = *yp = 0; /* error precaution */ |
224 |
< |
if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL) |
224 |
> |
if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "rb")) == NULL) |
225 |
|
returnErr(TM_E_BADFILE); |
226 |
|
*lpp = NULL; |
227 |
|
if (cpp != TM_NOCHROMP) *cpp = NULL; |
228 |
|
info = rhdefault; /* get our header */ |
229 |
< |
getheader(inpf, headline, (char *)&info); |
230 |
< |
if (info.format == FMTBAD | info.expos <= 0. || |
229 |
> |
getheader(inpf, headline, &info); |
230 |
> |
if ((info.format == FMTBAD) | (info.expos <= 0.) || |
231 |
|
fgetresolu(xp, yp, inpf) < 0) { |
232 |
|
err = TM_E_BADFILE; goto done; |
233 |
|
} |
234 |
< |
if (info.format == FMTUNK) /* assume RGBE format */ |
234 |
> |
if (info.format == FMTSPEC) { /* valid spectrum? */ |
235 |
> |
if (info.ncs <= 3) { |
236 |
> |
err = TM_E_BADFILE; goto done; |
237 |
> |
} |
238 |
> |
if (info.wpt[0] == 0) |
239 |
> |
memcpy(info.wpt, WLPART, sizeof(info.wpt)); |
240 |
> |
} else if (info.format == FMTUNK) /* assume RGBE format? */ |
241 |
|
info.format = FMTRGB; |
242 |
< |
if (info.format == FMTRGB) |
243 |
< |
info.expos /= WHTEFFICACY; |
208 |
< |
else if (info.format == FMTCIE) |
242 |
> |
|
243 |
> |
if (info.format == FMTCIE) |
244 |
|
info.primp = TM_XYZPRIM; |
245 |
+ |
else |
246 |
+ |
info.expos /= WHTEFFICACY; |
247 |
|
/* prepare library */ |
248 |
< |
if ((err = tmSetSpace(info.primp, 1./info.expos)) != TM_E_OK) |
248 |
> |
if ((err = tmSetSpace(tms, info.primp, 1./info.expos)) != TM_E_OK) |
249 |
|
goto done; |
250 |
|
err = TM_E_NOMEM; /* allocate arrays */ |
251 |
|
*lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp); |
252 |
|
if (*lpp == NULL) |
253 |
|
goto done; |
254 |
|
if (cpp != TM_NOCHROMP) { |
255 |
< |
*cpp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); |
255 |
> |
*cpp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp); |
256 |
|
if (*cpp == NULL) |
257 |
|
goto done; |
258 |
|
} |
261 |
|
goto done; |
262 |
|
err = TM_E_BADFILE; /* read & convert scanlines */ |
263 |
|
for (i = 0; i < *yp; i++) { |
264 |
< |
if (freadcolrs(scanin, *xp, inpf) < 0) { |
264 |
> |
if (fread2colrs(scanin, *xp, inpf, info.ncs, info.wpt) < 0) { |
265 |
|
err = TM_E_BADFILE; break; |
266 |
|
} |
267 |
< |
err = tmCvColrs(*lpp + (i * *xp), |
267 |
> |
err = tmCvColrs(tms, *lpp + (i * *xp), |
268 |
|
cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp + (i * 3 * *xp), |
269 |
|
scanin, *xp); |
270 |
|
if (err != TM_E_OK) |
274 |
|
if (fp == NULL) |
275 |
|
fclose(inpf); |
276 |
|
if (scanin != NULL) |
277 |
< |
free((MEM_PTR)scanin); |
277 |
> |
free(scanin); |
278 |
|
if (err != TM_E_OK) { |
279 |
|
if (*lpp != NULL) |
280 |
< |
free((MEM_PTR)*lpp); |
280 |
> |
free(*lpp); |
281 |
|
if (cpp != TM_NOCHROMP && *cpp != NULL) |
282 |
< |
free((MEM_PTR)*cpp); |
282 |
> |
free(*cpp); |
283 |
|
returnErr(err); |
284 |
|
} |
285 |
|
returnOK; |
287 |
|
|
288 |
|
|
289 |
|
#ifdef PCOND |
290 |
< |
int /* run pcond to map picture */ |
290 |
> |
static int /* run pcond to map picture */ |
291 |
|
dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname) |
292 |
< |
BYTE **psp; |
292 |
> |
uby8 **psp; |
293 |
|
int *xp, *yp; |
294 |
|
int flags; |
295 |
|
RGBPRIMP monpri; |
297 |
|
char *fname; |
298 |
|
{ |
299 |
|
char *funcName = fname; |
300 |
+ |
TMstruct *tms = NULL; |
301 |
|
char cmdbuf[1024]; |
302 |
|
FILE *infp; |
303 |
< |
register COLR *scan; |
304 |
< |
register BYTE *rp; |
303 |
> |
COLR *scan; |
304 |
> |
uby8 *rp; |
305 |
|
int y; |
306 |
< |
register int x; |
306 |
> |
int x; |
307 |
|
/* set up gamma correction */ |
308 |
|
if (setcolrcor(pow, 1./gamval) < 0) |
309 |
|
returnErr(TM_E_NOMEM); |
340 |
|
/* allocate arrays */ |
341 |
|
scan = (COLR *)malloc(sizeof(COLR) * *xp); |
342 |
|
if (flags & TM_F_BW) |
343 |
< |
rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
343 |
> |
rp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp); |
344 |
|
else |
345 |
< |
rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); |
346 |
< |
if ((*psp = rp) == NULL | scan == NULL) { |
345 |
> |
rp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp); |
346 |
> |
if (((*psp = rp) == NULL) | (scan == NULL)) { |
347 |
|
pclose(infp); |
348 |
|
returnErr(TM_E_NOMEM); |
349 |
|
} |
351 |
|
for (y = 0; y < *yp; y++) { |
352 |
|
if (freadcolrs(scan, *xp, infp) < 0) { |
353 |
|
pclose(infp); |
354 |
< |
free((MEM_PTR)scan); |
355 |
< |
free((MEM_PTR)*psp); |
354 |
> |
free(scan); |
355 |
> |
free(*psp); |
356 |
|
*psp = NULL; |
357 |
|
returnErr(TM_E_BADFILE); |
358 |
|
} |
367 |
|
*rp++ = scan[x][BLU]; |
368 |
|
} |
369 |
|
} |
370 |
< |
free((MEM_PTR)scan); |
370 |
> |
free(scan); |
371 |
|
pclose(infp); |
372 |
|
returnOK; |
373 |
|
} |
376 |
|
|
377 |
|
int /* map a Radiance picture */ |
378 |
|
tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp) |
379 |
< |
BYTE **psp; |
379 |
> |
uby8 **psp; |
380 |
|
int *xp, *yp; |
381 |
|
int flags; |
382 |
|
RGBPRIMP monpri; |
385 |
|
FILE *fp; |
386 |
|
{ |
387 |
|
char *funcName = fname==NULL ? "tmMapPicture" : fname; |
388 |
< |
BYTE *cp; |
388 |
> |
TMstruct *tms = NULL; |
389 |
> |
uby8 *cp; |
390 |
|
TMbright *lp; |
391 |
|
int err; |
392 |
|
/* check arguments */ |
393 |
< |
if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL | |
394 |
< |
(fname == NULL & fp == TM_GETFILE)) |
393 |
> |
if ((psp == NULL) | (xp == NULL) | (yp == NULL) | (monpri == NULL) | |
394 |
> |
((fname == NULL) & (fp == TM_GETFILE))) |
395 |
|
returnErr(TM_E_ILLEGAL); |
396 |
|
/* set defaults */ |
397 |
|
if (gamval < MINGAM) gamval = DEFGAM; |
405 |
|
monpri, gamval, Lddyn, Ldmax, fname) ); |
406 |
|
#endif |
407 |
|
/* initialize tone mapping */ |
408 |
< |
if (tmInit(flags, monpri, gamval) == NULL) |
408 |
> |
if ((tms = tmInit(flags, monpri, gamval)) == NULL) |
409 |
|
returnErr(TM_E_NOMEM); |
410 |
|
/* load & convert picture */ |
411 |
< |
err = tmLoadPicture(&lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp, |
411 |
> |
err = tmLoadPicture(tms, &lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp, |
412 |
|
xp, yp, fname, fp); |
413 |
|
if (err != TM_E_OK) { |
414 |
< |
tmDone(NULL); |
414 |
> |
tmDone(tms); |
415 |
|
return(err); |
416 |
|
} |
417 |
|
/* allocate space for result */ |
418 |
|
if (flags & TM_F_BW) { |
419 |
< |
*psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
419 |
> |
*psp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp); |
420 |
|
if (*psp == NULL) { |
421 |
< |
free((MEM_PTR)lp); |
422 |
< |
tmDone(NULL); |
421 |
> |
free(lp); |
422 |
> |
tmDone(tms); |
423 |
|
returnErr(TM_E_NOMEM); |
424 |
|
} |
425 |
|
cp = TM_NOCHROM; |
426 |
|
} else |
427 |
|
*psp = cp; |
428 |
|
/* compute color mapping */ |
429 |
< |
err = tmAddHisto(lp, *xp * *yp, 1); |
429 |
> |
err = tmAddHisto(tms, lp, *xp * *yp, 1); |
430 |
|
if (err != TM_E_OK) |
431 |
|
goto done; |
432 |
< |
err = tmComputeMapping(gamval, Lddyn, Ldmax); |
432 |
> |
err = tmComputeMapping(tms, gamval, Lddyn, Ldmax); |
433 |
|
if (err != TM_E_OK) |
434 |
|
goto done; |
435 |
|
/* map colors */ |
436 |
< |
err = tmMapPixels(*psp, lp, cp, *xp * *yp); |
436 |
> |
err = tmMapPixels(tms, *psp, lp, cp, *xp * *yp); |
437 |
|
|
438 |
|
done: /* clean up */ |
439 |
< |
free((MEM_PTR)lp); |
440 |
< |
tmDone(NULL); |
439 |
> |
free(lp); |
440 |
> |
tmDone(tms); |
441 |
|
if (err != TM_E_OK) { /* free memory on error */ |
442 |
< |
free((MEM_PTR)*psp); |
442 |
> |
free(*psp); |
443 |
|
*psp = NULL; |
444 |
|
returnErr(err); |
445 |
|
} |
449 |
|
|
450 |
|
static void |
451 |
|
colrNewSpace(tms) /* color space changed for tone mapping */ |
452 |
< |
register struct tmStruct *tms; |
452 |
> |
TMstruct *tms; |
453 |
|
{ |
454 |
< |
register COLRDATA *cd; |
454 |
> |
COLRDATA *cd; |
455 |
|
double d; |
456 |
+ |
int i, j; |
457 |
|
|
458 |
|
cd = (COLRDATA *)tms->pd[colrReg]; |
459 |
< |
cd->clfb[RED] = 256.*tms->clf[RED] + .5; |
460 |
< |
cd->clfb[GRN] = 256.*tms->clf[GRN] + .5; |
461 |
< |
cd->clfb[BLU] = 256.*tms->clf[BLU] + .5; |
462 |
< |
cd->clfb[EXP] = COLXS; |
463 |
< |
d = TM_BRTSCALE*log(tms->inpsf); |
464 |
< |
cd->inpsfb = d<0. ? d-.5 : d+.5; |
459 |
> |
for (i = 3; i--; ) |
460 |
> |
cd->clfb[i] = 0x1000*tms->clf[i] + .5; |
461 |
> |
cd->inpsfb = tmCvLuminance(tms->inpsf); |
462 |
> |
for (i = 3; i--; ) |
463 |
> |
for (j = 3; j--; ) { |
464 |
> |
d = tms->cmat[i][j] / tms->inpsf; |
465 |
> |
cd->cmatb[i][j] = 0x10000*d + (d<0. ? -.5 : .5); |
466 |
> |
} |
467 |
|
} |
468 |
|
|
469 |
|
|
470 |
< |
static MEM_PTR |
470 |
> |
static void * |
471 |
|
colrInit(tms) /* initialize private data for tone mapping */ |
472 |
< |
register struct tmStruct *tms; |
472 |
> |
TMstruct *tms; |
473 |
|
{ |
474 |
< |
register COLRDATA *cd; |
475 |
< |
register int i; |
474 |
> |
COLRDATA *cd; |
475 |
> |
int i; |
476 |
|
/* allocate our data */ |
477 |
|
cd = (COLRDATA *)malloc(sizeof(COLRDATA)); |
478 |
|
if (cd == NULL) |
479 |
|
return(NULL); |
480 |
< |
tms->pd[colrReg] = (MEM_PTR)cd; |
480 |
> |
tms->pd[colrReg] = (void *)cd; |
481 |
|
/* compute gamma table */ |
482 |
|
for (i = GAMTSZ; i--; ) |
483 |
|
cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam); |
484 |
|
/* compute color and scale factors */ |
485 |
|
colrNewSpace(tms); |
486 |
< |
return((MEM_PTR)cd); |
486 |
> |
return((void *)cd); |
487 |
|
} |