Skip to content

Commit

Permalink
Fix 1bpp image decoding and add a test image (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
fintelia authored Nov 1, 2024
1 parent e28ad56 commit 96d3a02
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/decoder/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
17 changes: 12 additions & 5 deletions src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,13 +969,20 @@ impl<R: Read + Seek> Decoder<R> {
}

fn result_buffer(&self, width: usize, height: usize) -> TiffResult<DecodingResult> {
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 {
Expand Down
5 changes: 5 additions & 0 deletions tests/decode_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Binary file added tests/images/tiled-gray-i1.tif
Binary file not shown.

0 comments on commit 96d3a02

Please sign in to comment.