Skip to content

Commit 9fc7e07

Browse files
Ddystopiarfuest
andauthored
Fix incorrect RLE4 padding checking (#50)
* fix: incorrect padding behaviour * chore: CHANGELOG.md entry * chore: add test * chore: reformat changelog entry a bit * Update CHANGELOG.md Co-authored-by: Ralf Fuest <mail@rfuest.de> * chore: improve readability of `has_padding` calculation for 4bit rle * chore: tidy up tests --------- Co-authored-by: Ralf Fuest <mail@rfuest.de>
1 parent 2c67eec commit 9fc7e07

5 files changed

Lines changed: 27 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
## [Unreleased] - ReleaseDate
88

9+
### Fixed
10+
11+
- [#50](https://github.com/embedded-graphics/tinybmp/pull/50) Fixed handling of padding bytes in absolute mode for RLE4 compressed files.
12+
913
### Changed
1014

1115
- **(breaking)** [#49](https://github.com/embedded-graphics/tinybmp/pull/41) Use 1.81 as the MSRV.

src/raw_iter.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,14 @@ impl<'a> Iterator for Rle4Pixels<'a> {
413413
// Delta encoding is unsupported.
414414
return None;
415415
}
416-
_ => {
416+
num_pixels => {
417+
let num_bytes = num_pixels.div_ceil(2);
417418
// Absolute mode
418419
self.rle_state = RleState::Absolute {
419420
remaining: param.saturating_sub(1),
420421
is_odd: (param % 2) != 0,
421422
// padding if the number of *bytes* is odd
422-
has_padding: ((param >> 1) % 2) != 0,
423+
has_padding: num_bytes & 1 != 0,
423424
};
424425
}
425426
}

tests/embedded_graphics.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use embedded_graphics::{
2-
image::Image,
2+
image::{Image, ImageRawLE},
33
mock_display::{ColorMapping, MockDisplay},
4-
pixelcolor::{Gray8, Rgb555, Rgb565, Rgb888},
4+
pixelcolor::{Bgr888, Gray8, Rgb555, Rgb565, Rgb888},
55
prelude::*,
66
primitives::Rectangle,
77
};
@@ -149,3 +149,21 @@ fn issue_8_height_is_negative() {
149149
bottom_up_display.assert_pattern(&["WK", "KK"]);
150150
top_down_display.assert_eq(&bottom_up_display);
151151
}
152+
153+
/// Test for PR #50
154+
#[test]
155+
fn pr_50_rle4_padding() {
156+
let bmp = Bmp::<Bgr888>::from_slice(include_bytes!("pr_50_rle4_padding.bmp")).unwrap();
157+
let raw = ImageRawLE::<Bgr888>::new(include_bytes!("pr_50_rle4_padding.raw"), 20);
158+
159+
assert_eq!(raw.size(), Size::new(20, 20));
160+
assert_eq!(bmp.size(), Size::new(20, 20));
161+
162+
let mut display_bmp = MockDisplay::new();
163+
let mut display_raw = MockDisplay::new();
164+
165+
bmp.draw(&mut display_bmp).unwrap();
166+
raw.draw(&mut display_raw).unwrap();
167+
168+
display_bmp.assert_eq(&display_raw);
169+
}

tests/pr_50_rle4_padding.bmp

378 Bytes
Binary file not shown.

tests/pr_50_rle4_padding.raw

1.17 KB
Binary file not shown.

0 commit comments

Comments
 (0)