ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapluv.c
Revision: 3.5
Committed: Tue Feb 25 02:47:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 3.4: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

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