Merge branch 'simplify/g1-image' into simplify/g1
Browse files at this commit1 changed file +161 -1
- tools/golden/src/image.rs +161 -1
tools/golden/src/image.rsmodified+161 -1
| @@ -1 +1,161 @@ | |||
| 1 | -pub fn run() {} | 1 | +use crate::{error, guard, hash, name, text}; |
| 2 | +use ec_image::gif_encode::{Encoder, GifOptions}; | ||
| 3 | +use ec_image::{decode_animation, info, png_encode, AnimationFrame, ImageFormat, Pixels}; | ||
| 4 | +use std::path::{Path, PathBuf}; | ||
| 5 | + | ||
| 6 | +const SIZES: [(u32, u32); 3] = [(7, 5), (64, 32), (33, 17)]; | ||
| 7 | +const IMAGE_EXTENSIONS: [&str; 8] = ["png", "jpg", "jpeg", "gif", "webp", "bmp", "tif", "tiff"]; | ||
| 8 | + | ||
| 9 | +pub fn run() { | ||
| 10 | + let mut stills = Vec::new(); | ||
| 11 | + collect(&stills_dir(), &mut stills); | ||
| 12 | + stills.sort(); | ||
| 13 | + for path in &stills { | ||
| 14 | + let label = format!("stills/{}", path.file_name().expect("still name").to_string_lossy()); | ||
| 15 | + decode_file(&label, path); | ||
| 16 | + } | ||
| 17 | + let mut fixtures = Vec::new(); | ||
| 18 | + collect(&ec_testkit::root(), &mut fixtures); | ||
| 19 | + fixtures.sort(); | ||
| 20 | + for path in &fixtures { | ||
| 21 | + decode_file(&name(path), path); | ||
| 22 | + } | ||
| 23 | + encoders(); | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +fn stills_dir() -> PathBuf { | ||
| 27 | + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../crates/ec-image/tests/data/stills") | ||
| 28 | +} | ||
| 29 | + | ||
| 30 | +fn collect(dir: &Path, out: &mut Vec<PathBuf>) { | ||
| 31 | + if !dir.is_dir() { | ||
| 32 | + return; | ||
| 33 | + } | ||
| 34 | + for entry in std::fs::read_dir(dir).expect("read image dir") { | ||
| 35 | + let path = entry.expect("read image entry").path(); | ||
| 36 | + if path.is_dir() { | ||
| 37 | + collect(&path, out); | ||
| 38 | + } else if is_image(&path) { | ||
| 39 | + out.push(path); | ||
| 40 | + } | ||
| 41 | + } | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +fn is_image(path: &Path) -> bool { | ||
| 45 | + let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase(); | ||
| 46 | + IMAGE_EXTENSIONS.contains(&ext.as_str()) | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +fn decode_file(label: &str, path: &Path) { | ||
| 50 | + match std::fs::read(path) { | ||
| 51 | + Ok(bytes) => { | ||
| 52 | + let codec = ImageFormat::guess(&bytes).map_or("none", |f| f.extension()); | ||
| 53 | + decode_bytes(codec, label, &bytes); | ||
| 54 | + } | ||
| 55 | + Err(err) => error(&format!("image/io/{label}/0"), err), | ||
| 56 | + } | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +fn decode_bytes(codec: &str, label: &str, bytes: &[u8]) { | ||
| 60 | + let base = format!("image/{codec}/{label}"); | ||
| 61 | + let info_case = format!("{base}/info"); | ||
| 62 | + guard(&info_case, || match info(bytes) { | ||
| 63 | + Ok(i) => text( | ||
| 64 | + &info_case, | ||
| 65 | + format!("{:?} {}x{} orientation={}", i.format, i.width, i.height, i.orientation), | ||
| 66 | + ), | ||
| 67 | + Err(err) => error(&info_case, err), | ||
| 68 | + }); | ||
| 69 | + guard(&base, || match decode_animation(bytes) { | ||
| 70 | + Ok(frames) => { | ||
| 71 | + for (index, frame) in frames.iter().enumerate() { | ||
| 72 | + frame_lines(&format!("{base}/{index}"), codec, frame); | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | + Err(err) => error(&base, err), | ||
| 76 | + }); | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +fn frame_lines(case: &str, codec: &str, frame: &AnimationFrame) { | ||
| 80 | + let image = &frame.image; | ||
| 81 | + hash(case, &pixel_bytes(&image.pixels)); | ||
| 82 | + hash(&format!("{case}/rgba8"), &image.to_rgba8()); | ||
| 83 | + text( | ||
| 84 | + &format!("{case}/meta"), | ||
| 85 | + format!( | ||
| 86 | + "{codec} {}x{} channels={} depth={} alpha={} delay={}/{} meta={:?}", | ||
| 87 | + image.width, | ||
| 88 | + image.height, | ||
| 89 | + image.pixels.channels(), | ||
| 90 | + image.pixels.bit_depth(), | ||
| 91 | + image.pixels.has_alpha(), | ||
| 92 | + frame.delay_num, | ||
| 93 | + frame.delay_den, | ||
| 94 | + image.meta | ||
| 95 | + ), | ||
| 96 | + ); | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +fn pixel_bytes(pixels: &Pixels) -> Vec<u8> { | ||
| 100 | + match pixels { | ||
| 101 | + Pixels::L8(v) | Pixels::La8(v) | Pixels::Rgb8(v) | Pixels::Rgba8(v) => v.to_vec(), | ||
| 102 | + Pixels::L16(v) | Pixels::La16(v) | Pixels::Rgb16(v) | Pixels::Rgba16(v) => { | ||
| 103 | + v.iter().flat_map(|s| s.to_le_bytes()).collect() | ||
| 104 | + } | ||
| 105 | + } | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +fn gradient(w: u32, h: u32, channels: usize, shift: usize) -> Vec<u8> { | ||
| 109 | + let mut out = Vec::new(); | ||
| 110 | + for y in 0..h as usize { | ||
| 111 | + for x in 0..w as usize { | ||
| 112 | + for c in 0..channels { | ||
| 113 | + out.push(((x * 37 + y * 19 + c * 83 + shift * 11) % 256) as u8); | ||
| 114 | + } | ||
| 115 | + } | ||
| 116 | + } | ||
| 117 | + out | ||
| 118 | +} | ||
| 119 | + | ||
| 120 | +fn encoders() { | ||
| 121 | + for (w, h) in SIZES { | ||
| 122 | + let rgb = gradient(w, h, 3, 0); | ||
| 123 | + let rgba = gradient(w, h, 4, 0); | ||
| 124 | + let label = format!("encode-rgb8-{w}x{h}"); | ||
| 125 | + guard(&format!("image/png/{label}/bytes"), || { | ||
| 126 | + encoded("png", &label, png_encode::encode_rgb8(w, h, &rgb)) | ||
| 127 | + }); | ||
| 128 | + let label = format!("encode-rgba8-{w}x{h}"); | ||
| 129 | + guard(&format!("image/png/{label}/bytes"), || { | ||
| 130 | + encoded("png", &label, png_encode::encode_rgba8(w, h, &rgba)) | ||
| 131 | + }); | ||
| 132 | + for (mode, opts) in [ | ||
| 133 | + ("default", GifOptions::default()), | ||
| 134 | + ("dither", GifOptions { loop_count: Some(0), max_colors: 16, dither: true }), | ||
| 135 | + ] { | ||
| 136 | + let label = format!("encode-{mode}-{w}x{h}"); | ||
| 137 | + guard(&format!("image/gif/{label}/bytes"), || { | ||
| 138 | + encoded("gif", &label, gif_bytes(w, h, opts)) | ||
| 139 | + }); | ||
| 140 | + } | ||
| 141 | + } | ||
| 142 | +} | ||
| 143 | + | ||
| 144 | +fn encoded(codec: &str, label: &str, result: ec_image::Result<Vec<u8>>) { | ||
| 145 | + let case = format!("image/{codec}/{label}/bytes"); | ||
| 146 | + match result { | ||
| 147 | + Ok(bytes) => { | ||
| 148 | + hash(&case, &bytes); | ||
| 149 | + decode_bytes(codec, label, &bytes); | ||
| 150 | + } | ||
| 151 | + Err(err) => error(&case, err), | ||
| 152 | + } | ||
| 153 | +} | ||
| 154 | + | ||
| 155 | +fn gif_bytes(w: u32, h: u32, opts: GifOptions) -> ec_image::Result<Vec<u8>> { | ||
| 156 | + let mut encoder = Encoder::new(w as u16, h as u16, opts)?; | ||
| 157 | + for shift in 0..3 { | ||
| 158 | + encoder.push_rgba8(&gradient(w, h, 4, shift), 5)?; | ||
| 159 | + } | ||
| 160 | + encoder.finish() | ||
| 161 | +} | ||