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
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ImageInfo {
pub width: usize,
pub height: usize,
pub row_stride: usize,
pub pixel_format: PixelFormat,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PixelFormat {
Rgb8,
Rgba8,
Bgr8,
Bgra8,
Mono8,
}
impl ImageInfo {
pub fn new(pixel_format: PixelFormat, width: usize, height: usize) -> Self {
let row_stride = usize::from(pixel_format.bytes_per_pixel()) * width;
Self { pixel_format, width, height, row_stride}
}
pub fn rgb8(width: usize, height: usize) -> Self {
Self::new(PixelFormat::Rgb8, width, height)
}
pub fn rgba8(width: usize, height: usize) -> Self {
Self::new(PixelFormat::Rgba8, width, height)
}
pub fn bgr8(width: usize, height: usize) -> Self {
Self::new(PixelFormat::Bgr8, width, height)
}
pub fn bgra8(width: usize, height: usize) -> Self {
Self::new(PixelFormat::Bgra8, width, height)
}
pub fn mono8(width: usize, height: usize) -> Self {
Self::new(PixelFormat::Mono8, width, height)
}
}
impl PixelFormat {
pub fn channels(self) -> u8 {
match self {
PixelFormat::Bgr8 => 3,
PixelFormat::Bgra8 => 4,
PixelFormat::Rgb8 => 3,
PixelFormat::Rgba8 => 4,
PixelFormat::Mono8 => 1,
}
}
const fn byte_depth(self) -> u8 {
1
}
pub fn bytes_per_pixel(self) -> u8 {
self.byte_depth() * self.channels()
}
}