Add test coverage for to byte decoding

This commit is contained in:
A. Molzer committed 2025-07-14 18:23:30 +02:00
1 parent 5a6b2ad8f3
commit 59d32a66a7
3 files changed
+63

No files matched your search

+2
View File
@@ -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"]
+4
View File
@@ -1001,6 +1001,8 @@ impl<R: Read + Seek> Decoder<R> {
/// 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<Layout> {
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<R: Read + Seek> Decoder<R> {
/// 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<Layout> {
let width = self.image().width;
let height = self.image().height;
+57
View File
@@ -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)],
);
}