ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapluv.c
(Generate patch)

Comparing ray/src/common/tmapluv.c (file contents):
Revision 3.1 by gwlarson, Thu Oct 22 13:53:06 1998 UTC vs.
Revision 3.6 by schorsch, Mon Jun 30 14:59:11 2003 UTC

# Line 1 | Line 1
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 + #include "copyright.h"
11 +
12 + #define LOGLUV_PUBLIC           1
13 +
14   #include <stdio.h>
15 + #include <string.h>
16   #include <math.h>
17   #include "tiffio.h"
18   #include "tmprivat.h"
19 < #include "uvcode.h"
19 > #include "tmaptiff.h"
20  
17 #ifndef BSD
18 #define bzero(d,n)              (void)memset(d,0,n)
19 #endif
21   #define uvflgop(p,uv,op)        ((p)->rgbflg[(uv)>>5] op (1L<<((uv)&0x1f)))
22   #define isuvset(p,uv)           uvflgop(p,uv,&)
23   #define setuv(p,uv)             uvflgop(p,uv,|=)
24   #define clruv(p,uv)             uvflgop(p,uv,&=~)
25 < #define clruvall(p)             bzero((MEM_PTR)(p)->rgbflg,sizeof((p)->rgbflg))
25 > #define clruvall(p)             memset((MEM_PTR)(p)->rgbflg,'\0',sizeof((p)->rgbflg))
26  
27 < #define U_NEU                   0.210526316
27 < #define V_NEU                   0.473684211
28 <
27 > #ifdef NOPROTO
28   static MEM_PTR  luv32Init();
29   static void     luv32NewSpace();
30   static MEM_PTR  luv24Init();
31   static void     luv24NewSpace();
32 < extern void     free();
32 > #else
33 > static MEM_PTR  luv32Init(struct tmStruct *);
34 > static void     luv32NewSpace(struct tmStruct *);
35 > static MEM_PTR  luv24Init(struct tmStruct *);
36 > static void     luv24NewSpace(struct tmStruct *);
37 > #endif
38  
39   typedef struct {
40          int     offset;                 /* computed luminance offset */
# Line 38 | Line 42 | typedef struct {
42          uint32  rgbflg[1<<(16-5)];      /* flags for computed values */
43   } LUV32DATA;                    /* LogLuv 32-bit conversion data */
44  
41 #define UVSCALE                 410.
45   #define UVNEU                   ((int)(UVSCALE*U_NEU)<<8 \
46                                          | (int)(UVSCALE*V_NEU))
47  
# Line 61 | Line 64 | static int     luv24Reg = -1;          /* 24-bit package reg. numb
64   static int      uv14neu = -1;           /* neutral index for 14-bit (u',v') */
65  
66  
67 < static
67 > static void
68   uv2rgb(rgb, tm, uvp)                    /* compute RGB from uv coordinate */
69   BYTE    rgb[3];
70   register struct tmStruct        *tm;
71   double  uvp[2];
72   {       /* Should check that tm->inppri==TM_XYZPRIM beforehand... */
73          double  d, x, y;
74 <        COLR    XYZ, RGB;
74 >        COLOR   XYZ, RGB;
75                                          /* convert to XYZ */
76          d = 1./(6.*uvp[0] - 16.*uvp[1] + 12.);
77          x = 9.*uvp[0] * d;
# Line 78 | Line 81 | double uvp[2];
81          XYZ[CIEZ] = (1.-x-y)/y * XYZ[CIEY];
82                                          /* convert to RGB and clip */
83          colortrans(RGB, tm->cmat, XYZ);
84 <        clipgamut(RGB, XYZ[CIEY], CGAMUT_LOWER, cblack, cwhite);
84 >        clipgamut(RGB, 1., CGAMUT_LOWER, cblack, cwhite);
85                                          /* perform final scaling & gamma */
86          d = tm->clf[RED] * RGB[RED];
87          rgb[RED] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tm->mongam));
# Line 94 | Line 97 | compmeshift(li, uvp)                   /* compute mesopic color shift
97   TMbright        li;             /* encoded world luminance */
98   double  uvp[2];                 /* world (u',v') -> returned desaturated */
99   {
100 <        /* UNIMPLEMENTED */
101 <        return(li);
99 < }
100 >        double  scotrat;
101 >        register double d;
102  
103 <
104 < static int
105 < uvpencode(uvp)                  /* encode (u',v') coordinates */
106 < double  uvp[2];
107 < {
108 <        register int    vi, ui;
109 <
110 <        if (uvp[1] < UV_VSTART)
111 <                return(-1);
112 <        vi = (uvp[1] - UV_VSTART)*(1./UV_SQSIZ);
113 <        if (vi >= UV_NVS)
114 <                return(-1);
115 <        if (uvp[0] < uv_row[vi].ustart)
116 <                return(-1);
117 <        ui = (uvp[0] - uv_row[vi].ustart)*(1./UV_SQSIZ);
118 <        if (ui >= uv_row[vi].nus)
119 <                return(-1);
120 <        return(uv_row[vi].ncum + ui);
103 >        if (li >= BMESUPPER)
104 >                return(li);
105 >        scotrat = (.767676768 - 1.02356902*uvp[1])/uvp[0] - .343434343;
106 >        if (li <= BMESLOWER) {
107 >                d = 0.;
108 >                uvp[0] = U_NEU; uvp[1] = V_NEU;
109 >        } else {
110 >                d = (tmMesofact[li-BMESLOWER] + .5) * (1./256.);
111 >                uvp[0] = d*uvp[0] + (1.-d)*U_NEU;
112 >                uvp[1] = d*uvp[1] + (1.-d)*V_NEU;
113 >        }
114 >        /*
115 >        d = li + (double)TM_BRTSCALE*log(d + (1.-d)*scotrat);
116 >        */
117 >        d = d + (1.-d)*scotrat;
118 >        d -= 1.;                        /* Taylor expansion of log(x) about 1 */
119 >        d = d*(1. + d*(-.5 + d*(1./3. + d*-.125)));
120 >        d = li + (double)TM_BRTSCALE*d;
121 >        return((TMbright)(d>0. ? d+.5 : d-.5));
122   }
123  
124  
122 static int
123 uvpdecode(uvp, c)               /* decode (u',v') index */
124 double  uvp[2];
125 int     c;
126 {
127        int     upper, lower;
128        register int    ui, vi;
129
130        if (c < 0 || c >= UV_NDIVS)
131                return(-1);
132        lower = 0;                      /* binary search */
133        upper = UV_NVS;
134        do {
135                vi = (lower + upper) >> 1;
136                ui = c - uv_row[vi].ncum;
137                if (ui > 0)
138                        lower = vi;
139                else if (ui < 0)
140                        upper = vi;
141                else
142                        break;
143        } while (upper - lower > 1);
144        vi = lower;
145        ui = c - uv_row[vi].ncum;
146        uvp[0] = uv_row[vi].ustart + (ui+.5)*UV_SQSIZ;
147        uvp[1] = UV_VSTART + (vi+.5)*UV_SQSIZ;
148        return(0);
149 }
150
151
125   int
126   tmCvLuv32(ls, cs, luvs, len)            /* convert raw 32-bit LogLuv values */
127   TMbright        *ls;
# Line 166 | Line 139 | int    len;
139          if (ls == NULL | luvs == NULL | len < 0)
140                  returnErr(TM_E_ILLEGAL);
141                                          /* check package registration */
142 <        if (luv32Reg < 0 && (luv32Reg = tmRegPkg(&luv32Pkg)) < 0)
143 <                returnErr(TM_E_CODERR1);
142 >        if (luv32Reg < 0) {
143 >                if ((luv32Reg = tmRegPkg(&luv32Pkg)) < 0)
144 >                        returnErr(TM_E_CODERR1);
145 >                tmMkMesofact();
146 >        }
147                                          /* get package data */
148          if ((ld = (LUV32DATA *)tmPkgData(tmTop,luv32Reg)) == NULL)
149                  returnErr(TM_E_NOMEM);
# Line 175 | Line 151 | int    len;
151          for (i = len; i--; ) {
152                  j = luvs[i] >> 16;              /* get luminance */
153                  if (j & 0x8000)                 /* negative luminance */
154 <                        ls[i] = MINBRT-1;       /* assign bogus value */
154 >                        ls[i] = TM_NOBRT;       /* assign bogus value */
155                  else                            /* else convert to lnL */
156 <                        ls[i] = (BRT2SCALE*j >> 8) - ld->offset;
156 >                        ls[i] = (BRT2SCALE(j) >> 8) - ld->offset;
157                  if (cs == TM_NOCHROM)           /* no color? */
158                          continue;
159                                                  /* get chrominance */
# Line 185 | Line 161 | int    len;
161                          uvp[0] = 1./UVSCALE*((luvs[i]>>8 & 0xff) + .5);
162                          uvp[1] = 1./UVSCALE*((luvs[i] & 0xff) + .5);
163                          ls[i] = compmeshift(ls[i], uvp);
164 <                        j = tmTop->flags & TM_F_BW || ls[i] < BMESLOWER
164 >                        j = tmTop->flags&TM_F_BW || ls[i]<BMESLOWER
165                                          ? UVNEU
166                                          : (int)(uvp[0]*UVSCALE)<<8
167                                                  | (int)(uvp[1]*UVSCALE);
# Line 193 | Line 169 | int    len;
169                          j = tmTop->flags&TM_F_BW ? UVNEU : luvs[i]&0xffff;
170                  }
171                  if (!isuvset(ld, j)) {
172 <                        uvp[0] = 1./UVSCALE*((luvs[i]>>8 & 0xff) + .5);
173 <                        uvp[1] = 1./UVSCALE*((luvs[i] & 0xff) + .5);
172 >                        uvp[0] = 1./UVSCALE*((j>>8) + .5);
173 >                        uvp[1] = 1./UVSCALE*((j & 0xff) + .5);
174                          uv2rgb(ld->rgbval[j], tmTop, uvp);
175                          setuv(ld, j);
176                  }
# Line 223 | Line 199 | int    len;
199          if (ls == NULL | luvs == NULL | len < 0)
200                  returnErr(TM_E_ILLEGAL);
201                                          /* check package registration */
202 <        if (luv24Reg < 0 && (luv24Reg = tmRegPkg(&luv24Pkg)) < 0)
203 <                returnErr(TM_E_CODERR1);
202 >        if (luv24Reg < 0) {
203 >                if ((luv24Reg = tmRegPkg(&luv24Pkg)) < 0)
204 >                        returnErr(TM_E_CODERR1);
205 >                tmMkMesofact();
206 >        }
207                                          /* get package data */
208          if ((ld = (LUV24DATA *)tmPkgData(tmTop,luv24Reg)) == NULL)
209                  returnErr(TM_E_NOMEM);
210                                          /* convert each pixel */
211          for (i = len; i--; ) {
212                  j = luvs[i] >> 14;              /* get luminance */
213 <                ls[i] = (BRT2SCALE*j >> 6) - ld->offset;
213 >                ls[i] = (BRT2SCALE(j) >> 6) - ld->offset;
214                  if (cs == TM_NOCHROM)           /* no color? */
215                          continue;
216                                                  /* get chrominance */
217                  if (tmTop->flags & TM_F_MESOPIC && ls[i] < BMESUPPER) {
218 <                        if (uvpdecode(uvp, luvs[i]&0x3fff) < 0) {
218 >                        if (uv_decode(uvp, uvp+1, luvs[i]&0x3fff) < 0) {
219                                  uvp[0] = U_NEU;         /* should barf? */
220                                  uvp[1] = V_NEU;
221                          }
222                          ls[i] = compmeshift(ls[i], uvp);
223 <                        if (tmTop->flags & TM_F_BW || ls[i] < BMESLOWER
224 <                                        || (j = uvpencode(uvp)) < 0)
223 >                        if (tmTop->flags&TM_F_BW || ls[i]<BMESLOWER
224 >                                        || (j = uv_encode(uvp[0], uvp[1],
225 >                                                SGILOGENCODE_NODITHER)) < 0)
226                                  j = uv14neu;
227                  } else {
228                          j = tmTop->flags&TM_F_BW ? uv14neu : luvs[i]&0x3fff;
229                  }
230                  if (!isuvset(ld, j)) {
231 <                        if (uvpdecode(uvp, j) < 0) {
232 <                                uvp[0] = U_NEU;
253 <                                uvp[1] = V_NEU;
231 >                        if (uv_decode(&uvp[0], &uvp[1], j) < 0) {
232 >                                uvp[0] = U_NEU; uvp[1] = V_NEU;
233                          }
234                          uv2rgb(ld->rgbval[j], tmTop, uvp);
235                          setuv(ld, j);
# Line 280 | Line 259 | int    len;
259                  returnErr(TM_E_ILLEGAL);
260                                          /* check scaling offset */
261          if (!FEQ(tmTop->inpsf, lastsf)) {
262 <                offset = BRT2SCALE*64;
262 >                offset = BRT2SCALE(64);
263                  if (tmTop->inpsf > 1.0001)
264                          offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)+.5);
265                  else if (tmTop->inpsf < 0.9999)
# Line 290 | Line 269 | int    len;
269                                          /* convert each pixel */
270          for (i = len; i--; ) {
271                  if (l16s[i] & 0x8000)           /* negative luminance */
272 <                        ls[i] = MINBRT-1;       /* assign bogus value */
272 >                        ls[i] = TM_NOBRT;       /* assign bogus value */
273                  else                            /* else convert to lnL */
274 <                        ls[i] = (BRT2SCALE*l16s[i] >> 8) - offset;
274 >                        ls[i] = (BRT2SCALE(l16s[i]) >> 8) - offset;
275          }
276          returnOK;
277   }
# Line 309 | Line 288 | struct tmStruct        *tm;
288                  exit(1);
289          }
290          ld = (LUV32DATA *)tm->pd[luv32Reg];
291 <        ld->offset = BRT2SCALE*64;
291 >        ld->offset = BRT2SCALE(64);
292          if (tm->inpsf > 1.0001)
293                  ld->offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)+.5);
294          else if (tm->inpsf < 0.9999)
# Line 338 | Line 317 | luv24NewSpace(tm)              /* initialize 24-bit LogLuv color s
317   struct tmStruct *tm;
318   {
319          register LUV24DATA      *ld;
341        double  uvp[2];
320  
321          if (tm->inppri != TM_XYZPRIM) {         /* panic time! */
322                  fputs("Improper input color space in luv24NewSpace!\n", stderr);
323                  exit(1);
324          }
325          ld = (LUV24DATA *)tm->pd[luv24Reg];
326 <        ld->offset = BRT2SCALE*12;
326 >        ld->offset = BRT2SCALE(12);
327          if (tm->inpsf > 1.0001)
328                  ld->offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)+.5);
329          else if (tm->inpsf < 0.9999)
# Line 364 | Line 342 | struct tmStruct        *tm;
342          if (ld == NULL)
343                  return(NULL);
344          tm->pd[luv24Reg] = (MEM_PTR)ld;
345 <        if (uv14neu < 0) {              /* initialize neutral color index */
346 <                double  uvp[2];
369 <                uvp[0] = U_NEU; uvp[1] = V_NEU;
370 <                uv14neu = uvpencode(uvp);
371 <        }
345 >        if (uv14neu < 0)                /* initialize neutral color index */
346 >                uv14neu = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
347          luv24NewSpace(tm);
348          return((MEM_PTR)ld);
349   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines