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

Comparing ray/src/common/colrops.c (file contents):
Revision 1.1 by greg, Fri Oct 19 16:33:44 1990 UTC vs.
Revision 2.8 by greg, Mon Mar 10 17:13:29 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1990 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Integer operations on COLR scanlines
6   */
7  
8 + #include "copyright.h"
9 +
10 + #include <math.h>
11   #include "color.h"
12  
13 < #define MAXGSHIFT       2               /* maximum shift for gamma table */
13 > extern char     *bmalloc();
14  
15 < static BYTE     g_mant[256], g_nexp[256];
15 > #define MAXGSHIFT       31              /* maximum shift for gamma table */
16  
17 < static BYTE     g_bval[MAXGSHIFT+1][256];
17 > static BYTE     *g_mant = NULL, *g_nexp = NULL;
18  
19 + static BYTE     (*g_bval)[256] = NULL;
20  
21 < setcolrgam(g)                   /* set gamma conversion */
22 < double  g;
21 >
22 > int
23 > setcolrcor(f, a2)               /* set brightness correction */
24 > double  (*f)();
25 > double  a2;
26   {
23        extern double   pow();
27          double  mult;
28          register int    i, j;
29 +                                        /* allocate tables */
30 +        if (g_bval == NULL && (g_bval =
31 +                        (BYTE (*)[256])bmalloc((MAXGSHIFT+1)*256)) == NULL)
32 +                return(-1);
33                                          /* compute colr -> gamb mapping */
34 +        mult = 1.0/256.0;
35          for (i = 0; i <= MAXGSHIFT; i++) {
28                mult = pow(0.5, (double)(i+8));
36                  for (j = 0; j < 256; j++)
37 <                        g_bval[i][j] = 256.0 * pow((j+.5)*mult, 1.0/g);
37 >                        g_bval[i][j] = 256.0 * (*f)((j+.5)*mult, a2);
38 >                mult *= 0.5;
39          }
40 +        return(0);
41 + }
42 +
43 +
44 + int
45 + setcolrinv(f, a2)               /* set inverse brightness correction */
46 + double  (*f)();
47 + double  a2;
48 + {
49 +        double  mult;
50 +        register int    i, j;
51 +                                        /* allocate tables */
52 +        if (g_mant == NULL && (g_mant = (BYTE *)bmalloc(256)) == NULL)
53 +                return(-1);
54 +        if (g_nexp == NULL && (g_nexp = (BYTE *)bmalloc(256)) == NULL)
55 +                return(-1);
56                                          /* compute gamb -> colr mapping */
57          i = 0;
58          mult = 256.0;
59 <        for (j = 255; j >= 0; j--) {
60 <        rept:
37 <                g_mant[j] = mult * pow((j+.5)/256.0, g);
38 <                if (g_mant[j] < 128) {
59 >        for (j = 256; j--; ) {
60 >                while ((g_mant[j] = mult * (*f)((j+.5)/256.0, a2)) < 128) {
61                          i++;
62                          mult *= 2.0;
41                        goto rept;
63                  }
64                  g_nexp[j] = i;
65          }
66 +        return(0);
67   }
68  
69  
70 + int
71 + setcolrgam(g)                   /* set gamma conversion */
72 + double  g;
73 + {
74 +        if (setcolrcor(pow, 1.0/g) < 0)
75 +                return(-1);
76 +        return(setcolrinv(pow, g));
77 + }
78 +
79 +
80 + int
81   colrs_gambs(scan, len)          /* convert scanline of colrs to gamma bytes */
82   register COLR   *scan;
83   int     len;
84   {
85          register int    i, expo;
86  
87 +        if (g_bval == NULL)
88 +                return(-1);
89          while (len-- > 0) {
90                  expo = scan[0][EXP] - COLXS;
91                  if (expo < -MAXGSHIFT) {
# Line 88 | Line 123 | int    len;
123                  scan[0][EXP] = COLXS;
124                  scan++;
125          }
126 +        return(0);
127   }
128  
129  
130 + int
131   gambs_colrs(scan, len)          /* convert gamma bytes to colr scanline */
132   register COLR   *scan;
133   int     len;
134   {
135          register int    nexpo;
136  
137 +        if (g_mant == NULL | g_nexp == NULL)
138 +                return(-1);
139          while (len-- > 0) {
140                  nexpo = g_nexp[scan[0][RED]];
141                  if (g_nexp[scan[0][GRN]] < nexpo)
# Line 121 | Line 160 | int    len;
160                  scan[0][EXP] = COLXS - nexpo;
161                  scan++;
162          }
163 +        return(0);
164   }
165  
166  
167 + void
168   shiftcolrs(scan, len, adjust)   /* shift a scanline of colors by 2^adjust */
169   register COLR   *scan;
170   register int    len;
171   register int    adjust;
172   {
173 +        int     minexp;
174 +
175 +        if (adjust == 0)
176 +                return;
177 +        minexp = adjust < 0 ? -adjust : 0;
178          while (len-- > 0) {
179 <                scan[0][EXP] += adjust;
179 >                if (scan[0][EXP] <= minexp)
180 >                        scan[0][RED] = scan[0][GRN] = scan[0][BLU] =
181 >                        scan[0][EXP] = 0;
182 >                else
183 >                        scan[0][EXP] += adjust;
184                  scan++;
185          }
186   }
187  
188  
189 + void
190   normcolrs(scan, len, adjust)    /* normalize a scanline of colrs */
191   register COLR  *scan;
192   int  len;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines