Deduplicate decoding tests

This commit is contained in:
Andrew Champion committed 2019-12-21 15:11:47 +00:00
1 parent 57783ba43f
commit 3f14098060
1 file changed
+59 -147
+59 -147
View File
@@ -4,201 +4,113 @@ use tiff::decoder::{ifd, Decoder, DecodingResult};
use tiff::ColorType;
use std::fs::File;
use std::path::PathBuf;
const TEST_IMAGE_DIR: &str = "./tests/images/";
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::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), expected_type);
let img_res = decoder.read_image().unwrap();
match img_res {
DecodingResult::$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_u16, U16, u64);
test_image_sum!(test_image_sum_u32, U32, u64);
test_image_sum!(test_image_sum_u64, U64, u64);
/// 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.
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::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), expected_type);
assert!(match decoder.read_image() {
Err(tiff::TiffError::UnsupportedError(tiff::TiffUnsupportedError::UnsupportedColorType(_))) => true,
_ => false,
});
}
#[test]
fn test_gray_u8() {
let img_file =
File::open("./tests/images/minisblack-1c-8b.tiff").expect("Cannot find test image!");
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), ColorType::Gray(8));
let img_res = decoder.read_image().unwrap();
if let DecodingResult::U8(res) = img_res {
let mut res_sum: u64 = 0;
for x in res {
res_sum += x as u64;
}
assert_eq!(res_sum, 2840893);
}
else {
panic!("Wrong bit depth")
}
}
#[test]
fn test_rgb_u8() {
let img_file = File::open("./tests/images/rgb-3c-8b.tiff").expect("Cannot find test image!");
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8));
let img_res = decoder.read_image().unwrap();
if let DecodingResult::U8(res) = img_res {
let mut res_sum: u64 = 0;
for x in res {
res_sum += x as u64;
}
assert_eq!(res_sum, 7842108)
}
else {
panic!("Wrong bit depth")
}
test_image_sum_u8("minisblack-1c-8b.tiff", ColorType::Gray(8), 2840893);
}
#[test]
fn test_gray_u12() {
let img_file =
File::open("./tests/images/12bit.cropped.tiff").expect("Cannot find test image!");
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), ColorType::Gray(12));
assert!(match decoder.read_image() {
Err(tiff::TiffError::UnsupportedError(tiff::TiffUnsupportedError::UnsupportedColorType(_))) => true,
_ => false,
});
test_image_color_type_unsupported("12bit.cropped.tiff", ColorType::Gray(12));
}
#[test]
fn test_gray_u16() {
let img_file =
File::open("./tests/images/minisblack-1c-16b.tiff").expect("Cannot find test image!");
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), ColorType::Gray(16));
let img_res = decoder.read_image().unwrap();
if let DecodingResult::U16(res) = img_res {
let mut res_sum: u64 = 0;
for x in res {
res_sum += x as u64;
}
assert_eq!(res_sum, 733126239);
}
else {
panic!("Wrong bit depth")
}
test_image_sum_u16("minisblack-1c-16b.tiff", ColorType::Gray(16), 733126239);
}
#[test]
fn test_gray_u32() {
let img_file =
File::open("./tests/images/gradient-1c-32b.tiff").expect("Cannot find test image!");
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), ColorType::Gray(32));
let img_res = decoder.read_image().unwrap();
if let DecodingResult::U32(res) = img_res {
let mut res_sum: u64 = 0;
for x in res {
res_sum += x as u64;
}
assert_eq!(res_sum, 549892913787);
}
else {
panic!("Wrong bit depth")
}
test_image_sum_u32("gradient-1c-32b.tiff", ColorType::Gray(32), 549892913787);
}
#[test]
fn test_gray_u64() {
let img_file =
File::open("./tests/images/gradient-1c-64b.tiff").expect("Cannot find test image!");
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), ColorType::Gray(64));
let img_res = decoder.read_image().unwrap();
if let DecodingResult::U64(res) = img_res {
let mut res_sum: u64 = 0;
for x in res {
res_sum += x as u64;
test_image_sum_u64("gradient-1c-64b.tiff", ColorType::Gray(64), 549892913787);
}
assert_eq!(res_sum, 549892913787);
}
else {
panic!("Wrong bit depth")
}
#[test]
fn test_rgb_u8() {
test_image_sum_u8("rgb-3c-8b.tiff", ColorType::RGB(8), 7842108);
}
#[test]
fn test_rgb_u12() {
let img_file =
File::open("./tests/images/12bit.cropped.rgb.tiff").expect("Cannot find test image!");
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(12));
assert!(match decoder.read_image() {
Err(tiff::TiffError::UnsupportedError(tiff::TiffUnsupportedError::UnsupportedColorType(_))) => true,
_ => false,
});
test_image_color_type_unsupported("12bit.cropped.rgb.tiff", ColorType::RGB(12));
}
#[test]
fn test_rgb_u16() {
let img_file = File::open("./tests/images/rgb-3c-16b.tiff").expect("Cannot find test image!");
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(16));
let img_res = decoder.read_image().unwrap();
if let DecodingResult::U16(res) = img_res {
let mut res_sum: u64 = 0;
for x in res {
res_sum += x as u64;
}
assert_eq!(res_sum, 2024349944);
}
else {
panic!("Wrong bit depth")
}
test_image_sum_u16("rgb-3c-16b.tiff", ColorType::RGB(16), 2024349944);
}
#[test]
fn test_rgb_u32() {
let img_file = File::open("./tests/images/gradient-3c-32b.tiff").expect("Cannot find test image!");
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(32));
let img_res = decoder.read_image().unwrap();
if let DecodingResult::U32(res) = img_res {
let mut res_sum: u64 = 0;
for x in res {
res_sum += x as u64;
}
assert_eq!(res_sum, 2030834111716);
}
else {
panic!("Wrong bit depth")
}
test_image_sum_u32("gradient-3c-32b.tiff", ColorType::RGB(32), 2030834111716);
}
#[test]
fn test_rgb_u64() {
let img_file = File::open("./tests/images/gradient-3c-64b.tiff").expect("Cannot find test image!");
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(64));
let img_res = decoder.read_image().unwrap();
if let DecodingResult::U64(res) = img_res {
let mut res_sum: u64 = 0;
for x in res {
res_sum += x as u64;
}
assert_eq!(res_sum, 2030834111716);
}
else {
panic!("Wrong bit depth")
}
test_image_sum_u64("gradient-3c-64b.tiff", ColorType::RGB(64), 2030834111716);
}
#[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 = vec!["minisblack-1c-16b.tiff", "rgb-3c-16b.tiff"];
let filenames = ["minisblack-1c-16b.tiff", "rgb-3c-16b.tiff"];
for filename in filenames.iter() {
let path = format!("./tests/images/{}", filename);
let img_file = File::open(path).expect("can't open file");
let path = PathBuf::from(TEST_IMAGE_DIR).join(filename);
let img_file = File::open(path).expect("Cannot find test image!");
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
let software = decoder.get_tag(ifd::Tag::Software).unwrap();
match software {
ifd::Value::Ascii(s) => assert_eq!(
s,
String::from("GraphicsMagick 1.2 unreleased Q16 http://www.GraphicsMagick.org/")
&s,
"GraphicsMagick 1.2 unreleased Q16 http://www.GraphicsMagick.org/"
),
_ => assert!(false),
};