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 |
+ |
|
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 |
|
|
18 |
– |
extern char *tempbuffer(); |
19 |
– |
|
25 |
|
#define GAMTSZ 1024 |
26 |
|
|
27 |
|
typedef struct { |
30 |
|
TMbright inpsfb; /* encoded tm->inpsf */ |
31 |
|
} COLRDATA; |
32 |
|
|
33 |
< |
static MEM_PTR colrInit(); |
34 |
< |
static void colrNewSpace(); |
35 |
< |
extern void free(); |
33 |
> |
static MEM_PTR colrInit(TMstruct *); |
34 |
> |
static void colrNewSpace(TMstruct *); |
35 |
> |
static gethfunc headline; |
36 |
> |
|
37 |
|
static struct tmPackage colrPkg = { /* our package functions */ |
38 |
|
colrInit, colrNewSpace, free |
39 |
|
}; |
41 |
|
|
42 |
|
#define LOGISZ 260 |
43 |
|
static TMbright logi[LOGISZ]; |
38 |
– |
static BYTE photofact[BMESUPPER-BMESLOWER]; |
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; |
47 |
> |
tmCvColrs( /* convert RGBE/XYZE colors */ |
48 |
> |
TMstruct *tms, |
49 |
> |
TMbright *ls, |
50 |
> |
BYTE *cs, |
51 |
> |
COLR *scan, |
52 |
> |
int len |
53 |
> |
) |
54 |
|
{ |
55 |
< |
static char funcName[] = "tmCvColrs"; |
55 |
> |
static const char funcName[] = "tmCvColrs"; |
56 |
|
COLR cmon; |
57 |
|
register COLRDATA *cd; |
58 |
|
register int i, bi, li; |
59 |
|
|
60 |
< |
if (tmTop == NULL) |
60 |
> |
if (tms == NULL) |
61 |
|
returnErr(TM_E_TMINVAL); |
62 |
< |
if (ls == NULL | scan == NULL | len < 0) |
62 |
> |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
63 |
|
returnErr(TM_E_ILLEGAL); |
64 |
< |
if (tmNeedMatrix(tmTop)) { /* need floating point */ |
64 |
> |
#if TM_PIC_CTRANS |
65 |
> |
if (tmNeedMatrix(tms)) { /* need floating point */ |
66 |
> |
#else |
67 |
> |
if (tms->inppri == TM_XYZPRIM) { /* no way around this */ |
68 |
> |
#endif |
69 |
|
register COLOR *newscan; |
70 |
|
newscan = (COLOR *)tempbuffer(len*sizeof(COLOR)); |
71 |
|
if (newscan == NULL) |
72 |
|
returnErr(TM_E_NOMEM); |
73 |
|
for (i = len; i--; ) |
74 |
|
colr_color(newscan[i], scan[i]); |
75 |
< |
return(tmCvColors(ls, cs, newscan, len)); |
75 |
> |
return(tmCvColors(tms, ls, cs, newscan, len)); |
76 |
|
} |
77 |
|
if (colrReg < 0) { /* build tables if necessary */ |
78 |
|
colrReg = tmRegPkg(&colrPkg); |
81 |
|
for (i = 256; i--; ) |
82 |
|
logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5; |
83 |
|
for (i = 256; i < LOGISZ; i++) |
84 |
< |
logi[i] = logi[255]; |
85 |
< |
for (i = BMESLOWER; i < BMESUPPER; i++) |
75 |
< |
photofact[i-BMESLOWER] = 256. * |
76 |
< |
(tmLuminance(i) - LMESLOWER) / |
77 |
< |
(LMESUPPER - LMESLOWER); |
84 |
> |
logi[i] = 0; |
85 |
> |
tmMkMesofact(); |
86 |
|
} |
87 |
< |
if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL) |
87 |
> |
if ((cd = (COLRDATA *)tmPkgData(tms,colrReg)) == NULL) |
88 |
|
returnErr(TM_E_NOMEM); |
89 |
|
for (i = len; i--; ) { |
90 |
|
copycolr(cmon, scan[i]); |
92 |
|
li = ( cd->clfb[RED]*cmon[RED] + |
93 |
|
cd->clfb[GRN]*cmon[GRN] + |
94 |
|
cd->clfb[BLU]*cmon[BLU] ) >> 8; |
95 |
< |
bi = BRT2SCALE*(cmon[EXP]-COLXS) + |
95 |
> |
bi = BRT2SCALE(cmon[EXP]-COLXS) + |
96 |
|
logi[li] + cd->inpsfb; |
97 |
< |
if (bi < MINBRT) { |
98 |
< |
bi = MINBRT-1; /* bogus value */ |
99 |
< |
li++; /* avoid li==0 */ |
97 |
> |
if (li <= 0) { |
98 |
> |
bi = TM_NOBRT; /* bogus value */ |
99 |
> |
li = 1; /* avoid li==0 */ |
100 |
|
} |
101 |
|
ls[i] = bi; |
102 |
|
if (cs == TM_NOCHROM) /* no color? */ |
103 |
|
continue; |
104 |
|
/* mesopic adj. */ |
105 |
< |
if (tmTop->flags & TM_F_MESOPIC && bi < BMESUPPER) { |
105 |
> |
if (tms->flags & TM_F_MESOPIC && bi < BMESUPPER) { |
106 |
|
register int pf, sli = normscot(cmon); |
107 |
|
if (bi < BMESLOWER) |
108 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = sli; |
109 |
|
else { |
110 |
< |
if (tmTop->flags & TM_F_BW) |
110 |
> |
if (tms->flags & TM_F_BW) |
111 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
112 |
< |
pf = photofact[bi-BMESLOWER]; |
112 |
> |
pf = tmMesofact[bi-BMESLOWER]; |
113 |
|
sli *= 256 - pf; |
114 |
|
cmon[RED] = ( sli + pf*cmon[RED] ) >> 8; |
115 |
|
cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8; |
116 |
|
cmon[BLU] = ( sli + pf*cmon[BLU] ) >> 8; |
117 |
|
} |
118 |
< |
} else if (tmTop->flags & TM_F_BW) { |
118 |
> |
} else if (tms->flags & TM_F_BW) { |
119 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
120 |
|
} |
121 |
< |
bi = ( (int4)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; |
121 |
> |
bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; |
122 |
|
cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
123 |
< |
bi = ( (int4)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; |
123 |
> |
bi = ( (int32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; |
124 |
|
cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
125 |
< |
bi = ( (int4)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; |
125 |
> |
bi = ( (int32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; |
126 |
|
cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
127 |
|
} |
128 |
|
returnOK; |
143 |
|
|
144 |
|
|
145 |
|
static int |
146 |
< |
headline(s, rh) /* grok a header line */ |
147 |
< |
register char *s; |
148 |
< |
register struct radhead *rh; |
146 |
> |
headline( /* grok a header line */ |
147 |
> |
register char *s, |
148 |
> |
void *vrh |
149 |
> |
) |
150 |
|
{ |
151 |
|
char fmt[32]; |
152 |
+ |
register struct radhead *rh = vrh; |
153 |
|
|
154 |
|
if (formatval(fmt, s)) { |
155 |
|
if (!strcmp(fmt, COLRFMT)) |
174 |
|
|
175 |
|
|
176 |
|
int |
177 |
< |
tmLoadPicture(lpp, cpp, xp, yp, fname, fp) /* convert Radiance picture */ |
178 |
< |
TMbright **lpp; |
179 |
< |
BYTE **cpp; |
180 |
< |
int *xp, *yp; |
181 |
< |
char *fname; |
182 |
< |
FILE *fp; |
177 |
> |
tmLoadPicture( /* convert Radiance picture */ |
178 |
> |
TMstruct *tms, |
179 |
> |
TMbright **lpp, |
180 |
> |
BYTE **cpp, |
181 |
> |
int *xp, |
182 |
> |
int *yp, |
183 |
> |
char *fname, |
184 |
> |
FILE *fp |
185 |
> |
) |
186 |
|
{ |
187 |
|
char *funcName = fname==NULL ? "tmLoadPicture" : fname; |
188 |
|
FILE *inpf; |
191 |
|
COLR *scanin = NULL; |
192 |
|
int i; |
193 |
|
/* check arguments */ |
194 |
< |
if (tmTop == NULL) |
194 |
> |
if (tms == NULL) |
195 |
|
returnErr(TM_E_TMINVAL); |
196 |
< |
if (lpp == NULL | xp == NULL | yp == NULL | |
197 |
< |
(fname == NULL & fp == TM_GETFILE)) |
196 |
> |
if ((lpp == NULL) | (xp == NULL) | (yp == NULL) | |
197 |
> |
((fname == NULL) & (fp == TM_GETFILE))) |
198 |
|
returnErr(TM_E_ILLEGAL); |
199 |
|
*xp = *yp = 0; /* error precaution */ |
200 |
|
if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL) |
202 |
|
*lpp = NULL; |
203 |
|
if (cpp != TM_NOCHROMP) *cpp = NULL; |
204 |
|
info = rhdefault; /* get our header */ |
205 |
< |
getheader(inpf, headline, (MEM_PTR)&info); |
206 |
< |
if (info.format == FMTBAD | info.expos <= 0. || |
205 |
> |
getheader(inpf, headline, &info); |
206 |
> |
if ((info.format == FMTBAD) | (info.expos <= 0.) || |
207 |
|
fgetresolu(xp, yp, inpf) < 0) { |
208 |
|
err = TM_E_BADFILE; goto done; |
209 |
|
} |
214 |
|
else if (info.format == FMTCIE) |
215 |
|
info.primp = TM_XYZPRIM; |
216 |
|
/* prepare library */ |
217 |
< |
if ((err = tmSetSpace(info.primp, 1./info.expos)) != TM_E_OK) |
217 |
> |
if ((err = tmSetSpace(tms, info.primp, 1./info.expos)) != TM_E_OK) |
218 |
|
goto done; |
219 |
|
err = TM_E_NOMEM; /* allocate arrays */ |
220 |
|
*lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp); |
233 |
|
if (freadcolrs(scanin, *xp, inpf) < 0) { |
234 |
|
err = TM_E_BADFILE; break; |
235 |
|
} |
236 |
< |
err = tmCvColrs(*lpp + (i * *xp), |
236 |
> |
err = tmCvColrs(tms, *lpp + (i * *xp), |
237 |
|
cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp + (i * 3 * *xp), |
238 |
|
scanin, *xp); |
239 |
|
if (err != TM_E_OK) |
256 |
|
|
257 |
|
|
258 |
|
#ifdef PCOND |
259 |
< |
int /* run pcond to map picture */ |
259 |
> |
static int /* run pcond to map picture */ |
260 |
|
dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname) |
261 |
|
BYTE **psp; |
262 |
|
int *xp, *yp; |
266 |
|
char *fname; |
267 |
|
{ |
268 |
|
char *funcName = fname; |
269 |
< |
char cmdbuf[512]; |
269 |
> |
TMstruct *tms = NULL; |
270 |
> |
char cmdbuf[1024]; |
271 |
|
FILE *infp; |
272 |
|
register COLR *scan; |
273 |
|
register BYTE *rp; |
312 |
|
rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
313 |
|
else |
314 |
|
rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); |
315 |
< |
if ((*psp = rp) == NULL | scan == NULL) { |
315 |
> |
if (((*psp = rp) == NULL) | (scan == NULL)) { |
316 |
|
pclose(infp); |
317 |
|
returnErr(TM_E_NOMEM); |
318 |
|
} |
354 |
|
FILE *fp; |
355 |
|
{ |
356 |
|
char *funcName = fname==NULL ? "tmMapPicture" : fname; |
357 |
+ |
TMstruct *tms; |
358 |
|
BYTE *cp; |
359 |
|
TMbright *lp; |
360 |
|
int err; |
361 |
|
/* check arguments */ |
362 |
< |
if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL | |
363 |
< |
(fname == NULL & fp == TM_GETFILE)) |
362 |
> |
if ((psp == NULL) | (xp == NULL) | (yp == NULL) | (monpri == NULL) | |
363 |
> |
((fname == NULL) & (fp == TM_GETFILE))) |
364 |
|
returnErr(TM_E_ILLEGAL); |
365 |
|
/* set defaults */ |
366 |
|
if (gamval < MINGAM) gamval = DEFGAM; |
374 |
|
monpri, gamval, Lddyn, Ldmax, fname) ); |
375 |
|
#endif |
376 |
|
/* initialize tone mapping */ |
377 |
< |
if (tmInit(flags, monpri, gamval) == NULL) |
377 |
> |
if ((tms = tmInit(flags, monpri, gamval)) == NULL) |
378 |
|
returnErr(TM_E_NOMEM); |
379 |
|
/* load & convert picture */ |
380 |
< |
err = tmLoadPicture(&lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp, |
380 |
> |
err = tmLoadPicture(tms, &lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp, |
381 |
|
xp, yp, fname, fp); |
382 |
|
if (err != TM_E_OK) { |
383 |
< |
tmDone(NULL); |
383 |
> |
tmDone(tms); |
384 |
|
return(err); |
385 |
|
} |
386 |
|
/* allocate space for result */ |
388 |
|
*psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
389 |
|
if (*psp == NULL) { |
390 |
|
free((MEM_PTR)lp); |
391 |
< |
tmDone(NULL); |
391 |
> |
tmDone(tms); |
392 |
|
returnErr(TM_E_NOMEM); |
393 |
|
} |
394 |
|
cp = TM_NOCHROM; |
395 |
|
} else |
396 |
|
*psp = cp; |
397 |
|
/* compute color mapping */ |
398 |
< |
err = tmAddHisto(lp, *xp * *yp, 1); |
398 |
> |
err = tmAddHisto(tms, lp, *xp * *yp, 1); |
399 |
|
if (err != TM_E_OK) |
400 |
|
goto done; |
401 |
< |
err = tmComputeMapping(gamval, Lddyn, Ldmax); |
401 |
> |
err = tmComputeMapping(tms, gamval, Lddyn, Ldmax); |
402 |
|
if (err != TM_E_OK) |
403 |
|
goto done; |
404 |
|
/* map colors */ |
405 |
< |
err = tmMapPixels(*psp, lp, cp, *xp * *yp); |
405 |
> |
err = tmMapPixels(tms, *psp, lp, cp, *xp * *yp); |
406 |
|
|
407 |
|
done: /* clean up */ |
408 |
|
free((MEM_PTR)lp); |
409 |
< |
tmDone(NULL); |
409 |
> |
tmDone(tms); |
410 |
|
if (err != TM_E_OK) { /* free memory on error */ |
411 |
|
free((MEM_PTR)*psp); |
412 |
|
*psp = NULL; |
418 |
|
|
419 |
|
static void |
420 |
|
colrNewSpace(tms) /* color space changed for tone mapping */ |
421 |
< |
register struct tmStruct *tms; |
421 |
> |
register TMstruct *tms; |
422 |
|
{ |
423 |
|
register COLRDATA *cd; |
424 |
|
double d; |
435 |
|
|
436 |
|
static MEM_PTR |
437 |
|
colrInit(tms) /* initialize private data for tone mapping */ |
438 |
< |
register struct tmStruct *tms; |
438 |
> |
register TMstruct *tms; |
439 |
|
{ |
440 |
|
register COLRDATA *cd; |
441 |
|
register int i; |