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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#![no_std]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
mod check_readme;
mod header;
mod pixel;
use crate::header::parse_header;
pub use crate::{
header::{FileType, Header},
pixel::Pixel,
};
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Bmp<'a> {
pub header: Header,
image_data: &'a [u8],
}
impl<'a> Bmp<'a> {
pub fn from_slice(bytes: &'a [u8]) -> Result<Self, ()> {
let (_remaining, header) = parse_header(bytes).map_err(|_| ())?;
let image_data = &bytes[header.image_data_start..];
Ok(Bmp { header, image_data })
}
pub fn image_data(&'a self) -> &'a [u8] {
self.image_data
}
pub fn width(&self) -> u32 {
self.header.image_width
}
pub fn height(&self) -> u32 {
self.header.image_height
}
pub fn dimensions(&self) -> (u32, u32) {
(self.header.image_width, self.header.image_height)
}
pub fn bpp(&self) -> u32 {
u32::from(self.header.bpp)
}
fn bytes_per_row(&self) -> usize {
let bits_per_row = self.width() as usize * self.bpp() as usize;
(bits_per_row + 31) / 32 * (32 / 8)
}
}
impl<'a> IntoIterator for &'a Bmp<'a> {
type Item = Pixel;
type IntoIter = BmpIterator<'a>;
fn into_iter(self) -> Self::IntoIter {
let pixel_stride = match self.bpp() {
1 | 8 | 16 | 24 | 32 => self.bpp() as usize,
depth => panic!("Bit depth {} not supported", depth),
};
BmpIterator {
bmp: self,
pixel_data: self.image_data(),
pixel_stride,
x: 0,
y: 0,
bit_idx: 0,
}
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct BmpIterator<'a> {
bmp: &'a Bmp<'a>,
pixel_data: &'a [u8],
pixel_stride: usize,
x: u32,
y: u32,
bit_idx: usize,
}
impl<'a> Iterator for BmpIterator<'a> {
type Item = Pixel;
fn next(&mut self) -> Option<Self::Item> {
let px = self.pixel_data;
if self.y < self.bmp.height() {
let x = self.x;
let y = self.y;
if self.x == 0 {
let row_index = (self.bmp.height() - 1) - self.y;
let row_start = self.bmp.bytes_per_row() * row_index as usize;
self.bit_idx = row_start * 8;
}
self.x += 1;
if self.x >= self.bmp.width() {
self.y += 1;
self.x = 0;
}
let byte_idx = self.bit_idx / 8;
let pixel_value = match self.pixel_stride {
1 => {
let mask = 0b_1000_0000 >> self.bit_idx % 8;
(px[byte_idx] & mask != 0) as u32
}
8 => u32::from(px[byte_idx]),
16 => u32::from_le_bytes([px[byte_idx], px[byte_idx + 1], 0, 0]),
24 => u32::from_le_bytes([px[byte_idx], px[byte_idx + 1], px[byte_idx + 2], 0]),
32 => u32::from_le_bytes([
px[byte_idx],
px[byte_idx + 1],
px[byte_idx + 2],
px[byte_idx + 3],
]),
_ => unreachable!(),
};
self.bit_idx += self.pixel_stride;
Some(Pixel {
x,
y,
color: pixel_value,
})
} else {
None
}
}
}
#[cfg(feature = "graphics")]
mod e_g {
use super::*;
use core::marker::PhantomData;
use embedded_graphics::{
drawable::Pixel as EgPixel,
geometry::Point,
image::{ImageDimensions, IntoPixelIter},
pixelcolor::{raw::RawData, PixelColor},
};
#[derive(Debug)]
pub struct EgPixelIterator<'a, C> {
it: BmpIterator<'a>,
c: PhantomData<C>,
}
impl<'a, C> Iterator for EgPixelIterator<'a, C>
where
C: PixelColor + From<<C as PixelColor>::Raw>,
{
type Item = EgPixel<C>;
fn next(&mut self) -> Option<Self::Item> {
self.it.next().map(|p| {
let raw = C::Raw::from_u32(p.color);
EgPixel(Point::new(p.x as i32, p.y as i32), raw.into())
})
}
}
impl ImageDimensions for Bmp<'_> {
fn width(&self) -> u32 {
Bmp::width(&self)
}
fn height(&self) -> u32 {
Bmp::height(&self)
}
}
impl<'a, C> IntoPixelIter<C> for &'a Bmp<'_>
where
C: PixelColor + From<<C as PixelColor>::Raw>,
{
type PixelIterator = EgPixelIterator<'a, C>;
fn pixel_iter(self) -> Self::PixelIterator {
EgPixelIterator {
it: self.into_iter(),
c: PhantomData,
}
}
}
}
#[cfg(feature = "graphics")]
pub use e_g::*;