| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use image::GenericImageView; |
| 12 | use std::path::{Path, PathBuf}; |
| 13 | |
| 14 | |
| 15 | |
| 16 | fn name_of(p: &std::path::Path) -> String { |
| 17 | p.file_name() |
| 18 | .unwrap_or_default() |
| 19 | .to_string_lossy() |
| 20 | .to_string() |
| 21 | } |
| 22 | |
| 23 | fn fixtures() -> PathBuf { |
| 24 | Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/stills") |
| 25 | } |
| 26 | |
| 27 | |
| 28 | fn corpus(extension: &str) -> Vec<PathBuf> { |
| 29 | let dir = fixtures(); |
| 30 | let mut found: Vec<PathBuf> = std::fs::read_dir(&dir) |
| 31 | .into_iter() |
| 32 | .flatten() |
| 33 | .flatten() |
| 34 | .map(|e| e.path()) |
| 35 | .filter(|p| p.extension().is_some_and(|e| e == extension)) |
| 36 | .filter(|p| { |
| 37 | !p.file_name() |
| 38 | .is_some_and(|n| n.to_string_lossy().starts_with("source")) |
| 39 | }) |
| 40 | .collect(); |
| 41 | found.sort(); |
| 42 | found |
| 43 | } |
| 44 | |
| 45 | fn skip(what: &str) -> bool { |
| 46 | eprintln!("skipped: fixtures/stills not generated ({what}); run scripts/gen-still-fixtures.sh"); |
| 47 | true |
| 48 | } |
| 49 | |
| 50 | |
| 51 | fn compare(ours: &[u8], theirs: &[u8]) -> (u32, f64) { |
| 52 | assert_eq!(ours.len(), theirs.len(), "sample counts differ"); |
| 53 | let mut max = 0u32; |
| 54 | let n = ours.len() as f64; |
| 55 | let (mut sa, mut sb, mut saa, mut sbb, mut sab) = (0.0, 0.0, 0.0, 0.0, 0.0); |
| 56 | for (&a, &b) in ours.iter().zip(theirs) { |
| 57 | max = max.max(u32::from(a.abs_diff(b))); |
| 58 | let (a, b) = (f64::from(a), f64::from(b)); |
| 59 | sa += a; |
| 60 | sb += b; |
| 61 | saa += a * a; |
| 62 | sbb += b * b; |
| 63 | sab += a * b; |
| 64 | } |
| 65 | let cov = sab - sa * sb / n; |
| 66 | let va = (saa - sa * sa / n).max(0.0); |
| 67 | let vb = (sbb - sb * sb / n).max(0.0); |
| 68 | let corr = if va * vb > 0.0 { |
| 69 | cov / (va * vb).sqrt() |
| 70 | } else { |
| 71 | 1.0 |
| 72 | }; |
| 73 | (max, corr) |
| 74 | } |
| 75 | |
| 76 | fn psnr(a: &[u8], b: &[u8]) -> f64 { |
| 77 | let mse: f64 = a |
| 78 | .iter() |
| 79 | .zip(b) |
| 80 | .map(|(&x, &y)| { |
| 81 | let d = f64::from(x) - f64::from(y); |
| 82 | d * d |
| 83 | }) |
| 84 | .sum::<f64>() |
| 85 | / a.len() as f64; |
| 86 | if mse == 0.0 { |
| 87 | f64::INFINITY |
| 88 | } else { |
| 89 | 10.0 * (255.0f64 * 255.0 / mse).log10() |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn png_decodes_pixel_exactly() { |
| 95 | let files = corpus("png"); |
| 96 | if files.is_empty() && skip("png") { |
| 97 | return; |
| 98 | } |
| 99 | for path in files { |
| 100 | let bytes = std::fs::read(&path).unwrap(); |
| 101 | let ours = ec_image::decode(&bytes).unwrap_or_else(|e| panic!("{}: {e}", path.display())); |
| 102 | let theirs = image::load_from_memory(&bytes).expect("incumbent decode"); |
| 103 | assert_eq!( |
| 104 | (ours.width, ours.height), |
| 105 | theirs.dimensions(), |
| 106 | "{}", |
| 107 | path.display() |
| 108 | ); |
| 109 | if ours.pixels.bit_depth() == 16 { |
| 110 | |
| 111 | |
| 112 | let a = ours.to_rgba16(); |
| 113 | let b = theirs.to_rgba16().into_raw(); |
| 114 | assert_eq!(a, b, "{} (16-bit samples)", path.display()); |
| 115 | } else { |
| 116 | let a = ours.to_rgba8(); |
| 117 | let b = theirs.to_rgba8().into_raw(); |
| 118 | assert_eq!(a, b, "{} (8-bit samples)", path.display()); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn webp_lossless_decodes_pixel_exactly() { |
| 125 | let files: Vec<PathBuf> = corpus("webp") |
| 126 | .into_iter() |
| 127 | .filter(|p| name_of(p).contains("lossless")) |
| 128 | .collect(); |
| 129 | if files.is_empty() && skip("webp") { |
| 130 | return; |
| 131 | } |
| 132 | for path in files { |
| 133 | let bytes = std::fs::read(&path).unwrap(); |
| 134 | let ours = ec_image::decode(&bytes).unwrap_or_else(|e| panic!("{}: {e}", path.display())); |
| 135 | let theirs = image::load_from_memory(&bytes).expect("incumbent decode"); |
| 136 | assert_eq!( |
| 137 | (ours.width, ours.height), |
| 138 | theirs.dimensions(), |
| 139 | "{}", |
| 140 | path.display() |
| 141 | ); |
| 142 | assert_eq!( |
| 143 | ours.to_rgba8(), |
| 144 | theirs.to_rgba8().into_raw(), |
| 145 | "{}", |
| 146 | path.display() |
| 147 | ); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn jpeg_matches_the_incumbent_within_rounding() { |
| 153 | let files = corpus("jpg"); |
| 154 | if files.is_empty() && skip("jpg") { |
| 155 | return; |
| 156 | } |
| 157 | let mut worst = 0u32; |
| 158 | for path in files { |
| 159 | let bytes = std::fs::read(&path).unwrap(); |
| 160 | let ours = ec_image::decode(&bytes).unwrap_or_else(|e| panic!("{}: {e}", path.display())); |
| 161 | let theirs = image::load_from_memory(&bytes).expect("incumbent decode"); |
| 162 | assert_eq!( |
| 163 | (ours.width, ours.height), |
| 164 | theirs.dimensions(), |
| 165 | "{}", |
| 166 | path.display() |
| 167 | ); |
| 168 | let (max, corr) = compare(&ours.to_rgb8(), &theirs.to_rgb8().into_raw()); |
| 169 | let db = psnr(&ours.to_rgb8(), &theirs.to_rgb8().into_raw()); |
| 170 | eprintln!( |
| 171 | "{:<22} max delta {max:>3} corr {corr:.6} psnr {db:.1} dB", |
| 172 | path.file_name().unwrap().to_string_lossy() |
| 173 | ); |
| 174 | worst = worst.max(max); |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | assert!(max <= 5, "{}: max sample delta {max}", path.display()); |
| 181 | assert!(corr > 0.9995, "{}: correlation {corr}", path.display()); |
| 182 | assert!( |
| 183 | db > 48.0, |
| 184 | "{}: {db:.1} dB against the incumbent decode", |
| 185 | path.display() |
| 186 | ); |
| 187 | } |
| 188 | eprintln!("jpeg worst per-sample delta vs incumbent: {worst}"); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn webp_lossy_reconstructs_at_least_as_well_as_the_incumbent() { |
| 193 | let files: Vec<PathBuf> = corpus("webp") |
| 194 | .into_iter() |
| 195 | .filter(|p| name_of(p).contains("lossy")) |
| 196 | .collect(); |
| 197 | if files.is_empty() && skip("webp") { |
| 198 | return; |
| 199 | } |
| 200 | let mut shortfalls: Vec<String> = Vec::new(); |
| 201 | for path in files { |
| 202 | let name = path.file_name().unwrap().to_string_lossy().to_string(); |
| 203 | let source = if name.starts_with("odd") { |
| 204 | fixtures().join("source-odd.png") |
| 205 | } else if name.contains("alpha") { |
| 206 | fixtures().join("source-alpha.png") |
| 207 | } else { |
| 208 | fixtures().join("source.png") |
| 209 | }; |
| 210 | let reference = image::open(&source).expect("source picture").to_rgb8(); |
| 211 | let bytes = std::fs::read(&path).unwrap(); |
| 212 | let ours = ec_image::decode(&bytes).unwrap_or_else(|e| panic!("{}: {e}", path.display())); |
| 213 | let theirs = image::load_from_memory(&bytes).expect("incumbent decode"); |
| 214 | let ours_rgb = ours.to_rgb8(); |
| 215 | let theirs_rgb = theirs.to_rgb8().into_raw(); |
| 216 | let mine = psnr(&ours_rgb, reference.as_raw()); |
| 217 | let incumbent = psnr(&theirs_rgb, reference.as_raw()); |
| 218 | let (max, corr) = compare(&ours_rgb, &theirs_rgb); |
| 219 | eprintln!( |
| 220 | "{name:<22} psnr {mine:.2} dB (incumbent {incumbent:.2} dB) \ |
| 221 | max delta {max} corr {corr:.6}" |
| 222 | ); |
| 223 | if mine < incumbent - 0.1 { |
| 224 | shortfalls.push(format!( |
| 225 | "{name}: {mine:.2} dB against the source, incumbent {incumbent:.2} dB" |
| 226 | )); |
| 227 | } |
| 228 | if ours.pixels.has_alpha() { |
| 229 | let a: Vec<u8> = ours.to_rgba8().as_chunks::<4>().0.iter().map(|p| p[3]).collect(); |
| 230 | let b: Vec<u8> = theirs |
| 231 | .to_rgba8() |
| 232 | .into_raw() |
| 233 | .as_chunks::<4>().0.iter() |
| 234 | .map(|p| p[3]) |
| 235 | .collect(); |
| 236 | assert_eq!(a, b, "{name}: alpha plane is lossless and must match"); |
| 237 | } |
| 238 | } |
| 239 | assert!(shortfalls.is_empty(), "{}", shortfalls.join("\n")); |
| 240 | } |
| 241 | |
| 242 | #[test] |
| 243 | fn header_dimensions_match_the_incumbent() { |
| 244 | let mut checked = 0; |
| 245 | for extension in ["png", "jpg", "webp"] { |
| 246 | for path in corpus(extension) { |
| 247 | if name_of(&path).contains("animated") { |
| 248 | continue; |
| 249 | } |
| 250 | let bytes = std::fs::read(&path).unwrap(); |
| 251 | let ours = ec_image::info(&bytes).unwrap_or_else(|e| panic!("{}: {e}", path.display())); |
| 252 | let theirs = image::ImageReader::new(std::io::Cursor::new(&bytes)) |
| 253 | .with_guessed_format() |
| 254 | .unwrap() |
| 255 | .into_dimensions() |
| 256 | .unwrap(); |
| 257 | assert_eq!((ours.width, ours.height), theirs, "{}", path.display()); |
| 258 | checked += 1; |
| 259 | } |
| 260 | } |
| 261 | if checked == 0 { |
| 262 | skip("any"); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | #[test] |
| 267 | fn an_animated_webp_is_refused_by_name() { |
| 268 | let path = fixtures().join("animated.webp"); |
| 269 | let Ok(bytes) = std::fs::read(&path) else { |
| 270 | skip("animated.webp"); |
| 271 | return; |
| 272 | }; |
| 273 | let err = ec_image::decode(&bytes).expect_err("an animation is not a still"); |
| 274 | let message = format!("{err}"); |
| 275 | assert!(message.contains("animated"), "{message}"); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | fn with_exif_orientation(jpeg: &[u8], orientation: u16) -> Vec<u8> { |
| 284 | let mut tiff = b"Exif\0\0II".to_vec(); |
| 285 | tiff.extend_from_slice(&42u16.to_le_bytes()); |
| 286 | tiff.extend_from_slice(&8u32.to_le_bytes()); |
| 287 | tiff.extend_from_slice(&1u16.to_le_bytes()); |
| 288 | tiff.extend_from_slice(&0x0112u16.to_le_bytes()); |
| 289 | tiff.extend_from_slice(&3u16.to_le_bytes()); |
| 290 | tiff.extend_from_slice(&1u32.to_le_bytes()); |
| 291 | tiff.extend_from_slice(&orientation.to_le_bytes()); |
| 292 | tiff.extend_from_slice(&0u16.to_le_bytes()); |
| 293 | tiff.extend_from_slice(&0u32.to_le_bytes()); |
| 294 | |
| 295 | let mut out = jpeg[..2].to_vec(); |
| 296 | out.extend_from_slice(&[0xff, 0xe1]); |
| 297 | out.extend_from_slice(&((tiff.len() + 2) as u16).to_be_bytes()); |
| 298 | out.extend_from_slice(&tiff); |
| 299 | out.extend_from_slice(&jpeg[2..]); |
| 300 | out |
| 301 | } |
| 302 | |
| 303 | #[test] |
| 304 | fn exif_orientation_is_reported_and_not_applied() { |
| 305 | let Ok(plain) = std::fs::read(fixtures().join("baseline-444.jpg")) else { |
| 306 | skip("baseline-444.jpg"); |
| 307 | return; |
| 308 | }; |
| 309 | let bytes = with_exif_orientation(&plain, 6); |
| 310 | let ours = ec_image::decode(&bytes).unwrap(); |
| 311 | let theirs = image::load_from_memory(&bytes).unwrap(); |
| 312 | assert_eq!(ours.meta.orientation, Some(6), "EXIF tag 0x0112 parsed"); |
| 313 | assert_eq!(ec_image::decode(&plain).unwrap().meta.orientation, None); |
| 314 | |
| 315 | |
| 316 | assert_eq!((ours.width, ours.height), theirs.dimensions()); |
| 317 | assert_eq!(ours.to_rgb8(), ec_image::decode(&plain).unwrap().to_rgb8()); |
| 318 | } |
| 319 | |
| 320 | |
| 321 | |
| 322 | #[test] |
| 323 | fn every_fixture_decodes() { |
| 324 | let mut failures = Vec::new(); |
| 325 | let mut count = 0; |
| 326 | for extension in ["png", "jpg", "webp"] { |
| 327 | for path in corpus(extension) { |
| 328 | let name = path.file_name().unwrap().to_string_lossy().to_string(); |
| 329 | if name.contains("animated") || name.starts_with("anim-") { |
| 330 | continue; |
| 331 | } |
| 332 | let bytes = std::fs::read(&path).unwrap(); |
| 333 | count += 1; |
| 334 | match ec_image::decode(&bytes) { |
| 335 | Ok(image) => { |
| 336 | let (w, h) = (image.width as usize, image.height as usize); |
| 337 | assert_eq!( |
| 338 | image.to_rgba8().len(), |
| 339 | w * h * 4, |
| 340 | "{name}: buffer is not width x height" |
| 341 | ); |
| 342 | } |
| 343 | Err(e) => failures.push(format!("{name}: {e}")), |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | if count == 0 { |
| 348 | skip("any"); |
| 349 | return; |
| 350 | } |
| 351 | assert!( |
| 352 | failures.is_empty(), |
| 353 | "{} failed:\n{}", |
| 354 | failures.len(), |
| 355 | failures.join("\n") |
| 356 | ); |
| 357 | } |
| 358 | |
| 359 | #[test] |
| 360 | fn gif_decodes_pixel_exactly() { |
| 361 | let files = corpus("gif"); |
| 362 | if files.is_empty() && skip("gif") { |
| 363 | return; |
| 364 | } |
| 365 | for path in files { |
| 366 | let bytes = std::fs::read(&path).unwrap(); |
| 367 | let ours = ec_image::decode(&bytes).unwrap_or_else(|e| panic!("{}: {e}", path.display())); |
| 368 | let theirs = image::load_from_memory(&bytes).expect("incumbent decode"); |
| 369 | assert_eq!( |
| 370 | (ours.width, ours.height), |
| 371 | theirs.dimensions(), |
| 372 | "{}", |
| 373 | path.display() |
| 374 | ); |
| 375 | assert_eq!( |
| 376 | ours.to_rgba8(), |
| 377 | theirs.to_rgba8().into_raw(), |
| 378 | "{} (first frame)", |
| 379 | path.file_name().unwrap().to_string_lossy() |
| 380 | ); |
| 381 | eprintln!( |
| 382 | "{:<22} {}x{} exact", |
| 383 | path.file_name().unwrap().to_string_lossy(), |
| 384 | ours.width, |
| 385 | ours.height |
| 386 | ); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | #[test] |
| 391 | fn a_gif_animation_composites_frame_for_frame_like_the_incumbent() { |
| 392 | use image::AnimationDecoder; |
| 393 | |
| 394 | let path = fixtures().join("animated.gif"); |
| 395 | if !path.exists() && skip("animated.gif") { |
| 396 | return; |
| 397 | } |
| 398 | let bytes = std::fs::read(&path).unwrap(); |
| 399 | let ours = ec_image::decode_animation(&bytes).expect("our animation decode"); |
| 400 | let theirs: Vec<image::Frame> = |
| 401 | image::codecs::gif::GifDecoder::new(std::io::Cursor::new(&bytes)) |
| 402 | .expect("incumbent decoder") |
| 403 | .into_frames() |
| 404 | .collect::<Result<Vec<_>, _>>() |
| 405 | .expect("incumbent frames"); |
| 406 | |
| 407 | assert_eq!(ours.len(), theirs.len(), "frame count"); |
| 408 | assert!(ours.len() > 1, "the fixture is not an animation"); |
| 409 | for (i, (ours, theirs)) in ours.iter().zip(&theirs).enumerate() { |
| 410 | let (num, den) = theirs.delay().numer_denom_ms(); |
| 411 | |
| 412 | |
| 413 | let ours_ms = f64::from(ours.delay_num) * 1000.0 / f64::from(ours.delay_den); |
| 414 | let theirs_ms = f64::from(num) / f64::from(den); |
| 415 | assert!( |
| 416 | (ours_ms - theirs_ms).abs() < 0.5, |
| 417 | "frame {i}: {ours_ms} ms against the incumbent's {theirs_ms} ms" |
| 418 | ); |
| 419 | assert_eq!( |
| 420 | (ours.image.width, ours.image.height), |
| 421 | theirs.buffer().dimensions(), |
| 422 | "frame {i} is not the whole canvas" |
| 423 | ); |
| 424 | assert_eq!( |
| 425 | ours.image.to_rgba8(), |
| 426 | theirs.buffer().as_raw().clone(), |
| 427 | "frame {i} samples" |
| 428 | ); |
| 429 | eprintln!("frame {i}: {ours_ms:.0} ms, exact"); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | #[test] |
| 434 | fn a_still_of_any_format_is_one_frame_of_animation() { |
| 435 | let path = fixtures().join("rgb8.png"); |
| 436 | if !path.exists() && skip("rgb8.png") { |
| 437 | return; |
| 438 | } |
| 439 | let bytes = std::fs::read(&path).unwrap(); |
| 440 | let frames = ec_image::decode_animation(&bytes).expect("decode"); |
| 441 | assert_eq!(frames.len(), 1); |
| 442 | assert_eq!(frames[0].delay_num, 0); |
| 443 | assert_eq!( |
| 444 | frames[0].image.to_rgba8(), |
| 445 | ec_image::decode(&bytes).unwrap().to_rgba8() |
| 446 | ); |
| 447 | } |
| 448 | |
| 449 | #[test] |
| 450 | fn an_animated_webp_composites_frame_for_frame_like_the_incumbent() { |
| 451 | one_animated_webp("animated.webp"); |
| 452 | } |
| 453 | |
| 454 | fn one_animated_webp(fixture: &str) { |
| 455 | use image::AnimationDecoder; |
| 456 | |
| 457 | let path = fixtures().join(fixture); |
| 458 | let Ok(bytes) = std::fs::read(&path) else { |
| 459 | skip(fixture); |
| 460 | return; |
| 461 | }; |
| 462 | assert!( |
| 463 | ec_image::webp::is_animated(&bytes), |
| 464 | "the fixture is not an animation" |
| 465 | ); |
| 466 | let ours = ec_image::decode_animation(&bytes).expect("our animation decode"); |
| 467 | let theirs: Vec<image::Frame> = |
| 468 | image::codecs::webp::WebPDecoder::new(std::io::Cursor::new(&bytes)) |
| 469 | .expect("incumbent decoder") |
| 470 | .into_frames() |
| 471 | .collect::<Result<Vec<_>, _>>() |
| 472 | .expect("incumbent frames"); |
| 473 | |
| 474 | assert_eq!(ours.len(), theirs.len(), "frame count"); |
| 475 | assert!(ours.len() > 1, "the fixture is not an animation"); |
| 476 | for (i, (ours, theirs)) in ours.iter().zip(&theirs).enumerate() { |
| 477 | let (num, den) = theirs.delay().numer_denom_ms(); |
| 478 | let ours_ms = f64::from(ours.delay_num) * 1000.0 / f64::from(ours.delay_den); |
| 479 | let theirs_ms = f64::from(num) / f64::from(den); |
| 480 | assert!( |
| 481 | (ours_ms - theirs_ms).abs() < 0.5, |
| 482 | "frame {i}: {ours_ms} ms against the incumbent's {theirs_ms} ms" |
| 483 | ); |
| 484 | assert_eq!( |
| 485 | (ours.image.width, ours.image.height), |
| 486 | theirs.buffer().dimensions(), |
| 487 | "frame {i} is not the whole canvas" |
| 488 | ); |
| 489 | |
| 490 | |
| 491 | |
| 492 | let a = ours.image.to_rgba8(); |
| 493 | let b = theirs.buffer().as_raw(); |
| 494 | let max = a |
| 495 | .iter() |
| 496 | .zip(b) |
| 497 | .map(|(&x, &y)| x.abs_diff(y)) |
| 498 | .max() |
| 499 | .unwrap_or(0); |
| 500 | let psnr = psnr(&a, b); |
| 501 | eprintln!("{fixture} frame {i}: {ours_ms:.0} ms, max delta {max}, {psnr:.1} dB"); |
| 502 | assert!( |
| 503 | max <= 5 && psnr > 48.0, |
| 504 | "{fixture} frame {i}: max delta {max}, {psnr:.1} dB" |
| 505 | ); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | #[test] |
| 517 | fn webp_disposal_and_blending_match_libwebp() { |
| 518 | let path = fixtures().join("anim-alpha.webp"); |
| 519 | let Ok(bytes) = std::fs::read(&path) else { |
| 520 | skip("anim-alpha.webp"); |
| 521 | return; |
| 522 | }; |
| 523 | let goldens: Vec<PathBuf> = (0..) |
| 524 | .map(|i| fixtures().join(format!("anim-alpha-f{i}.png"))) |
| 525 | .take_while(|p| p.exists()) |
| 526 | .collect(); |
| 527 | if goldens.is_empty() { |
| 528 | skip("anim-alpha-f0.png (libwebp goldens; Pillow not installed?)"); |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | let ours = ec_image::decode_animation(&bytes).expect("our animation decode"); |
| 533 | assert_eq!(ours.len(), goldens.len(), "frame count against libwebp"); |
| 534 | for (i, (ours, golden)) in ours.iter().zip(&goldens).enumerate() { |
| 535 | let want = ec_image::open(golden).expect("golden"); |
| 536 | assert_eq!( |
| 537 | (ours.image.width, ours.image.height), |
| 538 | (want.width, want.height), |
| 539 | "frame {i} is not the whole canvas" |
| 540 | ); |
| 541 | assert_eq!(ours.image.to_rgba8(), want.to_rgba8(), "frame {i} samples"); |
| 542 | eprintln!( |
| 543 | "frame {i}: {} ms, exact against libwebp", |
| 544 | f64::from(ours.delay_num) * 1000.0 / f64::from(ours.delay_den) |
| 545 | ); |
| 546 | } |
| 547 | |
| 548 | |
| 549 | let delays: Vec<u32> = ours.iter().map(|f| f.delay_num).collect(); |
| 550 | assert_eq!(delays, vec![60, 90, 30], "per-frame durations in ms"); |
| 551 | } |
| 552 | |
| 553 | |
| 554 | |
| 555 | #[test] |
| 556 | fn bmp_decodes_pixel_exactly() { |
| 557 | let files = corpus("bmp"); |
| 558 | if files.is_empty() && skip("bmp") { |
| 559 | return; |
| 560 | } |
| 561 | for path in files { |
| 562 | let name = path.file_name().unwrap().to_string_lossy().to_string(); |
| 563 | if name.starts_with("embedded-") || name == "os2v2.bmp" { |
| 564 | continue; |
| 565 | } |
| 566 | let data = std::fs::read(&path).unwrap(); |
| 567 | let ours = ec_image::decode(&data).unwrap_or_else(|e| panic!("{name}: {e}")); |
| 568 | let theirs = image::load_from_memory(&data).unwrap_or_else(|e| panic!("{name}: {e}")); |
| 569 | assert_eq!( |
| 570 | (ours.width, ours.height), |
| 571 | theirs.dimensions(), |
| 572 | "{name}: dimensions differ" |
| 573 | ); |
| 574 | let (max, corr) = compare(&ours.to_rgba8(), &theirs.to_rgba8().into_raw()); |
| 575 | println!("{name:>16} max {max} corr {corr:.6}"); |
| 576 | assert_eq!(max, 0, "{name}: samples differ from the incumbent"); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | #[test] |
| 585 | fn a_bmp_wrapping_another_format_decodes_to_that_format() { |
| 586 | |
| 587 | |
| 588 | for (fixture, bar) in [("embedded-png.bmp", 0u32), ("embedded-jpeg.bmp", 5)] { |
| 589 | let path = fixtures().join(fixture); |
| 590 | let Ok(data) = std::fs::read(&path) else { |
| 591 | if skip(fixture) { |
| 592 | return; |
| 593 | } |
| 594 | unreachable!() |
| 595 | }; |
| 596 | |
| 597 | let offset = u32::from_le_bytes([data[10], data[11], data[12], data[13]]) as usize; |
| 598 | let payload = &data[offset..]; |
| 599 | let ours = ec_image::decode(&data).unwrap_or_else(|e| panic!("{fixture}: {e}")); |
| 600 | let theirs = image::load_from_memory(payload).unwrap(); |
| 601 | assert_eq!( |
| 602 | (ours.width, ours.height), |
| 603 | theirs.dimensions(), |
| 604 | "{fixture}: dimensions differ" |
| 605 | ); |
| 606 | let (max, corr) = compare(&ours.to_rgba8(), &theirs.to_rgba8().into_raw()); |
| 607 | println!("{fixture:>20} max {max} corr {corr:.6}"); |
| 608 | assert!(max <= bar, "{fixture}: max delta {max} over the bar {bar}"); |
| 609 | assert_eq!( |
| 610 | ec_image::info(&data).unwrap().width, |
| 611 | theirs.dimensions().0, |
| 612 | "{fixture}: the header-only path disagrees with the pixels" |
| 613 | ); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | #[test] |
| 622 | fn an_os2_v2_header_reads_as_the_same_picture() { |
| 623 | let short = fixtures().join("os2v2.bmp"); |
| 624 | let long = fixtures().join("os2v1.bmp"); |
| 625 | let (Ok(short), Ok(long)) = (std::fs::read(&short), std::fs::read(&long)) else { |
| 626 | skip("os2v2.bmp"); |
| 627 | return; |
| 628 | }; |
| 629 | assert!( |
| 630 | image::load_from_memory(&short).is_err(), |
| 631 | "the incumbent now reads a 16-byte header; this test's premise is stale" |
| 632 | ); |
| 633 | let ours = ec_image::decode(&short).unwrap(); |
| 634 | let reference = image::load_from_memory(&long).unwrap(); |
| 635 | assert_eq!((ours.width, ours.height), reference.dimensions()); |
| 636 | let (max, _) = compare(&ours.to_rgba8(), &reference.to_rgba8().into_raw()); |
| 637 | assert_eq!( |
| 638 | max, 0, |
| 639 | "os2v2.bmp: samples differ from the same picture in a v1 header" |
| 640 | ); |
| 641 | } |
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | #[test] |
| 648 | fn tiff_decodes_pixel_exactly() { |
| 649 | let mut files = corpus("tiff"); |
| 650 | files.extend(corpus("tif")); |
| 651 | files.sort(); |
| 652 | if files.is_empty() && skip("tiff") { |
| 653 | return; |
| 654 | } |
| 655 | for path in files { |
| 656 | let name = path.file_name().unwrap().to_string_lossy().to_string(); |
| 657 | if name == "palette4.tiff" { |
| 658 | continue; |
| 659 | } |
| 660 | if name == "rgba8-assoc.tiff" { |
| 661 | continue; |
| 662 | } |
| 663 | let data = std::fs::read(&path).unwrap(); |
| 664 | let ours = ec_image::decode(&data).unwrap_or_else(|e| panic!("{name}: {e}")); |
| 665 | let theirs = image::load_from_memory(&data).unwrap_or_else(|e| panic!("{name}: {e}")); |
| 666 | assert_eq!( |
| 667 | (ours.width, ours.height), |
| 668 | theirs.dimensions(), |
| 669 | "{name}: dimensions differ" |
| 670 | ); |
| 671 | let (max, corr) = compare(&ours.to_rgba8(), &theirs.to_rgba8().into_raw()); |
| 672 | println!("{name:>20} max {max} corr {corr:.6}"); |
| 673 | assert_eq!(max, 0, "{name}: samples differ from the incumbent"); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | #[test] |
| 683 | fn an_associated_alpha_tiff_reads_unpremultiplied() { |
| 684 | let (Ok(assoc), Ok(straight)) = ( |
| 685 | std::fs::read(fixtures().join("rgba8-assoc.tiff")), |
| 686 | std::fs::read(fixtures().join("rgba8.tiff")), |
| 687 | ) else { |
| 688 | skip("rgba8-assoc.tiff"); |
| 689 | return; |
| 690 | }; |
| 691 | let ours = ec_image::decode(&assoc).expect("rgba8-assoc.tiff"); |
| 692 | let reference = ec_image::decode(&straight).expect("rgba8.tiff"); |
| 693 | |
| 694 | |
| 695 | let (ours, reference) = (ours.to_rgba8(), reference.to_rgba8()); |
| 696 | for (i, (a, b)) in ours.chunks(4).zip(reference.chunks(4)).enumerate() { |
| 697 | assert_eq!(a[3], b[3], "rgba8-assoc.tiff: alpha differs at pixel {i}"); |
| 698 | if b[3] != 0 { |
| 699 | assert_eq!( |
| 700 | a[..3], |
| 701 | b[..3], |
| 702 | "rgba8-assoc.tiff: the premultiplication was not undone at pixel {i}" |
| 703 | ); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | |
| 708 | |
| 709 | let a = image::load_from_memory(&assoc).expect("incumbent rgba8-assoc.tiff"); |
| 710 | let b = image::load_from_memory(&straight).expect("incumbent rgba8.tiff"); |
| 711 | assert_ne!( |
| 712 | a.to_rgba8().into_raw(), |
| 713 | b.to_rgba8().into_raw(), |
| 714 | "the incumbent now honours ExtraSamples; use it as the oracle instead" |
| 715 | ); |
| 716 | } |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | #[test] |
| 723 | fn a_four_bit_tiff_palette_reads_as_ffmpeg_reads_it() { |
| 724 | let (Ok(narrow), Ok(wide)) = ( |
| 725 | std::fs::read(fixtures().join("palette4.tiff")), |
| 726 | std::fs::read(fixtures().join("palette4-golden.png")), |
| 727 | ) else { |
| 728 | skip("palette4.tiff"); |
| 729 | return; |
| 730 | }; |
| 731 | assert!( |
| 732 | image::load_from_memory(&narrow).is_err(), |
| 733 | "the incumbent now reads a 4-bit palette; this test's premise is stale" |
| 734 | ); |
| 735 | let ours = ec_image::decode(&narrow).unwrap(); |
| 736 | let reference = image::load_from_memory(&wide).unwrap(); |
| 737 | assert_eq!((ours.width, ours.height), reference.dimensions()); |
| 738 | let (max, _) = compare(&ours.to_rgba8(), &reference.to_rgba8().into_raw()); |
| 739 | assert_eq!(max, 0, "palette4.tiff: samples differ from the same palette at 8 bits"); |
| 740 | } |