Merge pull request #370 from image-rs/restructure-buffer

Restructure decoding's Buffer
This commit is contained in:
Aurelia Molzer authored and GitHub committed 2026-03-27 19:48:46 +00:00
commit 8e315c6a00
12 files changed
+411 -393

No files matched your search

+6 -6
View File
@@ -1,6 +1,6 @@
extern crate tiff;
use tiff::decoder::{Decoder, DecodingResult};
use tiff::decoder::{Decoder, DecodingSampleBuffer};
use tiff::ColorType;
use std::fs::File;
@@ -28,7 +28,7 @@ fn test_white_ieee_fp16() {
ColorType::Gray(16)
);
if let DecodingResult::F16(img) = decoder.read_image().unwrap() {
if let DecodingSampleBuffer::F16(img) = decoder.read_image().unwrap() {
for p in img {
assert!(p == half::f16::from_f32_const(1.0));
}
@@ -58,7 +58,7 @@ fn test_one_black_pixel_ieee_fp16() {
ColorType::Gray(16)
);
if let DecodingResult::F16(img) = decoder.read_image().unwrap() {
if let DecodingSampleBuffer::F16(img) = decoder.read_image().unwrap() {
for (i, p) in img.iter().enumerate() {
if i == 0 {
assert!(p < &half::f16::from_f32_const(0.001));
@@ -92,7 +92,7 @@ fn test_pattern_horizontal_differencing_ieee_fp16() {
ColorType::Gray(16)
);
if let DecodingResult::F16(img) = decoder.read_image().unwrap() {
if let DecodingSampleBuffer::F16(img) = decoder.read_image().unwrap() {
// 0, 2, 5, 8, 12, 16, 255 are black
let black = [0, 2, 5, 8, 12, 16, 255];
for (i, p) in img.iter().enumerate() {
@@ -128,7 +128,7 @@ fn test_pattern_predictor_ieee_fp16() {
ColorType::Gray(16)
);
if let DecodingResult::F16(img) = decoder.read_image().unwrap() {
if let DecodingSampleBuffer::F16(img) = decoder.read_image().unwrap() {
// 0, 2, 5, 8, 12, 16, 255 are black
let black = [0, 2, 5, 8, 12, 16, 255];
for (i, p) in img.iter().enumerate() {
@@ -199,7 +199,7 @@ fn test_predictor_ieee_fp16() {
ColorType::Gray(16)
);
if let DecodingResult::F16(img) = decoder.read_image().unwrap() {
if let DecodingSampleBuffer::F16(img) = decoder.read_image().unwrap() {
for (exp, found) in std::iter::zip(pnm_values.iter(), img.iter()) {
assert!((exp - found.to_f32()).abs() < 0.0001);
}
+3 -3
View File
@@ -1,6 +1,6 @@
extern crate tiff;
use tiff::decoder::{Decoder, DecodingResult};
use tiff::decoder::{Decoder, DecodingSampleBuffer};
use tiff::tags::Tag;
use tiff::ColorType;
@@ -56,7 +56,7 @@ fn test_geo_tiff() {
.expect("Cannot get pixel scale"),
vec![60.0, 60.0, 0.0]
);
let DecodingResult::I16(data) = decoder.read_image().unwrap() else {
let DecodingSampleBuffer::I16(data) = decoder.read_image().unwrap() else {
panic!("Cannot read band data")
};
assert_eq!(data.len(), 500);
@@ -67,7 +67,7 @@ fn test_geo_tiff() {
#[test]
fn test_webp_tiff() {
let filenames = ["usda_naip_256_webp_z3.tif"];
let mut buffer = DecodingResult::U8(vec![]);
let mut buffer = DecodingSampleBuffer::U8(vec![]);
for filename in filenames.iter() {
let path = PathBuf::from(TEST_IMAGE_DIR).join(filename);
+20 -20
View File
@@ -1,6 +1,6 @@
extern crate tiff;
use tiff::decoder::{ifd, Decoder, DecodingResult};
use tiff::decoder::{ifd, Decoder, DecodingSampleBuffer};
use tiff::tags::{ByteOrder, Type};
use tiff::ColorType;
@@ -23,7 +23,7 @@ macro_rules! test_image_sum {
let img_res = decoder.read_image().unwrap();
match img_res {
DecodingResult::$buffer(res) => {
DecodingSampleBuffer::$buffer(res) => {
let sum: $sum_ty = res.into_iter().map(<$sum_ty>::from).sum();
assert_eq!(sum, expected_sum);
}
@@ -88,17 +88,17 @@ fn compute_tiff_hash<R: Read + Seek>(reader: R) -> u32 {
// Hash decoded data: TIFF type tag (u16 repr) + LE sample bytes.
// Map each variant to the corresponding TIFF Type for byte-order normalization.
let sample_type = match &result {
DecodingResult::U8(_) => Type::BYTE,
DecodingResult::I8(_) => Type::SBYTE,
DecodingResult::U16(_) => Type::SHORT,
DecodingResult::I16(_) => Type::SSHORT,
DecodingResult::U32(_) => Type::LONG,
DecodingResult::I32(_) => Type::SLONG,
DecodingResult::F32(_) => Type::FLOAT,
DecodingResult::U64(_) => Type::LONG8,
DecodingResult::I64(_) => Type::SLONG8,
DecodingResult::F64(_) => Type::DOUBLE,
DecodingResult::F16(_) => Type::SHORT, // f16 is 2 bytes like SHORT
DecodingSampleBuffer::U8(_) => Type::BYTE,
DecodingSampleBuffer::I8(_) => Type::SBYTE,
DecodingSampleBuffer::U16(_) => Type::SHORT,
DecodingSampleBuffer::I16(_) => Type::SSHORT,
DecodingSampleBuffer::U32(_) => Type::LONG,
DecodingSampleBuffer::I32(_) => Type::SLONG,
DecodingSampleBuffer::F32(_) => Type::FLOAT,
DecodingSampleBuffer::U64(_) => Type::LONG8,
DecodingSampleBuffer::I64(_) => Type::SLONG8,
DecodingSampleBuffer::F64(_) => Type::DOUBLE,
DecodingSampleBuffer::F16(_) => Type::SHORT, // f16 is 2 bytes like SHORT
};
hasher.update(&sample_type.to_u16().to_le_bytes());
@@ -431,7 +431,7 @@ fn test_decode_data() {
decoder.next_image().expect("Cannot read image IFD");
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8));
assert_eq!(decoder.dimensions().unwrap(), (100, 100));
if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() {
if let DecodingSampleBuffer::U8(img_res) = decoder.read_image().unwrap() {
assert_eq!(image_data, img_res);
} else {
panic!("Wrong data type");
@@ -511,7 +511,7 @@ fn test_inner_access() {
let raw_sum: u64 = buf.into_iter().map(<u64>::from).sum();
match decoder.read_chunk(0).unwrap() {
DecodingResult::U8(chunk) => {
DecodingSampleBuffer::U8(chunk) => {
let sum: u64 = chunk.into_iter().map(<u64>::from).sum();
assert_eq!(sum, raw_sum);
}
@@ -570,7 +570,7 @@ fn test_tiled_incremental() {
for tile in 0..tiles {
match decoder.read_chunk(tile).unwrap() {
DecodingResult::U8(res) => {
DecodingSampleBuffer::U8(res) => {
let sum: u64 = res.into_iter().map(<u64>::from).sum();
assert_eq!(sum, sums[tile as usize]);
}
@@ -604,21 +604,21 @@ fn test_read_planar_bands() {
// 1st band (red)
match decoder.read_chunk(0).unwrap() {
DecodingResult::U8(chunk) => {
DecodingSampleBuffer::U8(chunk) => {
assert_eq!(chunk[0], 73);
}
_ => panic!("Wrong bit depth"),
}
// 2nd band (green)
match decoder.read_chunk(chunks / 3).unwrap() {
DecodingResult::U8(chunk) => {
DecodingSampleBuffer::U8(chunk) => {
assert_eq!(chunk[0], 51);
}
_ => panic!("Wrong bit depth"),
}
// 3rd band (blue)
match decoder.read_chunk(chunks / 3 * 2).unwrap() {
DecodingResult::U8(chunk) => {
DecodingSampleBuffer::U8(chunk) => {
assert_eq!(chunk[0], 30);
}
_ => panic!("Wrong bit depth"),
@@ -994,7 +994,7 @@ fn test_decode_huge_g4() {
.read_image()
.expect("failed to decode huge G4 image");
if let tiff::decoder::DecodingResult::U8(data) = result {
if let tiff::decoder::DecodingSampleBuffer::U8(data) = result {
assert_eq!(data.len(), (width as usize * height as usize));
} else {
panic!("Unexpected decoding result type");
+14 -14
View File
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::{fs::File, path};
use tiff::decoder::{Decoder, DecodingResult};
use tiff::decoder::{Decoder, DecodingSampleBuffer};
use tiff::tags::{ByteOrder, Type};
use tiff::ColorType;
@@ -252,19 +252,19 @@ fn main() {
}
/// Map DecodingResult variant to a TIFF Type for byte-order normalization.
fn sample_type_of(result: &DecodingResult) -> Type {
fn sample_type_of(result: &DecodingSampleBuffer) -> Type {
match result {
DecodingResult::U8(_) => Type::BYTE,
DecodingResult::I8(_) => Type::SBYTE,
DecodingResult::U16(_) => Type::SHORT,
DecodingResult::I16(_) => Type::SSHORT,
DecodingResult::U32(_) => Type::LONG,
DecodingResult::I32(_) => Type::SLONG,
DecodingResult::F32(_) => Type::FLOAT,
DecodingResult::U64(_) => Type::LONG8,
DecodingResult::I64(_) => Type::SLONG8,
DecodingResult::F64(_) => Type::DOUBLE,
DecodingResult::F16(_) => Type::SHORT, // f16 is 2 bytes like SHORT
DecodingSampleBuffer::U8(_) => Type::BYTE,
DecodingSampleBuffer::I8(_) => Type::SBYTE,
DecodingSampleBuffer::U16(_) => Type::SHORT,
DecodingSampleBuffer::I16(_) => Type::SSHORT,
DecodingSampleBuffer::U32(_) => Type::LONG,
DecodingSampleBuffer::I32(_) => Type::SLONG,
DecodingSampleBuffer::F32(_) => Type::FLOAT,
DecodingSampleBuffer::U64(_) => Type::LONG8,
DecodingSampleBuffer::I64(_) => Type::SLONG8,
DecodingSampleBuffer::F64(_) => Type::DOUBLE,
DecodingSampleBuffer::F16(_) => Type::SHORT, // f16 is 2 bytes like SHORT
}
}
@@ -299,7 +299,7 @@ fn compute_hash(path: &std::path::Path) -> Result<u32, tiff::TiffError> {
let img_file = File::open(path)?;
let mut decoder = Decoder::open(img_file)?;
let mut hasher = crc32fast::Hasher::new();
let mut buffer = DecodingResult::U8(vec![]);
let mut buffer = DecodingSampleBuffer::U8(vec![]);
let mut page = 0u32;
while decoder.more_images() {
+2 -2
View File
@@ -1,4 +1,4 @@
use tiff::decoder::{Decoder, DecodingResult, Limits, TiffCodingUnit};
use tiff::decoder::{Decoder, DecodingSampleBuffer, Limits, TiffCodingUnit};
use std::fs::File;
use std::path::PathBuf;
@@ -84,7 +84,7 @@ fn read_planes_by_limits() {
let io = std::io::BufReader::new(file);
let mut limits = Limits::default();
let mut buffer = DecodingResult::U8(vec![]);
let mut buffer = DecodingSampleBuffer::U8(vec![]);
// Make room for 1 plane.
let mut tif = Decoder::open(io).unwrap();
+16 -16
View File
@@ -1,6 +1,6 @@
extern crate tiff;
use tiff::decoder::{ifd, Decoder, DecodingResult};
use tiff::decoder::{ifd, Decoder, DecodingSampleBuffer};
use tiff::encoder::{colortype, Ifd, Ifd8, Predictor, SRational, TiffEncoder};
use tiff::tags::Tag;
use tiff::ColorType;
@@ -84,9 +84,9 @@ fn encode_decode() {
ifd::Value::Ascii("Image-tiff".into())
);
let mut buffer = tiff::decoder::DecodingResult::U8(vec![]);
let mut buffer = tiff::decoder::DecodingSampleBuffer::U8(vec![]);
let _layout = decoder.read_image_to_buffer(&mut buffer).unwrap();
if let tiff::decoder::DecodingResult::U8(img_res) = buffer {
if let tiff::decoder::DecodingSampleBuffer::U8(img_res) = buffer {
assert_eq!(image_data, img_res);
} else {
panic!("Wrong data type");
@@ -168,9 +168,9 @@ fn encode_decode_big() {
decoder.get_tag(Tag::Artist).unwrap(),
ifd::Value::Ascii("Image-tiff".into())
);
let mut buffer = tiff::decoder::DecodingResult::U8(vec![]);
let mut buffer = tiff::decoder::DecodingSampleBuffer::U8(vec![]);
let _layout = decoder.read_image_to_buffer(&mut buffer).unwrap();
if let DecodingResult::U8(img_res) = buffer {
if let DecodingSampleBuffer::U8(img_res) = buffer {
assert_eq!(image_data, img_res);
} else {
panic!("Wrong data type");
@@ -258,11 +258,11 @@ macro_rules! test_roundtrip {
assert_eq!(decoder.colortype().unwrap(), expected_type);
let mut buffer = tiff::decoder::DecodingResult::U8(vec![]);
let mut buffer = tiff::decoder::DecodingSampleBuffer::U8(vec![]);
decoder.read_image_to_buffer(&mut buffer).unwrap();
let image_data = match buffer {
DecodingResult::$buffer(res) => res,
DecodingSampleBuffer::$buffer(res) => res,
_ => panic!("Wrong data type"),
};
@@ -275,11 +275,11 @@ macro_rules! test_roundtrip {
}
file.seek(SeekFrom::Start(0)).unwrap();
{
let mut buffer = tiff::decoder::DecodingResult::U8(vec![]);
let mut buffer = tiff::decoder::DecodingSampleBuffer::U8(vec![]);
let mut decoder = Decoder::open(&mut file).unwrap();
decoder.next_image().unwrap();
decoder.read_image_to_buffer(&mut buffer).unwrap();
if let DecodingResult::$buffer(img_res) = buffer {
if let DecodingSampleBuffer::$buffer(img_res) = buffer {
assert_eq!(*image_data, img_res);
} else {
panic!("Wrong data type");
@@ -418,9 +418,9 @@ fn test_gray_f32_floating_point_predictor() {
decoder.next_image().unwrap();
assert_eq!(decoder.colortype().unwrap(), ColorType::Gray(32));
let mut buffer = DecodingResult::F32(vec![]);
let mut buffer = DecodingSampleBuffer::F32(vec![]);
decoder.read_image_to_buffer(&mut buffer).unwrap();
if let DecodingResult::F32(img_res) = buffer {
if let DecodingSampleBuffer::F32(img_res) = buffer {
assert_eq!(image_data.len(), img_res.len());
for (orig, decoded) in image_data.iter().zip(img_res.iter()) {
assert_eq!(
@@ -465,9 +465,9 @@ fn test_rgb_f32_floating_point_predictor() {
decoder.next_image().unwrap();
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(32));
let mut buffer = DecodingResult::F32(vec![]);
let mut buffer = DecodingSampleBuffer::F32(vec![]);
decoder.read_image_to_buffer(&mut buffer).unwrap();
if let DecodingResult::F32(img_res) = buffer {
if let DecodingSampleBuffer::F32(img_res) = buffer {
assert_eq!(image_data.len(), img_res.len());
for (i, (orig, decoded)) in image_data.iter().zip(img_res.iter()).enumerate() {
assert_eq!(
@@ -509,9 +509,9 @@ fn test_gray_f64_floating_point_predictor() {
decoder.next_image().unwrap();
assert_eq!(decoder.colortype().unwrap(), ColorType::Gray(64));
let mut buffer = DecodingResult::F64(vec![]);
let mut buffer = DecodingSampleBuffer::F64(vec![]);
decoder.read_image_to_buffer(&mut buffer).unwrap();
if let DecodingResult::F64(img_res) = buffer {
if let DecodingSampleBuffer::F64(img_res) = buffer {
assert_eq!(image_data.len(), img_res.len());
for (orig, decoded) in image_data.iter().zip(img_res.iter()) {
assert_eq!(
@@ -782,7 +782,7 @@ fn test_rows_per_strip() {
for i in 0..50 {
let img2 = [i; 2 * 100];
match decoder.read_chunk(i as u32).unwrap() {
DecodingResult::U8(data) => assert_eq!(&img2[..], &data[..]),
DecodingSampleBuffer::U8(data) => assert_eq!(&img2[..], &data[..]),
other => panic!("Incorrect strip type {other:?}"),
}
}
+3 -3
View File
@@ -2,7 +2,7 @@ extern crate tiff;
use std::io::{Cursor, Seek, Write};
use tiff::{
decoder::{Decoder, DecodingResult},
decoder::{Decoder, DecodingSampleBuffer},
encoder::{
colortype::{self, ColorType},
Compression, TiffEncoder, TiffValue,
@@ -115,7 +115,7 @@ fn encode_decode_with_compression(compression: Compression) {
// Check the RGB image
assert_eq!(
match decoder.read_image() {
Ok(DecodingResult::U16(image_data)) => image_data,
Ok(DecodingSampleBuffer::U16(image_data)) => image_data,
unexpected => panic!("Descoding RGB failed: {unexpected:?}"),
},
image_rgb.reference_data()
@@ -125,7 +125,7 @@ fn encode_decode_with_compression(compression: Compression) {
decoder.next_image().unwrap();
assert_eq!(
match decoder.read_image() {
Ok(DecodingResult::U8(image_data)) => image_data,
Ok(DecodingSampleBuffer::U8(image_data)) => image_data,
unexpected => panic!("Decoding grayscale failed: {unexpected:?}"),
},
image_grayscale.reference_data()
+4 -4
View File
@@ -1,6 +1,6 @@
extern crate tiff;
use tiff::decoder::{Decoder, DecodingResult};
use tiff::decoder::{Decoder, DecodingSampleBuffer};
use tiff::encoder::{colortype, Predictor, TiffEncoder};
use tiff::ColorType;
@@ -23,7 +23,7 @@ macro_rules! test_predict {
decoder.next_image().unwrap();
assert_eq!(decoder.colortype().unwrap(), expected_type);
let image_data = match decoder.read_image().unwrap() {
DecodingResult::$buffer(res) => res,
DecodingSampleBuffer::$buffer(res) => res,
_ => panic!("Wrong data type"),
};
@@ -130,7 +130,7 @@ macro_rules! test_predict_roundtrip {
assert_eq!(decoder.colortype().unwrap(), expected_type);
let image_data = match decoder.read_image().unwrap() {
DecodingResult::$buffer(res) => res,
DecodingSampleBuffer::$buffer(res) => res,
_ => panic!("Wrong data type"),
};
@@ -147,7 +147,7 @@ macro_rules! test_predict_roundtrip {
{
let mut decoder = Decoder::open(&mut file).unwrap();
decoder.next_image().unwrap();
if let DecodingResult::$buffer(img_res) =
if let DecodingSampleBuffer::$buffer(img_res) =
decoder.read_image().expect("Decoding image failed")
{
assert_eq!(image_data, img_res);