1 |
– |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Routines for tone mapping on Radiance RGBE and XYZE pictures. |
6 |
< |
* See tonemap.h for detailed function descriptions. |
6 |
> |
* |
7 |
> |
* Externals declared in tonemap.h |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#include "copyright.h" |
11 |
+ |
|
12 |
|
#include <stdio.h> |
13 |
+ |
#include <stdlib.h> |
14 |
+ |
#include <string.h> |
15 |
|
#include <math.h> |
16 |
+ |
#include <time.h> |
17 |
+ |
|
18 |
+ |
#ifdef PCOND |
19 |
+ |
#include "paths.h" |
20 |
+ |
#endif |
21 |
|
#include "tmprivat.h" |
22 |
+ |
#include "rtio.h" |
23 |
|
#include "resolu.h" |
24 |
|
|
25 |
+ |
#define GAMTSZ 4096 |
26 |
|
|
18 |
– |
extern char *tempbuffer(); |
19 |
– |
|
20 |
– |
#define GAMTSZ 1024 |
21 |
– |
|
27 |
|
typedef struct { |
28 |
< |
BYTE gamb[GAMTSZ]; /* gamma lookup table */ |
29 |
< |
COLR clfb; /* encoded tm->clf */ |
28 |
> |
uby8 gamb[GAMTSZ]; /* gamma lookup table */ |
29 |
> |
int clfb[3]; /* encoded tm->clf */ |
30 |
> |
int32 cmatb[3][3]; /* encoded color transform */ |
31 |
|
TMbright inpsfb; /* encoded tm->inpsf */ |
32 |
|
} COLRDATA; |
33 |
|
|
34 |
< |
static MEM_PTR colrInit(); |
35 |
< |
static void colrNewSpace(); |
36 |
< |
extern void free(); |
34 |
> |
static MEM_PTR colrInit(TMstruct *); |
35 |
> |
static void colrNewSpace(TMstruct *); |
36 |
> |
static gethfunc headline; |
37 |
> |
|
38 |
|
static struct tmPackage colrPkg = { /* our package functions */ |
39 |
|
colrInit, colrNewSpace, free |
40 |
|
}; |
41 |
|
static int colrReg = -1; /* our package registration number */ |
42 |
|
|
43 |
< |
#define LOGISZ 260 |
37 |
< |
static TMbright logi[LOGISZ]; |
38 |
< |
static BYTE photofact[BMESUPPER-BMESLOWER]; |
43 |
> |
static TMbright logi[256]; |
44 |
|
|
45 |
|
|
46 |
|
int |
47 |
< |
tmCvColrs(ls, cs, scan, len) /* tone map RGBE/XYZE colors */ |
48 |
< |
TMbright *ls; |
49 |
< |
BYTE *cs; |
50 |
< |
COLR *scan; |
51 |
< |
int len; |
47 |
> |
tmCvColrs( /* convert RGBE/XYZE colors */ |
48 |
> |
TMstruct *tms, |
49 |
> |
TMbright *ls, |
50 |
> |
uby8 *cs, |
51 |
> |
COLR *scan, |
52 |
> |
int len |
53 |
> |
) |
54 |
|
{ |
55 |
< |
static char funcName[] = "tmCvColrs"; |
56 |
< |
COLR cmon; |
57 |
< |
register COLRDATA *cd; |
58 |
< |
register int i, bi, li; |
55 |
> |
static const char funcName[] = "tmCvColrs"; |
56 |
> |
int cmon[4]; |
57 |
> |
COLRDATA *cd; |
58 |
> |
int i, j, bi; |
59 |
> |
int32 li, vl; |
60 |
|
|
61 |
< |
if (tmTop == NULL) |
61 |
> |
if (tms == NULL) |
62 |
|
returnErr(TM_E_TMINVAL); |
63 |
< |
if (ls == NULL | scan == NULL | len < 0) |
63 |
> |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
64 |
|
returnErr(TM_E_ILLEGAL); |
65 |
< |
if (tmNeedMatrix(tmTop)) { /* need floating point */ |
58 |
< |
register COLOR *newscan; |
59 |
< |
newscan = (COLOR *)tempbuffer(len*sizeof(COLOR)); |
60 |
< |
if (newscan == NULL) |
61 |
< |
returnErr(TM_E_NOMEM); |
62 |
< |
for (i = len; i--; ) |
63 |
< |
colr_color(newscan[i], scan[i]); |
64 |
< |
return(tmCvColors(ls, cs, newscan, len)); |
65 |
< |
} |
66 |
< |
if (colrReg == -1) { /* build tables if necessary */ |
65 |
> |
if (colrReg < 0) { /* build tables if necessary */ |
66 |
|
colrReg = tmRegPkg(&colrPkg); |
67 |
|
if (colrReg < 0) |
68 |
|
returnErr(TM_E_CODERR1); |
69 |
|
for (i = 256; i--; ) |
70 |
|
logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5; |
71 |
< |
for (i = 256; i < LOGISZ; i++) |
73 |
< |
logi[i] = logi[255]; |
74 |
< |
for (i = BMESLOWER; i < BMESUPPER; i++) |
75 |
< |
photofact[i-BMESLOWER] = 256. * |
76 |
< |
(tmLuminance(i) - LMESLOWER) / |
77 |
< |
(LMESUPPER - LMESLOWER); |
71 |
> |
tmMkMesofact(); |
72 |
|
} |
73 |
< |
if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL) |
73 |
> |
if ((cd = (COLRDATA *)tmPkgData(tms,colrReg)) == NULL) |
74 |
|
returnErr(TM_E_NOMEM); |
75 |
|
for (i = len; i--; ) { |
76 |
< |
copycolr(cmon, scan[i]); |
76 |
> |
if (tmNeedMatrix(tms)) { /* apply color xform */ |
77 |
> |
for (j = 3; j--; ) { |
78 |
> |
vl = cd->cmatb[j][RED]*(int32)scan[i][RED] + |
79 |
> |
cd->cmatb[j][GRN]*(int32)scan[i][GRN] + |
80 |
> |
cd->cmatb[j][BLU]*(int32)scan[i][BLU] ; |
81 |
> |
if (vl < 0) cmon[j] = vl/0x10000; |
82 |
> |
else cmon[j] = vl>>16; |
83 |
> |
} |
84 |
> |
cmon[EXP] = scan[i][EXP]; |
85 |
> |
} else |
86 |
> |
copycolr(cmon, scan[i]); |
87 |
|
/* world luminance */ |
88 |
< |
li = ( cd->clfb[RED]*cmon[RED] + |
89 |
< |
cd->clfb[GRN]*cmon[GRN] + |
90 |
< |
cd->clfb[BLU]*cmon[BLU] ) >> 8; |
91 |
< |
bi = BRT2SCALE*(cmon[EXP]-COLXS) + |
92 |
< |
logi[li] + cd->inpsfb; |
93 |
< |
if (bi < MINBRT) { |
94 |
< |
bi = MINBRT-1; /* bogus value */ |
95 |
< |
li++; /* avoid li==0 */ |
88 |
> |
li = cd->clfb[RED]*(int32)cmon[RED] + |
89 |
> |
cd->clfb[GRN]*(int32)cmon[GRN] + |
90 |
> |
cd->clfb[BLU]*(int32)cmon[BLU] ; |
91 |
> |
if (li >= 1L<<(12+8)) li = 255; |
92 |
> |
else li >>= 12; |
93 |
> |
bi = BRT2SCALE(cmon[EXP]-COLXS) + cd->inpsfb; |
94 |
> |
if (li > 0) |
95 |
> |
bi += logi[li]; |
96 |
> |
else { |
97 |
> |
bi += logi[0]; |
98 |
> |
li = 1; /* avoid /0 */ |
99 |
|
} |
100 |
|
ls[i] = bi; |
101 |
|
if (cs == TM_NOCHROM) /* no color? */ |
102 |
|
continue; |
103 |
|
/* mesopic adj. */ |
104 |
< |
if (tmTop->flags & TM_F_MESOPIC && bi < BMESUPPER) { |
105 |
< |
register int pf, sli = normscot(cmon); |
106 |
< |
if (bi < BMESLOWER) |
104 |
> |
if (tms->flags & TM_F_MESOPIC && bi < BMESUPPER) { |
105 |
> |
int pf, sli = normscot(cmon); |
106 |
> |
if (bi < BMESLOWER) { |
107 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = sli; |
108 |
< |
else { |
109 |
< |
if (tmTop->flags & TM_F_BW) |
108 |
> |
} else { |
109 |
> |
if (tms->flags & TM_F_BW) |
110 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
111 |
< |
pf = photofact[bi-BMESLOWER]; |
111 |
> |
pf = tmMesofact[bi-BMESLOWER]; |
112 |
|
sli *= 256 - pf; |
113 |
< |
cmon[RED] = ( sli + pf*cmon[RED] ) >> 8; |
114 |
< |
cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8; |
115 |
< |
cmon[BLU] = ( sli + pf*cmon[BLU] ) >> 8; |
113 |
> |
for (j = 3; j--; ) { |
114 |
> |
cmon[j] = sli + pf*cmon[j]; |
115 |
> |
if (cmon[j] <= 0) cmon[j] = 0; |
116 |
> |
else cmon[j] >>= 8; |
117 |
> |
} |
118 |
|
} |
119 |
< |
} else if (tmTop->flags & TM_F_BW) { |
119 |
> |
} else if (tms->flags & TM_F_BW) { |
120 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
121 |
+ |
} else { |
122 |
+ |
for (j = 3; j--; ) |
123 |
+ |
if (cmon[j] < 0) cmon[j] = 0; |
124 |
|
} |
125 |
< |
bi = ( (int4)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; |
125 |
> |
bi = ( (uint32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 12; |
126 |
|
cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
127 |
< |
bi = ( (int4)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; |
127 |
> |
bi = ( (uint32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 12; |
128 |
|
cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
129 |
< |
bi = ( (int4)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; |
129 |
> |
bi = ( (uint32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 12; |
130 |
|
cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
131 |
|
} |
132 |
|
returnOK; |
147 |
|
|
148 |
|
|
149 |
|
static int |
150 |
< |
headline(s, rh) /* grok a header line */ |
151 |
< |
register char *s; |
152 |
< |
register struct radhead *rh; |
150 |
> |
headline( /* grok a header line */ |
151 |
> |
char *s, |
152 |
> |
void *vrh |
153 |
> |
) |
154 |
|
{ |
155 |
< |
char fmt[32]; |
155 |
> |
char fmt[MAXFMTLEN]; |
156 |
> |
struct radhead *rh = vrh; |
157 |
|
|
158 |
|
if (formatval(fmt, s)) { |
159 |
|
if (!strcmp(fmt, COLRFMT)) |
162 |
|
rh->format = FMTCIE; |
163 |
|
else |
164 |
|
rh->format = FMTBAD; |
165 |
< |
return; |
165 |
> |
return(0); |
166 |
|
} |
167 |
|
if (isexpos(s)) { |
168 |
|
rh->expos *= exposval(s); |
169 |
< |
return; |
169 |
> |
return(0); |
170 |
|
} |
171 |
|
if (isprims(s)) { |
172 |
|
primsval(rh->mypri, s); |
173 |
|
rh->primp = rh->mypri; |
174 |
< |
return; |
174 |
> |
return(0); |
175 |
|
} |
176 |
+ |
return(0); |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
int |
181 |
< |
tmLoadPicture(lpp, cpp, xp, yp, fname, fp) /* convert Radiance picture */ |
182 |
< |
TMbright **lpp; |
183 |
< |
BYTE **cpp; |
184 |
< |
int *xp, *yp; |
185 |
< |
char *fname; |
186 |
< |
FILE *fp; |
181 |
> |
tmLoadPicture( /* convert Radiance picture */ |
182 |
> |
TMstruct *tms, |
183 |
> |
TMbright **lpp, |
184 |
> |
uby8 **cpp, |
185 |
> |
int *xp, |
186 |
> |
int *yp, |
187 |
> |
char *fname, |
188 |
> |
FILE *fp |
189 |
> |
) |
190 |
|
{ |
191 |
|
char *funcName = fname==NULL ? "tmLoadPicture" : fname; |
192 |
|
FILE *inpf; |
195 |
|
COLR *scanin = NULL; |
196 |
|
int i; |
197 |
|
/* check arguments */ |
198 |
< |
if (tmTop == NULL) |
198 |
> |
if (tms == NULL) |
199 |
|
returnErr(TM_E_TMINVAL); |
200 |
< |
if (lpp == NULL | xp == NULL | yp == NULL | |
201 |
< |
(fname == NULL & fp == TM_GETFILE)) |
200 |
> |
if ((lpp == NULL) | (xp == NULL) | (yp == NULL) | |
201 |
> |
((fname == NULL) & (fp == TM_GETFILE))) |
202 |
|
returnErr(TM_E_ILLEGAL); |
203 |
|
*xp = *yp = 0; /* error precaution */ |
204 |
< |
if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL) |
204 |
> |
if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "rb")) == NULL) |
205 |
|
returnErr(TM_E_BADFILE); |
206 |
+ |
*lpp = NULL; |
207 |
+ |
if (cpp != TM_NOCHROMP) *cpp = NULL; |
208 |
|
info = rhdefault; /* get our header */ |
209 |
< |
getheader(inpf, headline, (char *)&info); |
210 |
< |
if (info.format == FMTBAD | info.expos <= 0. || |
209 |
> |
getheader(inpf, headline, &info); |
210 |
> |
if ((info.format == FMTBAD) | (info.expos <= 0.) || |
211 |
|
fgetresolu(xp, yp, inpf) < 0) { |
212 |
|
err = TM_E_BADFILE; goto done; |
213 |
|
} |
218 |
|
else if (info.format == FMTCIE) |
219 |
|
info.primp = TM_XYZPRIM; |
220 |
|
/* prepare library */ |
221 |
< |
if ((err = tmSetSpace(info.primp, 1./info.expos)) != TM_E_OK) |
221 |
> |
if ((err = tmSetSpace(tms, info.primp, 1./info.expos, NULL)) != TM_E_OK) |
222 |
|
goto done; |
223 |
|
err = TM_E_NOMEM; /* allocate arrays */ |
224 |
|
*lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp); |
225 |
|
if (*lpp == NULL) |
226 |
|
goto done; |
227 |
|
if (cpp != TM_NOCHROMP) { |
228 |
< |
*cpp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); |
228 |
> |
*cpp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp); |
229 |
|
if (*cpp == NULL) |
230 |
|
goto done; |
231 |
|
} |
237 |
|
if (freadcolrs(scanin, *xp, inpf) < 0) { |
238 |
|
err = TM_E_BADFILE; break; |
239 |
|
} |
240 |
< |
err = tmCvColrs(*lpp + (i * *xp), |
240 |
> |
err = tmCvColrs(tms, *lpp + (i * *xp), |
241 |
|
cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp + (i * 3 * *xp), |
242 |
|
scanin, *xp); |
243 |
|
if (err != TM_E_OK) |
247 |
|
if (fp == NULL) |
248 |
|
fclose(inpf); |
249 |
|
if (scanin != NULL) |
250 |
< |
free((char *)scanin); |
251 |
< |
if (err != TM_E_OK) |
250 |
> |
free((MEM_PTR)scanin); |
251 |
> |
if (err != TM_E_OK) { |
252 |
> |
if (*lpp != NULL) |
253 |
> |
free((MEM_PTR)*lpp); |
254 |
> |
if (cpp != TM_NOCHROMP && *cpp != NULL) |
255 |
> |
free((MEM_PTR)*cpp); |
256 |
|
returnErr(err); |
257 |
+ |
} |
258 |
|
returnOK; |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
< |
int /* run pcond to map picture */ |
262 |
> |
#ifdef PCOND |
263 |
> |
static int /* run pcond to map picture */ |
264 |
|
dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname) |
265 |
< |
BYTE **psp; |
265 |
> |
uby8 **psp; |
266 |
|
int *xp, *yp; |
267 |
|
int flags; |
268 |
|
RGBPRIMP monpri; |
270 |
|
char *fname; |
271 |
|
{ |
272 |
|
char *funcName = fname; |
273 |
< |
char cmdbuf[512]; |
273 |
> |
TMstruct *tms = NULL; |
274 |
> |
char cmdbuf[1024]; |
275 |
|
FILE *infp; |
276 |
< |
register COLR *scan; |
277 |
< |
register BYTE *rp; |
276 |
> |
COLR *scan; |
277 |
> |
uby8 *rp; |
278 |
|
int y; |
279 |
< |
register int x; |
279 |
> |
int x; |
280 |
|
/* set up gamma correction */ |
281 |
|
if (setcolrcor(pow, 1./gamval) < 0) |
282 |
|
returnErr(TM_E_NOMEM); |
283 |
|
/* create command */ |
284 |
< |
strcpy(cmdbuf, "pcond "); |
284 |
> |
strcpy(cmdbuf, PCOND); |
285 |
|
if (flags & TM_F_HCONTR) |
286 |
< |
strcat(cmdbuf, "-s "); |
286 |
> |
strcat(cmdbuf, " -s"); |
287 |
|
if (flags & TM_F_MESOPIC) |
288 |
< |
strcat(cmdbuf, "-c "); |
288 |
> |
strcat(cmdbuf, " -c"); |
289 |
|
if (flags & TM_F_LINEAR) |
290 |
< |
strcat(cmdbuf, "-l "); |
290 |
> |
strcat(cmdbuf, " -l"); |
291 |
|
if (flags & TM_F_ACUITY) |
292 |
< |
strcat(cmdbuf, "-a "); |
292 |
> |
strcat(cmdbuf, " -a"); |
293 |
|
if (flags & TM_F_VEIL) |
294 |
< |
strcat(cmdbuf, "-v "); |
294 |
> |
strcat(cmdbuf, " -v"); |
295 |
|
if (flags & TM_F_CWEIGHT) |
296 |
< |
strcat(cmdbuf, "-w "); |
297 |
< |
sprintf(cmdbuf+strlen(cmdbuf), |
298 |
< |
"-p %f %f %f %f %f %f %f %f -d %f -u %f %s", |
299 |
< |
monpri[RED][CIEX], monpri[RED][CIEY], |
300 |
< |
monpri[GRN][CIEX], monpri[GRN][CIEY], |
301 |
< |
monpri[BLU][CIEX], monpri[BLU][CIEY], |
302 |
< |
monpri[WHT][CIEX], monpri[WHT][CIEY], |
303 |
< |
Lddyn, Ldmax, fname); |
296 |
> |
strcat(cmdbuf, " -w"); |
297 |
> |
if (monpri != stdprims) |
298 |
> |
sprintf(cmdbuf+strlen(cmdbuf), " -p %f %f %f %f %f %f %f %f", |
299 |
> |
monpri[RED][CIEX], monpri[RED][CIEY], |
300 |
> |
monpri[GRN][CIEX], monpri[GRN][CIEY], |
301 |
> |
monpri[BLU][CIEX], monpri[BLU][CIEY], |
302 |
> |
monpri[WHT][CIEX], monpri[WHT][CIEY]); |
303 |
> |
sprintf(cmdbuf+strlen(cmdbuf), " -d %f -u %f %s", Lddyn, Ldmax, fname); |
304 |
|
/* start pcond */ |
305 |
|
if ((infp = popen(cmdbuf, "r")) == NULL) |
306 |
|
returnErr(TM_E_BADFILE); |
313 |
|
/* allocate arrays */ |
314 |
|
scan = (COLR *)malloc(sizeof(COLR) * *xp); |
315 |
|
if (flags & TM_F_BW) |
316 |
< |
rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
316 |
> |
rp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp); |
317 |
|
else |
318 |
< |
rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); |
319 |
< |
if ((*psp = rp) == NULL | scan == NULL) { |
318 |
> |
rp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp); |
319 |
> |
if (((*psp = rp) == NULL) | (scan == NULL)) { |
320 |
|
pclose(infp); |
321 |
|
returnErr(TM_E_NOMEM); |
322 |
|
} |
324 |
|
for (y = 0; y < *yp; y++) { |
325 |
|
if (freadcolrs(scan, *xp, infp) < 0) { |
326 |
|
pclose(infp); |
327 |
< |
free((char *)scan); |
328 |
< |
free((char *)*psp); |
327 |
> |
free((MEM_PTR)scan); |
328 |
> |
free((MEM_PTR)*psp); |
329 |
|
*psp = NULL; |
330 |
|
returnErr(TM_E_BADFILE); |
331 |
|
} |
340 |
|
*rp++ = scan[x][BLU]; |
341 |
|
} |
342 |
|
} |
343 |
< |
free((char *)scan); |
343 |
> |
free((MEM_PTR)scan); |
344 |
|
pclose(infp); |
345 |
|
returnOK; |
346 |
|
} |
347 |
+ |
#endif |
348 |
|
|
349 |
|
|
350 |
|
int /* map a Radiance picture */ |
351 |
|
tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp) |
352 |
< |
BYTE **psp; |
352 |
> |
uby8 **psp; |
353 |
|
int *xp, *yp; |
354 |
|
int flags; |
355 |
|
RGBPRIMP monpri; |
358 |
|
FILE *fp; |
359 |
|
{ |
360 |
|
char *funcName = fname==NULL ? "tmMapPicture" : fname; |
361 |
< |
FILE *inpf; |
362 |
< |
BYTE *cp; |
361 |
> |
TMstruct *tms = NULL; |
362 |
> |
uby8 *cp; |
363 |
|
TMbright *lp; |
364 |
|
int err; |
365 |
|
/* check arguments */ |
366 |
< |
if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL | |
367 |
< |
(fname == NULL & fp == TM_GETFILE)) |
366 |
> |
if ((psp == NULL) | (xp == NULL) | (yp == NULL) | (monpri == NULL) | |
367 |
> |
((fname == NULL) & (fp == TM_GETFILE))) |
368 |
|
returnErr(TM_E_ILLEGAL); |
369 |
|
/* set defaults */ |
370 |
|
if (gamval < MINGAM) gamval = DEFGAM; |
371 |
|
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; |
372 |
|
if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX; |
373 |
|
if (flags & TM_F_BW) monpri = stdprims; |
374 |
+ |
#ifdef PCOND |
375 |
|
/* check for pcond run */ |
376 |
|
if (fp == TM_GETFILE && flags & TM_F_UNIMPL) |
377 |
|
return( dopcond(psp, xp, yp, flags, |
378 |
|
monpri, gamval, Lddyn, Ldmax, fname) ); |
379 |
+ |
#endif |
380 |
|
/* initialize tone mapping */ |
381 |
< |
if (tmInit(flags, monpri, gamval) == NULL) |
381 |
> |
if ((tms = tmInit(flags, monpri, gamval)) == NULL) |
382 |
|
returnErr(TM_E_NOMEM); |
383 |
|
/* load & convert picture */ |
384 |
< |
err = tmLoadPicture(&lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp, |
384 |
> |
err = tmLoadPicture(tms, &lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp, |
385 |
|
xp, yp, fname, fp); |
386 |
|
if (err != TM_E_OK) { |
387 |
< |
tmDone(NULL); |
387 |
> |
tmDone(tms); |
388 |
|
return(err); |
389 |
|
} |
390 |
|
/* allocate space for result */ |
391 |
|
if (flags & TM_F_BW) { |
392 |
< |
*psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
392 |
> |
*psp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp); |
393 |
|
if (*psp == NULL) { |
394 |
< |
free((char *)lp); |
395 |
< |
tmDone(NULL); |
394 |
> |
free((MEM_PTR)lp); |
395 |
> |
tmDone(tms); |
396 |
|
returnErr(TM_E_NOMEM); |
397 |
|
} |
398 |
|
cp = TM_NOCHROM; |
399 |
|
} else |
400 |
|
*psp = cp; |
401 |
|
/* compute color mapping */ |
402 |
< |
err = tmAddHisto(lp, *xp * *yp, 1); |
402 |
> |
err = tmAddHisto(tms, lp, *xp * *yp, 1); |
403 |
|
if (err != TM_E_OK) |
404 |
|
goto done; |
405 |
< |
err = tmComputeMapping(gamval, Lddyn, Ldmax); |
405 |
> |
err = tmComputeMapping(tms, gamval, Lddyn, Ldmax); |
406 |
|
if (err != TM_E_OK) |
407 |
|
goto done; |
408 |
|
/* map colors */ |
409 |
< |
err = tmMapPixels(*psp, lp, cp, *xp * *yp); |
409 |
> |
err = tmMapPixels(tms, *psp, lp, cp, *xp * *yp); |
410 |
|
|
411 |
|
done: /* clean up */ |
412 |
< |
free((char *)lp); |
413 |
< |
tmDone(NULL); |
412 |
> |
free((MEM_PTR)lp); |
413 |
> |
tmDone(tms); |
414 |
|
if (err != TM_E_OK) { /* free memory on error */ |
415 |
< |
free((char *)*psp); |
415 |
> |
free((MEM_PTR)*psp); |
416 |
|
*psp = NULL; |
417 |
|
returnErr(err); |
418 |
|
} |
422 |
|
|
423 |
|
static void |
424 |
|
colrNewSpace(tms) /* color space changed for tone mapping */ |
425 |
< |
register struct tmStruct *tms; |
425 |
> |
TMstruct *tms; |
426 |
|
{ |
427 |
< |
register COLRDATA *cd; |
427 |
> |
COLRDATA *cd; |
428 |
|
double d; |
429 |
+ |
int i, j; |
430 |
|
|
431 |
|
cd = (COLRDATA *)tms->pd[colrReg]; |
432 |
< |
cd->clfb[RED] = 256.*tms->clf[RED] + .5; |
433 |
< |
cd->clfb[GRN] = 256.*tms->clf[GRN] + .5; |
434 |
< |
cd->clfb[BLU] = 256.*tms->clf[BLU] + .5; |
435 |
< |
cd->clfb[EXP] = COLXS; |
436 |
< |
d = TM_BRTSCALE*log(tms->inpsf); |
437 |
< |
cd->inpsfb = d<0. ? d-.5 : d+.5; |
432 |
> |
for (i = 3; i--; ) |
433 |
> |
cd->clfb[i] = 0x1000*tms->clf[i] + .5; |
434 |
> |
cd->inpsfb = tmCvLuminance(tms->inpsf); |
435 |
> |
for (i = 3; i--; ) |
436 |
> |
for (j = 3; j--; ) { |
437 |
> |
d = tms->cmat[i][j] / tms->inpsf; |
438 |
> |
cd->cmatb[i][j] = 0x10000*d + (d<0. ? -.5 : .5); |
439 |
> |
} |
440 |
|
} |
441 |
|
|
442 |
|
|
443 |
|
static MEM_PTR |
444 |
|
colrInit(tms) /* initialize private data for tone mapping */ |
445 |
< |
register struct tmStruct *tms; |
445 |
> |
TMstruct *tms; |
446 |
|
{ |
447 |
< |
register COLRDATA *cd; |
448 |
< |
register int i; |
447 |
> |
COLRDATA *cd; |
448 |
> |
int i; |
449 |
|
/* allocate our data */ |
450 |
|
cd = (COLRDATA *)malloc(sizeof(COLRDATA)); |
451 |
|
if (cd == NULL) |