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
use png::ColorType;
use pixel::{PixFmt,lerp_u8};
#[derive(Clone,Copy,Debug,Default)]
#[repr(C)]
pub struct Rgb8 {
red: u8,
green: u8,
blue: u8,
}
impl From<Rgb8> for i32 {
fn from(c: Rgb8) -> i32 {
let red = (c.red() as i32) << 0;
let green = (c.green() as i32) << 8;
let blue = (c.blue() as i32) << 16;
red | green | blue
}
}
impl Rgb8 {
pub fn new(red: u8, green: u8, blue: u8) -> Self {
Rgb8 { red, green, blue }
}
pub fn red(self) -> u8 {
self.red
}
pub fn green(self) -> u8 {
self.green
}
pub fn blue(self) -> u8 {
self.blue
}
fn over_alpha(self, bot: Rgb8, alpha: u8) -> Self {
let red = lerp_u8(self.red(), bot.red(), alpha);
let green = lerp_u8(self.green(), bot.green(), alpha);
let blue = lerp_u8(self.blue(), bot.blue(), alpha);
Rgb8::new(red, green, blue)
}
}
impl PixFmt for Rgb8 {
fn color_type() -> ColorType {
ColorType::RGB
}
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 [Rgb8], mask: &[u8], clr: Rgb8) {
for (bot, m) in pix.iter_mut().zip(mask) {
let mut out = clr.over_alpha(*bot, *m);
*bot = out;
}
}