Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

- [#50](https://github.com/embedded-graphics/tinybmp/pull/50) Fixed handling of padding bytes in absolute mode for RLE4 compressed files.

### Added

- [#51](https://github.com/embedded-graphics/tinybmp/pull/51) Added `RawBmp::colors` method to iterator of the raw color values in a file.
- [#51](https://github.com/embedded-graphics/tinybmp/pull/51) Added `DynamicRawColors`, `RawColors`, `Rle4Colors`, and `Rle8Colors` iterators.

### Changed

- **(breaking)** [#49](https://github.com/embedded-graphics/tinybmp/pull/41) Use 1.81 as the MSRV.
Expand Down
2 changes: 0 additions & 2 deletions src/header/dib_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub struct DibHeader {
pub compression: CompressionMethod,
pub image_data_len: u32,
pub channel_masks: Option<ChannelMasks>,
pub header_type: HeaderType,
pub row_order: RowOrder,
pub color_table_num_entries: u32,
}
Expand Down Expand Up @@ -105,7 +104,6 @@ impl DibHeader {
Ok((
input,
Self {
header_type,
image_size: Size::new(image_width.unsigned_abs(), image_height.unsigned_abs()),
image_data_len,
bpp,
Expand Down
33 changes: 17 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,13 @@ macro_rules! try_const {
pub(crate) use try_const;

use raw_bmp::ColorType;
use raw_iter::{RawColors, Rle4Pixels, Rle8Pixels};

pub use color_table::ColorTable;
pub use header::CompressionMethod;
pub use header::{Bpp, ChannelMasks, Header, RowOrder};
pub use iter::Pixels;
pub use raw_bmp::RawBmp;
pub use raw_iter::{RawPixel, RawPixels};
pub use raw_iter::{DynamicRawColors, RawColors, RawPixel, RawPixels, Rle4Colors, Rle8Colors};

/// A BMP-format bitmap.
///
Expand Down Expand Up @@ -294,19 +293,20 @@ where
let fallback_color = C::from(Rgb888::BLACK);
if let Some(color_table) = self.raw_bmp.color_table() {
if header.compression_method == CompressionMethod::Rle4 {
let mut colors = Rle4Pixels::new(&self.raw_bmp).map(|raw_pixel| {
let mut colors = Rle4Colors::new(&self.raw_bmp);
let map_color = |color: RawU4| {
color_table
.get(raw_pixel.color)
.get(color.into_inner() as u32)
.map(Into::into)
.unwrap_or(fallback_color)
});
};
// RLE produces pixels in bottom-up order, so we draw them line by line rather than the entire bitmap at once.
for y in (0..area.size.height).rev() {
colors.start_row();

let row = Rectangle::new(Point::new(0, y as i32), slice_size);
target.fill_contiguous(
&row,
colors.by_ref().take(area.size.width as usize),
)?;
let colors = colors.by_ref().map(map_color);
target.fill_contiguous(&row, colors.take(area.size.width as usize))?;
}
Ok(())
} else {
Expand All @@ -328,19 +328,20 @@ where
let fallback_color = C::from(Rgb888::BLACK);
if let Some(color_table) = self.raw_bmp.color_table() {
if header.compression_method == CompressionMethod::Rle8 {
let mut colors = Rle8Pixels::new(&self.raw_bmp).map(|raw_pixel| {
let mut colors = Rle8Colors::new(&self.raw_bmp);
let map_color = |color: RawU8| {
color_table
.get(raw_pixel.color)
.get(color.into_inner() as u32)
.map(Into::into)
.unwrap_or(fallback_color)
});
};
// RLE produces pixels in bottom-up order, so we draw them line by line rather than the entire bitmap at once.
for y in (0..area.size.height).rev() {
colors.start_row();

let row = Rectangle::new(Point::new(0, y as i32), slice_size);
target.fill_contiguous(
&row,
colors.by_ref().take(area.size.width as usize),
)?;
let colors = colors.by_ref().map(map_color);
target.fill_contiguous(&row, colors.take(area.size.width as usize))?;
}
Ok(())
} else {
Expand Down
11 changes: 10 additions & 1 deletion src/raw_bmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use embedded_graphics::{
use crate::{
color_table::ColorTable,
header::{Bpp, Header},
raw_iter::RawPixels,
raw_iter::{DynamicRawColors, RawPixels},
try_const, ChannelMasks, ParseError, RowOrder,
};

Expand Down Expand Up @@ -103,6 +103,15 @@ impl<'a> RawBmp<'a> {
RawPixels::new(self)
}

/// Returns an iterator over the raw colors in the image.
///
/// The iterator returns the color value in the order the pixels are stored in the file.
/// Use [`row_order`](DynamicRawColors::row_order) to determine the correct
/// pixel arrangement.
pub fn colors(&self) -> DynamicRawColors<'_> {
self.pixels().colors
}

/// Returns the raw color of a pixel.
///
/// Returns `None` if `p` is outside the image bounding box. Note that this function doesn't
Expand Down
Loading