| 1 |
extern crate tiff; |
| 2 |
|
| 3 |
use tiff::decoder::{ifd, Decoder, DecodingResult}; |
| 4 |
use tiff::ColorType; |
| 5 |
|
| 6 |
use std::fs::File; |
| 7 |
use std::io::{Read, Seek}; |
| 8 |
use std::path::PathBuf; |
| 9 |
|
| 10 |
const TEST_IMAGE_DIR: &str = "./tests/images/"; |
| 11 |
|
| 12 |
macro_rules! test_image_sum { |
| 13 |
($name:ident, $buffer:ident, $sum_ty:ty) => { |
| 14 |
fn $name(file: &str, expected_type: ColorType, expected_sum: $sum_ty) { |
| 15 |
let path = PathBuf::from(TEST_IMAGE_DIR).join(file); |
| 16 |
let img_file = File::open(path).expect("Cannot find test image!"); |
| 17 |
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 18 |
assert_eq!(decoder.colortype().unwrap(), expected_type); |
| 19 |
let img_res = decoder.read_image().unwrap(); |
| 20 |
|
| 21 |
match img_res { |
| 22 |
DecodingResult::$buffer(res) => { |
| 23 |
let sum: $sum_ty = res.into_iter().map(<$sum_ty>::from).sum(); |
| 24 |
assert_eq!(sum, expected_sum); |
| 25 |
} |
| 26 |
_ => panic!("Wrong bit depth"), |
| 27 |
} |
| 28 |
} |
| 29 |
}; |
| 30 |
} |
| 31 |
|
| 32 |
test_image_sum!(test_image_sum_u8, U8, u64); |
| 33 |
test_image_sum!(test_image_sum_i8, I8, i64); |
| 34 |
test_image_sum!(test_image_sum_u16, U16, u64); |
| 35 |
test_image_sum!(test_image_sum_i16, I16, i64); |
| 36 |
test_image_sum!(test_image_sum_u32, U32, u64); |
| 37 |
test_image_sum!(test_image_sum_u64, U64, u64); |
| 38 |
test_image_sum!(test_image_sum_f32, F32, f32); |
| 39 |
test_image_sum!(test_image_sum_f64, F64, f64); |
| 40 |
|
| 41 |
/// Tests that a decoder can be constructed for an image and the color type |
| 42 |
/// read from the IFD and is of the appropriate type, but the type is |
| 43 |
/// unsupported. |
| 44 |
fn test_image_color_type_unsupported(file: &str, expected_type: ColorType) { |
| 45 |
let path = PathBuf::from(TEST_IMAGE_DIR).join(file); |
| 46 |
let img_file = File::open(path).expect("Cannot find test image!"); |
| 47 |
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 48 |
assert_eq!(decoder.colortype().unwrap(), expected_type); |
| 49 |
assert!(matches!( |
| 50 |
decoder.read_image(), |
| 51 |
Err(tiff::TiffError::UnsupportedError( |
| 52 |
tiff::TiffUnsupportedError::UnsupportedColorType(_), |
| 53 |
)) |
| 54 |
)); |
| 55 |
} |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn test_cmyk_u8() { |
| 59 |
test_image_sum_u8("cmyk-3c-8b.tiff", ColorType::CMYK(8), 8522658); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn test_cmyk_u16() { |
| 64 |
test_image_sum_u16("cmyk-3c-16b.tiff", ColorType::CMYK(16), 2181426827); |
| 65 |
} |
| 66 |
|
| 67 |
#[test] |
| 68 |
fn test_cmyk_f32() { |
| 69 |
test_image_sum_f32("cmyk-3c-32b-float.tiff", ColorType::CMYK(32), 496.0405); |
| 70 |
} |
| 71 |
|
| 72 |
#[test] |
| 73 |
fn test_gray_u8() { |
| 74 |
test_image_sum_u8("minisblack-1c-8b.tiff", ColorType::Gray(8), 2840893); |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn test_gray_u12() { |
| 79 |
test_image_color_type_unsupported("12bit.cropped.tiff", ColorType::Gray(12)); |
| 80 |
} |
| 81 |
|
| 82 |
#[test] |
| 83 |
fn test_gray_u16() { |
| 84 |
test_image_sum_u16("minisblack-1c-16b.tiff", ColorType::Gray(16), 733126239); |
| 85 |
} |
| 86 |
|
| 87 |
#[test] |
| 88 |
fn test_gray_u32() { |
| 89 |
test_image_sum_u32("gradient-1c-32b.tiff", ColorType::Gray(32), 549892913787); |
| 90 |
} |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn test_gray_u64() { |
| 94 |
test_image_sum_u64("gradient-1c-64b.tiff", ColorType::Gray(64), 549892913787); |
| 95 |
} |
| 96 |
|
| 97 |
#[test] |
| 98 |
fn test_gray_f32() { |
| 99 |
test_image_sum_f32("gradient-1c-32b-float.tiff", ColorType::Gray(32), 128.03194); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn test_gray_f64() { |
| 104 |
test_image_sum_f64( |
| 105 |
"gradient-1c-64b-float.tiff", |
| 106 |
ColorType::Gray(64), |
| 107 |
128.0319210877642, |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn test_rgb_u8() { |
| 113 |
test_image_sum_u8("rgb-3c-8b.tiff", ColorType::RGB(8), 7842108); |
| 114 |
} |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn test_rgb_u12() { |
| 118 |
test_image_color_type_unsupported("12bit.cropped.rgb.tiff", ColorType::RGB(12)); |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn test_rgb_u16() { |
| 123 |
test_image_sum_u16("rgb-3c-16b.tiff", ColorType::RGB(16), 2024349944); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn test_rgb_u32() { |
| 128 |
test_image_sum_u32("gradient-3c-32b.tiff", ColorType::RGB(32), 2030834111716); |
| 129 |
} |
| 130 |
|
| 131 |
#[test] |
| 132 |
fn test_rgb_u64() { |
| 133 |
test_image_sum_u64("gradient-3c-64b.tiff", ColorType::RGB(64), 2030834111716); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn test_rgb_f32() { |
| 138 |
test_image_sum_f32("gradient-3c-32b-float.tiff", ColorType::RGB(32), 472.8405); |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn test_int8() { |
| 143 |
test_image_sum_i8("int8.tif", ColorType::Gray(8), 3111) |
| 144 |
} |
| 145 |
|
| 146 |
#[test] |
| 147 |
fn test_int8_rgb() { |
| 148 |
test_image_sum_i8("int8_rgb.tif", ColorType::RGB(8), -10344) |
| 149 |
} |
| 150 |
|
| 151 |
#[test] |
| 152 |
fn test_int16() { |
| 153 |
test_image_sum_i16("int16.tif", ColorType::Gray(16), 354396); |
| 154 |
} |
| 155 |
|
| 156 |
#[test] |
| 157 |
fn test_int16_rgb() { |
| 158 |
test_image_sum_i16("int16_rgb.tif", ColorType::RGB(16), 1063188); |
| 159 |
} |
| 160 |
|
| 161 |
#[test] |
| 162 |
fn test_string_tags() { |
| 163 |
// these files have null-terminated strings for their Software tag. One has extra bytes after |
| 164 |
// the null byte, so we check both to ensure that we're truncating properly |
| 165 |
let filenames = ["minisblack-1c-16b.tiff", "rgb-3c-16b.tiff"]; |
| 166 |
for filename in filenames.iter() { |
| 167 |
let path = PathBuf::from(TEST_IMAGE_DIR).join(filename); |
| 168 |
let img_file = File::open(path).expect("Cannot find test image!"); |
| 169 |
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 170 |
let software = decoder.get_tag(tiff::tags::Tag::Software).unwrap(); |
| 171 |
match software { |
| 172 |
ifd::Value::Ascii(s) => assert_eq!( |
| 173 |
&s, |
| 174 |
"GraphicsMagick 1.2 unreleased Q16 http://www.GraphicsMagick.org/" |
| 175 |
), |
| 176 |
other => unreachable!("Expected Ascii tag from file, got {other:?}"), |
| 177 |
}; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
#[test] |
| 182 |
fn test_decode_data() { |
| 183 |
let mut image_data = Vec::new(); |
| 184 |
for x in 0..100 { |
| 185 |
for y in 0..100u8 { |
| 186 |
let val = x + y; |
| 187 |
image_data.push(val); |
| 188 |
image_data.push(val); |
| 189 |
image_data.push(val); |
| 190 |
} |
| 191 |
} |
| 192 |
let file = File::open("./tests/decodedata-rgb-3c-8b.tiff").unwrap(); |
| 193 |
let mut decoder = Decoder::new(file).unwrap(); |
| 194 |
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8)); |
| 195 |
assert_eq!(decoder.dimensions().unwrap(), (100, 100)); |
| 196 |
if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() { |
| 197 |
assert_eq!(image_data, img_res); |
| 198 |
} else { |
| 199 |
panic!("Wrong data type"); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
#[test] |
| 204 |
fn issue_69() { |
| 205 |
test_image_sum_u16("issue_69_lzw.tiff", ColorType::Gray(16), 1015486); |
| 206 |
test_image_sum_u16("issue_69_packbits.tiff", ColorType::Gray(16), 1015486); |
| 207 |
} |
| 208 |
|
| 209 |
// TODO: GrayA support |
| 210 |
//#[test] |
| 211 |
//fn test_gray_alpha_u8() |
| 212 |
//{ |
| 213 |
//let img_file = File::open("./tests/images/minisblack-2c-8b-alpha.tiff").expect("Cannot find test image!"); |
| 214 |
//let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 215 |
//assert_eq!(decoder.colortype().unwrap(), ColorType::GrayA(8)); |
| 216 |
//let img_res = decoder.read_image(); |
| 217 |
//assert!(img_res.is_ok()); |
| 218 |
//} |
| 219 |
|
| 220 |
#[test] |
| 221 |
fn test_tiled_gray_i1() { |
| 222 |
test_image_sum_u8("tiled-gray-i1.tif", ColorType::Gray(1), 30531); |
| 223 |
} |
| 224 |
|
| 225 |
#[test] |
| 226 |
fn test_tiled_rgb_u8() { |
| 227 |
test_image_sum_u8("tiled-rgb-u8.tif", ColorType::RGB(8), 39528948); |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
fn test_tiled_rect_rgb_u8() { |
| 232 |
test_image_sum_u8("tiled-rect-rgb-u8.tif", ColorType::RGB(8), 62081032); |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn test_inner_access() { |
| 237 |
let path = PathBuf::from(TEST_IMAGE_DIR).join("tiled-rect-rgb-u8.tif"); |
| 238 |
let img_file = File::open(path).expect("Cannot find test image!"); |
| 239 |
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 240 |
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8)); |
| 241 |
|
| 242 |
let c = decoder |
| 243 |
.get_tag(tiff::tags::Tag::Compression) |
| 244 |
.unwrap() |
| 245 |
.into_u16() |
| 246 |
.unwrap(); |
| 247 |
assert_eq!(c, tiff::tags::CompressionMethod::None.to_u16()); |
| 248 |
|
| 249 |
// Because the image is uncompressed, reading the first tile directly with the inner reader |
| 250 |
// should yield the same result as reading it with the decoder's read_chunk method. |
| 251 |
let first_offset = decoder |
| 252 |
.get_tag_u32_vec(tiff::tags::Tag::TileOffsets) |
| 253 |
.unwrap() |
| 254 |
.into_iter() |
| 255 |
.next() |
| 256 |
.unwrap(); |
| 257 |
let first_byte_count = decoder |
| 258 |
.get_tag_u32_vec(tiff::tags::Tag::TileByteCounts) |
| 259 |
.unwrap() |
| 260 |
.into_iter() |
| 261 |
.next() |
| 262 |
.unwrap(); |
| 263 |
decoder |
| 264 |
.inner() |
| 265 |
.seek(std::io::SeekFrom::Start(first_offset as u64)) |
| 266 |
.expect("Cannot seek to tile offset"); |
| 267 |
let mut buf = vec![0; first_byte_count as usize]; |
| 268 |
decoder |
| 269 |
.inner() |
| 270 |
.read_exact(&mut buf) |
| 271 |
.expect("Cannot read tile data"); |
| 272 |
let raw_sum: u64 = buf.into_iter().map(<u64>::from).sum(); |
| 273 |
|
| 274 |
match decoder.read_chunk(0).unwrap() { |
| 275 |
DecodingResult::U8(chunk) => { |
| 276 |
let sum: u64 = chunk.into_iter().map(<u64>::from).sum(); |
| 277 |
assert_eq!(sum, raw_sum); |
| 278 |
} |
| 279 |
_ => panic!("Wrong bit depth"), |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
/* #[test] |
| 284 |
fn test_tiled_jpeg_rgb_u8() { |
| 285 |
test_image_sum_u8("tiled-jpeg-rgb-u8.tif", ColorType::RGB(8), 93031606); |
| 286 |
} */ |
| 287 |
|
| 288 |
#[test] |
| 289 |
fn test_tiled_oversize_gray_i8() { |
| 290 |
test_image_sum_i8("tiled-oversize-gray-i8.tif", ColorType::Gray(8), 1214996); |
| 291 |
} |
| 292 |
|
| 293 |
#[test] |
| 294 |
fn test_tiled_cmyk_i8() { |
| 295 |
test_image_sum_i8("tiled-cmyk-i8.tif", ColorType::CMYK(8), 1759101); |
| 296 |
} |
| 297 |
|
| 298 |
#[test] |
| 299 |
fn test_tiled_incremental() { |
| 300 |
let file = "tiled-rgb-u8.tif"; |
| 301 |
let expected_type = ColorType::RGB(8); |
| 302 |
let sums = [ |
| 303 |
188760, 195639, 108148, 81986, 665088, 366140, 705317, 423366, 172033, 324455, 244102, |
| 304 |
81853, 181258, 247971, 129486, 55600, 565625, 422102, 730888, 379271, 232142, 292549, |
| 305 |
244045, 86866, 188141, 115036, 150785, 84389, 353170, 459325, 719619, 329594, 278663, |
| 306 |
220474, 243048, 113563, 189152, 109684, 179391, 122188, 279651, 622093, 724682, 302459, |
| 307 |
268428, 204499, 224255, 124674, 170668, 121868, 192768, 183367, 378029, 585651, 657712, |
| 308 |
296790, 241444, 197083, 198429, 134869, 182318, 86034, 203655, 182338, 297255, 601284, |
| 309 |
633813, 242531, 228578, 206441, 193552, 125412, 181527, 165439, 202531, 159538, 268388, |
| 310 |
565790, 611382, 272967, 236497, 215154, 158881, 90806, 106114, 182342, 191824, 186138, |
| 311 |
215174, 393193, 701228, 198866, 227944, 193830, 166330, 49008, 55719, 122820, 197316, |
| 312 |
161969, 203152, 170986, 624427, 188605, 186187, 111064, 115192, 39538, 48626, 163929, |
| 313 |
144682, 135796, 194141, 154198, 584125, 180255, 153524, 121433, 132641, 35743, 47798, |
| 314 |
152343, 162874, 167664, 160175, 133038, 659882, 138339, 166470, 124173, 118929, 51317, |
| 315 |
45267, 155776, 161331, 161006, 130052, 137618, 337291, 106481, 161999, 127343, 87724, |
| 316 |
59540, 63907, 155677, 140668, 141523, 108061, 168657, 186482, 98599, 147614, 139963, 90444, |
| 317 |
56602, 92547, 125644, 134212, 126569, 144153, 179800, 174516, 133969, 129399, 117681, |
| 318 |
83305, 55075, 110737, 115108, 128572, 128911, 130922, 179986, 143288, 145884, 155856, |
| 319 |
96683, 94057, 56238, 79649, 71651, 70182, 75010, 77009, 98855, 78979, 74341, 83482, 53403, |
| 320 |
59842, 30305, |
| 321 |
]; |
| 322 |
|
| 323 |
let path = PathBuf::from(TEST_IMAGE_DIR).join(file); |
| 324 |
let img_file = File::open(path).expect("Cannot find test image!"); |
| 325 |
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 326 |
assert_eq!(decoder.colortype().unwrap(), expected_type); |
| 327 |
|
| 328 |
let tiles = decoder.tile_count().unwrap(); |
| 329 |
assert_eq!(tiles as usize, sums.len()); |
| 330 |
|
| 331 |
for tile in 0..tiles { |
| 332 |
match decoder.read_chunk(tile).unwrap() { |
| 333 |
DecodingResult::U8(res) => { |
| 334 |
let sum: u64 = res.into_iter().map(<u64>::from).sum(); |
| 335 |
assert_eq!(sum, sums[tile as usize]); |
| 336 |
} |
| 337 |
_ => panic!("Wrong bit depth"), |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
#[test] |
| 343 |
fn test_planar_rgb_u8() { |
| 344 |
test_image_sum_u8("planar-rgb-u8.tif", ColorType::RGB(8), 15417630); |
| 345 |
} |
| 346 |
|
| 347 |
#[test] |
| 348 |
fn test_read_planar_bands() { |
| 349 |
// gdal_translate tiled-rgb-u8.tif planar-rgb-u8.tif -co INTERLEAVE=BAND -co COMPRESS=LZW -co PROFILE=BASELINE |
| 350 |
let file = "planar-rgb-u8.tif"; |
| 351 |
let expected_type = ColorType::RGB(8); |
| 352 |
|
| 353 |
let path = PathBuf::from(TEST_IMAGE_DIR).join(file); |
| 354 |
let img_file = File::open(path).expect("Cannot find test image!"); |
| 355 |
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 356 |
assert_eq!(decoder.colortype().unwrap(), expected_type); |
| 357 |
|
| 358 |
let chunks = decoder.strip_count().unwrap(); |
| 359 |
assert_eq!(chunks as usize, 72); |
| 360 |
|
| 361 |
// convert -quiet planar-rgb-u8.tif[0] -crop 1x1+0+0 txt: |
| 362 |
// 0,0: (73,51,30) #49331E srgb(73,51,30) |
| 363 |
|
| 364 |
// 1st band (red) |
| 365 |
match decoder.read_chunk(0).unwrap() { |
| 366 |
DecodingResult::U8(chunk) => { |
| 367 |
assert_eq!(chunk[0], 73); |
| 368 |
} |
| 369 |
_ => panic!("Wrong bit depth"), |
| 370 |
} |
| 371 |
// 2nd band (green) |
| 372 |
match decoder.read_chunk(chunks / 3).unwrap() { |
| 373 |
DecodingResult::U8(chunk) => { |
| 374 |
assert_eq!(chunk[0], 51); |
| 375 |
} |
| 376 |
_ => panic!("Wrong bit depth"), |
| 377 |
} |
| 378 |
// 3rd band (blue) |
| 379 |
match decoder.read_chunk(chunks / 3 * 2).unwrap() { |
| 380 |
DecodingResult::U8(chunk) => { |
| 381 |
assert_eq!(chunk[0], 30); |
| 382 |
} |
| 383 |
_ => panic!("Wrong bit depth"), |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
#[test] |
| 388 |
fn test_div_zero() { |
| 389 |
use tiff::{TiffError, TiffFormatError}; |
| 390 |
|
| 391 |
let image = [ |
| 392 |
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, |
| 393 |
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, |
| 394 |
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, |
| 395 |
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, |
| 396 |
178, 178, 178, |
| 397 |
]; |
| 398 |
|
| 399 |
let err = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err(); |
| 400 |
|
| 401 |
match err { |
| 402 |
TiffError::FormatError(TiffFormatError::StripTileTagConflict) => {} |
| 403 |
unexpected => panic!("Unexpected error {unexpected}"), |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
#[test] |
| 408 |
fn test_too_many_value_bytes() { |
| 409 |
let image = [ |
| 410 |
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, |
| 411 |
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, |
| 412 |
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, |
| 413 |
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, |
| 414 |
0, 89, 89, 89, 89, 89, 89, 89, 89, 96, 1, 20, 89, 89, 89, 89, 18, |
| 415 |
]; |
| 416 |
|
| 417 |
let error = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err(); |
| 418 |
|
| 419 |
match error { |
| 420 |
tiff::TiffError::LimitsExceeded => {} |
| 421 |
unexpected => panic!("Unexpected error {unexpected}"), |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
#[test] |
| 426 |
fn fuzzer_testcase5() { |
| 427 |
let image = [ |
| 428 |
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, |
| 429 |
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, |
| 430 |
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, |
| 431 |
0, 0, 0, 40, 0, 251, 255, 23, 1, 4, 0, 1, 0, 0, 0, 48, 178, 178, 178, 178, 178, 178, 178, |
| 432 |
178, 178, 178, |
| 433 |
]; |
| 434 |
|
| 435 |
let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err(); |
| 436 |
} |
| 437 |
|
| 438 |
#[test] |
| 439 |
fn fuzzer_testcase1() { |
| 440 |
let image = [ |
| 441 |
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, |
| 442 |
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, |
| 443 |
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, |
| 444 |
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, |
| 445 |
178, |
| 446 |
]; |
| 447 |
|
| 448 |
let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err(); |
| 449 |
} |
| 450 |
|
| 451 |
#[test] |
| 452 |
fn fuzzer_testcase6() { |
| 453 |
let image = [ |
| 454 |
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, |
| 455 |
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, |
| 456 |
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, |
| 457 |
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, |
| 458 |
178, 178, |
| 459 |
]; |
| 460 |
|
| 461 |
let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err(); |
| 462 |
} |
| 463 |
|
| 464 |
#[test] |
| 465 |
fn oom() { |
| 466 |
let image = [ |
| 467 |
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, |
| 468 |
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, |
| 469 |
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, |
| 470 |
0, 0, 0, 40, 0, 0, 0, 23, 1, 4, 0, 1, 0, 0, 0, 178, 48, 178, 178, 178, 178, 162, 178, |
| 471 |
]; |
| 472 |
|
| 473 |
let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err(); |
| 474 |
} |
| 475 |
|
| 476 |
#[test] |
| 477 |
fn fuzzer_testcase4() { |
| 478 |
let image = [ |
| 479 |
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, |
| 480 |
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, |
| 481 |
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, |
| 482 |
0, 0, 0, 40, 0, 0, 0, 23, 1, 4, 0, 1, 0, 0, 0, 48, 178, 178, 178, 0, 1, 0, 13, 13, |
| 483 |
]; |
| 484 |
|
| 485 |
let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err(); |
| 486 |
} |
| 487 |
|
| 488 |
#[test] |
| 489 |
fn fuzzer_testcase2() { |
| 490 |
let image = [ |
| 491 |
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, |
| 492 |
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, |
| 493 |
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, |
| 494 |
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, |
| 495 |
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, |
| 496 |
1, 100, 0, 129, 0, 0, 0, 0, 0, 0, 0, 232, 254, 252, 255, 254, 255, 255, 255, 1, 29, 0, 0, |
| 497 |
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, |
| 498 |
73, |
| 499 |
]; |
| 500 |
|
| 501 |
let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err(); |
| 502 |
} |
| 503 |
|
| 504 |
#[test] |
| 505 |
fn invalid_jpeg_tag_2() { |
| 506 |
let image = [ |
| 507 |
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, |
| 508 |
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, |
| 509 |
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, |
| 510 |
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, |
| 511 |
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, |
| 512 |
91, 1, 7, 0, 5, 0, 0, 0, 64, 0, 0, 0, 237, 254, 65, 255, 255, 255, 255, 255, 1, 0, 0, 0, |
| 513 |
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, |
| 514 |
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 36, 73, 73, 0, 42, 36, 36, 36, 36, 0, 0, 8, 0, |
| 515 |
]; |
| 516 |
|
| 517 |
let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err(); |
| 518 |
} |
| 519 |
|
| 520 |
#[test] |
| 521 |
fn fuzzer_testcase3() { |
| 522 |
let image = [ |
| 523 |
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, |
| 524 |
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, |
| 525 |
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, |
| 526 |
0, 14, 0, 0, 0, 0, 0, 0, 0, 133, 133, 133, 77, 77, 77, 0, 0, 22, 128, 0, 255, 255, 255, |
| 527 |
255, 255, |
| 528 |
]; |
| 529 |
|
| 530 |
let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err(); |
| 531 |
} |
| 532 |
|
| 533 |
#[test] |
| 534 |
fn timeout() { |
| 535 |
use tiff::{TiffError, TiffFormatError}; |
| 536 |
|
| 537 |
let image = [ |
| 538 |
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, |
| 539 |
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, |
| 540 |
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, |
| 541 |
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, |
| 542 |
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, |
| 543 |
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, |
| 544 |
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, |
| 545 |
0, 0, 73, 73, 42, 0, 8, 0, 0, 0, 0, 0, 32, |
| 546 |
]; |
| 547 |
|
| 548 |
let error = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err(); |
| 549 |
|
| 550 |
match error { |
| 551 |
TiffError::FormatError(TiffFormatError::CycleInOffsets) => {} |
| 552 |
e => panic!("Unexpected error {e:?}"), |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
#[test] |
| 557 |
fn test_no_rows_per_strip() { |
| 558 |
test_image_sum_u8("no_rows_per_strip.tiff", ColorType::RGB(8), 99448840); |
| 559 |
} |
| 560 |
|
| 561 |
#[test] |
| 562 |
fn test_predictor_3_rgb_f32() { |
| 563 |
test_image_sum_f32("predictor-3-rgb-f32.tif", ColorType::RGB(32), 54004.33); |
| 564 |
} |
| 565 |
|
| 566 |
#[test] |
| 567 |
fn test_predictor_3_gray_f32() { |
| 568 |
test_image_sum_f32("predictor-3-gray-f32.tif", ColorType::Gray(32), 20008.275); |
| 569 |
} |
| 570 |
|
| 571 |
#[test] |
| 572 |
#[cfg(feature = "zstd")] |
| 573 |
fn test_zstd_compression() { |
| 574 |
// gdal_translate -co COMPRESS=ZSTD -co ZSTD_LEVEL=20 int16.tif int16_zstd.tif |
| 575 |
test_image_sum_i16("int16_zstd.tif", ColorType::Gray(16), 354396); |
| 576 |
} |
| 577 |
|
| 578 |
fn test_image_bytes( |
| 579 |
file: &str, |
| 580 |
expected: &[(ColorType, u32)], |
| 581 |
// Represent all samples as little endian, ensuring check sums on all platforms. |
| 582 |
normalize_byte_order: fn(&mut [u8]), |
| 583 |
) { |
| 584 |
let path = PathBuf::from(TEST_IMAGE_DIR).join(file); |
| 585 |
let img_file = File::open(path).expect("Cannot find test image!"); |
| 586 |
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); |
| 587 |
|
| 588 |
let mut buffer = vec![0u8; 0]; |
| 589 |
for (idx, &(expected_type, expected_crc)) in expected.iter().enumerate() { |
| 590 |
if idx > 0 { |
| 591 |
decoder |
| 592 |
.next_image() |
| 593 |
.expect("mismatched number of directories"); |
| 594 |
} |
| 595 |
|
| 596 |
assert_eq!(decoder.colortype().unwrap(), expected_type); |
| 597 |
let layout = decoder |
| 598 |
.image_buffer_layout() |
| 599 |
.expect("Cannot describe buffer layout"); |
| 600 |
|
| 601 |
buffer.resize(layout.len, 0u8); |
| 602 |
decoder.read_image_bytes(&mut buffer).unwrap(); |
| 603 |
normalize_byte_order(&mut buffer); |
| 604 |
|
| 605 |
// Well almost a crc.. |
| 606 |
assert_eq!(crc32fast::hash(&buffer), expected_crc); |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
fn byte_order_u16(bytes: &mut [u8]) { |
| 611 |
for chunk in bytes.chunks_exact_mut(2) { |
| 612 |
let n = u16::from_ne_bytes(chunk.try_into().unwrap()); |
| 613 |
chunk.copy_from_slice(&n.to_le_bytes()); |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
fn byte_order_u32(bytes: &mut [u8]) { |
| 618 |
for chunk in bytes.chunks_exact_mut(4) { |
| 619 |
let n = u32::from_ne_bytes(chunk.try_into().unwrap()); |
| 620 |
chunk.copy_from_slice(&n.to_le_bytes()); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
fn byte_order_u64(bytes: &mut [u8]) { |
| 625 |
for chunk in bytes.chunks_exact_mut(8) { |
| 626 |
let n = u64::from_ne_bytes(chunk.try_into().unwrap()); |
| 627 |
chunk.copy_from_slice(&n.to_le_bytes()); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
#[test] |
| 632 |
fn bytes_cmyk_3c_8b() { |
| 633 |
test_image_bytes( |
| 634 |
"cmyk-3c-8b.tiff", |
| 635 |
&[(ColorType::CMYK(8), 2935032230)], |
| 636 |
|_| (), |
| 637 |
); |
| 638 |
} |
| 639 |
|
| 640 |
#[test] |
| 641 |
fn bytes_gray_16b() { |
| 642 |
test_image_bytes( |
| 643 |
"minisblack-1c-16b.tiff", |
| 644 |
&[(ColorType::Gray(16), 3524597615)], |
| 645 |
byte_order_u16, |
| 646 |
); |
| 647 |
} |
| 648 |
|
| 649 |
#[test] |
| 650 |
fn bytes_gray_32b() { |
| 651 |
test_image_bytes( |
| 652 |
"gradient-1c-32b.tiff", |
| 653 |
&[(ColorType::Gray(32), 2257036530)], |
| 654 |
byte_order_u32, |
| 655 |
); |
| 656 |
} |
| 657 |
|
| 658 |
#[test] |
| 659 |
fn bytes_gray_u64() { |
| 660 |
test_image_bytes( |
| 661 |
"gradient-1c-64b.tiff", |
| 662 |
&[(ColorType::Gray(64), 2156547113)], |
| 663 |
byte_order_u64, |
| 664 |
); |
| 665 |
} |
| 666 |
|
| 667 |
#[test] |
| 668 |
fn bytes_gray_f32() { |
| 669 |
test_image_bytes( |
| 670 |
"gradient-1c-32b-float.tiff", |
| 671 |
&[(ColorType::Gray(32), 3232356730)], |
| 672 |
byte_order_u32, |
| 673 |
); |
| 674 |
} |
| 675 |
|
| 676 |
#[test] |
| 677 |
#[cfg(feature = "fax")] |
| 678 |
fn test_fax4() { |
| 679 |
test_image_sum_u8("fax4.tiff", ColorType::Gray(1), 7802706); |
| 680 |
} |
| 681 |
|
| 682 |
#[test] |
| 683 |
#[cfg(feature = "fax")] |
| 684 |
fn test_fax4_white_is_min() { |
| 685 |
test_image_sum_u8("imagemagick_group4.tiff", ColorType::Gray(1), 3742820); |
| 686 |
} |