| 1 |
– |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 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 "uvcode.h" |
| 74 |
> |
#include "tmaptiff.h" |
| 75 |
|
|
| 76 |
|
#ifndef BSD |
| 77 |
|
#define bzero(d,n) (void)memset(d,0,n) |
| 82 |
|
#define clruv(p,uv) uvflgop(p,uv,&=~) |
| 83 |
|
#define clruvall(p) bzero((MEM_PTR)(p)->rgbflg,sizeof((p)->rgbflg)) |
| 84 |
|
|
| 85 |
< |
#define U_NEU 0.210526316 |
| 27 |
< |
#define V_NEU 0.473684211 |
| 28 |
< |
|
| 85 |
> |
#ifdef NOPROTO |
| 86 |
|
static MEM_PTR luv32Init(); |
| 87 |
|
static void luv32NewSpace(); |
| 88 |
|
static MEM_PTR luv24Init(); |
| 89 |
|
static void luv24NewSpace(); |
| 90 |
< |
extern void free(); |
| 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 */ |
| 100 |
|
uint32 rgbflg[1<<(16-5)]; /* flags for computed values */ |
| 101 |
|
} LUV32DATA; /* LogLuv 32-bit conversion data */ |
| 102 |
|
|
| 41 |
– |
#define UVSCALE 410. |
| 103 |
|
#define UVNEU ((int)(UVSCALE*U_NEU)<<8 \ |
| 104 |
|
| (int)(UVSCALE*V_NEU)) |
| 105 |
|
|
| 122 |
|
static int uv14neu = -1; /* neutral index for 14-bit (u',v') */ |
| 123 |
|
|
| 124 |
|
|
| 125 |
< |
static |
| 125 |
> |
static void |
| 126 |
|
uv2rgb(rgb, tm, uvp) /* compute RGB from uv coordinate */ |
| 127 |
|
BYTE rgb[3]; |
| 128 |
|
register struct tmStruct *tm; |
| 139 |
|
XYZ[CIEZ] = (1.-x-y)/y * XYZ[CIEY]; |
| 140 |
|
/* convert to RGB and clip */ |
| 141 |
|
colortrans(RGB, tm->cmat, XYZ); |
| 142 |
< |
clipgamut(RGB, XYZ[CIEY], CGAMUT_LOWER, cblack, cwhite); |
| 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)); |
| 155 |
|
TMbright li; /* encoded world luminance */ |
| 156 |
|
double uvp[2]; /* world (u',v') -> returned desaturated */ |
| 157 |
|
{ |
| 158 |
< |
double scotrat, d; |
| 158 |
> |
double scotrat; |
| 159 |
> |
register double d; |
| 160 |
|
|
| 161 |
|
if (li >= BMESUPPER) |
| 162 |
|
return(li); |
| 163 |
< |
scotrat = 1.33/9.*(6.*uvp[0]-16.*uvp[1]+12.)/uvp[0] - 1.68; |
| 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 = (tmLuminance(li) - LMESLOWER)/(LMESUPPER - LMESLOWER); |
| 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 |
< |
d = li + (double)TM_BRTSCALE*log(d + (1.-d)*scotrat/2.26); |
| 173 |
< |
return((TMbright)(d+.5)); |
| 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 |
|
|
| 115 |
– |
static int |
| 116 |
– |
uvpencode(uvp) /* encode (u',v') coordinates */ |
| 117 |
– |
double uvp[2]; |
| 118 |
– |
{ |
| 119 |
– |
register int vi, ui; |
| 120 |
– |
|
| 121 |
– |
if (uvp[1] < UV_VSTART) |
| 122 |
– |
return(-1); |
| 123 |
– |
vi = (uvp[1] - UV_VSTART)*(1./UV_SQSIZ); |
| 124 |
– |
if (vi >= UV_NVS) |
| 125 |
– |
return(-1); |
| 126 |
– |
if (uvp[0] < uv_row[vi].ustart) |
| 127 |
– |
return(-1); |
| 128 |
– |
ui = (uvp[0] - uv_row[vi].ustart)*(1./UV_SQSIZ); |
| 129 |
– |
if (ui >= uv_row[vi].nus) |
| 130 |
– |
return(-1); |
| 131 |
– |
return(uv_row[vi].ncum + ui); |
| 132 |
– |
} |
| 133 |
– |
|
| 134 |
– |
|
| 135 |
– |
static int |
| 136 |
– |
uvpdecode(uvp, c) /* decode (u',v') index */ |
| 137 |
– |
double uvp[2]; |
| 138 |
– |
int c; |
| 139 |
– |
{ |
| 140 |
– |
int upper, lower; |
| 141 |
– |
register int ui, vi; |
| 142 |
– |
|
| 143 |
– |
if (c < 0 || c >= UV_NDIVS) |
| 144 |
– |
return(-1); |
| 145 |
– |
lower = 0; /* binary search */ |
| 146 |
– |
upper = UV_NVS; |
| 147 |
– |
do { |
| 148 |
– |
vi = (lower + upper) >> 1; |
| 149 |
– |
ui = c - uv_row[vi].ncum; |
| 150 |
– |
if (ui > 0) |
| 151 |
– |
lower = vi; |
| 152 |
– |
else if (ui < 0) |
| 153 |
– |
upper = vi; |
| 154 |
– |
else |
| 155 |
– |
break; |
| 156 |
– |
} while (upper - lower > 1); |
| 157 |
– |
vi = lower; |
| 158 |
– |
ui = c - uv_row[vi].ncum; |
| 159 |
– |
uvp[0] = uv_row[vi].ustart + (ui+.5)*UV_SQSIZ; |
| 160 |
– |
uvp[1] = UV_VSTART + (vi+.5)*UV_SQSIZ; |
| 161 |
– |
return(0); |
| 162 |
– |
} |
| 163 |
– |
|
| 164 |
– |
|
| 183 |
|
int |
| 184 |
|
tmCvLuv32(ls, cs, luvs, len) /* convert raw 32-bit LogLuv values */ |
| 185 |
|
TMbright *ls; |
| 197 |
|
if (ls == NULL | luvs == NULL | len < 0) |
| 198 |
|
returnErr(TM_E_ILLEGAL); |
| 199 |
|
/* check package registration */ |
| 200 |
< |
if (luv32Reg < 0 && (luv32Reg = tmRegPkg(&luv32Pkg)) < 0) |
| 201 |
< |
returnErr(TM_E_CODERR1); |
| 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); |
| 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; |
| 214 |
> |
ls[i] = (BRT2SCALE(j) >> 8) - ld->offset; |
| 215 |
|
if (cs == TM_NOCHROM) /* no color? */ |
| 216 |
|
continue; |
| 217 |
|
/* get chrominance */ |
| 227 |
|
j = tmTop->flags&TM_F_BW ? UVNEU : luvs[i]&0xffff; |
| 228 |
|
} |
| 229 |
|
if (!isuvset(ld, j)) { |
| 230 |
< |
uvp[0] = 1./UVSCALE*((luvs[i]>>8 & 0xff) + .5); |
| 231 |
< |
uvp[1] = 1./UVSCALE*((luvs[i] & 0xff) + .5); |
| 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 |
|
} |
| 257 |
|
if (ls == NULL | luvs == NULL | len < 0) |
| 258 |
|
returnErr(TM_E_ILLEGAL); |
| 259 |
|
/* check package registration */ |
| 260 |
< |
if (luv24Reg < 0 && (luv24Reg = tmRegPkg(&luv24Pkg)) < 0) |
| 261 |
< |
returnErr(TM_E_CODERR1); |
| 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; |
| 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 (uvpdecode(uvp, luvs[i]&0x3fff) < 0) { |
| 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 = uvpencode(uvp)) < 0) |
| 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 (uvpdecode(uvp, j) < 0) { |
| 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); |
| 317 |
|
returnErr(TM_E_ILLEGAL); |
| 318 |
|
/* check scaling offset */ |
| 319 |
|
if (!FEQ(tmTop->inpsf, lastsf)) { |
| 320 |
< |
offset = BRT2SCALE*64; |
| 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) |
| 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; |
| 332 |
> |
ls[i] = (BRT2SCALE(l16s[i]) >> 8) - offset; |
| 333 |
|
} |
| 334 |
|
returnOK; |
| 335 |
|
} |
| 346 |
|
exit(1); |
| 347 |
|
} |
| 348 |
|
ld = (LUV32DATA *)tm->pd[luv32Reg]; |
| 349 |
< |
ld->offset = BRT2SCALE*64; |
| 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) |
| 375 |
|
struct tmStruct *tm; |
| 376 |
|
{ |
| 377 |
|
register LUV24DATA *ld; |
| 353 |
– |
double uvp[2]; |
| 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; |
| 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) |
| 400 |
|
if (ld == NULL) |
| 401 |
|
return(NULL); |
| 402 |
|
tm->pd[luv24Reg] = (MEM_PTR)ld; |
| 403 |
< |
if (uv14neu < 0) { /* initialize neutral color index */ |
| 404 |
< |
double uvp[2]; |
| 381 |
< |
uvp[0] = U_NEU; uvp[1] = V_NEU; |
| 382 |
< |
uv14neu = uvpencode(uvp); |
| 383 |
< |
} |
| 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 |
|
} |