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