1 |
gwlarson |
3.1 |
#ifndef lint |
2 |
greg |
3.13 |
static const char RCSid[] = "$Id: tmapluv.c,v 3.12 2005/11/15 06:53:00 greg Exp $"; |
3 |
gwlarson |
3.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Routines for tone-mapping LogLuv encoded pixels. |
6 |
greg |
3.4 |
* |
7 |
|
|
* Externals declared in tmaptiff.h |
8 |
|
|
*/ |
9 |
|
|
|
10 |
greg |
3.5 |
#include "copyright.h" |
11 |
gwlarson |
3.1 |
|
12 |
greg |
3.4 |
#define LOGLUV_PUBLIC 1 |
13 |
|
|
|
14 |
gwlarson |
3.1 |
#include <stdio.h> |
15 |
greg |
3.4 |
#include <string.h> |
16 |
gwlarson |
3.1 |
#include <math.h> |
17 |
greg |
3.13 |
#include "tmprivat.h" |
18 |
gwlarson |
3.1 |
#include "tiffio.h" |
19 |
greg |
3.4 |
#include "tmaptiff.h" |
20 |
gwlarson |
3.1 |
|
21 |
|
|
#define uvflgop(p,uv,op) ((p)->rgbflg[(uv)>>5] op (1L<<((uv)&0x1f))) |
22 |
|
|
#define isuvset(p,uv) uvflgop(p,uv,&) |
23 |
|
|
#define setuv(p,uv) uvflgop(p,uv,|=) |
24 |
|
|
#define clruv(p,uv) uvflgop(p,uv,&=~) |
25 |
schorsch |
3.6 |
#define clruvall(p) memset((MEM_PTR)(p)->rgbflg,'\0',sizeof((p)->rgbflg)) |
26 |
gwlarson |
3.1 |
|
27 |
greg |
3.4 |
#ifdef NOPROTO |
28 |
gwlarson |
3.1 |
static MEM_PTR luv32Init(); |
29 |
|
|
static void luv32NewSpace(); |
30 |
|
|
static MEM_PTR luv24Init(); |
31 |
|
|
static void luv24NewSpace(); |
32 |
greg |
3.4 |
#else |
33 |
greg |
3.10 |
static MEM_PTR luv32Init(TMstruct *); |
34 |
|
|
static void luv32NewSpace(TMstruct *); |
35 |
|
|
static MEM_PTR luv24Init(TMstruct *); |
36 |
|
|
static void luv24NewSpace(TMstruct *); |
37 |
greg |
3.4 |
#endif |
38 |
gwlarson |
3.1 |
|
39 |
|
|
typedef struct { |
40 |
|
|
int offset; /* computed luminance offset */ |
41 |
|
|
BYTE rgbval[1<<16][3]; /* computed RGB value for given uv */ |
42 |
|
|
uint32 rgbflg[1<<(16-5)]; /* flags for computed values */ |
43 |
|
|
} LUV32DATA; /* LogLuv 32-bit conversion data */ |
44 |
|
|
|
45 |
|
|
#define UVNEU ((int)(UVSCALE*U_NEU)<<8 \ |
46 |
|
|
| (int)(UVSCALE*V_NEU)) |
47 |
|
|
|
48 |
|
|
static struct tmPackage luv32Pkg = { /* 32-bit package functions */ |
49 |
|
|
luv32Init, luv32NewSpace, free |
50 |
|
|
}; |
51 |
|
|
static int luv32Reg = -1; /* 32-bit package reg. number */ |
52 |
|
|
|
53 |
|
|
typedef struct { |
54 |
|
|
int offset; /* computed luminance offset */ |
55 |
|
|
BYTE rgbval[1<<14][3]; /* computed rgb value for uv index */ |
56 |
|
|
uint32 rgbflg[1<<(14-5)]; /* flags for computed values */ |
57 |
|
|
} LUV24DATA; /* LogLuv 24-bit conversion data */ |
58 |
|
|
|
59 |
|
|
static struct tmPackage luv24Pkg = { /* 24-bit package functions */ |
60 |
|
|
luv24Init, luv24NewSpace, free |
61 |
|
|
}; |
62 |
|
|
static int luv24Reg = -1; /* 24-bit package reg. number */ |
63 |
|
|
|
64 |
|
|
static int uv14neu = -1; /* neutral index for 14-bit (u',v') */ |
65 |
|
|
|
66 |
|
|
|
67 |
greg |
3.4 |
static void |
68 |
greg |
3.10 |
uv2rgb(rgb, tms, uvp) /* compute RGB from uv coordinate */ |
69 |
gwlarson |
3.1 |
BYTE rgb[3]; |
70 |
greg |
3.10 |
register TMstruct *tms; |
71 |
gwlarson |
3.1 |
double uvp[2]; |
72 |
greg |
3.10 |
{ /* Should check that tms->inppri==TM_XYZPRIM beforehand... */ |
73 |
gwlarson |
3.1 |
double d, x, y; |
74 |
gwlarson |
3.2 |
COLOR XYZ, RGB; |
75 |
gwlarson |
3.1 |
/* convert to XYZ */ |
76 |
|
|
d = 1./(6.*uvp[0] - 16.*uvp[1] + 12.); |
77 |
|
|
x = 9.*uvp[0] * d; |
78 |
|
|
y = 4.*uvp[1] * d; |
79 |
greg |
3.10 |
XYZ[CIEY] = 1./tms->inpsf; |
80 |
gwlarson |
3.1 |
XYZ[CIEX] = x/y * XYZ[CIEY]; |
81 |
|
|
XYZ[CIEZ] = (1.-x-y)/y * XYZ[CIEY]; |
82 |
|
|
/* convert to RGB and clip */ |
83 |
greg |
3.10 |
colortrans(RGB, tms->cmat, XYZ); |
84 |
greg |
3.4 |
clipgamut(RGB, 1., CGAMUT_LOWER, cblack, cwhite); |
85 |
gwlarson |
3.1 |
/* perform final scaling & gamma */ |
86 |
greg |
3.10 |
d = tms->clf[RED] * RGB[RED]; |
87 |
|
|
rgb[RED] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tms->mongam)); |
88 |
|
|
d = tms->clf[GRN] * RGB[GRN]; |
89 |
|
|
rgb[GRN] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tms->mongam)); |
90 |
|
|
d = tms->clf[BLU] * RGB[BLU]; |
91 |
|
|
rgb[BLU] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tms->mongam)); |
92 |
gwlarson |
3.1 |
} |
93 |
|
|
|
94 |
|
|
|
95 |
|
|
static TMbright |
96 |
|
|
compmeshift(li, uvp) /* compute mesopic color shift */ |
97 |
|
|
TMbright li; /* encoded world luminance */ |
98 |
|
|
double uvp[2]; /* world (u',v') -> returned desaturated */ |
99 |
|
|
{ |
100 |
greg |
3.4 |
double scotrat; |
101 |
|
|
register double d; |
102 |
gwlarson |
3.2 |
|
103 |
|
|
if (li >= BMESUPPER) |
104 |
|
|
return(li); |
105 |
greg |
3.4 |
scotrat = (.767676768 - 1.02356902*uvp[1])/uvp[0] - .343434343; |
106 |
gwlarson |
3.2 |
if (li <= BMESLOWER) { |
107 |
|
|
d = 0.; |
108 |
|
|
uvp[0] = U_NEU; uvp[1] = V_NEU; |
109 |
|
|
} else { |
110 |
greg |
3.4 |
d = (tmMesofact[li-BMESLOWER] + .5) * (1./256.); |
111 |
gwlarson |
3.2 |
uvp[0] = d*uvp[0] + (1.-d)*U_NEU; |
112 |
|
|
uvp[1] = d*uvp[1] + (1.-d)*V_NEU; |
113 |
|
|
} |
114 |
greg |
3.4 |
/* |
115 |
|
|
d = li + (double)TM_BRTSCALE*log(d + (1.-d)*scotrat); |
116 |
|
|
*/ |
117 |
|
|
d = d + (1.-d)*scotrat; |
118 |
|
|
d -= 1.; /* Taylor expansion of log(x) about 1 */ |
119 |
|
|
d = d*(1. + d*(-.5 + d*(1./3. + d*-.125))); |
120 |
|
|
d = li + (double)TM_BRTSCALE*d; |
121 |
|
|
return((TMbright)(d>0. ? d+.5 : d-.5)); |
122 |
gwlarson |
3.1 |
} |
123 |
|
|
|
124 |
|
|
|
125 |
|
|
int |
126 |
greg |
3.10 |
tmCvLuv32( /* convert raw 32-bit LogLuv values */ |
127 |
|
|
TMstruct *tms, |
128 |
|
|
TMbright *ls, |
129 |
|
|
BYTE *cs, |
130 |
|
|
uint32 *luvs, |
131 |
|
|
int len |
132 |
|
|
) |
133 |
gwlarson |
3.1 |
{ |
134 |
greg |
3.11 |
static const char funcName[] = "tmCvLuv32"; |
135 |
gwlarson |
3.1 |
double uvp[2]; |
136 |
|
|
register LUV32DATA *ld; |
137 |
|
|
register int i, j; |
138 |
|
|
/* check arguments */ |
139 |
greg |
3.10 |
if (tms == NULL) |
140 |
gwlarson |
3.1 |
returnErr(TM_E_TMINVAL); |
141 |
schorsch |
3.9 |
if ((ls == NULL) | (luvs == NULL) | (len < 0)) |
142 |
gwlarson |
3.1 |
returnErr(TM_E_ILLEGAL); |
143 |
|
|
/* check package registration */ |
144 |
greg |
3.4 |
if (luv32Reg < 0) { |
145 |
|
|
if ((luv32Reg = tmRegPkg(&luv32Pkg)) < 0) |
146 |
|
|
returnErr(TM_E_CODERR1); |
147 |
|
|
tmMkMesofact(); |
148 |
|
|
} |
149 |
gwlarson |
3.1 |
/* get package data */ |
150 |
greg |
3.10 |
if ((ld = (LUV32DATA *)tmPkgData(tms,luv32Reg)) == NULL) |
151 |
gwlarson |
3.1 |
returnErr(TM_E_NOMEM); |
152 |
|
|
/* convert each pixel */ |
153 |
|
|
for (i = len; i--; ) { |
154 |
|
|
j = luvs[i] >> 16; /* get luminance */ |
155 |
|
|
if (j & 0x8000) /* negative luminance */ |
156 |
gwlarson |
3.3 |
ls[i] = TM_NOBRT; /* assign bogus value */ |
157 |
gwlarson |
3.1 |
else /* else convert to lnL */ |
158 |
greg |
3.4 |
ls[i] = (BRT2SCALE(j) >> 8) - ld->offset; |
159 |
gwlarson |
3.1 |
if (cs == TM_NOCHROM) /* no color? */ |
160 |
|
|
continue; |
161 |
|
|
/* get chrominance */ |
162 |
greg |
3.10 |
if (tms->flags & TM_F_MESOPIC && ls[i] < BMESUPPER) { |
163 |
gwlarson |
3.1 |
uvp[0] = 1./UVSCALE*((luvs[i]>>8 & 0xff) + .5); |
164 |
|
|
uvp[1] = 1./UVSCALE*((luvs[i] & 0xff) + .5); |
165 |
|
|
ls[i] = compmeshift(ls[i], uvp); |
166 |
greg |
3.10 |
j = tms->flags&TM_F_BW || ls[i]<BMESLOWER |
167 |
gwlarson |
3.1 |
? UVNEU |
168 |
|
|
: (int)(uvp[0]*UVSCALE)<<8 |
169 |
|
|
| (int)(uvp[1]*UVSCALE); |
170 |
|
|
} else { |
171 |
greg |
3.10 |
j = tms->flags&TM_F_BW ? UVNEU : luvs[i]&0xffff; |
172 |
gwlarson |
3.1 |
} |
173 |
|
|
if (!isuvset(ld, j)) { |
174 |
greg |
3.4 |
uvp[0] = 1./UVSCALE*((j>>8) + .5); |
175 |
|
|
uvp[1] = 1./UVSCALE*((j & 0xff) + .5); |
176 |
greg |
3.10 |
uv2rgb(ld->rgbval[j], tms, uvp); |
177 |
gwlarson |
3.1 |
setuv(ld, j); |
178 |
|
|
} |
179 |
|
|
cs[3*i ] = ld->rgbval[j][RED]; |
180 |
|
|
cs[3*i+1] = ld->rgbval[j][GRN]; |
181 |
|
|
cs[3*i+2] = ld->rgbval[j][BLU]; |
182 |
|
|
} |
183 |
|
|
returnOK; |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
|
187 |
|
|
int |
188 |
greg |
3.10 |
tmCvLuv24( /* convert raw 24-bit LogLuv values */ |
189 |
|
|
TMstruct *tms, |
190 |
|
|
TMbright *ls, |
191 |
|
|
BYTE *cs, |
192 |
|
|
uint32 *luvs, |
193 |
|
|
int len |
194 |
|
|
) |
195 |
gwlarson |
3.1 |
{ |
196 |
|
|
char funcName[] = "tmCvLuv24"; |
197 |
|
|
double uvp[2]; |
198 |
|
|
register LUV24DATA *ld; |
199 |
|
|
register int i, j; |
200 |
|
|
/* check arguments */ |
201 |
greg |
3.10 |
if (tms == NULL) |
202 |
gwlarson |
3.1 |
returnErr(TM_E_TMINVAL); |
203 |
schorsch |
3.9 |
if ((ls == NULL) | (luvs == NULL) | (len < 0)) |
204 |
gwlarson |
3.1 |
returnErr(TM_E_ILLEGAL); |
205 |
|
|
/* check package registration */ |
206 |
greg |
3.4 |
if (luv24Reg < 0) { |
207 |
|
|
if ((luv24Reg = tmRegPkg(&luv24Pkg)) < 0) |
208 |
|
|
returnErr(TM_E_CODERR1); |
209 |
|
|
tmMkMesofact(); |
210 |
|
|
} |
211 |
gwlarson |
3.1 |
/* get package data */ |
212 |
greg |
3.10 |
if ((ld = (LUV24DATA *)tmPkgData(tms,luv24Reg)) == NULL) |
213 |
gwlarson |
3.1 |
returnErr(TM_E_NOMEM); |
214 |
|
|
/* convert each pixel */ |
215 |
|
|
for (i = len; i--; ) { |
216 |
|
|
j = luvs[i] >> 14; /* get luminance */ |
217 |
greg |
3.4 |
ls[i] = (BRT2SCALE(j) >> 6) - ld->offset; |
218 |
gwlarson |
3.1 |
if (cs == TM_NOCHROM) /* no color? */ |
219 |
|
|
continue; |
220 |
|
|
/* get chrominance */ |
221 |
greg |
3.10 |
if (tms->flags & TM_F_MESOPIC && ls[i] < BMESUPPER) { |
222 |
greg |
3.8 |
if (uv_decode(&uvp[0], &uvp[1], luvs[i]&0x3fff) < 0) { |
223 |
gwlarson |
3.1 |
uvp[0] = U_NEU; /* should barf? */ |
224 |
|
|
uvp[1] = V_NEU; |
225 |
|
|
} |
226 |
|
|
ls[i] = compmeshift(ls[i], uvp); |
227 |
greg |
3.10 |
if (tms->flags&TM_F_BW || ls[i]<BMESLOWER |
228 |
greg |
3.4 |
|| (j = uv_encode(uvp[0], uvp[1], |
229 |
|
|
SGILOGENCODE_NODITHER)) < 0) |
230 |
gwlarson |
3.1 |
j = uv14neu; |
231 |
|
|
} else { |
232 |
greg |
3.10 |
j = tms->flags&TM_F_BW ? uv14neu : |
233 |
greg |
3.7 |
(int)(luvs[i]&0x3fff); |
234 |
gwlarson |
3.1 |
} |
235 |
|
|
if (!isuvset(ld, j)) { |
236 |
greg |
3.4 |
if (uv_decode(&uvp[0], &uvp[1], j) < 0) { |
237 |
gwlarson |
3.2 |
uvp[0] = U_NEU; uvp[1] = V_NEU; |
238 |
gwlarson |
3.1 |
} |
239 |
greg |
3.10 |
uv2rgb(ld->rgbval[j], tms, uvp); |
240 |
gwlarson |
3.1 |
setuv(ld, j); |
241 |
|
|
} |
242 |
|
|
cs[3*i ] = ld->rgbval[j][RED]; |
243 |
|
|
cs[3*i+1] = ld->rgbval[j][GRN]; |
244 |
|
|
cs[3*i+2] = ld->rgbval[j][BLU]; |
245 |
|
|
} |
246 |
|
|
returnOK; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
|
250 |
|
|
int |
251 |
greg |
3.10 |
tmCvL16( /* convert 16-bit LogL values */ |
252 |
|
|
TMstruct *tms, |
253 |
|
|
TMbright *ls, |
254 |
|
|
uint16 *l16s, |
255 |
|
|
int len |
256 |
|
|
) |
257 |
gwlarson |
3.1 |
{ |
258 |
greg |
3.11 |
static const char funcName[] = "tmCvL16"; |
259 |
gwlarson |
3.1 |
static double lastsf; |
260 |
|
|
static int offset; |
261 |
|
|
register int i; |
262 |
|
|
/* check arguments */ |
263 |
greg |
3.10 |
if (tms == NULL) |
264 |
gwlarson |
3.1 |
returnErr(TM_E_TMINVAL); |
265 |
schorsch |
3.9 |
if ((ls == NULL) | (l16s == NULL) | (len < 0)) |
266 |
gwlarson |
3.1 |
returnErr(TM_E_ILLEGAL); |
267 |
|
|
/* check scaling offset */ |
268 |
greg |
3.10 |
if (!FEQ(tms->inpsf, lastsf)) { |
269 |
greg |
3.12 |
offset = BRT2SCALE(64) - tmCvLuminance(tms->inpsf); |
270 |
greg |
3.10 |
lastsf = tms->inpsf; |
271 |
gwlarson |
3.1 |
} |
272 |
|
|
/* convert each pixel */ |
273 |
|
|
for (i = len; i--; ) { |
274 |
|
|
if (l16s[i] & 0x8000) /* negative luminance */ |
275 |
gwlarson |
3.3 |
ls[i] = TM_NOBRT; /* assign bogus value */ |
276 |
gwlarson |
3.1 |
else /* else convert to lnL */ |
277 |
greg |
3.4 |
ls[i] = (BRT2SCALE(l16s[i]) >> 8) - offset; |
278 |
gwlarson |
3.1 |
} |
279 |
|
|
returnOK; |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
|
283 |
|
|
static void |
284 |
greg |
3.10 |
luv32NewSpace(tms) /* initialize 32-bit LogLuv color space */ |
285 |
|
|
TMstruct *tms; |
286 |
gwlarson |
3.1 |
{ |
287 |
|
|
register LUV32DATA *ld; |
288 |
|
|
|
289 |
greg |
3.10 |
if (tms->inppri != TM_XYZPRIM) { /* panic time! */ |
290 |
gwlarson |
3.1 |
fputs("Improper input color space in luv32NewSpace!\n", stderr); |
291 |
|
|
exit(1); |
292 |
|
|
} |
293 |
greg |
3.10 |
ld = (LUV32DATA *)tms->pd[luv32Reg]; |
294 |
greg |
3.12 |
ld->offset = BRT2SCALE(64) - tmCvLuminance(tms->inpsf); |
295 |
gwlarson |
3.1 |
clruvall(ld); |
296 |
|
|
} |
297 |
|
|
|
298 |
|
|
|
299 |
|
|
static MEM_PTR |
300 |
greg |
3.10 |
luv32Init(tms) /* allocate data for 32-bit LogLuv decoder */ |
301 |
|
|
TMstruct *tms; |
302 |
gwlarson |
3.1 |
{ |
303 |
|
|
register LUV32DATA *ld; |
304 |
|
|
|
305 |
|
|
ld = (LUV32DATA *)malloc(sizeof(LUV32DATA)); |
306 |
|
|
if (ld == NULL) |
307 |
|
|
return(NULL); |
308 |
greg |
3.10 |
tms->pd[luv32Reg] = (MEM_PTR)ld; |
309 |
|
|
luv32NewSpace(tms); |
310 |
gwlarson |
3.1 |
return((MEM_PTR)ld); |
311 |
|
|
} |
312 |
|
|
|
313 |
|
|
|
314 |
|
|
static void |
315 |
greg |
3.10 |
luv24NewSpace(tms) /* initialize 24-bit LogLuv color space */ |
316 |
|
|
TMstruct *tms; |
317 |
gwlarson |
3.1 |
{ |
318 |
|
|
register LUV24DATA *ld; |
319 |
|
|
|
320 |
greg |
3.10 |
if (tms->inppri != TM_XYZPRIM) { /* panic time! */ |
321 |
gwlarson |
3.1 |
fputs("Improper input color space in luv24NewSpace!\n", stderr); |
322 |
|
|
exit(1); |
323 |
|
|
} |
324 |
greg |
3.10 |
ld = (LUV24DATA *)tms->pd[luv24Reg]; |
325 |
greg |
3.12 |
ld->offset = BRT2SCALE(12) - tmCvLuminance(tms->inpsf); |
326 |
gwlarson |
3.1 |
clruvall(ld); |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
|
330 |
|
|
static MEM_PTR |
331 |
greg |
3.10 |
luv24Init(tms) /* allocate data for 24-bit LogLuv decoder */ |
332 |
|
|
TMstruct *tms; |
333 |
gwlarson |
3.1 |
{ |
334 |
|
|
register LUV24DATA *ld; |
335 |
|
|
|
336 |
|
|
ld = (LUV24DATA *)malloc(sizeof(LUV24DATA)); |
337 |
|
|
if (ld == NULL) |
338 |
|
|
return(NULL); |
339 |
greg |
3.10 |
tms->pd[luv24Reg] = (MEM_PTR)ld; |
340 |
greg |
3.4 |
if (uv14neu < 0) /* initialize neutral color index */ |
341 |
|
|
uv14neu = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER); |
342 |
greg |
3.10 |
luv24NewSpace(tms); |
343 |
gwlarson |
3.1 |
return((MEM_PTR)ld); |
344 |
|
|
} |