Deduplicate decoding tests
This commit is contained in:
1 parent
57783ba43f
commit
3f14098060
1 file changed
+57
-145
+57
-145
@@ -4,201 +4,113 @@ use tiff::decoder::{ifd, Decoder, DecodingResult};
|
|||||||
use tiff::ColorType;
|
use tiff::ColorType;
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[test]
|
const TEST_IMAGE_DIR: &str = "./tests/images/";
|
||||||
fn test_gray_u8() {
|
|
||||||
let img_file =
|
macro_rules! test_image_sum {
|
||||||
File::open("./tests/images/minisblack-1c-8b.tiff").expect("Cannot find test image!");
|
($name:ident, $buffer:ident, $sum_ty:ty) => {
|
||||||
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
|
fn $name(file: &str, expected_type: ColorType, expected_sum: $sum_ty) {
|
||||||
assert_eq!(decoder.colortype().unwrap(), ColorType::Gray(8));
|
let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
|
||||||
let img_res = decoder.read_image().unwrap();
|
let img_file = File::open(path).expect("Cannot find test image!");
|
||||||
if let DecodingResult::U8(res) = img_res {
|
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
|
||||||
let mut res_sum: u64 = 0;
|
assert_eq!(decoder.colortype().unwrap(), expected_type);
|
||||||
for x in res {
|
let img_res = decoder.read_image().unwrap();
|
||||||
res_sum += x as u64;
|
|
||||||
|
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"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
assert_eq!(res_sum, 2840893);
|
test_image_sum!(test_image_sum_u8, U8, u64);
|
||||||
}
|
test_image_sum!(test_image_sum_u16, U16, u64);
|
||||||
else {
|
test_image_sum!(test_image_sum_u32, U32, u64);
|
||||||
panic!("Wrong bit depth")
|
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]
|
#[test]
|
||||||
fn test_rgb_u8() {
|
fn test_gray_u8() {
|
||||||
let img_file = File::open("./tests/images/rgb-3c-8b.tiff").expect("Cannot find test image!");
|
test_image_sum_u8("minisblack-1c-8b.tiff", ColorType::Gray(8), 2840893);
|
||||||
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]
|
#[test]
|
||||||
fn test_gray_u12() {
|
fn test_gray_u12() {
|
||||||
let img_file =
|
test_image_color_type_unsupported("12bit.cropped.tiff", ColorType::Gray(12));
|
||||||
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]
|
#[test]
|
||||||
fn test_gray_u16() {
|
fn test_gray_u16() {
|
||||||
let img_file =
|
test_image_sum_u16("minisblack-1c-16b.tiff", ColorType::Gray(16), 733126239);
|
||||||
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]
|
#[test]
|
||||||
fn test_gray_u32() {
|
fn test_gray_u32() {
|
||||||
let img_file =
|
test_image_sum_u32("gradient-1c-32b.tiff", ColorType::Gray(32), 549892913787);
|
||||||
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]
|
#[test]
|
||||||
fn test_gray_u64() {
|
fn test_gray_u64() {
|
||||||
let img_file =
|
test_image_sum_u64("gradient-1c-64b.tiff", ColorType::Gray(64), 549892913787);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(res_sum, 549892913787);
|
#[test]
|
||||||
}
|
fn test_rgb_u8() {
|
||||||
else {
|
test_image_sum_u8("rgb-3c-8b.tiff", ColorType::RGB(8), 7842108);
|
||||||
panic!("Wrong bit depth")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rgb_u12() {
|
fn test_rgb_u12() {
|
||||||
let img_file =
|
test_image_color_type_unsupported("12bit.cropped.rgb.tiff", ColorType::RGB(12));
|
||||||
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]
|
#[test]
|
||||||
fn test_rgb_u16() {
|
fn test_rgb_u16() {
|
||||||
let img_file = File::open("./tests/images/rgb-3c-16b.tiff").expect("Cannot find test image!");
|
test_image_sum_u16("rgb-3c-16b.tiff", ColorType::RGB(16), 2024349944);
|
||||||
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]
|
#[test]
|
||||||
fn test_rgb_u32() {
|
fn test_rgb_u32() {
|
||||||
let img_file = File::open("./tests/images/gradient-3c-32b.tiff").expect("Cannot find test image!");
|
test_image_sum_u32("gradient-3c-32b.tiff", ColorType::RGB(32), 2030834111716);
|
||||||
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]
|
#[test]
|
||||||
fn test_rgb_u64() {
|
fn test_rgb_u64() {
|
||||||
let img_file = File::open("./tests/images/gradient-3c-64b.tiff").expect("Cannot find test image!");
|
test_image_sum_u64("gradient-3c-64b.tiff", ColorType::RGB(64), 2030834111716);
|
||||||
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]
|
#[test]
|
||||||
fn test_string_tags() {
|
fn test_string_tags() {
|
||||||
// these files have null-terminated strings for their Software tag. One has extra bytes after
|
// 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
|
// 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() {
|
for filename in filenames.iter() {
|
||||||
let path = format!("./tests/images/{}", filename);
|
let path = PathBuf::from(TEST_IMAGE_DIR).join(filename);
|
||||||
let img_file = File::open(path).expect("can't open 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 decoder = Decoder::new(img_file).expect("Cannot create decoder");
|
||||||
let software = decoder.get_tag(ifd::Tag::Software).unwrap();
|
let software = decoder.get_tag(ifd::Tag::Software).unwrap();
|
||||||
match software {
|
match software {
|
||||||
ifd::Value::Ascii(s) => assert_eq!(
|
ifd::Value::Ascii(s) => assert_eq!(
|
||||||
s,
|
&s,
|
||||||
String::from("GraphicsMagick 1.2 unreleased Q16 http://www.GraphicsMagick.org/")
|
"GraphicsMagick 1.2 unreleased Q16 http://www.GraphicsMagick.org/"
|
||||||
),
|
),
|
||||||
_ => assert!(false),
|
_ => assert!(false),
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user