| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use image::GenericImageView; |
| 9 | use std::path::PathBuf; |
| 10 | |
| 11 | |
| 12 | fn library() -> Vec<PathBuf> { |
| 13 | let home = std::env::var("HOME").unwrap_or_default(); |
| 14 | ["Videos", "Downloads"] |
| 15 | .iter() |
| 16 | .map(|d| PathBuf::from(&home).join(d)) |
| 17 | .filter(|p| p.is_dir()) |
| 18 | .collect() |
| 19 | } |
| 20 | |
| 21 | |
| 22 | fn stills(roots: &[PathBuf], cap: usize) -> Vec<PathBuf> { |
| 23 | fn walk(dir: &PathBuf, depth: usize, out: &mut Vec<PathBuf>) { |
| 24 | if depth == 0 { |
| 25 | return; |
| 26 | } |
| 27 | let Ok(entries) = std::fs::read_dir(dir) else { |
| 28 | return; |
| 29 | }; |
| 30 | for entry in entries.flatten() { |
| 31 | let path = entry.path(); |
| 32 | if path.is_dir() { |
| 33 | walk(&path, depth - 1, out); |
| 34 | } else if let Some(extension) = path.extension() { |
| 35 | let extension = extension.to_string_lossy().to_lowercase(); |
| 36 | if ["png", "jpg", "jpeg", "webp"].contains(&extension.as_str()) { |
| 37 | out.push(path); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | let mut found = Vec::new(); |
| 43 | for root in roots { |
| 44 | walk(root, 3, &mut found); |
| 45 | } |
| 46 | found.sort(); |
| 47 | found.truncate(cap); |
| 48 | found |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn real_pictures_decode_like_the_incumbent() { |
| 53 | let files = stills(&library(), 20); |
| 54 | if files.is_empty() { |
| 55 | eprintln!("skipped: no PNG/JPEG/WebP under ~/Videos or ~/Downloads"); |
| 56 | return; |
| 57 | } |
| 58 | let mut failures = Vec::new(); |
| 59 | for path in &files { |
| 60 | let name = path.file_name().unwrap().to_string_lossy().to_string(); |
| 61 | let bytes = std::fs::read(path).unwrap(); |
| 62 | let started = std::time::Instant::now(); |
| 63 | let ours = match ec_image::decode(&bytes) { |
| 64 | Ok(image) => image, |
| 65 | Err(e) => { |
| 66 | |
| 67 | |
| 68 | if image::load_from_memory(&bytes).is_ok() { |
| 69 | failures.push(format!( |
| 70 | "{name}: we refuse it, the incumbent decodes it ({e})" |
| 71 | )); |
| 72 | } else { |
| 73 | eprintln!("{name:<44} both refuse: {e}"); |
| 74 | } |
| 75 | continue; |
| 76 | } |
| 77 | }; |
| 78 | let ours_ms = started.elapsed().as_secs_f64() * 1000.0; |
| 79 | let theirs = match image::load_from_memory(&bytes) { |
| 80 | Ok(image) => image, |
| 81 | Err(e) => { |
| 82 | eprintln!("{name:<44} we decode it, the incumbent does not ({e})"); |
| 83 | continue; |
| 84 | } |
| 85 | }; |
| 86 | if (ours.width, ours.height) != theirs.dimensions() { |
| 87 | failures.push(format!( |
| 88 | "{name}: {}x{} against the incumbent's {:?}", |
| 89 | ours.width, |
| 90 | ours.height, |
| 91 | theirs.dimensions() |
| 92 | )); |
| 93 | continue; |
| 94 | } |
| 95 | let a = ours.to_rgb8(); |
| 96 | let b = theirs.to_rgb8().into_raw(); |
| 97 | let max = a |
| 98 | .iter() |
| 99 | .zip(&b) |
| 100 | .map(|(&x, &y)| x.abs_diff(y)) |
| 101 | .max() |
| 102 | .unwrap_or(0); |
| 103 | let lossless = path |
| 104 | .extension() |
| 105 | .is_some_and(|e| e.eq_ignore_ascii_case("png")); |
| 106 | eprintln!( |
| 107 | "{name:<44} {}x{} max delta {max:>3} {ours_ms:>7.1} ms", |
| 108 | ours.width, ours.height |
| 109 | ); |
| 110 | let bar = if lossless { 0 } else { 8 }; |
| 111 | if u32::from(max) > bar { |
| 112 | failures.push(format!("{name}: max delta {max}, bar {bar}")); |
| 113 | } |
| 114 | } |
| 115 | assert!( |
| 116 | failures.is_empty(), |
| 117 | "{} of {} real pictures failed:\n{}", |
| 118 | failures.len(), |
| 119 | files.len(), |
| 120 | failures.join("\n") |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | #[test] |
| 132 | fn real_gifs_animate_like_the_incumbent() { |
| 133 | use image::AnimationDecoder; |
| 134 | |
| 135 | let files = walk_extension(&library(), "gif", 20); |
| 136 | if files.is_empty() { |
| 137 | eprintln!("skipped: no GIF under ~/Videos or ~/Downloads"); |
| 138 | return; |
| 139 | } |
| 140 | let mut failures = Vec::new(); |
| 141 | for path in &files { |
| 142 | let name = path.file_name().unwrap().to_string_lossy().to_string(); |
| 143 | let bytes = std::fs::read(path).unwrap(); |
| 144 | let started = std::time::Instant::now(); |
| 145 | let ours = match ec_image::decode_animation(&bytes) { |
| 146 | Ok(frames) => frames, |
| 147 | Err(e) => { |
| 148 | if image::load_from_memory(&bytes).is_ok() { |
| 149 | failures.push(format!( |
| 150 | "{name}: we refuse it, the incumbent decodes it ({e})" |
| 151 | )); |
| 152 | } else { |
| 153 | eprintln!("{name:<44} both refuse: {e}"); |
| 154 | } |
| 155 | continue; |
| 156 | } |
| 157 | }; |
| 158 | let ours_ms = started.elapsed().as_secs_f64() * 1000.0; |
| 159 | let theirs = image::codecs::gif::GifDecoder::new(std::io::Cursor::new(&bytes)) |
| 160 | .and_then(|d| d.into_frames().collect::<Result<Vec<_>, _>>()); |
| 161 | let theirs = match theirs { |
| 162 | Ok(frames) => frames, |
| 163 | Err(e) => { |
| 164 | eprintln!("{name:<44} we decode it, the incumbent does not ({e})"); |
| 165 | continue; |
| 166 | } |
| 167 | }; |
| 168 | if ours.len() != theirs.len() { |
| 169 | failures.push(format!( |
| 170 | "{name}: {} frames against the incumbent's {}", |
| 171 | ours.len(), |
| 172 | theirs.len() |
| 173 | )); |
| 174 | continue; |
| 175 | } |
| 176 | let mut worst = None; |
| 177 | for (i, (ours, theirs)) in ours.iter().zip(&theirs).enumerate() { |
| 178 | if (ours.image.width, ours.image.height) != theirs.buffer().dimensions() { |
| 179 | worst = Some(format!( |
| 180 | "frame {i}: {}x{} against {:?}", |
| 181 | ours.image.width, |
| 182 | ours.image.height, |
| 183 | theirs.buffer().dimensions() |
| 184 | )); |
| 185 | break; |
| 186 | } |
| 187 | let a = ours.image.to_rgba8(); |
| 188 | let b = theirs.buffer().as_raw(); |
| 189 | let delta = a |
| 190 | .iter() |
| 191 | .zip(b) |
| 192 | .map(|(&x, &y)| x.abs_diff(y)) |
| 193 | .max() |
| 194 | .unwrap_or(0); |
| 195 | let (num, den) = theirs.delay().numer_denom_ms(); |
| 196 | let ours_ms = f64::from(ours.delay_num) * 1000.0 / f64::from(ours.delay_den); |
| 197 | let theirs_ms = f64::from(num) / f64::from(den); |
| 198 | if delta != 0 { |
| 199 | worst = Some(format!("frame {i}: max delta {delta}, bar 0")); |
| 200 | break; |
| 201 | } |
| 202 | if (ours_ms - theirs_ms).abs() >= 0.5 { |
| 203 | worst = Some(format!( |
| 204 | "frame {i}: {ours_ms} ms against the incumbent's {theirs_ms} ms" |
| 205 | )); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | eprintln!( |
| 210 | "{name:<44} {}x{} {:>3} frames {ours_ms:>7.1} ms {}", |
| 211 | ours[0].image.width, |
| 212 | ours[0].image.height, |
| 213 | ours.len(), |
| 214 | worst.as_deref().unwrap_or("exact") |
| 215 | ); |
| 216 | if let Some(worst) = worst { |
| 217 | failures.push(format!("{name}: {worst}")); |
| 218 | } |
| 219 | } |
| 220 | assert!( |
| 221 | failures.is_empty(), |
| 222 | "{} of {} real GIFs failed:\n{}", |
| 223 | failures.len(), |
| 224 | files.len(), |
| 225 | failures.join("\n") |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | fn walk_extension(roots: &[PathBuf], extension: &str, cap: usize) -> Vec<PathBuf> { |
| 231 | fn walk(dir: &PathBuf, want: &str, depth: usize, out: &mut Vec<PathBuf>) { |
| 232 | if depth == 0 { |
| 233 | return; |
| 234 | } |
| 235 | let Ok(entries) = std::fs::read_dir(dir) else { |
| 236 | return; |
| 237 | }; |
| 238 | for entry in entries.flatten() { |
| 239 | let path = entry.path(); |
| 240 | if path.is_dir() { |
| 241 | walk(&path, want, depth - 1, out); |
| 242 | } else if path |
| 243 | .extension() |
| 244 | .is_some_and(|e| e.eq_ignore_ascii_case(want)) |
| 245 | { |
| 246 | out.push(path); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | let mut found = Vec::new(); |
| 251 | for root in roots { |
| 252 | walk(root, extension, 3, &mut found); |
| 253 | } |
| 254 | found.sort(); |
| 255 | found.truncate(cap); |
| 256 | found |
| 257 | } |