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