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
use std::cmp::{Eq, PartialEq};
use std::fmt;
use std::hash::Hash;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PixelFormat {
Custom(String),
Depth(u32),
Gray(u32),
Bgr(u32),
Rgb(u32),
Jpeg,
}
impl PixelFormat {
pub fn bits(&self) -> Option<u32> {
match self {
PixelFormat::Custom(_) => None,
PixelFormat::Depth(bits) => Some(*bits),
PixelFormat::Gray(bits) => Some(*bits),
PixelFormat::Bgr(bits) => Some(*bits),
PixelFormat::Rgb(bits) => Some(*bits),
PixelFormat::Jpeg => None,
}
}
}
impl fmt::Display for PixelFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
#[derive(Clone, Debug)]
pub struct ImageFormat {
pub width: u32,
pub height: u32,
pub pixfmt: PixelFormat,
pub stride: Option<usize>,
}
impl ImageFormat {
pub fn new(width: u32, height: u32, pixfmt: PixelFormat) -> Self {
let stride = if let Some(bits) = pixfmt.bits() {
Some((width * (bits / 8)) as usize)
} else {
None
};
ImageFormat {
width,
height,
pixfmt,
stride,
}
}
pub fn stride(mut self, stride: usize) -> Self {
self.stride = Some(stride);
self
}
}