Debian Codegraph @

tests/decode_planes.rs

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