ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/colrops.c
Revision: 2.10
Committed: Sun Jul 27 22:12:01 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.9: +2 -2 lines
Log Message:
Added grouping parens to reduce ambiguity warnings.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.10 static const char RCSid[] = "$Id: colrops.c,v 2.9 2003/06/30 19:04:29 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * Integer operations on COLR scanlines
6     */
7    
8 greg 2.7 #include "copyright.h"
9 greg 2.6
10 greg 2.9 #include <stdio.h>
11 greg 2.6 #include <math.h>
12 greg 1.1 #include "color.h"
13    
14 greg 2.3 extern char *bmalloc();
15 greg 1.1
16 greg 2.3 #define MAXGSHIFT 31 /* maximum shift for gamma table */
17 greg 1.1
18 greg 2.3 static BYTE *g_mant = NULL, *g_nexp = NULL;
19 greg 1.1
20 greg 2.3 static BYTE (*g_bval)[256] = NULL;
21    
22    
23 greg 2.6 int
24 greg 2.3 setcolrcor(f, a2) /* set brightness correction */
25     double (*f)();
26     double a2;
27 greg 1.1 {
28     double mult;
29     register int i, j;
30 greg 2.3 /* allocate tables */
31     if (g_bval == NULL && (g_bval =
32     (BYTE (*)[256])bmalloc((MAXGSHIFT+1)*256)) == NULL)
33     return(-1);
34 greg 1.1 /* compute colr -> gamb mapping */
35 greg 2.3 mult = 1.0/256.0;
36 greg 1.1 for (i = 0; i <= MAXGSHIFT; i++) {
37     for (j = 0; j < 256; j++)
38 greg 2.3 g_bval[i][j] = 256.0 * (*f)((j+.5)*mult, a2);
39     mult *= 0.5;
40 greg 1.1 }
41 greg 2.3 return(0);
42     }
43    
44    
45 greg 2.6 int
46 greg 2.3 setcolrinv(f, a2) /* set inverse brightness correction */
47     double (*f)();
48     double a2;
49     {
50     double mult;
51     register int i, j;
52     /* allocate tables */
53     if (g_mant == NULL && (g_mant = (BYTE *)bmalloc(256)) == NULL)
54     return(-1);
55     if (g_nexp == NULL && (g_nexp = (BYTE *)bmalloc(256)) == NULL)
56     return(-1);
57 greg 1.1 /* compute gamb -> colr mapping */
58     i = 0;
59     mult = 256.0;
60 gregl 2.5 for (j = 256; j--; ) {
61     while ((g_mant[j] = mult * (*f)((j+.5)/256.0, a2)) < 128) {
62 greg 1.1 i++;
63     mult *= 2.0;
64     }
65     g_nexp[j] = i;
66     }
67 greg 2.3 return(0);
68 greg 1.1 }
69    
70    
71 greg 2.6 int
72 greg 2.3 setcolrgam(g) /* set gamma conversion */
73     double g;
74     {
75     if (setcolrcor(pow, 1.0/g) < 0)
76     return(-1);
77     return(setcolrinv(pow, g));
78     }
79    
80    
81 greg 2.6 int
82 greg 1.1 colrs_gambs(scan, len) /* convert scanline of colrs to gamma bytes */
83     register COLR *scan;
84     int len;
85     {
86     register int i, expo;
87    
88 greg 2.3 if (g_bval == NULL)
89     return(-1);
90 greg 1.1 while (len-- > 0) {
91     expo = scan[0][EXP] - COLXS;
92     if (expo < -MAXGSHIFT) {
93     if (expo < -MAXGSHIFT-8) {
94     scan[0][RED] =
95     scan[0][GRN] =
96     scan[0][BLU] = 0;
97     } else {
98     i = (-MAXGSHIFT-1) - expo;
99     scan[0][RED] =
100     g_bval[MAXGSHIFT][((scan[0][RED]>>i)+1)>>1];
101     scan[0][GRN] =
102     g_bval[MAXGSHIFT][((scan[0][GRN]>>i)+1)>>1];
103     scan[0][BLU] =
104     g_bval[MAXGSHIFT][((scan[0][BLU]>>i)+1)>>1];
105     }
106     } else if (expo > 0) {
107     if (expo > 8) {
108     scan[0][RED] =
109     scan[0][GRN] =
110     scan[0][BLU] = 255;
111     } else {
112     i = (scan[0][RED]<<1 | 1) << (expo-1);
113     scan[0][RED] = i > 255 ? 255 : g_bval[0][i];
114     i = (scan[0][GRN]<<1 | 1) << (expo-1);
115     scan[0][GRN] = i > 255 ? 255 : g_bval[0][i];
116     i = (scan[0][BLU]<<1 | 1) << (expo-1);
117     scan[0][BLU] = i > 255 ? 255 : g_bval[0][i];
118     }
119     } else {
120     scan[0][RED] = g_bval[-expo][scan[0][RED]];
121     scan[0][GRN] = g_bval[-expo][scan[0][GRN]];
122     scan[0][BLU] = g_bval[-expo][scan[0][BLU]];
123     }
124     scan[0][EXP] = COLXS;
125     scan++;
126     }
127 greg 2.3 return(0);
128 greg 1.1 }
129    
130    
131 greg 2.6 int
132 greg 1.1 gambs_colrs(scan, len) /* convert gamma bytes to colr scanline */
133     register COLR *scan;
134     int len;
135     {
136     register int nexpo;
137    
138 schorsch 2.10 if ((g_mant == NULL) | (g_nexp == NULL))
139 greg 2.3 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 greg 2.6 void
169 greg 1.1 shiftcolrs(scan, len, adjust) /* shift a scanline of colors by 2^adjust */
170     register COLR *scan;
171     register int len;
172     register int adjust;
173     {
174 greg 2.2 int minexp;
175    
176     if (adjust == 0)
177     return;
178     minexp = adjust < 0 ? -adjust : 0;
179 greg 1.1 while (len-- > 0) {
180 greg 2.2 if (scan[0][EXP] <= minexp)
181     scan[0][RED] = scan[0][GRN] = scan[0][BLU] =
182     scan[0][EXP] = 0;
183     else
184     scan[0][EXP] += adjust;
185 greg 1.1 scan++;
186     }
187     }
188    
189    
190 greg 2.6 void
191 greg 1.1 normcolrs(scan, len, adjust) /* normalize a scanline of colrs */
192     register COLR *scan;
193     int len;
194     int adjust;
195     {
196     register int c;
197     register int shift;
198    
199     while (len-- > 0) {
200     shift = scan[0][EXP] + adjust - COLXS;
201     if (shift > 0) {
202     if (shift > 8) {
203     scan[0][RED] =
204     scan[0][GRN] =
205     scan[0][BLU] = 255;
206     } else {
207     shift--;
208     c = (scan[0][RED]<<1 | 1) << shift;
209     scan[0][RED] = c > 255 ? 255 : c;
210     c = (scan[0][GRN]<<1 | 1) << shift;
211     scan[0][GRN] = c > 255 ? 255 : c;
212     c = (scan[0][BLU]<<1 | 1) << shift;
213     scan[0][BLU] = c > 255 ? 255 : c;
214     }
215     } else if (shift < 0) {
216     if (shift < -8) {
217     scan[0][RED] =
218     scan[0][GRN] =
219     scan[0][BLU] = 0;
220     } else {
221     shift = -1-shift;
222     scan[0][RED] = ((scan[0][RED]>>shift)+1)>>1;
223     scan[0][GRN] = ((scan[0][GRN]>>shift)+1)>>1;
224     scan[0][BLU] = ((scan[0][BLU]>>shift)+1)>>1;
225     }
226     }
227     scan[0][EXP] = COLXS - adjust;
228     scan++;
229     }
230     }