Fuzz / Fuzz (decode_comprehensive) (push) Failing after 37s
Fuzz / Fuzz (decode_image) (push) Failing after 35s
Fuzz / Fuzz regression (push) Successful in 28s
Rust CI / build (1.88.0) (push) Successful in 46s
Rust CI / build (beta) (push) Successful in 38s
Rust CI / build (nightly) (push) Successful in 36s
Rust CI / build (stable) (push) Successful in 39s
Rust CI / test-platforms (ubuntu-latest) (push) Successful in 33s
Rust CI / features () (push) Successful in 29s
Rust CI / features (default) (push) Successful in 37s
Rust CI / features (default,zstd) (push) Successful in 36s
Rust CI / features (deflate) (push) Successful in 35s
Rust CI / features (jpeg) (push) Successful in 36s
Rust CI / features (lzw) (push) Successful in 35s
Rust CI / features (zstd) (push) Successful in 35s
Rust CI / other_archs (i686-unknown-linux-gnu) (push) Failing after 19s
Rust CI / other_archs (powerpc-unknown-linux-gnu) (push) Failing after 18s
Rust CI / rustfmt (push) Successful in 25s
Rust CI / msrv (push) Successful in 57s
Rust CI / clippy (push) Successful in 33s
Rust CI / test-platforms (macos-26-intel) (push) Canceled after 0s
Rust CI / test-platforms (macos-latest) (push) Canceled after 0s
Rust CI / test-platforms (ubuntu-24.04-arm) (push) Canceled after 0s
Rust CI / test-platforms (windows-11-arm) (push) Canceled after 0s
Rust CI / test-platforms (windows-latest) (push) Canceled after 0s
1426 lines
49 KiB
Rust
1426 lines
49 KiB
Rust
extern crate tiff;
|
||
|
||
use tiff::decoder::{ifd, Decoder, DecodingSampleBuffer};
|
||
use tiff::tags::{ByteOrder, PhotometricInterpretation, SampleFormat, Type};
|
||
use tiff::ColorType;
|
||
|
||
use std::fs::File;
|
||
use std::io::{Cursor, Read, Seek};
|
||
use std::path::PathBuf;
|
||
|
||
const TEST_IMAGE_DIR: &str = "./tests/images/";
|
||
const SEQ_IMAGE_DIR: &str = "./tests/images/seq/";
|
||
|
||
macro_rules! test_image_sum {
|
||
($name:ident, $buffer:ident, $sum_ty:ty) => {
|
||
fn $name(file: &str, expected_type: ColorType, expected_sum: $sum_ty) {
|
||
let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
|
||
let img_file = File::open(path).expect("Cannot find test image!");
|
||
let mut decoder = Decoder::open(img_file)
|
||
.expect("Cannot create decoder")
|
||
.with_lenient(true);
|
||
|
||
decoder.next_image().expect("Cannot read image IFD");
|
||
assert_eq!(decoder.colortype().unwrap(), expected_type);
|
||
let img_res = decoder.read_image().unwrap();
|
||
|
||
match img_res {
|
||
DecodingSampleBuffer::$buffer(res) => {
|
||
let sum: $sum_ty = res.into_iter().map(<$sum_ty>::from).sum();
|
||
assert_eq!(sum, expected_sum);
|
||
}
|
||
_ => panic!("Wrong bit depth"),
|
||
}
|
||
}
|
||
};
|
||
}
|
||
|
||
test_image_sum!(test_image_sum_u8, U8, u64);
|
||
test_image_sum!(test_image_sum_i8, I8, i64);
|
||
test_image_sum!(test_image_sum_u16, U16, u64);
|
||
test_image_sum!(test_image_sum_i16, I16, i64);
|
||
test_image_sum!(test_image_sum_u32, U32, u64);
|
||
test_image_sum!(test_image_sum_u64, U64, u64);
|
||
test_image_sum!(test_image_sum_f32, F32, f32);
|
||
test_image_sum!(test_image_sum_f64, F64, f64);
|
||
|
||
/// Compute a CRC32 hash of the full decoded TIFF output including metadata.
|
||
///
|
||
/// Hashes: page count, and for each page: dimensions, color type, and decoded
|
||
/// sample data (in little-endian byte order for multi-byte types). This catches
|
||
/// wrong dimensions, wrong color type, byte-order errors, bit-alignment errors,
|
||
/// stride/padding errors, and pixel value errors.
|
||
fn compute_tiff_hash<R: Read + Seek>(reader: R) -> u32 {
|
||
let mut decoder = Decoder::open(reader).expect("Cannot create decoder");
|
||
let mut hasher = crc32fast::Hasher::new();
|
||
|
||
let mut page = 0u32;
|
||
loop {
|
||
decoder.next_image().expect("Cannot read image IFD");
|
||
let (width, height) = decoder.dimensions().unwrap();
|
||
let color_type = decoder.colortype().unwrap();
|
||
let mut result = decoder.read_image().unwrap();
|
||
|
||
// Hash page index + dimensions
|
||
hasher.update(&page.to_le_bytes());
|
||
hasher.update(&width.to_le_bytes());
|
||
hasher.update(&height.to_le_bytes());
|
||
|
||
// Hash color type (discriminant + fields)
|
||
match color_type {
|
||
ColorType::Gray(n) => hasher.update(&[0, n]),
|
||
ColorType::RGB(n) => hasher.update(&[1, n]),
|
||
ColorType::Palette(n) => hasher.update(&[2, n]),
|
||
ColorType::GrayA(n) => hasher.update(&[3, n]),
|
||
ColorType::RGBA(n) => hasher.update(&[4, n]),
|
||
ColorType::CMYK(n) => hasher.update(&[5, n]),
|
||
ColorType::CMYKA(n) => hasher.update(&[6, n]),
|
||
ColorType::YCbCr(n) => hasher.update(&[7, n]),
|
||
ColorType::Lab(n) => hasher.update(&[8, n]),
|
||
ColorType::Multiband {
|
||
bit_depth,
|
||
num_samples,
|
||
} => {
|
||
hasher.update(&[9, bit_depth]);
|
||
hasher.update(&num_samples.to_le_bytes());
|
||
}
|
||
other => panic!("compute_tiff_hash: unhandled ColorType {other:?}"),
|
||
}
|
||
|
||
// 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 {
|
||
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());
|
||
|
||
let mut buf = result.as_buffer(0);
|
||
let bytes = buf.as_bytes_mut();
|
||
ByteOrder::native().convert(sample_type, bytes, ByteOrder::LittleEndian);
|
||
hasher.update(bytes);
|
||
|
||
page += 1;
|
||
if !decoder.more_images() {
|
||
break;
|
||
}
|
||
}
|
||
|
||
// Hash total page count at the end
|
||
hasher.update(&page.to_le_bytes());
|
||
hasher.finalize()
|
||
}
|
||
|
||
/// Verify all TIFF files in tests/images/seq/ against their filename-embedded CRC32 hash.
|
||
///
|
||
/// Each file is named `<description>-<8hex>.tiff` where the last segment before `.tiff`
|
||
/// is the expected CRC32 hash of (dimensions, color type, decoded data) for all pages.
|
||
#[test]
|
||
fn verify_all_seq_images() {
|
||
let seq_dir = std::path::Path::new(SEQ_IMAGE_DIR);
|
||
if !seq_dir.is_dir() {
|
||
panic!("seq image directory not found: {}", seq_dir.display());
|
||
}
|
||
|
||
let mut files: Vec<_> = std::fs::read_dir(seq_dir)
|
||
.expect("Cannot read seq image directory")
|
||
.filter_map(|e| e.ok())
|
||
.filter(|e| {
|
||
e.path()
|
||
.extension()
|
||
.is_some_and(|ext| ext == "tiff" || ext == "tif")
|
||
})
|
||
.collect();
|
||
files.sort_by_key(|e| e.file_name());
|
||
|
||
assert!(
|
||
!files.is_empty(),
|
||
"No .tiff files found in {}",
|
||
seq_dir.display()
|
||
);
|
||
|
||
let mut failures = Vec::new();
|
||
for entry in &files {
|
||
let path = entry.path();
|
||
let stem = path.file_stem().unwrap().to_str().unwrap();
|
||
|
||
// Extract expected hash: last '-'-delimited segment
|
||
let expected_hash = stem
|
||
.rsplit('-')
|
||
.next()
|
||
.expect("filename must contain a '-' before the hash");
|
||
|
||
// Validate it looks like an 8-char hex string
|
||
if expected_hash.len() != 8 || !expected_hash.chars().all(|c| c.is_ascii_hexdigit()) {
|
||
failures.push(format!(
|
||
"{}: filename does not end with 8-char hex hash (got '{}')",
|
||
stem, expected_hash
|
||
));
|
||
continue;
|
||
}
|
||
|
||
let file = File::open(&path).expect("Cannot open test image");
|
||
let actual_hash = format!("{:08x}", compute_tiff_hash(file));
|
||
|
||
if actual_hash != expected_hash {
|
||
failures.push(format!(
|
||
"{}: expected hash {} but got {}",
|
||
stem, expected_hash, actual_hash
|
||
));
|
||
}
|
||
}
|
||
|
||
if !failures.is_empty() {
|
||
panic!(
|
||
"{} of {} seq images failed hash verification:\n {}",
|
||
failures.len(),
|
||
files.len(),
|
||
failures.join("\n ")
|
||
);
|
||
}
|
||
|
||
eprintln!("Verified {} seq images", files.len());
|
||
}
|
||
|
||
/// Helper: print the correct hash-suffixed filename for every *.tiff in tests/images/seq/.
|
||
/// Run with: cargo test print_seq_hashes -- --ignored --nocapture
|
||
#[test]
|
||
#[ignore]
|
||
fn print_seq_hashes() {
|
||
let dir = std::path::Path::new(SEQ_IMAGE_DIR);
|
||
let mut files: Vec<_> = std::fs::read_dir(dir)
|
||
.expect("Cannot read image directory")
|
||
.filter_map(|e| e.ok())
|
||
.filter(|e| {
|
||
e.file_name()
|
||
.to_str()
|
||
.is_some_and(|n| n.starts_with("seq-") && n.ends_with(".tiff"))
|
||
})
|
||
.collect();
|
||
files.sort_by_key(|e| e.file_name());
|
||
|
||
for entry in &files {
|
||
let path = entry.path();
|
||
let name = path.file_name().unwrap().to_str().unwrap();
|
||
let file = File::open(&path).expect("Cannot open test image");
|
||
let hash = format!("{:08x}", compute_tiff_hash(file));
|
||
// Strip the old hash (last -XXXXXXXX before .tiff) if present
|
||
let stem = path.file_stem().unwrap().to_str().unwrap();
|
||
let base = match stem.rsplit_once('-') {
|
||
Some((prefix, suffix))
|
||
if suffix.len() == 8 && suffix.chars().all(|c| c.is_ascii_hexdigit()) =>
|
||
{
|
||
prefix
|
||
}
|
||
_ => stem,
|
||
};
|
||
let new_name = format!("{base}-{hash}.tiff");
|
||
if new_name != name {
|
||
eprintln!("RENAME: {name} -> {new_name}");
|
||
} else {
|
||
eprintln!("OK: {name}");
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Tests that a decoder can be constructed for an image and the color type
|
||
/// read from the IFD and is of the appropriate type, but the type is
|
||
/// unsupported.
|
||
#[allow(dead_code)]
|
||
fn test_image_color_type_unsupported(file: &str, expected_type: ColorType) {
|
||
let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
|
||
let img_file = File::open(path).expect("Cannot find test image!");
|
||
let mut decoder = Decoder::open(img_file).expect("Cannot create decoder");
|
||
decoder.next_image().expect("Cannot read image IFD");
|
||
|
||
assert_eq!(decoder.colortype().unwrap(), expected_type);
|
||
assert!(matches!(
|
||
decoder.read_image(),
|
||
Err(tiff::TiffError::UnsupportedError(
|
||
tiff::TiffUnsupportedError::UnsupportedColorType(_),
|
||
))
|
||
));
|
||
}
|
||
|
||
#[test]
|
||
fn test_cmyk_u8() {
|
||
test_image_sum_u8("cmyk-3c-8b.tiff", ColorType::CMYK(8), 8522658);
|
||
}
|
||
|
||
#[test]
|
||
fn test_cmyk_u16() {
|
||
test_image_sum_u16("cmyk-3c-16b.tiff", ColorType::CMYK(16), 2181426827);
|
||
}
|
||
|
||
#[test]
|
||
fn test_cmyk_f32() {
|
||
test_image_sum_f32("cmyk-3c-32b-float.tiff", ColorType::CMYK(32), 496.0405);
|
||
}
|
||
|
||
#[test]
|
||
fn test_palette_u1() {
|
||
test_image_sum_u8("palette-1c-1b.tiff", ColorType::Palette(1), 379313);
|
||
}
|
||
|
||
#[test]
|
||
fn test_palette_u4() {
|
||
test_image_sum_u8("palette-1c-4b.tiff", ColorType::Palette(4), 1480490);
|
||
}
|
||
|
||
#[test]
|
||
fn test_palette_u8() {
|
||
test_image_sum_u8("palette-1c-8b.tiff", ColorType::Palette(8), 2876948);
|
||
}
|
||
|
||
#[test]
|
||
fn test_palette_color_map() {
|
||
let path = PathBuf::from(TEST_IMAGE_DIR).join("palette-1c-8b.tiff");
|
||
let img_file = File::open(path).expect("Cannot find test image!");
|
||
let mut decoder = Decoder::open(img_file).expect("Cannot create decoder");
|
||
decoder.next_image().expect("Cannot read image IFD");
|
||
|
||
assert_eq!(decoder.colortype().unwrap(), ColorType::Palette(8));
|
||
|
||
let color_map = decoder
|
||
.color_map()
|
||
.expect("Palette image should have a color map");
|
||
// 8-bit palette: 3 * 256 = 768 entries
|
||
assert_eq!(color_map.len(), 768);
|
||
|
||
// Each section (R, G, B) has 256 entries
|
||
let r = &color_map[..256];
|
||
let g = &color_map[256..512];
|
||
let b = &color_map[512..768];
|
||
|
||
// Verify all values are valid u16 (non-zero palette, at least some variation)
|
||
assert!(r.iter().any(|&v| v > 0));
|
||
assert!(g.iter().any(|&v| v > 0));
|
||
assert!(b.iter().any(|&v| v > 0));
|
||
}
|
||
|
||
#[test]
|
||
fn test_gray_u8() {
|
||
test_image_sum_u8("minisblack-1c-8b.tiff", ColorType::Gray(8), 2840893);
|
||
}
|
||
|
||
#[test]
|
||
fn test_gray_u12() {
|
||
test_image_sum_u16("12bit.cropped.tiff", ColorType::Gray(12), 10973);
|
||
}
|
||
|
||
#[test]
|
||
fn test_gray_u16() {
|
||
test_image_sum_u16("minisblack-1c-16b.tiff", ColorType::Gray(16), 733126239);
|
||
}
|
||
|
||
#[test]
|
||
fn test_gray_u32() {
|
||
test_image_sum_u32("gradient-1c-32b.tiff", ColorType::Gray(32), 549892913787);
|
||
}
|
||
|
||
#[test]
|
||
fn test_gray_u64() {
|
||
test_image_sum_u64("gradient-1c-64b.tiff", ColorType::Gray(64), 549892913787);
|
||
}
|
||
|
||
#[test]
|
||
fn test_gray_f32() {
|
||
test_image_sum_f32("gradient-1c-32b-float.tiff", ColorType::Gray(32), 128.03194);
|
||
}
|
||
|
||
#[test]
|
||
fn test_gray_f64() {
|
||
test_image_sum_f64(
|
||
"gradient-1c-64b-float.tiff",
|
||
ColorType::Gray(64),
|
||
128.0319210877642,
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn test_rgb_u8() {
|
||
test_image_sum_u8("rgb-3c-8b.tiff", ColorType::RGB(8), 7842108);
|
||
}
|
||
|
||
#[test]
|
||
fn test_rgb_u12() {
|
||
test_image_sum_u16("12bit.cropped.rgb.tiff", ColorType::RGB(12), 32919);
|
||
}
|
||
|
||
#[test]
|
||
fn test_rgb_u16() {
|
||
test_image_sum_u16("rgb-3c-16b.tiff", ColorType::RGB(16), 2024349944);
|
||
}
|
||
|
||
#[test]
|
||
fn test_rgb_u32() {
|
||
test_image_sum_u32("gradient-3c-32b.tiff", ColorType::RGB(32), 2030834111716);
|
||
}
|
||
|
||
#[test]
|
||
fn test_rgb_u64() {
|
||
test_image_sum_u64("gradient-3c-64b.tiff", ColorType::RGB(64), 2030834111716);
|
||
}
|
||
|
||
#[test]
|
||
fn test_rgb_f32() {
|
||
test_image_sum_f32("gradient-3c-32b-float.tiff", ColorType::RGB(32), 472.8405);
|
||
}
|
||
|
||
#[test]
|
||
fn test_int8() {
|
||
test_image_sum_i8("int8.tif", ColorType::Gray(8), 3111)
|
||
}
|
||
|
||
#[test]
|
||
fn test_int8_rgb() {
|
||
test_image_sum_i8("int8_rgb.tif", ColorType::RGB(8), -10344)
|
||
}
|
||
|
||
#[test]
|
||
fn test_int16() {
|
||
test_image_sum_i16("int16.tif", ColorType::Gray(16), 354396);
|
||
}
|
||
|
||
#[test]
|
||
fn test_int16_rgb() {
|
||
test_image_sum_i16("int16_rgb.tif", ColorType::RGB(16), 1063188);
|
||
}
|
||
|
||
#[test]
|
||
fn test_string_tags() {
|
||
// these files have null-terminated strings for their Software tag. One has extra bytes after
|
||
// the null byte, so we check both to ensure that we're truncating properly
|
||
let filenames = ["minisblack-1c-16b.tiff", "rgb-3c-16b.tiff"];
|
||
for filename in filenames.iter() {
|
||
let path = PathBuf::from(TEST_IMAGE_DIR).join(filename);
|
||
let img_file = File::open(path).expect("Cannot find test image!");
|
||
let mut decoder = Decoder::open(img_file).expect("Cannot create decoder");
|
||
decoder.next_image().expect("Cannot read image IFD");
|
||
let software = decoder
|
||
.current_ifd()
|
||
.get_tag(tiff::tags::Tag::Software)
|
||
.unwrap();
|
||
match software {
|
||
ifd::Value::Ascii(s) => assert_eq!(
|
||
&s,
|
||
"GraphicsMagick 1.2 unreleased Q16 http://www.GraphicsMagick.org/"
|
||
),
|
||
other => unreachable!("Expected Ascii tag from file, got {other:?}"),
|
||
};
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn test_decode_data() {
|
||
let mut image_data = Vec::new();
|
||
for x in 0..100 {
|
||
for y in 0..100u8 {
|
||
let val = x + y;
|
||
image_data.push(val);
|
||
image_data.push(val);
|
||
image_data.push(val);
|
||
}
|
||
}
|
||
let file = File::open("./tests/decodedata-rgb-3c-8b.tiff").unwrap();
|
||
let mut decoder = Decoder::open(file).unwrap();
|
||
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 DecodingSampleBuffer::U8(img_res) = decoder.read_image().unwrap() {
|
||
assert_eq!(image_data, img_res);
|
||
} else {
|
||
panic!("Wrong data type");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn issue_69() {
|
||
test_image_sum_u16("issue_69_lzw.tiff", ColorType::Gray(16), 1015486);
|
||
test_image_sum_u16("issue_69_packbits.tiff", ColorType::Gray(16), 1015486);
|
||
}
|
||
|
||
// TODO: GrayA support
|
||
//#[test]
|
||
//fn test_gray_alpha_u8()
|
||
//{
|
||
//let img_file = File::open("./tests/images/minisblack-2c-8b-alpha.tiff").expect("Cannot find test image!");
|
||
//let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
|
||
//assert_eq!(decoder.colortype().unwrap(), ColorType::GrayA(8));
|
||
//let img_res = decoder.read_image();
|
||
//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);
|
||
}
|
||
|
||
#[test]
|
||
fn test_tiled_rect_rgb_u8() {
|
||
test_image_sum_u8("tiled-rect-rgb-u8.tif", ColorType::RGB(8), 62081032);
|
||
}
|
||
|
||
#[test]
|
||
fn test_tiled_extra_samples_border_u8() {
|
||
// Regression test for https://github.com/image-rs/image-tiff/issues/400
|
||
//
|
||
// A tiled image whose dimensions are not multiples of the tile size, with
|
||
// an extra sample that is discarded on decode. The horizontal padding of
|
||
// the right border tiles used to be written past the end of the output
|
||
// row, spilling into the leftmost columns of the following row. The
|
||
// fixture's tile padding is filled with 0xEE so any spill is visible.
|
||
let path = PathBuf::from(TEST_IMAGE_DIR).join("tiled-extra-samples-border-u8.tif");
|
||
let img_file = File::open(path).expect("Cannot find test image!");
|
||
let mut decoder = Decoder::open(img_file).expect("Cannot create decoder");
|
||
decoder.next_image().expect("Cannot read image IFD");
|
||
assert_eq!(decoder.dimensions().unwrap(), (40, 40));
|
||
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8));
|
||
|
||
let DecodingSampleBuffer::U8(data) = decoder.read_image().unwrap() else {
|
||
panic!("Wrong bit depth")
|
||
};
|
||
|
||
let mut expected = Vec::with_capacity(40 * 40 * 3);
|
||
for y in 0..40u32 {
|
||
for x in 0..40u32 {
|
||
expected.push(((x * 3) % 251) as u8);
|
||
expected.push(((y * 7) % 251) as u8);
|
||
expected.push(((x + y) % 251) as u8);
|
||
}
|
||
}
|
||
assert_eq!(data, expected);
|
||
}
|
||
|
||
#[test]
|
||
fn test_tiled_planar_border_u16() {
|
||
// A planar (separate) tiled image whose dimensions are not multiples of the tile size.
|
||
// The tile grid repeats for every plane, but the chunk (tile) index counts all planes, so
|
||
// the border padding of the second plane used to be looked up with the wrong tile index.
|
||
// The bottom-right tile of plane 1 then claimed a full tile height, and the decoder ran
|
||
// past the end of the output buffer.
|
||
//
|
||
// Note that `read_image` only decodes the first plane, so this has to go through
|
||
// `read_image_to_buffer` to reach the tiles that were mis-sized.
|
||
let path = PathBuf::from(TEST_IMAGE_DIR).join("tiled-planar-2c-16b.tif");
|
||
let img_file = File::open(path).expect("Cannot find test image!");
|
||
let mut decoder = Decoder::open(img_file).expect("Cannot create decoder");
|
||
decoder.next_image().expect("Cannot read image IFD");
|
||
assert_eq!(decoder.dimensions().unwrap(), (30, 20));
|
||
assert_eq!(
|
||
decoder.colortype().unwrap(),
|
||
ColorType::Multiband {
|
||
bit_depth: 16,
|
||
num_samples: 2
|
||
}
|
||
);
|
||
|
||
let mut buffer = DecodingSampleBuffer::U16(Vec::new());
|
||
decoder.read_image_to_buffer(&mut buffer).unwrap();
|
||
let DecodingSampleBuffer::U16(data) = buffer else {
|
||
panic!("Wrong bit depth")
|
||
};
|
||
assert_eq!(data.len(), 2 * 20 * 30);
|
||
|
||
let mut expected = Vec::with_capacity(2 * 20 * 30);
|
||
for plane in 0..2u16 {
|
||
for y in 0..20u16 {
|
||
for x in 0..30u16 {
|
||
expected.push(1000 * plane + 10 * y + x);
|
||
}
|
||
}
|
||
}
|
||
assert_eq!(data, expected);
|
||
}
|
||
|
||
#[test]
|
||
fn test_inner_access() {
|
||
let path = PathBuf::from(TEST_IMAGE_DIR).join("tiled-rect-rgb-u8.tif");
|
||
let img_file = File::open(path).expect("Cannot find test image!");
|
||
let mut decoder = Decoder::open(img_file).expect("Cannot create decoder");
|
||
decoder.next_image().expect("Cannot read image IFD");
|
||
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8));
|
||
|
||
let mut ifd = decoder.current_ifd();
|
||
let c = ifd
|
||
.get_tag(tiff::tags::Tag::Compression)
|
||
.unwrap()
|
||
.into_u16()
|
||
.unwrap();
|
||
assert_eq!(c, tiff::tags::CompressionMethod::None.to_u16());
|
||
|
||
// Because the image is uncompressed, reading the first tile directly with the inner reader
|
||
// should yield the same result as reading it with the decoder's read_chunk method.
|
||
let first_offset = ifd
|
||
.get_tag_u32_vec(tiff::tags::Tag::TileOffsets)
|
||
.unwrap()
|
||
.into_iter()
|
||
.next()
|
||
.unwrap();
|
||
let first_byte_count = ifd
|
||
.get_tag_u32_vec(tiff::tags::Tag::TileByteCounts)
|
||
.unwrap()
|
||
.into_iter()
|
||
.next()
|
||
.unwrap();
|
||
decoder
|
||
.inner()
|
||
.seek(std::io::SeekFrom::Start(first_offset as u64))
|
||
.expect("Cannot seek to tile offset");
|
||
let mut buf = vec![0; first_byte_count as usize];
|
||
decoder
|
||
.inner()
|
||
.read_exact(&mut buf)
|
||
.expect("Cannot read tile data");
|
||
let raw_sum: u64 = buf.into_iter().map(<u64>::from).sum();
|
||
|
||
match decoder.read_chunk(0).unwrap() {
|
||
DecodingSampleBuffer::U8(chunk) => {
|
||
let sum: u64 = chunk.into_iter().map(<u64>::from).sum();
|
||
assert_eq!(sum, raw_sum);
|
||
}
|
||
_ => panic!("Wrong bit depth"),
|
||
}
|
||
}
|
||
|
||
/* #[test]
|
||
fn test_tiled_jpeg_rgb_u8() {
|
||
test_image_sum_u8("tiled-jpeg-rgb-u8.tif", ColorType::RGB(8), 93031606);
|
||
} */
|
||
|
||
#[test]
|
||
fn test_tiled_oversize_gray_i8() {
|
||
test_image_sum_i8("tiled-oversize-gray-i8.tif", ColorType::Gray(8), 1214996);
|
||
}
|
||
|
||
#[test]
|
||
fn test_tiled_cmyk_i8() {
|
||
test_image_sum_i8("tiled-cmyk-i8.tif", ColorType::CMYK(8), 1759101);
|
||
}
|
||
|
||
#[test]
|
||
fn test_tiled_incremental() {
|
||
let file = "tiled-rgb-u8.tif";
|
||
let expected_type = ColorType::RGB(8);
|
||
let sums = [
|
||
188760, 195639, 108148, 81986, 665088, 366140, 705317, 423366, 172033, 324455, 244102,
|
||
81853, 181258, 247971, 129486, 55600, 565625, 422102, 730888, 379271, 232142, 292549,
|
||
244045, 86866, 188141, 115036, 150785, 84389, 353170, 459325, 719619, 329594, 278663,
|
||
220474, 243048, 113563, 189152, 109684, 179391, 122188, 279651, 622093, 724682, 302459,
|
||
268428, 204499, 224255, 124674, 170668, 121868, 192768, 183367, 378029, 585651, 657712,
|
||
296790, 241444, 197083, 198429, 134869, 182318, 86034, 203655, 182338, 297255, 601284,
|
||
633813, 242531, 228578, 206441, 193552, 125412, 181527, 165439, 202531, 159538, 268388,
|
||
565790, 611382, 272967, 236497, 215154, 158881, 90806, 106114, 182342, 191824, 186138,
|
||
215174, 393193, 701228, 198866, 227944, 193830, 166330, 49008, 55719, 122820, 197316,
|
||
161969, 203152, 170986, 624427, 188605, 186187, 111064, 115192, 39538, 48626, 163929,
|
||
144682, 135796, 194141, 154198, 584125, 180255, 153524, 121433, 132641, 35743, 47798,
|
||
152343, 162874, 167664, 160175, 133038, 659882, 138339, 166470, 124173, 118929, 51317,
|
||
45267, 155776, 161331, 161006, 130052, 137618, 337291, 106481, 161999, 127343, 87724,
|
||
59540, 63907, 155677, 140668, 141523, 108061, 168657, 186482, 98599, 147614, 139963, 90444,
|
||
56602, 92547, 125644, 134212, 126569, 144153, 179800, 174516, 133969, 129399, 117681,
|
||
83305, 55075, 110737, 115108, 128572, 128911, 130922, 179986, 143288, 145884, 155856,
|
||
96683, 94057, 56238, 79649, 71651, 70182, 75010, 77009, 98855, 78979, 74341, 83482, 53403,
|
||
59842, 30305,
|
||
];
|
||
|
||
let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
|
||
let img_file = File::open(path).expect("Cannot find test image!");
|
||
let mut decoder = Decoder::open(img_file).expect("Cannot create decoder");
|
||
decoder.next_image().expect("Cannot read image IFD");
|
||
assert_eq!(decoder.colortype().unwrap(), expected_type);
|
||
|
||
let tiles = decoder.tile_count().unwrap();
|
||
assert_eq!(tiles as usize, sums.len());
|
||
|
||
for tile in 0..tiles {
|
||
match decoder.read_chunk(tile).unwrap() {
|
||
DecodingSampleBuffer::U8(res) => {
|
||
let sum: u64 = res.into_iter().map(<u64>::from).sum();
|
||
assert_eq!(sum, sums[tile as usize]);
|
||
}
|
||
_ => panic!("Wrong bit depth"),
|
||
}
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn test_planar_rgb_u8() {
|
||
test_image_sum_u8("planar-rgb-u8.tif", ColorType::RGB(8), 15417630);
|
||
}
|
||
|
||
#[test]
|
||
fn test_read_planar_bands() {
|
||
// gdal_translate tiled-rgb-u8.tif planar-rgb-u8.tif -co INTERLEAVE=BAND -co COMPRESS=LZW -co PROFILE=BASELINE
|
||
let file = "planar-rgb-u8.tif";
|
||
let expected_type = ColorType::RGB(8);
|
||
|
||
let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
|
||
let img_file = File::open(path).expect("Cannot find test image!");
|
||
let mut decoder = Decoder::open(img_file).expect("Cannot create decoder");
|
||
decoder.next_image().expect("Cannot read image IFD");
|
||
assert_eq!(decoder.colortype().unwrap(), expected_type);
|
||
|
||
let chunks = decoder.strip_count().unwrap();
|
||
assert_eq!(chunks as usize, 72);
|
||
|
||
// convert -quiet planar-rgb-u8.tif[0] -crop 1x1+0+0 txt:
|
||
// 0,0: (73,51,30) #49331E srgb(73,51,30)
|
||
|
||
// 1st band (red)
|
||
match decoder.read_chunk(0).unwrap() {
|
||
DecodingSampleBuffer::U8(chunk) => {
|
||
assert_eq!(chunk[0], 73);
|
||
}
|
||
_ => panic!("Wrong bit depth"),
|
||
}
|
||
// 2nd band (green)
|
||
match decoder.read_chunk(chunks / 3).unwrap() {
|
||
DecodingSampleBuffer::U8(chunk) => {
|
||
assert_eq!(chunk[0], 51);
|
||
}
|
||
_ => panic!("Wrong bit depth"),
|
||
}
|
||
// 3rd band (blue)
|
||
match decoder.read_chunk(chunks / 3 * 2).unwrap() {
|
||
DecodingSampleBuffer::U8(chunk) => {
|
||
assert_eq!(chunk[0], 30);
|
||
}
|
||
_ => panic!("Wrong bit depth"),
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn test_div_zero() {
|
||
use tiff::{TiffError, TiffFormatError};
|
||
|
||
let image = [
|
||
73, 73, 42, 0, 8, 0, 0, 0, 8, 0, 0, 1, 4, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 40, 1, 0, 0,
|
||
0, 158, 0, 0, 251, 3, 1, 3, 0, 1, 0, 0, 0, 1, 0, 0, 39, 6, 1, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0,
|
||
17, 1, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 3, 0, 1, 0, 0, 0, 158, 0, 0, 251, 67, 1, 3, 0,
|
||
1, 0, 0, 0, 40, 0, 0, 0, 66, 1, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 178, 178, 178, 178,
|
||
178, 178, 178,
|
||
];
|
||
|
||
let mut decoder = Decoder::open(std::io::Cursor::new(&image)).unwrap();
|
||
let err = decoder.next_image().unwrap_err();
|
||
|
||
match err {
|
||
TiffError::FormatError(TiffFormatError::StripTileTagConflict) => {}
|
||
unexpected => panic!("Unexpected error {unexpected}"),
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn test_too_many_value_bytes() {
|
||
let image = [
|
||
73, 73, 43, 0, 8, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 8, 0, 0, 0,
|
||
23, 0, 12, 0, 0, 65, 4, 0, 1, 6, 0, 0, 1, 16, 0, 1, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0,
|
||
0, 0, 3, 0, 1, 0, 0, 0, 1, 0, 0, 0, 59, 73, 84, 186, 202, 83, 240, 66, 1, 53, 22, 56, 47,
|
||
0, 0, 0, 0, 0, 0, 1, 222, 4, 0, 58, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 4, 0, 0, 100, 0,
|
||
0, 89, 89, 89, 89, 89, 89, 89, 89, 96, 1, 20, 89, 89, 89, 89, 18,
|
||
];
|
||
|
||
let mut decoder = Decoder::open(std::io::Cursor::new(&image)).unwrap();
|
||
let error = decoder.next_image().unwrap_err();
|
||
|
||
match error {
|
||
tiff::TiffError::LimitsExceeded => {}
|
||
unexpected => panic!("Unexpected error {unexpected}"),
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn fuzzer_testcase5() {
|
||
let image = [
|
||
73, 73, 42, 0, 8, 0, 0, 0, 8, 0, 0, 1, 4, 0, 1, 0, 0, 0, 100, 0, 0, 0, 1, 1, 4, 0, 1, 0, 0,
|
||
0, 158, 0, 0, 251, 3, 1, 3, 0, 1, 0, 0, 0, 1, 0, 0, 0, 6, 1, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0,
|
||
17, 1, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 1, 3, 0, 0, 0, 0, 0, 246, 16, 0, 0, 22, 1, 4, 0, 1,
|
||
0, 0, 0, 40, 0, 251, 255, 23, 1, 4, 0, 1, 0, 0, 0, 48, 178, 178, 178, 178, 178, 178, 178,
|
||
178, 178, 178,
|
||
];
|
||
|
||
let mut decoder = Decoder::open(std::io::Cursor::new(&image)).unwrap();
|
||
let _ = decoder.next_image().unwrap_err();
|
||
}
|
||
|
||
#[test]
|
||
fn fuzzer_testcase1() {
|
||
let image = [
|
||
73, 73, 42, 0, 8, 0, 0, 0, 8, 0, 0, 1, 4, 0, 1, 0, 0, 0, 99, 255, 255, 254, 1, 1, 4, 0, 1,
|
||
0, 0, 0, 158, 0, 0, 251, 3, 1, 3, 255, 254, 255, 255, 0, 1, 0, 0, 0, 6, 1, 3, 0, 1, 0, 0,
|
||
0, 0, 0, 0, 0, 17, 1, 4, 0, 9, 0, 0, 0, 0, 0, 0, 0, 2, 1, 3, 0, 2, 0, 0, 0, 63, 0, 0, 0,
|
||
22, 1, 4, 0, 1, 0, 0, 0, 44, 0, 0, 0, 23, 1, 4, 0, 0, 0, 0, 0, 0, 0, 2, 1, 3, 1, 0, 178,
|
||
178,
|
||
];
|
||
|
||
let mut decoder = Decoder::open(std::io::Cursor::new(&image)).unwrap();
|
||
let _ = decoder.next_image().unwrap_err();
|
||
}
|
||
|
||
#[test]
|
||
fn fuzzer_testcase6() {
|
||
let image = [
|
||
73, 73, 42, 0, 8, 0, 0, 0, 8, 0, 0, 1, 4, 0, 1, 0, 0, 0, 100, 0, 0, 148, 1, 1, 4, 0, 1, 0,
|
||
0, 0, 158, 0, 0, 251, 3, 1, 3, 255, 254, 255, 255, 0, 1, 0, 0, 0, 6, 1, 3, 0, 1, 0, 0, 0,
|
||
0, 0, 0, 0, 17, 1, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 1, 3, 0, 2, 0, 0, 0, 63, 0, 0, 0, 22,
|
||
1, 4, 0, 1, 0, 0, 0, 44, 0, 248, 255, 23, 1, 4, 0, 1, 0, 0, 0, 178, 178, 178, 0, 1, 178,
|
||
178, 178,
|
||
];
|
||
|
||
let mut decoder = Decoder::open(std::io::Cursor::new(&image)).unwrap();
|
||
let _ = decoder.next_image().unwrap_err();
|
||
}
|
||
|
||
#[test]
|
||
fn oom() {
|
||
let image = [
|
||
73, 73, 42, 0, 8, 0, 0, 0, 8, 0, 0, 1, 4, 0, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 40, 1, 0, 0,
|
||
0, 158, 0, 0, 251, 3, 1, 3, 0, 1, 0, 0, 0, 7, 0, 0, 0, 6, 1, 3, 0, 1, 0, 0, 0, 2, 0, 0, 0,
|
||
17, 1, 4, 0, 1, 0, 0, 0, 3, 77, 0, 0, 1, 1, 3, 0, 1, 0, 0, 0, 3, 128, 0, 0, 22, 1, 4, 0, 1,
|
||
0, 0, 0, 40, 0, 0, 0, 23, 1, 4, 0, 1, 0, 0, 0, 178, 48, 178, 178, 178, 178, 162, 178,
|
||
];
|
||
|
||
let mut decoder = Decoder::open(std::io::Cursor::new(&image)).unwrap();
|
||
let _ = decoder.next_image().unwrap_err();
|
||
}
|
||
|
||
#[test]
|
||
fn fuzzer_testcase4() {
|
||
let image = [
|
||
73, 73, 42, 0, 8, 0, 0, 0, 8, 0, 0, 1, 4, 0, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 40, 1, 0, 0,
|
||
0, 158, 0, 0, 251, 3, 1, 3, 0, 1, 0, 0, 0, 5, 0, 0, 0, 6, 1, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0,
|
||
17, 1, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 3, 0, 1, 0, 0, 0, 3, 128, 0, 0, 22, 1, 4, 0, 1,
|
||
0, 0, 0, 40, 0, 0, 0, 23, 1, 4, 0, 1, 0, 0, 0, 48, 178, 178, 178, 0, 1, 0, 13, 13,
|
||
];
|
||
|
||
let mut decoder = Decoder::open(std::io::Cursor::new(&image)).unwrap();
|
||
let _ = decoder.next_image().unwrap_err();
|
||
}
|
||
|
||
#[test]
|
||
fn fuzzer_testcase2() {
|
||
let image = [
|
||
73, 73, 42, 0, 8, 0, 0, 0, 15, 0, 0, 254, 44, 1, 0, 0, 0, 0, 0, 32, 0, 0, 0, 1, 4, 0, 1, 0,
|
||
0, 0, 0, 1, 0, 0, 91, 1, 1, 0, 0, 0, 0, 0, 242, 4, 0, 0, 0, 22, 0, 56, 77, 0, 77, 1, 0, 0,
|
||
73, 42, 0, 1, 4, 0, 1, 0, 0, 0, 4, 0, 8, 0, 0, 1, 4, 0, 1, 0, 0, 0, 158, 0, 0, 251, 3, 1,
|
||
3, 0, 1, 0, 0, 0, 7, 0, 0, 0, 6, 1, 3, 0, 1, 0, 0, 0, 2, 0, 0, 0, 17, 1, 4, 0, 1, 0, 0, 0,
|
||
0, 0, 0, 0, 1, 1, 3, 0, 1, 0, 0, 0, 0, 0, 0, 4, 61, 1, 18, 0, 1, 0, 0, 0, 202, 0, 0, 0, 17,
|
||
1, 100, 0, 129, 0, 0, 0, 0, 0, 0, 0, 232, 254, 252, 255, 254, 255, 255, 255, 1, 29, 0, 0,
|
||
22, 1, 3, 0, 1, 0, 0, 0, 16, 0, 0, 0, 23, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 123, 73, 254, 0,
|
||
73,
|
||
];
|
||
|
||
let mut decoder = Decoder::open(std::io::Cursor::new(&image)).unwrap();
|
||
let _ = decoder.next_image().unwrap_err();
|
||
}
|
||
|
||
#[test]
|
||
fn invalid_jpeg_tag_2() {
|
||
let image = [
|
||
73, 73, 42, 0, 8, 0, 0, 0, 16, 0, 254, 0, 4, 0, 1, 0, 0, 0, 0, 0, 0, 242, 0, 1, 4, 0, 1, 0,
|
||
0, 0, 0, 129, 16, 0, 1, 1, 4, 0, 1, 0, 0, 0, 214, 0, 0, 248, 253, 1, 3, 0, 1, 0, 0, 0, 64,
|
||
0, 0, 0, 3, 1, 3, 0, 1, 0, 0, 0, 7, 0, 0, 0, 6, 1, 3, 0, 1, 0, 0, 0, 1, 0, 0, 64, 14, 1, 0,
|
||
2, 0, 0, 148, 0, 206, 0, 0, 0, 17, 1, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
|
||
1, 0, 0, 0, 22, 1, 4, 0, 17, 0, 0, 201, 1, 0, 0, 0, 23, 1, 2, 0, 20, 0, 0, 0, 194, 0, 0, 0,
|
||
91, 1, 7, 0, 5, 0, 0, 0, 64, 0, 0, 0, 237, 254, 65, 255, 255, 255, 255, 255, 1, 0, 0, 0,
|
||
22, 1, 4, 0, 1, 0, 0, 0, 42, 0, 0, 0, 23, 1, 255, 255, 255, 255, 255, 36, 36, 0, 0, 0, 0,
|
||
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 36, 73, 73, 0, 42, 36, 36, 36, 36, 0, 0, 8, 0,
|
||
];
|
||
|
||
let mut decoder = Decoder::open(std::io::Cursor::new(&image)).unwrap();
|
||
let _ = decoder.next_image().unwrap_err();
|
||
}
|
||
|
||
#[test]
|
||
fn fuzzer_testcase3() {
|
||
let image = [
|
||
73, 73, 42, 0, 8, 0, 0, 0, 8, 0, 0, 1, 4, 0, 1, 0, 0, 0, 2, 0, 0, 0, 61, 1, 9, 0, 46, 22,
|
||
128, 0, 0, 0, 0, 1, 6, 1, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 17, 1, 4, 0, 27, 0, 0, 0, 0, 0, 0,
|
||
0, 1, 1, 3, 0, 1, 0, 0, 0, 17, 1, 0, 231, 22, 1, 1, 0, 1, 0, 0, 0, 130, 0, 0, 0, 23, 1, 4,
|
||
0, 14, 0, 0, 0, 0, 0, 0, 0, 133, 133, 133, 77, 77, 77, 0, 0, 22, 128, 0, 255, 255, 255,
|
||
255, 255,
|
||
];
|
||
|
||
let mut decoder = Decoder::open(std::io::Cursor::new(&image)).unwrap();
|
||
let _ = decoder.next_image().unwrap_err();
|
||
}
|
||
|
||
#[test]
|
||
fn timeout() {
|
||
use tiff::{TiffError, TiffFormatError};
|
||
|
||
let image = [
|
||
73, 73, 42, 0, 8, 0, 0, 0, 16, 0, 254, 0, 4, 0, 1, 68, 0, 0, 0, 2, 0, 32, 254, 252, 0, 109,
|
||
0, 129, 0, 0, 0, 32, 0, 58, 0, 1, 4, 0, 1, 0, 6, 0, 0, 0, 8, 0, 0, 1, 73, 73, 42, 0, 8, 0,
|
||
0, 0, 8, 0, 0, 1, 4, 0, 1, 0, 0, 0, 21, 0, 0, 0, 61, 1, 255, 128, 9, 0, 0, 8, 0, 1, 113, 2,
|
||
3, 1, 3, 0, 1, 0, 0, 0, 5, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 112, 0, 0, 36, 0, 0,
|
||
0, 112, 56, 200, 0, 5, 0, 0, 64, 0, 0, 1, 0, 4, 0, 0, 0, 2, 0, 6, 1, 3, 0, 1, 0, 0, 0, 0,
|
||
0, 0, 4, 17, 1, 1, 0, 93, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 3, 6, 0, 231, 22, 1,
|
||
1, 0, 1, 0, 0, 0, 2, 64, 118, 36, 23, 1, 1, 0, 43, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 4, 0, 8,
|
||
0, 0, 73, 73, 42, 0, 8, 0, 0, 0, 0, 0, 32,
|
||
];
|
||
|
||
let mut decoder = Decoder::open(std::io::Cursor::new(&image)).unwrap();
|
||
let error = decoder.next_image().unwrap_err();
|
||
|
||
match error {
|
||
TiffError::FormatError(TiffFormatError::CycleInOffsets) => {}
|
||
e => panic!("Unexpected error {e:?}"),
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn test_no_rows_per_strip() {
|
||
test_image_sum_u8("no_rows_per_strip.tiff", ColorType::RGB(8), 99448840);
|
||
}
|
||
|
||
#[test]
|
||
fn test_predictor_3_rgb_f32() {
|
||
test_image_sum_f32("predictor-3-rgb-f32.tif", ColorType::RGB(32), 54004.33);
|
||
}
|
||
|
||
#[test]
|
||
fn test_predictor_3_gray_f32() {
|
||
test_image_sum_f32("predictor-3-gray-f32.tif", ColorType::Gray(32), 20008.275);
|
||
}
|
||
|
||
#[test]
|
||
#[cfg(any(feature = "zstd", feature = "zstd-safe-rust"))]
|
||
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)],
|
||
// Represent all samples as little endian, ensuring check sums on all platforms.
|
||
normalize_byte_order: fn(&mut [u8]),
|
||
) {
|
||
let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
|
||
let img_file = File::open(path).expect("Cannot find test image!");
|
||
let mut decoder = Decoder::open(img_file).expect("Cannot create decoder");
|
||
decoder.next_image().unwrap();
|
||
|
||
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.len, 0u8);
|
||
decoder.read_image_bytes(&mut buffer).unwrap();
|
||
normalize_byte_order(&mut buffer);
|
||
|
||
// Well almost a crc..
|
||
assert_eq!(crc32fast::hash(&buffer), expected_crc);
|
||
}
|
||
}
|
||
|
||
fn byte_order_u16(bytes: &mut [u8]) {
|
||
for chunk in bytes.as_chunks_mut::<2>().0 {
|
||
let n = u16::from_ne_bytes(*chunk);
|
||
chunk.copy_from_slice(&n.to_le_bytes());
|
||
}
|
||
}
|
||
|
||
fn byte_order_u32(bytes: &mut [u8]) {
|
||
for chunk in bytes.as_chunks_mut::<4>().0 {
|
||
let n = u32::from_ne_bytes(*chunk);
|
||
chunk.copy_from_slice(&n.to_le_bytes());
|
||
}
|
||
}
|
||
|
||
fn byte_order_u64(bytes: &mut [u8]) {
|
||
for chunk in bytes.as_chunks_mut::<8>().0 {
|
||
let n = u64::from_ne_bytes(*chunk);
|
||
chunk.copy_from_slice(&n.to_le_bytes());
|
||
}
|
||
}
|
||
|
||
#[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)],
|
||
byte_order_u16,
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn bytes_gray_32b() {
|
||
test_image_bytes(
|
||
"gradient-1c-32b.tiff",
|
||
&[(ColorType::Gray(32), 2257036530)],
|
||
byte_order_u32,
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn bytes_gray_u64() {
|
||
test_image_bytes(
|
||
"gradient-1c-64b.tiff",
|
||
&[(ColorType::Gray(64), 2156547113)],
|
||
byte_order_u64,
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn bytes_gray_f32() {
|
||
test_image_bytes(
|
||
"gradient-1c-32b-float.tiff",
|
||
&[(ColorType::Gray(32), 3232356730)],
|
||
byte_order_u32,
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
#[cfg(feature = "fax")]
|
||
fn test_fax4() {
|
||
test_image_sum_u8("fax4.tiff", ColorType::Gray(1), 7802706);
|
||
}
|
||
|
||
#[test]
|
||
#[cfg(feature = "fax")]
|
||
fn test_fax4_white_is_min() {
|
||
test_image_sum_u8("imagemagick_group4.tiff", ColorType::Gray(1), 3742820);
|
||
}
|
||
|
||
#[test]
|
||
fn write_extra_bits_image() {
|
||
use tiff::{encoder, tags::ExtraSamples};
|
||
|
||
let path = PathBuf::from(TEST_IMAGE_DIR).join("extra_bits_rgb_8b.tiff");
|
||
let img_file = File::create(path).expect("Cannot find test image!");
|
||
|
||
let mut encoder = encoder::TiffEncoder::new(img_file).expect("Cannot create encoder");
|
||
let mut img = encoder.new_image::<encoder::colortype::RGB8>(8, 8).unwrap();
|
||
|
||
img.extra_samples(&[ExtraSamples::Unspecified]).unwrap();
|
||
|
||
// all pixels are supposed to be 0, the extra is 1
|
||
let data: [u8; 256] = core::array::from_fn(|i| (i as u8 % 4).saturating_sub(2));
|
||
|
||
img.write_data(&data)
|
||
.expect("correct amount of data to write");
|
||
}
|
||
|
||
#[test]
|
||
fn extra_bits_gray() {
|
||
test_image_sum_u8(
|
||
"extra_bits_gray_8b.tiff",
|
||
ColorType::Multiband {
|
||
bit_depth: 8,
|
||
num_samples: 2,
|
||
},
|
||
64,
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn extra_bits_rgb() {
|
||
test_image_sum_u8("extra_bits_rgb_8b.tiff", ColorType::RGBA(8), 64);
|
||
}
|
||
|
||
#[test]
|
||
#[cfg(feature = "fax")]
|
||
fn test_decode_huge_g4() {
|
||
let path = PathBuf::from(TEST_IMAGE_DIR).join("big_g4.tif");
|
||
let img_file = File::open(path).expect("failed to open large G4 test image");
|
||
let mut decoder = Decoder::open(img_file).expect("failed to create decoder");
|
||
|
||
decoder.next_image().expect("failed to read image IFD");
|
||
let (width, height) = decoder.dimensions().expect("failed to get dimensions");
|
||
|
||
assert!(
|
||
height > u16::MAX as u32,
|
||
"Test image height {} is not large enough to test u16 overflow",
|
||
height
|
||
);
|
||
|
||
let result = decoder
|
||
.read_image()
|
||
.expect("failed to decode huge G4 image");
|
||
|
||
if let tiff::decoder::DecodingSampleBuffer::U8(data) = result {
|
||
assert_eq!(data.len(), (width as usize * height as usize));
|
||
} else {
|
||
panic!("Unexpected decoding result type");
|
||
}
|
||
}
|
||
|
||
// ---- WhiteIsZero tests across bit depths ----
|
||
|
||
// WhiteIsZero + extended bit depth (12-bit gray, synthetic 4x2 image)
|
||
#[test]
|
||
fn test_gray_u12_white_is_zero() {
|
||
// Values: [0, 1000, 2000, 4095, 100, 500, 3000, 3500]
|
||
// After WhiteIsZero inversion: [4095, 3095, 2095, 0, 3995, 3595, 1095, 595]
|
||
test_image_sum_u16("miniswhite-1c-12b.tiff", ColorType::Gray(12), 18565);
|
||
}
|
||
|
||
// WhiteIsZero + non-standard sub-byte depths (3-bit and 6-bit, synthetic 8x2 images)
|
||
// These verify per-sample inversion for packed depths where samples cross byte boundaries.
|
||
#[test]
|
||
fn test_gray_u3_white_is_zero() {
|
||
test_image_sum_u8("miniswhite-1c-3b.tiff", ColorType::Gray(3), 765);
|
||
}
|
||
|
||
#[test]
|
||
fn test_gray_u6_white_is_zero() {
|
||
// Values: [0, 10, 20, 30, 40, 50, 60, 63] × 2 rows
|
||
// After inversion: [63, 53, 43, 33, 23, 13, 3, 0] × 2 rows
|
||
test_image_sum_u8("miniswhite-1c-6b.tiff", ColorType::Gray(6), 2124);
|
||
}
|
||
|
||
// ---- Non-standard sub-byte depths (BlackIsZero) ----
|
||
// Verify that odd bit depths (3, 5, 7) decode without error.
|
||
|
||
#[test]
|
||
fn test_gray_u3() {
|
||
// 8x2 image, values [0..7, 7..0], packed to 6 bytes
|
||
test_image_sum_u8("minisblack-1c-3b.tiff", ColorType::Gray(3), 765);
|
||
}
|
||
|
||
#[test]
|
||
fn test_gray_u5() {
|
||
// 8x2 image, packed to 10 bytes
|
||
test_image_sum_u8("minisblack-1c-5b.tiff", ColorType::Gray(5), 1275);
|
||
}
|
||
|
||
#[test]
|
||
fn test_gray_u7() {
|
||
// 8x2 image, packed to 14 bytes
|
||
test_image_sum_u8("minisblack-1c-7b.tiff", ColorType::Gray(7), 1711);
|
||
}
|
||
|
||
// ---- Horizontal predictor + extended bit depth ----
|
||
|
||
#[test]
|
||
fn test_gray_u12_hpredict() {
|
||
// Delta-encoded 12-bit values: [100, 200, 300, 400, 1000, 2000, 3000, 4000]
|
||
test_image_sum_u16("hpredict-1c-12b.tiff", ColorType::Gray(12), 11000);
|
||
}
|
||
|
||
// ---- Old-style LZW ----
|
||
|
||
#[test]
|
||
fn test_old_style_lzw() {
|
||
// Pre-TIFF-6.0 "old-style" LZW: LSB-first bit order with no early-change size
|
||
// switch. Auto-detected from the stream (first byte 0x00, second byte odd).
|
||
// Same image as libtiffpic's quad-lzw.tif; the decoded pixels must match.
|
||
test_image_sum_u8("quad-lzw-compat.tiff", ColorType::RGB(8), 21048924);
|
||
}
|
||
|
||
// ---- TransparencyMask ----
|
||
|
||
/// Build a minimal uncompressed 2x2 TIFF in memory, in the requested byte order,
|
||
/// with the given photometric interpretation and the pixel data as a single strip.
|
||
/// `sample_format` is emitted as a SampleFormat tag when `Some`; when `None` the
|
||
/// tag is omitted (the decoder then defaults to unsigned). Pixel bytes must already
|
||
/// be in `byte_order`.
|
||
fn build_raw_tiff(
|
||
byte_order: ByteOrder,
|
||
photometric: PhotometricInterpretation,
|
||
bits_per_sample: u16,
|
||
samples_per_pixel: u16,
|
||
sample_format: Option<SampleFormat>,
|
||
pixel_data: &[u8],
|
||
) -> Vec<u8> {
|
||
let le = matches!(byte_order, ByteOrder::LittleEndian);
|
||
let put16 = |buf: &mut Vec<u8>, v: u16| {
|
||
if le {
|
||
buf.extend_from_slice(&v.to_le_bytes());
|
||
} else {
|
||
buf.extend_from_slice(&v.to_be_bytes());
|
||
}
|
||
};
|
||
let put32 = |buf: &mut Vec<u8>, v: u32| {
|
||
if le {
|
||
buf.extend_from_slice(&v.to_le_bytes());
|
||
} else {
|
||
buf.extend_from_slice(&v.to_be_bytes());
|
||
}
|
||
};
|
||
|
||
let width: u16 = 2;
|
||
let height: u16 = 2;
|
||
let num_tags: u16 = if sample_format.is_some() { 9 } else { 8 };
|
||
let ifd_offset: u32 = 8;
|
||
let ifd_size = 2 + (num_tags as u32) * 12 + 4;
|
||
let strip_offset = 8 + ifd_size;
|
||
|
||
// IFD entries (must be ascending tag order). Tag IDs stay raw for the
|
||
// ascending-order property; the field type uses the `Type` enum.
|
||
let mut tags: Vec<(u16, Type, u32)> = vec![
|
||
(256, Type::SHORT, width as u32), // ImageWidth
|
||
(257, Type::SHORT, height as u32), // ImageLength
|
||
(258, Type::SHORT, bits_per_sample as u32), // BitsPerSample
|
||
(259, Type::SHORT, 1), // Compression = None
|
||
(262, Type::SHORT, photometric.to_u16() as u32), // PhotometricInterpretation
|
||
(273, Type::LONG, strip_offset), // StripOffsets
|
||
(277, Type::SHORT, samples_per_pixel as u32), // SamplesPerPixel
|
||
(279, Type::LONG, pixel_data.len() as u32), // StripByteCounts
|
||
];
|
||
if let Some(sf) = sample_format {
|
||
tags.push((339, Type::SHORT, sf.to_u16() as u32)); // SampleFormat (tag 339 > 279)
|
||
}
|
||
|
||
let mut buf = Vec::new();
|
||
buf.extend_from_slice(if le { b"II" } else { b"MM" });
|
||
put16(&mut buf, 42);
|
||
put32(&mut buf, ifd_offset);
|
||
put16(&mut buf, num_tags);
|
||
|
||
for (tag, typ, value) in tags {
|
||
put16(&mut buf, tag);
|
||
put16(&mut buf, typ.to_u16());
|
||
put32(&mut buf, 1); // count
|
||
// A SHORT value is left-justified in the 4-byte value field (value in the
|
||
// first two bytes, then padding); a LONG fills all four.
|
||
match typ {
|
||
Type::SHORT => {
|
||
put16(&mut buf, value as u16);
|
||
put16(&mut buf, 0);
|
||
}
|
||
_ => put32(&mut buf, value),
|
||
}
|
||
}
|
||
|
||
put32(&mut buf, 0); // next IFD = 0
|
||
buf.extend_from_slice(pixel_data);
|
||
buf
|
||
}
|
||
|
||
#[test]
|
||
fn test_transparency_mask() {
|
||
// 2x2 mask: values 0, 255, 128, 64
|
||
let data = build_raw_tiff(
|
||
ByteOrder::LittleEndian,
|
||
PhotometricInterpretation::TransparencyMask,
|
||
8,
|
||
1,
|
||
None,
|
||
&[0, 255, 128, 64],
|
||
);
|
||
let mut decoder = Decoder::open(Cursor::new(&data)).unwrap();
|
||
decoder.next_image().unwrap();
|
||
|
||
assert_eq!(decoder.colortype().unwrap(), ColorType::Gray(8));
|
||
|
||
let result = decoder.read_image().unwrap();
|
||
match result {
|
||
DecodingSampleBuffer::U8(pixels) => {
|
||
// TransparencyMask is decoded as grayscale
|
||
assert_eq!(pixels.len(), 4);
|
||
assert_eq!(pixels[0], 0);
|
||
assert_eq!(pixels[1], 255);
|
||
assert_eq!(pixels[2], 128);
|
||
assert_eq!(pixels[3], 64);
|
||
}
|
||
_ => panic!("Expected U8 buffer"),
|
||
}
|
||
}
|
||
|
||
// ---- CIE L*a*b* ----
|
||
|
||
#[test]
|
||
fn test_cielab_decode() {
|
||
// 2x2 CIELab image: 3 bytes per pixel (L, a, b)
|
||
// L=128, a=0 (signed neutral), b=0 (signed neutral)
|
||
// After re-biasing: a=128 (unsigned neutral), b=128 (unsigned neutral)
|
||
let pixels: Vec<u8> = vec![
|
||
128, 0, 0, // pixel 0: L=128, a=0, b=0 (neutral gray)
|
||
255, 50, 50, // pixel 1: L=255, a=+50, b=+50
|
||
100, 206, 206, // pixel 2: L=100, a=-50 (as i8), b=-50 (as i8)
|
||
0, 128, 128, // pixel 3: L=0, a=-128 (as i8), b=-128 (as i8)
|
||
];
|
||
|
||
let data = build_raw_tiff(
|
||
ByteOrder::LittleEndian,
|
||
PhotometricInterpretation::CIELab,
|
||
8,
|
||
3,
|
||
None,
|
||
&pixels,
|
||
);
|
||
let mut decoder = Decoder::open(Cursor::new(&data)).unwrap();
|
||
decoder.next_image().unwrap();
|
||
|
||
assert_eq!(decoder.colortype().unwrap(), ColorType::Lab(8));
|
||
|
||
let result = decoder.read_image().unwrap();
|
||
match result {
|
||
DecodingSampleBuffer::U8(decoded) => {
|
||
assert_eq!(decoded.len(), 12); // 4 pixels * 3 channels
|
||
|
||
// Pixel 0: L=128, a=0+128=128, b=0+128=128
|
||
assert_eq!(decoded[0], 128);
|
||
assert_eq!(decoded[1], 128);
|
||
assert_eq!(decoded[2], 128);
|
||
|
||
// Pixel 1: L=255, a=50+128=178, b=50+128=178
|
||
assert_eq!(decoded[3], 255);
|
||
assert_eq!(decoded[4], 178);
|
||
assert_eq!(decoded[5], 178);
|
||
|
||
// Pixel 2: L=100, a=206+128=78 (wrapping), b=206+128=78
|
||
assert_eq!(decoded[6], 100);
|
||
assert_eq!(decoded[7], 78);
|
||
assert_eq!(decoded[8], 78);
|
||
|
||
// Pixel 3: L=0, a=128+128=0 (wrapping), b=128+128=0
|
||
assert_eq!(decoded[9], 0);
|
||
assert_eq!(decoded[10], 0);
|
||
assert_eq!(decoded[11], 0);
|
||
}
|
||
_ => panic!("Expected U8 buffer"),
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn test_cielab_decode_16() {
|
||
// 2x2 16-bit CIELab image: 3 u16 samples per pixel (L, a, b), a/b signed.
|
||
// Re-biasing maps signed a/b to unsigned by adding 0x8000 (wrapping):
|
||
// 0 -> 32768, +1000 -> 33768, -1000 -> 31768, -32768 -> 0, +32767 -> 65535.
|
||
let samples: [u16; 12] = [
|
||
32768, 0, 0, // pixel 0: L, a=0, b=0 (neutral)
|
||
65535, 1000, 1000, // pixel 1: a=+1000, b=+1000
|
||
10000, 64536, 64536, // pixel 2: a=-1000, b=-1000 (as u16)
|
||
0, 32768, 32767, // pixel 3: a=-32768, b=+32767 (as u16)
|
||
];
|
||
let mut pixels = Vec::new();
|
||
for s in samples {
|
||
pixels.extend_from_slice(&s.to_le_bytes());
|
||
}
|
||
|
||
let data = build_raw_tiff(
|
||
ByteOrder::LittleEndian,
|
||
PhotometricInterpretation::CIELab,
|
||
16,
|
||
3,
|
||
None,
|
||
&pixels,
|
||
);
|
||
let mut decoder = Decoder::open(Cursor::new(&data)).unwrap();
|
||
decoder.next_image().unwrap();
|
||
|
||
assert_eq!(decoder.colortype().unwrap(), ColorType::Lab(16));
|
||
|
||
let result = decoder.read_image().unwrap();
|
||
match result {
|
||
DecodingSampleBuffer::U16(decoded) => {
|
||
assert_eq!(decoded.len(), 12); // 4 pixels * 3 channels
|
||
|
||
// L unchanged; a/b re-biased by +0x8000 (wrapping).
|
||
let expected: [u16; 12] = [
|
||
32768, 32768, 32768, // pixel 0
|
||
65535, 33768, 33768, // pixel 1
|
||
10000, 31768, 31768, // pixel 2
|
||
0, 0, 65535, // pixel 3
|
||
];
|
||
assert_eq!(decoded, expected);
|
||
}
|
||
_ => panic!("Expected U16 buffer"),
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn test_cielab_decode_16_big_endian() {
|
||
// Same image and expected output as `test_cielab_decode_16`, but stored as a
|
||
// big-endian ("MM") TIFF. 16-bit is the case where byte order matters: the
|
||
// decoder must swap to native before the a/b re-bias, so the result is
|
||
// identical to the little-endian case.
|
||
let samples: [u16; 12] = [
|
||
32768, 0, 0, // pixel 0
|
||
65535, 1000, 1000, // pixel 1
|
||
10000, 64536, 64536, // pixel 2
|
||
0, 32768, 32767, // pixel 3
|
||
];
|
||
let mut pixels = Vec::new();
|
||
for s in samples {
|
||
pixels.extend_from_slice(&s.to_be_bytes());
|
||
}
|
||
|
||
let data = build_raw_tiff(
|
||
ByteOrder::BigEndian,
|
||
PhotometricInterpretation::CIELab,
|
||
16,
|
||
3,
|
||
None,
|
||
&pixels,
|
||
);
|
||
let mut decoder = Decoder::open(Cursor::new(&data)).unwrap();
|
||
decoder.next_image().unwrap();
|
||
|
||
assert_eq!(decoder.colortype().unwrap(), ColorType::Lab(16));
|
||
|
||
let result = decoder.read_image().unwrap();
|
||
match result {
|
||
DecodingSampleBuffer::U16(decoded) => {
|
||
let expected: [u16; 12] = [
|
||
32768, 32768, 32768, // pixel 0
|
||
65535, 33768, 33768, // pixel 1
|
||
10000, 31768, 31768, // pixel 2
|
||
0, 0, 65535, // pixel 3
|
||
];
|
||
assert_eq!(decoded, expected);
|
||
}
|
||
_ => panic!("Expected U16 buffer"),
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn test_cielab_rejects_non_uint_sample_format() {
|
||
// CIELab treats a/b as signed via the photometric interpretation and requires
|
||
// SampleFormat::Uint (matching the ICCLab arm). A file declaring Int must be
|
||
// rejected rather than silently producing a signed output buffer.
|
||
let pixels = vec![0u8; 12]; // 2x2, 3 samples, 8-bit
|
||
let data = build_raw_tiff(
|
||
ByteOrder::LittleEndian,
|
||
PhotometricInterpretation::CIELab,
|
||
8,
|
||
3,
|
||
Some(SampleFormat::Int),
|
||
&pixels,
|
||
);
|
||
|
||
let mut decoder = Decoder::open(Cursor::new(&data)).unwrap();
|
||
// Rejection may surface at either next_image() or colortype().
|
||
let rejected = decoder.next_image().is_err() || decoder.colortype().is_err();
|
||
assert!(
|
||
rejected,
|
||
"CIELab with SampleFormat=Int must be rejected, not decoded"
|
||
);
|
||
}
|