ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/colrops.c
Revision: 1.1
Committed: Fri Oct 19 16:33:44 1990 UTC (33 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1990 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Integer operations on COLR scanlines
9     */
10    
11     #include "color.h"
12    
13     #define MAXGSHIFT 2 /* maximum shift for gamma table */
14    
15     static BYTE g_mant[256], g_nexp[256];
16    
17     static BYTE g_bval[MAXGSHIFT+1][256];
18    
19    
20     setcolrgam(g) /* set gamma conversion */
21     double g;
22     {
23     extern double pow();
24     double mult;
25     register int i, j;
26     /* compute colr -> gamb mapping */
27     for (i = 0; i <= MAXGSHIFT; i++) {
28     mult = pow(0.5, (double)(i+8));
29     for (j = 0; j < 256; j++)
30     g_bval[i][j] = 256.0 * pow((j+.5)*mult, 1.0/g);
31     }
32     /* compute gamb -> colr mapping */
33     i = 0;
34     mult = 256.0;
35     for (j = 255; j >= 0; j--) {
36     rept:
37     g_mant[j] = mult * pow((j+.5)/256.0, g);
38     if (g_mant[j] < 128) {
39     i++;
40     mult *= 2.0;
41     goto rept;
42     }
43     g_nexp[j] = i;
44     }
45     }
46    
47    
48     colrs_gambs(scan, len) /* convert scanline of colrs to gamma bytes */
49     register COLR *scan;
50     int len;
51     {
52     register int i, expo;
53    
54     while (len-- > 0) {
55     expo = scan[0][EXP] - COLXS;
56     if (expo < -MAXGSHIFT) {
57     if (expo < -MAXGSHIFT-8) {
58     scan[0][RED] =
59     scan[0][GRN] =
60     scan[0][BLU] = 0;
61     } else {
62     i = (-MAXGSHIFT-1) - expo;
63     scan[0][RED] =
64     g_bval[MAXGSHIFT][((scan[0][RED]>>i)+1)>>1];
65     scan[0][GRN] =
66     g_bval[MAXGSHIFT][((scan[0][GRN]>>i)+1)>>1];
67     scan[0][BLU] =
68     g_bval[MAXGSHIFT][((scan[0][BLU]>>i)+1)>>1];
69     }
70     } else if (expo > 0) {
71     if (expo > 8) {
72     scan[0][RED] =
73     scan[0][GRN] =
74     scan[0][BLU] = 255;
75     } else {
76     i = (scan[0][RED]<<1 | 1) << (expo-1);
77     scan[0][RED] = i > 255 ? 255 : g_bval[0][i];
78     i = (scan[0][GRN]<<1 | 1) << (expo-1);
79     scan[0][GRN] = i > 255 ? 255 : g_bval[0][i];
80     i = (scan[0][BLU]<<1 | 1) << (expo-1);
81     scan[0][BLU] = i > 255 ? 255 : g_bval[0][i];
82     }
83     } else {
84     scan[0][RED] = g_bval[-expo][scan[0][RED]];
85     scan[0][GRN] = g_bval[-expo][scan[0][GRN]];
86     scan[0][BLU] = g_bval[-expo][scan[0][BLU]];
87     }
88     scan[0][EXP] = COLXS;
89     scan++;
90     }
91     }
92    
93    
94     gambs_colrs(scan, len) /* convert gamma bytes to colr scanline */
95     register COLR *scan;
96     int len;
97     {
98     register int nexpo;
99    
100     while (len-- > 0) {
101     nexpo = g_nexp[scan[0][RED]];
102     if (g_nexp[scan[0][GRN]] < nexpo)
103     nexpo = g_nexp[scan[0][GRN]];
104     if (g_nexp[scan[0][BLU]] < nexpo)
105     nexpo = g_nexp[scan[0][BLU]];
106     if (nexpo < g_nexp[scan[0][RED]])
107     scan[0][RED] = g_mant[scan[0][RED]]
108     >> (g_nexp[scan[0][RED]]-nexpo);
109     else
110     scan[0][RED] = g_mant[scan[0][RED]];
111     if (nexpo < g_nexp[scan[0][GRN]])
112     scan[0][GRN] = g_mant[scan[0][GRN]]
113     >> (g_nexp[scan[0][GRN]]-nexpo);
114     else
115     scan[0][GRN] = g_mant[scan[0][GRN]];
116     if (nexpo < g_nexp[scan[0][BLU]])
117     scan[0][BLU] = g_mant[scan[0][BLU]]
118     >> (g_nexp[scan[0][BLU]]-nexpo);
119     else
120     scan[0][BLU] = g_mant[scan[0][BLU]];
121     scan[0][EXP] = COLXS - nexpo;
122     scan++;
123     }
124     }
125    
126    
127     shiftcolrs(scan, len, adjust) /* shift a scanline of colors by 2^adjust */
128     register COLR *scan;
129     register int len;
130     register int adjust;
131     {
132     while (len-- > 0) {
133     scan[0][EXP] += adjust;
134     scan++;
135     }
136     }
137    
138    
139     normcolrs(scan, len, adjust) /* normalize a scanline of colrs */
140     register COLR *scan;
141     int len;
142     int adjust;
143     {
144     register int c;
145     register int shift;
146    
147     while (len-- > 0) {
148     shift = scan[0][EXP] + adjust - COLXS;
149     if (shift > 0) {
150     if (shift > 8) {
151     scan[0][RED] =
152     scan[0][GRN] =
153     scan[0][BLU] = 255;
154     } else {
155     shift--;
156     c = (scan[0][RED]<<1 | 1) << shift;
157     scan[0][RED] = c > 255 ? 255 : c;
158     c = (scan[0][GRN]<<1 | 1) << shift;
159     scan[0][GRN] = c > 255 ? 255 : c;
160     c = (scan[0][BLU]<<1 | 1) << shift;
161     scan[0][BLU] = c > 255 ? 255 : c;
162     }
163     } else if (shift < 0) {
164     if (shift < -8) {
165     scan[0][RED] =
166     scan[0][GRN] =
167     scan[0][BLU] = 0;
168     } else {
169     shift = -1-shift;
170     scan[0][RED] = ((scan[0][RED]>>shift)+1)>>1;
171     scan[0][GRN] = ((scan[0][GRN]>>shift)+1)>>1;
172     scan[0][BLU] = ((scan[0][BLU]>>shift)+1)>>1;
173     }
174     }
175     scan[0][EXP] = COLXS - adjust;
176     scan++;
177     }
178     }