use crate::{error, guard, hash, name, text}; use ec_image::gif_encode::{Encoder, GifOptions}; use ec_image::{decode_animation, info, png_encode, AnimationFrame, ImageFormat, Pixels}; use std::path::{Path, PathBuf}; const SIZES: [(u32, u32); 3] = [(7, 5), (64, 32), (33, 17)]; const IMAGE_EXTENSIONS: [&str; 8] = ["png", "jpg", "jpeg", "gif", "webp", "bmp", "tif", "tiff"]; pub fn run() { let mut stills = Vec::new(); collect(&stills_dir(), &mut stills); stills.sort(); for path in &stills { let label = format!("stills/{}", path.file_name().expect("still name").to_string_lossy()); decode_file(&label, path); } let mut fixtures = Vec::new(); collect(&ec_testkit::root(), &mut fixtures); fixtures.sort(); for path in &fixtures { decode_file(&name(path), path); } encoders(); } fn stills_dir() -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")).join("../../crates/ec-image/tests/data/stills") } fn collect(dir: &Path, out: &mut Vec) { if !dir.is_dir() { return; } for entry in std::fs::read_dir(dir).expect("read image dir") { let path = entry.expect("read image entry").path(); if path.is_dir() { collect(&path, out); } else if is_image(&path) { out.push(path); } } } fn is_image(path: &Path) -> bool { let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase(); IMAGE_EXTENSIONS.contains(&ext.as_str()) } fn decode_file(label: &str, path: &Path) { match std::fs::read(path) { Ok(bytes) => { let codec = ImageFormat::guess(&bytes).map_or("none", |f| f.extension()); decode_bytes(codec, label, &bytes); } Err(err) => error(&format!("image/io/{label}/0"), err), } } fn decode_bytes(codec: &str, label: &str, bytes: &[u8]) { let base = format!("image/{codec}/{label}"); let info_case = format!("{base}/info"); guard(&info_case, || match info(bytes) { Ok(i) => text( &info_case, format!("{:?} {}x{} orientation={}", i.format, i.width, i.height, i.orientation), ), Err(err) => error(&info_case, err), }); guard(&base, || match decode_animation(bytes) { Ok(frames) => { for (index, frame) in frames.iter().enumerate() { frame_lines(&format!("{base}/{index}"), codec, frame); } } Err(err) => error(&base, err), }); } fn frame_lines(case: &str, codec: &str, frame: &AnimationFrame) { let image = &frame.image; hash(case, &pixel_bytes(&image.pixels)); hash(&format!("{case}/rgba8"), &image.to_rgba8()); text( &format!("{case}/meta"), format!( "{codec} {}x{} channels={} depth={} alpha={} delay={}/{} meta={:?}", image.width, image.height, image.pixels.channels(), image.pixels.bit_depth(), image.pixels.has_alpha(), frame.delay_num, frame.delay_den, image.meta ), ); } fn pixel_bytes(pixels: &Pixels) -> Vec { match pixels { Pixels::L8(v) | Pixels::La8(v) | Pixels::Rgb8(v) | Pixels::Rgba8(v) => v.to_vec(), Pixels::L16(v) | Pixels::La16(v) | Pixels::Rgb16(v) | Pixels::Rgba16(v) => { v.iter().flat_map(|s| s.to_le_bytes()).collect() } } } fn gradient(w: u32, h: u32, channels: usize, shift: usize) -> Vec { let mut out = Vec::new(); for y in 0..h as usize { for x in 0..w as usize { for c in 0..channels { out.push(((x * 37 + y * 19 + c * 83 + shift * 11) % 256) as u8); } } } out } fn encoders() { for (w, h) in SIZES { let rgb = gradient(w, h, 3, 0); let rgba = gradient(w, h, 4, 0); let label = format!("encode-rgb8-{w}x{h}"); guard(&format!("image/png/{label}/bytes"), || { encoded("png", &label, png_encode::encode_rgb8(w, h, &rgb)) }); let label = format!("encode-rgba8-{w}x{h}"); guard(&format!("image/png/{label}/bytes"), || { encoded("png", &label, png_encode::encode_rgba8(w, h, &rgba)) }); for (mode, opts) in [ ("default", GifOptions::default()), ("dither", GifOptions { loop_count: Some(0), max_colors: 16, dither: true }), ] { let label = format!("encode-{mode}-{w}x{h}"); guard(&format!("image/gif/{label}/bytes"), || { encoded("gif", &label, gif_bytes(w, h, opts)) }); } } } fn encoded(codec: &str, label: &str, result: ec_image::Result>) { let case = format!("image/{codec}/{label}/bytes"); match result { Ok(bytes) => { hash(&case, &bytes); decode_bytes(codec, label, &bytes); } Err(err) => error(&case, err), } } fn gif_bytes(w: u32, h: u32, opts: GifOptions) -> ec_image::Result> { let mut encoder = Encoder::new(w as u16, h as u16, opts)?; for shift in 0..3 { encoder.push_rgba8(&gradient(w, h, 4, shift), 5)?; } encoder.finish() }