Debian Codegraph @

tests/decode_fp16_images.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 extern crate tiff;
2
3 use tiff::decoder::{Decoder, DecodingResult};
4 use tiff::ColorType;
5
6 use std::fs::File;
7 use std::path::PathBuf;
8
9 const TEST_IMAGE_DIR: &str = "./tests/images/";
10
•11 /*
12 /// Test a basic all white image
13 #[test]
14 fn test_white_ieee_fp16() {
15 let filenames = ["white-fp16.tiff"];
16
17 for filename in filenames.iter() {
18 let path = PathBuf::from(TEST_IMAGE_DIR).join(filename);
19 let img_file = File::open(path).expect("Cannot find test image!");
20 let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
21 assert_eq!(
22 decoder.dimensions().expect("Cannot get dimensions"),
23 (256, 256)
24 );
25 assert_eq!(
26 decoder.colortype().expect("Cannot get colortype"),
27 ColorType::Gray(16)
28 );
29 if let DecodingResult::F16(img) = decoder.read_image().unwrap() {
30 for p in img {
31 assert!(p == half::f16::from_f32_const(1.0));
32 }
33 } else {
34 panic!("Wrong data type");
35 }
36 }
37 }
38
39 /// Test a single black pixel, to make sure scaling is ok
40 #[test]
41 fn test_one_black_pixel_ieee_fp16() {
42 let filenames = ["single-black-fp16.tiff"];
43
44 for filename in filenames.iter() {
45 let path = PathBuf::from(TEST_IMAGE_DIR).join(filename);
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!(
49 decoder.dimensions().expect("Cannot get dimensions"),
50 (256, 256)
51 );
52 assert_eq!(
53 decoder.colortype().expect("Cannot get colortype"),
54 ColorType::Gray(16)
55 );
56 if let DecodingResult::F16(img) = decoder.read_image().unwrap() {
57 for (i, p) in img.iter().enumerate() {
58 if i == 0 {
59 assert!(p < &half::f16::from_f32_const(0.001));
60 } else {
61 assert!(p == &half::f16::from_f32_const(1.0));
62 }
63 }
64 } else {
65 panic!("Wrong data type");
66 }
67 }
68 }
69
70 /// Test white with horizontal differencing predictor
71 #[test]
72 fn test_pattern_horizontal_differencing_ieee_fp16() {
73 let filenames = ["white-fp16-pred2.tiff"];
74
75 for filename in filenames.iter() {
76 let path = PathBuf::from(TEST_IMAGE_DIR).join(filename);
77 let img_file = File::open(path).expect("Cannot find test image!");
78 let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
79 assert_eq!(
80 decoder.dimensions().expect("Cannot get dimensions"),
81 (256, 256)
82 );
83 assert_eq!(
84 decoder.colortype().expect("Cannot get colortype"),
85 ColorType::Gray(16)
86 );
87 if let DecodingResult::F16(img) = decoder.read_image().unwrap() {
88 // 0, 2, 5, 8, 12, 16, 255 are black
89 let black = [0, 2, 5, 8, 12, 16, 255];
90 for (i, p) in img.iter().enumerate() {
91 if black.contains(&i) {
92 assert!(p < &half::f16::from_f32_const(0.001));
93 } else {
94 assert!(p == &half::f16::from_f32_const(1.0));
95 }
96 }
97 } else {
98 panic!("Wrong data type");
99 }
100 }
101 }
102
103 /// Test white with floating point predictor
104 #[test]
105 fn test_pattern_predictor_ieee_fp16() {
106 let filenames = ["white-fp16-pred3.tiff"];
107
108 for filename in filenames.iter() {
109 let path = PathBuf::from(TEST_IMAGE_DIR).join(filename);
110 let img_file = File::open(path).expect("Cannot find test image!");
111 let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
112 assert_eq!(
113 decoder.dimensions().expect("Cannot get dimensions"),
114 (256, 256)
115 );
116 assert_eq!(
117 decoder.colortype().expect("Cannot get colortype"),
118 ColorType::Gray(16)
119 );
120 if let DecodingResult::F16(img) = decoder.read_image().unwrap() {
121 // 0, 2, 5, 8, 12, 16, 255 are black
122 let black = [0, 2, 5, 8, 12, 16, 255];
123 for (i, p) in img.iter().enumerate() {
124 if black.contains(&i) {
125 assert!(p < &half::f16::from_f32_const(0.001));
126 } else {
127 assert!(p == &half::f16::from_f32_const(1.0));
128 }
129 }
130 } else {
131 panic!("Wrong data type");
132 }
133 }
134 }
135
136 /// Test several random images
137 /// we'rell compare against a pnm file, that scales from 0 (for 0.0) to 65767 (for 1.0)
138 #[test]
139 fn test_predictor_ieee_fp16() {
140 // first parse pnm, skip the first 4 \n
141 let pnm_path = PathBuf::from(TEST_IMAGE_DIR).join("random-fp16.pgm");
142 let pnm_bytes = std::fs::read(pnm_path).expect("Failed to read expected PNM file");
143
144 // PGM looks like this:
145 // ---
146 // P5
147 // #Created with GIMP
148 // 16 16
149 // 65535
150 // ... <big-endian bytes>
151 // ---
152 // get index of 4th \n
153 let byte_start = pnm_bytes
154 .iter()
155 .enumerate()
156 .filter(|(_, &v)| v == b'\n')
157 .map(|(i, _)| i)
158 .nth(3)
159 .expect("Must be 4 \\n's");
160
161 let pnm_values: Vec<f32> = pnm_bytes[(byte_start + 1)..]
162 .chunks(2)
163 .map(|slice| {
164 let bts = [slice[0], slice[1]];
165 (u16::from_be_bytes(bts) as f32) / (u16::MAX as f32)
166 })
167 .collect();
168 assert!(pnm_values.len() == 256);
169
170 let filenames = [
171 "random-fp16-pred2.tiff",
172 "random-fp16-pred3.tiff",
173 "random-fp16.tiff",
174 ];
175
176 for filename in filenames.iter() {
177 let path = PathBuf::from(TEST_IMAGE_DIR).join(filename);
178 let img_file = File::open(path).expect("Cannot find test image!");
179 let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
180 assert_eq!(
181 decoder.dimensions().expect("Cannot get dimensions"),
182 (16, 16)
183 );
184 assert_eq!(
185 decoder.colortype().expect("Cannot get colortype"),
186 ColorType::Gray(16)
187 );
188 if let DecodingResult::F16(img) = decoder.read_image().unwrap() {
189 for (exp, found) in std::iter::zip(pnm_values.iter(), img.iter()) {
190 assert!((exp - found.to_f32()).abs() < 0.0001);
191 }
192 } else {
193 panic!("Wrong data type");
194 }
195 }
196 }
•197 */