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, 11 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

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 greg 3.16 static const char RCSid[] = "$Id: tmapluv.c,v 3.15 2010/12/02 18:10:40 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.14 #include <stdlib.h>
16 greg 3.4 #include <string.h>
17 gwlarson 3.1 #include <math.h>
18 greg 3.15 #include "tiffio.h"
19 greg 3.13 #include "tmprivat.h"
20 greg 3.4 #include "tmaptiff.h"
21 gwlarson 3.1
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 schorsch 3.6 #define clruvall(p) memset((MEM_PTR)(p)->rgbflg,'\0',sizeof((p)->rgbflg))
27 gwlarson 3.1
28 greg 3.4 #ifdef NOPROTO
29 gwlarson 3.1 static MEM_PTR luv32Init();
30     static void luv32NewSpace();
31     static MEM_PTR luv24Init();
32     static void luv24NewSpace();
33 greg 3.4 #else
34 greg 3.10 static MEM_PTR luv32Init(TMstruct *);
35     static void luv32NewSpace(TMstruct *);
36     static MEM_PTR luv24Init(TMstruct *);
37     static void luv24NewSpace(TMstruct *);
38 greg 3.4 #endif
39 gwlarson 3.1
40     typedef struct {
41     int offset; /* computed luminance offset */
42 greg 3.16 uby8 rgbval[1<<16][3]; /* computed RGB value for given uv */
43 gwlarson 3.1 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 greg 3.16 uby8 rgbval[1<<14][3]; /* computed rgb value for uv index */
57 gwlarson 3.1 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 greg 3.4 static void
69 greg 3.10 uv2rgb(rgb, tms, uvp) /* compute RGB from uv coordinate */
70 greg 3.16 uby8 rgb[3];
71 greg 3.10 register TMstruct *tms;
72 gwlarson 3.1 double uvp[2];
73 greg 3.10 { /* Should check that tms->inppri==TM_XYZPRIM beforehand... */
74 gwlarson 3.1 double d, x, y;
75 gwlarson 3.2 COLOR XYZ, RGB;
76 gwlarson 3.1 /* 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 greg 3.10 XYZ[CIEY] = 1./tms->inpsf;
81 gwlarson 3.1 XYZ[CIEX] = x/y * XYZ[CIEY];
82     XYZ[CIEZ] = (1.-x-y)/y * XYZ[CIEY];
83     /* convert to RGB and clip */
84 greg 3.10 colortrans(RGB, tms->cmat, XYZ);
85 greg 3.4 clipgamut(RGB, 1., CGAMUT_LOWER, cblack, cwhite);
86 gwlarson 3.1 /* perform final scaling & gamma */
87 greg 3.10 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 gwlarson 3.1 }
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 greg 3.4 double scotrat;
102     register double d;
103 gwlarson 3.2
104     if (li >= BMESUPPER)
105     return(li);
106 greg 3.4 scotrat = (.767676768 - 1.02356902*uvp[1])/uvp[0] - .343434343;
107 gwlarson 3.2 if (li <= BMESLOWER) {
108     d = 0.;
109     uvp[0] = U_NEU; uvp[1] = V_NEU;
110     } else {
111 greg 3.4 d = (tmMesofact[li-BMESLOWER] + .5) * (1./256.);
112 gwlarson 3.2 uvp[0] = d*uvp[0] + (1.-d)*U_NEU;
113     uvp[1] = d*uvp[1] + (1.-d)*V_NEU;
114     }
115 greg 3.4 /*
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 gwlarson 3.1 }
124    
125    
126     int
127 greg 3.10 tmCvLuv32( /* convert raw 32-bit LogLuv values */
128     TMstruct *tms,
129     TMbright *ls,
130 greg 3.16 uby8 *cs,
131 greg 3.10 uint32 *luvs,
132     int len
133     )
134 gwlarson 3.1 {
135 greg 3.11 static const char funcName[] = "tmCvLuv32";
136 gwlarson 3.1 double uvp[2];
137     register LUV32DATA *ld;
138     register int i, j;
139     /* check arguments */
140 greg 3.10 if (tms == NULL)
141 gwlarson 3.1 returnErr(TM_E_TMINVAL);
142 schorsch 3.9 if ((ls == NULL) | (luvs == NULL) | (len < 0))
143 gwlarson 3.1 returnErr(TM_E_ILLEGAL);
144     /* check package registration */
145 greg 3.4 if (luv32Reg < 0) {
146     if ((luv32Reg = tmRegPkg(&luv32Pkg)) < 0)
147     returnErr(TM_E_CODERR1);
148     tmMkMesofact();
149     }
150 gwlarson 3.1 /* get package data */
151 greg 3.10 if ((ld = (LUV32DATA *)tmPkgData(tms,luv32Reg)) == NULL)
152 gwlarson 3.1 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 gwlarson 3.3 ls[i] = TM_NOBRT; /* assign bogus value */
158 gwlarson 3.1 else /* else convert to lnL */
159 greg 3.4 ls[i] = (BRT2SCALE(j) >> 8) - ld->offset;
160 gwlarson 3.1 if (cs == TM_NOCHROM) /* no color? */
161     continue;
162     /* get chrominance */
163 greg 3.10 if (tms->flags & TM_F_MESOPIC && ls[i] < BMESUPPER) {
164 gwlarson 3.1 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 greg 3.10 j = tms->flags&TM_F_BW || ls[i]<BMESLOWER
168 gwlarson 3.1 ? UVNEU
169     : (int)(uvp[0]*UVSCALE)<<8
170     | (int)(uvp[1]*UVSCALE);
171     } else {
172 greg 3.10 j = tms->flags&TM_F_BW ? UVNEU : luvs[i]&0xffff;
173 gwlarson 3.1 }
174     if (!isuvset(ld, j)) {
175 greg 3.4 uvp[0] = 1./UVSCALE*((j>>8) + .5);
176     uvp[1] = 1./UVSCALE*((j & 0xff) + .5);
177 greg 3.10 uv2rgb(ld->rgbval[j], tms, uvp);
178 gwlarson 3.1 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 greg 3.10 tmCvLuv24( /* convert raw 24-bit LogLuv values */
190     TMstruct *tms,
191     TMbright *ls,
192 greg 3.16 uby8 *cs,
193 greg 3.10 uint32 *luvs,
194     int len
195     )
196 gwlarson 3.1 {
197     char funcName[] = "tmCvLuv24";
198     double uvp[2];
199     register LUV24DATA *ld;
200     register int i, j;
201     /* check arguments */
202 greg 3.10 if (tms == NULL)
203 gwlarson 3.1 returnErr(TM_E_TMINVAL);
204 schorsch 3.9 if ((ls == NULL) | (luvs == NULL) | (len < 0))
205 gwlarson 3.1 returnErr(TM_E_ILLEGAL);
206     /* check package registration */
207 greg 3.4 if (luv24Reg < 0) {
208     if ((luv24Reg = tmRegPkg(&luv24Pkg)) < 0)
209     returnErr(TM_E_CODERR1);
210     tmMkMesofact();
211     }
212 gwlarson 3.1 /* get package data */
213 greg 3.10 if ((ld = (LUV24DATA *)tmPkgData(tms,luv24Reg)) == NULL)
214 gwlarson 3.1 returnErr(TM_E_NOMEM);
215     /* convert each pixel */
216     for (i = len; i--; ) {
217     j = luvs[i] >> 14; /* get luminance */
218 greg 3.4 ls[i] = (BRT2SCALE(j) >> 6) - ld->offset;
219 gwlarson 3.1 if (cs == TM_NOCHROM) /* no color? */
220     continue;
221     /* get chrominance */
222 greg 3.10 if (tms->flags & TM_F_MESOPIC && ls[i] < BMESUPPER) {
223 greg 3.8 if (uv_decode(&uvp[0], &uvp[1], luvs[i]&0x3fff) < 0) {
224 gwlarson 3.1 uvp[0] = U_NEU; /* should barf? */
225     uvp[1] = V_NEU;
226     }
227     ls[i] = compmeshift(ls[i], uvp);
228 greg 3.10 if (tms->flags&TM_F_BW || ls[i]<BMESLOWER
229 greg 3.4 || (j = uv_encode(uvp[0], uvp[1],
230     SGILOGENCODE_NODITHER)) < 0)
231 gwlarson 3.1 j = uv14neu;
232     } else {
233 greg 3.10 j = tms->flags&TM_F_BW ? uv14neu :
234 greg 3.7 (int)(luvs[i]&0x3fff);
235 gwlarson 3.1 }
236     if (!isuvset(ld, j)) {
237 greg 3.4 if (uv_decode(&uvp[0], &uvp[1], j) < 0) {
238 gwlarson 3.2 uvp[0] = U_NEU; uvp[1] = V_NEU;
239 gwlarson 3.1 }
240 greg 3.10 uv2rgb(ld->rgbval[j], tms, uvp);
241 gwlarson 3.1 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 greg 3.10 tmCvL16( /* convert 16-bit LogL values */
253     TMstruct *tms,
254     TMbright *ls,
255     uint16 *l16s,
256     int len
257     )
258 gwlarson 3.1 {
259 greg 3.11 static const char funcName[] = "tmCvL16";
260 gwlarson 3.1 static double lastsf;
261     static int offset;
262     register int i;
263     /* check arguments */
264 greg 3.10 if (tms == NULL)
265 gwlarson 3.1 returnErr(TM_E_TMINVAL);
266 schorsch 3.9 if ((ls == NULL) | (l16s == NULL) | (len < 0))
267 gwlarson 3.1 returnErr(TM_E_ILLEGAL);
268     /* check scaling offset */
269 greg 3.10 if (!FEQ(tms->inpsf, lastsf)) {
270 greg 3.12 offset = BRT2SCALE(64) - tmCvLuminance(tms->inpsf);
271 greg 3.10 lastsf = tms->inpsf;
272 gwlarson 3.1 }
273     /* convert each pixel */
274     for (i = len; i--; ) {
275     if (l16s[i] & 0x8000) /* negative luminance */
276 gwlarson 3.3 ls[i] = TM_NOBRT; /* assign bogus value */
277 gwlarson 3.1 else /* else convert to lnL */
278 greg 3.4 ls[i] = (BRT2SCALE(l16s[i]) >> 8) - offset;
279 gwlarson 3.1 }
280     returnOK;
281     }
282    
283    
284     static void
285 greg 3.10 luv32NewSpace(tms) /* initialize 32-bit LogLuv color space */
286     TMstruct *tms;
287 gwlarson 3.1 {
288     register LUV32DATA *ld;
289    
290 greg 3.10 if (tms->inppri != TM_XYZPRIM) { /* panic time! */
291 gwlarson 3.1 fputs("Improper input color space in luv32NewSpace!\n", stderr);
292     exit(1);
293     }
294 greg 3.10 ld = (LUV32DATA *)tms->pd[luv32Reg];
295 greg 3.12 ld->offset = BRT2SCALE(64) - tmCvLuminance(tms->inpsf);
296 gwlarson 3.1 clruvall(ld);
297     }
298    
299    
300     static MEM_PTR
301 greg 3.10 luv32Init(tms) /* allocate data for 32-bit LogLuv decoder */
302     TMstruct *tms;
303 gwlarson 3.1 {
304     register LUV32DATA *ld;
305    
306     ld = (LUV32DATA *)malloc(sizeof(LUV32DATA));
307     if (ld == NULL)
308     return(NULL);
309 greg 3.10 tms->pd[luv32Reg] = (MEM_PTR)ld;
310     luv32NewSpace(tms);
311 gwlarson 3.1 return((MEM_PTR)ld);
312     }
313    
314    
315     static void
316 greg 3.10 luv24NewSpace(tms) /* initialize 24-bit LogLuv color space */
317     TMstruct *tms;
318 gwlarson 3.1 {
319     register LUV24DATA *ld;
320    
321 greg 3.10 if (tms->inppri != TM_XYZPRIM) { /* panic time! */
322 gwlarson 3.1 fputs("Improper input color space in luv24NewSpace!\n", stderr);
323     exit(1);
324     }
325 greg 3.10 ld = (LUV24DATA *)tms->pd[luv24Reg];
326 greg 3.12 ld->offset = BRT2SCALE(12) - tmCvLuminance(tms->inpsf);
327 gwlarson 3.1 clruvall(ld);
328     }
329    
330    
331     static MEM_PTR
332 greg 3.10 luv24Init(tms) /* allocate data for 24-bit LogLuv decoder */
333     TMstruct *tms;
334 gwlarson 3.1 {
335     register LUV24DATA *ld;
336    
337     ld = (LUV24DATA *)malloc(sizeof(LUV24DATA));
338     if (ld == NULL)
339     return(NULL);
340 greg 3.10 tms->pd[luv24Reg] = (MEM_PTR)ld;
341 greg 3.4 if (uv14neu < 0) /* initialize neutral color index */
342     uv14neu = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
343 greg 3.10 luv24NewSpace(tms);
344 gwlarson 3.1 return((MEM_PTR)ld);
345     }