Content-Aware Move: exact 8-bit rounding and a bounded no-pixels scan (#12192)

Review follow-up. A pixel counts as selected exactly when the selection's
coverage8 stores it nonzero: the least such coverage is found once by
bisecting the f32 bits, so the scan stays one compare per pixel.

The scan no longer costs more than the select.lasso it replaces: strips
start at 64 rows and double up to about 4 MP, and each strip is checked
in parallel, branch-free blocks (sequential on wasm). A corner-to-corner
line on 7360 x 4912 now takes 25-32 ms (was 65-73 ms; select.lasso took
23-27 ms). A lasso that selects is still answered after the first strip.

Adds a test for a lasso over pixel centres with Anti-alias off.
This commit is contained in:
RickiNano committed 2026-11-08 18:59:50 +01:00
1 parent e9d9c2a18c
commit bf330293cb
2 files changed
+47 -7

No files matched your search

@@ -140,6 +140,17 @@ fn a_tiny_lasso_that_covers_part_of_a_pixel_still_selects() {
assert_eq!(steps(&app), before + 1);
}
#[test]
fn without_anti_alias_a_lasso_over_pixel_centres_selects() {
let mut app = app();
app.ui.tool_options.anti_alias = false;
let before = steps(&app);
drag(&mut app, &[[10.0, 20.0], [13.0, 20.0], [13.0, 23.0], [10.0, 23.0]]);
assert!(alerts(&app).is_empty());
assert!(selected(&app));
assert_eq!(steps(&app), before + 1);
}
#[test]
fn a_thin_lasso_between_pixel_centres_warns_without_anti_alias() {
// 21 px² of area, but between the centres of row 20 (20.5) and the row above: no pixel.
+36 -7
View File
@@ -442,11 +442,25 @@ pub fn content_aware_move_no_pixels(app: &mut PhotocraftApp, points: &[[f64; 2]]
}
/// Whether `select.lasso` through `points` would select any pixel of `canvas`: the same
/// rasterization (its coverage over the canvas's coordinates), kept where it rounds to at least
/// 1/255 as the selection stores it. Only the lasso's bounds are rasterized, a strip of rows at a
/// time, stopping at the first selected pixel.
/// rasterization (its coverage over the canvas's coordinates), kept where it rounds to a nonzero
/// 8-bit value as the selection stores it. Only the lasso's bounds are rasterized, a strip of rows
/// at a time, stopping at the first selected pixel. The strips start at 64 rows, so a lasso that
/// selects is answered after the first, and double up to about 4 MP (16 MB), so a lasso that
/// selects nothing (a long line) is rasterized and scanned in parallel: a corner-to-corner line
/// on 7360 x 4912 takes 25-32 ms, about what the `select.lasso` it replaces took (23-27 ms).
fn lasso_selects_pixels(points: &[[f64; 2]], canvas: photocraft_geom::Rect, anti_alias: bool) -> bool {
const STRIP: i32 = 64;
const FIRST_STRIP: u32 = 64;
const MAX_STRIP_PIXELS: u32 = 1 << 22;
// `coverage8` of the selection combine: the 8-bit value a pixel's coverage is stored as. It
// only grows with the coverage, so a pixel is kept from the least coverage it keeps (found
// once over the f32 bits in [0, 1]), and the scan is a single compare per pixel.
let stored = |v: f32| (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8;
let (mut below, mut kept) = (0u32, 1.0f32.to_bits());
while kept - below > 1 {
let mid = below + (kept - below) / 2;
if stored(f32::from_bits(mid)) > 0 { kept = mid } else { below = mid }
}
let least = f32::from_bits(kept);
// As `select.lasso` reads the points.
let pts: Vec<(f32, f32)> = points.iter().map(|p| (p[0] as f32, p[1] as f32)).collect();
if pts.len() < 3 {
@@ -464,17 +478,32 @@ fn lasso_selects_pixels(points: &[[f64; 2]], canvas: photocraft_geom::Rect, anti
let bounds =
photocraft_geom::Rect::new(lo.0.floor() as i32, lo.1.floor() as i32, (hi.0.ceil() as i32).saturating_add(1), (hi.1.ceil() as i32).saturating_add(1));
let window = bounds.intersect(&canvas);
let mut y = window.y0;
let max_rows = (MAX_STRIP_PIXELS / window.width().max(1)).max(FIRST_STRIP);
let (mut y, mut rows) = (window.y0, FIRST_STRIP);
while y < window.y1 {
let strip = photocraft_geom::Rect::new(window.x0, y, window.x1, y.saturating_add(STRIP).min(window.y1));
if photocraft_algo::selection::polygon_window(&pts, canvas, strip, anti_alias).iter().any(|v| *v * 255.0 >= 0.5) {
let strip = photocraft_geom::Rect::new(window.x0, y, window.x1, y.saturating_add_unsigned(rows).min(window.y1));
if any_at_least(&photocraft_algo::selection::polygon_window(&pts, canvas, strip, anti_alias), least) {
return true;
}
y = strip.y1;
rows = rows.saturating_mul(2).min(max_rows);
}
false
}
/// Whether any of `mask` is at least `least`: in parallel blocks, each one a branch-free pass the
/// compiler vectorizes.
fn any_at_least(mask: &[f32], least: f32) -> bool {
let block = |b: &[f32]| b.iter().fold(false, |found, v| found | (*v >= least));
#[cfg(not(target_arch = "wasm32"))]
{
use rayon::prelude::*;
mask.par_chunks(1 << 16).any(block)
}
#[cfg(target_arch = "wasm32")]
mask.chunks(1 << 16).any(block)
}
/// `paint.contentAwareMove`'s params for a drag by `offset`, from the options bar.
pub fn content_aware_move_params(app: &PhotocraftApp, offset: [i64; 2]) -> Value {
let o = &app.ui.tool_options;