Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

## [Unreleased] - ReleaseDate

### Fixed

- Incorrect handling of paddings for absolute sequences
Comment thread
Ddystopia marked this conversation as resolved.
Outdated

### Changed

- **(breaking)** [#49](https://github.com/embedded-graphics/tinybmp/pull/41) Use 1.81 as the MSRV.
Expand Down
2 changes: 1 addition & 1 deletion src/raw_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ impl<'a> Iterator for Rle4Pixels<'a> {
remaining: param.saturating_sub(1),
is_odd: (param % 2) != 0,
// padding if the number of *bytes* is odd
has_padding: ((param >> 1) % 2) != 0,
has_padding: (param & 0b11).count_ones() == 1,
Comment thread
Ddystopia marked this conversation as resolved.
Outdated
};
}
}
Expand Down
Binary file added tests/ethernet_port.bmp
Binary file not shown.
Binary file added tests/ethernet_port.raw
Binary file not shown.
110 changes: 110 additions & 0 deletions tests/ethernet_port.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use embedded_graphics::{
Comment thread
Ddystopia marked this conversation as resolved.
Outdated
image::{Image, ImageRaw},
iterator::raw::RawDataSlice,
pixelcolor::{raw::LittleEndian, Bgr888, Rgb555, Rgb565, Rgb888},
prelude::*,
};
use tinybmp::Bmp;

const WIDTH: usize = 20;
const HEIGHT: usize = 20;

// TODO: use e-g framebuffer when it's added
#[derive(Debug, PartialEq)]
struct Framebuffer<C> {
pixels: [[C; WIDTH]; HEIGHT],
}

impl<C: PixelColor + From<Rgb888> + std::fmt::Debug> Framebuffer<C> {
pub fn new() -> Self {
let color = C::from(Rgb888::BLACK);

Self {
pixels: [[color; WIDTH]; HEIGHT],
}
}

pub fn from_image(image: impl ImageDrawable<Color = C>) -> Self {
let mut framebuffer = Framebuffer::<C>::new();
Image::new(&image, Point::zero())
.draw(&mut framebuffer)
.unwrap();
framebuffer
}

pub fn pixels(&self) -> impl Iterator<Item = C> + '_ {
self.pixels.iter().flatten().copied()
}

pub fn assert_eq(&self, expected: &Self) {
let zipped = || self.pixels().zip(expected.pixels());

let errors = zipped().filter(|(a, b)| a != b).count();
let first_error = zipped()
.enumerate()
.find(|(_, (a, b))| a != b)
.map(|(i, (a, b))| (Point::new((i % WIDTH) as i32, (i / WIDTH) as i32), a, b));

//let first_error = self.pixels()

if self != expected {
let first_error = first_error.unwrap();
panic!(
"framebuffer differs from expected\n{} errors\nfirst error at ({}): {:?} (expected {:?})",
errors,
first_error.0,
first_error.1,
first_error.2,
);
}
}
}

impl<C: PixelColor> DrawTarget for Framebuffer<C> {
type Color = C;
type Error = std::convert::Infallible;

fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
{
for Pixel(p, c) in pixels {
self.pixels[p.y as usize][p.x as usize] = c;
}

Ok(())
}
}

impl<C> OriginDimensions for Framebuffer<C> {
fn size(&self) -> embedded_graphics::prelude::Size {
Size::new(WIDTH as u32, HEIGHT as u32)
}
}

fn draw_raw<C>(data: &[u8]) -> Framebuffer<C>
where
C: PixelColor + From<C::Raw> + From<Rgb888> + std::fmt::Debug,
for<'a> RawDataSlice<'a, C::Raw, LittleEndian>: IntoIterator<Item = C::Raw>,
{
let raw = ImageRaw::<C, LittleEndian>::new(data, WIDTH as u32);

Framebuffer::from_image(raw)
}

fn draw_bmp<C>(data: &[u8]) -> Framebuffer<C>
where
C: PixelColor + From<Rgb555> + From<Rgb565> + From<Rgb888> + std::fmt::Debug,
{
let bmp = Bmp::<C>::from_slice(data).unwrap();

Framebuffer::from_image(bmp)
}

#[test]
fn ethernet_port() {
let raw = draw_raw::<Bgr888>(include_bytes!("ethernet_port.raw"));
let bmp = draw_bmp::<Bgr888>(include_bytes!("ethernet_port.bmp"));

bmp.assert_eq(&raw);
}
Comment thread
Ddystopia marked this conversation as resolved.
Outdated