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 <string.h> |
14 |
|
#include <math.h> |
15 |
+ |
#include <time.h> |
16 |
|
#include "tmprivat.h" |
17 |
|
#include "resolu.h" |
18 |
|
|
19 |
+ |
#ifndef TM_PIC_CTRANS |
20 |
+ |
#define TM_PIC_CTRANS 1 /* transform colors? (expensive) */ |
21 |
+ |
#endif |
22 |
|
|
18 |
– |
extern char *tempbuffer(); |
19 |
– |
|
23 |
|
#define GAMTSZ 1024 |
24 |
|
|
25 |
|
typedef struct { |
28 |
|
TMbright inpsfb; /* encoded tm->inpsf */ |
29 |
|
} COLRDATA; |
30 |
|
|
31 |
< |
static MEM_PTR colrInit(); |
32 |
< |
static void colrNewSpace(); |
33 |
< |
extern void free(); |
31 |
> |
static MEM_PTR colrInit(struct tmStruct *); |
32 |
> |
static void colrNewSpace(struct tmStruct *); |
33 |
> |
static gethfunc headline; |
34 |
> |
|
35 |
|
static struct tmPackage colrPkg = { /* our package functions */ |
36 |
|
colrInit, colrNewSpace, free |
37 |
|
}; |
39 |
|
|
40 |
|
#define LOGISZ 260 |
41 |
|
static TMbright logi[LOGISZ]; |
38 |
– |
static BYTE photofact[BMESUPPER-BMESLOWER]; |
42 |
|
|
43 |
|
|
44 |
|
int |
45 |
< |
tmCvColrs(ls, cs, scan, len) /* tone map RGBE/XYZE colors */ |
45 |
> |
tmCvColrs(ls, cs, scan, len) /* convert RGBE/XYZE colors */ |
46 |
|
TMbright *ls; |
47 |
|
BYTE *cs; |
48 |
|
COLR *scan; |
55 |
|
|
56 |
|
if (tmTop == NULL) |
57 |
|
returnErr(TM_E_TMINVAL); |
58 |
< |
if (ls == NULL | scan == NULL | len < 0) |
58 |
> |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
59 |
|
returnErr(TM_E_ILLEGAL); |
60 |
+ |
#if TM_PIC_CTRANS |
61 |
|
if (tmNeedMatrix(tmTop)) { /* need floating point */ |
62 |
+ |
#else |
63 |
+ |
if (tmTop->inppri == TM_XYZPRIM) { /* no way around this */ |
64 |
+ |
#endif |
65 |
|
register COLOR *newscan; |
66 |
|
newscan = (COLOR *)tempbuffer(len*sizeof(COLOR)); |
67 |
|
if (newscan == NULL) |
70 |
|
colr_color(newscan[i], scan[i]); |
71 |
|
return(tmCvColors(ls, cs, newscan, len)); |
72 |
|
} |
73 |
< |
if (colrReg == -1) { /* build tables if necessary */ |
73 |
> |
if (colrReg < 0) { /* build tables if necessary */ |
74 |
|
colrReg = tmRegPkg(&colrPkg); |
75 |
|
if (colrReg < 0) |
76 |
|
returnErr(TM_E_CODERR1); |
77 |
|
for (i = 256; i--; ) |
78 |
|
logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5; |
79 |
|
for (i = 256; i < LOGISZ; i++) |
80 |
< |
logi[i] = logi[255]; |
81 |
< |
for (i = BMESLOWER; i < BMESUPPER; i++) |
75 |
< |
photofact[i-BMESLOWER] = 256. * |
76 |
< |
(tmLuminance(i) - LMESLOWER) / |
77 |
< |
(LMESUPPER - LMESLOWER); |
80 |
> |
logi[i] = 0; |
81 |
> |
tmMkMesofact(); |
82 |
|
} |
83 |
|
if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL) |
84 |
|
returnErr(TM_E_NOMEM); |
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) + |
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 */ |
93 |
> |
if (li <= 0) { |
94 |
> |
bi = TM_NOBRT; /* bogus value */ |
95 |
> |
li = 1; /* avoid li==0 */ |
96 |
|
} |
97 |
|
ls[i] = bi; |
98 |
|
if (cs == TM_NOCHROM) /* no color? */ |
99 |
|
continue; |
96 |
– |
if (tmTop->flags & TM_F_BW) |
97 |
– |
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
100 |
|
/* mesopic adj. */ |
101 |
|
if (tmTop->flags & TM_F_MESOPIC && bi < BMESUPPER) { |
102 |
|
register int pf, sli = normscot(cmon); |
103 |
|
if (bi < BMESLOWER) |
104 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = sli; |
105 |
|
else { |
106 |
< |
pf = photofact[bi-BMESLOWER]; |
106 |
> |
if (tmTop->flags & TM_F_BW) |
107 |
> |
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
108 |
> |
pf = tmMesofact[bi-BMESLOWER]; |
109 |
|
sli *= 256 - pf; |
110 |
|
cmon[RED] = ( sli + pf*cmon[RED] ) >> 8; |
111 |
|
cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8; |
112 |
|
cmon[BLU] = ( sli + pf*cmon[BLU] ) >> 8; |
113 |
|
} |
114 |
+ |
} else if (tmTop->flags & TM_F_BW) { |
115 |
+ |
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
116 |
|
} |
117 |
< |
bi = ( (int4)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; |
117 |
> |
bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; |
118 |
|
cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
119 |
< |
bi = ( (int4)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; |
119 |
> |
bi = ( (int32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; |
120 |
|
cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
121 |
< |
bi = ( (int4)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; |
121 |
> |
bi = ( (int32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; |
122 |
|
cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
123 |
|
} |
124 |
|
returnOK; |
139 |
|
|
140 |
|
|
141 |
|
static int |
142 |
< |
headline(s, rh) /* grok a header line */ |
143 |
< |
register char *s; |
144 |
< |
register struct radhead *rh; |
142 |
> |
headline( /* grok a header line */ |
143 |
> |
register char *s, |
144 |
> |
void *vrh |
145 |
> |
) |
146 |
|
{ |
147 |
|
char fmt[32]; |
148 |
+ |
register struct radhead *rh = vrh; |
149 |
|
|
150 |
|
if (formatval(fmt, s)) { |
151 |
|
if (!strcmp(fmt, COLRFMT)) |
154 |
|
rh->format = FMTCIE; |
155 |
|
else |
156 |
|
rh->format = FMTBAD; |
157 |
< |
return; |
157 |
> |
return(0); |
158 |
|
} |
159 |
|
if (isexpos(s)) { |
160 |
|
rh->expos *= exposval(s); |
161 |
< |
return; |
161 |
> |
return(0); |
162 |
|
} |
163 |
|
if (isprims(s)) { |
164 |
|
primsval(rh->mypri, s); |
165 |
|
rh->primp = rh->mypri; |
166 |
< |
return; |
166 |
> |
return(0); |
167 |
|
} |
168 |
+ |
return(0); |
169 |
|
} |
170 |
|
|
171 |
|
|
186 |
|
/* check arguments */ |
187 |
|
if (tmTop == NULL) |
188 |
|
returnErr(TM_E_TMINVAL); |
189 |
< |
if (lpp == NULL | xp == NULL | yp == NULL | |
190 |
< |
(fname == NULL & fp == TM_GETFILE)) |
189 |
> |
if ((lpp == NULL) | (xp == NULL) | (yp == NULL) | |
190 |
> |
((fname == NULL) & (fp == TM_GETFILE))) |
191 |
|
returnErr(TM_E_ILLEGAL); |
192 |
|
*xp = *yp = 0; /* error precaution */ |
193 |
|
if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL) |
194 |
|
returnErr(TM_E_BADFILE); |
195 |
+ |
*lpp = NULL; |
196 |
+ |
if (cpp != TM_NOCHROMP) *cpp = NULL; |
197 |
|
info = rhdefault; /* get our header */ |
198 |
< |
getheader(inpf, headline, (char *)&info); |
199 |
< |
if (info.format == FMTBAD | info.expos <= 0. || |
198 |
> |
getheader(inpf, headline, &info); |
199 |
> |
if ((info.format == FMTBAD) | (info.expos <= 0.) || |
200 |
|
fgetresolu(xp, yp, inpf) < 0) { |
201 |
|
err = TM_E_BADFILE; goto done; |
202 |
|
} |
236 |
|
if (fp == NULL) |
237 |
|
fclose(inpf); |
238 |
|
if (scanin != NULL) |
239 |
< |
free((char *)scanin); |
240 |
< |
if (err != TM_E_OK) |
239 |
> |
free((MEM_PTR)scanin); |
240 |
> |
if (err != TM_E_OK) { |
241 |
> |
if (*lpp != NULL) |
242 |
> |
free((MEM_PTR)*lpp); |
243 |
> |
if (cpp != TM_NOCHROMP && *cpp != NULL) |
244 |
> |
free((MEM_PTR)*cpp); |
245 |
|
returnErr(err); |
246 |
+ |
} |
247 |
|
returnOK; |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
< |
int /* run pcond to map picture */ |
251 |
> |
#ifdef PCOND |
252 |
> |
static int /* run pcond to map picture */ |
253 |
|
dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname) |
254 |
|
BYTE **psp; |
255 |
|
int *xp, *yp; |
259 |
|
char *fname; |
260 |
|
{ |
261 |
|
char *funcName = fname; |
262 |
< |
char cmdbuf[512]; |
262 |
> |
char cmdbuf[1024]; |
263 |
|
FILE *infp; |
264 |
|
register COLR *scan; |
265 |
|
register BYTE *rp; |
269 |
|
if (setcolrcor(pow, 1./gamval) < 0) |
270 |
|
returnErr(TM_E_NOMEM); |
271 |
|
/* create command */ |
272 |
< |
strcpy(cmdbuf, "pcond "); |
272 |
> |
strcpy(cmdbuf, PCOND); |
273 |
|
if (flags & TM_F_HCONTR) |
274 |
< |
strcat(cmdbuf, "-s "); |
274 |
> |
strcat(cmdbuf, " -s"); |
275 |
|
if (flags & TM_F_MESOPIC) |
276 |
< |
strcat(cmdbuf, "-c "); |
276 |
> |
strcat(cmdbuf, " -c"); |
277 |
|
if (flags & TM_F_LINEAR) |
278 |
< |
strcat(cmdbuf, "-l "); |
278 |
> |
strcat(cmdbuf, " -l"); |
279 |
|
if (flags & TM_F_ACUITY) |
280 |
< |
strcat(cmdbuf, "-a "); |
280 |
> |
strcat(cmdbuf, " -a"); |
281 |
|
if (flags & TM_F_VEIL) |
282 |
< |
strcat(cmdbuf, "-v "); |
282 |
> |
strcat(cmdbuf, " -v"); |
283 |
|
if (flags & TM_F_CWEIGHT) |
284 |
< |
strcat(cmdbuf, "-w "); |
285 |
< |
sprintf(cmdbuf+strlen(cmdbuf), |
286 |
< |
"-p %f %f %f %f %f %f %f %f -d %f -u %f %s", |
287 |
< |
monpri[RED][CIEX], monpri[RED][CIEY], |
288 |
< |
monpri[GRN][CIEX], monpri[GRN][CIEY], |
289 |
< |
monpri[BLU][CIEX], monpri[BLU][CIEY], |
290 |
< |
monpri[WHT][CIEX], monpri[WHT][CIEY], |
291 |
< |
Lddyn, Ldmax, fname); |
284 |
> |
strcat(cmdbuf, " -w"); |
285 |
> |
if (monpri != stdprims) |
286 |
> |
sprintf(cmdbuf+strlen(cmdbuf), " -p %f %f %f %f %f %f %f %f", |
287 |
> |
monpri[RED][CIEX], monpri[RED][CIEY], |
288 |
> |
monpri[GRN][CIEX], monpri[GRN][CIEY], |
289 |
> |
monpri[BLU][CIEX], monpri[BLU][CIEY], |
290 |
> |
monpri[WHT][CIEX], monpri[WHT][CIEY]); |
291 |
> |
sprintf(cmdbuf+strlen(cmdbuf), " -d %f -u %f %s", Lddyn, Ldmax, fname); |
292 |
|
/* start pcond */ |
293 |
|
if ((infp = popen(cmdbuf, "r")) == NULL) |
294 |
|
returnErr(TM_E_BADFILE); |
304 |
|
rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
305 |
|
else |
306 |
|
rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); |
307 |
< |
if ((*psp = rp) == NULL | scan == NULL) { |
307 |
> |
if (((*psp = rp) == NULL) | (scan == NULL)) { |
308 |
|
pclose(infp); |
309 |
|
returnErr(TM_E_NOMEM); |
310 |
|
} |
312 |
|
for (y = 0; y < *yp; y++) { |
313 |
|
if (freadcolrs(scan, *xp, infp) < 0) { |
314 |
|
pclose(infp); |
315 |
< |
free((char *)scan); |
316 |
< |
free((char *)*psp); |
315 |
> |
free((MEM_PTR)scan); |
316 |
> |
free((MEM_PTR)*psp); |
317 |
|
*psp = NULL; |
318 |
|
returnErr(TM_E_BADFILE); |
319 |
|
} |
328 |
|
*rp++ = scan[x][BLU]; |
329 |
|
} |
330 |
|
} |
331 |
< |
free((char *)scan); |
331 |
> |
free((MEM_PTR)scan); |
332 |
|
pclose(infp); |
333 |
|
returnOK; |
334 |
|
} |
335 |
+ |
#endif |
336 |
|
|
337 |
|
|
338 |
|
int /* map a Radiance picture */ |
346 |
|
FILE *fp; |
347 |
|
{ |
348 |
|
char *funcName = fname==NULL ? "tmMapPicture" : fname; |
331 |
– |
FILE *inpf; |
349 |
|
BYTE *cp; |
350 |
|
TMbright *lp; |
351 |
|
int err; |
352 |
|
/* check arguments */ |
353 |
< |
if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL | |
354 |
< |
(fname == NULL & fp == TM_GETFILE)) |
353 |
> |
if ((psp == NULL) | (xp == NULL) | (yp == NULL) | (monpri == NULL) | |
354 |
> |
((fname == NULL) & (fp == TM_GETFILE))) |
355 |
|
returnErr(TM_E_ILLEGAL); |
356 |
|
/* set defaults */ |
357 |
|
if (gamval < MINGAM) gamval = DEFGAM; |
358 |
|
if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; |
359 |
|
if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX; |
360 |
|
if (flags & TM_F_BW) monpri = stdprims; |
361 |
+ |
#ifdef PCOND |
362 |
|
/* check for pcond run */ |
363 |
|
if (fp == TM_GETFILE && flags & TM_F_UNIMPL) |
364 |
|
return( dopcond(psp, xp, yp, flags, |
365 |
|
monpri, gamval, Lddyn, Ldmax, fname) ); |
366 |
+ |
#endif |
367 |
|
/* initialize tone mapping */ |
368 |
|
if (tmInit(flags, monpri, gamval) == NULL) |
369 |
|
returnErr(TM_E_NOMEM); |
378 |
|
if (flags & TM_F_BW) { |
379 |
|
*psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
380 |
|
if (*psp == NULL) { |
381 |
< |
free((char *)lp); |
381 |
> |
free((MEM_PTR)lp); |
382 |
|
tmDone(NULL); |
383 |
|
returnErr(TM_E_NOMEM); |
384 |
|
} |
396 |
|
err = tmMapPixels(*psp, lp, cp, *xp * *yp); |
397 |
|
|
398 |
|
done: /* clean up */ |
399 |
< |
free((char *)lp); |
399 |
> |
free((MEM_PTR)lp); |
400 |
|
tmDone(NULL); |
401 |
|
if (err != TM_E_OK) { /* free memory on error */ |
402 |
< |
free((char *)*psp); |
402 |
> |
free((MEM_PTR)*psp); |
403 |
|
*psp = NULL; |
404 |
|
returnErr(err); |
405 |
|
} |