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 |
+ |
#ifdef NOPROTO |
32 |
|
static MEM_PTR colrInit(); |
33 |
|
static void colrNewSpace(); |
34 |
< |
extern void free(); |
34 |
> |
#else |
35 |
> |
static MEM_PTR colrInit(struct tmStruct *); |
36 |
> |
static void colrNewSpace(struct tmStruct *); |
37 |
> |
#endif |
38 |
|
static struct tmPackage colrPkg = { /* our package functions */ |
39 |
|
colrInit, colrNewSpace, free |
40 |
|
}; |
42 |
|
|
43 |
|
#define LOGISZ 260 |
44 |
|
static TMbright logi[LOGISZ]; |
38 |
– |
static BYTE photofact[BMESUPPER-BMESLOWER]; |
45 |
|
|
46 |
|
|
47 |
|
int |
58 |
|
|
59 |
|
if (tmTop == NULL) |
60 |
|
returnErr(TM_E_TMINVAL); |
61 |
< |
if (ls == NULL | scan == NULL | len < 0) |
61 |
> |
if ((ls == NULL) | (scan == NULL) | (len < 0)) |
62 |
|
returnErr(TM_E_ILLEGAL); |
63 |
+ |
#if TM_PIC_CTRANS |
64 |
|
if (tmNeedMatrix(tmTop)) { /* need floating point */ |
65 |
+ |
#else |
66 |
+ |
if (tmTop->inppri == TM_XYZPRIM) { /* no way around this */ |
67 |
+ |
#endif |
68 |
|
register COLOR *newscan; |
69 |
|
newscan = (COLOR *)tempbuffer(len*sizeof(COLOR)); |
70 |
|
if (newscan == NULL) |
80 |
|
for (i = 256; i--; ) |
81 |
|
logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5; |
82 |
|
for (i = 256; i < LOGISZ; i++) |
83 |
< |
logi[i] = logi[255]; |
84 |
< |
for (i = BMESLOWER; i < BMESUPPER; i++) |
75 |
< |
photofact[i-BMESLOWER] = 256. * |
76 |
< |
(tmLuminance(i) - LMESLOWER) / |
77 |
< |
(LMESUPPER - LMESLOWER); |
83 |
> |
logi[i] = 0; |
84 |
> |
tmMkMesofact(); |
85 |
|
} |
86 |
|
if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL) |
87 |
|
returnErr(TM_E_NOMEM); |
91 |
|
li = ( cd->clfb[RED]*cmon[RED] + |
92 |
|
cd->clfb[GRN]*cmon[GRN] + |
93 |
|
cd->clfb[BLU]*cmon[BLU] ) >> 8; |
94 |
< |
bi = BRT2SCALE*(cmon[EXP]-COLXS) + |
94 |
> |
bi = BRT2SCALE(cmon[EXP]-COLXS) + |
95 |
|
logi[li] + cd->inpsfb; |
96 |
< |
if (bi < MINBRT) { |
97 |
< |
bi = MINBRT-1; /* bogus value */ |
98 |
< |
li++; /* avoid li==0 */ |
96 |
> |
if (li <= 0) { |
97 |
> |
bi = TM_NOBRT; /* bogus value */ |
98 |
> |
li = 1; /* avoid li==0 */ |
99 |
|
} |
100 |
|
ls[i] = bi; |
101 |
|
if (cs == TM_NOCHROM) /* no color? */ |
108 |
|
else { |
109 |
|
if (tmTop->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; |
117 |
|
} else if (tmTop->flags & TM_F_BW) { |
118 |
|
cmon[RED] = cmon[GRN] = cmon[BLU] = li; |
119 |
|
} |
120 |
< |
bi = ( (int4)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; |
120 |
> |
bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; |
121 |
|
cs[3*i ] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
122 |
< |
bi = ( (int4)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; |
122 |
> |
bi = ( (int32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; |
123 |
|
cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
124 |
< |
bi = ( (int4)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; |
124 |
> |
bi = ( (int32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; |
125 |
|
cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; |
126 |
|
} |
127 |
|
returnOK; |
187 |
|
/* check arguments */ |
188 |
|
if (tmTop == NULL) |
189 |
|
returnErr(TM_E_TMINVAL); |
190 |
< |
if (lpp == NULL | xp == NULL | yp == NULL | |
191 |
< |
(fname == NULL & fp == TM_GETFILE)) |
190 |
> |
if ((lpp == NULL) | (xp == NULL) | (yp == NULL) | |
191 |
> |
((fname == NULL) & (fp == TM_GETFILE))) |
192 |
|
returnErr(TM_E_ILLEGAL); |
193 |
|
*xp = *yp = 0; /* error precaution */ |
194 |
|
if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL) |
196 |
|
*lpp = NULL; |
197 |
|
if (cpp != TM_NOCHROMP) *cpp = NULL; |
198 |
|
info = rhdefault; /* get our header */ |
199 |
< |
getheader(inpf, headline, (MEM_PTR)&info); |
200 |
< |
if (info.format == FMTBAD | info.expos <= 0. || |
199 |
> |
getheader(inpf, headline, (char *)&info); |
200 |
> |
if ((info.format == FMTBAD) | (info.expos <= 0.) || |
201 |
|
fgetresolu(xp, yp, inpf) < 0) { |
202 |
|
err = TM_E_BADFILE; goto done; |
203 |
|
} |
250 |
|
|
251 |
|
|
252 |
|
#ifdef PCOND |
253 |
< |
int /* run pcond to map picture */ |
253 |
> |
static int /* run pcond to map picture */ |
254 |
|
dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname) |
255 |
|
BYTE **psp; |
256 |
|
int *xp, *yp; |
260 |
|
char *fname; |
261 |
|
{ |
262 |
|
char *funcName = fname; |
263 |
< |
char cmdbuf[512]; |
263 |
> |
char cmdbuf[1024]; |
264 |
|
FILE *infp; |
265 |
|
register COLR *scan; |
266 |
|
register BYTE *rp; |
305 |
|
rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); |
306 |
|
else |
307 |
|
rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); |
308 |
< |
if ((*psp = rp) == NULL | scan == NULL) { |
308 |
> |
if (((*psp = rp) == NULL) | (scan == NULL)) { |
309 |
|
pclose(infp); |
310 |
|
returnErr(TM_E_NOMEM); |
311 |
|
} |
351 |
|
TMbright *lp; |
352 |
|
int err; |
353 |
|
/* check arguments */ |
354 |
< |
if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL | |
355 |
< |
(fname == NULL & fp == TM_GETFILE)) |
354 |
> |
if ((psp == NULL) | (xp == NULL) | (yp == NULL) | (monpri == NULL) | |
355 |
> |
((fname == NULL) & (fp == TM_GETFILE))) |
356 |
|
returnErr(TM_E_ILLEGAL); |
357 |
|
/* set defaults */ |
358 |
|
if (gamval < MINGAM) gamval = DEFGAM; |