| 1 |
extern crate exr; |
| 2 |
|
| 3 |
extern crate smallvec; |
| 4 |
|
| 5 |
use std::{panic}; |
| 6 |
use std::io::{Cursor}; |
| 7 |
use std::panic::catch_unwind; |
| 8 |
use std::path::{PathBuf, Path}; |
| 9 |
use std::ffi::OsStr; |
| 10 |
|
| 11 |
use exr::prelude::*; |
| 12 |
use exr::error::{Error, UnitResult}; |
| 13 |
use exr::prelude::pixel_vec::PixelVec; |
| 14 |
use exr::image::validate_results::ValidateResult; |
| 15 |
use rayon::prelude::IntoParallelIterator; |
| 16 |
use rayon::iter::ParallelIterator; |
| 17 |
use exr::block::samples::IntoNativeSample; |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn roundtrip_all_files_in_repository_x4(){ |
| 21 |
check_all_files_in_repo(|path|{ |
| 22 |
let file = std::fs::read(path).expect("cannot open file"); |
| 23 |
|
| 24 |
round_trip_simple(&file)?; |
| 25 |
round_trip_full(&file)?; |
| 26 |
round_trip_rgba_file(path, &file)?; |
| 27 |
round_trip_parallel_file(&file)?; |
| 28 |
|
| 29 |
Ok(()) |
| 30 |
}); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
fn round_trip_full(file: &[u8]) -> Result<()> { |
| 35 |
let read_image = read() |
| 36 |
.no_deep_data().all_resolution_levels().all_channels().all_layers().all_attributes() |
| 37 |
.non_parallel(); |
| 38 |
|
| 39 |
let image = read_image.clone().from_buffered(Cursor::new(file))?; |
| 40 |
|
| 41 |
let mut tmp_bytes = Vec::with_capacity(file.len()); |
| 42 |
image.write().non_parallel().to_buffered(Cursor::new(&mut tmp_bytes))?; |
| 43 |
|
| 44 |
let image2 = read_image.from_buffered(Cursor::new(tmp_bytes))?; |
| 45 |
|
| 46 |
image.assert_equals_result(&image2); |
| 47 |
Ok(()) |
| 48 |
} |
| 49 |
|
| 50 |
fn round_trip_simple(file: &[u8]) -> Result<()> { |
| 51 |
let read_image = read() |
| 52 |
.no_deep_data().largest_resolution_level().all_channels().all_layers().all_attributes() |
| 53 |
.non_parallel(); |
| 54 |
|
| 55 |
let image = read_image.clone().from_buffered(Cursor::new(file))?; |
| 56 |
|
| 57 |
let mut tmp_bytes = Vec::with_capacity(file.len()); |
| 58 |
image.write().non_parallel().to_buffered(&mut Cursor::new(&mut tmp_bytes))?; |
| 59 |
|
| 60 |
let image2 = read_image.from_buffered(Cursor::new(&tmp_bytes))?; |
| 61 |
|
| 62 |
image.assert_equals_result(&image2); |
| 63 |
Ok(()) |
| 64 |
} |
| 65 |
|
| 66 |
fn round_trip_rgba_file(path: &Path, file: &[u8]) -> Result<()> { |
| 67 |
// these files are known to be invalid, because they do not contain any rgb channels |
| 68 |
let blacklist = [ |
| 69 |
Path::new("tests/images/valid/openexr/LuminanceChroma/Garden.exr"), |
| 70 |
Path::new("tests/images/valid/openexr/MultiView/Fog.exr"), |
| 71 |
Path::new("tests/images/valid/openexr/TestImages/GrayRampsDiagonal.exr"), |
| 72 |
Path::new("tests/images/valid/openexr/TestImages/GrayRampsHorizontal.exr"), |
| 73 |
Path::new("tests/images/valid/openexr/TestImages/WideFloatRange.exr"), |
| 74 |
Path::new("tests/images/valid/openexr/IlmfmlmflmTest/v1.7.test.tiled.exr") |
| 75 |
]; |
| 76 |
|
| 77 |
if blacklist.contains(&path) { return Ok(()) } |
| 78 |
|
| 79 |
let image_reader = read() |
| 80 |
.no_deep_data() |
| 81 |
.largest_resolution_level() // TODO all levels |
| 82 |
.rgba_channels(PixelVec::<(f32,f32,f32,f32)>::constructor, PixelVec::set_pixel) |
| 83 |
.first_valid_layer() |
| 84 |
.all_attributes() |
| 85 |
.non_parallel(); |
| 86 |
|
| 87 |
let image = image_reader.clone().from_buffered(Cursor::new(file))?; |
| 88 |
|
| 89 |
let mut tmp_bytes = Vec::with_capacity(file.len()); |
| 90 |
|
| 91 |
image.write().non_parallel() |
| 92 |
.to_buffered(&mut Cursor::new(&mut tmp_bytes))?; |
| 93 |
|
| 94 |
let image2 = image_reader.from_buffered(Cursor::new(&tmp_bytes))?; |
| 95 |
|
| 96 |
image.assert_equals_result(&image2); |
| 97 |
Ok(()) |
| 98 |
} |
| 99 |
|
| 100 |
// TODO compare rgba vs rgb images for color content, and rgb vs rgb(a?) |
| 101 |
|
| 102 |
|
| 103 |
fn round_trip_parallel_file(file: &[u8]) -> Result<()> { |
| 104 |
let image = read() |
| 105 |
.no_deep_data().all_resolution_levels().all_channels().all_layers().all_attributes() |
| 106 |
.from_buffered(Cursor::new(file))?; |
| 107 |
|
| 108 |
let mut tmp_bytes = Vec::with_capacity(file.len()); |
| 109 |
image.write().to_buffered(Cursor::new(&mut tmp_bytes))?; |
| 110 |
|
| 111 |
let image2 = read() |
| 112 |
.no_deep_data().all_resolution_levels().all_channels().all_layers().all_attributes() |
| 113 |
.pedantic() |
| 114 |
.from_buffered(Cursor::new(tmp_bytes.as_slice()))?; |
| 115 |
|
| 116 |
image.assert_equals_result(&image2); |
| 117 |
Ok(()) |
| 118 |
} |
| 119 |
|
| 120 |
/// read all images in a directory. |
| 121 |
/// does not check any content, just checks whether a read error or panic happened. |
| 122 |
fn check_all_files_in_repo<T>( |
| 123 |
operation: impl Sync + std::panic::RefUnwindSafe + Fn(&Path) -> exr::error::Result<T> |
| 124 |
) { |
| 125 |
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)] |
| 126 |
enum Result { Ok, Unsupported(String), Error(String) } |
| 127 |
|
| 128 |
let files: Vec<PathBuf> = all_exr_files_in_repo().collect(); |
| 129 |
let mut results: Vec<(PathBuf, Result)> = files.into_par_iter() |
| 130 |
.map(|file| { |
| 131 |
let result = catch_unwind(||{ |
| 132 |
let prev_hook = panic::take_hook(); |
| 133 |
panic::set_hook(Box::new(|_| (/* do not println panics */))); |
| 134 |
let result = operation(&file); |
| 135 |
panic::set_hook(prev_hook); |
| 136 |
|
| 137 |
result |
| 138 |
}); |
| 139 |
|
| 140 |
let result = match result { |
| 141 |
Ok(Ok(_)) => Result::Ok, |
| 142 |
Ok(Err(Error::NotSupported(message))) => Result::Unsupported(message.to_string()), |
| 143 |
|
| 144 |
Ok(Err(Error::Io(io))) => Result::Error(format!("IoError: {:?}", io)), |
| 145 |
Ok(Err(Error::Invalid(message))) => Result::Error(format!("Invalid: {:?}", message)), |
| 146 |
Ok(Err(Error::Aborted)) => panic!("a test produced `Error::Abort`"), |
| 147 |
|
| 148 |
Err(_) => Result::Error("Panic".to_owned()), |
| 149 |
}; |
| 150 |
|
| 151 |
match &result { |
| 152 |
Result::Error(_) => println!("✗ Error when processing {:?}", file), |
| 153 |
_ => println!("✓ No error when processing {:?}", file) |
| 154 |
}; |
| 155 |
|
| 156 |
(file, result) |
| 157 |
}) |
| 158 |
.collect(); |
| 159 |
|
| 160 |
results.sort_by(|(_, a), (_, b)| a.cmp(b)); |
| 161 |
|
| 162 |
println!("{:#?}", results.iter().map(|(path, result)| { |
| 163 |
format!("{:?}: {}", result, path.to_str().unwrap()) |
| 164 |
}).collect::<Vec<_>>()); |
| 165 |
|
| 166 |
assert!(results.len() > 80, "Not enough files were tested!"); |
| 167 |
|
| 168 |
if let Result::Error(_) = results.last().unwrap().1 { |
| 169 |
panic!("A file triggered a panic"); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
fn all_exr_files_in_repo() -> impl Iterator<Item=PathBuf> { |
| 174 |
walkdir::WalkDir::new("tests/images/valid").into_iter().map(std::result::Result::unwrap) |
| 175 |
.filter(|entry| entry.path().extension() == Some(OsStr::new("exr"))) |
| 176 |
.map(walkdir::DirEntry::into_path) |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
fn roundtrip_unusual_2() -> UnitResult { |
| 181 |
let random_pixels: Vec<(f16, u32)> = vec![ |
| 182 |
( f16::from_f32(-5.0), 4), |
| 183 |
( f16::from_f32(4.0), 9), |
| 184 |
( f16::from_f32(2.0), 6), |
| 185 |
( f16::from_f32(21.0), 8), |
| 186 |
( f16::from_f32(64.0), 7), |
| 187 |
]; |
| 188 |
|
| 189 |
let size = Vec2(3, 2); |
| 190 |
let pixels = (0..size.area()) |
| 191 |
.zip(random_pixels.into_iter().cycle()) |
| 192 |
.map(|(_index, color)| color).collect::<Vec<_>>(); |
| 193 |
|
| 194 |
let pixels = PixelVec { resolution: size, pixels }; |
| 195 |
|
| 196 |
let channels = SpecificChannels::build() |
| 197 |
.with_channel("N") |
| 198 |
.with_channel("Ploppalori Taranos") |
| 199 |
.with_pixels(pixels.clone() |
| 200 |
); |
| 201 |
|
| 202 |
let image = Image::from_channels(size, channels); |
| 203 |
|
| 204 |
let mut tmp_bytes = Vec::new(); |
| 205 |
image.write().non_parallel().to_buffered(&mut Cursor::new(&mut tmp_bytes))?; |
| 206 |
|
| 207 |
let image_reader = read() |
| 208 |
.no_deep_data() |
| 209 |
.largest_resolution_level() // TODO all levels |
| 210 |
.specific_channels().required("N").required("Ploppalori Taranos") |
| 211 |
.collect_pixels(PixelVec::<(f16,u32)>::constructor, PixelVec::set_pixel) |
| 212 |
.first_valid_layer() |
| 213 |
.all_attributes() |
| 214 |
.non_parallel(); |
| 215 |
|
| 216 |
let image2 = image_reader.from_buffered(Cursor::new(&tmp_bytes))?; |
| 217 |
|
| 218 |
// custom compare function: considers nan equal to nan |
| 219 |
assert_eq!(image.layer_data.size, size, "test is buggy"); |
| 220 |
let pixels1 = &image.layer_data.channel_data.pixels; |
| 221 |
let pixels2 = &image2.layer_data.channel_data.pixels; |
| 222 |
|
| 223 |
assert_eq!(pixels1.pixels, pixels2.pixels); |
| 224 |
|
| 225 |
Ok(()) |
| 226 |
} |
| 227 |
|
| 228 |
// TODO test optional reader |
| 229 |
// TODO dedup |
| 230 |
#[test] |
| 231 |
fn roundtrip_unusual_7() -> UnitResult { |
| 232 |
|
| 233 |
let random_pixels: Vec<(f16, u32, f32,f32,f32,f32,f32)> = vec![ |
| 234 |
( f16::from_f32(-5.0), 4, 1.0,2.0,3.0,4.0,5.0), |
| 235 |
( f16::from_f32(4.0), 8, 2.0,3.0,4.0,5.0,1.0), |
| 236 |
( f16::from_f32(2.0), 9, 3.0,4.0,5.0,1.0,2.0), |
| 237 |
( f16::from_f32(21.0), 6, 4.0,5.0,1.0,2.0,3.0), |
| 238 |
( f16::from_f32(64.0), 5, 5.0,1.0,2.0,3.0,4.0), |
| 239 |
]; |
| 240 |
|
| 241 |
let size = Vec2(3, 2); |
| 242 |
let pixels = (0..size.area()) |
| 243 |
.zip(random_pixels.into_iter().cycle()) |
| 244 |
.map(|(_index, color)| color).collect::<Vec<_>>(); |
| 245 |
|
| 246 |
let pixels = PixelVec { resolution: size, pixels }; |
| 247 |
|
| 248 |
let channels = SpecificChannels::build() |
| 249 |
.with_channel("N") |
| 250 |
.with_channel("Ploppalori Taranos") |
| 251 |
.with_channel("4") |
| 252 |
.with_channel(".") |
| 253 |
.with_channel("____") |
| 254 |
.with_channel(" ") |
| 255 |
.with_channel(" ") |
| 256 |
.with_pixels(pixels.clone() |
| 257 |
); |
| 258 |
|
| 259 |
let image = Image::from_channels(size, channels); |
| 260 |
|
| 261 |
let mut tmp_bytes = Vec::new(); |
| 262 |
image.write().non_parallel().to_buffered(&mut Cursor::new(&mut tmp_bytes))?; |
| 263 |
|
| 264 |
let image_reader = read() |
| 265 |
.no_deep_data() |
| 266 |
.largest_resolution_level() // TODO all levels |
| 267 |
.specific_channels() |
| 268 |
.required("N") |
| 269 |
.required("Ploppalori Taranos") |
| 270 |
.required("4") |
| 271 |
.required(".") |
| 272 |
.required("____") |
| 273 |
.required(" ") |
| 274 |
.required(" ") |
| 275 |
.collect_pixels(PixelVec::<(f16, u32, f32,f32,f32,f32,f32)>::constructor, PixelVec::set_pixel) |
| 276 |
.first_valid_layer() |
| 277 |
.all_attributes() |
| 278 |
.non_parallel(); |
| 279 |
|
| 280 |
let image2 = image_reader.from_buffered(Cursor::new(&tmp_bytes))?; |
| 281 |
|
| 282 |
// custom compare function: considers nan equal to nan |
| 283 |
assert_eq!(image.layer_data.size, size, "test is buggy"); |
| 284 |
let pixels1 = &image.layer_data.channel_data.pixels; |
| 285 |
let pixels2 = &image2.layer_data.channel_data.pixels; |
| 286 |
|
| 287 |
assert_eq!(pixels1.pixels, pixels2.pixels); |
| 288 |
Ok(()) |
| 289 |
} |
| 290 |
|
| 291 |
#[test] |
| 292 |
fn roundtrip_pxr24() { |
| 293 |
test_mixed_roundtrip_with_compression(Compression::PXR24) |
| 294 |
} |
| 295 |
|
| 296 |
#[test] |
| 297 |
fn roundtrip_rle() { |
| 298 |
test_mixed_roundtrip_with_compression(Compression::RLE) |
| 299 |
} |
| 300 |
|
| 301 |
#[test] |
| 302 |
fn roundtrip_zip1() { |
| 303 |
test_mixed_roundtrip_with_compression(Compression::ZIP1) |
| 304 |
} |
| 305 |
|
| 306 |
#[test] |
| 307 |
fn roundtrip_zip16() { |
| 308 |
test_mixed_roundtrip_with_compression(Compression::ZIP16) |
| 309 |
} |
| 310 |
|
| 311 |
#[test] |
| 312 |
fn roundtrip_b44() { |
| 313 |
test_mixed_roundtrip_with_compression(Compression::B44) |
| 314 |
} |
| 315 |
|
| 316 |
#[test] |
| 317 |
fn roundtrip_b44a() { |
| 318 |
test_mixed_roundtrip_with_compression(Compression::B44A) |
| 319 |
} |
| 320 |
|
| 321 |
#[test] |
| 322 |
fn roundtrip_piz() { |
| 323 |
test_mixed_roundtrip_with_compression(Compression::PIZ) |
| 324 |
} |
| 325 |
|
| 326 |
#[test] |
| 327 |
fn roundtrip_uncompressed() { |
| 328 |
test_mixed_roundtrip_with_compression(Compression::Uncompressed) |
| 329 |
} |
| 330 |
|
| 331 |
fn test_mixed_roundtrip_with_compression(compression: Compression) { |
| 332 |
|
| 333 |
let original_pixels: [(f16,f32,f32); 4] = [ |
| 334 |
(0.0.to_f16(), -1.1, std::f32::consts::PI), |
| 335 |
(9.1.to_f16(), -3.1, std::f32::consts::TAU), |
| 336 |
(-10.0.to_f16(), -11.1, f32::EPSILON), |
| 337 |
(half::f16::NAN, 10000.1, -1024.009), |
| 338 |
]; |
| 339 |
|
| 340 |
let mut file_bytes = Vec::new(); |
| 341 |
let original_image = Image::from_encoded_channels( |
| 342 |
(2,2), |
| 343 |
Encoding { |
| 344 |
compression, |
| 345 |
.. Encoding::default() |
| 346 |
}, |
| 347 |
SpecificChannels::rgb( |
| 348 |
PixelVec::new(Vec2(2,2), original_pixels.to_vec()) |
| 349 |
) |
| 350 |
); |
| 351 |
|
| 352 |
original_image.write().to_buffered(Cursor::new(&mut file_bytes)).unwrap(); |
| 353 |
|
| 354 |
let lossy_image = read().no_deep_data().largest_resolution_level() |
| 355 |
.rgb_channels(PixelVec::<(f16,f32,f32)>::constructor, PixelVec::set_pixel) |
| 356 |
.first_valid_layer().all_attributes().from_buffered(Cursor::new(&file_bytes)).unwrap(); |
| 357 |
|
| 358 |
// use automatic lossy detection by compression method |
| 359 |
original_image.assert_equals_result(&original_image); |
| 360 |
lossy_image.assert_equals_result(&lossy_image); |
| 361 |
original_image.assert_equals_result(&lossy_image); |
| 362 |
} |