| 1 | use std::fs::{self, File}; |
| 2 | use std::path::{Path, PathBuf}; |
| 3 | |
| 4 | use anyhow::{bail, Result}; |
| 5 | use cfg_if::cfg_if; |
| 6 | use walkdir::{DirEntry, WalkDir}; |
| 7 | |
| 8 | use qoi::{decode_to_vec, encode_to_vec}; |
| 9 | |
| 10 | fn find_qoi_png_pairs(root: impl AsRef<Path>) -> Vec<(PathBuf, PathBuf)> { |
| 11 | let root = root.as_ref(); |
| 12 | |
| 13 | let get_ext = |
| 14 | |path: &Path| path.extension().unwrap_or_default().to_string_lossy().to_ascii_lowercase(); |
| 15 | let check_qoi_png_pair = |path: &Path| { |
| 16 | let (qoi, png) = (path.to_path_buf(), path.with_extension("png")); |
| 17 | if qoi.is_file() && get_ext(&qoi) == "qoi" && png.is_file() { |
| 18 | Some((qoi, png)) |
| 19 | } else { |
| 20 | None |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | let mut out = vec![]; |
| 25 | if let Some(pair) = check_qoi_png_pair(root) { |
| 26 | out.push(pair); |
| 27 | } else if root.is_dir() { |
| 28 | out.extend( |
| 29 | WalkDir::new(root) |
| 30 | .follow_links(true) |
| 31 | .into_iter() |
| 32 | .filter_map(Result::ok) |
| 33 | .map(DirEntry::into_path) |
| 34 | .filter_map(|p| check_qoi_png_pair(&p)), |
| 35 | ) |
| 36 | } |
| 37 | out |
| 38 | } |
| 39 | |
| 40 | struct Image { |
| 41 | pub width: u32, |
| 42 | pub height: u32, |
| 43 | pub channels: u8, |
| 44 | pub data: Vec<u8>, |
| 45 | } |
| 46 | |
| 47 | impl Image { |
| 48 | fn from_png(filename: &Path) -> Result<Self> { |
| 49 | let decoder = png::Decoder::new(File::open(filename)?); |
| 50 | let mut reader = decoder.read_info()?; |
| 51 | let mut buf = vec![0; reader.output_buffer_size()]; |
| 52 | let info = reader.next_frame(&mut buf)?; |
| 53 | let bytes = &buf[..info.buffer_size()]; |
| 54 | Ok(Self { |
| 55 | width: info.width, |
| 56 | height: info.height, |
| 57 | channels: info.color_type.samples() as u8, |
| 58 | data: bytes.to_vec(), |
| 59 | }) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn compare_slices(name: &str, desc: &str, result: &[u8], expected: &[u8]) -> Result<()> { |
| 64 | if result == expected { |
| 65 | Ok(()) |
| 66 | } else { |
| 67 | if let Some(i) = |
| 68 | (0..result.len().min(expected.len())).position(|i| result[i] != expected[i]) |
| 69 | { |
| 70 | bail!( |
| 71 | "{}: {} mismatch at byte {}: expected {:?}, got {:?}", |
| 72 | name, |
| 73 | desc, |
| 74 | i, |
| 75 | &expected[i..(i + 4).min(expected.len())], |
| 76 | &result[i..(i + 4).min(result.len())], |
| 77 | ); |
| 78 | } else { |
| 79 | bail!( |
| 80 | "{}: {} length mismatch: expected {}, got {}", |
| 81 | name, |
| 82 | desc, |
| 83 | expected.len(), |
| 84 | result.len() |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn test_reference_images() -> Result<()> { |
| 92 | let pairs = find_qoi_png_pairs("assets"); |
| 93 | assert!(!pairs.is_empty()); |
| 94 | |
| 95 | for (qoi_path, png_path) in &pairs { |
| 96 | let png_name = png_path.file_name().unwrap_or_default().to_string_lossy(); |
| 97 | let img = Image::from_png(png_path)?; |
| 98 | println!("{} {} {} {}", png_name, img.width, img.height, img.channels); |
| 99 | let encoded = encode_to_vec(&img.data, img.width, img.height)?; |
| 100 | let expected = fs::read(qoi_path)?; |
| 101 | assert_eq!(encoded.len(), expected.len()); // this should match regardless |
| 102 | cfg_if! { |
| 103 | if #[cfg(feature = "reference")] { |
| 104 | compare_slices(&png_name, "encoding", &encoded, &expected)?; |
| 105 | } |
| 106 | } |
| 107 | let (_header1, decoded1) = decode_to_vec(&encoded)?; |
| 108 | let (_header2, decoded2) = decode_to_vec(&expected)?; |
| 109 | compare_slices(&png_name, "decoding [1]", &decoded1, &img.data)?; |
| 110 | compare_slices(&png_name, "decoding [2]", &decoded2, &img.data)?; |
| 111 | } |
| 112 | |
| 113 | Ok(()) |
| 114 | } |