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
use png::ColorType;
use mask::Mask;
use pixel;
#[derive(Clone,Copy,Debug,Default)]
#[repr(C)]
pub struct Gray8 {
value: u8,
}
impl Gray8 {
pub fn new(value: u8) -> Self {
Gray8 { value }
}
pub fn value(self) -> u8 {
self.value
}
fn over_alpha(self, bot: Gray8, alpha: u8) -> Self {
let v = self.value() as i32 - bot.value() as i32;
let value = (bot.value() as i32 + scale_i32(v, alpha)) as u8;
Gray8::new(value)
}
}
fn scale_i32(a: i32, b: u8) -> i32 {
let c = a * b as i32;
(((c + 1) + (c >> 8)) >> 8) as i32
}
impl pixel::PixFmt for Gray8 {
fn color_type() -> ColorType {
ColorType::Grayscale
}
fn over(pix: &mut [Self], mask: &Mask, clr: Self) {
over_fallback(pix, mask, clr);
}
fn divide_alpha(_pix: &mut [Self]) { }
}
fn over_fallback(pix: &mut [Gray8], mask: &Mask, clr: Gray8) {
for (bot, m) in pix.iter_mut().zip(mask.pixels()) {
*bot = clr.over_alpha(*bot, *m);
}
}