From 96d3a02efe032f24175dacc614796fad48ec2772 Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Thu, 31 Oct 2024 20:21:02 -0700 Subject: [PATCH] Fix 1bpp image decoding and add a test image (#252) --- src/decoder/image.rs | 2 -- src/decoder/mod.rs | 17 ++++++++++++----- tests/decode_images.rs | 5 +++++ tests/images/tiled-gray-i1.tif | Bin 0 -> 614 bytes 4 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 tests/images/tiled-gray-i1.tif diff --git a/src/decoder/image.rs b/src/decoder/image.rs index 97658f45..1a616401 100644 --- a/src/decoder/image.rs +++ b/src/decoder/image.rs @@ -687,8 +687,6 @@ impl Image { let row = &mut row[..data_row_bytes]; reader.read_exact(row)?; - println!("chunk={chunk_index}, index={i}"); - // Skip horizontal padding if chunk_row_bytes > data_row_bytes { let len = u64::try_from(chunk_row_bytes - data_row_bytes)?; diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 09d530bc..8b5ffbcf 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -969,13 +969,20 @@ impl Decoder { } fn result_buffer(&self, width: usize, height: usize) -> TiffResult { - let buffer_size = match width + let bits_per_sample = self.image().bits_per_sample; + + let row_samples = if bits_per_sample >= 8 { + width + } else { + ((((width as u64) * bits_per_sample as u64) + 7) / 8) + .try_into() + .map_err(|_| TiffError::LimitsExceeded)? + }; + + let buffer_size = row_samples .checked_mul(height) .and_then(|x| x.checked_mul(self.image().samples_per_pixel())) - { - Some(s) => s, - None => return Err(TiffError::LimitsExceeded), - }; + .ok_or(TiffError::LimitsExceeded)?; let max_sample_bits = self.image().bits_per_sample; match self.image().sample_format { diff --git a/tests/decode_images.rs b/tests/decode_images.rs index 77f48e9d..1c769d06 100644 --- a/tests/decode_images.rs +++ b/tests/decode_images.rs @@ -216,6 +216,11 @@ fn issue_69() { //assert!(img_res.is_ok()); //} +#[test] +fn test_tiled_gray_i1() { + test_image_sum_u8("tiled-gray-i1.tif", ColorType::Gray(1), 30531); +} + #[test] fn test_tiled_rgb_u8() { test_image_sum_u8("tiled-rgb-u8.tif", ColorType::RGB(8), 39528948); diff --git a/tests/images/tiled-gray-i1.tif b/tests/images/tiled-gray-i1.tif new file mode 100644 index 0000000000000000000000000000000000000000..d6305d4c2aff776c60fb9651c315144d516c6bf3 GIT binary patch literal 614 zcmchSy$!-J5QU#(?6=P5`0wXX=W=MCuQzFGh96J2; za-V&ky?UyX)&!_tQdiyrV1)he$aGv hi{Vd_)&};_UhHZm|2f}F!JBq(8T)qg$1?vp`vPt@f^7f* literal 0 HcmV?d00001