From 59d32a66a75b11f905ec8c3cd575cedf458200fc Mon Sep 17 00:00:00 2001 From: "A. Molzer" <5550310+197g@users.noreply.github.com> Date: Mon, 14 Jul 2025 17:49:55 +0200 Subject: [PATCH] Add test coverage for to byte decoding --- Cargo.toml | 2 ++ src/decoder/mod.rs | 4 +++ tests/decode_images.rs | 57 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index ae4f4e3..22d4718 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,8 @@ zune-jpeg = { version = "0.4.17", optional = true } [dev-dependencies] criterion = "0.3.1" +# We may already depend on this via flate2 +crc32fast = "1" [features] default = ["deflate", "jpeg", "lzw"] diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 780a22c..0587385 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -1001,6 +1001,8 @@ impl Decoder { /// Returns the layout without being specific as to the underlying type for forward /// compatibility. Note that, in general, a TIFF may contain an almost arbitrary number of /// channels of individual *bit* length and format each. + /// + /// See [`Self::colortype`] to describe the sample types. pub fn image_chunk_buffer_layout(&mut self, chunk_index: u32) -> TiffResult { let data_dims = self.image().chunk_data_dimensions(chunk_index)?; self.result_extent(data_dims.0 as usize, data_dims.1 as usize)? @@ -1052,6 +1054,8 @@ impl Decoder { /// Returns the layout without being specific as to the underlying type for forward /// compatibility. Note that, in general, a TIFF may contain an almost arbitrary number of /// channels of individual *bit* length and format each. + /// + /// See [`Self::colortype`] to describe the sample types. pub fn image_buffer_layout(&mut self) -> TiffResult { let width = self.image().width; let height = self.image().height; diff --git a/tests/decode_images.rs b/tests/decode_images.rs index 5a62279..bc93640 100644 --- a/tests/decode_images.rs +++ b/tests/decode_images.rs @@ -522,3 +522,60 @@ fn test_zstd_compression() { // gdal_translate -co COMPRESS=ZSTD -co ZSTD_LEVEL=20 int16.tif int16_zstd.tif test_image_sum_i16("int16_zstd.tif", ColorType::Gray(16), 354396); } + +fn test_image_bytes(file: &str, expected: &[(ColorType, u32)]) { + let path = PathBuf::from(TEST_IMAGE_DIR).join(file); + let img_file = File::open(path).expect("Cannot find test image!"); + let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); + + let mut buffer = vec![0u8; 0]; + for (idx, &(expected_type, expected_crc)) in expected.iter().enumerate() { + if idx > 0 { + decoder + .next_image() + .expect("mismatched number of directories"); + } + + assert_eq!(decoder.colortype().unwrap(), expected_type); + let layout = decoder + .image_buffer_layout() + .expect("Cannot describe buffer layout"); + + buffer.resize(layout.size(), 0u8); + decoder.read_image_bytes(&mut buffer).unwrap(); + + // Well almost a crc.. + assert_eq!(crc32fast::hash(&buffer), expected_crc); + } +} + +#[test] +fn bytes_cmyk_3c_8b() { + test_image_bytes("cmyk-3c-8b.tiff", &[(ColorType::CMYK(8), 2935032230)]); +} + +#[test] +fn bytes_gray_16b() { + test_image_bytes( + "minisblack-1c-16b.tiff", + &[(ColorType::Gray(16), 3524597615)], + ); +} + +#[test] +fn bytes_gray_32b() { + test_image_bytes("gradient-1c-32b.tiff", &[(ColorType::Gray(32), 2257036530)]); +} + +#[test] +fn bytes_gray_u64() { + test_image_bytes("gradient-1c-64b.tiff", &[(ColorType::Gray(64), 2156547113)]); +} + +#[test] +fn bytes_gray_f32() { + test_image_bytes( + "gradient-1c-32b-float.tiff", + &[(ColorType::Gray(32), 3232356730)], + ); +}