Debian Codegraph @

.cargo/vendor/exr/tests/across_compression.rs

raw
1 use std::path::Path;
2 use exr::prelude::*;
3 use exr::image::validate_results::ValidateResult;
4
5 fn dir() -> &'static Path { Path::new("tests/images/valid/custom/compression_methods") }
6
7 fn expect_eq_other(sub_dir: &str, image_name: &str, expected: &str) {
8 let path = dir().join(sub_dir).join(image_name);
9
10 match read_first_flat_layer_from_file(path) {
11 Err(Error::NotSupported(message)) => println!("skipping ({})", message),
12 Err(error) => panic!("unexpected error: {}", error),
13 Ok(mut decompressed) => {
14 let decompressed_path = dir().join(sub_dir).join(expected);
15 let mut expected_decompressed = read_first_flat_layer_from_file(decompressed_path)
16 .expect("uncompressed image could not be loaded");
17
18 // HACK: make metadata match artificially, to avoid failing the check due to meta data mismatch
19 // (the name of the compression methods should not be equal, as we test between compression methods)
20 expected_decompressed.layer_data.encoding.compression = Compression::Uncompressed;
21 decompressed.layer_data.encoding.compression = Compression::Uncompressed;
22
23 debug_assert_eq!(expected_decompressed.layer_data.attributes, decompressed.layer_data.attributes, "attributes should not be affected by compression");
24 debug_assert_eq!(expected_decompressed.layer_data.size, decompressed.layer_data.size, "size should not be affected by compression");
25
26 // Note: Unimplemented methods may still work, if each compressed tile would be larger than uncompressed.
27 expected_decompressed.assert_equals_result(&decompressed);
28 }
29 }
30 }
31
32 // comparing to a different format, png,
33 // is the only real way to check that
34 // little endian data is unpacked correctly on big endian systems
35 // it does not attempt to compare NaN
36 fn expect_eq_png(image_name: &str) {
37 type Rgb16Image = ::image::ImageBuffer<::image::Rgb<u16>, Vec<u16>>;
38
39 let exr_path = dir().join("u16").join(image_name);
40 let png_from_exr = read_first_rgba_layer_from_file(
41 exr_path,
42
43 |resolution, _channels: &RgbaChannels| -> Rgb16Image {
44 ::image::ImageBuffer::new(
45 resolution.width() as u32,
46 resolution.height() as u32
47 )
48 },
49
50 // set each pixel in the png buffer from the exr file
51 |png_pixels: &mut Rgb16Image, position: Vec2<usize>, (r,g,b,_): (f32,f32,f32,f32)| {
52 png_pixels.put_pixel(
53 position.x() as u32, position.y() as u32,
54 ::image::Rgb([to_u16(r), to_u16(g), to_u16(b)])
55 );
56 }
57 );
58
59 fn to_u16(num: f32) -> u16 { (num.powf(1.0/2.14).clamp(0.0, 1.0) * u16::MAX as f32).round() as u16 }
60
61 match png_from_exr {
62 Err(Error::NotSupported(message)) => println!("skipping ({})", message),
63 Err(error) => panic!("unexpected error: {}", error),
64 Ok(decompressed) => {
65 let truth_path = dir().join("u16").join("ground_truth.png");
66 let truth_dyn_img = image::open(truth_path).unwrap();
67
68 let ground_truth_png = truth_dyn_img.to_rgb16();
69 let exr_as_png_px = decompressed.layer_data.channel_data.pixels;
70 debug_assert_eq!(ground_truth_png.dimensions(), exr_as_png_px.dimensions(), "size should not be affected by compression");
71
72 let expected_px = ground_truth_png.pixels()
73 .flat_map(|px| px.0.iter().copied());
74
75 let actual_px = exr_as_png_px.pixels()
76 .flat_map(|px| px.0.iter().copied());
77
78 let max_diff = u16::MAX/10;
79 for (exp, val) in expected_px.zip(actual_px) {
80 assert!(
81 exp.abs_diff(val) < max_diff,
82 "values not similar enough: found {}, expected {}", val, exp
83 );
84 }
85 }
86 }
87 }
88
89 fn expect_eq_uncompressed(sub_dir: &str, image_name: &str) {
90 expect_eq_other(sub_dir, image_name, "uncompressed.exr")
91 }
92
93
94 #[test]
95 fn compare_compression_contents_zip_f32() {
96 expect_eq_uncompressed("f32", "zip.exr");
97 }
98
99 #[test]
100 fn compare_compression_contents_zip_f16() {
101 expect_eq_uncompressed("f16", "zip.exr");
102 }
103
104 #[test]
105 fn compare_compression_contents_zips_f32() {
106 expect_eq_uncompressed("f32", "zips.exr");
107 }
108
109 #[test]
110 fn compare_compression_contents_zips_f16() {
111 expect_eq_uncompressed("f16", "zips.exr");
112 }
113
114 #[test]
115 fn compare_compression_contents_b44_f32() {
116 expect_eq_uncompressed("f32", "b44.exr"); // f32s are not compressed in b44 and can be compared exactly
117 }
118
119 #[test]
120 fn compare_compression_contents_b44_f16() {
121 expect_eq_other("f16", "b44.exr", "decompressed_b44.exr");
122 }
123
124 #[test]
125 fn compare_compression_contents_b44a_f32() {
126 expect_eq_uncompressed("f32", "b44a.exr"); // f32s are not compressed in b44 and can be compared exactly
127 }
128
129 #[test]
130 fn compare_compression_contents_b44a_f16() {
131 expect_eq_other("f16", "b44a.exr", "decompressed_b44a.exr");
132 }
133
134 #[test]
135 fn compare_compression_contents_dwaa_f32() {
136 expect_eq_other("f32", "dwaa.exr", "decompressed_dwaa.exr");
137 }
138
139 #[test]
140 fn compare_compression_contents_dwaa_f16() {
141 expect_eq_other("f16", "dwaa.exr", "decompressed_dwaa.exr");
142 }
143
144 #[test]
145 fn compare_compression_contents_dwab_f32() {
146 expect_eq_other("f32", "dwab.exr", "decompressed_dwab.exr");
147 }
148
149 #[test]
150 fn compare_compression_contents_dwab_f16() {
151 expect_eq_other("f16", "dwab.exr", "decompressed_dwab.exr");
152 }
153
154 #[test]
155 fn compare_compression_contents_piz_f32() {
156 expect_eq_uncompressed("f32", "piz.exr");
157 }
158
159 #[test]
160 fn compare_compression_contents_piz_f16() {
161 expect_eq_uncompressed("f16", "piz.exr");
162 }
163
164 #[test]
165 fn compare_compression_contents_rle_f32() {
166 expect_eq_uncompressed("f32", "rle.exr");
167 }
168
169 #[test]
170 fn compare_compression_contents_rle_f16() {
171 expect_eq_uncompressed("f16", "rle.exr");
172 }
173
174
175 #[test]
176 fn compare_compression_contents_pxr24_f16() {
177 expect_eq_other("f16", "pxr24.exr", "decompressed_pxr24.exr");
178 }
179
180 #[test]
181 fn compare_compression_contents_pxr24_f32() {
182 expect_eq_other("f32", "pxr24.exr", "decompressed_pxr24.exr");
183 }
184
185 #[test]
186 fn compare_png_to_uncompressed_f16() {
187 expect_eq_png("f16_uncompressed.exr");
188 }
189
190 #[test]
191 fn compare_png_to_piz_f16() {
192 expect_eq_png("f16_piz.exr");
193 }
194
195 #[test]
196 fn compare_png_to_rle_f16() {
197 expect_eq_png("f16_rle.exr");
198 }
199
200 #[test]
201 fn compare_png_to_zip_f16() {
202 expect_eq_png("f16_zip.exr");
203 }
204
205 #[test]
206 fn compare_png_to_zips_f16() {
207 expect_eq_png("f16_zips.exr");
208 }
209
210 #[test]
211 fn compare_png_to_dwaa_f16() {
212 expect_eq_png("f16_dwaa.exr");
213 }
214
215 #[test]
216 fn compare_png_to_b44_f16() {
217 expect_eq_png("f16_b44.exr");
218 }
219
220 #[test]
221 fn compare_png_to_b44a_f16() {
222 expect_eq_png("f16_b44a.exr");
223 }
224
225 #[test]
226 fn compare_png_to_pxr24_f16() {
227 expect_eq_png("f16_pxr24.exr");
228 }
229
230
231 #[test]
232 fn compare_png_to_uncompressed_f32() {
233 expect_eq_png("f32_uncompressed.exr");
234 }
235
236 #[test]
237 fn compare_png_to_piz_f32() {
238 expect_eq_png("f32_piz.exr");
239 }
240
241 #[test]
242 fn compare_png_to_rle_f32() {
243 expect_eq_png("f32_rle.exr");
244 }
245
246 #[test]
247 fn compare_png_to_zip_f32() {
248 expect_eq_png("f32_zip.exr");
249 }
250
251 #[test]
252 fn compare_png_to_dwaa_f32() {
253 expect_eq_png("f32_dwaa.exr");
254 }
255
256 #[test]
257 fn compare_png_to_pxr24_f32() {
258 expect_eq_png("f32_pxr24.exr");
259 }