1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: colrops.c,v 2.12 2011/05/20 02:06:38 greg Exp $"; |
3 |
#endif |
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 |
|
16 |
|
17 |
#define MAXGSHIFT 31 /* maximum shift for gamma table */ |
18 |
|
19 |
static uby8 *g_mant = NULL, *g_nexp = NULL; |
20 |
|
21 |
static uby8 (*g_bval)[256] = NULL; |
22 |
|
23 |
|
24 |
int |
25 |
setcolrcor( /* set brightness correction */ |
26 |
double (*f)(double,double), |
27 |
double a2 |
28 |
) |
29 |
{ |
30 |
double mult; |
31 |
int i, j; |
32 |
/* allocate tables */ |
33 |
if (g_bval == NULL && (g_bval = |
34 |
(uby8 (*)[256])bmalloc((MAXGSHIFT+1)*256)) == NULL) |
35 |
return(-1); |
36 |
/* compute colr -> gamb mapping */ |
37 |
mult = 1.0/256.0; |
38 |
for (i = 0; i <= MAXGSHIFT; i++) { |
39 |
for (j = 0; j < 256; j++) |
40 |
g_bval[i][j] = 256.0 * (*f)((j+.5)*mult, a2); |
41 |
mult *= 0.5; |
42 |
} |
43 |
return(0); |
44 |
} |
45 |
|
46 |
|
47 |
int |
48 |
setcolrinv( /* set inverse brightness correction */ |
49 |
double (*f)(double,double), |
50 |
double a2 |
51 |
) |
52 |
{ |
53 |
double mult; |
54 |
int i, j; |
55 |
/* allocate tables */ |
56 |
if (g_mant == NULL && (g_mant = (uby8 *)bmalloc(256)) == NULL) |
57 |
return(-1); |
58 |
if (g_nexp == NULL && (g_nexp = (uby8 *)bmalloc(256)) == NULL) |
59 |
return(-1); |
60 |
/* compute gamb -> colr mapping */ |
61 |
i = 0; |
62 |
mult = 256.0; |
63 |
for (j = 256; j--; ) { |
64 |
while ((g_mant[j] = mult * (*f)((j+.5)/256.0, a2)) < 128) { |
65 |
i++; |
66 |
mult *= 2.0; |
67 |
} |
68 |
g_nexp[j] = i; |
69 |
} |
70 |
return(0); |
71 |
} |
72 |
|
73 |
|
74 |
int |
75 |
setcolrgam( /* set gamma conversion */ |
76 |
double g |
77 |
) |
78 |
{ |
79 |
if (setcolrcor(pow, 1.0/g) < 0) |
80 |
return(-1); |
81 |
return(setcolrinv(pow, g)); |
82 |
} |
83 |
|
84 |
|
85 |
int |
86 |
colrs_gambs( /* convert scanline of colrs to gamma bytes */ |
87 |
COLR *scan, |
88 |
int len |
89 |
) |
90 |
{ |
91 |
int i, expo; |
92 |
|
93 |
if (g_bval == NULL) |
94 |
return(-1); |
95 |
while (len-- > 0) { |
96 |
expo = scan[0][EXP] - COLXS; |
97 |
if (expo < -MAXGSHIFT) { |
98 |
if (expo < -MAXGSHIFT-8) { |
99 |
scan[0][RED] = |
100 |
scan[0][GRN] = |
101 |
scan[0][BLU] = 0; |
102 |
} else { |
103 |
i = (-MAXGSHIFT-1) - expo; |
104 |
scan[0][RED] = |
105 |
g_bval[MAXGSHIFT][((scan[0][RED]>>i)+1)>>1]; |
106 |
scan[0][GRN] = |
107 |
g_bval[MAXGSHIFT][((scan[0][GRN]>>i)+1)>>1]; |
108 |
scan[0][BLU] = |
109 |
g_bval[MAXGSHIFT][((scan[0][BLU]>>i)+1)>>1]; |
110 |
} |
111 |
} else if (expo > 0) { |
112 |
if (expo > 8) { |
113 |
scan[0][RED] = |
114 |
scan[0][GRN] = |
115 |
scan[0][BLU] = 255; |
116 |
} else { |
117 |
i = (scan[0][RED]<<1 | 1) << (expo-1); |
118 |
scan[0][RED] = i > 255 ? 255 : g_bval[0][i]; |
119 |
i = (scan[0][GRN]<<1 | 1) << (expo-1); |
120 |
scan[0][GRN] = i > 255 ? 255 : g_bval[0][i]; |
121 |
i = (scan[0][BLU]<<1 | 1) << (expo-1); |
122 |
scan[0][BLU] = i > 255 ? 255 : g_bval[0][i]; |
123 |
} |
124 |
} else { |
125 |
scan[0][RED] = g_bval[-expo][scan[0][RED]]; |
126 |
scan[0][GRN] = g_bval[-expo][scan[0][GRN]]; |
127 |
scan[0][BLU] = g_bval[-expo][scan[0][BLU]]; |
128 |
} |
129 |
scan[0][EXP] = COLXS; |
130 |
scan++; |
131 |
} |
132 |
return(0); |
133 |
} |
134 |
|
135 |
|
136 |
int |
137 |
gambs_colrs( /* convert gamma bytes to colr scanline */ |
138 |
COLR *scan, |
139 |
int len |
140 |
) |
141 |
{ |
142 |
int nexpo; |
143 |
|
144 |
if ((g_mant == NULL) | (g_nexp == NULL)) |
145 |
return(-1); |
146 |
while (len-- > 0) { |
147 |
nexpo = g_nexp[scan[0][RED]]; |
148 |
if (g_nexp[scan[0][GRN]] < nexpo) |
149 |
nexpo = g_nexp[scan[0][GRN]]; |
150 |
if (g_nexp[scan[0][BLU]] < nexpo) |
151 |
nexpo = g_nexp[scan[0][BLU]]; |
152 |
if (nexpo < g_nexp[scan[0][RED]]) |
153 |
scan[0][RED] = g_mant[scan[0][RED]] |
154 |
>> (g_nexp[scan[0][RED]]-nexpo); |
155 |
else |
156 |
scan[0][RED] = g_mant[scan[0][RED]]; |
157 |
if (nexpo < g_nexp[scan[0][GRN]]) |
158 |
scan[0][GRN] = g_mant[scan[0][GRN]] |
159 |
>> (g_nexp[scan[0][GRN]]-nexpo); |
160 |
else |
161 |
scan[0][GRN] = g_mant[scan[0][GRN]]; |
162 |
if (nexpo < g_nexp[scan[0][BLU]]) |
163 |
scan[0][BLU] = g_mant[scan[0][BLU]] |
164 |
>> (g_nexp[scan[0][BLU]]-nexpo); |
165 |
else |
166 |
scan[0][BLU] = g_mant[scan[0][BLU]]; |
167 |
scan[0][EXP] = COLXS - nexpo; |
168 |
scan++; |
169 |
} |
170 |
return(0); |
171 |
} |
172 |
|
173 |
|
174 |
void |
175 |
shiftcolrs( /* shift a scanline of colors by 2^adjust */ |
176 |
COLR *scan, |
177 |
int len, |
178 |
int adjust |
179 |
) |
180 |
{ |
181 |
int minexp; |
182 |
|
183 |
if (adjust == 0) |
184 |
return; |
185 |
minexp = adjust < 0 ? -adjust : 0; |
186 |
while (len-- > 0) { |
187 |
if (scan[0][EXP] <= minexp) |
188 |
scan[0][RED] = scan[0][GRN] = scan[0][BLU] = |
189 |
scan[0][EXP] = 0; |
190 |
else |
191 |
scan[0][EXP] += adjust; |
192 |
scan++; |
193 |
} |
194 |
} |
195 |
|
196 |
|
197 |
void |
198 |
normcolrs( /* normalize a scanline of colrs */ |
199 |
COLR *scan, |
200 |
int len, |
201 |
int adjust |
202 |
) |
203 |
{ |
204 |
int c; |
205 |
int shift; |
206 |
|
207 |
while (len-- > 0) { |
208 |
shift = scan[0][EXP] + adjust - COLXS; |
209 |
if (shift > 0) { |
210 |
if (shift > 8) { |
211 |
scan[0][RED] = |
212 |
scan[0][GRN] = |
213 |
scan[0][BLU] = 255; |
214 |
} else { |
215 |
shift--; |
216 |
c = (scan[0][RED]<<1 | 1) << shift; |
217 |
scan[0][RED] = c > 255 ? 255 : c; |
218 |
c = (scan[0][GRN]<<1 | 1) << shift; |
219 |
scan[0][GRN] = c > 255 ? 255 : c; |
220 |
c = (scan[0][BLU]<<1 | 1) << shift; |
221 |
scan[0][BLU] = c > 255 ? 255 : c; |
222 |
} |
223 |
} else if (shift < 0) { |
224 |
if (shift < -8) { |
225 |
scan[0][RED] = |
226 |
scan[0][GRN] = |
227 |
scan[0][BLU] = 0; |
228 |
} else { |
229 |
shift = -1-shift; |
230 |
scan[0][RED] = ((scan[0][RED]>>shift)+1)>>1; |
231 |
scan[0][GRN] = ((scan[0][GRN]>>shift)+1)>>1; |
232 |
scan[0][BLU] = ((scan[0][BLU]>>shift)+1)>>1; |
233 |
} |
234 |
} |
235 |
scan[0][EXP] = COLXS - adjust; |
236 |
scan++; |
237 |
} |
238 |
} |