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.14 by greg, Mon Feb 9 20:48:08 2009 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 <stdlib.h>
16 + #include <string.h>
17   #include <math.h>
13 #include "tiffio.h"
18   #include "tmprivat.h"
19 < #include "uvcode.h"
19 > #include "tiffio.h"
20 > #include "tmaptiff.h"
21  
17 #ifndef BSD
18 #define bzero(d,n)              (void)memset(d,0,n)
19 #endif
22   #define uvflgop(p,uv,op)        ((p)->rgbflg[(uv)>>5] op (1L<<((uv)&0x1f)))
23   #define isuvset(p,uv)           uvflgop(p,uv,&)
24   #define setuv(p,uv)             uvflgop(p,uv,|=)
25   #define clruv(p,uv)             uvflgop(p,uv,&=~)
26 < #define clruvall(p)             bzero((MEM_PTR)(p)->rgbflg,sizeof((p)->rgbflg))
26 > #define clruvall(p)             memset((MEM_PTR)(p)->rgbflg,'\0',sizeof((p)->rgbflg))
27  
28 < #define U_NEU                   0.210526316
27 < #define V_NEU                   0.473684211
28 <
28 > #ifdef NOPROTO
29   static MEM_PTR  luv32Init();
30   static void     luv32NewSpace();
31   static MEM_PTR  luv24Init();
32   static void     luv24NewSpace();
33 < extern void     free();
33 > #else
34 > static MEM_PTR  luv32Init(TMstruct *);
35 > static void     luv32NewSpace(TMstruct *);
36 > static MEM_PTR  luv24Init(TMstruct *);
37 > static void     luv24NewSpace(TMstruct *);
38 > #endif
39  
40   typedef struct {
41          int     offset;                 /* computed luminance offset */
# Line 38 | Line 43 | typedef struct {
43          uint32  rgbflg[1<<(16-5)];      /* flags for computed values */
44   } LUV32DATA;                    /* LogLuv 32-bit conversion data */
45  
41 #define UVSCALE                 410.
46   #define UVNEU                   ((int)(UVSCALE*U_NEU)<<8 \
47                                          | (int)(UVSCALE*V_NEU))
48  
# Line 61 | Line 65 | static int     luv24Reg = -1;          /* 24-bit package reg. numb
65   static int      uv14neu = -1;           /* neutral index for 14-bit (u',v') */
66  
67  
68 < static
69 < uv2rgb(rgb, tm, uvp)                    /* compute RGB from uv coordinate */
68 > static void
69 > uv2rgb(rgb, tms, uvp)                   /* compute RGB from uv coordinate */
70   BYTE    rgb[3];
71 < register struct tmStruct        *tm;
71 > register TMstruct       *tms;
72   double  uvp[2];
73 < {       /* Should check that tm->inppri==TM_XYZPRIM beforehand... */
73 > {       /* Should check that tms->inppri==TM_XYZPRIM beforehand... */
74          double  d, x, y;
75 <        COLR    XYZ, RGB;
75 >        COLOR   XYZ, RGB;
76                                          /* convert to XYZ */
77          d = 1./(6.*uvp[0] - 16.*uvp[1] + 12.);
78          x = 9.*uvp[0] * d;
79          y = 4.*uvp[1] * d;
80 <        XYZ[CIEY] = 1./tm->inpsf;
80 >        XYZ[CIEY] = 1./tms->inpsf;
81          XYZ[CIEX] = x/y * XYZ[CIEY];
82          XYZ[CIEZ] = (1.-x-y)/y * XYZ[CIEY];
83                                          /* convert to RGB and clip */
84 <        colortrans(RGB, tm->cmat, XYZ);
85 <        clipgamut(RGB, XYZ[CIEY], CGAMUT_LOWER, cblack, cwhite);
84 >        colortrans(RGB, tms->cmat, XYZ);
85 >        clipgamut(RGB, 1., CGAMUT_LOWER, cblack, cwhite);
86                                          /* perform final scaling & gamma */
87 <        d = tm->clf[RED] * RGB[RED];
88 <        rgb[RED] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tm->mongam));
89 <        d = tm->clf[GRN] * RGB[GRN];
90 <        rgb[GRN] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tm->mongam));
91 <        d = tm->clf[BLU] * RGB[BLU];
92 <        rgb[BLU] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tm->mongam));
87 >        d = tms->clf[RED] * RGB[RED];
88 >        rgb[RED] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tms->mongam));
89 >        d = tms->clf[GRN] * RGB[GRN];
90 >        rgb[GRN] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tms->mongam));
91 >        d = tms->clf[BLU] * RGB[BLU];
92 >        rgb[BLU] = d>=.999 ? 255 : (int)(256.*pow(d, 1./tms->mongam));
93   }
94  
95  
# Line 94 | Line 98 | compmeshift(li, uvp)                   /* compute mesopic color shift
98   TMbright        li;             /* encoded world luminance */
99   double  uvp[2];                 /* world (u',v') -> returned desaturated */
100   {
101 <        /* UNIMPLEMENTED */
102 <        return(li);
99 < }
101 >        double  scotrat;
102 >        register double d;
103  
104 <
105 < static int
106 < uvpencode(uvp)                  /* encode (u',v') coordinates */
107 < double  uvp[2];
108 < {
109 <        register int    vi, ui;
110 <
111 <        if (uvp[1] < UV_VSTART)
112 <                return(-1);
113 <        vi = (uvp[1] - UV_VSTART)*(1./UV_SQSIZ);
114 <        if (vi >= UV_NVS)
115 <                return(-1);
116 <        if (uvp[0] < uv_row[vi].ustart)
117 <                return(-1);
118 <        ui = (uvp[0] - uv_row[vi].ustart)*(1./UV_SQSIZ);
119 <        if (ui >= uv_row[vi].nus)
120 <                return(-1);
121 <        return(uv_row[vi].ncum + ui);
104 >        if (li >= BMESUPPER)
105 >                return(li);
106 >        scotrat = (.767676768 - 1.02356902*uvp[1])/uvp[0] - .343434343;
107 >        if (li <= BMESLOWER) {
108 >                d = 0.;
109 >                uvp[0] = U_NEU; uvp[1] = V_NEU;
110 >        } else {
111 >                d = (tmMesofact[li-BMESLOWER] + .5) * (1./256.);
112 >                uvp[0] = d*uvp[0] + (1.-d)*U_NEU;
113 >                uvp[1] = d*uvp[1] + (1.-d)*V_NEU;
114 >        }
115 >        /*
116 >        d = li + (double)TM_BRTSCALE*log(d + (1.-d)*scotrat);
117 >        */
118 >        d = d + (1.-d)*scotrat;
119 >        d -= 1.;                        /* Taylor expansion of log(x) about 1 */
120 >        d = d*(1. + d*(-.5 + d*(1./3. + d*-.125)));
121 >        d = li + (double)TM_BRTSCALE*d;
122 >        return((TMbright)(d>0. ? d+.5 : d-.5));
123   }
124  
125  
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
126   int
127 < tmCvLuv32(ls, cs, luvs, len)            /* convert raw 32-bit LogLuv values */
128 < TMbright        *ls;
129 < BYTE    *cs;
130 < uint32  *luvs;
131 < int     len;
127 > tmCvLuv32(                              /* convert raw 32-bit LogLuv values */
128 > TMstruct        *tms,
129 > TMbright        *ls,
130 > BYTE    *cs,
131 > uint32  *luvs,
132 > int     len
133 > )
134   {
135 <        static char     funcName[] = "tmCvLuv32";
135 >        static const char       funcName[] = "tmCvLuv32";
136          double  uvp[2];
137          register LUV32DATA      *ld;
138          register int    i, j;
139                                          /* check arguments */
140 <        if (tmTop == NULL)
140 >        if (tms == NULL)
141                  returnErr(TM_E_TMINVAL);
142 <        if (ls == NULL | luvs == NULL | len < 0)
142 >        if ((ls == NULL) | (luvs == NULL) | (len < 0))
143                  returnErr(TM_E_ILLEGAL);
144                                          /* check package registration */
145 <        if (luv32Reg < 0 && (luv32Reg = tmRegPkg(&luv32Pkg)) < 0)
146 <                returnErr(TM_E_CODERR1);
145 >        if (luv32Reg < 0) {
146 >                if ((luv32Reg = tmRegPkg(&luv32Pkg)) < 0)
147 >                        returnErr(TM_E_CODERR1);
148 >                tmMkMesofact();
149 >        }
150                                          /* get package data */
151 <        if ((ld = (LUV32DATA *)tmPkgData(tmTop,luv32Reg)) == NULL)
151 >        if ((ld = (LUV32DATA *)tmPkgData(tms,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 <                        ls[i] = MINBRT-1;       /* assign bogus value */
157 >                        ls[i] = TM_NOBRT;       /* assign bogus value */
158                  else                            /* else convert to lnL */
159 <                        ls[i] = (BRT2SCALE*j >> 8) - ld->offset;
159 >                        ls[i] = (BRT2SCALE(j) >> 8) - ld->offset;
160                  if (cs == TM_NOCHROM)           /* no color? */
161                          continue;
162                                                  /* get chrominance */
163 <                if (tmTop->flags & TM_F_MESOPIC && ls[i] < BMESUPPER) {
163 >                if (tms->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 <                        j = tmTop->flags & TM_F_BW || ls[i] < BMESLOWER
167 >                        j = tms->flags&TM_F_BW || ls[i]<BMESLOWER
168                                          ? 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;
172 >                        j = tms->flags&TM_F_BW ? UVNEU : luvs[i]&0xffff;
173                  }
174                  if (!isuvset(ld, j)) {
175 <                        uvp[0] = 1./UVSCALE*((luvs[i]>>8 & 0xff) + .5);
176 <                        uvp[1] = 1./UVSCALE*((luvs[i] & 0xff) + .5);
177 <                        uv2rgb(ld->rgbval[j], tmTop, uvp);
175 >                        uvp[0] = 1./UVSCALE*((j>>8) + .5);
176 >                        uvp[1] = 1./UVSCALE*((j & 0xff) + .5);
177 >                        uv2rgb(ld->rgbval[j], tms, uvp);
178                          setuv(ld, j);
179                  }
180                  cs[3*i  ] = ld->rgbval[j][RED];
# Line 207 | Line 186 | int    len;
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;
189 > tmCvLuv24(                      /* convert raw 24-bit LogLuv values */
190 > TMstruct        *tms,
191 > TMbright        *ls,
192 > BYTE    *cs,
193 > uint32  *luvs,
194 > int     len
195 > )
196   {
197          char    funcName[] = "tmCvLuv24";
198          double  uvp[2];
199          register LUV24DATA      *ld;
200          register int    i, j;
201                                          /* check arguments */
202 <        if (tmTop == NULL)
202 >        if (tms == NULL)
203                  returnErr(TM_E_TMINVAL);
204 <        if (ls == NULL | luvs == NULL | len < 0)
204 >        if ((ls == NULL) | (luvs == NULL) | (len < 0))
205                  returnErr(TM_E_ILLEGAL);
206                                          /* check package registration */
207 <        if (luv24Reg < 0 && (luv24Reg = tmRegPkg(&luv24Pkg)) < 0)
208 <                returnErr(TM_E_CODERR1);
207 >        if (luv24Reg < 0) {
208 >                if ((luv24Reg = tmRegPkg(&luv24Pkg)) < 0)
209 >                        returnErr(TM_E_CODERR1);
210 >                tmMkMesofact();
211 >        }
212                                          /* get package data */
213 <        if ((ld = (LUV24DATA *)tmPkgData(tmTop,luv24Reg)) == NULL)
213 >        if ((ld = (LUV24DATA *)tmPkgData(tms,luv24Reg)) == NULL)
214                  returnErr(TM_E_NOMEM);
215                                          /* convert each pixel */
216          for (i = len; i--; ) {
217                  j = luvs[i] >> 14;              /* get luminance */
218 <                ls[i] = (BRT2SCALE*j >> 6) - ld->offset;
218 >                ls[i] = (BRT2SCALE(j) >> 6) - ld->offset;
219                  if (cs == TM_NOCHROM)           /* no color? */
220                          continue;
221                                                  /* get chrominance */
222 <                if (tmTop->flags & TM_F_MESOPIC && ls[i] < BMESUPPER) {
223 <                        if (uvpdecode(uvp, luvs[i]&0x3fff) < 0) {
222 >                if (tms->flags & TM_F_MESOPIC && ls[i] < BMESUPPER) {
223 >                        if (uv_decode(&uvp[0], &uvp[1], luvs[i]&0x3fff) < 0) {
224                                  uvp[0] = U_NEU;         /* should barf? */
225                                  uvp[1] = V_NEU;
226                          }
227                          ls[i] = compmeshift(ls[i], uvp);
228 <                        if (tmTop->flags & TM_F_BW || ls[i] < BMESLOWER
229 <                                        || (j = uvpencode(uvp)) < 0)
228 >                        if (tms->flags&TM_F_BW || ls[i]<BMESLOWER
229 >                                        || (j = uv_encode(uvp[0], uvp[1],
230 >                                                SGILOGENCODE_NODITHER)) < 0)
231                                  j = uv14neu;
232                  } else {
233 <                        j = tmTop->flags&TM_F_BW ? uv14neu : luvs[i]&0x3fff;
233 >                        j = tms->flags&TM_F_BW ? uv14neu :
234 >                                        (int)(luvs[i]&0x3fff);
235                  }
236                  if (!isuvset(ld, j)) {
237 <                        if (uvpdecode(uvp, j) < 0) {
238 <                                uvp[0] = U_NEU;
253 <                                uvp[1] = V_NEU;
237 >                        if (uv_decode(&uvp[0], &uvp[1], j) < 0) {
238 >                                uvp[0] = U_NEU; uvp[1] = V_NEU;
239                          }
240 <                        uv2rgb(ld->rgbval[j], tmTop, uvp);
240 >                        uv2rgb(ld->rgbval[j], tms, uvp);
241                          setuv(ld, j);
242                  }
243                  cs[3*i  ] = ld->rgbval[j][RED];
# Line 264 | Line 249 | int    len;
249  
250  
251   int
252 < tmCvL16(ls, l16s, len)                  /* convert 16-bit LogL values */
253 < TMbright        *ls;
254 < uint16  *l16s;
255 < int     len;
252 > tmCvL16(                                /* convert 16-bit LogL values */
253 > TMstruct        *tms,
254 > TMbright        *ls,
255 > uint16  *l16s,
256 > int     len
257 > )
258   {
259 <        static char     funcName[] = "tmCvL16";
259 >        static const char       funcName[] = "tmCvL16";
260          static double   lastsf;
261          static int      offset;
262          register int    i;
263                                          /* check arguments */
264 <        if (tmTop == NULL)
264 >        if (tms == NULL)
265                  returnErr(TM_E_TMINVAL);
266 <        if (ls == NULL | l16s == NULL | len < 0)
266 >        if ((ls == NULL) | (l16s == NULL) | (len < 0))
267                  returnErr(TM_E_ILLEGAL);
268                                          /* check scaling offset */
269 <        if (!FEQ(tmTop->inpsf, lastsf)) {
270 <                offset = BRT2SCALE*64;
271 <                if (tmTop->inpsf > 1.0001)
285 <                        offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)+.5);
286 <                else if (tmTop->inpsf < 0.9999)
287 <                        offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)-.5);
288 <                lastsf = tmTop->inpsf;
269 >        if (!FEQ(tms->inpsf, lastsf)) {
270 >                offset = BRT2SCALE(64) - tmCvLuminance(tms->inpsf);
271 >                lastsf = tms->inpsf;
272          }
273                                          /* convert each pixel */
274          for (i = len; i--; ) {
275                  if (l16s[i] & 0x8000)           /* negative luminance */
276 <                        ls[i] = MINBRT-1;       /* assign bogus value */
276 >                        ls[i] = TM_NOBRT;       /* assign bogus value */
277                  else                            /* else convert to lnL */
278 <                        ls[i] = (BRT2SCALE*l16s[i] >> 8) - offset;
278 >                        ls[i] = (BRT2SCALE(l16s[i]) >> 8) - offset;
279          }
280          returnOK;
281   }
282  
283  
284   static void
285 < luv32NewSpace(tm)               /* initialize 32-bit LogLuv color space */
286 < struct tmStruct *tm;
285 > luv32NewSpace(tms)              /* initialize 32-bit LogLuv color space */
286 > TMstruct        *tms;
287   {
288          register LUV32DATA      *ld;
289  
290 <        if (tm->inppri != TM_XYZPRIM) {         /* panic time! */
290 >        if (tms->inppri != TM_XYZPRIM) {                /* panic time! */
291                  fputs("Improper input color space in luv32NewSpace!\n", stderr);
292                  exit(1);
293          }
294 <        ld = (LUV32DATA *)tm->pd[luv32Reg];
295 <        ld->offset = BRT2SCALE*64;
313 <        if (tm->inpsf > 1.0001)
314 <                ld->offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)+.5);
315 <        else if (tm->inpsf < 0.9999)
316 <                ld->offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)-.5);
294 >        ld = (LUV32DATA *)tms->pd[luv32Reg];
295 >        ld->offset = BRT2SCALE(64) - tmCvLuminance(tms->inpsf);
296          clruvall(ld);
297   }
298  
299  
300   static MEM_PTR
301 < luv32Init(tm)                   /* allocate data for 32-bit LogLuv decoder */
302 < struct tmStruct *tm;
301 > luv32Init(tms)                  /* allocate data for 32-bit LogLuv decoder */
302 > TMstruct        *tms;
303   {
304          register LUV32DATA      *ld;
305  
306          ld = (LUV32DATA *)malloc(sizeof(LUV32DATA));
307          if (ld == NULL)
308                  return(NULL);
309 <        tm->pd[luv32Reg] = (MEM_PTR)ld;
310 <        luv32NewSpace(tm);
309 >        tms->pd[luv32Reg] = (MEM_PTR)ld;
310 >        luv32NewSpace(tms);
311          return((MEM_PTR)ld);
312   }
313  
314  
315   static void
316 < luv24NewSpace(tm)               /* initialize 24-bit LogLuv color space */
317 < struct tmStruct *tm;
316 > luv24NewSpace(tms)              /* initialize 24-bit LogLuv color space */
317 > TMstruct *tms;
318   {
319          register LUV24DATA      *ld;
341        double  uvp[2];
320  
321 <        if (tm->inppri != TM_XYZPRIM) {         /* panic time! */
321 >        if (tms->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;
349 <        if (tm->inpsf > 1.0001)
350 <                ld->offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)+.5);
351 <        else if (tm->inpsf < 0.9999)
352 <                ld->offset -= (int)(TM_BRTSCALE*log(tmTop->inpsf)-.5);
325 >        ld = (LUV24DATA *)tms->pd[luv24Reg];
326 >        ld->offset = BRT2SCALE(12) - tmCvLuminance(tms->inpsf);
327          clruvall(ld);
328   }
329  
330  
331   static MEM_PTR
332 < luv24Init(tm)                   /* allocate data for 24-bit LogLuv decoder */
333 < struct tmStruct *tm;
332 > luv24Init(tms)                  /* allocate data for 24-bit LogLuv decoder */
333 > TMstruct        *tms;
334   {
335          register LUV24DATA      *ld;
336  
337          ld = (LUV24DATA *)malloc(sizeof(LUV24DATA));
338          if (ld == NULL)
339                  return(NULL);
340 <        tm->pd[luv24Reg] = (MEM_PTR)ld;
341 <        if (uv14neu < 0) {              /* initialize neutral color index */
342 <                double  uvp[2];
343 <                uvp[0] = U_NEU; uvp[1] = V_NEU;
370 <                uv14neu = uvpencode(uvp);
371 <        }
372 <        luv24NewSpace(tm);
340 >        tms->pd[luv24Reg] = (MEM_PTR)ld;
341 >        if (uv14neu < 0)                /* initialize neutral color index */
342 >                uv14neu = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
343 >        luv24NewSpace(tms);
344          return((MEM_PTR)ld);
345   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines