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