1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.12 |
static const char RCSid[] = "$Id: colrops.c,v 2.11 2003/07/30 10:11:06 schorsch 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 |
schorsch |
2.11 |
|
13 |
|
|
#include "rtmisc.h" |
14 |
greg |
1.1 |
#include "color.h" |
15 |
|
|
|
16 |
|
|
|
17 |
greg |
2.3 |
#define MAXGSHIFT 31 /* maximum shift for gamma table */ |
18 |
greg |
1.1 |
|
19 |
greg |
2.12 |
static uby8 *g_mant = NULL, *g_nexp = NULL; |
20 |
greg |
1.1 |
|
21 |
greg |
2.12 |
static uby8 (*g_bval)[256] = NULL; |
22 |
greg |
2.3 |
|
23 |
|
|
|
24 |
greg |
2.6 |
int |
25 |
greg |
2.3 |
setcolrcor(f, a2) /* set brightness correction */ |
26 |
schorsch |
2.11 |
double (*f)(double,double); |
27 |
greg |
2.3 |
double a2; |
28 |
greg |
1.1 |
{ |
29 |
|
|
double mult; |
30 |
|
|
register int i, j; |
31 |
greg |
2.3 |
/* allocate tables */ |
32 |
|
|
if (g_bval == NULL && (g_bval = |
33 |
greg |
2.12 |
(uby8 (*)[256])bmalloc((MAXGSHIFT+1)*256)) == NULL) |
34 |
greg |
2.3 |
return(-1); |
35 |
greg |
1.1 |
/* compute colr -> gamb mapping */ |
36 |
greg |
2.3 |
mult = 1.0/256.0; |
37 |
greg |
1.1 |
for (i = 0; i <= MAXGSHIFT; i++) { |
38 |
|
|
for (j = 0; j < 256; j++) |
39 |
greg |
2.3 |
g_bval[i][j] = 256.0 * (*f)((j+.5)*mult, a2); |
40 |
|
|
mult *= 0.5; |
41 |
greg |
1.1 |
} |
42 |
greg |
2.3 |
return(0); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
|
46 |
greg |
2.6 |
int |
47 |
greg |
2.3 |
setcolrinv(f, a2) /* set inverse brightness correction */ |
48 |
schorsch |
2.11 |
double (*f)(double,double); |
49 |
greg |
2.3 |
double a2; |
50 |
|
|
{ |
51 |
|
|
double mult; |
52 |
|
|
register int i, j; |
53 |
|
|
/* allocate tables */ |
54 |
greg |
2.12 |
if (g_mant == NULL && (g_mant = (uby8 *)bmalloc(256)) == NULL) |
55 |
greg |
2.3 |
return(-1); |
56 |
greg |
2.12 |
if (g_nexp == NULL && (g_nexp = (uby8 *)bmalloc(256)) == NULL) |
57 |
greg |
2.3 |
return(-1); |
58 |
greg |
1.1 |
/* compute gamb -> colr mapping */ |
59 |
|
|
i = 0; |
60 |
|
|
mult = 256.0; |
61 |
gregl |
2.5 |
for (j = 256; j--; ) { |
62 |
|
|
while ((g_mant[j] = mult * (*f)((j+.5)/256.0, a2)) < 128) { |
63 |
greg |
1.1 |
i++; |
64 |
|
|
mult *= 2.0; |
65 |
|
|
} |
66 |
|
|
g_nexp[j] = i; |
67 |
|
|
} |
68 |
greg |
2.3 |
return(0); |
69 |
greg |
1.1 |
} |
70 |
|
|
|
71 |
|
|
|
72 |
greg |
2.6 |
int |
73 |
greg |
2.3 |
setcolrgam(g) /* set gamma conversion */ |
74 |
|
|
double g; |
75 |
|
|
{ |
76 |
|
|
if (setcolrcor(pow, 1.0/g) < 0) |
77 |
|
|
return(-1); |
78 |
|
|
return(setcolrinv(pow, g)); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
|
82 |
greg |
2.6 |
int |
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 |
greg |
2.6 |
int |
133 |
greg |
1.1 |
gambs_colrs(scan, len) /* convert gamma bytes to colr scanline */ |
134 |
|
|
register COLR *scan; |
135 |
|
|
int len; |
136 |
|
|
{ |
137 |
|
|
register int nexpo; |
138 |
|
|
|
139 |
schorsch |
2.10 |
if ((g_mant == NULL) | (g_nexp == NULL)) |
140 |
greg |
2.3 |
return(-1); |
141 |
greg |
1.1 |
while (len-- > 0) { |
142 |
|
|
nexpo = g_nexp[scan[0][RED]]; |
143 |
|
|
if (g_nexp[scan[0][GRN]] < nexpo) |
144 |
|
|
nexpo = g_nexp[scan[0][GRN]]; |
145 |
|
|
if (g_nexp[scan[0][BLU]] < nexpo) |
146 |
|
|
nexpo = g_nexp[scan[0][BLU]]; |
147 |
|
|
if (nexpo < g_nexp[scan[0][RED]]) |
148 |
|
|
scan[0][RED] = g_mant[scan[0][RED]] |
149 |
|
|
>> (g_nexp[scan[0][RED]]-nexpo); |
150 |
|
|
else |
151 |
|
|
scan[0][RED] = g_mant[scan[0][RED]]; |
152 |
|
|
if (nexpo < g_nexp[scan[0][GRN]]) |
153 |
|
|
scan[0][GRN] = g_mant[scan[0][GRN]] |
154 |
|
|
>> (g_nexp[scan[0][GRN]]-nexpo); |
155 |
|
|
else |
156 |
|
|
scan[0][GRN] = g_mant[scan[0][GRN]]; |
157 |
|
|
if (nexpo < g_nexp[scan[0][BLU]]) |
158 |
|
|
scan[0][BLU] = g_mant[scan[0][BLU]] |
159 |
|
|
>> (g_nexp[scan[0][BLU]]-nexpo); |
160 |
|
|
else |
161 |
|
|
scan[0][BLU] = g_mant[scan[0][BLU]]; |
162 |
|
|
scan[0][EXP] = COLXS - nexpo; |
163 |
|
|
scan++; |
164 |
|
|
} |
165 |
greg |
2.3 |
return(0); |
166 |
greg |
1.1 |
} |
167 |
|
|
|
168 |
|
|
|
169 |
greg |
2.6 |
void |
170 |
greg |
1.1 |
shiftcolrs(scan, len, adjust) /* shift a scanline of colors by 2^adjust */ |
171 |
|
|
register COLR *scan; |
172 |
|
|
register int len; |
173 |
|
|
register int adjust; |
174 |
|
|
{ |
175 |
greg |
2.2 |
int minexp; |
176 |
|
|
|
177 |
|
|
if (adjust == 0) |
178 |
|
|
return; |
179 |
|
|
minexp = adjust < 0 ? -adjust : 0; |
180 |
greg |
1.1 |
while (len-- > 0) { |
181 |
greg |
2.2 |
if (scan[0][EXP] <= minexp) |
182 |
|
|
scan[0][RED] = scan[0][GRN] = scan[0][BLU] = |
183 |
|
|
scan[0][EXP] = 0; |
184 |
|
|
else |
185 |
|
|
scan[0][EXP] += adjust; |
186 |
greg |
1.1 |
scan++; |
187 |
|
|
} |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
|
191 |
greg |
2.6 |
void |
192 |
greg |
1.1 |
normcolrs(scan, len, adjust) /* normalize a scanline of colrs */ |
193 |
|
|
register COLR *scan; |
194 |
|
|
int len; |
195 |
|
|
int adjust; |
196 |
|
|
{ |
197 |
|
|
register int c; |
198 |
|
|
register int shift; |
199 |
|
|
|
200 |
|
|
while (len-- > 0) { |
201 |
|
|
shift = scan[0][EXP] + adjust - COLXS; |
202 |
|
|
if (shift > 0) { |
203 |
|
|
if (shift > 8) { |
204 |
|
|
scan[0][RED] = |
205 |
|
|
scan[0][GRN] = |
206 |
|
|
scan[0][BLU] = 255; |
207 |
|
|
} else { |
208 |
|
|
shift--; |
209 |
|
|
c = (scan[0][RED]<<1 | 1) << shift; |
210 |
|
|
scan[0][RED] = c > 255 ? 255 : c; |
211 |
|
|
c = (scan[0][GRN]<<1 | 1) << shift; |
212 |
|
|
scan[0][GRN] = c > 255 ? 255 : c; |
213 |
|
|
c = (scan[0][BLU]<<1 | 1) << shift; |
214 |
|
|
scan[0][BLU] = c > 255 ? 255 : c; |
215 |
|
|
} |
216 |
|
|
} else if (shift < 0) { |
217 |
|
|
if (shift < -8) { |
218 |
|
|
scan[0][RED] = |
219 |
|
|
scan[0][GRN] = |
220 |
|
|
scan[0][BLU] = 0; |
221 |
|
|
} else { |
222 |
|
|
shift = -1-shift; |
223 |
|
|
scan[0][RED] = ((scan[0][RED]>>shift)+1)>>1; |
224 |
|
|
scan[0][GRN] = ((scan[0][GRN]>>shift)+1)>>1; |
225 |
|
|
scan[0][BLU] = ((scan[0][BLU]>>shift)+1)>>1; |
226 |
|
|
} |
227 |
|
|
} |
228 |
|
|
scan[0][EXP] = COLXS - adjust; |
229 |
|
|
scan++; |
230 |
|
|
} |
231 |
|
|
} |