diff options
Diffstat (limited to 'vendor/tiff/tests')
-rw-r--r-- | vendor/tiff/tests/benches/README.md | 4 | ||||
-rw-r--r-- | vendor/tiff/tests/benches/Transparency-lzw.tif | bin | 0 -> 6158 bytes | |||
-rw-r--r-- | vendor/tiff/tests/benches/kodim02-lzw.tif | bin | 0 -> 726638 bytes | |||
-rw-r--r-- | vendor/tiff/tests/benches/kodim07-lzw.tif | bin | 0 -> 643034 bytes | |||
-rw-r--r-- | vendor/tiff/tests/decode_bigtiff_images.rs | 46 | ||||
-rw-r--r-- | vendor/tiff/tests/decode_images.rs | 479 | ||||
-rw-r--r-- | vendor/tiff/tests/decodedata-rgb-3c-8b.tiff | bin | 0 -> 30180 bytes | |||
-rw-r--r-- | vendor/tiff/tests/encode_images.rs | 529 | ||||
-rw-r--r-- | vendor/tiff/tests/encode_images_with_compression.rs | 157 | ||||
-rw-r--r-- | vendor/tiff/tests/fuzz_tests.rs | 51 |
10 files changed, 1266 insertions, 0 deletions
diff --git a/vendor/tiff/tests/benches/README.md b/vendor/tiff/tests/benches/README.md new file mode 100644 index 0000000..616292c --- /dev/null +++ b/vendor/tiff/tests/benches/README.md @@ -0,0 +1,4 @@ +Copyrights: + +kodim*.png: Eastman Kodak Company, released for unrestricted use +Transparency.png: Public Domain, according to Wikimedia diff --git a/vendor/tiff/tests/benches/Transparency-lzw.tif b/vendor/tiff/tests/benches/Transparency-lzw.tif Binary files differnew file mode 100644 index 0000000..a7a26a9 --- /dev/null +++ b/vendor/tiff/tests/benches/Transparency-lzw.tif diff --git a/vendor/tiff/tests/benches/kodim02-lzw.tif b/vendor/tiff/tests/benches/kodim02-lzw.tif Binary files differnew file mode 100644 index 0000000..a1aabfb --- /dev/null +++ b/vendor/tiff/tests/benches/kodim02-lzw.tif diff --git a/vendor/tiff/tests/benches/kodim07-lzw.tif b/vendor/tiff/tests/benches/kodim07-lzw.tif Binary files differnew file mode 100644 index 0000000..f8d543d --- /dev/null +++ b/vendor/tiff/tests/benches/kodim07-lzw.tif diff --git a/vendor/tiff/tests/decode_bigtiff_images.rs b/vendor/tiff/tests/decode_bigtiff_images.rs new file mode 100644 index 0000000..9113f42 --- /dev/null +++ b/vendor/tiff/tests/decode_bigtiff_images.rs @@ -0,0 +1,46 @@ +extern crate tiff; + +use tiff::decoder::Decoder; +use tiff::tags::Tag; +use tiff::ColorType; + +use std::fs::File; +use std::path::PathBuf; + +const TEST_IMAGE_DIR: &str = "./tests/images/bigtiff"; + +#[test] +fn test_big_tiff() { + let filenames = ["BigTIFF.tif", "BigTIFFMotorola.tif", "BigTIFFLong.tif"]; + 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::new(img_file).expect("Cannot create decoder"); + assert_eq!( + decoder.dimensions().expect("Cannot get dimensions"), + (64, 64) + ); + assert_eq!( + decoder.colortype().expect("Cannot get colortype"), + ColorType::RGB(8) + ); + assert_eq!( + decoder + .get_tag_u64(Tag::StripOffsets) + .expect("Cannot get StripOffsets"), + 16 + ); + assert_eq!( + decoder + .get_tag_u64(Tag::RowsPerStrip) + .expect("Cannot get RowsPerStrip"), + 64 + ); + assert_eq!( + decoder + .get_tag_u64(Tag::StripByteCounts) + .expect("Cannot get StripByteCounts"), + 12288 + ) + } +} diff --git a/vendor/tiff/tests/decode_images.rs b/vendor/tiff/tests/decode_images.rs new file mode 100644 index 0000000..9d5b809 --- /dev/null +++ b/vendor/tiff/tests/decode_images.rs @@ -0,0 +1,479 @@ +extern crate tiff; + +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_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); + +/// 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_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_gray_u8() { + test_image_sum_u8("minisblack-1c-8b.tiff", ColorType::Gray(8), 2840893); +} + +#[test] +fn test_gray_u12() { + test_image_color_type_unsupported("12bit.cropped.tiff", ColorType::Gray(12)); +} + +#[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_color_type_unsupported("12bit.cropped.rgb.tiff", ColorType::RGB(12)); +} + +#[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::new(img_file).expect("Cannot create decoder"); + let software = decoder.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/" + ), + _ => assert!(false), + }; + } +} + +#[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::new(file).unwrap(); + 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() { + 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_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_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::new(img_file).expect("Cannot create decoder"); + 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() { + DecodingResult::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_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 err = tiff::decoder::Decoder::new(std::io::Cursor::new(&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 error = tiff::decoder::Decoder::new(std::io::Cursor::new(&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 = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap(); + + let _ = decoder.read_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 _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err(); +} + +#[test] +fn test_stripped_image_overflow() { + 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 = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap(); + + let err = decoder.read_image().unwrap_err(); + + match err { + tiff::TiffError::LimitsExceeded => {} + unexpected => panic!("Unexpected error {}", unexpected), + } +} + +#[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 _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&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 _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&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 _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&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 _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&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 _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&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 error = tiff::decoder::Decoder::new(std::io::Cursor::new(&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); +} diff --git a/vendor/tiff/tests/decodedata-rgb-3c-8b.tiff b/vendor/tiff/tests/decodedata-rgb-3c-8b.tiff Binary files differnew file mode 100644 index 0000000..ad80292 --- /dev/null +++ b/vendor/tiff/tests/decodedata-rgb-3c-8b.tiff diff --git a/vendor/tiff/tests/encode_images.rs b/vendor/tiff/tests/encode_images.rs new file mode 100644 index 0000000..57c569f --- /dev/null +++ b/vendor/tiff/tests/encode_images.rs @@ -0,0 +1,529 @@ +extern crate tiff; + +use tiff::decoder::{ifd, Decoder, DecodingResult}; +use tiff::encoder::{colortype, Ifd, Ifd8, SRational, TiffEncoder}; +use tiff::tags::Tag; +use tiff::ColorType; + +use std::fs::File; +use std::io::{Cursor, Seek, SeekFrom}; +use std::path::PathBuf; + +#[test] +fn encode_decode() { + 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 mut file = Cursor::new(Vec::new()); + { + let mut tiff = TiffEncoder::new(&mut file).unwrap(); + + let mut image = tiff.new_image::<colortype::RGB8>(100, 100).unwrap(); + image + .encoder() + .write_tag(Tag::Artist, "Image-tiff") + .unwrap(); + image.write_data(&image_data).unwrap(); + } + { + file.seek(SeekFrom::Start(0)).unwrap(); + let mut decoder = Decoder::new(&mut file).unwrap(); + assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8)); + assert_eq!(decoder.dimensions().unwrap(), (100, 100)); + assert_eq!( + decoder.get_tag(Tag::Artist).unwrap(), + ifd::Value::Ascii("Image-tiff".into()) + ); + if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() { + assert_eq!(image_data, img_res); + } else { + panic!("Wrong data type"); + } + } +} + +#[test] +fn encode_decode_big() { + 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 mut file = Cursor::new(Vec::new()); + { + let mut tiff = TiffEncoder::new_big(&mut file).unwrap(); + + let mut image = tiff.new_image::<colortype::RGB8>(100, 100).unwrap(); + image + .encoder() + .write_tag(Tag::Artist, "Image-tiff") + .unwrap(); + image.write_data(&image_data).unwrap(); + } + { + file.seek(SeekFrom::Start(0)).unwrap(); + let mut decoder = Decoder::new(&mut file).unwrap(); + assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8)); + assert_eq!(decoder.dimensions().unwrap(), (100, 100)); + assert_eq!( + decoder.get_tag(Tag::Artist).unwrap(), + ifd::Value::Ascii("Image-tiff".into()) + ); + if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() { + assert_eq!(image_data, img_res); + } else { + panic!("Wrong data type"); + } + } +} + +#[test] +fn test_encode_ifd() { + let mut data = Cursor::new(Vec::new()); + + { + let mut tiff = TiffEncoder::new(&mut data).unwrap(); + let mut image_encoder = tiff.new_image::<colortype::Gray8>(1, 1).unwrap(); + image_encoder.write_strip(&[1]).unwrap(); + let encoder = image_encoder.encoder(); + + // Use the "reusable" tags section as per the TIFF6 spec + encoder.write_tag(Tag::Unknown(65000), Ifd(42u32)).unwrap(); + encoder + .write_tag(Tag::Unknown(65001), &[Ifd(100u32)][..]) + .unwrap(); + encoder + .write_tag(Tag::Unknown(65002), &[Ifd(1u32), Ifd(2u32), Ifd(3u32)][..]) + .unwrap(); + + encoder.write_tag(Tag::Unknown(65010), Ifd8(43u64)).unwrap(); + encoder + .write_tag(Tag::Unknown(65011), &[Ifd8(100u64)][..]) + .unwrap(); + encoder + .write_tag( + Tag::Unknown(65012), + &[Ifd8(1u64), Ifd8(2u64), Ifd8(3u64)][..], + ) + .unwrap(); + } + + // Rewind the cursor for reading + data.set_position(0); + { + let mut decoder = Decoder::new(&mut data).unwrap(); + + assert_eq!(decoder.assert_tag_u32(65000), 42); + assert_eq!(decoder.assert_tag_u32_vec(65000), [42]); + assert_eq!(decoder.assert_tag_u32_vec(65001), [100]); + assert_eq!(decoder.assert_tag_u32_vec(65002), [1, 2, 3]); + + assert_eq!(decoder.assert_tag_u64(65010), 43); + assert_eq!(decoder.assert_tag_u64_vec(65010), [43]); + assert_eq!(decoder.assert_tag_u64_vec(65011), [100]); + assert_eq!(decoder.assert_tag_u64_vec(65012), [1, 2, 3]); + } +} + +#[test] +/// Test that attempting to encode when the input buffer is undersized returns +/// an error rather than panicking. +/// See: https://github.com/PistonDevelopers/image-tiff/issues/35 +fn test_encode_undersized_buffer() { + let input_data = vec![1, 2, 3]; + let output = Vec::new(); + let mut output_stream = Cursor::new(output); + if let Ok(mut tiff) = TiffEncoder::new(&mut output_stream) { + let res = tiff.write_image::<colortype::RGB8>(50, 50, &input_data); + assert!(res.is_err()); + } +} + +const TEST_IMAGE_DIR: &str = "./tests/images/"; + +macro_rules! test_roundtrip { + ($name:ident, $buffer:ident, $buffer_ty:ty) => { + fn $name<C: colortype::ColorType<Inner = $buffer_ty>>( + 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); + + let image_data = match decoder.read_image().unwrap() { + DecodingResult::$buffer(res) => res, + _ => panic!("Wrong data type"), + }; + + let mut file = Cursor::new(Vec::new()); + { + let mut tiff = TiffEncoder::new(&mut file).unwrap(); + + let (width, height) = decoder.dimensions().unwrap(); + tiff.write_image::<C>(width, height, &image_data).unwrap(); + } + file.seek(SeekFrom::Start(0)).unwrap(); + { + let mut decoder = Decoder::new(&mut file).unwrap(); + if let DecodingResult::$buffer(img_res) = decoder.read_image().unwrap() { + assert_eq!(image_data, img_res); + } else { + panic!("Wrong data type"); + } + } + } + }; +} + +test_roundtrip!(test_u8_roundtrip, U8, u8); +test_roundtrip!(test_u16_roundtrip, U16, u16); +test_roundtrip!(test_u32_roundtrip, U32, u32); +test_roundtrip!(test_u64_roundtrip, U64, u64); +test_roundtrip!(test_f32_roundtrip, F32, f32); +test_roundtrip!(test_f64_roundtrip, F64, f64); + +#[test] +fn test_gray_u8_roundtrip() { + test_u8_roundtrip::<colortype::Gray8>("minisblack-1c-8b.tiff", ColorType::Gray(8)); +} + +#[test] +fn test_rgb_u8_roundtrip() { + test_u8_roundtrip::<colortype::RGB8>("rgb-3c-8b.tiff", ColorType::RGB(8)); +} + +#[test] +fn test_cmyk_u8_roundtrip() { + test_u8_roundtrip::<colortype::CMYK8>("cmyk-3c-8b.tiff", ColorType::CMYK(8)); +} + +#[test] +fn test_gray_u16_roundtrip() { + test_u16_roundtrip::<colortype::Gray16>("minisblack-1c-16b.tiff", ColorType::Gray(16)); +} + +#[test] +fn test_rgb_u16_roundtrip() { + test_u16_roundtrip::<colortype::RGB16>("rgb-3c-16b.tiff", ColorType::RGB(16)); +} + +#[test] +fn test_cmyk_u16_roundtrip() { + test_u16_roundtrip::<colortype::CMYK16>("cmyk-3c-16b.tiff", ColorType::CMYK(16)); +} + +#[test] +fn test_gray_u32_roundtrip() { + test_u32_roundtrip::<colortype::Gray32>("gradient-1c-32b.tiff", ColorType::Gray(32)); +} + +#[test] +fn test_rgb_u32_roundtrip() { + test_u32_roundtrip::<colortype::RGB32>("gradient-3c-32b.tiff", ColorType::RGB(32)); +} + +#[test] +fn test_gray_u64_roundtrip() { + test_u64_roundtrip::<colortype::Gray64>("gradient-1c-64b.tiff", ColorType::Gray(64)); +} + +#[test] +fn test_rgb_u64_roundtrip() { + test_u64_roundtrip::<colortype::RGB64>("gradient-3c-64b.tiff", ColorType::RGB(64)); +} + +#[test] +fn test_gray_f32_roundtrip() { + test_f32_roundtrip::<colortype::Gray32Float>("gradient-1c-32b-float.tiff", ColorType::Gray(32)); +} + +#[test] +fn test_rgb_f32_roundtrip() { + test_f32_roundtrip::<colortype::RGB32Float>("gradient-3c-32b-float.tiff", ColorType::RGB(32)); +} + +#[test] +fn test_cmyk_f32_roundtrip() { + test_f32_roundtrip::<colortype::CMYK32Float>("cmyk-3c-32b-float.tiff", ColorType::CMYK(32)); +} + +#[test] +fn test_gray_f64_roundtrip() { + test_f64_roundtrip::<colortype::Gray64Float>("gradient-1c-64b-float.tiff", ColorType::Gray(64)); +} + +#[test] +fn test_ycbcr_u8_roundtrip() { + test_u8_roundtrip::<colortype::YCbCr8>("tiled-jpeg-ycbcr.tif", ColorType::YCbCr(8)); +} + +trait AssertDecode { + fn assert_tag_u32(&mut self, tag: u16) -> u32; + fn assert_tag_u32_vec(&mut self, tag: u16) -> Vec<u32>; + fn assert_tag_i32(&mut self, tag: u16) -> i32; + fn assert_tag_i32_vec(&mut self, tag: u16) -> Vec<i32>; + fn assert_tag_u64(&mut self, tag: u16) -> u64; + fn assert_tag_u64_vec(&mut self, tag: u16) -> Vec<u64>; + fn assert_tag_i64(&mut self, tag: u16) -> i64; + fn assert_tag_i64_vec(&mut self, tag: u16) -> Vec<i64>; +} + +impl<R: std::io::Read + std::io::Seek> AssertDecode for Decoder<R> { + fn assert_tag_u32(&mut self, tag: u16) -> u32 { + self.get_tag(Tag::Unknown(tag)).unwrap().into_u32().unwrap() + } + fn assert_tag_u32_vec(&mut self, tag: u16) -> Vec<u32> { + self.get_tag(Tag::Unknown(tag)) + .unwrap() + .into_u32_vec() + .unwrap() + } + fn assert_tag_i32(&mut self, tag: u16) -> i32 { + self.get_tag(Tag::Unknown(tag)).unwrap().into_i32().unwrap() + } + fn assert_tag_i32_vec(&mut self, tag: u16) -> Vec<i32> { + self.get_tag(Tag::Unknown(tag)) + .unwrap() + .into_i32_vec() + .unwrap() + } + fn assert_tag_u64(&mut self, tag: u16) -> u64 { + self.get_tag(Tag::Unknown(tag)).unwrap().into_u64().unwrap() + } + fn assert_tag_u64_vec(&mut self, tag: u16) -> Vec<u64> { + self.get_tag(Tag::Unknown(tag)) + .unwrap() + .into_u64_vec() + .unwrap() + } + fn assert_tag_i64(&mut self, tag: u16) -> i64 { + self.get_tag(Tag::Unknown(tag)).unwrap().into_i64().unwrap() + } + fn assert_tag_i64_vec(&mut self, tag: u16) -> Vec<i64> { + self.get_tag(Tag::Unknown(tag)) + .unwrap() + .into_i64_vec() + .unwrap() + } +} + +#[test] +fn test_multiple_byte() { + let mut data = Cursor::new(Vec::new()); + + { + let mut tiff = TiffEncoder::new(&mut data).unwrap(); + let mut image_encoder = tiff.new_image::<colortype::Gray8>(1, 1).unwrap(); + image_encoder.write_strip(&[1]).unwrap(); + let encoder = image_encoder.encoder(); + + encoder.write_tag(Tag::Unknown(65000), &[1_u8][..]).unwrap(); + encoder + .write_tag(Tag::Unknown(65001), &[1_u8, 2][..]) + .unwrap(); + encoder + .write_tag(Tag::Unknown(65002), &[1_u8, 2, 3][..]) + .unwrap(); + encoder + .write_tag(Tag::Unknown(65003), &[1_u8, 2, 3, 4][..]) + .unwrap(); + encoder + .write_tag(Tag::Unknown(65004), &[1_u8, 2, 3, 4, 5][..]) + .unwrap(); + } + + data.set_position(0); + { + let mut decoder = Decoder::new(&mut data).unwrap(); + + assert_eq!(decoder.assert_tag_u32_vec(65000), [1]); + assert_eq!(decoder.assert_tag_u32_vec(65001), [1, 2]); + assert_eq!(decoder.assert_tag_u32_vec(65002), [1, 2, 3]); + assert_eq!(decoder.assert_tag_u32_vec(65003), [1, 2, 3, 4]); + assert_eq!(decoder.assert_tag_u32_vec(65004), [1, 2, 3, 4, 5]); + } +} + +#[test] +/// Test writing signed tags from TIFF 6.0 +fn test_signed() { + let mut data = Cursor::new(Vec::new()); + fn make_srational(i: i32) -> SRational { + SRational { n: i, d: 100 } + } + + { + let mut tiff = TiffEncoder::new(&mut data).unwrap(); + let mut image_encoder = tiff.new_image::<colortype::Gray8>(1, 1).unwrap(); + image_encoder.write_strip(&[1]).unwrap(); + let encoder = image_encoder.encoder(); + + //Use the "reusable" tags section as per the TIFF6 spec + encoder.write_tag(Tag::Unknown(65000), -1_i8).unwrap(); + encoder + .write_tag(Tag::Unknown(65001), &[-1_i8][..]) + .unwrap(); + encoder + .write_tag(Tag::Unknown(65002), &[-1_i8, 2][..]) + .unwrap(); + encoder + .write_tag(Tag::Unknown(65003), &[-1_i8, 2, -3][..]) + .unwrap(); + encoder + .write_tag(Tag::Unknown(65004), &[-1_i8, 2, -3, 4][..]) + .unwrap(); + encoder + .write_tag(Tag::Unknown(65005), &[-1_i8, 2, -3, 4, -5][..]) + .unwrap(); + + encoder.write_tag(Tag::Unknown(65010), -1_i16).unwrap(); + encoder.write_tag(Tag::Unknown(65011), -1_i16).unwrap(); + encoder + .write_tag(Tag::Unknown(65012), &[-1_i16, 2][..]) + .unwrap(); + encoder + .write_tag(Tag::Unknown(65013), &[-1_i16, 2, -3][..]) + .unwrap(); + + encoder.write_tag(Tag::Unknown(65020), -1_i32).unwrap(); + encoder + .write_tag(Tag::Unknown(65021), &[-1_i32][..]) + .unwrap(); + encoder + .write_tag(Tag::Unknown(65022), &[-1_i32, 2][..]) + .unwrap(); + + encoder.write_tag(Tag::Unknown(65030), -1_i64).unwrap(); + encoder + .write_tag(Tag::Unknown(65031), &[-1_i64][..]) + .unwrap(); + encoder + .write_tag(Tag::Unknown(65032), &[-1_i64, 2][..]) + .unwrap(); + + encoder + .write_tag(Tag::Unknown(65040), make_srational(-1)) + .unwrap(); + encoder + .write_tag( + Tag::Unknown(65041), + &[make_srational(-1), make_srational(2)][..], + ) + .unwrap(); + } + + //Rewind the cursor for reading + data.set_position(0); + { + let mut decoder = Decoder::new(&mut data).unwrap(); + + assert_eq!(decoder.assert_tag_i32(65000), -1); + assert_eq!(decoder.assert_tag_i32_vec(65001), [-1]); + assert_eq!(decoder.assert_tag_i32_vec(65002), [-1, 2]); + assert_eq!(decoder.assert_tag_i32_vec(65003), [-1, 2, -3]); + assert_eq!(decoder.assert_tag_i32_vec(65004), [-1, 2, -3, 4]); + assert_eq!(decoder.assert_tag_i32_vec(65005), [-1, 2, -3, 4, -5],); + + assert_eq!(decoder.assert_tag_i32(65010), -1); + assert_eq!(decoder.assert_tag_i32_vec(65011), [-1]); + assert_eq!(decoder.assert_tag_i32_vec(65012), [-1, 2]); + assert_eq!(decoder.assert_tag_i32_vec(65013), [-1, 2, -3]); + + assert_eq!(decoder.assert_tag_i32(65020), -1); + assert_eq!(decoder.assert_tag_i32_vec(65021), [-1]); + assert_eq!(decoder.assert_tag_i32_vec(65022), [-1, 2]); + + assert_eq!(decoder.assert_tag_i64(65030), -1); + assert_eq!(decoder.assert_tag_i64_vec(65031), [-1]); + assert_eq!(decoder.assert_tag_i64_vec(65032), [-1, 2]); + + assert_eq!(decoder.assert_tag_i32_vec(65040), [-1, 100]); + assert_eq!(decoder.assert_tag_i32_vec(65041), [-1_i32, 100, 2, 100]); + } +} + +#[test] +/// check multipage image handling +fn test_multipage_image() { + let mut img_file = Cursor::new(Vec::new()); + + { + // first create a multipage image with 2 images + let mut img_encoder = TiffEncoder::new(&mut img_file).unwrap(); + + // write first grayscale image (2x2 16-bit) + let img1: Vec<u16> = [1, 2, 3, 4].to_vec(); + img_encoder + .write_image::<colortype::Gray16>(2, 2, &img1[..]) + .unwrap(); + // write second grayscale image (3x3 8-bit) + let img2: Vec<u8> = [9, 8, 7, 6, 5, 4, 3, 2, 1].to_vec(); + img_encoder + .write_image::<colortype::Gray8>(3, 3, &img2[..]) + .unwrap(); + } + + // seek to the beginning of the file, so that it can be decoded + img_file.seek(SeekFrom::Start(0)).unwrap(); + + { + let mut img_decoder = Decoder::new(&mut img_file).unwrap(); + + // check the dimensions of the image in the first page + assert_eq!(img_decoder.dimensions().unwrap(), (2, 2)); + img_decoder.next_image().unwrap(); + // check the dimensions of the image in the second page + assert_eq!(img_decoder.dimensions().unwrap(), (3, 3)); + } +} + +#[test] +/// verify rows per strip setting +fn test_rows_per_strip() { + let mut file = Cursor::new(Vec::new()); + { + let mut img_encoder = TiffEncoder::new(&mut file).unwrap(); + + let mut image = img_encoder.new_image::<colortype::Gray8>(100, 100).unwrap(); + assert_eq!(image.next_strip_sample_count(), 100 * 100); + image.rows_per_strip(2).unwrap(); + assert_eq!(image.next_strip_sample_count(), 2 * 100); + + let img2: Vec<u8> = vec![0; 2 * 100]; + image.write_strip(&img2[..]).unwrap(); + assert!(image.rows_per_strip(5).is_err()); + for i in 1..50 { + let img2: Vec<u8> = vec![i; 2 * 100]; + image.write_strip(&img2[..]).unwrap(); + } + assert!(image.write_strip(&img2[..]).is_err()); + image.finish().unwrap(); + } + + file.seek(SeekFrom::Start(0)).unwrap(); + { + let mut decoder = Decoder::new(&mut file).unwrap(); + assert_eq!(decoder.get_tag_u64(Tag::RowsPerStrip).unwrap(), 2); + assert_eq!(decoder.strip_count().unwrap(), 50); + + 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[..]), + other => panic!("Incorrect strip type {:?}", other), + } + } + } +} diff --git a/vendor/tiff/tests/encode_images_with_compression.rs b/vendor/tiff/tests/encode_images_with_compression.rs new file mode 100644 index 0000000..1cf178f --- /dev/null +++ b/vendor/tiff/tests/encode_images_with_compression.rs @@ -0,0 +1,157 @@ +extern crate tiff; + +use std::io::{Cursor, Seek, Write}; +use tiff::{ + decoder::{Decoder, DecodingResult}, + encoder::{ + colortype::{self, ColorType}, + compression::*, + TiffEncoder, TiffValue, + }, +}; + +trait TestImage<const NUM_CHANNELS: usize>: From<Vec<<Self::Color as ColorType>::Inner>> { + const WIDTH: u32; + const HEIGHT: u32; + type Color: ColorType; + + fn reference_data(&self) -> &[<Self::Color as ColorType>::Inner]; + fn generate_pixel(x: u32, y: u32) -> [<Self::Color as ColorType>::Inner; NUM_CHANNELS]; + + fn compress<C: Compression, W: Write + Seek>( + &self, + encoder: &mut TiffEncoder<W>, + compression: C, + ) where + [<Self::Color as ColorType>::Inner]: TiffValue, + { + let image = encoder + .new_image_with_compression::<Self::Color, C>(Self::WIDTH, Self::HEIGHT, compression) + .unwrap(); + image.write_data(self.reference_data()).unwrap(); + } + + fn generate() -> Self { + assert_eq!( + Self::Color::BITS_PER_SAMPLE.len(), + NUM_CHANNELS, + "Incompatible color type" + ); + + let mut data = Vec::with_capacity((Self::WIDTH * Self::HEIGHT) as usize * NUM_CHANNELS); + for x in 0..Self::WIDTH { + for y in 0..Self::HEIGHT { + data.extend(IntoIterator::into_iter(Self::generate_pixel(x, y))); + } + } + Self::from(data) + } +} + +struct TestImageColor(Vec<u16>); + +impl From<Vec<u16>> for TestImageColor { + fn from(value: Vec<u16>) -> Self { + Self(value) + } +} + +impl TestImage<3> for TestImageColor { + const WIDTH: u32 = 1; + const HEIGHT: u32 = 7; + type Color = colortype::RGB16; + + fn reference_data(&self) -> &[u16] { + &self.0 + } + + fn generate_pixel(x: u32, y: u32) -> [<Self::Color as ColorType>::Inner; 3] { + let val = (x + y) % <Self::Color as ColorType>::Inner::MAX as u32; + [val as <Self::Color as ColorType>::Inner; 3] + } +} + +struct TestImageGrayscale(Vec<u8>); + +impl From<Vec<u8>> for TestImageGrayscale { + fn from(value: Vec<u8>) -> Self { + Self(value) + } +} + +impl TestImage<1> for TestImageGrayscale { + const WIDTH: u32 = 21; + const HEIGHT: u32 = 10; + type Color = colortype::Gray8; + + fn reference_data(&self) -> &[u8] { + &self.0 + } + + fn generate_pixel(x: u32, y: u32) -> [<Self::Color as ColorType>::Inner; 1] { + let val = (x + y) % <Self::Color as ColorType>::Inner::MAX as u32; + [val as <Self::Color as ColorType>::Inner] + } +} + +fn encode_decode_with_compression<C: Compression + Clone>(compression: C) { + let mut data = Cursor::new(Vec::new()); + + let image_rgb = TestImageColor::generate(); + let image_grayscale = TestImageGrayscale::generate(); + + // Encode tiff with compression + { + // Create a multipage image with 2 images + let mut encoder = TiffEncoder::new(&mut data).unwrap(); + image_rgb.compress(&mut encoder, compression.clone()); + image_grayscale.compress(&mut encoder, compression); + } + + // Decode tiff + data.set_position(0); + { + let mut decoder = Decoder::new(data).unwrap(); + + // Check the RGB image + assert_eq!( + match decoder.read_image() { + Ok(DecodingResult::U16(image_data)) => image_data, + unexpected => panic!("Descoding RGB failed: {:?}", unexpected), + }, + image_rgb.reference_data() + ); + + // Check the grayscale image + decoder.next_image().unwrap(); + assert_eq!( + match decoder.read_image() { + Ok(DecodingResult::U8(image_data)) => image_data, + unexpected => panic!("Decoding grayscale failed: {:?}", unexpected), + }, + image_grayscale.reference_data() + ); + } +} + +#[test] +fn encode_decode_without_compression() { + encode_decode_with_compression(Uncompressed::default()); +} + +#[test] +fn encode_decode_with_lzw() { + encode_decode_with_compression(Lzw::default()); +} + +#[test] +fn encode_decode_with_deflate() { + encode_decode_with_compression(Deflate::with_level(DeflateLevel::Fast)); + encode_decode_with_compression(Deflate::with_level(DeflateLevel::Balanced)); + encode_decode_with_compression(Deflate::with_level(DeflateLevel::Best)); +} + +#[test] +fn encode_decode_with_packbits() { + encode_decode_with_compression(Packbits::default()); +} diff --git a/vendor/tiff/tests/fuzz_tests.rs b/vendor/tiff/tests/fuzz_tests.rs new file mode 100644 index 0000000..446d6bd --- /dev/null +++ b/vendor/tiff/tests/fuzz_tests.rs @@ -0,0 +1,51 @@ +extern crate tiff; + +use tiff::decoder::Decoder; +use tiff::TiffResult; + +use std::fs::File; + +fn test_directory<F: Fn(File) -> bool>(path: &str, f: F) { + for entry in std::fs::read_dir(path).unwrap() { + let file = File::open(entry.unwrap().path()).unwrap(); + assert!(f(file)); + } +} + +fn decode_tiff(file: File) -> TiffResult<()> { + let mut decoder = Decoder::new(file)?; + decoder.read_image()?; + Ok(()) +} + +#[test] +fn oor_panic() { + test_directory("./tests/fuzz_images/oor_panic", |file| { + let _ = decode_tiff(file); + true + }); +} + +#[test] +fn oom_crash() { + test_directory("./tests/fuzz_images/oom_crash", |file| { + decode_tiff(file).is_err() + }); +} + +#[test] +fn inf_loop() { + test_directory("./tests/fuzz_images/inf_loop", |file| { + let _ = decode_tiff(file); + true + }); +} + +// https://github.com/image-rs/image-tiff/issues/33 +#[test] +fn divide_by_zero() { + test_directory("./tests/fuzz_images/divide_by_zero", |file| { + let _ = decode_tiff(file); + true + }); +} |