The design language that makes your AI harness better at design.
JavaScript
Crop the comp's own pixels, not a stale PNG cache
Paul Bakausbbb46432b971
diff --git a/Cargo.lock b/Cargo.lockindex 5124f75ee..09453b48e 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -597,6 +597,7 @@ dependencies = [name = "impeccable-comp-verbs"version = "0.1.11"dependencies = [+ "image","impeccable-common","impeccable-comp","impeccable-context",diff --git a/crates/comp-verbs/Cargo.toml b/crates/comp-verbs/Cargo.tomlindex 8bc548fa3..8aac734c7 100644--- a/crates/comp-verbs/Cargo.toml+++ b/crates/comp-verbs/Cargo.toml@@ -26,4 +26,6 @@ sha1 = "0.10"sha2 = "0.10"[dev-dependencies]+# Encodes a non-PNG comp for the stale-cache crop test.+image = { version = "0.25", default-features = false, features = ["gif"] }serde_json = { workspace = true, features = ["preserve_order", "float_roundtrip"] }diff --git a/crates/comp-verbs/src/comp_spec.rs b/crates/comp-verbs/src/comp_spec.rsindex 0960a73d3..2767e2bb6 100644--- a/crates/comp-verbs/src/comp_spec.rs+++ b/crates/comp-verbs/src/comp_spec.rs@@ -1119,6 +1119,15 @@ pub fn region_source_issue(io: &Io, spec: &Value) -> Option<String> {Some(format!("region source {path} changed or is missing since measurement; fix it and re-run comp-spec --regions {path} before continuing"))}+/// The comp's pixels, decoded from its own bytes first: `load_raster` may read+/// a stale sibling PNG cache for a WebP or JPEG source, and both the spec and+/// a crop must use (and identify) the pixels the approved-comp record and the+/// spec's `compSha256` identify.+fn read_comp(io: &Io, comp: &str) -> Result<Image, String> {+ let own = std::fs::read(resolve(io, comp)).ok().and_then(|b| crate::approved_comp::decode_comp(&b));+ own.map(Ok).unwrap_or_else(|| png_io::load_raster(&resolve(io, comp)).map(|(d, _)| d.image))+}+pub fn run(argv: &[String], io: &mut Io) -> i32 {let spec_path = arg_or(argv, "spec", SPEC_PATH).to_string();if flag(argv, "schema") {@@ -1173,8 +1182,8 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {io.err(&format!("comp-spec: {why}\n"));return 2;}- let comp = match png_io::load_raster(&resolve(io, comp_file)) {- Ok((d, _)) => d.image,+ let comp = match read_comp(io, comp_file) {+ Ok(img) => img,Err(e) => {io.err(&format!("comp-spec: cannot read {comp_file}: {e}\n"));return 1;@@ -1226,11 +1235,7 @@ pub fn run(argv: &[String], io: &mut Io) -> i32 {io.err("usage: comp-spec.mjs --comp <png> (--grid | --regions <json> | --auto) [--spec out.json]\n comp-spec.mjs --print | --crop <id> [--out file] [--scale n] | --plate-prompt <id>\n");return 1;};- // Decode the comp's own bytes first: load_raster may read a stale sibling- // PNG cache for a WebP or JPEG source, and the spec must measure (and- // identify) the pixels the approved-comp record identifies.- let own = std::fs::read(resolve(io, comp_path)).ok().and_then(|b| crate::approved_comp::decode_comp(&b));- let comp = match own.map(Ok).unwrap_or_else(|| png_io::load_raster(&resolve(io, comp_path)).map(|(d, _)| d.image)) {+ let comp = match read_comp(io, comp_path) {Ok(img) => img,Err(e) => {io.err(&format!("comp-spec: cannot read {comp_path}: {e}\n"));@@ -1403,6 +1408,38 @@ mod reference_tests {}+ #[test]+ fn crop_reads_the_comp_not_a_stale_png_cache() {+ let dir = std::env::temp_dir().join(format!("impeccable-crop-cache-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&dir);+ std::fs::create_dir_all(&dir).unwrap();+ // The source the spec measured: solid blue, as a GIF so load_raster+ // would consult its sibling cache.+ let blue = image::RgbaImage::from_pixel(8, 8, image::Rgba([0, 0, 255, 255]));+ let mut gif = Vec::new();+ image::DynamicImage::ImageRgba8(blue)+ .write_to(&mut std::io::Cursor::new(&mut gif), image::ImageFormat::Gif)+ .unwrap();+ std::fs::write(dir.join("comp.gif"), &gif).unwrap();+ // A cache left from an earlier version of the comp: solid red.+ let red = r::create_image(8, 8, [255, 0, 0, 255]);+ std::fs::write(dir.join("comp.gif.png"), png_io::encode_png(&red, &[]).unwrap()).unwrap();+ let mut io = Io::stdio();+ io.cwd = dir.clone();+ io.stdout = Box::new(Vec::<u8>::new());+ io.stderr = Box::new(Vec::<u8>::new());+ let hash = crate::approved_comp::file_pixel_sha256(&io, "comp.gif").unwrap();+ let spec = json!({"comp":"comp.gif","compSha256":hash,"regions":[+ {"id":"band","kind":"text","medium":"code","px":{"x":0,"y":0,"w":8,"h":8}}]});+ std::fs::write(dir.join("spec.json"), spec.to_string()).unwrap();+ let argv: Vec<String> = ["--crop", "band", "--spec", "spec.json", "--out", "crop.png"]+ .iter().map(|s| s.to_string()).collect();+ assert_eq!(run(&argv, &mut io), 0);+ let (crop, _) = png_io::load_raster(&dir.join("crop.png")).unwrap();+ assert!(crop.image.data.chunks_exact(4).all(|px| px == [0, 0, 255, 255]));+ let _ = std::fs::remove_dir_all(&dir);+ }+#[test]fn automatic_snap_preserves_separated_navigation_and_multiline_copy() {let mut comp = r::create_image(300, 100, [255,255,255,255]);