ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapluv.c
Revision: 3.4
Committed: Sat Feb 22 02:07:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.3: +109 -88 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 * Routines for tone-mapping LogLuv encoded pixels.
6 *
7 * Externals declared in tmaptiff.h
8 */
9
10 /* ====================================================================
11 * The Radiance Software License, Version 1.0
12 *
13 * Copyright (c) 1990 - 2002 The Regents of the University of California,
14 * through Lawrence Berkeley National Laboratory. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 *
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 *
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
26 * distribution.
27 *
28 * 3. The end-user documentation included with the redistribution,
29 * if any, must include the following acknowledgment:
30 * "This product includes Radiance software
31 * (http://radsite.lbl.gov/)
32 * developed by the Lawrence Berkeley National Laboratory
33 * (http://www.lbl.gov/)."
34 * Alternately, this acknowledgment may appear in the software itself,
35 * if and wherever such third-party acknowledgments normally appear.
36 *
37 * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38 * and "The Regents of the University of California" must
39 * not be used to endorse or promote products derived from this
40 * software without prior written permission. For written
41 * permission, please contact [email protected].
42 *
43 * 5. Products derived from this software may not be called "Radiance",
44 * nor may "Radiance" appear in their name, without prior written
45 * permission of Lawrence Berkeley National Laboratory.
46 *
47 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 * ====================================================================
60 *
61 * This software consists of voluntary contributions made by many
62 * individuals on behalf of Lawrence Berkeley National Laboratory. For more
63 * information on Lawrence Berkeley National Laboratory, please see
64 * <http://www.lbl.gov/>.
65 */
66
67 #define LOGLUV_PUBLIC 1
68
69 #include <stdio.h>
70 #include <string.h>
71 #include <math.h>
72 #include "tiffio.h"
73 #include "tmprivat.h"
74 #include "tmaptiff.h"
75
76 #ifndef BSD
77 #define bzero(d,n) (void)memset(d,0,n)
78 #endif
79 #define uvflgop(p,uv,op) ((p)->rgbflg[(uv)>>5] op (1L<<((uv)&0x1f)))
80 #define isuvset(p,uv) uvflgop(p,uv,&)
81 #define setuv(p,uv) uvflgop(p,uv,|=)
82 #define clruv(p,uv) uvflgop(p,uv,&=~)
83 #define clruvall(p) bzero((MEM_PTR)(p)->rgbflg,sizeof((p)->rgbflg))
84
85 #ifdef NOPROTO
86 static MEM_PTR luv32Init();
87 static void luv32NewSpace();
88 static MEM_PTR luv24Init();
89 static void luv24NewSpace();
90 #else
91 static MEM_PTR luv32Init(struct tmStruct *);
92 static void luv32NewSpace(struct tmStruct *);
93 static MEM_PTR luv24Init(struct tmStruct *);
94 static void luv24NewSpace(struct tmStruct *);
95 #endif
96
97 typedef struct {
98 int offset; /* computed luminance offset */
99 BYTE rgbval[1<<16][3]; /* computed RGB value for given uv */
100 uint32 rgbflg[1<<(16-5)]; /* flags for computed values */
101 } LUV32DATA; /* LogLuv 32-bit conversion data */
102
103 #define UVNEU ((int)(UVSCALE*U_NEU)<<8 \
104 | (int)(UVSCALE*V_NEU))
105
106 static struct tmPackage luv32Pkg = { /* 32-bit package functions */
107 luv32Init, luv32NewSpace, free
108 };
109 static int luv32Reg = -1; /* 32-bit package reg. number */
110
111 typedef struct {
112 int offset; /* computed luminance offset */
113 BYTE rgbval[1<<14][3]; /* computed rgb value for uv index */
114 uint32 rgbflg[1<<(14-5)]; /* flags for computed values */
115 } LUV24DATA; /* LogLuv 24-bit conversion data */
116
117 static struct tmPackage luv24Pkg = { /* 24-bit package functions */
118 luv24Init, luv24NewSpace, free
119 };
120 static int luv24Reg = -1; /* 24-bit package reg. number */
121
122 static int uv14neu = -1; /* neutral index for 14-bit (u',v') */
123
124
125 static void
126 uv2rgb(rgb, tm, uvp) /* compute RGB from uv coordinate */
127 BYTE rgb[3];
128 register struct tmStruct *tm;
129 double uvp[2];
130 { /* Should check that tm->inppri==TM_XYZPRIM beforehand... */
131 double d, x, y;
132 COLOR XYZ, RGB;
133 /* convert to XYZ */
134 d = 1./(6.*uvp[0] - 16.*uvp[1] + 12.);
135 x = 9.*uvp[0] * d;
136 y = 4.*uvp[1] * d;
137 XYZ[CIEY] = 1./tm->inpsf;
138 XYZ[CIEX] = x/y * XYZ[CIEY];
139 XYZ[CIEZ] = (1.-x-y)/y * XYZ[CIEY];
140 /* convert to RGB and clip */
141 colortrans(RGB, tm->cmat, XYZ);
142 clipgamut(RGB, 1., CGAMUT_LOWER, cblack, cwhite);
143 /* perform final scaling & gamma */
144 d = tm->clf[RED] * RGB[RED];
145 rgb[RED] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tm->mongam));
146 d = tm->clf[GRN] * RGB[GRN];
147 rgb[GRN] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tm->mongam));
148 d = tm->clf[BLU] * RGB[BLU];
149 rgb[BLU] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tm->mongam));
150 }
151
152
153 static TMbright
154 compmeshift(li, uvp) /* compute mesopic color shift */
155 TMbright li; /* encoded world luminance */
156 double uvp[2]; /* world (u',v') -> returned desaturated */
157 {
158 double scotrat;
159 register double d;
160
161 if (li >= BMESUPPER)
162 return(li);
163 scotrat = (.767676768 - 1.02356902*uvp[1])/uvp[0] - .343434343;
164 if (li <= BMESLOWER) {
165 d = 0.;
166 uvp[0] = U_NEU; uvp[1] = V_NEU;
167 } else {
168 d = (tmMesofact[li-BMESLOWER] + .5) * (1./256.);
169 uvp[0] = d*uvp[0] + (1.-d)*U_NEU;
170 uvp[1] = d*uvp[1] + (1.-d)*V_NEU;
171 }
172 /*
173 d = li + (double)TM_BRTSCALE*log(d + (1.-d)*scotrat);
174 */
175 d = d + (1.-d)*scotrat;
176 d -= 1.; /* Taylor expansion of log(x) about 1 */
177 d = d*(1. + d*(-.5 + d*(1./3. + d*-.125)));
178 d = li + (double)TM_BRTSCALE*d;
179 return((TMbright)(d>0. ? d+.5 : d-.5));
180 }
181
182
183 int
184 tmCvLuv32(ls, cs, luvs, len) /* convert raw 32-bit LogLuv values */
185 TMbright *ls;
186 BYTE *cs;
187 uint32 *luvs;
188 int len;
189 {
190 static char funcName[] = "tmCvLuv32";
191 double uvp[2];
192 register LUV32DATA *ld;
193 register int i, j;
194 /* check arguments */
195 if (tmTop == NULL)
196 returnErr(TM_E_TMINVAL);
197 if (ls == NULL | luvs == NULL | len < 0)
198 returnErr(TM_E_ILLEGAL);
199 /* check package registration */
200 if (luv32Reg < 0) {
201 if ((luv32Reg = tmRegPkg(&luv32Pkg)) < 0)
202 returnErr(TM_E_CODERR1);
203 tmMkMesofact();
204 }
205 /* get package data */
206 if ((ld = (LUV32DATA *)tmPkgData(tmTop,luv32Reg)) == NULL)
207 returnErr(TM_E_NOMEM);
208 /* convert each pixel */
209 for (i = len; i--; ) {
210 j = luvs[i] >> 16; /* get luminance */
211 if (j & 0x8000) /* negative luminance */
212 ls[i] = TM_NOBRT; /* assign bogus value */
213 else /* else convert to lnL */
214 ls[i] = (BRT2SCALE(j) >> 8) - ld->offset;
215 if (cs == TM_NOCHROM) /* no color? */
216 continue;
217 /* get chrominance */
218 if (tmTop->flags & TM_F_MESOPIC && ls[i] < BMESUPPER) {
219 uvp[0] = 1./UVSCALE*((luvs[i]>>8 & 0xff) + .5);
220 uvp[1] = 1./UVSCALE*((luvs[i] & 0xff) + .5);
221 ls[i] = compmeshift(ls[i], uvp);
222 j = tmTop->flags&TM_F_BW || ls[i]<BMESLOWER
223 ? UVNEU
224 : (int)(uvp[0]*UVSCALE)<<8
225 | (int)(uvp[1]*UVSCALE);
226 } else {
227 j = tmTop->flags&TM_F_BW ? UVNEU : luvs[i]&0xffff;
228 }
229 if (!isuvset(ld, j)) {
230 uvp[0] = 1./UVSCALE*((j>>8) + .5);
231 uvp[1] = 1./UVSCALE*((j & 0xff) + .5);
232 uv2rgb(ld->rgbval[j], tmTop, uvp);
233 setuv(ld, j);
234 }
235 cs[3*i ] = ld->rgbval[j][RED];
236 cs[3*i+1] = ld->rgbval[j][GRN];
237 cs[3*i+2] = ld->rgbval[j][BLU];
238 }
239 returnOK;
240 }
241
242
243 int
244 tmCvLuv24(ls, cs, luvs, len) /* convert raw 24-bit LogLuv values */
245 TMbright *ls;
246 BYTE *cs;
247 uint32 *luvs;
248 int len;
249 {
250 char funcName[] = "tmCvLuv24";
251 double uvp[2];
252 register LUV24DATA *ld;
253 register int i, j;
254 /* check arguments */
255 if (tmTop == NULL)
256 returnErr(TM_E_TMINVAL);
257 if (ls == NULL | luvs == NULL | len < 0)
258 returnErr(TM_E_ILLEGAL);
259 /* check package registration */
260 if (luv24Reg < 0) {
261 if ((luv24Reg = tmRegPkg(&luv24Pkg)) < 0)
262 returnErr(TM_E_CODERR1);
263 tmMkMesofact();
264 }
265 /* get package data */
266 if ((ld = (LUV24DATA *)tmPkgData(tmTop,luv24Reg)) == NULL)
267 returnErr(TM_E_NOMEM);
268 /* convert each pixel */
269 for (i = len; i--; ) {
270 j = luvs[i] >> 14; /* get luminance */
271 ls[i] = (BRT2SCALE(j) >> 6) - ld->offset;
272 if (cs == TM_NOCHROM) /* no color? */
273 continue;
274 /* get chrominance */
275 if (tmTop->flags & TM_F_MESOPIC && ls[i] < BMESUPPER) {
276 if (uv_decode(uvp, uvp+1, luvs[i]&0x3fff) < 0) {
277 uvp[0] = U_NEU; /* should barf? */
278 uvp[1] = V_NEU;
279 }
280 ls[i] = compmeshift(ls[i], uvp);
281 if (tmTop->flags&TM_F_BW || ls[i]<BMESLOWER
282 || (j = uv_encode(uvp[0], uvp[1],
283 SGILOGENCODE_NODITHER)) < 0)
284 j = uv14neu;
285 } else {
286 j = tmTop->flags&TM_F_BW ? uv14neu : luvs[i]&0x3fff;
287 }
288 if (!isuvset(ld, j)) {
289 if (uv_decode(&uvp[0], &uvp[1], j) < 0) {
290 uvp[0] = U_NEU; uvp[1] = V_NEU;
291 }
292 uv2rgb(ld->rgbval[j], tmTop, uvp);
293 setuv(ld, j);
294 }
295 cs[3*i ] = ld->rgbval[j][RED];
296 cs[3*i+1] = ld->rgbval[j][GRN];
297 cs[3*i+2] = ld->rgbval[j][BLU];
298 }
299 returnOK;
300 }
301
302
303 int
304 tmCvL16(ls, l16s, len) /* convert 16-bit LogL values */
305 TMbright *ls;
306 uint16 *l16s;
307 int len;
308 {
309 static char funcName[] = "tmCvL16";
310 static double lastsf;
311 static int offset;
312 register int i;
313 /* check arguments */
314 if (tmTop == NULL)
315 returnErr(TM_E_TMINVAL);
316 if (ls == NULL | l16s == NULL | len < 0)
317 returnErr(TM_E_ILLEGAL);
318 /* check scaling offset */
319 if (!FEQ(tmTop->inpsf, lastsf)) {
320 offset = BRT2SCALE(64);
321 if (tmTop->inpsf > 1.0001)
322 offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)+.5);
323 else if (tmTop->inpsf < 0.9999)
324 offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)-.5);
325 lastsf = tmTop->inpsf;
326 }
327 /* convert each pixel */
328 for (i = len; i--; ) {
329 if (l16s[i] & 0x8000) /* negative luminance */
330 ls[i] = TM_NOBRT; /* assign bogus value */
331 else /* else convert to lnL */
332 ls[i] = (BRT2SCALE(l16s[i]) >> 8) - offset;
333 }
334 returnOK;
335 }
336
337
338 static void
339 luv32NewSpace(tm) /* initialize 32-bit LogLuv color space */
340 struct tmStruct *tm;
341 {
342 register LUV32DATA *ld;
343
344 if (tm->inppri != TM_XYZPRIM) { /* panic time! */
345 fputs("Improper input color space in luv32NewSpace!\n", stderr);
346 exit(1);
347 }
348 ld = (LUV32DATA *)tm->pd[luv32Reg];
349 ld->offset = BRT2SCALE(64);
350 if (tm->inpsf > 1.0001)
351 ld->offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)+.5);
352 else if (tm->inpsf < 0.9999)
353 ld->offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)-.5);
354 clruvall(ld);
355 }
356
357
358 static MEM_PTR
359 luv32Init(tm) /* allocate data for 32-bit LogLuv decoder */
360 struct tmStruct *tm;
361 {
362 register LUV32DATA *ld;
363
364 ld = (LUV32DATA *)malloc(sizeof(LUV32DATA));
365 if (ld == NULL)
366 return(NULL);
367 tm->pd[luv32Reg] = (MEM_PTR)ld;
368 luv32NewSpace(tm);
369 return((MEM_PTR)ld);
370 }
371
372
373 static void
374 luv24NewSpace(tm) /* initialize 24-bit LogLuv color space */
375 struct tmStruct *tm;
376 {
377 register LUV24DATA *ld;
378
379 if (tm->inppri != TM_XYZPRIM) { /* panic time! */
380 fputs("Improper input color space in luv24NewSpace!\n", stderr);
381 exit(1);
382 }
383 ld = (LUV24DATA *)tm->pd[luv24Reg];
384 ld->offset = BRT2SCALE(12);
385 if (tm->inpsf > 1.0001)
386 ld->offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)+.5);
387 else if (tm->inpsf < 0.9999)
388 ld->offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)-.5);
389 clruvall(ld);
390 }
391
392
393 static MEM_PTR
394 luv24Init(tm) /* allocate data for 24-bit LogLuv decoder */
395 struct tmStruct *tm;
396 {
397 register LUV24DATA *ld;
398
399 ld = (LUV24DATA *)malloc(sizeof(LUV24DATA));
400 if (ld == NULL)
401 return(NULL);
402 tm->pd[luv24Reg] = (MEM_PTR)ld;
403 if (uv14neu < 0) /* initialize neutral color index */
404 uv14neu = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
405 luv24NewSpace(tm);
406 return((MEM_PTR)ld);
407 }