1 |
– |
/* Copyright (c) 1997 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
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 |
+ |
|
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 |
< |
extern char *tempbuffer(); |
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]; |
22 |
– |
static BYTE photofact[BMESUPPER-BMESLOWER]; |
44 |
|
|
45 |
|
|
46 |
|
int |
47 |
< |
tmCvColrs(ls, cs, scan, len) /* tone map RGBE/XYZE colors */ |
47 |
> |
tmCvColrs(ls, cs, scan, len) /* convert RGBE/XYZE colors */ |
48 |
|
TMbright *ls; |
49 |
|
BYTE *cs; |
50 |
|
COLR *scan; |
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) |
60 |
> |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
61 |
|
returnErr(TM_E_ILLEGAL); |
62 |
< |
if (tmTop->flags & TM_F_NEEDMAT) { /* need floating point */ |
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) |
72 |
|
colr_color(newscan[i], scan[i]); |
73 |
|
return(tmCvColors(ls, cs, newscan, len)); |
74 |
|
} |
75 |
< |
if (logi[0] == 0) { /* build tables if necessary */ |
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] = logi[255]; |
83 |
< |
for (i = BMESLOWER; i < BMESUPPER; i++) |
55 |
< |
photofact[i-BMESLOWER] = 256. * |
56 |
< |
(tmLuminance(i) - LMESLOWER) / |
57 |
< |
(LMESUPPER - LMESLOWER); |
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 = ( tmTop->clfb[RED]*cmon[RED] + |
91 |
< |
tmTop->clfb[GRN]*cmon[GRN] + |
92 |
< |
tmTop->clfb[BLU]*cmon[BLU] ) >> 8; |
93 |
< |
bi = BRT2SCALE*(cmon[EXP]-COLXS) + |
94 |
< |
logi[li] + tmTop->inpsfb; |
95 |
< |
if (bi < MINBRT) { |
96 |
< |
bi = MINBRT-1; /* bogus value */ |
97 |
< |
li++; /* avoid li==0 */ |
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? */ |
107 |
|
else { |
108 |
|
if (tmTop->flags & TM_F_BW) |
109 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
110 |
< |
pf = photofact[bi-BMESLOWER]; |
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; |
116 |
|
} else if (tmTop->flags & TM_F_BW) { |
117 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
118 |
|
} |
119 |
< |
bi = ( (int4)TM_GAMTSZ*tmTop->clfb[RED]*cmon[RED]/li ) >> 8; |
120 |
< |
cs[3*i ] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi]; |
121 |
< |
bi = ( (int4)TM_GAMTSZ*tmTop->clfb[GRN]*cmon[GRN]/li ) >> 8; |
122 |
< |
cs[3*i+1] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi]; |
123 |
< |
bi = ( (int4)TM_GAMTSZ*tmTop->clfb[BLU]*cmon[BLU]/li ) >> 8; |
124 |
< |
cs[3*i+2] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi]; |
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 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
static int |
144 |
< |
headline(s, rh) /* grok a header line */ |
145 |
< |
register char *s; |
146 |
< |
register struct radhead *rh; |
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)) |
156 |
|
rh->format = FMTCIE; |
157 |
|
else |
158 |
|
rh->format = FMTBAD; |
159 |
< |
return; |
159 |
> |
return(0); |
160 |
|
} |
161 |
|
if (isexpos(s)) { |
162 |
|
rh->expos *= exposval(s); |
163 |
< |
return; |
163 |
> |
return(0); |
164 |
|
} |
165 |
|
if (isprims(s)) { |
166 |
|
primsval(rh->mypri, s); |
167 |
|
rh->primp = rh->mypri; |
168 |
< |
return; |
168 |
> |
return(0); |
169 |
|
} |
170 |
+ |
return(0); |
171 |
|
} |
172 |
|
|
173 |
|
|
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)) |
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, (char *)&info); |
201 |
< |
if (info.format == FMTBAD | info.expos <= 0. || |
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 |
|
} |
238 |
|
if (fp == NULL) |
239 |
|
fclose(inpf); |
240 |
|
if (scanin != NULL) |
241 |
< |
free((char *)scanin); |
242 |
< |
if (err != TM_E_OK) |
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 |
< |
int /* run pcond to map picture */ |
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; |
261 |
|
char *fname; |
262 |
|
{ |
263 |
|
char *funcName = fname; |
264 |
< |
char cmdbuf[512]; |
264 |
> |
char cmdbuf[1024]; |
265 |
|
FILE *infp; |
266 |
|
register COLR *scan; |
267 |
|
register BYTE *rp; |
271 |
|
if (setcolrcor(pow, 1./gamval) < 0) |
272 |
|
returnErr(TM_E_NOMEM); |
273 |
|
/* create command */ |
274 |
< |
strcpy(cmdbuf, "pcond "); |
274 |
> |
strcpy(cmdbuf, PCOND); |
275 |
|
if (flags & TM_F_HCONTR) |
276 |
< |
strcat(cmdbuf, "-s "); |
276 |
> |
strcat(cmdbuf, " -s"); |
277 |
|
if (flags & TM_F_MESOPIC) |
278 |
< |
strcat(cmdbuf, "-c "); |
278 |
> |
strcat(cmdbuf, " -c"); |
279 |
|
if (flags & TM_F_LINEAR) |
280 |
< |
strcat(cmdbuf, "-l "); |
280 |
> |
strcat(cmdbuf, " -l"); |
281 |
|
if (flags & TM_F_ACUITY) |
282 |
< |
strcat(cmdbuf, "-a "); |
282 |
> |
strcat(cmdbuf, " -a"); |
283 |
|
if (flags & TM_F_VEIL) |
284 |
< |
strcat(cmdbuf, "-v "); |
284 |
> |
strcat(cmdbuf, " -v"); |
285 |
|
if (flags & TM_F_CWEIGHT) |
286 |
< |
strcat(cmdbuf, "-w "); |
287 |
< |
sprintf(cmdbuf+strlen(cmdbuf), |
288 |
< |
"-p %f %f %f %f %f %f %f %f -d %f -u %f %s", |
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 |
< |
Lddyn, Ldmax, fname); |
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); |
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 |
< |
scan = (COLR *)malloc(sizeof(COLR) * *xp); |
270 |
< |
if ((*psp = rp) == NULL | scan == NULL) { |
309 |
> |
if (((*psp = rp) == NULL) | (scan == NULL)) { |
310 |
|
pclose(infp); |
311 |
|
returnErr(TM_E_NOMEM); |
312 |
|
} |
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); |
330 |
|
*rp++ = scan[x][BLU]; |
331 |
|
} |
332 |
|
} |
333 |
< |
free((char *)scan); |
333 |
> |
free((MEM_PTR)scan); |
334 |
|
pclose(infp); |
335 |
|
returnOK; |
336 |
|
} |
337 |
+ |
#endif |
338 |
|
|
339 |
|
|
340 |
|
int /* map a Radiance picture */ |
348 |
|
FILE *fp; |
349 |
|
{ |
350 |
|
char *funcName = fname==NULL ? "tmMapPicture" : fname; |
308 |
– |
FILE *inpf; |
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)) |
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); |
379 |
|
/* allocate space for result */ |
380 |
|
if (flags & TM_F_BW) { |
381 |
|
*psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
382 |
< |
if (*psp == NULL) |
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; |
398 |
|
err = tmMapPixels(*psp, lp, cp, *xp * *yp); |
399 |
|
|
400 |
|
done: /* clean up */ |
401 |
< |
free((char *)lp); |
401 |
> |
free((MEM_PTR)lp); |
402 |
|
tmDone(NULL); |
403 |
|
if (err != TM_E_OK) { /* free memory on error */ |
404 |
< |
free((char *)*psp); |
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 |
|
} |