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