1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const BILINEAR_INTERPOLATION_BITS: u32 = 4;
pub struct Image {
pub width: i32,
pub height: i32,
pub data: Vec<u32>,
}
fn lerp(a: u32, b: u32, t: u32) -> u32 {
let mask = 0xff00ff;
let brb = ((b & 0xff00ff) * t) >> 8;
let bag = ((b >> 8) & 0xff00ff) * t;
let t = 256 - t;
let arb = ((a & 0xff00ff) * t) >> 8;
let aag = ((a >> 8) & 0xff00ff) * t;
let rb = arb + brb;
let ag = aag + bag;
return (rb & mask) | (ag & !mask);
}
pub struct GradientStop {
pub position: f32,
pub color: u32,
}
pub struct GradientSource {
matrix: MatrixFixedPoint,
lut: [u32; 257],
}
impl GradientSource {
pub fn radial_gradient_eval(&self, x: u16, y: u16) -> u32 {
let p = self.matrix.transform(x, y);
let mut distance = (p.x as f32).hypot(p.y as f32) as u32;
distance >>= 8;
if distance > 32768 {
distance = 32786;
}
self.lut[(distance >> 7) as usize]
}
pub fn linear_gradient_eval(&self, x: u16, y: u16) -> u32 {
let p = self.matrix.transform(x, y);
let mut lx = p.x >> 16;
if lx >= 256 {
lx = 256;
}
if lx < 0 {
lx = 0;
}
self.lut[lx as usize]
}
}
pub struct Gradient {
pub stops: Vec<GradientStop>
}
impl Gradient {
pub fn make_source(&self, matrix: &MatrixFixedPoint) -> Box<GradientSource> {
let mut source = Box::new(GradientSource { matrix: (*matrix).clone(), lut: [0; 257] });
self.build_lut(&mut source.lut);
source
}
fn build_lut(&self, lut: &mut [u32; 257]) {
let mut stop_idx = 0;
let mut stop = &self.stops[stop_idx];
let mut last_color = stop.color;
let mut last_pos = 0;
let mut next_color = last_color;
let mut next_pos = (256. * stop.position) as u32;
let mut i = 0;
while i <= 256 {
while next_pos <= i {
stop_idx += 1;
last_color = next_color;
if stop_idx >= self.stops.len() {
stop = &self.stops[self.stops.len() - 1];
next_pos = 256;
next_color = stop.color;
break;
} else {
stop = &self.stops[stop_idx];
}
next_pos = (256. * stop.position) as u32;
next_color = stop.color;
}
let inverse = (256 * 256) / (next_pos - last_pos);
let mut t = 0;
while i <= next_pos {
lut[i as usize] = lerp(last_color, next_color, t >> 8);
t += inverse;
i += 1;
}
last_pos = next_pos;
}
}
}
fn get_pixel(bitmap: &Image, mut x: i32, mut y: i32) -> u32 {
if x < 0 {
x = 0;
}
if x >= bitmap.width {
x = bitmap.width - 1;
}
if y < 0 {
y = 0;
}
if y >= bitmap.height {
y = bitmap.height - 1;
}
return bitmap.data[(y * bitmap.width + x) as usize];
}
fn bilinear_interpolation(
tl: u32,
tr: u32,
bl: u32,
br: u32,
mut distx: u32,
mut disty: u32,
) -> u32 {
let distxy;
let distxiy;
let distixy;
let distixiy;
let mut lo;
let mut hi;
distx <<= 4 - BILINEAR_INTERPOLATION_BITS;
disty <<= 4 - BILINEAR_INTERPOLATION_BITS;
distxy = distx * disty;
distxiy = (distx << 4) - distxy;
distixy = (disty << 4) - distxy;
distixiy = 16 * 16 - (disty << 4) - (distx << 4) + distxy;
lo = (tl & 0xff00ff) * distixiy;
hi = ((tl >> 8) & 0xff00ff) * distixiy;
lo += (tr & 0xff00ff) * distxiy;
hi += ((tr >> 8) & 0xff00ff) * distxiy;
lo += (bl & 0xff00ff) * distixy;
hi += ((bl >> 8) & 0xff00ff) * distixy;
lo += (br & 0xff00ff) * distxy;
hi += ((br >> 8) & 0xff00ff) * distxy;
((lo >> 8) & 0xff00ff) | (hi & !0xff00ff)
}
const FIXED_FRACTION_BITS: u32 = 16;
pub const FIXED_ONE: i32 = 1 << FIXED_FRACTION_BITS;
fn bilinear_weight(x: Fixed) -> u32 {
let reduced = x >> (FIXED_FRACTION_BITS - BILINEAR_INTERPOLATION_BITS);
let fraction = reduced & ((1 << BILINEAR_INTERPOLATION_BITS) - 1);
fraction as u32
}
type Fixed = i32;
fn fixed_to_int(x: Fixed) -> i32 {
x >> FIXED_FRACTION_BITS
}
pub fn float_to_fixed(x: f32) -> Fixed {
(x * (1 << FIXED_FRACTION_BITS) as f32) as i32
}
pub fn fetch_bilinear(image: &Image, x: Fixed, y: Fixed) -> u32 {
let dist_x = bilinear_weight(x);
let dist_y = bilinear_weight(y);
let x1 = fixed_to_int(x);
let y1 = fixed_to_int(y);
let x2 = x1 + 1;
let y2 = y1 + 1;
let tl = get_pixel(image, x1, y1);
let tr = get_pixel(image, x2, y1);
let bl = get_pixel(image, x1, y2);
let br = get_pixel(image, x2, y2);
bilinear_interpolation(tl, tr, bl, br, dist_x, dist_y)
}
pub struct PointFixedPoint {
pub x: Fixed,
pub y: Fixed,
}
#[derive(Clone)]
pub struct MatrixFixedPoint {
pub xx: Fixed,
pub xy: Fixed,
pub yx: Fixed,
pub yy: Fixed,
pub x0: Fixed,
pub y0: Fixed,
}
impl MatrixFixedPoint {
pub fn transform(&self, x: u16, y: u16) -> PointFixedPoint {
let x = x as i32;
let y = y as i32;
PointFixedPoint {
x: x * self.xx + self.xy * y + self.x0,
y: y * self.yy + self.yx * x + self.y0,
}
}
}
fn packed_alpha(x: u32) -> u32 {
x >> 24
}
pub fn over(src: u32, dst: u32) -> u32 {
let a = packed_alpha(src);
let a = 256 - a;
let mask = 0xff00ff;
let rb = ((dst & 0xff00ff) * a) >> 8;
let ag = ((dst >> 8) & 0xff00ff) * a;
src + (rb & mask) | (ag & !mask)
}
fn alpha_to_alpha256(alpha: u32) -> u32 {
alpha + 1
}
fn alpha_mul_inv256(value: u32, alpha256: u32) -> u32 {
let prod = 0xFFFF - value * alpha256;
return (prod + (prod >> 8)) >> 8;
}
fn alpha_mul_256(value: u32, alpha256: u32) -> u32 {
let prod = value * alpha256;
return (prod + (prod >> 8)) >> 8;
}
pub fn muldiv255(a: u8, b: u8) -> u8 {
let tmp = (a as u32) * (b as u32) + 0x128;
((tmp + (tmp >> 8)) >> 8) as u8
}
pub fn over_in(src: u32, dst: u32, alpha: u32) -> u32 {
let src_alpha = alpha_to_alpha256(alpha);
let dst_alpha = alpha_mul_inv256(packed_alpha(src), src_alpha);
let mask = 0xFF00FF;
let src_rb = (src & mask) * src_alpha;
let src_ag = ((src >> 8) & mask) * src_alpha;
let dst_rb = (dst & mask) * dst_alpha;
let dst_ag = ((dst >> 8) & mask) * dst_alpha;
return (((src_rb + dst_rb) >> 8) & mask) | ((src_ag + dst_ag) & !mask);
}
pub fn over_in_in(src: u32, dst: u32, mask: u32, clip: u32) -> u32 {
let src_alpha = alpha_to_alpha256(mask);
let src_alpha = alpha_mul_256(src_alpha, clip);
let dst_alpha = alpha_mul_inv256(packed_alpha(src), src_alpha);
let mask = 0xFF00FF;
let src_rb = (src & mask) * src_alpha;
let src_ag = ((src >> 8) & mask) * src_alpha;
let dst_rb = (dst & mask) * dst_alpha;
let dst_ag = ((dst >> 8) & mask) * dst_alpha;
return (((src_rb + dst_rb) >> 8) & mask) | ((src_ag + dst_ag) & !mask);
}