--- ray/src/common/tmapluv.c 1998/10/26 17:07:05 3.2 +++ ray/src/common/tmapluv.c 2003/07/27 22:12:01 3.9 @@ -1,36 +1,40 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: tmapluv.c,v 3.9 2003/07/27 22:12:01 schorsch Exp $"; #endif - /* * Routines for tone-mapping LogLuv encoded pixels. + * + * Externals declared in tmaptiff.h */ +#include "copyright.h" + +#define LOGLUV_PUBLIC 1 + #include +#include #include #include "tiffio.h" #include "tmprivat.h" -#include "uvcode.h" +#include "tmaptiff.h" -#ifndef BSD -#define bzero(d,n) (void)memset(d,0,n) -#endif #define uvflgop(p,uv,op) ((p)->rgbflg[(uv)>>5] op (1L<<((uv)&0x1f))) #define isuvset(p,uv) uvflgop(p,uv,&) #define setuv(p,uv) uvflgop(p,uv,|=) #define clruv(p,uv) uvflgop(p,uv,&=~) -#define clruvall(p) bzero((MEM_PTR)(p)->rgbflg,sizeof((p)->rgbflg)) +#define clruvall(p) memset((MEM_PTR)(p)->rgbflg,'\0',sizeof((p)->rgbflg)) -#define U_NEU 0.210526316 -#define V_NEU 0.473684211 - +#ifdef NOPROTO static MEM_PTR luv32Init(); static void luv32NewSpace(); static MEM_PTR luv24Init(); static void luv24NewSpace(); -extern void free(); +#else +static MEM_PTR luv32Init(struct tmStruct *); +static void luv32NewSpace(struct tmStruct *); +static MEM_PTR luv24Init(struct tmStruct *); +static void luv24NewSpace(struct tmStruct *); +#endif typedef struct { int offset; /* computed luminance offset */ @@ -38,7 +42,6 @@ typedef struct { uint32 rgbflg[1<<(16-5)]; /* flags for computed values */ } LUV32DATA; /* LogLuv 32-bit conversion data */ -#define UVSCALE 410. #define UVNEU ((int)(UVSCALE*U_NEU)<<8 \ | (int)(UVSCALE*V_NEU)) @@ -61,7 +64,7 @@ static int luv24Reg = -1; /* 24-bit package reg. numb static int uv14neu = -1; /* neutral index for 14-bit (u',v') */ -static +static void uv2rgb(rgb, tm, uvp) /* compute RGB from uv coordinate */ BYTE rgb[3]; register struct tmStruct *tm; @@ -78,7 +81,7 @@ double uvp[2]; XYZ[CIEZ] = (1.-x-y)/y * XYZ[CIEY]; /* convert to RGB and clip */ colortrans(RGB, tm->cmat, XYZ); - clipgamut(RGB, XYZ[CIEY], CGAMUT_LOWER, cblack, cwhite); + clipgamut(RGB, 1., CGAMUT_LOWER, cblack, cwhite); /* perform final scaling & gamma */ d = tm->clf[RED] * RGB[RED]; rgb[RED] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tm->mongam)); @@ -94,74 +97,31 @@ compmeshift(li, uvp) /* compute mesopic color shift TMbright li; /* encoded world luminance */ double uvp[2]; /* world (u',v') -> returned desaturated */ { - double scotrat, d; + double scotrat; + register double d; if (li >= BMESUPPER) return(li); - scotrat = 1.33/9.*(6.*uvp[0]-16.*uvp[1]+12.)/uvp[0] - 1.68; + scotrat = (.767676768 - 1.02356902*uvp[1])/uvp[0] - .343434343; if (li <= BMESLOWER) { d = 0.; uvp[0] = U_NEU; uvp[1] = V_NEU; } else { - d = (tmLuminance(li) - LMESLOWER)/(LMESUPPER - LMESLOWER); + d = (tmMesofact[li-BMESLOWER] + .5) * (1./256.); uvp[0] = d*uvp[0] + (1.-d)*U_NEU; uvp[1] = d*uvp[1] + (1.-d)*V_NEU; } - d = li + (double)TM_BRTSCALE*log(d + (1.-d)*scotrat/2.26); - return((TMbright)(d+.5)); + /* + d = li + (double)TM_BRTSCALE*log(d + (1.-d)*scotrat); + */ + d = d + (1.-d)*scotrat; + d -= 1.; /* Taylor expansion of log(x) about 1 */ + d = d*(1. + d*(-.5 + d*(1./3. + d*-.125))); + d = li + (double)TM_BRTSCALE*d; + return((TMbright)(d>0. ? d+.5 : d-.5)); } -static int -uvpencode(uvp) /* encode (u',v') coordinates */ -double uvp[2]; -{ - register int vi, ui; - - if (uvp[1] < UV_VSTART) - return(-1); - vi = (uvp[1] - UV_VSTART)*(1./UV_SQSIZ); - if (vi >= UV_NVS) - return(-1); - if (uvp[0] < uv_row[vi].ustart) - return(-1); - ui = (uvp[0] - uv_row[vi].ustart)*(1./UV_SQSIZ); - if (ui >= uv_row[vi].nus) - return(-1); - return(uv_row[vi].ncum + ui); -} - - -static int -uvpdecode(uvp, c) /* decode (u',v') index */ -double uvp[2]; -int c; -{ - int upper, lower; - register int ui, vi; - - if (c < 0 || c >= UV_NDIVS) - return(-1); - lower = 0; /* binary search */ - upper = UV_NVS; - do { - vi = (lower + upper) >> 1; - ui = c - uv_row[vi].ncum; - if (ui > 0) - lower = vi; - else if (ui < 0) - upper = vi; - else - break; - } while (upper - lower > 1); - vi = lower; - ui = c - uv_row[vi].ncum; - uvp[0] = uv_row[vi].ustart + (ui+.5)*UV_SQSIZ; - uvp[1] = UV_VSTART + (vi+.5)*UV_SQSIZ; - return(0); -} - - int tmCvLuv32(ls, cs, luvs, len) /* convert raw 32-bit LogLuv values */ TMbright *ls; @@ -176,11 +136,14 @@ int len; /* check arguments */ if (tmTop == NULL) returnErr(TM_E_TMINVAL); - if (ls == NULL | luvs == NULL | len < 0) + if ((ls == NULL) | (luvs == NULL) | (len < 0)) returnErr(TM_E_ILLEGAL); /* check package registration */ - if (luv32Reg < 0 && (luv32Reg = tmRegPkg(&luv32Pkg)) < 0) - returnErr(TM_E_CODERR1); + if (luv32Reg < 0) { + if ((luv32Reg = tmRegPkg(&luv32Pkg)) < 0) + returnErr(TM_E_CODERR1); + tmMkMesofact(); + } /* get package data */ if ((ld = (LUV32DATA *)tmPkgData(tmTop,luv32Reg)) == NULL) returnErr(TM_E_NOMEM); @@ -188,9 +151,9 @@ int len; for (i = len; i--; ) { j = luvs[i] >> 16; /* get luminance */ if (j & 0x8000) /* negative luminance */ - ls[i] = MINBRT-1; /* assign bogus value */ + ls[i] = TM_NOBRT; /* assign bogus value */ else /* else convert to lnL */ - ls[i] = (BRT2SCALE*j >> 8) - ld->offset; + ls[i] = (BRT2SCALE(j) >> 8) - ld->offset; if (cs == TM_NOCHROM) /* no color? */ continue; /* get chrominance */ @@ -206,8 +169,8 @@ int len; j = tmTop->flags&TM_F_BW ? UVNEU : luvs[i]&0xffff; } if (!isuvset(ld, j)) { - uvp[0] = 1./UVSCALE*((luvs[i]>>8 & 0xff) + .5); - uvp[1] = 1./UVSCALE*((luvs[i] & 0xff) + .5); + uvp[0] = 1./UVSCALE*((j>>8) + .5); + uvp[1] = 1./UVSCALE*((j & 0xff) + .5); uv2rgb(ld->rgbval[j], tmTop, uvp); setuv(ld, j); } @@ -233,35 +196,40 @@ int len; /* check arguments */ if (tmTop == NULL) returnErr(TM_E_TMINVAL); - if (ls == NULL | luvs == NULL | len < 0) + if ((ls == NULL) | (luvs == NULL) | (len < 0)) returnErr(TM_E_ILLEGAL); /* check package registration */ - if (luv24Reg < 0 && (luv24Reg = tmRegPkg(&luv24Pkg)) < 0) - returnErr(TM_E_CODERR1); + if (luv24Reg < 0) { + if ((luv24Reg = tmRegPkg(&luv24Pkg)) < 0) + returnErr(TM_E_CODERR1); + tmMkMesofact(); + } /* get package data */ if ((ld = (LUV24DATA *)tmPkgData(tmTop,luv24Reg)) == NULL) returnErr(TM_E_NOMEM); /* convert each pixel */ for (i = len; i--; ) { j = luvs[i] >> 14; /* get luminance */ - ls[i] = (BRT2SCALE*j >> 6) - ld->offset; + ls[i] = (BRT2SCALE(j) >> 6) - ld->offset; if (cs == TM_NOCHROM) /* no color? */ continue; /* get chrominance */ if (tmTop->flags & TM_F_MESOPIC && ls[i] < BMESUPPER) { - if (uvpdecode(uvp, luvs[i]&0x3fff) < 0) { + if (uv_decode(&uvp[0], &uvp[1], luvs[i]&0x3fff) < 0) { uvp[0] = U_NEU; /* should barf? */ uvp[1] = V_NEU; } ls[i] = compmeshift(ls[i], uvp); if (tmTop->flags&TM_F_BW || ls[i]flags&TM_F_BW ? uv14neu : luvs[i]&0x3fff; + j = tmTop->flags&TM_F_BW ? uv14neu : + (int)(luvs[i]&0x3fff); } if (!isuvset(ld, j)) { - if (uvpdecode(uvp, j) < 0) { + if (uv_decode(&uvp[0], &uvp[1], j) < 0) { uvp[0] = U_NEU; uvp[1] = V_NEU; } uv2rgb(ld->rgbval[j], tmTop, uvp); @@ -288,11 +256,11 @@ int len; /* check arguments */ if (tmTop == NULL) returnErr(TM_E_TMINVAL); - if (ls == NULL | l16s == NULL | len < 0) + if ((ls == NULL) | (l16s == NULL) | (len < 0)) returnErr(TM_E_ILLEGAL); /* check scaling offset */ if (!FEQ(tmTop->inpsf, lastsf)) { - offset = BRT2SCALE*64; + offset = BRT2SCALE(64); if (tmTop->inpsf > 1.0001) offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)+.5); else if (tmTop->inpsf < 0.9999) @@ -302,9 +270,9 @@ int len; /* convert each pixel */ for (i = len; i--; ) { if (l16s[i] & 0x8000) /* negative luminance */ - ls[i] = MINBRT-1; /* assign bogus value */ + ls[i] = TM_NOBRT; /* assign bogus value */ else /* else convert to lnL */ - ls[i] = (BRT2SCALE*l16s[i] >> 8) - offset; + ls[i] = (BRT2SCALE(l16s[i]) >> 8) - offset; } returnOK; } @@ -321,7 +289,7 @@ struct tmStruct *tm; exit(1); } ld = (LUV32DATA *)tm->pd[luv32Reg]; - ld->offset = BRT2SCALE*64; + ld->offset = BRT2SCALE(64); if (tm->inpsf > 1.0001) ld->offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)+.5); else if (tm->inpsf < 0.9999) @@ -350,14 +318,13 @@ luv24NewSpace(tm) /* initialize 24-bit LogLuv color s struct tmStruct *tm; { register LUV24DATA *ld; - double uvp[2]; if (tm->inppri != TM_XYZPRIM) { /* panic time! */ fputs("Improper input color space in luv24NewSpace!\n", stderr); exit(1); } ld = (LUV24DATA *)tm->pd[luv24Reg]; - ld->offset = BRT2SCALE*12; + ld->offset = BRT2SCALE(12); if (tm->inpsf > 1.0001) ld->offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)+.5); else if (tm->inpsf < 0.9999) @@ -376,11 +343,8 @@ struct tmStruct *tm; if (ld == NULL) return(NULL); tm->pd[luv24Reg] = (MEM_PTR)ld; - if (uv14neu < 0) { /* initialize neutral color index */ - double uvp[2]; - uvp[0] = U_NEU; uvp[1] = V_NEU; - uv14neu = uvpencode(uvp); - } + if (uv14neu < 0) /* initialize neutral color index */ + uv14neu = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER); luv24NewSpace(tm); return((MEM_PTR)ld); }