| 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 |
#[ignore] |
| 95 |
#[test] |
| 96 |
fn compare_compression_contents_zip_f32() { |
| 97 |
expect_eq_uncompressed("f32", "zip.exr"); |
| 98 |
} |
| 99 |
|
| •100 |
#[ignore] |
| 101 |
#[test] |
| 102 |
fn compare_compression_contents_zip_f16() { |
| 103 |
expect_eq_uncompressed("f16", "zip.exr"); |
| 104 |
} |
| 105 |
|
| •106 |
#[ignore] |
| 107 |
#[test] |
| 108 |
fn compare_compression_contents_zips_f32() { |
| 109 |
expect_eq_uncompressed("f32", "zips.exr"); |
| 110 |
} |
| 111 |
|
| •112 |
#[ignore] |
| 113 |
#[test] |
| 114 |
fn compare_compression_contents_zips_f16() { |
| 115 |
expect_eq_uncompressed("f16", "zips.exr"); |
| 116 |
} |
| 117 |
|
| •118 |
#[ignore] |
| 119 |
#[test] |
| 120 |
fn compare_compression_contents_b44_f32() { |
| 121 |
expect_eq_uncompressed("f32", "b44.exr"); // f32s are not compressed in b44 and can be compared exactly |
| 122 |
} |
| 123 |
|
| •124 |
#[ignore] |
| 125 |
#[test] |
| 126 |
fn compare_compression_contents_b44_f16() { |
| 127 |
expect_eq_other("f16", "b44.exr", "decompressed_b44.exr"); |
| 128 |
} |
| 129 |
|
| •130 |
#[ignore] |
| 131 |
#[test] |
| 132 |
fn compare_compression_contents_b44a_f32() { |
| 133 |
expect_eq_uncompressed("f32", "b44a.exr"); // f32s are not compressed in b44 and can be compared exactly |
| 134 |
} |
| 135 |
|
| •136 |
#[ignore] |
| 137 |
#[test] |
| 138 |
fn compare_compression_contents_b44a_f16() { |
| 139 |
expect_eq_other("f16", "b44a.exr", "decompressed_b44a.exr"); |
| 140 |
} |
| 141 |
|
| •142 |
#[ignore] |
| 143 |
#[test] |
| 144 |
fn compare_compression_contents_dwaa_f32() { |
| 145 |
expect_eq_other("f32", "dwaa.exr", "decompressed_dwaa.exr"); |
| 146 |
} |
| 147 |
|
| •148 |
#[ignore] |
| 149 |
#[test] |
| 150 |
fn compare_compression_contents_dwaa_f16() { |
| 151 |
expect_eq_other("f16", "dwaa.exr", "decompressed_dwaa.exr"); |
| 152 |
} |
| 153 |
|
| •154 |
#[ignore] |
| 155 |
#[test] |
| 156 |
fn compare_compression_contents_dwab_f32() { |
| 157 |
expect_eq_other("f32", "dwab.exr", "decompressed_dwab.exr"); |
| 158 |
} |
| 159 |
|
| •160 |
#[ignore] |
| 161 |
#[test] |
| 162 |
fn compare_compression_contents_dwab_f16() { |
| 163 |
expect_eq_other("f16", "dwab.exr", "decompressed_dwab.exr"); |
| 164 |
} |
| 165 |
|
| •166 |
#[ignore] |
| 167 |
#[test] |
| 168 |
fn compare_compression_contents_piz_f32() { |
| 169 |
expect_eq_uncompressed("f32", "piz.exr"); |
| 170 |
} |
| 171 |
|
| •172 |
#[ignore] |
| 173 |
#[test] |
| 174 |
fn compare_compression_contents_piz_f16() { |
| 175 |
expect_eq_uncompressed("f16", "piz.exr"); |
| 176 |
} |
| 177 |
|
| •178 |
#[ignore] |
| 179 |
#[test] |
| 180 |
fn compare_compression_contents_rle_f32() { |
| 181 |
expect_eq_uncompressed("f32", "rle.exr"); |
| 182 |
} |
| 183 |
|
| •184 |
#[ignore] |
| 185 |
#[test] |
| 186 |
fn compare_compression_contents_rle_f16() { |
| 187 |
expect_eq_uncompressed("f16", "rle.exr"); |
| 188 |
} |
| 189 |
|
| 190 |
|
| •191 |
#[ignore] |
| 192 |
#[test] |
| 193 |
fn compare_compression_contents_pxr24_f16() { |
| 194 |
expect_eq_other("f16", "pxr24.exr", "decompressed_pxr24.exr"); |
| 195 |
} |
| 196 |
|
| •197 |
#[ignore] |
| 198 |
#[test] |
| 199 |
fn compare_compression_contents_pxr24_f32() { |
| 200 |
expect_eq_other("f32", "pxr24.exr", "decompressed_pxr24.exr"); |
| 201 |
} |
| 202 |
|
| •203 |
#[ignore] |
| 204 |
#[test] |
| 205 |
fn compare_png_to_uncompressed_f16() { |
| 206 |
expect_eq_png("f16_uncompressed.exr"); |
| 207 |
} |
| 208 |
|
| •209 |
#[ignore] |
| 210 |
#[test] |
| 211 |
fn compare_png_to_piz_f16() { |
| 212 |
expect_eq_png("f16_piz.exr"); |
| 213 |
} |
| 214 |
|
| •215 |
#[ignore] |
| 216 |
#[test] |
| 217 |
fn compare_png_to_rle_f16() { |
| 218 |
expect_eq_png("f16_rle.exr"); |
| 219 |
} |
| 220 |
|
| •221 |
#[ignore] |
| 222 |
#[test] |
| 223 |
fn compare_png_to_zip_f16() { |
| 224 |
expect_eq_png("f16_zip.exr"); |
| 225 |
} |
| 226 |
|
| •227 |
#[ignore] |
| 228 |
#[test] |
| 229 |
fn compare_png_to_zips_f16() { |
| 230 |
expect_eq_png("f16_zips.exr"); |
| 231 |
} |
| 232 |
|
| •233 |
#[ignore] |
| 234 |
#[test] |
| 235 |
fn compare_png_to_dwaa_f16() { |
| 236 |
expect_eq_png("f16_dwaa.exr"); |
| 237 |
} |
| 238 |
|
| •239 |
#[ignore] |
| 240 |
#[test] |
| 241 |
fn compare_png_to_b44_f16() { |
| 242 |
expect_eq_png("f16_b44.exr"); |
| 243 |
} |
| 244 |
|
| •245 |
#[ignore] |
| 246 |
#[test] |
| 247 |
fn compare_png_to_b44a_f16() { |
| 248 |
expect_eq_png("f16_b44a.exr"); |
| 249 |
} |
| 250 |
|
| •251 |
#[ignore] |
| 252 |
#[test] |
| 253 |
fn compare_png_to_pxr24_f16() { |
| 254 |
expect_eq_png("f16_pxr24.exr"); |
| 255 |
} |
| 256 |
|
| 257 |
|
| •258 |
#[ignore] |
| 259 |
#[test] |
| 260 |
fn compare_png_to_uncompressed_f32() { |
| 261 |
expect_eq_png("f32_uncompressed.exr"); |
| 262 |
} |
| 263 |
|
| •264 |
#[ignore] |
| 265 |
#[test] |
| 266 |
fn compare_png_to_piz_f32() { |
| 267 |
expect_eq_png("f32_piz.exr"); |
| 268 |
} |
| 269 |
|
| •270 |
#[ignore] |
| 271 |
#[test] |
| 272 |
fn compare_png_to_rle_f32() { |
| 273 |
expect_eq_png("f32_rle.exr"); |
| 274 |
} |
| 275 |
|
| •276 |
#[ignore] |
| 277 |
#[test] |
| 278 |
fn compare_png_to_zip_f32() { |
| 279 |
expect_eq_png("f32_zip.exr"); |
| 280 |
} |
| 281 |
|
| •282 |
#[ignore] |
| 283 |
#[test] |
| 284 |
fn compare_png_to_dwaa_f32() { |
| 285 |
expect_eq_png("f32_dwaa.exr"); |
| 286 |
} |
| 287 |
|
| •288 |
#[ignore] |
| 289 |
#[test] |
| 290 |
fn compare_png_to_pxr24_f32() { |
| 291 |
expect_eq_png("f32_pxr24.exr"); |
| 292 |
} |