Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Quick Selection: Photoshop's behaviour, measured and fitted (#1966)

Unemars9da40a1c7a6c
diff --git a/crates/algo/examples/bench_smartselect.rs b/crates/algo/examples/bench_smartselect.rs
index 8e8c5807f..66146e50f 100644
--- a/crates/algo/examples/bench_smartselect.rs
+++ b/crates/algo/examples/bench_smartselect.rs
@@ -63,7 +63,7 @@ fn main() {
for size in [30.0f32, 300.0] {
let x = c + rad - size * 2.0;
let t = Instant::now();
- let r = quick::quick_select(&sampler, canvas, &[(x, c - size), (x, c + size)], size, quick::WORK_PX).expect("region");
+ let r = quick::quick_select(&sampler, canvas, &[(x, c - size), (x, c + size)], size).expect("region");
println!("quick selection stroke (brush {size} px): {:.2?} (region {}x{})", t.elapsed(), r.bbox.width(), r.bbox.height());
}
diff --git a/crates/algo/src/segment/mod.rs b/crates/algo/src/segment/mod.rs
index 5c6da253e..ee85294d2 100644
--- a/crates/algo/src/segment/mod.rs
+++ b/crates/algo/src/segment/mod.rs
@@ -11,9 +11,8 @@
//! Superpixel Methods", PAMI 2012).
//! * [`grabcut`]: GrabCut (Rother, Kolmogorov & Blake, SIGGRAPH 2004): iterated graph cuts
//! (Boykov & Jolly, ICCV 2001) with GMM colour models.
-//! * [`quick`]: Quick Selection brush: stroke seeds, geodesic background sampling (Criminisi et
-//! al., "GeoS", ECCV 2008; Bai & Sapiro 2007) and a contrast-sensitive graph cut, in the spirit
-//! of "Paint Selection" (Liu, Sun & Shum, SIGGRAPH 2009).
+//! * [`quick`]: Quick Selection brush: one contrast-sensitive min cut with an area cost, its
+//! behaviour and constants measured on Photoshop (see the module).
//! * [`subject`]: Select Subject: superpixel saliency (boundary connectivity + background-weighted
//! contrast, Zhu et al., "Saliency Optimization from Robust Background Detection", CVPR 2014;
//! frequency-tuned saliency, Achanta et al., CVPR 2009) seeding a GrabCut. Heuristic.
@@ -283,6 +282,57 @@ pub fn grid_cut(img: &RgbImage, cost_fg: &[f32], cost_bg: &[f32], fixed: &[u8],
out
}
+/// [`grid_cut`] with any edge weight: separating neighbours costs `weight(‖z_p−z_q‖²) / dist(p,q)`.
+pub fn grid_cut_with(img: &RgbImage, cost_fg: &[f32], cost_bg: &[f32], fixed: &[u8], weight: impl Fn(f32) -> f32) -> Vec<bool> {
+ let (w, h) = (img.w as i32, img.h as i32);
+ let n = img.w * img.h;
+ let mut idx = vec![u32::MAX; n];
+ let mut count = 0u32;
+ for i in 0..n {
+ if fixed[i] == FREE {
+ idx[i] = count;
+ count += 1;
+ }
+ }
+ let mut out: Vec<bool> = fixed.iter().map(|f| *f == HARD_FG).collect();
+ if count == 0 {
+ return out;
+ }
+ let mut g = Graph::with_capacity(count as usize, count as usize * 4);
+ for i in 0..n {
+ if idx[i] != u32::MAX {
+ g.add_tweights(idx[i] as usize, cost_bg[i], cost_fg[i]);
+ }
+ }
+ for y in 0..h {
+ for x in 0..w {
+ let i = (y * w + x) as usize;
+ let a = img.px[i];
+ for (dx, dy, len) in NB {
+ let (nx, ny) = (x + dx, y + dy);
+ if nx < 0 || nx >= w || ny >= h {
+ continue;
+ }
+ let j = (ny * w + nx) as usize;
+ let wt = weight(d2(a, img.px[j])) / len;
+ match (idx[i] != u32::MAX, idx[j] != u32::MAX) {
+ (true, true) => g.add_edge(idx[i] as usize, idx[j] as usize, wt, wt),
+ (true, false) => add_fixed_link(&mut g, idx[i] as usize, fixed[j], wt),
+ (false, true) => add_fixed_link(&mut g, idx[j] as usize, fixed[i], wt),
+ (false, false) => {}
+ }
+ }
+ }
+ }
+ g.maxflow();
+ for i in 0..n {
+ if idx[i] != u32::MAX {
+ out[i] = g.in_source(idx[i] as usize);
+ }
+ }
+ out
+}
+
fn add_fixed_link(g: &mut Graph, node: usize, fixed: u8, wt: f32) {
if fixed == HARD_FG {
// Labelling `node` background cuts the link.
diff --git a/crates/algo/src/segment/quick.rs b/crates/algo/src/segment/quick.rs
index f38d19366..ec9947d22 100644
--- a/crates/algo/src/segment/quick.rs
+++ b/crates/algo/src/segment/quick.rs
@@ -1,49 +1,93 @@
-//! Quick Selection brush.
+//! Quick Selection brush, behaving like Photoshop's.
//!
-//! A brush stroke seeds a graph cut in a working window around the stroke:
+//! Photoshop 25.4 was measured as a black box: synthetic images and public-domain paintings,
+//! strokes driven on its canvas, the exact selection masks read back (no Photoshop code or
+//! assets were used). What it does:
//!
-//! 1. Stroked pixels are hard foreground; a GMM of their colours is the foreground model.
-//! 2. A geodesic distance from the stroke over a barrier map (the colour gradient of a lightly
-//! smoothed image above the texture level found under the stroke; raster-scan geodesic
-//! transform, Toivanen 1996; Criminisi, Sharp & Blake, "GeoS: Geodesic Image Segmentation",
-//! ECCV 2008) separates what is reachable without crossing an edge from what lies behind
-//! edges. Pixels behind edges are
-//! sampled for the background GMM (mixed with a uniform outlier density, so a window without
-//! any edge simply selects everything similar), and the distance also enters the data term.
-//! 3. A contrast-sensitive min cut (Boykov & Jolly 2001) snaps the result to image edges; only
-//! components touching the stroke are kept.
+//! - The brush footprint is always selected, whatever colours it covers.
+//! - Beyond the footprint the selection grows to image edges, but every selected pixel costs a
+//! little: a click selects a whole object only when the object is small enough to be worth it
+//! for that brush. With a 30 px brush a click selects a 220 px red square on blue but not a
+//! 240 px one, a 120 x 400 bar but not a 160 x 400 one, and a weak edge (a step of 15 levels)
+//! only around a 100 px square. A click in a flat area selects about the brush disc.
+//! - A stroke is one set of seeds: a short drag inside a square too big for a click selects it.
+//! - Colours elsewhere in the image make no difference (no global colour model).
+//! - It works on the whole document when it has at most 512 x 512 pixels and at exactly half
+//! resolution above (any size, any brush), aligned to the document's corner; the mask is then
+//! scaled up bilinearly (edges show a 25 % / 75 % step).
//!
-//! This follows the spirit of "Paint Selection" (Liu, Sun & Shum, SIGGRAPH 2009) and of
-//! geodesic matting (Bai & Sapiro, ICCV 2007). Large windows run at a reduced working resolution
-//! and the boundary is re-cut at full resolution.
+//! The model reproducing this is one min cut (Boykov & Jolly, ICCV 2001; max-flow by Boykov &
+//! Kolmogorov, PAMI 2004) over the working image minimising
+//!
+//! ```text
+//! sum over selected pixels p beyond the footprint of AREA * (1 + DIST * d(p)^POW)
+//! + sum over cut neighbour pairs p~q of w(|I_p - I_q|) / |p - q|
+//! w(D) = FLOOR + 1 / (1 + (D / SIGMA)^K)
+//! ```
+//!
+//! with the (slightly grown) footprint hard foreground and `d` the distance from it. The
+//! constants were fitted to Photoshop's masks.
use photocraft_geom::Rect;
-use super::gmm::Gmm;
-use super::{FREE, HARD_FG, Region, RgbImage, Sampler, contrast_beta, grid_cut, keep_seeded, subsample};
+use super::{FREE, HARD_FG, Region, RgbImage, Sampler};
-/// Covariance ridge for the (often tiny) stroke sample: ~6 levels of standard deviation.
-const QREG: f32 = 6e-4;
-/// Geodesic distance (colour units above noise) that counts as "behind an edge".
-const EDGE: f32 = 0.06;
-/// Neighbour colour steps below this never count as edges (smooth shading, fine noise).
-const MIN_STEP: f32 = 0.03;
-/// Data-term weight of the geodesic distance (nats per `EDGE`).
-const KAPPA: f32 = 2.0;
-const GAMMA: f32 = 50.0;
+/// The model's constants (see the module docs); [`Tuning::default`] is the fit to Photoshop.
+#[derive(Clone, Copy, Debug)]
+pub struct Tuning {
+ /// Cost of each selected pixel beyond the footprint, times `1 + dist * d^pow` with `d` its
+ /// distance from the footprint (working pixels).
+ pub area: f32,
+ pub dist: f32,
+ pub pow: f32,
+ /// Cut weight between neighbours: `floor + 1 / (1 + (|Ip - Iq| / sigma)^k)` (RGB in 0..1).
+ pub floor: f32,
+ pub sigma: f32,
+ pub k: f32,
+ /// The footprint grows by `grow + grow_frac * radius` working pixels (Photoshop's settled click).
+ pub grow: f32,
+ pub grow_frac: f32,
+ /// Larger working windows are cut coarse to fine: at half size first, then again only in a
+ /// band around that boundary (Lombaert, Sun, Grady & Xu, "A Multilevel Banded Graph Cuts
+ /// Method for Fast Image Segmentation", ICCV 2005).
+ pub direct_px: usize,
+}
-/// Default working-resolution budget (pixels) for a stroke.
-pub const WORK_PX: usize = 160_000;
+impl Default for Tuning {
+ fn default() -> Self {
+ Self {
+ area: 0.004079,
+ dist: 0.002651,
+ pow: 0.98885,
+ floor: 0.018847,
+ sigma: 0.048450,
+ k: 1.98549,
+ grow: 1.69371,
+ grow_frac: 0.103003,
+ direct_px: 300_000,
+ }
+ }
+}
-/// Grows a selection from a brush stroke (`points` in document pixels, brush diameter `size`).
-/// Returns the new region (to be added to or subtracted from the current selection), or `None`
-/// when the stroke misses the canvas.
-pub fn quick_select(sampler: &dyn Sampler, canvas: Rect, points: &[(f32, f32)], size: f32, work_px: usize) -> Option<Region> {
- if points.is_empty() {
+/// The working step for a document of `w` x `h` pixels: Photoshop works on the whole document
+/// up to 512 x 512 pixels and at half resolution above.
+pub fn working_step(w: usize, h: usize) -> usize {
+ if w.saturating_mul(h) <= 512 * 512 { 1 } else { 2 }
+}
+
+/// Grows a selection from a brush stroke (`points` in document pixels, brush diameter `size`)
+/// on the document `canvas`. Returns the new region (to be added to or subtracted from the
+/// current selection), or `None` when the stroke misses the canvas.
+pub fn quick_select(sampler: &dyn Sampler, canvas: Rect, points: &[(f32, f32)], size: f32) -> Option<Region> {
+ quick_select_with(sampler, canvas, points, size, &Tuning::default())
+}
+
+/// [`quick_select`] with explicit model constants.
+pub fn quick_select_with(sampler: &dyn Sampler, canvas: Rect, points: &[(f32, f32)], size: f32, t: &Tuning) -> Option<Region> {
+ if points.is_empty() || canvas.is_empty() {
return None;
}
- let size = size.max(1.0);
- let r = size / 2.0;
+ let r = size.max(1.0) / 2.0;
let (mut x0, mut y0, mut x1, mut y1) = (f32::MAX, f32::MAX, f32::MIN, f32::MIN);
for &(x, y) in points {
x0 = x0.min(x - r);
@@ -51,66 +95,114 @@ pub fn quick_select(sampler: &dyn Sampler, canvas: Rect, points: &[(f32, f32)],
x1 = x1.max(x + r);
y1 = y1.max(y + r);
}
- let bbox = Rect::new(x0.floor() as i32, y0.floor() as i32, (x1.ceil() as i32).saturating_add(1), (y1.ceil() as i32).saturating_add(1)).intersect(&canvas);
+ let clamp = |v: f32| v.clamp(i32::MIN as f32 / 2.0, i32::MAX as f32 / 2.0) as i32;
+ let bbox = Rect::new(clamp(x0.floor()), clamp(y0.floor()), clamp(x1.ceil()).saturating_add(1), clamp(y1.ceil()).saturating_add(1)).intersect(&canvas);
if bbox.is_empty() {
return None;
}
- let margin = (size * 6.0).max(512.0) as i32;
- let window = bbox.inflate(margin).intersect(&canvas);
- let step = super::scale_for(window, work_px);
- let img = sampler.rgb_scaled(window, step);
- let (lw, lh) = (img.w, img.h);
- let st = step as f32;
- let pts: Vec<(f32, f32)> = points.iter().map(|&(x, y)| ((x - window.x0 as f32) / st, (y - window.y0 as f32) / st)).collect();
- let seeds = stroke_mask(&pts, (r / st).max(0.75), lw, lh);
- if !seeds.iter().any(|s| *s) {
- return None;
- }
- let (fg, bg, low) = segment(&img, &seeds);
- if !low.iter().any(|v| *v) {
- return None;
+ let step = working_step(canvas.width() as usize, canvas.height() as usize) as i32;
+ let rw = r / step as f32;
+ // Work in a window around the stroke; grow it while the selection runs into its border.
+ let mut margin = (48.0f32).max(2.0 * rw) as i32 * step;
+ loop {
+ let snap0 = |v: i32, o: i32| o + (v - o).div_euclid(step) * step;
+ let grown = bbox.inflate(margin).intersect(&canvas);
+ let window = Rect::new(snap0(grown.x0, canvas.x0), snap0(grown.y0, canvas.y0), grown.x1, grown.y1);
+ let img = sampler.rgb_scaled(window, step as usize);
+ let (lw, lh) = (img.w, img.h);
+ let s = step as f32;
+ let pts: Vec<(f32, f32)> = points.iter().map(|&(x, y)| ((x - window.x0 as f32) / s, (y - window.y0 as f32) / s)).collect();
+ let seeds = stroke_mask(&pts, rw.max(0.5), lw, lh);
+ if !seeds.iter().any(|v| *v) {
+ return None;
+ }
+ // The window's sides are free like the canvas' edges, so a selection that stays clear of
+ // them is the best over the whole canvas too (outside the window it could only add cost).
+ // One that reaches a side inside the canvas is recomputed in a larger window.
+ let inner = [window.x0 > canvas.x0, window.y0 > canvas.y0, window.x1 < canvas.x1, window.y1 < canvas.y1];
+ let low = select_working(&img, &seeds, rw, t);
+ let touches = |side: usize| match side {
+ 0 => (0..lh).any(|y| low[y * lw]),
+ 1 => (0..lw).any(|x| low[x]),
+ 2 => (0..lh).any(|y| low[y * lw + lw - 1]),
+ _ => (0..lw).any(|x| low[(lh - 1) * lw + x]),
+ };
+ if (0..4).any(|k| inner[k] && touches(k)) {
+ margin = margin.saturating_mul(2);
+ continue;
+ }
+ let mask = upsample(&low, lw, lh, step as usize, window.width() as usize, window.height() as usize);
+ return super::trim_region(Region { bbox: window, mask });
}
- super::finish_region(sampler, window, step, &low, lw, lh, Some((&fg, &bg)))
}
-/// The working-resolution segmentation: (foreground model, background model, labels).
-pub fn segment(img: &RgbImage, seeds: &[bool]) -> (Gmm, Gmm, Vec<bool>) {
- let (lw, lh) = (img.w, img.h);
- let geo = geodesic(img, seeds);
- let fs: Vec<[f32; 3]> = img.px.iter().zip(seeds).filter(|(_, s)| **s).map(|(p, _)| *p).collect();
- let bs: Vec<[f32; 3]> = img.px.iter().zip(&geo).filter(|(_, d)| **d > EDGE).map(|(p, _)| *p).collect();
- let fs = subsample(&fs, 20_000);
- let bs = subsample(&bs, 20_000);
- let fg = Gmm::fit(&fs, (fs.len() / 40).clamp(1, 4), QREG).unwrap_or_else(broad_model);
- let bg = if bs.len() >= 30 { Gmm::fit(&bs, 6, QREG) } else { None };
- let eval = |(p, d): (&[f32; 3], &f32)| -> (f32, f32) {
- let cf = fg.neg_log(*p) + KAPPA * (d / EDGE).min(4.0);
- let cb = match &bg {
- Some(b) => -((0.85 * b.log_prob(*p).exp() + 0.15).ln() as f32),
- None => 0.0,
- };
- (cf, cb)
- };
- #[cfg(not(target_arch = "wasm32"))]
- let costs: Vec<(f32, f32)> = {
- use rayon::prelude::*;
- img.px.par_iter().zip(geo.par_iter()).map(eval).collect()
- };
- #[cfg(target_arch = "wasm32")]
- let costs: Vec<(f32, f32)> = img.px.iter().zip(geo.iter()).map(eval).collect();
- let (cf, cb): (Vec<f32>, Vec<f32>) = costs.into_iter().unzip();
- let fixed: Vec<u8> = seeds.iter().map(|s| if *s { HARD_FG } else { FREE }).collect();
- let cut = grid_cut(img, &cf, &cb, &fixed, GAMMA, contrast_beta(img));
- let low = keep_seeded(&cut, seeds, lw, lh);
- (fg, bg.unwrap_or_else(broad_model), low)
+/// The model at working resolution: `seeds` (the brush footprint, `radius` working pixels) are
+/// selected; returns the selection.
+pub fn select_working(img: &RgbImage, seeds: &[bool], radius: f32, t: &Tuning) -> Vec<bool> {
+ let (w, h) = (img.w, img.h);
+ let d = crate::selection::edt(seeds, w, h);
+ let grow = (t.grow + t.grow_frac * radius).max(0.0);
+ let cost_fg: Vec<f32> = d.iter().map(|d| t.area * (1.0 + t.dist * (d - grow).max(0.0).powf(t.pow))).collect();
+ let fixed: Vec<u8> = d.iter().map(|d| if *d <= grow { HARD_FG } else { FREE }).collect();
+ let (floor, sigma, k) = (t.floor, t.sigma.max(1e-4), t.k);
+ let weight = move |d2: f32| floor + 1.0 / (1.0 + (d2.sqrt() / sigma).powf(k));
+ cut_levels(img, &fixed, &cost_fg, &weight, 1.0, t.direct_px.max(1024))
}
-/// A near-uniform density over the RGB cube.
-pub fn broad_model() -> Gmm {
- let corners: Vec<[f32; 3]> = (0..8).map(|i| [(i & 1) as f32, ((i >> 1) & 1) as f32, ((i >> 2) & 1) as f32]).collect();
- // Eight fixed, distinct samples: the fit cannot fail.
- #[allow(clippy::expect_used)]
- Gmm::fit(&corners, 1, 0.0).expect("non-degenerate")
+/// Band half-width (pixels) re-cut at each finer level.
+const BAND: f32 = 3.0;
+
+/// Min cut with only foreground costs, coarse to fine above `direct_px` pixels: the half-size
+/// problem (colours averaged, pixel costs summed, cut weights doubled since a coarse side spans two
+/// fine ones) gives a boundary, and the full-size cut is redone only within `BAND` of it. Seeds
+/// stay foreground.
+fn cut_levels(img: &RgbImage, fixed: &[u8], cost_fg: &[f32], weight: &dyn Fn(f32) -> f32, scale: f32, direct_px: usize) -> Vec<bool> {
+ let (w, h) = (img.w, img.h);
+ let zeros = vec![0.0f32; w * h];
+ if w * h <= direct_px || w < 8 || h < 8 {
+ return super::grid_cut_with(img, cost_fg, &zeros, fixed, |d2| scale * weight(d2));
+ }
+ let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));
+ let coarse_img = img.downsample(2);
+ let mut cfix = vec![FREE; cw * ch];
+ let mut ccost = vec![0.0f32; cw * ch];
+ for y in 0..h {
+ for x in 0..w {
+ let c = (y / 2) * cw + x / 2;
+ ccost[c] += cost_fg[y * w + x];
+ if fixed[y * w + x] == HARD_FG {
+ cfix[c] = HARD_FG;
+ }
+ }
+ }
+ let coarse = cut_levels(&coarse_img, &cfix, &ccost, weight, scale * 2.0, direct_px);
+ let proj: Vec<bool> = (0..w * h).map(|i| coarse[(i / w / 2) * cw + (i % w) / 2]).collect();
+ // Band: within BAND of the projected boundary.
+ let edge: Vec<bool> = (0..w * h)
+ .map(|i| {
+ let (x, y) = (i % w, i / w);
+ let v = proj[i];
+ (x > 0 && proj[i - 1] != v) || (x + 1 < w && proj[i + 1] != v) || (y > 0 && proj[i - w] != v) || (y + 1 < h && proj[i + w] != v)
+ })
+ .collect();
+ if !edge.iter().any(|e| *e) {
+ return (0..w * h).map(|i| proj[i] || fixed[i] == HARD_FG).collect();
+ }
+ let near = crate::selection::edt(&edge, w, h);
+ let banded: Vec<u8> = (0..w * h)
+ .map(|i| {
+ if fixed[i] == HARD_FG {
+ HARD_FG
+ } else if near[i] <= BAND {
+ FREE
+ } else if proj[i] {
+ HARD_FG
+ } else {
+ super::HARD_BG
+ }
+ })
+ .collect();
+ super::grid_cut_with(img, cost_fg, &zeros, &banded, |d2| scale * weight(d2))
}
/// Pixels within `radius` (working pixels) of the polyline `pts`, plus each point's own pixel.
@@ -143,105 +235,24 @@ pub fn stroke_mask(pts: &[(f32, f32)], radius: f32, w: usize, h: usize) -> Vec<b
m
}
-fn blur3(img: &RgbImage) -> RgbImage {
- let (w, h) = (img.w, img.h);
- let mut tmp = img.clone();
- for y in 0..h {
- for x in 0..w {
- let (l, r) = (img.at(x.saturating_sub(1), y), img.at((x + 1).min(w - 1), y));
- let c = img.at(x, y);
- tmp.px[y * w + x] = [0, 1, 2].map(|k| (l[k] + c[k] + r[k]) / 3.0);
- }
+/// Bilinear upsampling (pixel centres aligned, edges clamped) of a working-resolution selection
+/// to 8-bit coverage, as Photoshop scales its half-resolution result.
+pub fn upsample(low: &[bool], lw: usize, lh: usize, step: usize, w: usize, h: usize) -> Vec<u8> {
+ if step == 1 {
+ return low.iter().map(|b| if *b { 255 } else { 0 }).collect();
}
- let mut out = tmp.clone();
+ let at = |x: i64, y: i64| -> f32 { if low[(y.clamp(0, lh as i64 - 1) as usize) * lw + x.clamp(0, lw as i64 - 1) as usize] { 1.0 } else { 0.0 } };
+ let s = step as f32;
+ let mut out = vec![0u8; w * h];
for y in 0..h {
+ let fy = (y as f32 + 0.5) / s - 0.5;
+ let (iy, ty) = (fy.floor() as i64, fy - fy.floor());
for x in 0..w {
- let (u, d) = (tmp.at(x, y.saturating_sub(1)), tmp.at(x, (y + 1).min(h - 1)));
- let c = tmp.at(x, y);
- out.px[y * w + x] = [0, 1, 2].map(|k| (u[k] + c[k] + d[k]) / 3.0);
+ let fx = (x as f32 + 0.5) / s - 0.5;
+ let (ix, tx) = (fx.floor() as i64, fx - fx.floor());
+ let v = (at(ix, iy) * (1.0 - tx) + at(ix + 1, iy) * tx) * (1.0 - ty) + (at(ix, iy + 1) * (1.0 - tx) + at(ix + 1, iy + 1) * tx) * ty;
+ out[y * w + x] = (v * 255.0).round() as u8;
}
}
out
}
-
-/// Geodesic distance from `seeds` over a barrier map: each pixel costs its colour gradient
-/// magnitude (smoothed image) above the texture level of the stroke (twice the median gradient
-/// under it), plus a tiny spatial term. Raster-scan approximation (two forward/backward sweeps,
-/// 8-neighbours).
-pub fn geodesic(img: &RgbImage, seeds: &[bool]) -> Vec<f32> {
- let (w, h) = (img.w, img.h);
- let sm = blur3(&blur3(img));
- // Texture level of what the user painted: median local gradient over the stroke (grown by
- // two pixels). Gradual colour changes and texture at that level cost nothing.
- let mut near = vec![false; w * h];
- for (i, _) in seeds.iter().enumerate().filter(|(_, s)| **s) {
- let (x, y) = (i % w, i / w);
- for yy in y.saturating_sub(2)..(y + 3).min(h) {
- near[yy * w + x.saturating_sub(2)..yy * w + (x + 3).min(w)].fill(true);
- }
- }
- // Colour gradient magnitude (central differences) of the smoothed image.
- let mut g = vec![0.0f32; w * h];
- for y in 0..h {
- for x in 0..w {
- let (l, r) = (sm.at(x.saturating_sub(1), y), sm.at((x + 1).min(w - 1), y));
- let (u, dn) = (sm.at(x, y.saturating_sub(1)), sm.at(x, (y + 1).min(h - 1)));
- g[y * w + x] = ((super::d2(l, r) + super::d2(u, dn)) * 0.25).sqrt();
- }
- }
- let mut diffs: Vec<f32> = (0..w * h).filter(|i| near[*i]).map(|i| g[i]).collect();
- let tau = if diffs.is_empty() {
- 0.0
- } else {
- let mid = diffs.len() / 2;
- *diffs.select_nth_unstable_by(mid, |a, b| a.total_cmp(b)).1
- };
- let thr = (2.0 * tau).max(MIN_STEP);
- // Crossing pixels whose gradient exceeds the texture level costs the excess: a barrier map,
- // so an edge costs about its contrast whatever direction the path crosses it.
- let e: Vec<f32> = g.iter().map(|v| (v - thr).max(0.0)).collect();
- let cost = |a: usize, b: usize, len: f32| len * (0.5 * (e[a] + e[b]) + 1e-6);
- let mut d: Vec<f32> = seeds.iter().map(|s| if *s { 0.0 } else { f32::INFINITY }).collect();
- let sq = std::f32::consts::SQRT_2;
- for _ in 0..2 {
- for y in 0..h {
- for x in 0..w {
- let i = y * w + x;
- let mut v = d[i];
- if x > 0 {
- v = v.min(d[i - 1] + cost(i, i - 1, 1.0));
- }
- if y > 0 {
- v = v.min(d[i - w] + cost(i, i - w, 1.0));
- if x > 0 {
- v = v.min(d[i - w - 1] + cost(i, i - w - 1, sq));
- }
- if x + 1 < w {
- v = v.min(d[i - w + 1] + cost(i, i - w + 1, sq));
- }
- }
- d[i] = v;
- }
- }
- for y in (0..h).rev() {
- for x in (0..w).rev() {
- let i = y * w + x;
- let mut v = d[i];
- if x + 1 < w {
- v = v.min(d[i + 1] + cost(i, i + 1, 1.0));
- }
- if y + 1 < h {
- v = v.min(d[i + w] + cost(i, i + w, 1.0));
- if x + 1 < w {
- v = v.min(d[i + w + 1] + cost(i, i + w + 1, sq));
- }
- if x > 0 {
- v = v.min(d[i + w - 1] + cost(i, i + w - 1, sq));
- }
- }
- d[i] = v;
- }
- }
- }
- d
-}
diff --git a/crates/algo/src/segment/tests.rs b/crates/algo/src/segment/tests.rs
index 9d5c524f5..68ce38348 100644
--- a/crates/algo/src/segment/tests.rs
+++ b/crates/algo/src/segment/tests.rs
@@ -311,7 +311,7 @@ fn quick_select_with_points_far_off_canvas_selects_nothing() {
let s = ImageSampler { img: &img, origin: (0, 0) };
let canvas = Rect::new(0, 0, 140, 100);
for p in [(3e9, 0.0), (0.0, 3e9), (f32::MAX, f32::MAX)] {
- assert!(quick::quick_select(&s, canvas, &[p], 8.0, quick::WORK_PX).is_none(), "{p:?}");
+ assert!(quick::quick_select(&s, canvas, &[p], 8.0).is_none(), "{p:?}");
}
}
@@ -320,7 +320,7 @@ fn quick_select_grows_without_leaking() {
let img = two_regions(140, 100);
let s = ImageSampler { img: &img, origin: (0, 0) };
let canvas = Rect::new(0, 0, 140, 100);
- let reg = quick::quick_select(&s, canvas, &[(20.0, 50.0), (35.0, 50.0)], 8.0, quick::WORK_PX).unwrap();
+ let reg = quick::quick_select(&s, canvas, &[(20.0, 50.0), (35.0, 50.0)], 8.0).unwrap();
let mut inside = 0;
for y in 0..100 {
for x in 0..140 {
@@ -346,7 +346,7 @@ fn quick_select_stops_at_thin_line() {
if x == 60 || x == 61 { [0.05, 0.05, 0.05] } else { [0.6 + n, 0.6 + n, 0.55 + n] }
});
let s = ImageSampler { img: &img, origin: (0, 0) };
- let reg = quick::quick_select(&s, Rect::new(0, 0, 120, 80), &[(30.0, 40.0)], 6.0, quick::WORK_PX).unwrap();
+ let reg = quick::quick_select(&s, Rect::new(0, 0, 120, 80), &[(30.0, 40.0)], 6.0).unwrap();
assert!(reg.at(10, 10) >= 0.5 && reg.at(50, 70) >= 0.5);
for y in 0..80 {
for x in 63..120 {
@@ -356,13 +356,134 @@ fn quick_select_stops_at_thin_line() {
}
#[test]
-fn quick_select_downsampled_window() {
- // A larger image with a small working budget: step > 1 plus band refinement.
+fn quick_select_a_drag_selects_around_itself_not_a_big_object() {
+ // Photoshop 25.4 on this very scene: an 80 px drag inside a 180 px radius disc selects about
+ // the stroke (5285 px), not the disc (~100,000 px): the disc is far too big for the stroke.
let (img, truth) = disc_scene(600, 500, 300.0, 250.0, 180.0, 4);
let s = ImageSampler { img: &img, origin: (0, 0) };
- let reg = quick::quick_select(&s, Rect::new(0, 0, 600, 500), &[(260.0, 250.0), (340.0, 250.0)], 30.0, 15_000).unwrap();
- let v = iou(&reg, &truth, 600, 500);
- assert!(v > 0.93, "IoU {v}");
+ let reg = quick::quick_select(&s, Rect::new(0, 0, 600, 500), &[(260.0, 250.0), (340.0, 250.0)], 30.0).unwrap();
+ let n = count(&reg);
+ assert!((3000..9000).contains(&n), "{n} px");
+ assert!(reg.at(300, 250) >= 0.5 && reg.at(262, 250) >= 0.5);
+ assert!(iou(&reg, &truth, 600, 500) < 0.2);
+}
+
+// ---------- Quick selection: Photoshop's measured behaviour ----------
+//
+// Each case below was run in Photoshop 25.4 (the same image and stroke, the mouse held 1 s so
+// its solver settles); the numbers in the comments are Photoshop's.
+
+const QS_RED: [f32; 3] = [200.0 / 255.0, 60.0 / 255.0, 60.0 / 255.0];
+const QS_BLUE: [f32; 3] = [60.0 / 255.0, 60.0 / 255.0, 200.0 / 255.0];
+
+/// A `w` x `h` image of `bg` with `fg` over `[x0, x1) x [y0, y1)`.
+fn qs_scene(w: usize, h: usize, bg: [f32; 3], fg: [f32; 3], rect: (usize, usize, usize, usize)) -> RgbImage {
+ let (x0, y0, x1, y1) = rect;
+ RgbImage::from_fn(w, h, |x, y| if x >= x0 && x < x1 && y >= y0 && y < y1 { fg } else { bg })
+}
+
+/// A centred `side` square of `fg` on `bg` in a `w` x `w` image.
+fn qs_square(w: usize, side: usize, fg: [f32; 3], bg: [f32; 3]) -> RgbImage {
+ let x0 = (w - side) / 2;
+ qs_scene(w, w, bg, fg, (x0, x0, x0 + side, x0 + side))
+}
+
+fn qs(img: &RgbImage, pts: &[(f32, f32)], size: f32) -> Region {
+ let s = ImageSampler { img, origin: (0, 0) };
+ quick::quick_select(&s, Rect::new(0, 0, img.w as i32, img.h as i32), pts, size).expect("a selection")
+}
+
+fn count(r: &Region) -> usize {
+ r.mask.iter().filter(|v| **v >= 128).count()
+}
+
+/// Selected pixels inside and outside `[x0, x1) x [y0, y1)`.
+fn inside_outside(r: &Region, rect: (i32, i32, i32, i32)) -> (usize, usize) {
+ let (mut i, mut o) = (0, 0);
+ for y in r.bbox.y0..r.bbox.y1 {
+ for x in r.bbox.x0..r.bbox.x1 {
+ if r.at(x, y) >= 0.5 {
+ if x >= rect.0 && x < rect.2 && y >= rect.1 && y < rect.3 { i += 1 } else { o += 1 }
+ }
+ }
+ }
+ (i, o)
+}
+
+#[test]
+fn quick_select_works_at_full_or_exactly_half_resolution() {
+ // Up to 512 x 512 pixels Photoshop works on the whole document, above at half resolution,
+ // whatever the document's shape or the brush.
+ assert_eq!(quick::working_step(512, 512), 1);
+ assert_eq!(quick::working_step(513, 512), 2);
+ assert_eq!(quick::working_step(520, 300), 1);
+ assert_eq!(quick::working_step(900, 300), 2);
+ assert_eq!(quick::working_step(5000, 3500), 2);
+ // Half resolution: the mask is scaled up bilinearly, so a selected square's edge steps
+ // 64, 191, 255 (Photoshop's own values on a 640 px document).
+ let img = qs_square(640, 200, QS_RED, QS_BLUE);
+ let r = qs(&img, &[(320.0, 320.0)], 30.0);
+ let row: Vec<u8> = (216..223).map(|x| (r.at(x, 320) * 255.0).round() as u8).collect();
+ assert_eq!(row, [0, 0, 0, 64, 191, 255, 255]);
+}
+
+#[test]
+fn quick_select_click_in_a_flat_area_selects_about_the_brush() {
+ // Photoshop: 1025-1120 px for a 30 px brush (the disc grows a little while the button is held).
+ let img = qs_scene(400, 400, QS_RED, QS_RED, (0, 0, 0, 0));
+ let n = count(&qs(&img, &[(200.0, 200.0)], 30.0));
+ assert!((700..1600).contains(&n), "{n} px");
+}
+
+#[test]
+fn quick_select_click_takes_an_object_only_while_it_is_small_enough() {
+ // 30 px brush, red on blue: Photoshop selects a 220 px square whole, not a 240 px one.
+ let img = qs_square(500, 220, QS_RED, QS_BLUE);
+ let (i, o) = inside_outside(&qs(&img, &[(250.0, 250.0)], 30.0), (140, 140, 360, 360));
+ assert!(i >= 220 * 220 - 10 && o == 0, "220: {i} in, {o} out");
+ let img = qs_square(500, 240, QS_RED, QS_BLUE);
+ let n = count(&qs(&img, &[(250.0, 250.0)], 30.0));
+ assert!(n < 2000, "240: {n} px");
+ // A weak edge (15 levels) holds only a small square: 94 px yes, 120 px no.
+ let (lo, hi) = ([125.0 / 255.0, 110.0 / 255.0, 110.0 / 255.0], [110.0 / 255.0, 110.0 / 255.0, 125.0 / 255.0]);
+ let img = qs_square(500, 94, lo, hi);
+ let (i, o) = inside_outside(&qs(&img, &[(250.0, 250.0)], 30.0), (203, 203, 297, 297));
+ assert!(i >= 94 * 94 - 10 && o == 0, "94: {i} in, {o} out");
+ let img = qs_square(500, 120, lo, hi);
+ let n = count(&qs(&img, &[(250.0, 250.0)], 30.0));
+ assert!(n < 2000, "120: {n} px");
+}
+
+#[test]
+fn quick_select_takes_a_narrow_bar_end_to_end_but_only_the_end_near_it() {
+ // An 80 x 400 bar: a click in the middle selects all of it; a click 30 px from its end only
+ // the end (Photoshop: 3762 px, cut just above the brush).
+ let img = qs_scene(500, 500, QS_BLUE, QS_RED, (100, 50, 180, 450));
+ let (i, o) = inside_outside(&qs(&img, &[(140.0, 250.0)], 30.0), (100, 50, 180, 450));
+ assert!(i >= 80 * 400 - 10 && o == 0, "middle: {i} in, {o} out");
+ let r = qs(&img, &[(140.0, 420.0)], 30.0);
+ let (i, o) = inside_outside(&r, (100, 380, 180, 450));
+ assert!(i > 2500 && o == 0, "near the end: {i} in the end, {o} elsewhere");
+}
+
+#[test]
+fn quick_select_a_short_drag_takes_what_a_click_does_not() {
+ // A 300 px square: a click selects about the brush; a 60 px drag the whole square.
+ let img = qs_square(500, 300, QS_RED, QS_BLUE);
+ assert!(count(&qs(&img, &[(250.0, 250.0)], 30.0)) < 2000);
+ let (i, o) = inside_outside(&qs(&img, &[(220.0, 250.0), (280.0, 250.0)], 30.0), (100, 100, 400, 400));
+ assert!(i >= 300 * 300 - 10 && o == 0, "{i} in, {o} out");
+}
+
+#[test]
+fn quick_select_on_a_large_document_grows_its_window_to_the_object() {
+ // A 1000 x 600 document (half resolution) with an 80 x 960 bar: Photoshop selects all of it
+ // from a click in the middle (76800 px). The working window starts around the stroke and grows
+ // until the selection no longer runs into it.
+ let img = qs_scene(1000, 600, QS_BLUE, QS_RED, (20, 260, 980, 340));
+ // The mouse on pixel (500, 300), as in Photoshop: its centre.
+ let (i, o) = inside_outside(&qs(&img, &[(500.5, 300.5)], 30.0), (20, 260, 980, 340));
+ assert!(i >= 960 * 80 * 98 / 100 && o < 2000, "{i} in, {o} out");
}
// ---------- Select subject ----------
@@ -452,25 +573,21 @@ fn clean_mask_drops_islands_and_fills_holes() {
assert!(!c[36 * w + 36]);
}
+/// Writes the quick-selection test scenes as raw RGB8 (for measuring them in Photoshop).
#[test]
-fn geodesic_distance_jumps_across_edges() {
- let img = two_regions(140, 100);
- let seeds = quick::stroke_mask(&[(20.0, 50.0)], 4.0, 140, 100);
- let d = quick::geodesic(&img, &seeds);
- assert!(d[50 * 140 + 40] < 0.02, "same region {}", d[50 * 140 + 40]);
- assert!(d[50 * 140 + 100] > 0.1, "across the edge {}", d[50 * 140 + 100]);
-}
-
-#[test]
-fn geodesic_barrier_blocks_oblique_crossings() {
- // A large disc whose slightly tilted edge crosses the image: paths running along the soft
- // edge must not sneak across it.
- let (img, truth) = disc_scene(352, 372, -470.0, 186.0, 668.0, 4);
- let seeds = quick::stroke_mask(&[(175.0, 176.0), (175.0, 196.0)], 5.0, 352, 372);
- assert!(seeds.iter().zip(&truth).all(|(s, t)| !*s || *t));
- let d = quick::geodesic(&img, &seeds);
- assert!(d[186 * 352 + 100] < 0.02);
- for y in (0..372).step_by(31) {
- assert!(d[y * 352 + 291] > 0.1, "row {y}: {}", d[y * 352 + 291]);
- }
+#[ignore]
+fn dump_quick_scenes() {
+ let Ok(dir) = std::env::var("QS_DUMP") else { return };
+ let save = |name: &str, img: &RgbImage| {
+ let bytes: Vec<u8> = img.px.iter().flat_map(|p| p.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8)).collect();
+ std::fs::write(format!("{dir}/{name}_{}x{}.rgb", img.w, img.h), bytes).unwrap();
+ };
+ save("two_regions", &two_regions(140, 100));
+ let mut rng = Rng::new(9);
+ let line = RgbImage::from_fn(120, 80, |x, _| {
+ let n = 0.03 * rng.normal();
+ if x == 60 || x == 61 { [0.05, 0.05, 0.05] } else { [0.6 + n, 0.6 + n, 0.55 + n] }
+ });
+ save("thin_line", &line);
+ save("disc_scene", &disc_scene(600, 500, 300.0, 250.0, 180.0, 4).0);
}
diff --git a/crates/engine/src/smartselect_cmds.rs b/crates/engine/src/smartselect_cmds.rs
index f2ff1995c..adf281a3c 100644
--- a/crates/engine/src/smartselect_cmds.rs
+++ b/crates/engine/src/smartselect_cmds.rs
@@ -99,7 +99,7 @@ fn quick_selection(s: &mut Session, p: &Value) -> Result<Value> {
let enhance = b(p, "enhanceEdge", false);
let region = with_sampler(s, b(p, "sampleAllLayers", false), |smp, doc| {
let canvas = doc.bounds();
- let r = quick::quick_select(smp, canvas, &pts, size, quick::WORK_PX)?;
+ let r = quick::quick_select(smp, canvas, &pts, size)?;
if enhance { matting::refine_mask(smp, &matting::region_reader(&r), r.bbox, canvas, &ENHANCE) } else { Some(r) }
})?;
apply(s, "Quick Selection", region, m)