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
use png::ColorType;
use pixel::{PixFmt,lerp_u8};
#[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 value = lerp_u8(self.value(), bot.value(), alpha);
Gray8::new(value)
}
}
impl PixFmt for Gray8 {
fn color_type() -> ColorType {
ColorType::Grayscale
}
fn over(pix: &mut [Self], mask: &[u8], clr: Self) {
debug_assert_eq!(pix.len(), mask.len());
over_fallback(pix, mask, clr);
}
fn divide_alpha(_pix: &mut [Self]) { }
}
fn over_fallback(pix: &mut [Gray8], mask: &[u8], clr: Gray8) {
for (bot, m) in pix.iter_mut().zip(mask) {
*bot = clr.over_alpha(*bot, *m);
}
}