| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use image::GenericImageView; |
| 12 | use std::path::{Path, PathBuf}; |
| 13 | |
| 14 | fn fixtures() -> PathBuf { |
| 15 | Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/stills") |
| 16 | } |
| 17 | |
| 18 | |
| 19 | fn corpus(extension: &str) -> Vec<PathBuf> { |
| 20 | let dir = fixtures(); |
| 21 | let mut found: Vec<PathBuf> = std::fs::read_dir(&dir) |
| 22 | .into_iter() |
| 23 | .flatten() |
| 24 | .flatten() |
| 25 | .map(|e| e.path()) |
| 26 | .filter(|p| p.extension().is_some_and(|e| e == extension)) |
| 27 | .filter(|p| { |
| 28 | !p.file_name() |
| 29 | .is_some_and(|n| n.to_string_lossy().starts_with("source")) |
| 30 | }) |
| 31 | .collect(); |
| 32 | found.sort(); |
| 33 | found |
| 34 | } |
| 35 | |
| 36 | fn skip(what: &str) -> bool { |
| 37 | eprintln!("skipped: fixtures/stills not generated ({what}); run scripts/gen-still-fixtures.sh"); |
| 38 | true |
| 39 | } |
| 40 | |
| 41 | |
| 42 | fn compare(ours: &[u8], theirs: &[u8]) -> (u32, f64) { |
| 43 | assert_eq!(ours.len(), theirs.len(), "sample counts differ"); |
| 44 | let mut max = 0u32; |
| 45 | let n = ours.len() as f64; |
| 46 | let (mut sa, mut sb, mut saa, mut sbb, mut sab) = (0.0, 0.0, 0.0, 0.0, 0.0); |
| 47 | for (&a, &b) in ours.iter().zip(theirs) { |
| 48 | max = max.max(u32::from(a.abs_diff(b))); |
| 49 | let (a, b) = (f64::from(a), f64::from(b)); |
| 50 | sa += a; |
| 51 | sb += b; |
| 52 | saa += a * a; |
| 53 | sbb += b * b; |
| 54 | sab += a * b; |
| 55 | } |
| 56 | let cov = sab - sa * sb / n; |
| 57 | let va = (saa - sa * sa / n).max(0.0); |
| 58 | let vb = (sbb - sb * sb / n).max(0.0); |
| 59 | let corr = if va * vb > 0.0 { |
| 60 | cov / (va * vb).sqrt() |
| 61 | } else { |
| 62 | 1.0 |
| 63 | }; |
| 64 | (max, corr) |
| 65 | } |
| 66 | |
| 67 | fn psnr(a: &[u8], b: &[u8]) -> f64 { |
| 68 | let mse: f64 = a |
| 69 | .iter() |
| 70 | .zip(b) |
| 71 | .map(|(&x, &y)| { |
| 72 | let d = f64::from(x) - f64::from(y); |
| 73 | d * d |
| 74 | }) |
| 75 | .sum::<f64>() |
| 76 | / a.len() as f64; |
| 77 | if mse == 0.0 { |
| 78 | f64::INFINITY |
| 79 | } else { |
| 80 | 10.0 * (255.0f64 * 255.0 / mse).log10() |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn png_decodes_pixel_exactly() { |
| 86 | let files = corpus("png"); |
| 87 | if files.is_empty() && skip("png") { |
| 88 | return; |
| 89 | } |
| 90 | for path in files { |
| 91 | let bytes = std::fs::read(&path).unwrap(); |
| 92 | let ours = ec_image::decode(&bytes).unwrap_or_else(|e| panic!("{}: {e}", path.display())); |
| 93 | let theirs = image::load_from_memory(&bytes).expect("incumbent decode"); |
| 94 | assert_eq!( |
| 95 | (ours.width, ours.height), |
| 96 | theirs.dimensions(), |
| 97 | "{}", |
| 98 | path.display() |
| 99 | ); |
| 100 | if ours.pixels.bit_depth() == 16 { |
| 101 | |
| 102 | |
| 103 | let a = ours.to_rgba16(); |
| 104 | let b = theirs.to_rgba16().into_raw(); |
| 105 | assert_eq!(a, b, "{} (16-bit samples)", path.display()); |
| 106 | } else { |
| 107 | let a = ours.to_rgba8(); |
| 108 | let b = theirs.to_rgba8().into_raw(); |
| 109 | assert_eq!(a, b, "{} (8-bit samples)", path.display()); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn webp_lossless_decodes_pixel_exactly() { |
| 116 | let files: Vec<PathBuf> = corpus("webp") |
| 117 | .into_iter() |
| 118 | .filter(|p| p.to_string_lossy().contains("lossless")) |
| 119 | .collect(); |
| 120 | if files.is_empty() && skip("webp") { |
| 121 | return; |
| 122 | } |
| 123 | for path in files { |
| 124 | let bytes = std::fs::read(&path).unwrap(); |
| 125 | let ours = ec_image::decode(&bytes).unwrap_or_else(|e| panic!("{}: {e}", path.display())); |
| 126 | let theirs = image::load_from_memory(&bytes).expect("incumbent decode"); |
| 127 | assert_eq!( |
| 128 | (ours.width, ours.height), |
| 129 | theirs.dimensions(), |
| 130 | "{}", |
| 131 | path.display() |
| 132 | ); |
| 133 | assert_eq!( |
| 134 | ours.to_rgba8(), |
| 135 | theirs.to_rgba8().into_raw(), |
| 136 | "{}", |
| 137 | path.display() |
| 138 | ); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn jpeg_matches_the_incumbent_within_rounding() { |
| 144 | let files = corpus("jpg"); |
| 145 | if files.is_empty() && skip("jpg") { |
| 146 | return; |
| 147 | } |
| 148 | let mut worst = 0u32; |
| 149 | for path in files { |
| 150 | let bytes = std::fs::read(&path).unwrap(); |
| 151 | let ours = ec_image::decode(&bytes).unwrap_or_else(|e| panic!("{}: {e}", path.display())); |
| 152 | let theirs = image::load_from_memory(&bytes).expect("incumbent decode"); |
| 153 | assert_eq!( |
| 154 | (ours.width, ours.height), |
| 155 | theirs.dimensions(), |
| 156 | "{}", |
| 157 | path.display() |
| 158 | ); |
| 159 | let (max, corr) = compare(&ours.to_rgb8(), &theirs.to_rgb8().into_raw()); |
| 160 | let db = psnr(&ours.to_rgb8(), &theirs.to_rgb8().into_raw()); |
| 161 | eprintln!( |
| 162 | "{:<22} max delta {max:>3} corr {corr:.6} psnr {db:.1} dB", |
| 163 | path.file_name().unwrap().to_string_lossy() |
| 164 | ); |
| 165 | worst = worst.max(max); |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | assert!(max <= 5, "{}: max sample delta {max}", path.display()); |
| 172 | assert!(corr > 0.9995, "{}: correlation {corr}", path.display()); |
| 173 | assert!( |
| 174 | db > 48.0, |
| 175 | "{}: {db:.1} dB against the incumbent decode", |
| 176 | path.display() |
| 177 | ); |
| 178 | } |
| 179 | eprintln!("jpeg worst per-sample delta vs incumbent: {worst}"); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn webp_lossy_reconstructs_at_least_as_well_as_the_incumbent() { |
| 184 | let files: Vec<PathBuf> = corpus("webp") |
| 185 | .into_iter() |
| 186 | .filter(|p| p.to_string_lossy().contains("lossy")) |
| 187 | .collect(); |
| 188 | if files.is_empty() && skip("webp") { |
| 189 | return; |
| 190 | } |
| 191 | let mut shortfalls: Vec<String> = Vec::new(); |
| 192 | for path in files { |
| 193 | let name = path.file_name().unwrap().to_string_lossy().to_string(); |
| 194 | let source = if name.starts_with("odd") { |
| 195 | fixtures().join("source-odd.png") |
| 196 | } else if name.contains("alpha") { |
| 197 | fixtures().join("source-alpha.png") |
| 198 | } else { |
| 199 | fixtures().join("source.png") |
| 200 | }; |
| 201 | let reference = image::open(&source).expect("source picture").to_rgb8(); |
| 202 | let bytes = std::fs::read(&path).unwrap(); |
| 203 | let ours = ec_image::decode(&bytes).unwrap_or_else(|e| panic!("{}: {e}", path.display())); |
| 204 | let theirs = image::load_from_memory(&bytes).expect("incumbent decode"); |
| 205 | let ours_rgb = ours.to_rgb8(); |
| 206 | let theirs_rgb = theirs.to_rgb8().into_raw(); |
| 207 | let mine = psnr(&ours_rgb, reference.as_raw()); |
| 208 | let incumbent = psnr(&theirs_rgb, reference.as_raw()); |
| 209 | let (max, corr) = compare(&ours_rgb, &theirs_rgb); |
| 210 | eprintln!( |
| 211 | "{name:<22} psnr {mine:.2} dB (incumbent {incumbent:.2} dB) \ |
| 212 | max delta {max} corr {corr:.6}" |
| 213 | ); |
| 214 | if mine < incumbent - 0.1 { |
| 215 | shortfalls.push(format!( |
| 216 | "{name}: {mine:.2} dB against the source, incumbent {incumbent:.2} dB" |
| 217 | )); |
| 218 | } |
| 219 | if ours.pixels.has_alpha() { |
| 220 | let a: Vec<u8> = ours.to_rgba8().chunks_exact(4).map(|p| p[3]).collect(); |
| 221 | let b: Vec<u8> = theirs |
| 222 | .to_rgba8() |
| 223 | .into_raw() |
| 224 | .chunks_exact(4) |
| 225 | .map(|p| p[3]) |
| 226 | .collect(); |
| 227 | assert_eq!(a, b, "{name}: alpha plane is lossless and must match"); |
| 228 | } |
| 229 | } |
| 230 | assert!(shortfalls.is_empty(), "{}", shortfalls.join("\n")); |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn header_dimensions_match_the_incumbent() { |
| 235 | let mut checked = 0; |
| 236 | for extension in ["png", "jpg", "webp"] { |
| 237 | for path in corpus(extension) { |
| 238 | if path.to_string_lossy().contains("animated") { |
| 239 | continue; |
| 240 | } |
| 241 | let bytes = std::fs::read(&path).unwrap(); |
| 242 | let ours = ec_image::info(&bytes).unwrap_or_else(|e| panic!("{}: {e}", path.display())); |
| 243 | let theirs = image::ImageReader::new(std::io::Cursor::new(&bytes)) |
| 244 | .with_guessed_format() |
| 245 | .unwrap() |
| 246 | .into_dimensions() |
| 247 | .unwrap(); |
| 248 | assert_eq!((ours.width, ours.height), theirs, "{}", path.display()); |
| 249 | checked += 1; |
| 250 | } |
| 251 | } |
| 252 | if checked == 0 { |
| 253 | skip("any"); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | #[test] |
| 258 | fn an_animated_webp_is_refused_by_name() { |
| 259 | let path = fixtures().join("animated.webp"); |
| 260 | let Ok(bytes) = std::fs::read(&path) else { |
| 261 | skip("animated.webp"); |
| 262 | return; |
| 263 | }; |
| 264 | let err = ec_image::decode(&bytes).expect_err("an animation is not a still"); |
| 265 | let message = format!("{err}"); |
| 266 | assert!(message.contains("animated"), "{message}"); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | fn with_exif_orientation(jpeg: &[u8], orientation: u16) -> Vec<u8> { |
| 275 | let mut tiff = b"Exif\0\0II".to_vec(); |
| 276 | tiff.extend_from_slice(&42u16.to_le_bytes()); |
| 277 | tiff.extend_from_slice(&8u32.to_le_bytes()); |
| 278 | tiff.extend_from_slice(&1u16.to_le_bytes()); |
| 279 | tiff.extend_from_slice(&0x0112u16.to_le_bytes()); |
| 280 | tiff.extend_from_slice(&3u16.to_le_bytes()); |
| 281 | tiff.extend_from_slice(&1u32.to_le_bytes()); |
| 282 | tiff.extend_from_slice(&orientation.to_le_bytes()); |
| 283 | tiff.extend_from_slice(&0u16.to_le_bytes()); |
| 284 | tiff.extend_from_slice(&0u32.to_le_bytes()); |
| 285 | |
| 286 | let mut out = jpeg[..2].to_vec(); |
| 287 | out.extend_from_slice(&[0xff, 0xe1]); |
| 288 | out.extend_from_slice(&((tiff.len() + 2) as u16).to_be_bytes()); |
| 289 | out.extend_from_slice(&tiff); |
| 290 | out.extend_from_slice(&jpeg[2..]); |
| 291 | out |
| 292 | } |
| 293 | |
| 294 | #[test] |
| 295 | fn exif_orientation_is_reported_and_not_applied() { |
| 296 | let Ok(plain) = std::fs::read(fixtures().join("baseline-444.jpg")) else { |
| 297 | skip("baseline-444.jpg"); |
| 298 | return; |
| 299 | }; |
| 300 | let bytes = with_exif_orientation(&plain, 6); |
| 301 | let ours = ec_image::decode(&bytes).unwrap(); |
| 302 | let theirs = image::load_from_memory(&bytes).unwrap(); |
| 303 | assert_eq!(ours.meta.orientation, Some(6), "EXIF tag 0x0112 parsed"); |
| 304 | assert_eq!(ec_image::decode(&plain).unwrap().meta.orientation, None); |
| 305 | |
| 306 | |
| 307 | assert_eq!((ours.width, ours.height), theirs.dimensions()); |
| 308 | assert_eq!(ours.to_rgb8(), ec_image::decode(&plain).unwrap().to_rgb8()); |
| 309 | } |
| 310 | |
| 311 | |
| 312 | |
| 313 | #[test] |
| 314 | fn every_fixture_decodes() { |
| 315 | let mut failures = Vec::new(); |
| 316 | let mut count = 0; |
| 317 | for extension in ["png", "jpg", "webp"] { |
| 318 | for path in corpus(extension) { |
| 319 | let name = path.file_name().unwrap().to_string_lossy().to_string(); |
| 320 | if name.contains("animated") { |
| 321 | continue; |
| 322 | } |
| 323 | let bytes = std::fs::read(&path).unwrap(); |
| 324 | count += 1; |
| 325 | match ec_image::decode(&bytes) { |
| 326 | Ok(image) => { |
| 327 | let (w, h) = (image.width as usize, image.height as usize); |
| 328 | assert_eq!( |
| 329 | image.to_rgba8().len(), |
| 330 | w * h * 4, |
| 331 | "{name}: buffer is not width x height" |
| 332 | ); |
| 333 | } |
| 334 | Err(e) => failures.push(format!("{name}: {e}")), |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | if count == 0 { |
| 339 | skip("any"); |
| 340 | return; |
| 341 | } |
| 342 | assert!( |
| 343 | failures.is_empty(), |
| 344 | "{} failed:\n{}", |
| 345 | failures.len(), |
| 346 | failures.join("\n") |
| 347 | ); |
| 348 | } |