Debian Codegraph @

tests/decode_planes.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 use tiff::decoder::{Decoder, DecodingResult, Limits, TiffCodingUnit};
2
3 use std::fs::File;
4 use std::path::PathBuf;
5
6 const TEST_IMAGE_DIR: &str = "./tests/images/";
7
8 #[test]
•9 #[ignore = "skipping, missing test data"]
10 fn read_all_planes() {
11 let path = PathBuf::from(TEST_IMAGE_DIR).join("planar-rgb-u8.tif");
12 let file = File::open(path).unwrap();
13 let io = std::io::BufReader::new(file);
14
15 let mut tif = Decoder::new(io).unwrap();
16 let layout = tif.image_buffer_layout().unwrap();
17 assert_eq!(layout.planes, 3);
18 let plane_stride = layout.plane_stride.map_or(0, |x| x.get());
19
20 eprintln!("{:?}", layout);
21 let mut buffer = vec![0u8; layout.complete_len];
22 tif.read_image_bytes(&mut buffer).unwrap();
23
24 // Mainly you can see: these are different and non-zero. Otherwise these magic constants just
25 // depend on the file itself that is being used.
26 const PLANE_EXCPECTED_SUMS: [u32; 3] = [15417630, 13007788, 11103530];
27
28 for (plane, expected) in (0..layout.planes).zip(PLANE_EXCPECTED_SUMS) {
29 let plane_data = &buffer[plane_stride * plane..][..layout.len];
30 let sum: u32 = plane_data.iter().copied().map(u32::from).sum();
31 assert_eq!(sum, expected);
32 }
33 }
34
35 #[test]
•36 #[ignore = "skipping, missing test data"]
37 fn read_first_coding_unit() {
38 let path = PathBuf::from(TEST_IMAGE_DIR).join("planar-rgb-u8.tif");
39 let file = File::open(path).unwrap();
40 let io = std::io::BufReader::new(file);
41
42 let mut tif = Decoder::new(io).unwrap();
43 let layout = tif.image_coding_unit_layout(TiffCodingUnit(0)).unwrap();
44
45 assert_eq!(layout.planes, 3);
46 let plane_stride = layout.plane_stride.map_or(0, |x| x.get());
47 eprintln!("{:?}", layout);
48 let mut buffer = vec![0u8; layout.complete_len];
49
50 struct Expected {
51 plane_sums: [u32; 3],
52 unit: TiffCodingUnit,
53 }
54
55 // Mainly you can see: these are different and non-zero. Otherwise these magic constants just
56 // depend on the file itself that is being used. Also this varies by what unit we are reading.
57 const PLANE_EXCPECTED_SUMS: [Expected; 2] = [
58 Expected {
59 plane_sums: [856853, 767686, 688532],
60 unit: TiffCodingUnit(0),
61 },
62 Expected {
63 plane_sums: [883679, 797329, 711086],
64 unit: TiffCodingUnit(1),
65 },
66 ];
67
68 for expected in PLANE_EXCPECTED_SUMS {
69 tif.read_coding_unit_bytes(expected.unit, &mut buffer)
70 .unwrap();
71
72 for (plane, expected) in (0..layout.planes).zip(expected.plane_sums) {
73 let plane_data = &buffer[plane_stride * plane..][..layout.len];
74 let sum: u32 = plane_data.iter().copied().map(u32::from).sum();
75 assert_eq!(sum, expected);
76 }
77 }
78 }
79
80 #[test]
•81 #[ignore = "skipping, missing test data"]
82 fn read_planes_by_limits() {
83 let path = PathBuf::from(TEST_IMAGE_DIR).join("planar-rgb-u8.tif");
84 let file = File::open(path).unwrap();
85 let io = std::io::BufReader::new(file);
86
87 let mut limits = Limits::default();
88 let mut buffer = DecodingResult::U8(vec![]);
89
90 // Make room for 1 plane.
91 let mut tif = Decoder::new(io).unwrap();
92 tif = tif.with_limits({
93 limits.decoding_buffer_size = 374 * 499;
94 limits.clone()
95 });
96
97 tif.read_image_to_buffer(&mut buffer).unwrap();
98 assert_eq!(buffer.as_buffer(0).byte_len(), 374 * 499);
99
100 // Make room for 2 planes.
101 tif = tif.with_limits({
102 limits.decoding_buffer_size = 374 * 499 * 2;
103 limits.clone()
104 });
105
106 tif.read_image_to_buffer(&mut buffer).unwrap();
107 assert_eq!(buffer.as_buffer(0).byte_len(), 374 * 499);
108
109 // Make room for 3 planes.
110 tif = tif.with_limits({
111 limits.decoding_buffer_size = 374 * 499 * 3;
112 limits.clone()
113 });
114
115 tif.read_image_to_buffer(&mut buffer).unwrap();
116 assert_eq!(buffer.as_buffer(0).byte_len(), 374 * 499 * 3);
117
118 // Make room for 4 planes?
119 tif = tif.with_limits({
120 limits.decoding_buffer_size = 374 * 499 * 4;
121 limits.clone()
122 });
123
124 tif.read_image_to_buffer(&mut buffer).unwrap();
125 assert_eq!(buffer.as_buffer(0).byte_len(), 374 * 499 * 3);
126 }