Debian Codegraph @

tests/decode_images.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 #[ignore = "skipping, missing testdata"]
205 fn issue_69() {
206 test_image_sum_u16("issue_69_lzw.tiff", ColorType::Gray(16), 1015486);
207 test_image_sum_u16("issue_69_packbits.tiff", ColorType::Gray(16), 1015486);
208 }
209
210 // TODO: GrayA support
211 //#[test]
212 //fn test_gray_alpha_u8()
213 //{
214 //let img_file = File::open("./tests/images/minisblack-2c-8b-alpha.tiff").expect("Cannot find test image!");
215 //let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
216 //assert_eq!(decoder.colortype().unwrap(), ColorType::GrayA(8));
217 //let img_res = decoder.read_image();
218 //assert!(img_res.is_ok());
219 //}
220
221 #[test]
•222 #[ignore = "skipping, missing testdata"]
223 fn test_tiled_gray_i1() {
224 test_image_sum_u8("tiled-gray-i1.tif", ColorType::Gray(1), 30531);
225 }
226
227 #[test]
•228 #[ignore = "skipping, missing testdata"]
229 fn test_tiled_rgb_u8() {
230 test_image_sum_u8("tiled-rgb-u8.tif", ColorType::RGB(8), 39528948);
231 }
232
233 #[test]
•234 #[ignore = "skipping, missing testdata"]
235 fn test_tiled_rect_rgb_u8() {
236 test_image_sum_u8("tiled-rect-rgb-u8.tif", ColorType::RGB(8), 62081032);
237 }
238
239 #[test]
•240 #[ignore = "skipping, missing test data"]
241 fn test_inner_access() {
242 let path = PathBuf::from(TEST_IMAGE_DIR).join("tiled-rect-rgb-u8.tif");
243 let img_file = File::open(path).expect("Cannot find test image!");
244 let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
245 assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8));
246
247 let c = decoder
248 .get_tag(tiff::tags::Tag::Compression)
249 .unwrap()
250 .into_u16()
251 .unwrap();
252 assert_eq!(c, tiff::tags::CompressionMethod::None.to_u16());
253
254 // Because the image is uncompressed, reading the first tile directly with the inner reader
255 // should yield the same result as reading it with the decoder's read_chunk method.
256 let first_offset = decoder
257 .get_tag_u32_vec(tiff::tags::Tag::TileOffsets)
258 .unwrap()
259 .into_iter()
260 .next()
261 .unwrap();
262 let first_byte_count = decoder
263 .get_tag_u32_vec(tiff::tags::Tag::TileByteCounts)
264 .unwrap()
265 .into_iter()
266 .next()
267 .unwrap();
268 decoder
269 .inner()
270 .seek(std::io::SeekFrom::Start(first_offset as u64))
271 .expect("Cannot seek to tile offset");
272 let mut buf = vec![0; first_byte_count as usize];
273 decoder
274 .inner()
275 .read_exact(&mut buf)
276 .expect("Cannot read tile data");
277 let raw_sum: u64 = buf.into_iter().map(<u64>::from).sum();
278
279 match decoder.read_chunk(0).unwrap() {
280 DecodingResult::U8(chunk) => {
281 let sum: u64 = chunk.into_iter().map(<u64>::from).sum();
282 assert_eq!(sum, raw_sum);
283 }
284 _ => panic!("Wrong bit depth"),
285 }
286 }
287
288 /* #[test]
289 fn test_tiled_jpeg_rgb_u8() {
290 test_image_sum_u8("tiled-jpeg-rgb-u8.tif", ColorType::RGB(8), 93031606);
291 } */
292
293 #[test]
•294 #[ignore = "skipping, missing testdata"]
295 fn test_tiled_oversize_gray_i8() {
296 test_image_sum_i8("tiled-oversize-gray-i8.tif", ColorType::Gray(8), 1214996);
297 }
298
299 #[test]
•300 #[ignore = "skipping, missing testdata"]
301 fn test_tiled_cmyk_i8() {
302 test_image_sum_i8("tiled-cmyk-i8.tif", ColorType::CMYK(8), 1759101);
303 }
304
305 #[test]
•306 #[ignore = "skipping, missing testdata"]
307 fn test_tiled_incremental() {
308 let file = "tiled-rgb-u8.tif";
309 let expected_type = ColorType::RGB(8);
310 let sums = [
311 188760, 195639, 108148, 81986, 665088, 366140, 705317, 423366, 172033, 324455, 244102,
312 81853, 181258, 247971, 129486, 55600, 565625, 422102, 730888, 379271, 232142, 292549,
313 244045, 86866, 188141, 115036, 150785, 84389, 353170, 459325, 719619, 329594, 278663,
314 220474, 243048, 113563, 189152, 109684, 179391, 122188, 279651, 622093, 724682, 302459,
315 268428, 204499, 224255, 124674, 170668, 121868, 192768, 183367, 378029, 585651, 657712,
316 296790, 241444, 197083, 198429, 134869, 182318, 86034, 203655, 182338, 297255, 601284,
317 633813, 242531, 228578, 206441, 193552, 125412, 181527, 165439, 202531, 159538, 268388,
318 565790, 611382, 272967, 236497, 215154, 158881, 90806, 106114, 182342, 191824, 186138,
319 215174, 393193, 701228, 198866, 227944, 193830, 166330, 49008, 55719, 122820, 197316,
320 161969, 203152, 170986, 624427, 188605, 186187, 111064, 115192, 39538, 48626, 163929,
321 144682, 135796, 194141, 154198, 584125, 180255, 153524, 121433, 132641, 35743, 47798,
322 152343, 162874, 167664, 160175, 133038, 659882, 138339, 166470, 124173, 118929, 51317,
323 45267, 155776, 161331, 161006, 130052, 137618, 337291, 106481, 161999, 127343, 87724,
324 59540, 63907, 155677, 140668, 141523, 108061, 168657, 186482, 98599, 147614, 139963, 90444,
325 56602, 92547, 125644, 134212, 126569, 144153, 179800, 174516, 133969, 129399, 117681,
326 83305, 55075, 110737, 115108, 128572, 128911, 130922, 179986, 143288, 145884, 155856,
327 96683, 94057, 56238, 79649, 71651, 70182, 75010, 77009, 98855, 78979, 74341, 83482, 53403,
328 59842, 30305,
329 ];
330
331 let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
332 let img_file = File::open(path).expect("Cannot find test image!");
333 let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
334 assert_eq!(decoder.colortype().unwrap(), expected_type);
335
336 let tiles = decoder.tile_count().unwrap();
337 assert_eq!(tiles as usize, sums.len());
338
339 for tile in 0..tiles {
340 match decoder.read_chunk(tile).unwrap() {
341 DecodingResult::U8(res) => {
342 let sum: u64 = res.into_iter().map(<u64>::from).sum();
343 assert_eq!(sum, sums[tile as usize]);
344 }
345 _ => panic!("Wrong bit depth"),
346 }
347 }
348 }
349
350 #[test]
•351 #[ignore = "skipping, missing test data"]
352 fn test_planar_rgb_u8() {
353 test_image_sum_u8("planar-rgb-u8.tif", ColorType::RGB(8), 15417630);
354 }
355
356 #[test]
•357 #[ignore = "skipping, missing test data"]
358 fn test_read_planar_bands() {
359 // gdal_translate tiled-rgb-u8.tif planar-rgb-u8.tif -co INTERLEAVE=BAND -co COMPRESS=LZW -co PROFILE=BASELINE
360 let file = "planar-rgb-u8.tif";
361 let expected_type = ColorType::RGB(8);
362
363 let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
364 let img_file = File::open(path).expect("Cannot find test image!");
365 let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
366 assert_eq!(decoder.colortype().unwrap(), expected_type);
367
368 let chunks = decoder.strip_count().unwrap();
369 assert_eq!(chunks as usize, 72);
370
371 // convert -quiet planar-rgb-u8.tif[0] -crop 1x1+0+0 txt:
372 // 0,0: (73,51,30) #49331E srgb(73,51,30)
373
374 // 1st band (red)
375 match decoder.read_chunk(0).unwrap() {
376 DecodingResult::U8(chunk) => {
377 assert_eq!(chunk[0], 73);
378 }
379 _ => panic!("Wrong bit depth"),
380 }
381 // 2nd band (green)
382 match decoder.read_chunk(chunks / 3).unwrap() {
383 DecodingResult::U8(chunk) => {
384 assert_eq!(chunk[0], 51);
385 }
386 _ => panic!("Wrong bit depth"),
387 }
388 // 3rd band (blue)
389 match decoder.read_chunk(chunks / 3 * 2).unwrap() {
390 DecodingResult::U8(chunk) => {
391 assert_eq!(chunk[0], 30);
392 }
393 _ => panic!("Wrong bit depth"),
394 }
395 }
396
397 #[test]
398 fn test_div_zero() {
399 use tiff::{TiffError, TiffFormatError};
400
401 let image = [
402 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,
403 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,
404 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,
405 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,
406 178, 178, 178,
407 ];
408
409 let err = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err();
410
411 match err {
412 TiffError::FormatError(TiffFormatError::StripTileTagConflict) => {}
413 unexpected => panic!("Unexpected error {unexpected}"),
414 }
415 }
416
417 #[test]
418 fn test_too_many_value_bytes() {
419 let image = [
420 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,
421 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,
422 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,
423 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,
424 0, 89, 89, 89, 89, 89, 89, 89, 89, 96, 1, 20, 89, 89, 89, 89, 18,
425 ];
426
427 let error = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err();
428
429 match error {
430 tiff::TiffError::LimitsExceeded => {}
431 unexpected => panic!("Unexpected error {unexpected}"),
432 }
433 }
434
435 #[test]
436 fn fuzzer_testcase5() {
437 let image = [
438 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,
439 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,
440 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,
441 0, 0, 0, 40, 0, 251, 255, 23, 1, 4, 0, 1, 0, 0, 0, 48, 178, 178, 178, 178, 178, 178, 178,
442 178, 178, 178,
443 ];
444
445 let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err();
446 }
447
448 #[test]
449 fn fuzzer_testcase1() {
450 let image = [
451 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,
452 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,
453 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,
454 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,
455 178,
456 ];
457
458 let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err();
459 }
460
461 #[test]
462 fn fuzzer_testcase6() {
463 let image = [
464 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,
465 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,
466 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,
467 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,
468 178, 178,
469 ];
470
471 let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err();
472 }
473
474 #[test]
475 fn oom() {
476 let image = [
477 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,
478 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,
479 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,
480 0, 0, 0, 40, 0, 0, 0, 23, 1, 4, 0, 1, 0, 0, 0, 178, 48, 178, 178, 178, 178, 162, 178,
481 ];
482
483 let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err();
484 }
485
486 #[test]
487 fn fuzzer_testcase4() {
488 let image = [
489 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,
490 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,
491 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,
492 0, 0, 0, 40, 0, 0, 0, 23, 1, 4, 0, 1, 0, 0, 0, 48, 178, 178, 178, 0, 1, 0, 13, 13,
493 ];
494
495 let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err();
496 }
497
498 #[test]
499 fn fuzzer_testcase2() {
500 let image = [
501 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,
502 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,
503 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,
504 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,
505 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,
506 1, 100, 0, 129, 0, 0, 0, 0, 0, 0, 0, 232, 254, 252, 255, 254, 255, 255, 255, 1, 29, 0, 0,
507 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,
508 73,
509 ];
510
511 let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err();
512 }
513
514 #[test]
515 fn invalid_jpeg_tag_2() {
516 let image = [
517 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,
518 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,
519 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,
520 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,
521 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,
522 91, 1, 7, 0, 5, 0, 0, 0, 64, 0, 0, 0, 237, 254, 65, 255, 255, 255, 255, 255, 1, 0, 0, 0,
523 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,
524 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 36, 73, 73, 0, 42, 36, 36, 36, 36, 0, 0, 8, 0,
525 ];
526
527 let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err();
528 }
529
530 #[test]
531 fn fuzzer_testcase3() {
532 let image = [
533 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,
534 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,
535 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,
536 0, 14, 0, 0, 0, 0, 0, 0, 0, 133, 133, 133, 77, 77, 77, 0, 0, 22, 128, 0, 255, 255, 255,
537 255, 255,
538 ];
539
540 let _ = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err();
541 }
542
543 #[test]
544 fn timeout() {
545 use tiff::{TiffError, TiffFormatError};
546
547 let image = [
548 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,
549 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,
550 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,
551 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,
552 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,
553 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,
554 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,
555 0, 0, 73, 73, 42, 0, 8, 0, 0, 0, 0, 0, 32,
556 ];
557
558 let error = tiff::decoder::Decoder::new(std::io::Cursor::new(&image)).unwrap_err();
559
560 match error {
561 TiffError::FormatError(TiffFormatError::CycleInOffsets) => {}
562 e => panic!("Unexpected error {e:?}"),
563 }
564 }
565
566 #[test]
•567 #[ignore = "skipping, missing testdata"]
568 fn test_no_rows_per_strip() {
569 test_image_sum_u8("no_rows_per_strip.tiff", ColorType::RGB(8), 99448840);
570 }
571
572 #[test]
•573 #[ignore = "skipping, missing testdata"]
574 fn test_predictor_3_rgb_f32() {
575 test_image_sum_f32("predictor-3-rgb-f32.tif", ColorType::RGB(32), 54004.33);
576 }
577
578 #[test]
•579 #[ignore = "skipping, missing testdata"]
580 fn test_predictor_3_gray_f32() {
581 test_image_sum_f32("predictor-3-gray-f32.tif", ColorType::Gray(32), 20008.275);
582 }
583
584 #[test]
585 #[cfg(feature = "zstd")]
•586 #[ignore = "skipping, missing testdata"]
587 fn test_zstd_compression() {
588 // gdal_translate -co COMPRESS=ZSTD -co ZSTD_LEVEL=20 int16.tif int16_zstd.tif
589 test_image_sum_i16("int16_zstd.tif", ColorType::Gray(16), 354396);
590 }
591
592 fn test_image_bytes(
593 file: &str,
594 expected: &[(ColorType, u32)],
595 // Represent all samples as little endian, ensuring check sums on all platforms.
596 normalize_byte_order: fn(&mut [u8]),
597 ) {
598 let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
599 let img_file = File::open(path).expect("Cannot find test image!");
600 let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
601
602 let mut buffer = vec![0u8; 0];
603 for (idx, &(expected_type, expected_crc)) in expected.iter().enumerate() {
604 if idx > 0 {
605 decoder
606 .next_image()
607 .expect("mismatched number of directories");
608 }
609
610 assert_eq!(decoder.colortype().unwrap(), expected_type);
611 let layout = decoder
612 .image_buffer_layout()
613 .expect("Cannot describe buffer layout");
614
615 buffer.resize(layout.len, 0u8);
616 decoder.read_image_bytes(&mut buffer).unwrap();
617 normalize_byte_order(&mut buffer);
618
619 // Well almost a crc..
620 assert_eq!(crc32fast::hash(&buffer), expected_crc);
621 }
622 }
623
624 fn byte_order_u16(bytes: &mut [u8]) {
625 for chunk in bytes.chunks_exact_mut(2) {
626 let n = u16::from_ne_bytes(chunk.try_into().unwrap());
627 chunk.copy_from_slice(&n.to_le_bytes());
628 }
629 }
630
631 fn byte_order_u32(bytes: &mut [u8]) {
632 for chunk in bytes.chunks_exact_mut(4) {
633 let n = u32::from_ne_bytes(chunk.try_into().unwrap());
634 chunk.copy_from_slice(&n.to_le_bytes());
635 }
636 }
637
638 fn byte_order_u64(bytes: &mut [u8]) {
639 for chunk in bytes.chunks_exact_mut(8) {
640 let n = u64::from_ne_bytes(chunk.try_into().unwrap());
641 chunk.copy_from_slice(&n.to_le_bytes());
642 }
643 }
644
645 #[test]
•646 #[ignore = "skipping, missing test data"]
647 fn bytes_cmyk_3c_8b() {
648 test_image_bytes(
649 "cmyk-3c-8b.tiff",
650 &[(ColorType::CMYK(8), 2935032230)],
651 |_| (),
652 );
653 }
654
655 #[test]
•656 #[ignore = "skipping, missing test data"]
657 fn bytes_gray_16b() {
658 test_image_bytes(
659 "minisblack-1c-16b.tiff",
660 &[(ColorType::Gray(16), 3524597615)],
661 byte_order_u16,
662 );
663 }
664
665 #[test]
•666 #[ignore = "skipping, missing test data"]
667 fn bytes_gray_32b() {
668 test_image_bytes(
669 "gradient-1c-32b.tiff",
670 &[(ColorType::Gray(32), 2257036530)],
671 byte_order_u32,
672 );
673 }
674
675 #[test]
•676 #[ignore = "skipping, missing test data"]
677 fn bytes_gray_u64() {
678 test_image_bytes(
679 "gradient-1c-64b.tiff",
680 &[(ColorType::Gray(64), 2156547113)],
681 byte_order_u64,
682 );
683 }
684
685 #[test]
•686 #[ignore = "skipping, missing test data"]
687 fn bytes_gray_f32() {
688 test_image_bytes(
689 "gradient-1c-32b-float.tiff",
690 &[(ColorType::Gray(32), 3232356730)],
691 byte_order_u32,
692 );
693 }
694
695 #[test]
696 #[cfg(feature = "fax")]
•697 #[ignore = "skipping, missing test data"]
698 fn test_fax4() {
699 test_image_sum_u8("fax4.tiff", ColorType::Gray(1), 7802706);
700 }
701
702 #[test]
703 #[cfg(feature = "fax")]
•704 #[ignore = "skipping, missing test data"]
705 fn test_fax4_white_is_min() {
706 test_image_sum_u8("imagemagick_group4.tiff", ColorType::Gray(1), 3742820);
707 }
708
709 #[test]
•710 #[ignore = "skipping, missing test data"]
711 fn write_extra_bits_image() {
712 use tiff::{encoder, tags::ExtraSamples};
713
714 let path = PathBuf::from(TEST_IMAGE_DIR).join("extra_bits_rgb_8b.tiff");
715 let img_file = File::create(path).expect("Cannot find test image!");
716
717 let mut encoder = encoder::TiffEncoder::new(img_file).expect("Cannot create encoder");
718 let mut img = encoder.new_image::<encoder::colortype::RGB8>(8, 8).unwrap();
719
720 img.extra_samples(&[ExtraSamples::Unspecified]).unwrap();
721
722 // all pixels are supposed to be 0, the extra is 1
723 let data: [u8; 256] = core::array::from_fn(|i| (i as u8 % 4).saturating_sub(2));
724
725 img.write_data(&data)
726 .expect("correct amount of data to write");
727 }
728
729 #[test]
•730 #[ignore = "skipping, missing test data"]
731 fn extra_bits_gray() {
732 test_image_sum_u8(
733 "extra_bits_gray_8b.tiff",
734 ColorType::Multiband {
735 bit_depth: 8,
736 num_samples: 2,
737 },
738 64,
739 );
740 }
741
742 #[test]
•743 #[ignore = "skipping, missing test data"]
744 fn extra_bits_rgb() {
745 test_image_sum_u8("extra_bits_rgb_8b.tiff", ColorType::RGB(8), 0);
746 }