mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Integration: Remove trail shows thin strokes on big documents (#8923)
The trail mask is at most 1024 px on its long side, so on a big document one mask pixel stands for k x k document pixels, and the Remove trail sampled only each block's centre. A thin stroke that fell between the centres (2 px on 6000x4000, k = 6; 4 px on 12000x8000, k = 12) showed no tint at all, though every pixel it touched is removed. In reach mode a mask pixel now takes the largest coverage of its whole block, clipped to the document. Blocks no coverage tile reaches are skipped (StrokeRenderer::touches), so releasing a whole stroke on a 30000x20000 document restamps in 0.22 s instead of 23 s. Also pins the waiting strokes' kept tint where a later stroke restamps its rectangle off its own path (fails with the base max removed). Co-authored-by: Hartmut Nörenberg <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
b0b100643b
commit
ac9b374fab
3 files changed
+124
-8
No files matched your search
@@ -1735,3 +1735,38 @@ fn resizing_with_bad_sizes_leaves_the_dual_tip_alone() {
|
||||
b.resize_with_dual_brush(50.0);
|
||||
assert_eq!(b.dual_brush.size, 20.0);
|
||||
}
|
||||
|
||||
/// `touches` is true wherever `coverage_at` can be above 0, false only where it is 0 for sure.
|
||||
#[test]
|
||||
fn touches_covers_every_pixel_with_coverage() {
|
||||
let b = BrushSettings { size: 6.0, ..brush() };
|
||||
let mut r = StrokeRenderer::new(&b, None, 1.0);
|
||||
r.push(&line(100.0, 300.0, 200.0));
|
||||
r.finish();
|
||||
for y in (0..400).step_by(3) {
|
||||
for x in (0..500).step_by(3) {
|
||||
if r.coverage_at(x, y) > 0.0 {
|
||||
assert!(r.touches(Rect::new(x, y, x + 1, y + 1)), "({x}, {y})");
|
||||
}
|
||||
}
|
||||
}
|
||||
// Across the origin and tile edges (64 px): every pixel, both sides of each edge.
|
||||
let mut o = StrokeRenderer::new(&b, None, 1.0);
|
||||
o.push(&[StrokePoint::new(-70.0, -70.0, 1.0), StrokePoint::new(70.0, 70.0, 1.0)]);
|
||||
o.finish();
|
||||
for y in -140..140 {
|
||||
for x in -140..140 {
|
||||
if o.coverage_at(x, y) > 0.0 {
|
||||
assert!(o.touches(Rect::new(x, y, x + 1, y + 1)), "({x}, {y})");
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(!o.touches(Rect::new(-128, 64, -64, 128)), "a tile the diagonal misses");
|
||||
assert!(!o.touches(Rect::new(-200, -200, -128, -128)), "ends exactly at a tile edge");
|
||||
assert!(o.touches(Rect::new(-200, -200, -127, -127)), "one pixel into the next tile");
|
||||
assert!(r.touches(Rect::new(150, 199, 151, 201)));
|
||||
assert!(!r.touches(Rect::new(0, 0, 50, 50)), "far from the stroke");
|
||||
assert!(!r.touches(Rect::new(400, 400, 400, 400)), "empty");
|
||||
assert!(r.touches(Rect::new(i32::MIN, i32::MIN, i32::MAX, i32::MAX)), "huge, without a huge scan");
|
||||
assert!(!r.touches(Rect::new(1_000_000, 1_000_000, i32::MAX, i32::MAX)));
|
||||
}
|
||||
@@ -927,6 +927,21 @@ impl StrokeRenderer {
|
||||
self.ctx.stroke_mask_wet(c, self.cov.depth(x, y), self.dual.as_ref().map(|d| d.get(x, y)), x, y)
|
||||
}
|
||||
|
||||
/// Whether any coverage tile overlaps `r`: outside them [`coverage_at`](Self::coverage_at) is
|
||||
/// 0, so a caller scanning a big area can skip it.
|
||||
pub fn touches(&self, r: Rect) -> bool {
|
||||
if r.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let (t0x, t0y) = (r.x0.div_euclid(COV_TILE), r.y0.div_euclid(COV_TILE));
|
||||
let (t1x, t1y) = ((r.x1 - 1).div_euclid(COV_TILE), (r.y1 - 1).div_euclid(COV_TILE));
|
||||
let span = (i64::from(t1x) - i64::from(t0x) + 1).saturating_mul(i64::from(t1y) - i64::from(t0y) + 1);
|
||||
if span > self.cov.tiles.len() as i64 {
|
||||
return self.cov.tiles.keys().any(|&(tx, ty)| (t0x..=t1x).contains(&tx) && (t0y..=t1y).contains(&ty));
|
||||
}
|
||||
(t0y..=t1y).any(|ty| (t0x..=t1x).any(|tx| self.cov.tiles.contains_key(&(tx, ty))))
|
||||
}
|
||||
|
||||
/// Dense final coverage over the stroke bounds.
|
||||
pub fn dense_coverage(&self) -> (Rect, Vec<f32>) {
|
||||
let b = self.bounds();
|
||||
|
||||
@@ -126,13 +126,19 @@ impl Trail {
|
||||
let tail = tail.map(|(_, t)| t);
|
||||
let renderer = &dabs.renderer;
|
||||
let full = |c: f32| if dabs.reach && c > 0.0 { 1.0 } else { c };
|
||||
self.restamp(changed, |x, y| full(renderer.coverage_at(x, y).max(tail.as_ref().map_or(0.0, |t| t.coverage_at(x, y)))));
|
||||
let reach = dabs.reach;
|
||||
let touches = |r| renderer.touches(r) || tail.as_ref().is_some_and(|t| t.touches(r));
|
||||
self.restamp(changed, reach, touches, |x, y| full(renderer.coverage_at(x, y).max(tail.as_ref().map_or(0.0, |t| t.coverage_at(x, y)))));
|
||||
self.dabs = Some(dabs);
|
||||
}
|
||||
|
||||
/// Recompute the mask pixels over the document rectangle `r` from `cov` (document pixel →
|
||||
/// coverage 0..=1), sampled at each mask pixel's centre.
|
||||
fn restamp(&mut self, r: photocraft_geom::Rect, cov: impl Fn(i32, i32) -> f32) {
|
||||
/// coverage 0..=1), sampled at each mask pixel's centre. With `block`, a mask pixel takes the
|
||||
/// largest coverage of all the document pixels it stands for instead: on a big document (one
|
||||
/// mask pixel per k x k) a thin Remove stroke can fall between the centres, and every pixel it
|
||||
/// touches is removed. `touches` says whether `cov` can be above 0 anywhere in a document
|
||||
/// rectangle, so blocks the stroke never reached aren't scanned pixel by pixel.
|
||||
fn restamp(&mut self, r: photocraft_geom::Rect, block: bool, touches: impl Fn(photocraft_geom::Rect) -> bool, cov: impl Fn(i32, i32) -> f32) {
|
||||
if r.is_empty() {
|
||||
return;
|
||||
}
|
||||
@@ -144,14 +150,39 @@ impl Trail {
|
||||
if x1 <= x0 || y1 <= y0 {
|
||||
return;
|
||||
}
|
||||
// Document pixels of mask pixel `i` along a side `n` long: its centre, or its whole span
|
||||
// clipped to the document (at least one pixel).
|
||||
let span = |i: usize, n: u32| -> (i32, i32) {
|
||||
if block {
|
||||
let lo = (i as f64 * k) as i64;
|
||||
let hi = (((i + 1) as f64 * k) as i64).min(i64::from(n)).max(lo + 1);
|
||||
(i32::try_from(lo).unwrap_or(i32::MAX), i32::try_from(hi).unwrap_or(i32::MAX))
|
||||
} else {
|
||||
let c = ((i as f64 + 0.5) * k).floor() as i32;
|
||||
(c, c.saturating_add(1))
|
||||
}
|
||||
};
|
||||
for y in y0..y1 {
|
||||
let dy = ((y as f64 + 0.5) * k).floor() as i32;
|
||||
let (dy0, dy1) = span(y, self.doc[1]);
|
||||
for x in x0..x1 {
|
||||
let dx = ((x as f64 + 0.5) * k).floor() as i32;
|
||||
let c = cov(dx, dy);
|
||||
let c = if c.is_finite() { (c.clamp(0.0, 1.0) * 255.0).round() as u8 } else { 0 };
|
||||
let (dx0, dx1) = span(x, self.doc[0]);
|
||||
let i = y * self.w + x;
|
||||
let c = c.max(self.base.get(i).copied().unwrap_or(0));
|
||||
let base = self.base.get(i).copied().unwrap_or(0);
|
||||
let mut c = 0.0f32;
|
||||
let skip = base == u8::MAX || !touches(photocraft_geom::Rect::new(dx0, dy0, dx1, dy1));
|
||||
'block: for dy in if skip { 0..0 } else { dy0..dy1 } {
|
||||
for dx in dx0..dx1 {
|
||||
let v = cov(dx, dy);
|
||||
if v > c {
|
||||
c = v;
|
||||
if c >= 1.0 {
|
||||
break 'block;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let c = if c.is_finite() { (c.clamp(0.0, 1.0) * 255.0).round() as u8 } else { 0 };
|
||||
let c = c.max(base);
|
||||
if let Some(v) = self.cov.get_mut(i) {
|
||||
*v = c;
|
||||
}
|
||||
@@ -312,6 +343,41 @@ mod tests {
|
||||
assert_eq!(t.coverage(40, 40), 0);
|
||||
}
|
||||
|
||||
/// On a big document one mask pixel stands for k x k document pixels. A thin Remove stroke
|
||||
/// that falls between the mask pixels' centres still removes what it touches, so it still
|
||||
/// shows.
|
||||
#[test]
|
||||
fn a_thin_reach_stroke_on_a_big_document_still_shows() {
|
||||
// A 2 px brush reaches document rows 10-14, a 4 px one rows 9-16; the mask's centres
|
||||
// sample rows 9 and 15 at k = 6, rows 6 and 18 at k = 12.
|
||||
for (doc, size) in [([6000, 4000], 2.0), ([12000, 8000], 4.0)] {
|
||||
let brush = BrushSettings { size, hardness: 1.0, ..Default::default() };
|
||||
let pts = [[300.0, 12.5, 1.0], [3000.0, 12.5, 1.0]];
|
||||
let mut t = Trail::new(doc);
|
||||
t.feed_reach(&pts, || brush.clone());
|
||||
let x = (1500.0 / t.scale()) as usize;
|
||||
assert!((0..3).any(|y| t.coverage(x, y) == 255), "{size} px on {doc:?}: the stroke shows");
|
||||
assert_eq!(t.coverage(x, 3), 0, "{size} px on {doc:?}: nothing below it");
|
||||
let mut s = Trail::new(doc);
|
||||
s.feed_stroke_reach(&pts, || brush.clone());
|
||||
assert!((0..3).any(|y| s.coverage(x, y) == 255), "{size} px on {doc:?}: a waiting stroke too");
|
||||
}
|
||||
}
|
||||
|
||||
/// A later stroke restamps its whole dirty rectangle, earlier strokes' tint included, also
|
||||
/// off its own path.
|
||||
#[test]
|
||||
fn a_crossing_reach_stroke_keeps_the_earlier_tint_inside_its_rect() {
|
||||
let mut t = Trail::new([200, 200]);
|
||||
let brush = BrushSettings { size: 20.0, ..Default::default() };
|
||||
t.feed_stroke_reach(&[[20.0, 100.0, 1.0], [180.0, 100.0, 1.0]], || brush.clone());
|
||||
t.feed_stroke_reach(&[[20.0, 20.0, 1.0], [180.0, 180.0, 1.0]], || brush.clone());
|
||||
assert_eq!(t.coverage(150, 100), 255, "the first stroke, off the second, inside its rect");
|
||||
assert_eq!(t.coverage(50, 100), 255);
|
||||
assert_eq!(t.coverage(150, 150), 255, "the second stroke");
|
||||
assert_eq!(t.coverage(150, 40), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_points_and_huge_documents_are_safe() {
|
||||
let mut t = Trail::new([300_000, 2]);
|
||||
|
||||
Reference in new issue
Block a user