Crop Image: keep faint colors, the box's content and the settings of a failed crop

- Resampling at a resolution asked for weights colors by alpha in 16 bits a
  channel, so a pixel with alpha 1 keeps its color instead of turning black.
- A box between pixels is drawn with the image stretched so the box fills
  the whole pixels drawn; nothing past the box is squeezed into it.
- A linked W or H below a point raises both sides together, keeping the
  box's proportions.
- Apply no longer clears the box and the resolution; leaving the tool does,
  so a crop that fails keeps them for another try.
This commit is contained in:
dotCooCoo
2026-11-15 12:48:39 -08:00
parent 431947bad6
commit 49d53b8aaf
3 changed files with 142 additions and 39 deletions
+31 -13
View File
@@ -1030,23 +1030,29 @@ fn check_crop_size(w: f64, h: f64) -> Result<()> {
}
/// `img` resampled to `w` × `h` with its colors weighted by their alpha, so the color of a fully
/// transparent pixel does not show at the edges of what is opaque.
/// transparent pixel does not show at the edges of what is opaque. The weighted colors are held in
/// 16 bits a channel until the end, so a nearly transparent pixel keeps its color.
fn resize_premultiplied(img: &image::RgbaImage, w: u32, h: u32) -> image::RgbaImage {
let mut pre = img.clone();
for p in pre.pixels_mut() {
let a = u32::from(p[3]);
for c in &mut p.0[..3] {
*c = ((u32::from(*c) * a + 127) / 255) as u8;
let mut pre = image::ImageBuffer::<image::Rgba<u16>, Vec<u16>>::new(img.width(), img.height());
for (d, s) in pre.pixels_mut().zip(img.pixels()) {
let a = u32::from(s[3]);
for (dc, sc) in d.0[..3].iter_mut().zip(&s.0[..3]) {
// c × 257 × a / 255, rounded: the color scaled to 16 bits and weighted.
*dc = ((u32::from(*sc) * 257 * a + 127) / 255) as u16;
}
d.0[3] = (a * 257) as u16;
}
let mut out = image::imageops::resize(&pre, w, h, image::imageops::FilterType::Lanczos3);
for p in out.pixels_mut() {
let a = u32::from(p[3]);
for c in &mut p.0[..3] {
*c = (u32::from(*c) * 255 + a / 2).checked_div(a).unwrap_or(0).min(255) as u8;
let out = image::imageops::resize(&pre, w, h, image::imageops::FilterType::Lanczos3);
let mut res = image::RgbaImage::new(out.width(), out.height());
for (d, s) in res.pixels_mut().zip(out.pixels()) {
let a = u64::from(s[3]);
for (dc, sc) in d.0[..3].iter_mut().zip(&s.0[..3]) {
// c × 65535 / a back to 8 bits (÷ 257), rounded: c × 255 / a.
*dc = (u64::from(*sc) * 255 + a / 2).checked_div(a).unwrap_or(0).min(255) as u8;
}
d.0[3] = ((a + 128) / 257).min(255) as u8;
}
out
res
}
/// `img` as PNG bytes.
@@ -1141,8 +1147,20 @@ fn crop_image(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(e) = bare.extras_if_any() {
e.color_mask = None;
}
let (dw, dh) = ((r.width() * scale).round().max(1.0), (r.height() * scale).round().max(1.0));
let region = Rect::new(r.x0, r.y0, r.x0 + dw / scale, r.y0 + dh / scale);
// At a resolution asked for, the drawing is fitted to the box below: the image is stretched
// about the box's corner so the box fills the whole pixels drawn, and nothing past it is drawn.
if target.is_some()
&& let NodeKind::Image(bi) = &mut bare.kind
{
let o = r.origin().to_vec2();
bi.xf = Affine::translate(o)
* Affine::scale_non_uniform(region.width() / r.width(), region.height() / r.height())
* Affine::translate(-o)
* bi.xf;
}
let tmp = isolated_doc(&st.doc, vec![bare]);
let region = Rect::new(r.x0, r.y0, r.x0 + (r.width() * scale).round().max(1.0) / scale, r.y0 + (r.height() * scale).round().max(1.0) / scale);
let (png, w, h, warning) =
render_png(&tmp, region, scale, &RasterExportOptions::default().render_options(RasterFormat::Png), RasterColorModel::Document)?;
// At a resolution asked for, the drawing is resampled to the box's size at that resolution and
+51 -1
View File
@@ -1600,7 +1600,10 @@ fn crop_image_resamples_the_cut_to_the_resolution_asked_for() {
let NodeKind::Image(im) = &node(&s, img).kind else { panic!("an image") };
let out = image::load_from_memory(&s.doc().unwrap().doc.images[&im.key].bytes).unwrap().to_rgba8();
let cut = image::imageops::crop_imm(&px, 10, 5, 30, 20).to_image();
assert_eq!(out, image::imageops::resize(&cut, 15, 10, image::imageops::FilterType::Lanczos3));
// Resampled in 16 bits a channel: within a level of the 8-bit resampling.
let want = image::imageops::resize(&cut, 15, 10, image::imageops::FilterType::Lanczos3);
assert_eq!(out.dimensions(), want.dimensions());
assert!(out.as_raw().iter().zip(want.as_raw()).all(|(a, b)| a.abs_diff(*b) <= 1));
}
/// Resampling weights colors by their alpha: the color hidden in fully transparent pixels does not
@@ -1626,6 +1629,53 @@ fn crop_image_resampling_keeps_hidden_colors_out_of_the_edges() {
}
}
/// Resampling keeps the color of pixels that are nearly transparent: a uniform color at the lowest
/// alpha comes out as that color, not black.
#[test]
fn crop_image_resampling_keeps_the_color_of_nearly_transparent_pixels() {
let px = image::RgbaImage::from_pixel(100, 80, image::Rgba([100, 50, 25, 1]));
let mut png = Vec::new();
px.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut s = Session::new();
s.execute("document.open", &json!({"name": "faint.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
s.execute("select.all", &json!({})).unwrap();
let img = selected(&s)[0];
s.execute("object.scale", &json!({"sx": 200, "origin": [0, 0]})).unwrap();
s.execute("object.cropImage", &json!({"rect": [20, 10, 60, 40], "ppi": 18})).unwrap();
let NodeKind::Image(im) = &node(&s, img).kind else { panic!("an image") };
let out = image::load_from_memory(&s.doc().unwrap().doc.images[&im.key].bytes).unwrap().to_rgba8();
for p in out.pixels() {
let near = |a: u8, b: u8| a.abs_diff(b) <= 1;
assert!(p[3] == 1 && near(p[0], 100) && near(p[1], 50) && near(p[2], 25), "{p:?}");
}
}
/// A box between pixels drawn at a resolution asked for holds what is inside the box only, at its
/// place: the edge of the image's content lands where it was in the box.
#[test]
fn crop_image_between_pixels_keeps_the_content_at_its_place() {
// White pixels 0 to 49 across, black from 50: at 2 pt a pixel, the edge is at x = 100 pt.
let px = image::RgbaImage::from_fn(100, 80, |x, _| if x < 50 { image::Rgba([255, 255, 255, 255]) } else { image::Rgba([0, 0, 0, 255]) });
let mut png = Vec::new();
px.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
let mut s = Session::new();
s.execute("document.open", &json!({"name": "edge.png", "dataBase64": vectorcraft_format::base64_encode(&png)})).unwrap();
s.execute("select.all", &json!({})).unwrap();
let img = selected(&s)[0];
s.execute("object.scale", &json!({"sx": 200, "origin": [0, 0]})).unwrap();
// 101 × 31 pt from (21, 11), off the pixel grid, at 35 ppi: 49 × 15 px. The edge is 79 pt into
// the 101 pt box, so each row holds 49 × 79 / 101 pixels' worth of white.
let r = s.execute("object.cropImage", &json!({"rect": [21, 11, 101, 31], "ppi": 35})).unwrap();
assert_eq!((r["width"].as_u64().unwrap(), r["height"].as_u64().unwrap()), (49, 15), "{r}");
let NodeKind::Image(im) = &node(&s, img).kind else { panic!("an image") };
let out = image::load_from_memory(&s.doc().unwrap().doc.images[&im.key].bytes).unwrap().to_rgba8();
let white: f64 = out.pixels().map(|p| f64::from(p[1]) / 255.0).sum::<f64>() / f64::from(out.height());
let want = 49.0 * 79.0 / 101.0;
assert!((white - want).abs() < 0.1, "{white} pixels of white a row, want {want}");
let b = bounds(&s, img);
assert!(close(b.x0, 21.0) && close(b.y0, 11.0) && close(b.width(), 101.0) && close(b.height(), 31.0), "{b:?}");
}
/// A box between pixels is drawn at the resolution asked for, and lands on the box.
#[test]
fn crop_image_between_pixels_draws_at_the_resolution_asked_for() {
+60 -25
View File
@@ -75,7 +75,8 @@ pub fn check_options(values: &serde_json::Map<String, Value>, max: Option<f64>)
/// The box `r` over an image with bounds `image`, with one Control bar field set: `x` or `y` moves
/// the box so its `reference` point is there; `width` or `height` resizes it about that point, the
/// other side following in proportion when `linked`. Sizes below [`MIN`] are raised to it. A
/// other side following in proportion when `linked`. Sizes below [`MIN`] are raised to it, a
/// linked size with both sides together so it keeps its proportions. A
/// linked size is held to the image with both sides scaled together, and a box that fits is slid
/// back within the image, so its proportions survive being kept within it. `None` for another key
/// or a value that isn't finite.
@@ -89,20 +90,13 @@ pub fn edited_box(r: Rect, image: Rect, reference: usize, linked: bool, key: &st
match key {
"x" => return Some(fit(r + Vec2::new(v - p.x, 0.0))),
"y" => return Some(fit(r + Vec2::new(0.0, v - p.y))),
"width" => {
let nw = v.max(MIN);
if linked && w > 0.0 {
h = (h * nw / w).max(MIN);
}
w = nw;
}
"height" => {
let nh = v.max(MIN);
if linked && h > 0.0 {
w = (w * nh / h).max(MIN);
}
h = nh;
// Linked, both sides take one scale, raised so neither side falls below MIN.
"width" | "height" if linked && w > 0.0 && h > 0.0 => {
let k = (v / if key == "width" { w } else { h }).max(MIN / w).max(MIN / h);
(w, h) = (w * k, h * k);
}
"width" => w = v.max(MIN),
"height" => h = v.max(MIN),
_ => return None,
}
if linked {
@@ -204,11 +198,12 @@ impl CropImageTool {
}
/// Crop to the box at the chosen resolution and go back to the Selection tool (only back when
/// the box covers the image and the resolution is the image's own).
/// the box covers the image and the resolution is the image's own). The box and the resolution
/// are forgotten when the tool is left, so a crop that fails keeps them for another try.
fn apply(&mut self, cx: &ToolContext) -> Vec<Action> {
let current = self.current(cx);
let ppi = self.ppi;
self.reset();
self.drag = None;
let mut out = vec![];
if let Some((id, image, r)) = current {
let im = match cx.doc.node(id).map(|n| &n.kind) {
@@ -456,6 +451,15 @@ mod tests {
}
}
/// Enter, then the switch to the Selection tool that a crop that ran is followed by.
fn enter(t: &mut CropImageTool, cx: &ToolContext) -> Vec<Action> {
let acts = t.key(cx, ToolKey::Enter, Mods::default());
if acts.contains(&Action::SwitchTool("selection".into())) {
assert!(t.deactivate(cx).is_empty());
}
acts
}
fn crop_rect(acts: &[Action]) -> Option<Vec<f64>> {
acts.iter().find_map(|a| match a {
Action::Exec(c, v) if c == "object.cropImage" => Some(v["rect"].as_array()?.iter().filter_map(Value::as_f64).collect()),
@@ -487,11 +491,11 @@ mod tests {
assert_eq!(ov.iter().filter(|o| matches!(o, Overlay::Highlight { .. })).count(), 2, "above and right of the box");
assert_eq!(ov.iter().filter(|o| matches!(o, Overlay::Handle { .. })).count(), 8);
// Enter crops to it and goes back to the Selection tool.
let acts = t.key(&cx, ToolKey::Enter, Mods::default());
let acts = enter(&mut t, &cx);
assert_eq!(crop_rect(&acts), Some(vec![50.0, 70.0, 130.0, 90.0]));
assert_eq!(acts.last(), Some(&Action::SwitchTool("selection".into())));
assert!(t.options().get("rect").is_none(), "the next crop starts afresh");
assert!(crop_rect(&t.key(&cx, ToolKey::Enter, Mods::default())).is_none(), "the whole image: nothing to cut");
assert!(crop_rect(&enter(&mut t, &cx)).is_none(), "the whole image: nothing to cut");
}
/// In a view turned 90° the handles' cursors turn with the view.
@@ -515,10 +519,10 @@ mod tests {
let mut t = CropImageTool::default();
drag(&mut t, &cx, (100.0, 150.0), (110.0, 150.0));
assert_eq!(t.key(&cx, ToolKey::Escape, Mods::default()), vec![Action::SwitchTool("selection".into())]);
assert_eq!(crop_rect(&t.key(&cx, ToolKey::Enter, Mods::default())), Some(vec![0.0, 100.0, 150.0, 100.0]));
assert_eq!(crop_rect(&enter(&mut t, &cx)), Some(vec![0.0, 100.0, 150.0, 100.0]));
// The Control bar sets the box; it stays within the image.
t.set_option("rect", &json!([100, 120, 500, 20]));
assert_eq!(crop_rect(&t.key(&cx, ToolKey::Enter, Mods::default())), Some(vec![100.0, 120.0, 50.0, 20.0]));
assert_eq!(crop_rect(&enter(&mut t, &cx)), Some(vec![100.0, 120.0, 50.0, 20.0]));
// Nothing selected: no box, and the keys go elsewhere.
let none = Selection::default();
let cx = crate::testutil::cx(&d, &none, &p);
@@ -660,19 +664,50 @@ mod tests {
};
let mut t = CropImageTool::default();
t.set_option("ppi", &json!(20));
assert_eq!(exec(&t.key(&cx, ToolKey::Enter, Mods::default())), Some(json!({ "rect": [50.0, 60.0, 200.0, 100.0], "ppi": 20.0 })));
assert_eq!(exec(&enter(&mut t, &cx)), Some(json!({ "rect": [50.0, 60.0, 200.0, 100.0], "ppi": 20.0 })));
assert!(t.options().get("ppi").is_none(), "the next crop starts at the image's own");
t.set_option("ppi", &json!(36));
assert_eq!(exec(&t.key(&cx, ToolKey::Enter, Mods::default())), None, "the image's own over the whole image: nothing to do");
assert_eq!(exec(&enter(&mut t, &cx)), None, "the image's own over the whole image: nothing to do");
t.set_option("ppi", &json!(36));
t.set_option("rect", &json!([60, 70, 100, 50]));
assert_eq!(exec(&t.key(&cx, ToolKey::Enter, Mods::default())), Some(json!({ "rect": [60.0, 70.0, 100.0, 50.0] })));
assert_eq!(exec(&enter(&mut t, &cx)), Some(json!({ "rect": [60.0, 70.0, 100.0, 50.0] })));
// A resolution set before this image was selected is held to its limit: 300 → its own 36.
t.set_option("ppi", &json!(300));
t.set_option("rect", &json!([60, 70, 100, 50]));
assert_eq!(exec(&t.key(&cx, ToolKey::Enter, Mods::default())), Some(json!({ "rect": [60.0, 70.0, 100.0, 50.0] })));
assert_eq!(exec(&enter(&mut t, &cx)), Some(json!({ "rect": [60.0, 70.0, 100.0, 50.0] })));
t.set_option("ppi", &json!(300));
assert_eq!(exec(&t.key(&cx, ToolKey::Enter, Mods::default())), None, "held to its own over the whole image: nothing to do");
assert_eq!(exec(&enter(&mut t, &cx)), None, "held to its own over the whole image: nothing to do");
}
/// A crop that fails leaves the tool chosen (the switch to the Selection tool is not made), so
/// the box and the resolution stay for another try; leaving the tool forgets them.
#[test]
fn the_box_and_resolution_stay_until_the_tool_is_left() {
let (d, s) = doc_with_image((50.0, 60.0));
let p = paint();
let cx = cx(&d, &s, &p);
let mut t = CropImageTool::default();
t.set_option("ppi", &json!(20));
t.set_option("rect", &json!([60, 70, 100, 50]));
let acts = t.key(&cx, ToolKey::Enter, Mods::default());
assert_eq!(crop_rect(&acts), Some(vec![60.0, 70.0, 100.0, 50.0]));
assert_eq!(t.options()["rect"], json!([60.0, 70.0, 100.0, 50.0]));
assert_eq!(t.options()["ppi"], json!(20.0));
assert!(t.deactivate(&cx).is_empty());
assert!(t.options().get("rect").is_none() && t.options().get("ppi").is_none());
}
/// A linked size raised to the smallest box keeps the box's proportions: both sides grow
/// together until the shorter one is a point.
#[test]
fn a_linked_size_at_the_smallest_keeps_its_proportions() {
let big = Rect::new(0.0, 0.0, 1000.0, 1000.0);
let wide = Rect::new(10.0, 20.0, 110.0, 70.0);
assert_eq!(edited_box(wide, big, 0, true, "width", 1.0), Some(Rect::new(10.0, 20.0, 12.0, 21.0)));
assert_eq!(edited_box(wide, big, 0, true, "height", 0.25), Some(Rect::new(10.0, 20.0, 12.0, 21.0)));
let tall = Rect::new(10.0, 20.0, 60.0, 120.0);
assert_eq!(edited_box(tall, big, 0, true, "height", 1.0), Some(Rect::new(10.0, 20.0, 11.0, 22.0)));
assert_eq!(edited_box(tall, big, 0, true, "width", -3.0), Some(Rect::new(10.0, 20.0, 11.0, 22.0)));
}
#[test]