ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/colrops.c
Revision: 2.5
Committed: Wed Jan 21 12:55:00 1998 UTC (26 years, 3 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 2.4: +4 -6 lines
Log Message:
made conversion from gamma-corrected bytes more robust --
no more zeroes in output

File Contents

# User Rev Content
1 gregl 2.5 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2 greg 1.1
3     #ifndef lint
4 gregl 2.5 static char SCCSid[] = "$SunId$ SGI";
5 greg 1.1 #endif
6    
7     /*
8     * Integer operations on COLR scanlines
9     */
10    
11     #include "color.h"
12    
13 greg 2.3 #define NULL 0
14 greg 1.1
15 greg 2.3 extern char *bmalloc();
16 greg 1.1
17 greg 2.3 #define MAXGSHIFT 31 /* maximum shift for gamma table */
18 greg 1.1
19 greg 2.3 static BYTE *g_mant = NULL, *g_nexp = NULL;
20 greg 1.1
21 greg 2.3 static BYTE (*g_bval)[256] = NULL;
22    
23 greg 2.4 #ifndef pow
24 greg 2.3 extern double pow();
25 greg 2.4 #endif
26 greg 2.3
27    
28     setcolrcor(f, a2) /* set brightness correction */
29     double (*f)();
30     double a2;
31 greg 1.1 {
32     double mult;
33     register int i, j;
34 greg 2.3 /* allocate tables */
35     if (g_bval == NULL && (g_bval =
36     (BYTE (*)[256])bmalloc((MAXGSHIFT+1)*256)) == NULL)
37     return(-1);
38 greg 1.1 /* compute colr -> gamb mapping */
39 greg 2.3 mult = 1.0/256.0;
40 greg 1.1 for (i = 0; i <= MAXGSHIFT; i++) {
41     for (j = 0; j < 256; j++)
42 greg 2.3 g_bval[i][j] = 256.0 * (*f)((j+.5)*mult, a2);
43     mult *= 0.5;
44 greg 1.1 }
45 greg 2.3 return(0);
46     }
47    
48    
49     setcolrinv(f, a2) /* set inverse brightness correction */
50     double (*f)();
51     double a2;
52     {
53     double mult;
54     register int i, j;
55     /* allocate tables */
56     if (g_mant == NULL && (g_mant = (BYTE *)bmalloc(256)) == NULL)
57     return(-1);
58     if (g_nexp == NULL && (g_nexp = (BYTE *)bmalloc(256)) == NULL)
59     return(-1);
60 greg 1.1 /* compute gamb -> colr mapping */
61     i = 0;
62     mult = 256.0;
63 gregl 2.5 for (j = 256; j--; ) {
64     while ((g_mant[j] = mult * (*f)((j+.5)/256.0, a2)) < 128) {
65 greg 1.1 i++;
66     mult *= 2.0;
67     }
68     g_nexp[j] = i;
69     }
70 greg 2.3 return(0);
71 greg 1.1 }
72    
73    
74 greg 2.3 setcolrgam(g) /* set gamma conversion */
75     double g;
76     {
77     if (setcolrcor(pow, 1.0/g) < 0)
78     return(-1);
79     return(setcolrinv(pow, g));
80     }
81    
82    
83 greg 1.1 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 greg 2.3 if (g_bval == NULL)
90     return(-1);
91 greg 1.1 while (len-- > 0) {
92     expo = scan[0][EXP] - COLXS;
93     if (expo < -MAXGSHIFT) {
94     if (expo < -MAXGSHIFT-8) {
95     scan[0][RED] =
96     scan[0][GRN] =
97     scan[0][BLU] = 0;
98     } else {
99     i = (-MAXGSHIFT-1) - expo;
100     scan[0][RED] =
101     g_bval[MAXGSHIFT][((scan[0][RED]>>i)+1)>>1];
102     scan[0][GRN] =
103     g_bval[MAXGSHIFT][((scan[0][GRN]>>i)+1)>>1];
104     scan[0][BLU] =
105     g_bval[MAXGSHIFT][((scan[0][BLU]>>i)+1)>>1];
106     }
107     } else if (expo > 0) {
108     if (expo > 8) {
109     scan[0][RED] =
110     scan[0][GRN] =
111     scan[0][BLU] = 255;
112     } else {
113     i = (scan[0][RED]<<1 | 1) << (expo-1);
114     scan[0][RED] = i > 255 ? 255 : g_bval[0][i];
115     i = (scan[0][GRN]<<1 | 1) << (expo-1);
116     scan[0][GRN] = i > 255 ? 255 : g_bval[0][i];
117     i = (scan[0][BLU]<<1 | 1) << (expo-1);
118     scan[0][BLU] = i > 255 ? 255 : g_bval[0][i];
119     }
120     } else {
121     scan[0][RED] = g_bval[-expo][scan[0][RED]];
122     scan[0][GRN] = g_bval[-expo][scan[0][GRN]];
123     scan[0][BLU] = g_bval[-expo][scan[0][BLU]];
124     }
125     scan[0][EXP] = COLXS;
126     scan++;
127     }
128 greg 2.3 return(0);
129 greg 1.1 }
130    
131    
132     gambs_colrs(scan, len) /* convert gamma bytes to colr scanline */
133     register COLR *scan;
134     int len;
135     {
136     register int nexpo;
137    
138 greg 2.3 if (g_mant == NULL | g_nexp == NULL)
139     return(-1);
140 greg 1.1 while (len-- > 0) {
141     nexpo = g_nexp[scan[0][RED]];
142     if (g_nexp[scan[0][GRN]] < nexpo)
143     nexpo = g_nexp[scan[0][GRN]];
144     if (g_nexp[scan[0][BLU]] < nexpo)
145     nexpo = g_nexp[scan[0][BLU]];
146     if (nexpo < g_nexp[scan[0][RED]])
147     scan[0][RED] = g_mant[scan[0][RED]]
148     >> (g_nexp[scan[0][RED]]-nexpo);
149     else
150     scan[0][RED] = g_mant[scan[0][RED]];
151     if (nexpo < g_nexp[scan[0][GRN]])
152     scan[0][GRN] = g_mant[scan[0][GRN]]
153     >> (g_nexp[scan[0][GRN]]-nexpo);
154     else
155     scan[0][GRN] = g_mant[scan[0][GRN]];
156     if (nexpo < g_nexp[scan[0][BLU]])
157     scan[0][BLU] = g_mant[scan[0][BLU]]
158     >> (g_nexp[scan[0][BLU]]-nexpo);
159     else
160     scan[0][BLU] = g_mant[scan[0][BLU]];
161     scan[0][EXP] = COLXS - nexpo;
162     scan++;
163     }
164 greg 2.3 return(0);
165 greg 1.1 }
166    
167    
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 greg 2.2 int minexp;
174    
175     if (adjust == 0)
176     return;
177     minexp = adjust < 0 ? -adjust : 0;
178 greg 1.1 while (len-- > 0) {
179 greg 2.2 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 greg 1.1 scan++;
185     }
186     }
187    
188    
189     normcolrs(scan, len, adjust) /* normalize a scanline of colrs */
190     register COLR *scan;
191     int len;
192     int adjust;
193     {
194     register int c;
195     register int shift;
196    
197     while (len-- > 0) {
198     shift = scan[0][EXP] + adjust - COLXS;
199     if (shift > 0) {
200     if (shift > 8) {
201     scan[0][RED] =
202     scan[0][GRN] =
203     scan[0][BLU] = 255;
204     } else {
205     shift--;
206     c = (scan[0][RED]<<1 | 1) << shift;
207     scan[0][RED] = c > 255 ? 255 : c;
208     c = (scan[0][GRN]<<1 | 1) << shift;
209     scan[0][GRN] = c > 255 ? 255 : c;
210     c = (scan[0][BLU]<<1 | 1) << shift;
211     scan[0][BLU] = c > 255 ? 255 : c;
212     }
213     } else if (shift < 0) {
214     if (shift < -8) {
215     scan[0][RED] =
216     scan[0][GRN] =
217     scan[0][BLU] = 0;
218     } else {
219     shift = -1-shift;
220     scan[0][RED] = ((scan[0][RED]>>shift)+1)>>1;
221     scan[0][GRN] = ((scan[0][GRN]>>shift)+1)>>1;
222     scan[0][BLU] = ((scan[0][BLU]>>shift)+1)>>1;
223     }
224     }
225     scan[0][EXP] = COLXS - adjust;
226     scan++;
227     }
228     }