ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapluv.c
Revision: 3.16
Committed: Fri May 20 02:06:38 2011 UTC (12 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad4R2P2, rad5R0, rad5R1, rad4R2, rad4R1, rad4R2P1, rad5R3
Changes since 3.15: +6 -6 lines
Log Message:
Changed every instance of BYTE to uby8 to avoid conflicts

File Contents

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