Skip to content

Commit d809be2

Browse files
committed
feat: RawBmp::colors
Additional work that was required by `RawBmp::colors`: - Making `DynamicRawColors` public. - Making `DynamicRawColors` implement iterator. - Refactoring logic. - Rename `Rle{4,8}Pixels` to `Rle{4,8}Colors` - Due to removal of position handling in `Rle{4,8}Pixels`, they can no longer assert the the new line corresponds with the width. I left the assertion functionally the same, but with slightly different approach. Personally, I don't think it is necessary that that assert there at all - like, either way the image would be broken and developer will have to fix the `bmp` file, that assertion isn't helpful at all.
1 parent 1b93ce0 commit d809be2

3 files changed

Lines changed: 154 additions & 118 deletions

File tree

src/lib.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub use header::CompressionMethod;
211211
pub use header::{Bpp, ChannelMasks, Header, RowOrder};
212212
pub use iter::Pixels;
213213
pub use raw_bmp::RawBmp;
214-
pub use raw_iter::{RawColors, RawPixel, RawPixels, Rle4Pixels, Rle8Pixels};
214+
pub use raw_iter::{RawColors, RawPixel, RawPixels, Rle4Colors, Rle8Colors};
215215

216216
/// A BMP-format bitmap.
217217
///
@@ -293,19 +293,20 @@ where
293293
let fallback_color = C::from(Rgb888::BLACK);
294294
if let Some(color_table) = self.raw_bmp.color_table() {
295295
if header.compression_method == CompressionMethod::Rle4 {
296-
let mut colors = Rle4Pixels::new(&self.raw_bmp).map(|raw_pixel| {
296+
let (mut colors, _points) = Rle4Colors::new(&self.raw_bmp);
297+
let map_color = |color: RawU4| {
297298
color_table
298-
.get(raw_pixel.color)
299+
.get(color.into_inner() as u32)
299300
.map(Into::into)
300301
.unwrap_or(fallback_color)
301-
});
302+
};
302303
// RLE produces pixels in bottom-up order, so we draw them line by line rather than the entire bitmap at once.
303304
for y in (0..area.size.height).rev() {
305+
colors.start_a_line();
306+
304307
let row = Rectangle::new(Point::new(0, y as i32), slice_size);
305-
target.fill_contiguous(
306-
&row,
307-
colors.by_ref().take(area.size.width as usize),
308-
)?;
308+
let colors = colors.by_ref().map(map_color);
309+
target.fill_contiguous(&row, colors.take(area.size.width as usize))?;
309310
}
310311
Ok(())
311312
} else {
@@ -327,19 +328,20 @@ where
327328
let fallback_color = C::from(Rgb888::BLACK);
328329
if let Some(color_table) = self.raw_bmp.color_table() {
329330
if header.compression_method == CompressionMethod::Rle8 {
330-
let mut colors = Rle8Pixels::new(&self.raw_bmp).map(|raw_pixel| {
331+
let (mut colors, _points) = Rle8Colors::new(&self.raw_bmp);
332+
let map_color = |color: RawU8| {
331333
color_table
332-
.get(raw_pixel.color)
334+
.get(color.into_inner() as u32)
333335
.map(Into::into)
334336
.unwrap_or(fallback_color)
335-
});
337+
};
336338
// RLE produces pixels in bottom-up order, so we draw them line by line rather than the entire bitmap at once.
337339
for y in (0..area.size.height).rev() {
340+
colors.start_a_line();
341+
338342
let row = Rectangle::new(Point::new(0, y as i32), slice_size);
339-
target.fill_contiguous(
340-
&row,
341-
colors.by_ref().take(area.size.width as usize),
342-
)?;
343+
let colors = colors.by_ref().map(map_color);
344+
target.fill_contiguous(&row, colors.take(area.size.width as usize))?;
343345
}
344346
Ok(())
345347
} else {

src/raw_bmp.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use embedded_graphics::{
88
use crate::{
99
color_table::ColorTable,
1010
header::{Bpp, Header},
11-
raw_iter::RawPixels,
11+
raw_iter::{DynamicRawColors, RawPixels},
1212
try_const, ChannelMasks, ParseError, RowOrder,
1313
};
1414

@@ -103,6 +103,11 @@ impl<'a> RawBmp<'a> {
103103
RawPixels::new(self)
104104
}
105105

106+
/// Return an iterator over the raw colors in the image. Positions are dependent on the row order.
107+
pub fn colors(&self) -> DynamicRawColors<'_> {
108+
self.pixels().colors
109+
}
110+
106111
/// Returns the raw color of a pixel.
107112
///
108113
/// Returns `None` if `p` is outside the image bounding box. Note that this function doesn't

0 commit comments

Comments
 (0)