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
use embedded_graphics::{prelude::*, primitives::Rectangle};
use crate::{
header::{Bpp, Header},
pixels::Pixels,
raw_pixels::RawPixels,
ParseError,
};
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct RawBmp<'a> {
header: Header,
image_data: &'a [u8],
}
impl<'a> RawBmp<'a> {
pub fn from_slice(bytes: &'a [u8]) -> Result<Self, ParseError> {
let (_remaining, header) = Header::parse(bytes).map_err(|_| ParseError::Header)?;
let image_data = &bytes[header.image_data_start..];
Ok(Self { header, image_data })
}
pub fn size(&self) -> Size {
self.header.image_size
}
pub fn color_bpp(&self) -> Bpp {
self.header.bpp
}
pub fn image_data(&self) -> &'a [u8] {
self.image_data
}
pub fn header(&self) -> &Header {
&self.header
}
pub fn pixels<'b>(&'b self) -> RawPixels<'b, 'a> {
RawPixels::new(self)
}
pub(crate) fn bytes_per_row(&self) -> usize {
let bits_per_row =
self.header.image_size.width as usize * usize::from(self.header.bpp.bits());
(bits_per_row + 31) / 32 * (32 / 8)
}
pub(crate) fn draw<D>(&self, target: &mut D) -> Result<(), D::Error>
where
D: DrawTarget,
D::Color: From<<D::Color as PixelColor>::Raw>,
{
target.fill_contiguous(
&Rectangle::new(Point::zero(), self.size()),
Pixels::new(self.pixels()).map(|Pixel(_, color)| color),
)
}
}