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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines