diff --git a/crates/engine/src/retouch_cmds.rs b/crates/engine/src/retouch_cmds.rs index 063eca3363..d7283e247d 100644 --- a/crates/engine/src/retouch_cmds.rs +++ b/crates/engine/src/retouch_cmds.rs @@ -203,1329 +203,6 @@ fn damage_json(r: Rect) -> Value { json!([r.x0, r.y0, r.width(), r.height()]) } -// --------------------------------------------------------------------------------------------- -// Sampling sources -// --------------------------------------------------------------------------------------------- - -/// Which pixels Clone Stamp / Healing Brush sample. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) enum SampleLayers { - Current, - CurrentAndBelow, - All, -} - -fn sample_layers(p: &Value, cmd: &str) -> Result { - match string(p, "sampleLayer", "current") { - "current" => Ok(SampleLayers::Current), - "currentAndBelow" => Ok(SampleLayers::CurrentAndBelow), - "all" => Ok(SampleLayers::All), - o => Err(bad(cmd, format!("unknown sampleLayer `{o}` (current|currentAndBelow|all)"))), - } -} - -/// Composite pixels of `rect` in `fmt` (native channels): the whole document, or the target layer -/// and everything below it. -pub(crate) fn composite_region(pre: &Document, id: Option, which: SampleLayers, rect: Rect, fmt: PixelFormat) -> Region { - let mut doc = pre.clone(); - if which == SampleLayers::CurrentAndBelow - && let Some(id) = id - { - // Hide everything composited after (above) the target in the bottom-to-top walk. - let walk: Vec<(Vec, LayerId)> = pre.walk().into_iter().map(|(p, _, l)| (p, l.id)).collect(); - if let Some(pos) = walk.iter().position(|(_, l)| *l == id) { - for (path, _) in &walk[pos + 1..] { - if let Some(l) = doc.layer_at_mut(path) { - l.visible = false; - } - } - } - } - let buf = photocraft_compose::render(&doc, rect); - let n = fmt.channels(); - let mut out = Region::new(rect, n); - let mut enc = [0.0f32; 8]; - for (i, px) in buf.px.iter().enumerate() { - from_rgba_into(&fmt, *px, &mut enc); - out.data[i * n..(i + 1) * n].copy_from_slice(&enc[..n]); - } - out -} - -/// Pixels to sample at `rect` (document coordinates, already offset to the source position). -fn sample(pre: &Document, id: Option, surf: &Surface, which: SampleLayers, rect: Rect) -> Region { - match which { - SampleLayers::Current => Region::read(surf, rect), - _ => composite_region(pre, id, which, rect, surf.format()), - } -} - -/// Where a clone stroke samples: `offset` / `source` params, else the active Clone Source slot, -/// with the slot's (or the call's) scale, rotation and flips (see `presets::clone_source`). -fn clone_mapping(s: &mut Session, p: &Value, stroke: &Stroke, cmd: &str) -> Result { - let f = stroke.points[0]; - crate::presets::clone_source::mapping(s, p, (f.x, f.y), cmd) -} - -/// Source pixels for destination `rect`: a translated read, or a bilinear resample when the -/// clone source is scaled, rotated or flipped. -#[allow(clippy::too_many_arguments)] -fn clone_sample( - pre: &Document, - id: Option, - surf: &Surface, - which: SampleLayers, - rect: Rect, - map: &crate::presets::clone_source::Mapping, - wrap: bool, - cmd: &str, -) -> Result { - let read = |rect| { - if wrap { periodic::sample(pre, id, surf, which, rect, cmd) } else { Ok(sample(pre, id, surf, which, rect)) } - }; - if map.is_translation() { - let off = map.offset(); - let src = rect.translate(off.0, off.1); - // `translate` saturates at the i32 edges, so a far source (issue #1111) yields a window - // smaller than `rect`; relabelling that as `rect` would index past the sampled pixels. - if src.width() != rect.width() || src.height() != rect.height() { - return Err(bad(cmd, format!("source offset [{}, {}] is too far: the sample window leaves the coordinate range", off.0, off.1))); - } - let mut r = read(src)?; - r.rect = rect; - return Ok(r); - } - let src = read(map.source_rect(rect))?; - Ok(crate::presets::clone_source::resample(&src, rect, map, alpha_index(&surf.format()))) -} - -/// Read-only clone overlay. Shares the stroke sampler without anchoring a source or editing history. -/// Both destination and transformed source allocations are bounded for interactive use. -pub fn clone_preview(s: &Session, rect: Rect, map: &crate::presets::clone_source::Mapping, sample_layer: &str) -> Result { - clone_preview_periodic(s, rect, map, sample_layer, false) -} - -/// Clone-source overlay with optional source repetition for View > Pattern Preview. -pub fn clone_preview_periodic( - s: &Session, - rect: Rect, - map: &crate::presets::clone_source::Mapping, - sample_layer: &str, - wrap: bool, -) -> Result { - if ![map.source.0, map.source.1, map.anchor.0, map.anchor.1].into_iter().chain(map.m).all(|v| v.is_finite() && v.abs() < 1_000_000.0) { - return Err(bad("clone.preview", "invalid preview mapping")); - } - if ![rect.x0, rect.y0, rect.x1, rect.y1].into_iter().all(|v| v.abs_diff(0) < 1_000_000) - || ![(rect.x0, rect.y0), (rect.x1, rect.y1), (rect.x0, rect.y1), (rect.x1, rect.y0)].into_iter().all(|(x, y)| { - let (x, y) = map.map(f64::from(x), f64::from(y)); - x.is_finite() && y.is_finite() && x.abs() < 1_000_000.0 && y.abs() < 1_000_000.0 - }) - { - return Err(bad("clone.preview", "preview coordinates exceed their budget")); - } - let bounded = |r: Rect| r.width() > 0 && r.height() > 0 && r.width() <= 1024 && r.height() <= 1024; - if !bounded(rect) || !bounded(map.source_rect(rect)) { - return Err(bad("clone.preview", "preview region exceeds its budget")); - } - let d = s.active().ok_or(EngineError::NoDocument)?; - let id = d.active_layer.ok_or_else(|| bad("clone.preview", "no active layer"))?; - let surf = d.doc.layer(id).and_then(|l| l.surface()).ok_or_else(|| bad("clone.preview", "no pixel surface"))?; - let which = sample_layers(&json!({"sampleLayer": sample_layer}), "clone.preview")?; - let region = clone_sample(&d.doc, Some(id), surf, which, rect, map, wrap, "clone.preview")?; - let px = region.data.chunks_exact(surf.format().channels()).map(|p| to_rgba(&surf.format(), p)).collect(); - Ok(photocraft_compose::Buffer { rect, px }) -} - -/// Result JSON shared by Clone Stamp and Healing Brush: the offset used and where the next stroke -/// should sample from if the UI keeps Photoshop's "Aligned" semantics. -fn clone_result(dmg: Rect, off: (i32, i32), aligned: bool, p: &Value, stroke: &Stroke) -> Value { - let f = stroke.points[0]; - let source = point(p, "source").unwrap_or((f.x + off.0 as f64, f.y + off.1 as f64)); - // Aligned: the offset persists (next stroke samples at its own first point + offset). - // Non-aligned: every stroke restarts at the original source point. - json!({ "damage": damage_json(dmg), "offset": [off.0, off.1], "aligned": aligned, "nextSource": if aligned { Value::Null } else { json!([source.0, source.1]) } }) -} - -fn blend_param(p: &Value, cmd: &str) -> Result { - match p.get("mode").and_then(Value::as_str) { - None => Ok(BlendMode::Normal), - Some(m) => blend_from_str(m).ok_or_else(|| bad(cmd, format!("unknown blend mode `{m}`"))), - } -} - -/// Clamp results into the storable range: integer depths to 0..1, float depths only below at 0; -/// alpha always 0..1. -fn clamp_samples(fmt: &PixelFormat, data: &mut [f32]) { - let n = fmt.channels(); - let a = alpha_index(fmt); - let float = fmt.sample == SampleType::F32; - for px in data.chunks_exact_mut(n) { - for (c, v) in px.iter_mut().enumerate() { - *v = if Some(c) == a || !float { v.clamp(0.0, 1.0) } else { v.max(0.0) }; - } - } -} - -// --------------------------------------------------------------------------------------------- -// Clone Stamp / Healing Brush / History Brush -// --------------------------------------------------------------------------------------------- - -fn clone_stamp(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "paint.cloneStamp"; - let (stroke, id) = parse_brush(s, p, CMD)?; - reject_huge_stroke(CMD, &stroke)?; - let which = sample_layers(p, CMD)?; - let mode = blend_param(p, CMD)?; - let aligned = flag(p, "aligned", true); - // The live stroke the canvas already drew for exactly this commit: take its pixels instead of - // rendering the stroke again (the hitch on release). - let live = s.live_commit.take().filter(|l| l.matches(s, CMD, p, &stroke.points)); - let from_live = live.is_some(); - let map = clone_mapping(s, p, &stroke, CMD)?; - let off = map.offset(); - let dmg = - run_stroke(s, "Clone Stamp", id, p, |pre, surf, sel, lock| { - if let Some(l) = live { - *surf = l.surface; - return Ok(l.bounds); - } - if flag(p, "wrap", false) { - let mut renderer = photocraft_paint::StrokeRenderer::new(&stroke.brush, None, 1.0); - renderer.push(&stroke.points); - renderer.finish(); - let before = surf.clone(); - let bounds = renderer.bounds(); - return periodic::Composite { target: surf, before: &before, tile: pre.bounds(), opacity: stroke.brush.opacity, selection: sel, lock, mode } - .draw(bounds, &stroke.brush, &stroke.points, |r| clone_sample(pre, id, &before, which, r, &map, true, CMD)); - } - let (bounds, cov) = coverage(CMD, &stroke)?; - // Samples come from the pre-stroke state, so pixels painted earlier in the stroke are never re-cloned. - let paint = clone_sample(pre, id, surf, which, bounds, &map, flag(p, "wrap", false), CMD)?; - Ok(apply_coverage(surf, bounds, &cov, stroke.brush.opacity, sel, lock, &paint, mode)) - })?; - let mut r = clone_result(dmg, off, aligned, p, &stroke); - if from_live { - r["fromLive"] = json!(true); - } - Ok(r) -} - -/// What a live retouching stroke paints through its coverage. -enum LivePaint { - /// The clone source (Clone Stamp; the Healing Brush shows its texture while drawing and - /// blends it on release). - Clone { which: SampleLayers, map: crate::presets::clone_source::Mapping, cmd: &'static str }, - /// The History Brush's history state. - History(Surface), - /// The Pattern Stamp's pattern, tiled from its aligned placement (Impressionist strokes are - /// laid dab by dab: `pattern_stamp::live_impressionist`). - Pattern { tile: std::sync::Arc, place: photocraft_compose::pattern::Placement }, -} - -/// A retouching stroke shown while it is drawn: the dabs so far composited onto a copy of the -/// active document as the command with the same params commits them (the same coverage, -/// sampling and blend): Clone Stamp, History Brush, and the Healing Brush's texture. -pub struct LiveRetouch { - /// The active document with the stroke so far. - pub doc: std::sync::Arc, - cmd: String, - renderer: photocraft_paint::StrokeRenderer, - pre: std::sync::Arc, - pre_surf: Surface, - id: Option, - params: Value, - paint: LivePaint, - mode: BlendMode, - opacity: f32, - sel: Option, - lock: bool, - /// Where the doc shows what finishing the stroke now would add (a lone first dab, the - /// smoothing catch-up), redrawn on every step as the Brush's live stroke does. - tail: Rect, - /// The document and revision the stroke started on. - base: (photocraft_doc::DocId, u64), - brush: Box, - /// Every point rendered so far, in order. - points: Vec, -} - -impl LiveRetouch { - /// Start a live stroke of `cmd` (`paint.cloneStamp`, `paint.healingBrush`, - /// `paint.historyBrush` or an aligned, non-Impressionist `paint.patternStamp`) from its - /// params; their `points` are rendered. - pub fn begin(s: &Session, cmd: &str, p: &Value) -> Result { - has_pixel_layer(s).map_err(EngineError::Other)?; - if cmd == "paint.patternStamp" { - let st = pattern_stamp::Setup::parse(s, p)?; - if st.impressionist { - return Err(bad(cmd, "Impressionist strokes are laid dab by dab (pattern_stamp::live_impressionist)")); - } - let paint = LivePaint::Pattern { place: st.placement(), tile: st.tile }; - return Self::with_paint(s, cmd, p, st.stroke, st.id, paint, st.mode); - } - let (stroke, id) = parse_brush(s, p, cmd)?; - let mode = blend_param(p, cmd)?; - let paint = match cmd { - "paint.cloneStamp" | "paint.healingBrush" => { - let f = *stroke.points.first().ok_or_else(|| bad(cmd, "`points` is empty"))?; - LivePaint::Clone { - which: sample_layers(p, cmd)?, - map: crate::presets::clone_source::resolve(s, p, (f.x, f.y), cmd)?.0, - cmd: if cmd == "paint.healingBrush" { "paint.healingBrush" } else { "paint.cloneStamp" }, - } - } - "paint.historyBrush" => LivePaint::History(history_source(s, id, p, cmd)?), - other => return Err(bad(other, "live retouching strokes are the Clone Stamp, Healing Brush, History Brush and Pattern Stamp")), - }; - Self::with_paint(s, cmd, p, stroke, id, paint, mode) - } - - /// Start a live stroke of `cmd` painting `paint` through `stroke`'s coverage. - fn with_paint(s: &Session, cmd: &str, p: &Value, stroke: Stroke, id: Option, paint: LivePaint, mode: BlendMode) -> Result { - let st = s.active().ok_or(EngineError::NoDocument)?; - let (pre, base) = (st.doc.clone(), (st.doc.id, st.revision)); - let mut doc = (*pre).clone(); - let sel = doc.selection.clone(); - let (surf, lock) = crate::channel_cmds::target_surface(&mut doc, id, p)?; - let pre_surf = surf.clone(); - // As `stroke_coverage`, which the commit uses. - let renderer = photocraft_paint::StrokeRenderer::new(&stroke.brush, None, 1.0); - let opacity = stroke.brush.opacity; - let brush = Box::new(stroke.brush.clone()); - let paint = match paint { - LivePaint::History(src) if src.format() != pre_surf.format() => LivePaint::History(src.convert(pre_surf.format())), - other => other, - }; - let mut live = Self { - doc: std::sync::Arc::new(doc), - cmd: cmd.into(), - renderer, - pre, - pre_surf, - id, - params: p.clone(), - paint, - mode, - opacity, - sel, - lock, - tail: Rect::EMPTY, - base, - brush, - points: Vec::new(), - }; - live.push(&stroke.points)?; - Ok(live) - } - - /// Everything the stroke has touched so far. - pub fn bounds(&self) -> Rect { - let bounds = self.renderer.bounds().union(&self.tail); - if self.cmd == "paint.cloneStamp" && flag(&self.params, "wrap", false) { - periodic::project(bounds, self.pre.bounds()).into_iter().fold(Rect::EMPTY, |a, b| a.union(&b)) - } else { - bounds - } - } - - /// Every point rendered so far, in order. - pub fn points(&self) -> &[StrokePoint] { - &self.points - } - - /// The finished stroke as its commit would take it over ([`Session::offer_live_retouch`]). - /// Only the Clone Stamp and History Brush draw what they commit (the Healing Brush blends on - /// release), and only while the brush, layer and clone source are still the ones the stroke - /// started with: the document revision doesn't cover those. - pub(crate) fn into_commit(mut self, s: &Session) -> Result { - let (stroke, id) = parse_brush(s, &self.params, &self.cmd)?; - let source_unchanged = match &self.paint { - LivePaint::Clone { map, .. } if self.cmd == "paint.cloneStamp" => { - let f = stroke.points.first().ok_or_else(|| bad(&self.cmd, "`points` is empty"))?; - crate::presets::clone_source::resolve(s, &self.params, (f.x, f.y), &self.cmd)?.0 == *map - } - LivePaint::History(_) if self.cmd == "paint.historyBrush" => true, - _ => return Err(bad(&self.cmd, "only Clone Stamp and History Brush strokes commit their live pixels")), - }; - if !source_unchanged || *self.brush != stroke.brush || id != self.id { - return Err(bad(&self.cmd, "the brush, layer or clone source changed during the stroke")); - } - let bounds = self.bounds(); - let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; - let surface = surf.clone(); - Ok(crate::brush_cmds::LiveCommit { mixer: None, cmd: self.cmd, base: self.base, params: self.params, seed: None, points: self.points, surface, bounds }) - } - - /// Render more points; returns the rectangle that changed. Each changed area is redrawn from - /// the pre-stroke pixels with the stroke's coverage so far, so overlapping dabs build up as in - /// the commit, and pixels painted earlier in the stroke are never re-cloned. What finishing - /// the stroke now would add is drawn too (a lone first dab shows on the press), and redrawn on - /// every step. - /// Invalid coordinates reject the whole batch without changing the preview. - /// Only the position and pressure of `pts` are used: the commands take no tilt or times. - pub fn push(&mut self, pts: &[StrokePoint]) -> Result { - crate::brush_cmds::check_coords(pts, &self.cmd)?; - let pts: Vec<_> = pts.iter().map(|q| StrokePoint::new(q.x, q.y, q.pressure)).collect(); - self.points.extend_from_slice(&pts); - self.renderer.push(&pts); - if self.cmd == "paint.cloneStamp" && flag(&self.params, "wrap", false) { - return self.push_periodic(); - } - let old = std::mem::replace(&mut self.tail, Rect::EMPTY); - let mut dmg = Rect::EMPTY; - if !old.is_empty() { - // Back to the stroke without the previous tail. - let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; - surf.write_region(old, &self.pre_surf.read_region(old)); - self.renderer.mark_dirty(old); - dmg = old; - } - let r = self.renderer.take_dirty_rect(); - let cov = self.renderer.coverage_in(r); - dmg = dmg.union(&self.composite(r, &cov)?); - if let Some(mut tail) = self.renderer.tail_preview() { - let r = tail.take_dirty_rect(); - let cov = tail.coverage_in(r); - self.tail = self.composite(r, &cov)?; - dmg = dmg.union(&self.tail); - } - Ok(dmg) - } - - fn push_periodic(&mut self) -> Result { - let mut dirty = self.renderer.take_dirty_rect().union(&self.tail); - let mut tail = self.renderer.tail_preview(); - self.tail = tail.as_mut().map_or(Rect::EMPTY, |t| t.take_dirty_rect()); - dirty = dirty.union(&self.tail); - let LivePaint::Clone { which, map, cmd } = &self.paint else { - return Err(bad(&self.cmd, "periodic painting requires a clone source")); - }; - let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; - periodic::Composite { - target: surf, - before: &self.pre_surf, - tile: self.pre.bounds(), - opacity: self.opacity, - selection: self.sel.as_ref(), - lock: self.lock, - mode: self.mode, - } - .draw(dirty, &self.brush, &self.points, |r| clone_sample(&self.pre, self.id, &self.pre_surf, *which, r, map, true, cmd)) - } - - /// Redraw `r` from the pre-stroke pixels with the clone source through coverage `cov`. - fn composite(&mut self, r: Rect, cov: &[f32]) -> Result { - if r.is_empty() { - return Ok(r); - } - let paint = match &self.paint { - LivePaint::Clone { which, map, cmd } => { - clone_sample(&self.pre, self.id, &self.pre_surf, *which, r, map, *cmd == "paint.cloneStamp" && flag(&self.params, "wrap", false), cmd)? - } - LivePaint::History(src) => Region::read(src, r), - LivePaint::Pattern { tile, place } => pattern_stamp::paint_region(tile, place, &self.pre_surf.format(), r), - }; - let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; - surf.write_region(r, &self.pre_surf.read_region(r)); - Ok(apply_coverage(surf, r, cov, self.opacity, self.sel.as_ref(), self.lock, &paint, self.mode).union(&r)) - } -} - -/// Grow a stroke's coverage map by `m` pixels of zero coverage (a Dirichlet boundary for the solve). -fn pad_coverage(bounds: Rect, cov: &[f32], m: i32) -> (Rect, Vec) { - let g = bounds.inflate(m); - let (w, gw) = (bounds.width() as usize, g.width() as usize); - let mut out = vec![0.0f32; gw * g.height() as usize]; - for (i, c) in cov.iter().enumerate() { - let (x, y) = (i % w + m as usize, i / w + m as usize); - out[y * gw + x] = *c; - } - (g, out) -} - -/// Seamless-clone `src` into `dst` over the mask (both regions cover the same rect). The solve runs -/// only on the mask's bounding box plus a one-pixel Dirichlet ring; the rest is `dst`. -fn heal_region(fmt: &PixelFormat, src: &Region, dst: &Region, mask: &[bool]) -> Region { - let w = dst.width(); - let bbox = mask.iter().enumerate().filter(|(_, m)| **m).fold(Rect::EMPTY, |r, (i, _)| { - let (x, y) = (dst.rect.x0 + (i % w) as i32, dst.rect.y0 + (i / w) as i32); - r.union(&Rect::new(x, y, x + 1, y + 1)) - }); - let mut out = dst.clone(); - if bbox.is_empty() { - return out; - } - let r = bbox.inflate(1).intersect(&dst.rect); - let (s, d) = (src.crop(r), dst.crop(r)); - let m: Vec = (r.y0..r.y1).flat_map(|y| (r.x0..r.x1).map(move |x| (x, y))).map(|(x, y)| mask[dst.index(x, y)]).collect(); - let mut data = poisson::seamless_clone(d.width(), d.height(), d.ch, &s.data, &d.data, &m); - clamp_samples(fmt, &mut data); - let (n, rw) = (dst.ch, r.width() as usize); - for y in r.y0..r.y1 { - let o = out.index(r.x0, y) * n; - let i = (y - r.y0) as usize * rw * n; - out.data[o..o + rw * n].copy_from_slice(&data[i..i + rw * n]); - } - out -} - -fn healing_brush(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "paint.healingBrush"; - let (stroke, id) = parse_brush(s, p, CMD)?; - reject_huge_stroke(CMD, &stroke)?; - let which = sample_layers(p, CMD)?; - let mode = blend_param(p, CMD)?; - let aligned = flag(p, "aligned", true); - let map = clone_mapping(s, p, &stroke, CMD)?; - let off = map.offset(); - let dmg = run_stroke(s, "Healing Brush", id, p, |pre, surf, sel, lock| { - let (bounds, cov) = coverage(CMD, &stroke)?; - let (g, cov) = pad_coverage(bounds, &cov, 2); - let fmt = surf.format(); - let src = clone_sample(pre, id, surf, which, g, &map, false, CMD)?; - // The destination the texture is fitted to: the layer itself, or what the user sees when - // sampling several layers (so healing onto an empty layer matches the composite). - let dst = if which == SampleLayers::Current { Region::read(surf, g) } else { composite_region(pre, id, which, g, fmt) }; - let mask: Vec = cov.iter().map(|c| *c > 0.0).collect(); - let healed = heal_region(&fmt, &src, &dst, &mask); - Ok(apply_coverage(surf, g, &cov, stroke.brush.opacity, sel, lock, &healed, mode)) - })?; - Ok(clone_result(dmg, off, aligned, p, &stroke)) -} - -/// The History Brush's source: the targeted surface (pixels, mask, channel) as it was in History -/// panel row `state` or snapshot `snapshot` (default: the source set in the History panel, -/// `history.setSource`, else the oldest state still held, the "Open" state unless trimmed). -fn history_source(s: &Session, id: Option, p: &Value, cmd: &str) -> Result { - let st = s.active().ok_or(EngineError::NoDocument)?; - let source_doc = crate::history_source_cmds::paint_source(st, p, cmd)?; - crate::channel_cmds::read_target_surface(source_doc.as_ref(), id, p) - .cloned() - .map_err(|_| EngineError::Other("the target did not exist (as pixels, a mask or a channel) in that history state".into())) -} - -fn history_brush(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "paint.historyBrush"; - let (stroke, id) = parse_brush(s, p, CMD)?; - let mode = blend_param(p, CMD)?; - // The coverage and the source region below are both dense over the stroke's bounds, so a - // thin stroke across a large canvas would still ask for the whole box. Refuse it from the - // points and brush size, before the source surface is cloned or anything is rendered (#4961). - reject_huge_stroke(CMD, &stroke)?; - let live = s.live_commit.take().filter(|l| l.matches(s, CMD, p, &stroke.points)); - let from_live = live.is_some(); - let src_surface = history_source(s, id, p, CMD)?; - let dmg = run_stroke(s, "History Brush", id, p, |_, surf, sel, lock| { - if let Some(l) = live { - *surf = l.surface; - return Ok(l.bounds); - } - let fmt = surf.format(); - let src = if src_surface.format() == fmt { src_surface } else { src_surface.convert(fmt) }; - let (bounds, cov) = coverage(CMD, &stroke)?; - let paint = Region::read(&src, bounds); - Ok(apply_coverage(surf, bounds, &cov, stroke.brush.opacity, sel, lock, &paint, mode)) - })?; - let mut r = json!({ "damage": damage_json(dmg) }); - if from_live { - r["fromLive"] = json!(true); - } - Ok(r) -} - -// --------------------------------------------------------------------------------------------- -// Spot Healing -// --------------------------------------------------------------------------------------------- - -/// Dilate a mask by `r` pixels (square structuring element). -fn dilate(w: usize, h: usize, m: &[bool], r: usize) -> Vec { - let mut tmp = vec![false; w * h]; - for y in 0..h { - for x in 0..w { - if m[y * w + x] { - for xx in x.saturating_sub(r)..(x + r + 1).min(w) { - tmp[y * w + xx] = true; - } - } - } - } - let mut out = vec![false; w * h]; - for y in 0..h { - for x in 0..w { - if tmp[y * w + x] { - for yy in y.saturating_sub(r)..(y + r + 1).min(h) { - out[yy * w + x] = true; - } - } - } - } - out -} - -/// Copy a translated Proximity Match patch from the *pre-healing* pixels. Reading -/// `out` here would make overlapping offsets repeatedly copy the pixels just written, -/// leaving streaks instead of the selected source texture. -fn proximity_source_patch(img: &[f32], w: usize, h: usize, ch: usize, mask: &[bool], dx: i32, dy: i32) -> Option> { - if w == 0 || h == 0 || ch == 0 || mask.len() != w.checked_mul(h)? || img.len() != mask.len().checked_mul(ch)? { - return None; - } - let mut out = img.to_vec(); - for (p, masked) in mask.iter().enumerate() { - if !masked { - continue; - } - let x = i32::try_from(p % w).ok()?.checked_add(dx)?; - let y = i32::try_from(p / w).ok()?.checked_add(dy)?; - if x < 0 || y < 0 || x >= i32::try_from(w).ok()? || y >= i32::try_from(h).ok()? { - return None; - } - let src = (usize::try_from(y).ok()?.checked_mul(w)? + usize::try_from(x).ok()?).checked_mul(ch)?; - let dst = p.checked_mul(ch)?; - out.get_mut(dst..dst + ch)?.copy_from_slice(img.get(src..src + ch)?); - } - Some(out) -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum SpotType { - ContentAware, - CreateTexture, - ProximityMatch, -} - -/// Fill the stroke area with no source, then gradient-domain blend it into its surroundings. With -/// `all` (Sample All Layers) it heals what is visible, so it works on an empty layer above the image. -/// -/// Photoshop 25.4 (measured): a dab changes exactly the brush's footprint, and Content-Aware takes -/// its texture from around it, as Edit › Fill does from its window -/// ([`crate::fill_cmds::sampling_window`] of the stroke's bounds), which is what this samples. -fn spot_heal_surface(surf: &mut Surface, pre: &Document, stroke: &Stroke, kind: SpotType, all: bool, sel: Option<&Surface>, lock: bool) -> Result { - let (bounds, cov) = coverage("paint.spotHealing", stroke)?; - let size = stroke.brush.size; - let region_rect = crate::fill_cmds::sampling_window(bounds, pre.bounds()); - if region_rect.is_empty() { - return Ok(Rect::EMPTY); - } - // Proximity Match looks for its source anywhere in that window. - let margin = (region_rect.width().max(region_rect.height()) / 2) as i32; - let fmt = surf.format(); - let img = if all { composite_region(pre, None, SampleLayers::All, region_rect, fmt) } else { Region::read(surf, region_rect) }; - let (w, h, ch) = (img.width(), img.height(), img.ch); - let bw = bounds.width() as usize; - let mut stroke_cov = vec![0.0f32; w * h]; - for (i, c) in cov.iter().enumerate() { - let (x, y) = (bounds.x0 + (i % bw) as i32, bounds.y0 + (i / bw) as i32); - if region_rect.contains(x, y) { - stroke_cov[img.index(x, y)] = *c; - } - } - let hole: Vec = stroke_cov.iter().map(|c| *c > 0.0).collect(); - if !hole.iter().any(|h| *h) { - return Ok(Rect::EMPTY); - } - // Synthesise over a slightly larger domain than the Poisson region, so the solve's boundary pixels - // carry synthesised values that differ from the untouched surroundings: that mismatch is what the - // membrane corrects (otherwise the Dirichlet data would be all zero and the blend a no-op). - let domain = dilate(w, h, &hole, 2); - let heal_mask = dilate(w, h, &hole, 1); - let content_aware = - |img: &[f32]| inpaint::complete(w, h, ch, img, &domain, &CompleteParams::default()).unwrap_or_else(|| poisson::membrane_fill(w, h, ch, img, &domain)); - let filled = match kind { - SpotType::ContentAware => content_aware(&img.data), - SpotType::CreateTexture => inpaint::synthesize(w, h, ch, &img.data, &domain, (size / 4.0).clamp(6.0, 32.0) as usize, 0x5eed), - SpotType::ProximityMatch => { - let ring = (size / 8.0).clamp(3.0, 16.0) as usize; - match inpaint::best_offset(w, h, ch, &img.data, &domain, ring, margin) { - Some((dx, dy)) => proximity_source_patch(&img.data, w, h, ch, &domain, dx, dy).unwrap_or_else(|| content_aware(&img.data)), - None => content_aware(&img.data), - } - } - }; - let src = Region { rect: region_rect, ch, data: filled }; - let healed = heal_region(&fmt, &src, &img, &heal_mask); - let dmg = bounds.intersect(®ion_rect); - let dmg_cov: Vec = (dmg.y0..dmg.y1).flat_map(|y| (dmg.x0..dmg.x1).map(move |x| (x, y))).map(|(x, y)| stroke_cov[img.index(x, y)]).collect(); - Ok(apply_coverage(surf, dmg, &dmg_cov, stroke.brush.opacity, sel, lock, &healed, BlendMode::Normal)) -} - -fn spot_healing(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "paint.spotHealing"; - let (stroke, id) = parse_brush(s, p, CMD)?; - reject_huge_stroke(CMD, &stroke)?; - let kind = match string(p, "type", "contentAware") { - "contentAware" => SpotType::ContentAware, - "createTexture" => SpotType::CreateTexture, - "proximityMatch" => SpotType::ProximityMatch, - o => return Err(bad(CMD, format!("unknown type `{o}` (contentAware|createTexture|proximityMatch)"))), - }; - let all = sample_all_layers(p); - let dmg = run_stroke(s, "Spot Healing Brush", id, p, |pre, surf, sel, lock| spot_heal_surface(surf, pre, &stroke, kind, all, sel, lock))?; - Ok(json!({ "damage": damage_json(dmg) })) -} - -// --------------------------------------------------------------------------------------------- -// Toning (Dodge / Burn / Sponge) and focus (Blur / Sharpen), Smudge -// --------------------------------------------------------------------------------------------- - -/// Per-dab strength such that one pass over a point totals roughly `e`, whatever the spacing -/// (about `1/spacing` dabs overlap each point on the stroke's centre line). -#[inline] -fn per_dab(e: f32, spacing: f32) -> f32 { - 1.0 - (1.0 - e.clamp(0.0, 1.0)).powf(spacing.clamp(0.01, 1.0)) -} - -/// Apply an RGB colour transform `f(rgb, strength)` to each covered pixel of a dab (through straight -/// RGBA, so it works for Gray/CMYK/Lab too; untouched pixels are never round-tripped). -fn color_dab(fmt: &PixelFormat, work: &mut Region, fp: &Footprint, strength: f32, spacing: f32, f: impl Fn([f32; 3], f32) -> [f32; 3]) { - let r = fp.rect.intersect(&work.rect); - let n = fmt.channels(); - let mut enc = [0.0f32; 8]; - for y in r.y0..r.y1 { - for x in r.x0..r.x1 { - let k = per_dab(fp.at(x, y) * strength, spacing); - if k <= 0.0 { - continue; - } - let px = work.px_mut(x, y); - let rgba = to_rgba(fmt, px); - let o = f([rgba[0], rgba[1], rgba[2]], k); - from_rgba_into(fmt, [o[0], o[1], o[2], rgba[3]], &mut enc); - let a = alpha_index(fmt); - for c in 0..n { - if Some(c) != a { - px[c] = enc[c]; - } - } - } - } -} - -/// Dodge and Burn: a stroke tones each pixel once, from its original colour, mixed in by the -/// stroke's coverage there (`toned` is the full-exposure colour). Dabs build up coverage as in a -/// brush stroke, capped at full coverage, so Exposure caps a stroke the way Opacity caps a paint -/// stroke: overlapping dabs and passes within one stroke don't compound the tone curve, and a -/// new stroke builds on the last. -fn tone_stroke(fmt: PixelFormat, toned: impl Fn([f32; 3]) -> [f32; 3] + 'static) -> DabEffect { - /// Side of the cells that remember, per touched pixel, the coverage applied so far and the - /// original pixel (copied from the working pixels the first time a dab reaches it). Dense - /// cells rather than a map per pixel: a map lookup per covered pixel made long strokes with - /// large brushes over ten times slower. - const CELL: i32 = 64; - struct Cell { - cov: Vec, - orig: Vec, - } - let mut cells: std::collections::HashMap<(i32, i32), Cell> = std::collections::HashMap::new(); - let a = alpha_index(&fmt); - let n = fmt.channels(); - Box::new(move |work, fp| { - let r = fp.rect.intersect(&work.rect); - if r.is_empty() { - return; - } - let mut enc = [0.0f32; 8]; - for cy in r.y0.div_euclid(CELL)..=(r.y1 - 1).div_euclid(CELL) { - for cx in r.x0.div_euclid(CELL)..=(r.x1 - 1).div_euclid(CELL) { - let cr = Rect::new(cx * CELL, cy * CELL, cx * CELL + CELL, cy * CELL + CELL); - let part = r.intersect(&cr); - if part.is_empty() { - continue; - } - let len = (CELL * CELL) as usize; - // Coverage -1: not touched yet, so `orig` isn't filled in for that pixel. - let cell = cells.entry((cx, cy)).or_insert_with(|| Cell { cov: vec![-1.0; len], orig: vec![0.0; len * n] }); - for y in part.y0..part.y1 { - for x in part.x0..part.x1 { - let k = fp.at(x, y).clamp(0.0, 1.0); - if k <= 0.0 { - continue; - } - let i = ((y - cr.y0) * CELL + (x - cr.x0)) as usize; - let (Some(prev), Some(src)) = (cell.cov.get_mut(i), cell.orig.get_mut(i * n..i * n + n)) else { continue }; - if *prev < 0.0 { - // First touch: the working pixel is still the original. - src.copy_from_slice(work.px(x, y)); - *prev = 0.0; - } - // Dabs build up the stroke's coverage as a brush stroke's do (flow), up - // to full coverage; the tone is applied once, from the original. - let k = *prev + k * (1.0 - *prev); - if k <= *prev { - continue; - } - *prev = k; - let o = to_rgba(&fmt, src); - let t = toned([o[0], o[1], o[2]]); - let m = [o[0] + (t[0] - o[0]) * k, o[1] + (t[1] - o[1]) * k, o[2] + (t[2] - o[2]) * k]; - from_rgba_into(&fmt, [m[0], m[1], m[2], o[3]], &mut enc); - let px = work.px_mut(x, y); - for c in 0..n { - if Some(c) != a { - px[c] = enc[c]; - } - } - } - } - } - } - }) -} - -fn tone_range(p: &Value, cmd: &str) -> Result { - match string(p, "range", "midtones") { - "shadows" => Ok(ToneRange::Shadows), - "midtones" => Ok(ToneRange::Midtones), - "highlights" => Ok(ToneRange::Highlights), - o => Err(bad(cmd, format!("unknown range `{o}` (shadows|midtones|highlights)"))), - } -} - -/// A sequential-dab tool's settings (Dodge, Burn, Sponge, Blur, Sharpen, Smudge), parsed once and -/// turned into its per-dab effect, for the commit and the live preview alike. -#[derive(Clone, Debug, PartialEq)] -enum DabTool { - Tone { burn: bool, range: ToneRange, exposure: f32, protect: bool }, - Sponge { saturate: bool, vibrance: bool }, - Focus { sharpen: bool, strength: f32, protect: bool, sigma: f32 }, - Smudge { strength: f32, finger: Option<[f32; 4]>, max_size: f32 }, -} - -/// One dab's edit of the working copy. -pub(crate) type DabEffect = Box; - -/// Pixels an effect reads beyond the dab it writes. Smudge's pickup is a fixed-size square, -/// independent of pressure, roundness and the dab's current angle. -#[derive(Clone, Copy)] -enum DabNeighborhood { - Halo(i32), - Smudge(i32), -} - -impl DabNeighborhood { - fn halo(self) -> i32 { - match self { - Self::Halo(radius) | Self::Smudge(radius) => radius, - } - } - - fn read_rect(self, rect: Rect, dab: &photocraft_paint::Dab) -> Rect { - match self { - Self::Halo(radius) => rect.inflate(radius), - Self::Smudge(radius) => { - let (x, y) = (dab.center.x.floor() as i32, dab.center.y.floor() as i32); - rect.union(&Rect::new( - x.saturating_sub(radius), - y.saturating_sub(radius), - x.saturating_add(radius).saturating_add(1), - y.saturating_add(radius).saturating_add(1), - )) - } - } - } -} - -impl DabTool { - /// The tool for `cmd` and its history label. - fn parse(s: &Session, cmd: &str, p: &Value, stroke: &Stroke) -> Result<(Self, &'static str)> { - Ok(match cmd { - "paint.dodge" | "paint.burn" => { - let burn = cmd == "paint.burn"; - let tool = Self::Tone { - burn, - range: tone_range(p, cmd)?, - exposure: num(p, "exposure", 50.0).clamp(1.0, 100.0) / 100.0, - protect: flag(p, "protectTones", true), - }; - (tool, if burn { "Burn Tool" } else { "Dodge Tool" }) - } - "paint.sponge" => { - let saturate = match string(p, "mode", "desaturate") { - "desaturate" => false, - "saturate" => true, - o => return Err(bad(cmd, format!("unknown mode `{o}` (desaturate|saturate)"))), - }; - (Self::Sponge { saturate, vibrance: flag(p, "vibrance", true) }, "Sponge Tool") - } - "paint.blur" | "paint.sharpen" => { - let sharpen = cmd == "paint.sharpen"; - let tool = Self::Focus { - sharpen, - strength: num(p, "strength", 50.0).clamp(1.0, 100.0) / 100.0, - protect: flag(p, "protectDetail", true), - sigma: (stroke.brush.size * 0.03).clamp(1.0, 4.0), - }; - (tool, if sharpen { "Sharpen Tool" } else { "Blur Tool" }) - } - "paint.smudge" => { - let finger = flag(p, "fingerPainting", false).then_some(s.tools.foreground); - (Self::Smudge { strength: num(p, "strength", 50.0).clamp(1.0, 100.0) / 100.0, finger, max_size: stroke.brush.size }, "Smudge Tool") - } - o => return Err(bad(o, "not a sequential-dab tool")), - }) - } - - /// How far around each dab the effect reads (Blur and Sharpen read a neighbourhood). - fn neighborhood(&self) -> DabNeighborhood { - match self { - Self::Focus { sigma, .. } => DabNeighborhood::Halo((sigma * 3.0).ceil() as i32 + 1), - Self::Smudge { max_size, .. } => DabNeighborhood::Smudge(Smudge::sampling_radius(*max_size)), - _ => DabNeighborhood::Halo(0), - } - } - - /// The per-dab effect on a working copy in `fmt`, for a brush of `spacing`. - fn effect(&self, fmt: PixelFormat, spacing: f32) -> DabEffect { - let sp = spacing; - match self.clone() { - Self::Tone { burn, range, exposure, protect } => tone_stroke(fmt, move |c| dodge_burn(c, exposure, range, burn, protect)), - // Flow is already in each dab's coverage; a full-flow pass moves colours half-way. - Self::Sponge { saturate, vibrance } => Box::new(move |work, fp| color_dab(&fmt, work, fp, 0.5, sp, |c, k| sponge(c, k, saturate, vibrance))), - Self::Focus { sharpen, strength, protect, sigma } => { - let a = alpha_index(&fmt); - Box::new(move |work, fp| { - let r = fp.rect.intersect(&work.rect); - if r.is_empty() { - return; - } - let (w, h, n) = (work.width(), work.height(), work.ch); - let win = ((r.x0 - work.rect.x0) as usize, (r.y0 - work.rect.y0) as usize, (r.x1 - work.rect.x0) as usize, (r.y1 - work.rect.y0) as usize); - let out = if sharpen { local_sharpen(&work.data, w, h, n, a, win, 1.0, protect) } else { local_blur(&work.data, w, h, n, a, win, sigma) }; - let rw = r.width() as usize; - // Row-parallel, like the blur itself (#5426). - let (wx0, wy0) = (work.rect.x0, work.rect.y0); - work.data.par_chunks_mut(w * n).enumerate().for_each(|(row, line)| { - let y = wy0 + row as i32; - if y < r.y0 || y >= r.y1 { - return; - } - for x in r.x0..r.x1 { - let k = per_dab(fp.at(x, y) * strength, sp); - if k <= 0.0 { - continue; - } - let o = ((y - r.y0) as usize * rw + (x - r.x0) as usize) * n; - let i = (x - wx0) as usize * n; - let px = &mut line[i..i + n]; - for c in 0..n { - px[c] += (out[o + c] - px[c]) * k; - } - } - }); - }) - } - Self::Smudge { strength, finger, max_size } => { - let finger_px = finger.map(|fg| photocraft_raster::from_rgba(&fmt, fg)); - // Premultiplied mixing: transparent pixels carry no colour, so no dark fringes. - let mut sm = Smudge::new(strength, finger_px, max_size).with_alpha(alpha_index(&fmt)); - Box::new(move |work, fp| sm.dab(work, fp)) - } - } - } -} - -/// Dodge, Burn, Sponge, Blur, Sharpen and Smudge: each dab edits a working copy in order -/// (`apply_dab_stroke`). Blur, Sharpen and Smudge can sample all layers. -fn dab_cmd(s: &mut Session, p: &Value, cmd: &str) -> Result { - let (stroke, id) = parse_brush(s, p, cmd)?; - let (tool, label) = DabTool::parse(s, cmd, p, &stroke)?; - let all = matches!(tool, DabTool::Focus { .. } | DabTool::Smudge { .. }) && sample_all_layers(p); - // The live stroke the canvas already drew for exactly this commit (`LiveDab::into_commit`): - // take its pixels instead of running every dab again (the hitch on release). - let live = s.live_commit.take().filter(|l| l.matches(s, cmd, p, &stroke.points)); - let from_live = live.is_some(); - let dmg = run_stroke(s, label, id, p, |pre, surf, sel, lock| { - if let Some(l) = live { - *surf = l.surface; - return Ok(l.bounds); - } - let fmt = surf.format(); - let mut effect = tool.effect(fmt, stroke.brush.spacing); - let mut sampler = all.then(|| AllLayers::new(pre, fmt)); - Ok(apply_dab_stroke(surf, &stroke, sel, lock, tool.neighborhood().halo(), |work, fp| match &mut sampler { - Some(all) => all.dab(work, fp, |w, f| effect(w, f)), - None => effect(work, fp), - })) - })?; - let mut r = json!({ "damage": damage_json(dmg) }); - if from_live { - r["fromLive"] = json!(true); - } - Ok(r) -} - -/// Side of the tiles a live sequential-dab stroke keeps its working pixels in ([`WorkTiles`]). -const WORK_TILE: i32 = 128; - -/// The working copy of a live sequential-dab stroke: the target's native channels as `f32`, in -/// tiles copied from the pre-stroke surface the first time a dab writes to them. Unlike a -/// `Surface`, it isn't rounded to the document's depth between dabs, so each dab sees what the -/// commit's working copy (`apply_dab_stroke`) holds and the preview is the commit's pixels. -struct WorkTiles { - ch: usize, - tiles: std::collections::HashMap<(i32, i32), Vec>, -} - -impl WorkTiles { - fn new(ch: usize) -> Self { - Self { ch, tiles: std::collections::HashMap::new() } - } - - fn tile_rect((tx, ty): (i32, i32)) -> Rect { - Rect::new(tx * WORK_TILE, ty * WORK_TILE, tx * WORK_TILE + WORK_TILE, ty * WORK_TILE + WORK_TILE) - } - - /// The tiles `r` overlaps. - fn coords(r: Rect) -> impl Iterator { - let (x0, x1) = (r.x0.div_euclid(WORK_TILE), (r.x1 - 1).div_euclid(WORK_TILE)); - let (y0, y1) = (r.y0.div_euclid(WORK_TILE), (r.y1 - 1).div_euclid(WORK_TILE)); - let empty = r.is_empty(); - (y0..=y1).filter(move |_| !empty).flat_map(move |ty| (x0..=x1).map(move |tx| (tx, ty))) - } - - /// The working pixels of `r`: the pre-stroke pixels `pre` where no dab has written yet. - fn read(&self, pre: &Surface, r: Rect) -> Region { - let mut out = Region::new(r, self.ch); - let n = self.ch; - let mut buf = Vec::new(); - for c in Self::coords(r) { - let tr = Self::tile_rect(c); - let part = r.intersect(&tr); - let w = part.width() as usize * n; - let tile = self.tiles.get(&c); - if tile.is_none() { - pre.read_region_into(part, &mut buf); - } - for y in part.y0..part.y1 { - let src = match tile { - Some(t) => t.get(((y - tr.y0) * WORK_TILE + (part.x0 - tr.x0)) as usize * n..).and_then(|s| s.get(..w)), - None => buf.get((y - part.y0) as usize * w..).and_then(|s| s.get(..w)), - }; - let dst = out.index(part.x0, y) * n; - if let (Some(s), Some(d)) = (src, out.data.get_mut(dst..dst + w)) { - d.copy_from_slice(s); - } - } - } - out - } - - /// Store `region` as the working pixels of its rectangle. - fn write(&mut self, pre: &Surface, region: &Region) { - let (r, n) = (region.rect, self.ch); - for c in Self::coords(r) { - let tr = Self::tile_rect(c); - let tile = self.tiles.entry(c).or_insert_with(|| pre.read_region(tr)); - let part = r.intersect(&tr); - let w = part.width() as usize * n; - for y in part.y0..part.y1 { - let dst = ((y - tr.y0) * WORK_TILE + (part.x0 - tr.x0)) as usize * n; - let src = region.index(part.x0, y) * n; - if let (Some(s), Some(d)) = (region.data.get(src..src + w), tile.get_mut(dst..dst + w)) { - d.copy_from_slice(s); - } - } - } - } -} - -/// A Dodge, Burn, Sponge, Blur, Sharpen or Smudge stroke shown while it is drawn: each new dab -/// runs the tool's effect on a working copy of the target, in order, and the changed area is -/// mixed back over the original at the brush opacity, as `apply_dab_stroke` does on release. -/// The working copy keeps full precision ([`WorkTiles`]), so the pixels are the commit's, and the -/// commit takes them over ([`Session::offer_live_dab`]) instead of running every dab again. -/// Sample All Layers isn't previewed (its composite spans the whole stroke). -pub struct LiveDab { - /// The active document with the stroke so far. - pub doc: std::sync::Arc, - cmd: String, - renderer: photocraft_paint::StrokeRenderer, - done: usize, - effect: DabEffect, - neighborhood: DabNeighborhood, - pre_surf: Surface, - work: WorkTiles, - id: Option, - params: Value, - opacity: f32, - sel: Option, - lock: bool, - bounds: Rect, - /// Show the dabs finishing the stroke would add, so the preview at the last point is the - /// commit. Only for effects that a repeated dab doesn't change (Color Replacement): the - /// tail runs on a scratch copy at every push, so an effect that carries paint from dab to - /// dab (Smudge) would drift. - preview_tail: bool, - /// Where the shown tail is. - tail: Rect, - /// The tool and brush the stroke started with, which its commit must still have: the - /// document revision doesn't cover the session's brush or foreground colour. `None` for - /// strokes whose commit renders itself (Color Replacement). - tool: Option<(DabTool, Box)>, - /// The document and revision the stroke started on, for strokes started from the session's - /// active document ([`Self::with_effect`]); `None` for strokes on a prepared copy. - base: Option<(photocraft_doc::DocId, u64)>, - /// Every point rendered so far, in order. - points: Vec, -} - -impl LiveDab { - /// Start a live stroke of `cmd` from its params; their `points` are rendered. - pub fn begin(s: &Session, cmd: &str, p: &Value) -> Result { - has_pixel_layer(s).map_err(EngineError::Other)?; - if sample_all_layers(p) { - return Err(bad(cmd, "Sample All Layers strokes aren't previewed live")); - } - let (stroke, id) = parse_brush(s, p, cmd)?; - let (tool, _) = DabTool::parse(s, cmd, p, &stroke)?; - let (neighborhood, spacing, brush) = (tool.neighborhood(), stroke.brush.spacing, Box::new(stroke.brush.clone())); - let effect = tool.clone(); - let mut live = Self::with_neighborhood(s, cmd, p, stroke, id, neighborhood, |pre| effect.effect(pre.format(), spacing))?; - live.tool = Some((tool, brush)); - Ok(live) - } - - /// Start a live stroke of `cmd` that runs `effect` (made from the target before the stroke) - /// at each dab of `stroke`, reading `halo` pixels around it. `p` gives the target. - pub(crate) fn with_effect( - s: &Session, - cmd: &str, - p: &Value, - stroke: Stroke, - id: Option, - halo: i32, - effect: impl FnOnce(&Surface) -> DabEffect, - ) -> Result { - Self::with_neighborhood(s, cmd, p, stroke, id, DabNeighborhood::Halo(halo), effect) - } - - fn with_neighborhood( - s: &Session, - cmd: &str, - p: &Value, - stroke: Stroke, - id: Option, - neighborhood: DabNeighborhood, - effect: impl FnOnce(&Surface) -> DabEffect, - ) -> Result { - let st = s.active().ok_or(EngineError::NoDocument)?; - let base = (st.doc.id, st.revision); - let doc = (*st.doc).clone(); - let mut live = Self::with_neighborhood_on(doc, cmd, p, stroke, id, neighborhood, effect)?; - live.base = Some(base); - Ok(live) - } - - /// [`Self::with_effect`] on `doc`, a copy of the active document with what the commit's own - /// edit does first (the Background Eraser's Background turned into a normal layer, say). - pub(crate) fn with_effect_on( - doc: Document, - cmd: &str, - p: &Value, - stroke: Stroke, - id: Option, - halo: i32, - effect: impl FnOnce(&Surface) -> DabEffect, - ) -> Result { - Self::with_neighborhood_on(doc, cmd, p, stroke, id, DabNeighborhood::Halo(halo), effect) - } - - fn with_neighborhood_on( - mut doc: Document, - cmd: &str, - p: &Value, - stroke: Stroke, - id: Option, - neighborhood: DabNeighborhood, - effect: impl FnOnce(&Surface) -> DabEffect, - ) -> Result { - let sel = doc.selection.clone(); - let (surf, lock) = crate::channel_cmds::target_surface(&mut doc, id, p)?; - let pre_surf = surf.clone(); - let effect = effect(&pre_surf); - // `apply_dab_stroke` generates the same dabs (`dabs`: zoom 1, no smoothing scale). - let renderer = photocraft_paint::StrokeRenderer::new(&stroke.brush, None, 1.0).record_dabs(); - let mut live = Self { - doc: std::sync::Arc::new(doc), - cmd: cmd.into(), - renderer, - done: 0, - effect, - neighborhood, - work: WorkTiles::new(pre_surf.channels()), - pre_surf, - id, - params: p.clone(), - opacity: stroke.brush.opacity, - sel, - lock, - bounds: Rect::EMPTY, - preview_tail: false, - tail: Rect::EMPTY, - tool: None, - base: None, - points: Vec::new(), - }; - live.push(&stroke.points)?; - Ok(live) - } - - /// Also show the stroke's finishing dabs (see `preview_tail`). - pub(crate) fn with_tail_preview(mut self) -> Result { - self.preview_tail = true; - self.push(&[])?; - Ok(self) - } - - /// Everything the stroke has touched so far. - pub fn bounds(&self) -> Rect { - self.bounds - } - - /// Run the dabs the renderer has generated since the last call on the working copy; returns - /// the rectangle they changed. - fn run_new_dabs(&mut self) -> Rect { - let ctx = self.renderer.ctx.clone(); - let new: Vec<_> = self.renderer.dabs().get(self.done..).unwrap_or_default().to_vec(); - let mut dmg = Rect::EMPTY; - for (k, d) in new.iter().enumerate() { - let r = ctx.dab_rect(d, false); - if r.is_empty() { - continue; - } - let mut region = self.work.read(&self.pre_surf, self.neighborhood.read_rect(r, d)); - (self.effect)(&mut region, &ctx.footprint(d, self.done + k)); - self.work.write(&self.pre_surf, ®ion.crop(r)); - dmg = dmg.union(&r); - } - self.done += new.len(); - dmg - } - - /// Mix the working pixels of `r` back over the original into the shown document. - fn show(&mut self, r: Rect, work: &Region) -> Result<()> { - let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; - photocraft_paint::retouch::mix_back(surf, &Region::read(&self.pre_surf, r), work, r, self.opacity, self.sel.as_ref(), self.lock); - Ok(()) - } - - /// Render more points; returns the rectangle that changed. - /// Invalid coordinates reject the whole batch without changing the preview. - /// Only the position and pressure of `pts` are used: the commands take no tilt or times. - pub fn push(&mut self, pts: &[StrokePoint]) -> Result { - crate::brush_cmds::check_coords(pts, &self.cmd)?; - let pts: Vec<_> = pts.iter().map(|q| StrokePoint::new(q.x, q.y, q.pressure)).collect(); - self.points.extend_from_slice(&pts); - self.renderer.push(&pts); - let dmg = self.run_new_dabs(); - let ctx = self.renderer.ctx.clone(); - // The finishing dabs, run on a scratch copy of the working pixels. - let mut tail = None; - if self.preview_tail { - let ds = self.renderer.tail_dabs(); - let r = ds.iter().fold(Rect::EMPTY, |a, d| a.union(&ctx.dab_rect(d, false))); - if !r.is_empty() { - let mut region = self.work.read(&self.pre_surf, r.inflate(self.neighborhood.halo())); - for (k, d) in ds.iter().enumerate() { - (self.effect)(&mut region, &ctx.footprint(d, self.done + k)); - } - tail = Some((r, region.crop(r))); - } - } - // Back to the stroke without the previous tail. - let old = std::mem::replace(&mut self.tail, Rect::EMPTY); - let shown = dmg.union(&old); - let tail_rect = tail.as_ref().map_or(Rect::EMPTY, |(r, _)| *r); - if shown.is_empty() && tail_rect.is_empty() { - return Ok(Rect::EMPTY); - } - self.bounds = self.bounds.union(&dmg).union(&tail_rect); - if !shown.is_empty() { - let work = self.work.read(&self.pre_surf, shown); - self.show(shown, &work)?; - } - if let Some((r, work)) = tail { - self.show(r, &work)?; - self.tail = r; - } - Ok(shown.union(&tail_rect)) - } - - /// The finished stroke as its commit would take it over ([`Session::offer_live_dab`]): the - /// finishing dabs are run, and the pixels are the ones the command with the same params and - /// points paints. Only while the brush, tool settings and layer are still the ones the stroke - /// started with: the document revision doesn't cover those. - pub(crate) fn into_commit(mut self, s: &Session) -> Result { - let (Some((tool, brush)), Some(base)) = (self.tool.take(), self.base) else { - return Err(bad(&self.cmd, "this stroke's commit renders itself")); - }; - let (stroke, id) = parse_brush(s, &self.params, &self.cmd)?; - let (now, _) = DabTool::parse(s, &self.cmd, &self.params, &stroke)?; - if now != tool || stroke.brush != *brush || id != self.id { - return Err(bad(&self.cmd, "the brush, tool options or layer changed during the stroke")); - } - self.renderer.finish(); - let dmg = self.run_new_dabs(); - let shown = dmg.union(&self.tail); - if !shown.is_empty() { - let work = self.work.read(&self.pre_surf, shown); - self.show(shown, &work)?; - } - self.bounds = self.bounds.union(&dmg); - let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; - let surface = surf.clone(); - Ok(crate::brush_cmds::LiveCommit { - mixer: None, - cmd: self.cmd, - base, - params: self.params, - seed: None, - points: self.points, - surface, - bounds: self.bounds, - }) - } -} - -/// "Sample All Layers" for Spot Healing, Blur, Sharpen and Smudge (layer pixels only: a mask or -/// channel has nothing to sample from other layers). -fn sample_all_layers(p: &Value) -> bool { - flag(p, "sampleAllLayers", false) && targets_pixels(p) -} - -/// Sample All Layers for the sequential-dab tools: each dab's effect runs on a copy of the visible -/// composite (kept across dabs, so a dab sees the earlier ones), and the result is laid onto the -/// layer under the dab. An empty layer above the image so picks up the blurred or smudged picture, -/// and pixels the effect didn't change look as before (they are what was already visible). -struct AllLayers<'a> { - pre: &'a Document, - fmt: PixelFormat, - visible: Option, -} - -impl<'a> AllLayers<'a> { - fn new(pre: &'a Document, fmt: PixelFormat) -> Self { - Self { pre, fmt, visible: None } - } - - fn dab(&mut self, work: &mut Region, fp: &Footprint, effect: impl FnOnce(&mut Region, &Footprint)) { - let (pre, fmt) = (self.pre, self.fmt); - let visible = self.visible.get_or_insert_with(|| composite_region(pre, None, SampleLayers::All, work.rect, fmt)); - if visible.rect != work.rect || visible.ch != work.ch { - return effect(work, fp); - } - effect(visible, fp); - let r = fp.rect.intersect(&work.rect); - for y in r.y0..r.y1 { - for x in r.x0..r.x1 { - if fp.at(x, y) > 0.0 { - work.px_mut(x, y).copy_from_slice(visible.px(x, y)); - } - } - } - } -} - // --------------------------------------------------------------------------------------------- // Registry // --------------------------------------------------------------------------------------------- @@ -1537,6 +214,22 @@ macro_rules! brush_params { }; } +mod clone; +mod dab; +mod heal; +mod sampling; + +pub use clone::{LiveRetouch, clone_preview, clone_preview_periodic}; +use clone::{blend_param, clamp_samples, clone_stamp}; +pub(crate) use dab::DabEffect; +pub use dab::LiveDab; +use dab::dab_cmd; +#[cfg(test)] +use heal::proximity_source_patch; +use heal::{heal_region, healing_brush, history_brush, history_source, spot_healing}; +use sampling::sample; +pub(crate) use sampling::{SampleLayers, composite_region}; + /// Retouching command specs. pub fn specs() -> Vec { vec![ diff --git a/crates/engine/src/retouch_cmds/clone.rs b/crates/engine/src/retouch_cmds/clone.rs new file mode 100644 index 0000000000..5e5ac27104 --- /dev/null +++ b/crates/engine/src/retouch_cmds/clone.rs @@ -0,0 +1,375 @@ +use super::heal::history_source; +use super::sampling::{SampleLayers, sample, sample_layers}; +use super::*; + +/// Where a clone stroke samples: `offset` / `source` params, else the active Clone Source slot, +/// with the slot's (or the call's) scale, rotation and flips (see `presets::clone_source`). +pub(super) fn clone_mapping(s: &mut Session, p: &Value, stroke: &Stroke, cmd: &str) -> Result { + let f = stroke.points[0]; + crate::presets::clone_source::mapping(s, p, (f.x, f.y), cmd) +} + +/// Source pixels for destination `rect`: a translated read, or a bilinear resample when the +/// clone source is scaled, rotated or flipped. +#[allow(clippy::too_many_arguments)] +pub(super) fn clone_sample( + pre: &Document, + id: Option, + surf: &Surface, + which: SampleLayers, + rect: Rect, + map: &crate::presets::clone_source::Mapping, + wrap: bool, + cmd: &str, +) -> Result { + let read = |rect| { + if wrap { periodic::sample(pre, id, surf, which, rect, cmd) } else { Ok(sample(pre, id, surf, which, rect)) } + }; + if map.is_translation() { + let off = map.offset(); + let src = rect.translate(off.0, off.1); + // `translate` saturates at the i32 edges, so a far source (issue #1111) yields a window + // smaller than `rect`; relabelling that as `rect` would index past the sampled pixels. + if src.width() != rect.width() || src.height() != rect.height() { + return Err(bad(cmd, format!("source offset [{}, {}] is too far: the sample window leaves the coordinate range", off.0, off.1))); + } + let mut r = read(src)?; + r.rect = rect; + return Ok(r); + } + let src = read(map.source_rect(rect))?; + Ok(crate::presets::clone_source::resample(&src, rect, map, alpha_index(&surf.format()))) +} + +/// Read-only clone overlay. Shares the stroke sampler without anchoring a source or editing history. +/// Both destination and transformed source allocations are bounded for interactive use. +pub fn clone_preview(s: &Session, rect: Rect, map: &crate::presets::clone_source::Mapping, sample_layer: &str) -> Result { + clone_preview_periodic(s, rect, map, sample_layer, false) +} + +/// Clone-source overlay with optional source repetition for View > Pattern Preview. +pub fn clone_preview_periodic( + s: &Session, + rect: Rect, + map: &crate::presets::clone_source::Mapping, + sample_layer: &str, + wrap: bool, +) -> Result { + if ![map.source.0, map.source.1, map.anchor.0, map.anchor.1].into_iter().chain(map.m).all(|v| v.is_finite() && v.abs() < 1_000_000.0) { + return Err(bad("clone.preview", "invalid preview mapping")); + } + if ![rect.x0, rect.y0, rect.x1, rect.y1].into_iter().all(|v| v.abs_diff(0) < 1_000_000) + || ![(rect.x0, rect.y0), (rect.x1, rect.y1), (rect.x0, rect.y1), (rect.x1, rect.y0)].into_iter().all(|(x, y)| { + let (x, y) = map.map(f64::from(x), f64::from(y)); + x.is_finite() && y.is_finite() && x.abs() < 1_000_000.0 && y.abs() < 1_000_000.0 + }) + { + return Err(bad("clone.preview", "preview coordinates exceed their budget")); + } + let bounded = |r: Rect| r.width() > 0 && r.height() > 0 && r.width() <= 1024 && r.height() <= 1024; + if !bounded(rect) || !bounded(map.source_rect(rect)) { + return Err(bad("clone.preview", "preview region exceeds its budget")); + } + let d = s.active().ok_or(EngineError::NoDocument)?; + let id = d.active_layer.ok_or_else(|| bad("clone.preview", "no active layer"))?; + let surf = d.doc.layer(id).and_then(|l| l.surface()).ok_or_else(|| bad("clone.preview", "no pixel surface"))?; + let which = sample_layers(&json!({"sampleLayer": sample_layer}), "clone.preview")?; + let region = clone_sample(&d.doc, Some(id), surf, which, rect, map, wrap, "clone.preview")?; + let px = region.data.chunks_exact(surf.format().channels()).map(|p| to_rgba(&surf.format(), p)).collect(); + Ok(photocraft_compose::Buffer { rect, px }) +} + +/// Result JSON shared by Clone Stamp and Healing Brush: the offset used and where the next stroke +/// should sample from if the UI keeps Photoshop's "Aligned" semantics. +pub(super) fn clone_result(dmg: Rect, off: (i32, i32), aligned: bool, p: &Value, stroke: &Stroke) -> Value { + let f = stroke.points[0]; + let source = point(p, "source").unwrap_or((f.x + off.0 as f64, f.y + off.1 as f64)); + // Aligned: the offset persists (next stroke samples at its own first point + offset). + // Non-aligned: every stroke restarts at the original source point. + json!({ "damage": damage_json(dmg), "offset": [off.0, off.1], "aligned": aligned, "nextSource": if aligned { Value::Null } else { json!([source.0, source.1]) } }) +} + +pub(super) fn blend_param(p: &Value, cmd: &str) -> Result { + match p.get("mode").and_then(Value::as_str) { + None => Ok(BlendMode::Normal), + Some(m) => blend_from_str(m).ok_or_else(|| bad(cmd, format!("unknown blend mode `{m}`"))), + } +} + +/// Clamp results into the storable range: integer depths to 0..1, float depths only below at 0; +/// alpha always 0..1. +pub(super) fn clamp_samples(fmt: &PixelFormat, data: &mut [f32]) { + let n = fmt.channels(); + let a = alpha_index(fmt); + let float = fmt.sample == SampleType::F32; + for px in data.chunks_exact_mut(n) { + for (c, v) in px.iter_mut().enumerate() { + *v = if Some(c) == a || !float { v.clamp(0.0, 1.0) } else { v.max(0.0) }; + } + } +} + +// --------------------------------------------------------------------------------------------- +// Clone Stamp / Healing Brush / History Brush +// --------------------------------------------------------------------------------------------- + +pub(super) fn clone_stamp(s: &mut Session, p: &Value) -> Result { + const CMD: &str = "paint.cloneStamp"; + let (stroke, id) = parse_brush(s, p, CMD)?; + reject_huge_stroke(CMD, &stroke)?; + let which = sample_layers(p, CMD)?; + let mode = blend_param(p, CMD)?; + let aligned = flag(p, "aligned", true); + // The live stroke the canvas already drew for exactly this commit: take its pixels instead of + // rendering the stroke again (the hitch on release). + let live = s.live_commit.take().filter(|l| l.matches(s, CMD, p, &stroke.points)); + let from_live = live.is_some(); + let map = clone_mapping(s, p, &stroke, CMD)?; + let off = map.offset(); + let dmg = + run_stroke(s, "Clone Stamp", id, p, |pre, surf, sel, lock| { + if let Some(l) = live { + *surf = l.surface; + return Ok(l.bounds); + } + if flag(p, "wrap", false) { + let mut renderer = photocraft_paint::StrokeRenderer::new(&stroke.brush, None, 1.0); + renderer.push(&stroke.points); + renderer.finish(); + let before = surf.clone(); + let bounds = renderer.bounds(); + return periodic::Composite { target: surf, before: &before, tile: pre.bounds(), opacity: stroke.brush.opacity, selection: sel, lock, mode } + .draw(bounds, &stroke.brush, &stroke.points, |r| clone_sample(pre, id, &before, which, r, &map, true, CMD)); + } + let (bounds, cov) = coverage(CMD, &stroke)?; + // Samples come from the pre-stroke state, so pixels painted earlier in the stroke are never re-cloned. + let paint = clone_sample(pre, id, surf, which, bounds, &map, flag(p, "wrap", false), CMD)?; + Ok(apply_coverage(surf, bounds, &cov, stroke.brush.opacity, sel, lock, &paint, mode)) + })?; + let mut r = clone_result(dmg, off, aligned, p, &stroke); + if from_live { + r["fromLive"] = json!(true); + } + Ok(r) +} + +/// What a live retouching stroke paints through its coverage. +enum LivePaint { + /// The clone source (Clone Stamp; the Healing Brush shows its texture while drawing and + /// blends it on release). + Clone { which: SampleLayers, map: crate::presets::clone_source::Mapping, cmd: &'static str }, + /// The History Brush's history state. + History(Surface), + /// The Pattern Stamp's pattern, tiled from its aligned placement (Impressionist strokes are + /// laid dab by dab: `pattern_stamp::live_impressionist`). + Pattern { tile: std::sync::Arc, place: photocraft_compose::pattern::Placement }, +} + +/// A retouching stroke shown while it is drawn: the dabs so far composited onto a copy of the +/// active document as the command with the same params commits them (the same coverage, +/// sampling and blend): Clone Stamp, History Brush, and the Healing Brush's texture. +pub struct LiveRetouch { + /// The active document with the stroke so far. + pub doc: std::sync::Arc, + cmd: String, + renderer: photocraft_paint::StrokeRenderer, + pre: std::sync::Arc, + pre_surf: Surface, + id: Option, + params: Value, + paint: LivePaint, + mode: BlendMode, + opacity: f32, + sel: Option, + lock: bool, + /// Where the doc shows what finishing the stroke now would add (a lone first dab, the + /// smoothing catch-up), redrawn on every step as the Brush's live stroke does. + tail: Rect, + /// The document and revision the stroke started on. + base: (photocraft_doc::DocId, u64), + brush: Box, + /// Every point rendered so far, in order. + points: Vec, +} + +impl LiveRetouch { + /// Start a live stroke of `cmd` (`paint.cloneStamp`, `paint.healingBrush`, + /// `paint.historyBrush` or an aligned, non-Impressionist `paint.patternStamp`) from its + /// params; their `points` are rendered. + pub fn begin(s: &Session, cmd: &str, p: &Value) -> Result { + has_pixel_layer(s).map_err(EngineError::Other)?; + if cmd == "paint.patternStamp" { + let st = pattern_stamp::Setup::parse(s, p)?; + if st.impressionist { + return Err(bad(cmd, "Impressionist strokes are laid dab by dab (pattern_stamp::live_impressionist)")); + } + let paint = LivePaint::Pattern { place: st.placement(), tile: st.tile }; + return Self::with_paint(s, cmd, p, st.stroke, st.id, paint, st.mode); + } + let (stroke, id) = parse_brush(s, p, cmd)?; + let mode = blend_param(p, cmd)?; + let paint = match cmd { + "paint.cloneStamp" | "paint.healingBrush" => { + let f = *stroke.points.first().ok_or_else(|| bad(cmd, "`points` is empty"))?; + LivePaint::Clone { + which: sample_layers(p, cmd)?, + map: crate::presets::clone_source::resolve(s, p, (f.x, f.y), cmd)?.0, + cmd: if cmd == "paint.healingBrush" { "paint.healingBrush" } else { "paint.cloneStamp" }, + } + } + "paint.historyBrush" => LivePaint::History(history_source(s, id, p, cmd)?), + other => return Err(bad(other, "live retouching strokes are the Clone Stamp, Healing Brush, History Brush and Pattern Stamp")), + }; + Self::with_paint(s, cmd, p, stroke, id, paint, mode) + } + + /// Start a live stroke of `cmd` painting `paint` through `stroke`'s coverage. + fn with_paint(s: &Session, cmd: &str, p: &Value, stroke: Stroke, id: Option, paint: LivePaint, mode: BlendMode) -> Result { + let st = s.active().ok_or(EngineError::NoDocument)?; + let (pre, base) = (st.doc.clone(), (st.doc.id, st.revision)); + let mut doc = (*pre).clone(); + let sel = doc.selection.clone(); + let (surf, lock) = crate::channel_cmds::target_surface(&mut doc, id, p)?; + let pre_surf = surf.clone(); + // As `stroke_coverage`, which the commit uses. + let renderer = photocraft_paint::StrokeRenderer::new(&stroke.brush, None, 1.0); + let opacity = stroke.brush.opacity; + let brush = Box::new(stroke.brush.clone()); + let paint = match paint { + LivePaint::History(src) if src.format() != pre_surf.format() => LivePaint::History(src.convert(pre_surf.format())), + other => other, + }; + let mut live = Self { + doc: std::sync::Arc::new(doc), + cmd: cmd.into(), + renderer, + pre, + pre_surf, + id, + params: p.clone(), + paint, + mode, + opacity, + sel, + lock, + tail: Rect::EMPTY, + base, + brush, + points: Vec::new(), + }; + live.push(&stroke.points)?; + Ok(live) + } + + /// Everything the stroke has touched so far. + pub fn bounds(&self) -> Rect { + let bounds = self.renderer.bounds().union(&self.tail); + if self.cmd == "paint.cloneStamp" && flag(&self.params, "wrap", false) { + periodic::project(bounds, self.pre.bounds()).into_iter().fold(Rect::EMPTY, |a, b| a.union(&b)) + } else { + bounds + } + } + + /// Every point rendered so far, in order. + pub fn points(&self) -> &[StrokePoint] { + &self.points + } + + /// The finished stroke as its commit would take it over ([`Session::offer_live_retouch`]). + /// Only the Clone Stamp and History Brush draw what they commit (the Healing Brush blends on + /// release), and only while the brush, layer and clone source are still the ones the stroke + /// started with: the document revision doesn't cover those. + pub(crate) fn into_commit(mut self, s: &Session) -> Result { + let (stroke, id) = parse_brush(s, &self.params, &self.cmd)?; + let source_unchanged = match &self.paint { + LivePaint::Clone { map, .. } if self.cmd == "paint.cloneStamp" => { + let f = stroke.points.first().ok_or_else(|| bad(&self.cmd, "`points` is empty"))?; + crate::presets::clone_source::resolve(s, &self.params, (f.x, f.y), &self.cmd)?.0 == *map + } + LivePaint::History(_) if self.cmd == "paint.historyBrush" => true, + _ => return Err(bad(&self.cmd, "only Clone Stamp and History Brush strokes commit their live pixels")), + }; + if !source_unchanged || *self.brush != stroke.brush || id != self.id { + return Err(bad(&self.cmd, "the brush, layer or clone source changed during the stroke")); + } + let bounds = self.bounds(); + let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; + let surface = surf.clone(); + Ok(crate::brush_cmds::LiveCommit { mixer: None, cmd: self.cmd, base: self.base, params: self.params, seed: None, points: self.points, surface, bounds }) + } + + /// Render more points; returns the rectangle that changed. Each changed area is redrawn from + /// the pre-stroke pixels with the stroke's coverage so far, so overlapping dabs build up as in + /// the commit, and pixels painted earlier in the stroke are never re-cloned. What finishing + /// the stroke now would add is drawn too (a lone first dab shows on the press), and redrawn on + /// every step. + /// Invalid coordinates reject the whole batch without changing the preview. + /// Only the position and pressure of `pts` are used: the commands take no tilt or times. + pub fn push(&mut self, pts: &[StrokePoint]) -> Result { + crate::brush_cmds::check_coords(pts, &self.cmd)?; + let pts: Vec<_> = pts.iter().map(|q| StrokePoint::new(q.x, q.y, q.pressure)).collect(); + self.points.extend_from_slice(&pts); + self.renderer.push(&pts); + if self.cmd == "paint.cloneStamp" && flag(&self.params, "wrap", false) { + return self.push_periodic(); + } + let old = std::mem::replace(&mut self.tail, Rect::EMPTY); + let mut dmg = Rect::EMPTY; + if !old.is_empty() { + // Back to the stroke without the previous tail. + let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; + surf.write_region(old, &self.pre_surf.read_region(old)); + self.renderer.mark_dirty(old); + dmg = old; + } + let r = self.renderer.take_dirty_rect(); + let cov = self.renderer.coverage_in(r); + dmg = dmg.union(&self.composite(r, &cov)?); + if let Some(mut tail) = self.renderer.tail_preview() { + let r = tail.take_dirty_rect(); + let cov = tail.coverage_in(r); + self.tail = self.composite(r, &cov)?; + dmg = dmg.union(&self.tail); + } + Ok(dmg) + } + + fn push_periodic(&mut self) -> Result { + let mut dirty = self.renderer.take_dirty_rect().union(&self.tail); + let mut tail = self.renderer.tail_preview(); + self.tail = tail.as_mut().map_or(Rect::EMPTY, |t| t.take_dirty_rect()); + dirty = dirty.union(&self.tail); + let LivePaint::Clone { which, map, cmd } = &self.paint else { + return Err(bad(&self.cmd, "periodic painting requires a clone source")); + }; + let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; + periodic::Composite { + target: surf, + before: &self.pre_surf, + tile: self.pre.bounds(), + opacity: self.opacity, + selection: self.sel.as_ref(), + lock: self.lock, + mode: self.mode, + } + .draw(dirty, &self.brush, &self.points, |r| clone_sample(&self.pre, self.id, &self.pre_surf, *which, r, map, true, cmd)) + } + + /// Redraw `r` from the pre-stroke pixels with the clone source through coverage `cov`. + fn composite(&mut self, r: Rect, cov: &[f32]) -> Result { + if r.is_empty() { + return Ok(r); + } + let paint = match &self.paint { + LivePaint::Clone { which, map, cmd } => { + clone_sample(&self.pre, self.id, &self.pre_surf, *which, r, map, *cmd == "paint.cloneStamp" && flag(&self.params, "wrap", false), cmd)? + } + LivePaint::History(src) => Region::read(src, r), + LivePaint::Pattern { tile, place } => pattern_stamp::paint_region(tile, place, &self.pre_surf.format(), r), + }; + let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; + surf.write_region(r, &self.pre_surf.read_region(r)); + Ok(apply_coverage(surf, r, cov, self.opacity, self.sel.as_ref(), self.lock, &paint, self.mode).union(&r)) + } +} diff --git a/crates/engine/src/retouch_cmds/dab.rs b/crates/engine/src/retouch_cmds/dab.rs new file mode 100644 index 0000000000..818c27a6f1 --- /dev/null +++ b/crates/engine/src/retouch_cmds/dab.rs @@ -0,0 +1,625 @@ +use super::sampling::{AllLayers, sample_all_layers}; +use super::*; + +// --------------------------------------------------------------------------------------------- +// Toning (Dodge / Burn / Sponge) and focus (Blur / Sharpen), Smudge +// --------------------------------------------------------------------------------------------- + +/// Per-dab strength such that one pass over a point totals roughly `e`, whatever the spacing +/// (about `1/spacing` dabs overlap each point on the stroke's centre line). +#[inline] +fn per_dab(e: f32, spacing: f32) -> f32 { + 1.0 - (1.0 - e.clamp(0.0, 1.0)).powf(spacing.clamp(0.01, 1.0)) +} + +/// Apply an RGB colour transform `f(rgb, strength)` to each covered pixel of a dab (through straight +/// RGBA, so it works for Gray/CMYK/Lab too; untouched pixels are never round-tripped). +fn color_dab(fmt: &PixelFormat, work: &mut Region, fp: &Footprint, strength: f32, spacing: f32, f: impl Fn([f32; 3], f32) -> [f32; 3]) { + let r = fp.rect.intersect(&work.rect); + let n = fmt.channels(); + let mut enc = [0.0f32; 8]; + for y in r.y0..r.y1 { + for x in r.x0..r.x1 { + let k = per_dab(fp.at(x, y) * strength, spacing); + if k <= 0.0 { + continue; + } + let px = work.px_mut(x, y); + let rgba = to_rgba(fmt, px); + let o = f([rgba[0], rgba[1], rgba[2]], k); + from_rgba_into(fmt, [o[0], o[1], o[2], rgba[3]], &mut enc); + let a = alpha_index(fmt); + for c in 0..n { + if Some(c) != a { + px[c] = enc[c]; + } + } + } + } +} + +/// Dodge and Burn: a stroke tones each pixel once, from its original colour, mixed in by the +/// stroke's coverage there (`toned` is the full-exposure colour). Dabs build up coverage as in a +/// brush stroke, capped at full coverage, so Exposure caps a stroke the way Opacity caps a paint +/// stroke: overlapping dabs and passes within one stroke don't compound the tone curve, and a +/// new stroke builds on the last. +fn tone_stroke(fmt: PixelFormat, toned: impl Fn([f32; 3]) -> [f32; 3] + 'static) -> DabEffect { + /// Side of the cells that remember, per touched pixel, the coverage applied so far and the + /// original pixel (copied from the working pixels the first time a dab reaches it). Dense + /// cells rather than a map per pixel: a map lookup per covered pixel made long strokes with + /// large brushes over ten times slower. + const CELL: i32 = 64; + struct Cell { + cov: Vec, + orig: Vec, + } + let mut cells: std::collections::HashMap<(i32, i32), Cell> = std::collections::HashMap::new(); + let a = alpha_index(&fmt); + let n = fmt.channels(); + Box::new(move |work, fp| { + let r = fp.rect.intersect(&work.rect); + if r.is_empty() { + return; + } + let mut enc = [0.0f32; 8]; + for cy in r.y0.div_euclid(CELL)..=(r.y1 - 1).div_euclid(CELL) { + for cx in r.x0.div_euclid(CELL)..=(r.x1 - 1).div_euclid(CELL) { + let cr = Rect::new(cx * CELL, cy * CELL, cx * CELL + CELL, cy * CELL + CELL); + let part = r.intersect(&cr); + if part.is_empty() { + continue; + } + let len = (CELL * CELL) as usize; + // Coverage -1: not touched yet, so `orig` isn't filled in for that pixel. + let cell = cells.entry((cx, cy)).or_insert_with(|| Cell { cov: vec![-1.0; len], orig: vec![0.0; len * n] }); + for y in part.y0..part.y1 { + for x in part.x0..part.x1 { + let k = fp.at(x, y).clamp(0.0, 1.0); + if k <= 0.0 { + continue; + } + let i = ((y - cr.y0) * CELL + (x - cr.x0)) as usize; + let (Some(prev), Some(src)) = (cell.cov.get_mut(i), cell.orig.get_mut(i * n..i * n + n)) else { continue }; + if *prev < 0.0 { + // First touch: the working pixel is still the original. + src.copy_from_slice(work.px(x, y)); + *prev = 0.0; + } + // Dabs build up the stroke's coverage as a brush stroke's do (flow), up + // to full coverage; the tone is applied once, from the original. + let k = *prev + k * (1.0 - *prev); + if k <= *prev { + continue; + } + *prev = k; + let o = to_rgba(&fmt, src); + let t = toned([o[0], o[1], o[2]]); + let m = [o[0] + (t[0] - o[0]) * k, o[1] + (t[1] - o[1]) * k, o[2] + (t[2] - o[2]) * k]; + from_rgba_into(&fmt, [m[0], m[1], m[2], o[3]], &mut enc); + let px = work.px_mut(x, y); + for c in 0..n { + if Some(c) != a { + px[c] = enc[c]; + } + } + } + } + } + } + }) +} + +fn tone_range(p: &Value, cmd: &str) -> Result { + match string(p, "range", "midtones") { + "shadows" => Ok(ToneRange::Shadows), + "midtones" => Ok(ToneRange::Midtones), + "highlights" => Ok(ToneRange::Highlights), + o => Err(bad(cmd, format!("unknown range `{o}` (shadows|midtones|highlights)"))), + } +} + +/// A sequential-dab tool's settings (Dodge, Burn, Sponge, Blur, Sharpen, Smudge), parsed once and +/// turned into its per-dab effect, for the commit and the live preview alike. +#[derive(Clone, Debug, PartialEq)] +enum DabTool { + Tone { burn: bool, range: ToneRange, exposure: f32, protect: bool }, + Sponge { saturate: bool, vibrance: bool }, + Focus { sharpen: bool, strength: f32, protect: bool, sigma: f32 }, + Smudge { strength: f32, finger: Option<[f32; 4]>, max_size: f32 }, +} + +/// One dab's edit of the working copy. +pub(crate) type DabEffect = Box; + +/// Pixels an effect reads beyond the dab it writes. Smudge's pickup is a fixed-size square, +/// independent of pressure, roundness and the dab's current angle. +#[derive(Clone, Copy)] +enum DabNeighborhood { + Halo(i32), + Smudge(i32), +} + +impl DabNeighborhood { + fn halo(self) -> i32 { + match self { + Self::Halo(radius) | Self::Smudge(radius) => radius, + } + } + + fn read_rect(self, rect: Rect, dab: &photocraft_paint::Dab) -> Rect { + match self { + Self::Halo(radius) => rect.inflate(radius), + Self::Smudge(radius) => { + let (x, y) = (dab.center.x.floor() as i32, dab.center.y.floor() as i32); + rect.union(&Rect::new( + x.saturating_sub(radius), + y.saturating_sub(radius), + x.saturating_add(radius).saturating_add(1), + y.saturating_add(radius).saturating_add(1), + )) + } + } + } +} + +impl DabTool { + /// The tool for `cmd` and its history label. + fn parse(s: &Session, cmd: &str, p: &Value, stroke: &Stroke) -> Result<(Self, &'static str)> { + Ok(match cmd { + "paint.dodge" | "paint.burn" => { + let burn = cmd == "paint.burn"; + let tool = Self::Tone { + burn, + range: tone_range(p, cmd)?, + exposure: num(p, "exposure", 50.0).clamp(1.0, 100.0) / 100.0, + protect: flag(p, "protectTones", true), + }; + (tool, if burn { "Burn Tool" } else { "Dodge Tool" }) + } + "paint.sponge" => { + let saturate = match string(p, "mode", "desaturate") { + "desaturate" => false, + "saturate" => true, + o => return Err(bad(cmd, format!("unknown mode `{o}` (desaturate|saturate)"))), + }; + (Self::Sponge { saturate, vibrance: flag(p, "vibrance", true) }, "Sponge Tool") + } + "paint.blur" | "paint.sharpen" => { + let sharpen = cmd == "paint.sharpen"; + let tool = Self::Focus { + sharpen, + strength: num(p, "strength", 50.0).clamp(1.0, 100.0) / 100.0, + protect: flag(p, "protectDetail", true), + sigma: (stroke.brush.size * 0.03).clamp(1.0, 4.0), + }; + (tool, if sharpen { "Sharpen Tool" } else { "Blur Tool" }) + } + "paint.smudge" => { + let finger = flag(p, "fingerPainting", false).then_some(s.tools.foreground); + (Self::Smudge { strength: num(p, "strength", 50.0).clamp(1.0, 100.0) / 100.0, finger, max_size: stroke.brush.size }, "Smudge Tool") + } + o => return Err(bad(o, "not a sequential-dab tool")), + }) + } + + /// How far around each dab the effect reads (Blur and Sharpen read a neighbourhood). + fn neighborhood(&self) -> DabNeighborhood { + match self { + Self::Focus { sigma, .. } => DabNeighborhood::Halo((sigma * 3.0).ceil() as i32 + 1), + Self::Smudge { max_size, .. } => DabNeighborhood::Smudge(Smudge::sampling_radius(*max_size)), + _ => DabNeighborhood::Halo(0), + } + } + + /// The per-dab effect on a working copy in `fmt`, for a brush of `spacing`. + fn effect(&self, fmt: PixelFormat, spacing: f32) -> DabEffect { + let sp = spacing; + match self.clone() { + Self::Tone { burn, range, exposure, protect } => tone_stroke(fmt, move |c| dodge_burn(c, exposure, range, burn, protect)), + // Flow is already in each dab's coverage; a full-flow pass moves colours half-way. + Self::Sponge { saturate, vibrance } => Box::new(move |work, fp| color_dab(&fmt, work, fp, 0.5, sp, |c, k| sponge(c, k, saturate, vibrance))), + Self::Focus { sharpen, strength, protect, sigma } => { + let a = alpha_index(&fmt); + Box::new(move |work, fp| { + let r = fp.rect.intersect(&work.rect); + if r.is_empty() { + return; + } + let (w, h, n) = (work.width(), work.height(), work.ch); + let win = ((r.x0 - work.rect.x0) as usize, (r.y0 - work.rect.y0) as usize, (r.x1 - work.rect.x0) as usize, (r.y1 - work.rect.y0) as usize); + let out = if sharpen { local_sharpen(&work.data, w, h, n, a, win, 1.0, protect) } else { local_blur(&work.data, w, h, n, a, win, sigma) }; + let rw = r.width() as usize; + // Row-parallel, like the blur itself (#5426). + let (wx0, wy0) = (work.rect.x0, work.rect.y0); + work.data.par_chunks_mut(w * n).enumerate().for_each(|(row, line)| { + let y = wy0 + row as i32; + if y < r.y0 || y >= r.y1 { + return; + } + for x in r.x0..r.x1 { + let k = per_dab(fp.at(x, y) * strength, sp); + if k <= 0.0 { + continue; + } + let o = ((y - r.y0) as usize * rw + (x - r.x0) as usize) * n; + let i = (x - wx0) as usize * n; + let px = &mut line[i..i + n]; + for c in 0..n { + px[c] += (out[o + c] - px[c]) * k; + } + } + }); + }) + } + Self::Smudge { strength, finger, max_size } => { + let finger_px = finger.map(|fg| photocraft_raster::from_rgba(&fmt, fg)); + // Premultiplied mixing: transparent pixels carry no colour, so no dark fringes. + let mut sm = Smudge::new(strength, finger_px, max_size).with_alpha(alpha_index(&fmt)); + Box::new(move |work, fp| sm.dab(work, fp)) + } + } + } +} + +/// Dodge, Burn, Sponge, Blur, Sharpen and Smudge: each dab edits a working copy in order +/// (`apply_dab_stroke`). Blur, Sharpen and Smudge can sample all layers. +pub(super) fn dab_cmd(s: &mut Session, p: &Value, cmd: &str) -> Result { + let (stroke, id) = parse_brush(s, p, cmd)?; + let (tool, label) = DabTool::parse(s, cmd, p, &stroke)?; + let all = matches!(tool, DabTool::Focus { .. } | DabTool::Smudge { .. }) && sample_all_layers(p); + // The live stroke the canvas already drew for exactly this commit (`LiveDab::into_commit`): + // take its pixels instead of running every dab again (the hitch on release). + let live = s.live_commit.take().filter(|l| l.matches(s, cmd, p, &stroke.points)); + let from_live = live.is_some(); + let dmg = run_stroke(s, label, id, p, |pre, surf, sel, lock| { + if let Some(l) = live { + *surf = l.surface; + return Ok(l.bounds); + } + let fmt = surf.format(); + let mut effect = tool.effect(fmt, stroke.brush.spacing); + let mut sampler = all.then(|| AllLayers::new(pre, fmt)); + Ok(apply_dab_stroke(surf, &stroke, sel, lock, tool.neighborhood().halo(), |work, fp| match &mut sampler { + Some(all) => all.dab(work, fp, |w, f| effect(w, f)), + None => effect(work, fp), + })) + })?; + let mut r = json!({ "damage": damage_json(dmg) }); + if from_live { + r["fromLive"] = json!(true); + } + Ok(r) +} + +/// Side of the tiles a live sequential-dab stroke keeps its working pixels in ([`WorkTiles`]). +const WORK_TILE: i32 = 128; + +/// The working copy of a live sequential-dab stroke: the target's native channels as `f32`, in +/// tiles copied from the pre-stroke surface the first time a dab writes to them. Unlike a +/// `Surface`, it isn't rounded to the document's depth between dabs, so each dab sees what the +/// commit's working copy (`apply_dab_stroke`) holds and the preview is the commit's pixels. +struct WorkTiles { + ch: usize, + tiles: std::collections::HashMap<(i32, i32), Vec>, +} + +impl WorkTiles { + fn new(ch: usize) -> Self { + Self { ch, tiles: std::collections::HashMap::new() } + } + + fn tile_rect((tx, ty): (i32, i32)) -> Rect { + Rect::new(tx * WORK_TILE, ty * WORK_TILE, tx * WORK_TILE + WORK_TILE, ty * WORK_TILE + WORK_TILE) + } + + /// The tiles `r` overlaps. + fn coords(r: Rect) -> impl Iterator { + let (x0, x1) = (r.x0.div_euclid(WORK_TILE), (r.x1 - 1).div_euclid(WORK_TILE)); + let (y0, y1) = (r.y0.div_euclid(WORK_TILE), (r.y1 - 1).div_euclid(WORK_TILE)); + let empty = r.is_empty(); + (y0..=y1).filter(move |_| !empty).flat_map(move |ty| (x0..=x1).map(move |tx| (tx, ty))) + } + + /// The working pixels of `r`: the pre-stroke pixels `pre` where no dab has written yet. + fn read(&self, pre: &Surface, r: Rect) -> Region { + let mut out = Region::new(r, self.ch); + let n = self.ch; + let mut buf = Vec::new(); + for c in Self::coords(r) { + let tr = Self::tile_rect(c); + let part = r.intersect(&tr); + let w = part.width() as usize * n; + let tile = self.tiles.get(&c); + if tile.is_none() { + pre.read_region_into(part, &mut buf); + } + for y in part.y0..part.y1 { + let src = match tile { + Some(t) => t.get(((y - tr.y0) * WORK_TILE + (part.x0 - tr.x0)) as usize * n..).and_then(|s| s.get(..w)), + None => buf.get((y - part.y0) as usize * w..).and_then(|s| s.get(..w)), + }; + let dst = out.index(part.x0, y) * n; + if let (Some(s), Some(d)) = (src, out.data.get_mut(dst..dst + w)) { + d.copy_from_slice(s); + } + } + } + out + } + + /// Store `region` as the working pixels of its rectangle. + fn write(&mut self, pre: &Surface, region: &Region) { + let (r, n) = (region.rect, self.ch); + for c in Self::coords(r) { + let tr = Self::tile_rect(c); + let tile = self.tiles.entry(c).or_insert_with(|| pre.read_region(tr)); + let part = r.intersect(&tr); + let w = part.width() as usize * n; + for y in part.y0..part.y1 { + let dst = ((y - tr.y0) * WORK_TILE + (part.x0 - tr.x0)) as usize * n; + let src = region.index(part.x0, y) * n; + if let (Some(s), Some(d)) = (region.data.get(src..src + w), tile.get_mut(dst..dst + w)) { + d.copy_from_slice(s); + } + } + } + } +} + +/// A Dodge, Burn, Sponge, Blur, Sharpen or Smudge stroke shown while it is drawn: each new dab +/// runs the tool's effect on a working copy of the target, in order, and the changed area is +/// mixed back over the original at the brush opacity, as `apply_dab_stroke` does on release. +/// The working copy keeps full precision ([`WorkTiles`]), so the pixels are the commit's, and the +/// commit takes them over ([`Session::offer_live_dab`]) instead of running every dab again. +/// Sample All Layers isn't previewed (its composite spans the whole stroke). +pub struct LiveDab { + /// The active document with the stroke so far. + pub doc: std::sync::Arc, + cmd: String, + renderer: photocraft_paint::StrokeRenderer, + done: usize, + effect: DabEffect, + neighborhood: DabNeighborhood, + pre_surf: Surface, + work: WorkTiles, + id: Option, + params: Value, + opacity: f32, + sel: Option, + lock: bool, + bounds: Rect, + /// Show the dabs finishing the stroke would add, so the preview at the last point is the + /// commit. Only for effects that a repeated dab doesn't change (Color Replacement): the + /// tail runs on a scratch copy at every push, so an effect that carries paint from dab to + /// dab (Smudge) would drift. + preview_tail: bool, + /// Where the shown tail is. + tail: Rect, + /// The tool and brush the stroke started with, which its commit must still have: the + /// document revision doesn't cover the session's brush or foreground colour. `None` for + /// strokes whose commit renders itself (Color Replacement). + tool: Option<(DabTool, Box)>, + /// The document and revision the stroke started on, for strokes started from the session's + /// active document ([`Self::with_effect`]); `None` for strokes on a prepared copy. + base: Option<(photocraft_doc::DocId, u64)>, + /// Every point rendered so far, in order. + points: Vec, +} + +impl LiveDab { + /// Start a live stroke of `cmd` from its params; their `points` are rendered. + pub fn begin(s: &Session, cmd: &str, p: &Value) -> Result { + has_pixel_layer(s).map_err(EngineError::Other)?; + if sample_all_layers(p) { + return Err(bad(cmd, "Sample All Layers strokes aren't previewed live")); + } + let (stroke, id) = parse_brush(s, p, cmd)?; + let (tool, _) = DabTool::parse(s, cmd, p, &stroke)?; + let (neighborhood, spacing, brush) = (tool.neighborhood(), stroke.brush.spacing, Box::new(stroke.brush.clone())); + let effect = tool.clone(); + let mut live = Self::with_neighborhood(s, cmd, p, stroke, id, neighborhood, |pre| effect.effect(pre.format(), spacing))?; + live.tool = Some((tool, brush)); + Ok(live) + } + + /// Start a live stroke of `cmd` that runs `effect` (made from the target before the stroke) + /// at each dab of `stroke`, reading `halo` pixels around it. `p` gives the target. + pub(crate) fn with_effect( + s: &Session, + cmd: &str, + p: &Value, + stroke: Stroke, + id: Option, + halo: i32, + effect: impl FnOnce(&Surface) -> DabEffect, + ) -> Result { + Self::with_neighborhood(s, cmd, p, stroke, id, DabNeighborhood::Halo(halo), effect) + } + + fn with_neighborhood( + s: &Session, + cmd: &str, + p: &Value, + stroke: Stroke, + id: Option, + neighborhood: DabNeighborhood, + effect: impl FnOnce(&Surface) -> DabEffect, + ) -> Result { + let st = s.active().ok_or(EngineError::NoDocument)?; + let base = (st.doc.id, st.revision); + let doc = (*st.doc).clone(); + let mut live = Self::with_neighborhood_on(doc, cmd, p, stroke, id, neighborhood, effect)?; + live.base = Some(base); + Ok(live) + } + + /// [`Self::with_effect`] on `doc`, a copy of the active document with what the commit's own + /// edit does first (the Background Eraser's Background turned into a normal layer, say). + pub(crate) fn with_effect_on( + doc: Document, + cmd: &str, + p: &Value, + stroke: Stroke, + id: Option, + halo: i32, + effect: impl FnOnce(&Surface) -> DabEffect, + ) -> Result { + Self::with_neighborhood_on(doc, cmd, p, stroke, id, DabNeighborhood::Halo(halo), effect) + } + + fn with_neighborhood_on( + mut doc: Document, + cmd: &str, + p: &Value, + stroke: Stroke, + id: Option, + neighborhood: DabNeighborhood, + effect: impl FnOnce(&Surface) -> DabEffect, + ) -> Result { + let sel = doc.selection.clone(); + let (surf, lock) = crate::channel_cmds::target_surface(&mut doc, id, p)?; + let pre_surf = surf.clone(); + let effect = effect(&pre_surf); + // `apply_dab_stroke` generates the same dabs (`dabs`: zoom 1, no smoothing scale). + let renderer = photocraft_paint::StrokeRenderer::new(&stroke.brush, None, 1.0).record_dabs(); + let mut live = Self { + doc: std::sync::Arc::new(doc), + cmd: cmd.into(), + renderer, + done: 0, + effect, + neighborhood, + work: WorkTiles::new(pre_surf.channels()), + pre_surf, + id, + params: p.clone(), + opacity: stroke.brush.opacity, + sel, + lock, + bounds: Rect::EMPTY, + preview_tail: false, + tail: Rect::EMPTY, + tool: None, + base: None, + points: Vec::new(), + }; + live.push(&stroke.points)?; + Ok(live) + } + + /// Also show the stroke's finishing dabs (see `preview_tail`). + pub(crate) fn with_tail_preview(mut self) -> Result { + self.preview_tail = true; + self.push(&[])?; + Ok(self) + } + + /// Everything the stroke has touched so far. + pub fn bounds(&self) -> Rect { + self.bounds + } + + /// Run the dabs the renderer has generated since the last call on the working copy; returns + /// the rectangle they changed. + fn run_new_dabs(&mut self) -> Rect { + let ctx = self.renderer.ctx.clone(); + let new: Vec<_> = self.renderer.dabs().get(self.done..).unwrap_or_default().to_vec(); + let mut dmg = Rect::EMPTY; + for (k, d) in new.iter().enumerate() { + let r = ctx.dab_rect(d, false); + if r.is_empty() { + continue; + } + let mut region = self.work.read(&self.pre_surf, self.neighborhood.read_rect(r, d)); + (self.effect)(&mut region, &ctx.footprint(d, self.done + k)); + self.work.write(&self.pre_surf, ®ion.crop(r)); + dmg = dmg.union(&r); + } + self.done += new.len(); + dmg + } + + /// Mix the working pixels of `r` back over the original into the shown document. + fn show(&mut self, r: Rect, work: &Region) -> Result<()> { + let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; + photocraft_paint::retouch::mix_back(surf, &Region::read(&self.pre_surf, r), work, r, self.opacity, self.sel.as_ref(), self.lock); + Ok(()) + } + + /// Render more points; returns the rectangle that changed. + /// Invalid coordinates reject the whole batch without changing the preview. + /// Only the position and pressure of `pts` are used: the commands take no tilt or times. + pub fn push(&mut self, pts: &[StrokePoint]) -> Result { + crate::brush_cmds::check_coords(pts, &self.cmd)?; + let pts: Vec<_> = pts.iter().map(|q| StrokePoint::new(q.x, q.y, q.pressure)).collect(); + self.points.extend_from_slice(&pts); + self.renderer.push(&pts); + let dmg = self.run_new_dabs(); + let ctx = self.renderer.ctx.clone(); + // The finishing dabs, run on a scratch copy of the working pixels. + let mut tail = None; + if self.preview_tail { + let ds = self.renderer.tail_dabs(); + let r = ds.iter().fold(Rect::EMPTY, |a, d| a.union(&ctx.dab_rect(d, false))); + if !r.is_empty() { + let mut region = self.work.read(&self.pre_surf, r.inflate(self.neighborhood.halo())); + for (k, d) in ds.iter().enumerate() { + (self.effect)(&mut region, &ctx.footprint(d, self.done + k)); + } + tail = Some((r, region.crop(r))); + } + } + // Back to the stroke without the previous tail. + let old = std::mem::replace(&mut self.tail, Rect::EMPTY); + let shown = dmg.union(&old); + let tail_rect = tail.as_ref().map_or(Rect::EMPTY, |(r, _)| *r); + if shown.is_empty() && tail_rect.is_empty() { + return Ok(Rect::EMPTY); + } + self.bounds = self.bounds.union(&dmg).union(&tail_rect); + if !shown.is_empty() { + let work = self.work.read(&self.pre_surf, shown); + self.show(shown, &work)?; + } + if let Some((r, work)) = tail { + self.show(r, &work)?; + self.tail = r; + } + Ok(shown.union(&tail_rect)) + } + + /// The finished stroke as its commit would take it over ([`Session::offer_live_dab`]): the + /// finishing dabs are run, and the pixels are the ones the command with the same params and + /// points paints. Only while the brush, tool settings and layer are still the ones the stroke + /// started with: the document revision doesn't cover those. + pub(crate) fn into_commit(mut self, s: &Session) -> Result { + let (Some((tool, brush)), Some(base)) = (self.tool.take(), self.base) else { + return Err(bad(&self.cmd, "this stroke's commit renders itself")); + }; + let (stroke, id) = parse_brush(s, &self.params, &self.cmd)?; + let (now, _) = DabTool::parse(s, &self.cmd, &self.params, &stroke)?; + if now != tool || stroke.brush != *brush || id != self.id { + return Err(bad(&self.cmd, "the brush, tool options or layer changed during the stroke")); + } + self.renderer.finish(); + let dmg = self.run_new_dabs(); + let shown = dmg.union(&self.tail); + if !shown.is_empty() { + let work = self.work.read(&self.pre_surf, shown); + self.show(shown, &work)?; + } + self.bounds = self.bounds.union(&dmg); + let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; + let surface = surf.clone(); + Ok(crate::brush_cmds::LiveCommit { + mixer: None, + cmd: self.cmd, + base, + params: self.params, + seed: None, + points: self.points, + surface, + bounds: self.bounds, + }) + } +} diff --git a/crates/engine/src/retouch_cmds/heal.rs b/crates/engine/src/retouch_cmds/heal.rs new file mode 100644 index 0000000000..79e42723e6 --- /dev/null +++ b/crates/engine/src/retouch_cmds/heal.rs @@ -0,0 +1,235 @@ +use super::clone::{blend_param, clamp_samples, clone_mapping, clone_result, clone_sample}; +use super::sampling::{SampleLayers, composite_region, sample_all_layers, sample_layers}; +use super::*; + +/// Grow a stroke's coverage map by `m` pixels of zero coverage (a Dirichlet boundary for the solve). +fn pad_coverage(bounds: Rect, cov: &[f32], m: i32) -> (Rect, Vec) { + let g = bounds.inflate(m); + let (w, gw) = (bounds.width() as usize, g.width() as usize); + let mut out = vec![0.0f32; gw * g.height() as usize]; + for (i, c) in cov.iter().enumerate() { + let (x, y) = (i % w + m as usize, i / w + m as usize); + out[y * gw + x] = *c; + } + (g, out) +} + +/// Seamless-clone `src` into `dst` over the mask (both regions cover the same rect). The solve runs +/// only on the mask's bounding box plus a one-pixel Dirichlet ring; the rest is `dst`. +pub(super) fn heal_region(fmt: &PixelFormat, src: &Region, dst: &Region, mask: &[bool]) -> Region { + let w = dst.width(); + let bbox = mask.iter().enumerate().filter(|(_, m)| **m).fold(Rect::EMPTY, |r, (i, _)| { + let (x, y) = (dst.rect.x0 + (i % w) as i32, dst.rect.y0 + (i / w) as i32); + r.union(&Rect::new(x, y, x + 1, y + 1)) + }); + let mut out = dst.clone(); + if bbox.is_empty() { + return out; + } + let r = bbox.inflate(1).intersect(&dst.rect); + let (s, d) = (src.crop(r), dst.crop(r)); + let m: Vec = (r.y0..r.y1).flat_map(|y| (r.x0..r.x1).map(move |x| (x, y))).map(|(x, y)| mask[dst.index(x, y)]).collect(); + let mut data = poisson::seamless_clone(d.width(), d.height(), d.ch, &s.data, &d.data, &m); + clamp_samples(fmt, &mut data); + let (n, rw) = (dst.ch, r.width() as usize); + for y in r.y0..r.y1 { + let o = out.index(r.x0, y) * n; + let i = (y - r.y0) as usize * rw * n; + out.data[o..o + rw * n].copy_from_slice(&data[i..i + rw * n]); + } + out +} + +pub(super) fn healing_brush(s: &mut Session, p: &Value) -> Result { + const CMD: &str = "paint.healingBrush"; + let (stroke, id) = parse_brush(s, p, CMD)?; + reject_huge_stroke(CMD, &stroke)?; + let which = sample_layers(p, CMD)?; + let mode = blend_param(p, CMD)?; + let aligned = flag(p, "aligned", true); + let map = clone_mapping(s, p, &stroke, CMD)?; + let off = map.offset(); + let dmg = run_stroke(s, "Healing Brush", id, p, |pre, surf, sel, lock| { + let (bounds, cov) = coverage(CMD, &stroke)?; + let (g, cov) = pad_coverage(bounds, &cov, 2); + let fmt = surf.format(); + let src = clone_sample(pre, id, surf, which, g, &map, false, CMD)?; + // The destination the texture is fitted to: the layer itself, or what the user sees when + // sampling several layers (so healing onto an empty layer matches the composite). + let dst = if which == SampleLayers::Current { Region::read(surf, g) } else { composite_region(pre, id, which, g, fmt) }; + let mask: Vec = cov.iter().map(|c| *c > 0.0).collect(); + let healed = heal_region(&fmt, &src, &dst, &mask); + Ok(apply_coverage(surf, g, &cov, stroke.brush.opacity, sel, lock, &healed, mode)) + })?; + Ok(clone_result(dmg, off, aligned, p, &stroke)) +} + +/// The History Brush's source: the targeted surface (pixels, mask, channel) as it was in History +/// panel row `state` or snapshot `snapshot` (default: the source set in the History panel, +/// `history.setSource`, else the oldest state still held, the "Open" state unless trimmed). +pub(super) fn history_source(s: &Session, id: Option, p: &Value, cmd: &str) -> Result { + let st = s.active().ok_or(EngineError::NoDocument)?; + let source_doc = crate::history_source_cmds::paint_source(st, p, cmd)?; + crate::channel_cmds::read_target_surface(source_doc.as_ref(), id, p) + .cloned() + .map_err(|_| EngineError::Other("the target did not exist (as pixels, a mask or a channel) in that history state".into())) +} + +pub(super) fn history_brush(s: &mut Session, p: &Value) -> Result { + const CMD: &str = "paint.historyBrush"; + let (stroke, id) = parse_brush(s, p, CMD)?; + let mode = blend_param(p, CMD)?; + // The coverage and the source region below are both dense over the stroke's bounds, so a + // thin stroke across a large canvas would still ask for the whole box. Refuse it from the + // points and brush size, before the source surface is cloned or anything is rendered (#4961). + reject_huge_stroke(CMD, &stroke)?; + let live = s.live_commit.take().filter(|l| l.matches(s, CMD, p, &stroke.points)); + let from_live = live.is_some(); + let src_surface = history_source(s, id, p, CMD)?; + let dmg = run_stroke(s, "History Brush", id, p, |_, surf, sel, lock| { + if let Some(l) = live { + *surf = l.surface; + return Ok(l.bounds); + } + let fmt = surf.format(); + let src = if src_surface.format() == fmt { src_surface } else { src_surface.convert(fmt) }; + let (bounds, cov) = coverage(CMD, &stroke)?; + let paint = Region::read(&src, bounds); + Ok(apply_coverage(surf, bounds, &cov, stroke.brush.opacity, sel, lock, &paint, mode)) + })?; + let mut r = json!({ "damage": damage_json(dmg) }); + if from_live { + r["fromLive"] = json!(true); + } + Ok(r) +} + +// --------------------------------------------------------------------------------------------- +// Spot Healing +// --------------------------------------------------------------------------------------------- + +/// Dilate a mask by `r` pixels (square structuring element). +fn dilate(w: usize, h: usize, m: &[bool], r: usize) -> Vec { + let mut tmp = vec![false; w * h]; + for y in 0..h { + for x in 0..w { + if m[y * w + x] { + for xx in x.saturating_sub(r)..(x + r + 1).min(w) { + tmp[y * w + xx] = true; + } + } + } + } + let mut out = vec![false; w * h]; + for y in 0..h { + for x in 0..w { + if tmp[y * w + x] { + for yy in y.saturating_sub(r)..(y + r + 1).min(h) { + out[yy * w + x] = true; + } + } + } + } + out +} + +/// Copy a translated Proximity Match patch from the *pre-healing* pixels. Reading +/// `out` here would make overlapping offsets repeatedly copy the pixels just written, +/// leaving streaks instead of the selected source texture. +pub(super) fn proximity_source_patch(img: &[f32], w: usize, h: usize, ch: usize, mask: &[bool], dx: i32, dy: i32) -> Option> { + if w == 0 || h == 0 || ch == 0 || mask.len() != w.checked_mul(h)? || img.len() != mask.len().checked_mul(ch)? { + return None; + } + let mut out = img.to_vec(); + for (p, masked) in mask.iter().enumerate() { + if !masked { + continue; + } + let x = i32::try_from(p % w).ok()?.checked_add(dx)?; + let y = i32::try_from(p / w).ok()?.checked_add(dy)?; + if x < 0 || y < 0 || x >= i32::try_from(w).ok()? || y >= i32::try_from(h).ok()? { + return None; + } + let src = (usize::try_from(y).ok()?.checked_mul(w)? + usize::try_from(x).ok()?).checked_mul(ch)?; + let dst = p.checked_mul(ch)?; + out.get_mut(dst..dst + ch)?.copy_from_slice(img.get(src..src + ch)?); + } + Some(out) +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum SpotType { + ContentAware, + CreateTexture, + ProximityMatch, +} + +/// Fill the stroke area with no source, then gradient-domain blend it into its surroundings. With +/// `all` (Sample All Layers) it heals what is visible, so it works on an empty layer above the image. +/// +/// Photoshop 25.4 (measured): a dab changes exactly the brush's footprint, and Content-Aware takes +/// its texture from around it, as Edit › Fill does from its window +/// ([`crate::fill_cmds::sampling_window`] of the stroke's bounds), which is what this samples. +fn spot_heal_surface(surf: &mut Surface, pre: &Document, stroke: &Stroke, kind: SpotType, all: bool, sel: Option<&Surface>, lock: bool) -> Result { + let (bounds, cov) = coverage("paint.spotHealing", stroke)?; + let size = stroke.brush.size; + let region_rect = crate::fill_cmds::sampling_window(bounds, pre.bounds()); + if region_rect.is_empty() { + return Ok(Rect::EMPTY); + } + // Proximity Match looks for its source anywhere in that window. + let margin = (region_rect.width().max(region_rect.height()) / 2) as i32; + let fmt = surf.format(); + let img = if all { composite_region(pre, None, SampleLayers::All, region_rect, fmt) } else { Region::read(surf, region_rect) }; + let (w, h, ch) = (img.width(), img.height(), img.ch); + let bw = bounds.width() as usize; + let mut stroke_cov = vec![0.0f32; w * h]; + for (i, c) in cov.iter().enumerate() { + let (x, y) = (bounds.x0 + (i % bw) as i32, bounds.y0 + (i / bw) as i32); + if region_rect.contains(x, y) { + stroke_cov[img.index(x, y)] = *c; + } + } + let hole: Vec = stroke_cov.iter().map(|c| *c > 0.0).collect(); + if !hole.iter().any(|h| *h) { + return Ok(Rect::EMPTY); + } + // Synthesise over a slightly larger domain than the Poisson region, so the solve's boundary pixels + // carry synthesised values that differ from the untouched surroundings: that mismatch is what the + // membrane corrects (otherwise the Dirichlet data would be all zero and the blend a no-op). + let domain = dilate(w, h, &hole, 2); + let heal_mask = dilate(w, h, &hole, 1); + let content_aware = + |img: &[f32]| inpaint::complete(w, h, ch, img, &domain, &CompleteParams::default()).unwrap_or_else(|| poisson::membrane_fill(w, h, ch, img, &domain)); + let filled = match kind { + SpotType::ContentAware => content_aware(&img.data), + SpotType::CreateTexture => inpaint::synthesize(w, h, ch, &img.data, &domain, (size / 4.0).clamp(6.0, 32.0) as usize, 0x5eed), + SpotType::ProximityMatch => { + let ring = (size / 8.0).clamp(3.0, 16.0) as usize; + match inpaint::best_offset(w, h, ch, &img.data, &domain, ring, margin) { + Some((dx, dy)) => proximity_source_patch(&img.data, w, h, ch, &domain, dx, dy).unwrap_or_else(|| content_aware(&img.data)), + None => content_aware(&img.data), + } + } + }; + let src = Region { rect: region_rect, ch, data: filled }; + let healed = heal_region(&fmt, &src, &img, &heal_mask); + let dmg = bounds.intersect(®ion_rect); + let dmg_cov: Vec = (dmg.y0..dmg.y1).flat_map(|y| (dmg.x0..dmg.x1).map(move |x| (x, y))).map(|(x, y)| stroke_cov[img.index(x, y)]).collect(); + Ok(apply_coverage(surf, dmg, &dmg_cov, stroke.brush.opacity, sel, lock, &healed, BlendMode::Normal)) +} + +pub(super) fn spot_healing(s: &mut Session, p: &Value) -> Result { + const CMD: &str = "paint.spotHealing"; + let (stroke, id) = parse_brush(s, p, CMD)?; + reject_huge_stroke(CMD, &stroke)?; + let kind = match string(p, "type", "contentAware") { + "contentAware" => SpotType::ContentAware, + "createTexture" => SpotType::CreateTexture, + "proximityMatch" => SpotType::ProximityMatch, + o => return Err(bad(CMD, format!("unknown type `{o}` (contentAware|createTexture|proximityMatch)"))), + }; + let all = sample_all_layers(p); + let dmg = run_stroke(s, "Spot Healing Brush", id, p, |pre, surf, sel, lock| spot_heal_surface(surf, pre, &stroke, kind, all, sel, lock))?; + Ok(json!({ "damage": damage_json(dmg) })) +} diff --git a/crates/engine/src/retouch_cmds/sampling.rs b/crates/engine/src/retouch_cmds/sampling.rs new file mode 100644 index 0000000000..31ab9ae99c --- /dev/null +++ b/crates/engine/src/retouch_cmds/sampling.rs @@ -0,0 +1,97 @@ +use super::*; + +// --------------------------------------------------------------------------------------------- +// Sampling sources +// --------------------------------------------------------------------------------------------- + +/// Which pixels Clone Stamp / Healing Brush sample. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum SampleLayers { + Current, + CurrentAndBelow, + All, +} + +pub(super) fn sample_layers(p: &Value, cmd: &str) -> Result { + match string(p, "sampleLayer", "current") { + "current" => Ok(SampleLayers::Current), + "currentAndBelow" => Ok(SampleLayers::CurrentAndBelow), + "all" => Ok(SampleLayers::All), + o => Err(bad(cmd, format!("unknown sampleLayer `{o}` (current|currentAndBelow|all)"))), + } +} + +/// Composite pixels of `rect` in `fmt` (native channels): the whole document, or the target layer +/// and everything below it. +pub(crate) fn composite_region(pre: &Document, id: Option, which: SampleLayers, rect: Rect, fmt: PixelFormat) -> Region { + let mut doc = pre.clone(); + if which == SampleLayers::CurrentAndBelow + && let Some(id) = id + { + // Hide everything composited after (above) the target in the bottom-to-top walk. + let walk: Vec<(Vec, LayerId)> = pre.walk().into_iter().map(|(p, _, l)| (p, l.id)).collect(); + if let Some(pos) = walk.iter().position(|(_, l)| *l == id) { + for (path, _) in &walk[pos + 1..] { + if let Some(l) = doc.layer_at_mut(path) { + l.visible = false; + } + } + } + } + let buf = photocraft_compose::render(&doc, rect); + let n = fmt.channels(); + let mut out = Region::new(rect, n); + let mut enc = [0.0f32; 8]; + for (i, px) in buf.px.iter().enumerate() { + from_rgba_into(&fmt, *px, &mut enc); + out.data[i * n..(i + 1) * n].copy_from_slice(&enc[..n]); + } + out +} + +/// Pixels to sample at `rect` (document coordinates, already offset to the source position). +pub(super) fn sample(pre: &Document, id: Option, surf: &Surface, which: SampleLayers, rect: Rect) -> Region { + match which { + SampleLayers::Current => Region::read(surf, rect), + _ => composite_region(pre, id, which, rect, surf.format()), + } +} + +/// "Sample All Layers" for Spot Healing, Blur, Sharpen and Smudge (layer pixels only: a mask or +/// channel has nothing to sample from other layers). +pub(super) fn sample_all_layers(p: &Value) -> bool { + flag(p, "sampleAllLayers", false) && targets_pixels(p) +} + +/// Sample All Layers for the sequential-dab tools: each dab's effect runs on a copy of the visible +/// composite (kept across dabs, so a dab sees the earlier ones), and the result is laid onto the +/// layer under the dab. An empty layer above the image so picks up the blurred or smudged picture, +/// and pixels the effect didn't change look as before (they are what was already visible). +pub(super) struct AllLayers<'a> { + pre: &'a Document, + fmt: PixelFormat, + visible: Option, +} + +impl<'a> AllLayers<'a> { + pub(super) fn new(pre: &'a Document, fmt: PixelFormat) -> Self { + Self { pre, fmt, visible: None } + } + + pub(super) fn dab(&mut self, work: &mut Region, fp: &Footprint, effect: impl FnOnce(&mut Region, &Footprint)) { + let (pre, fmt) = (self.pre, self.fmt); + let visible = self.visible.get_or_insert_with(|| composite_region(pre, None, SampleLayers::All, work.rect, fmt)); + if visible.rect != work.rect || visible.ch != work.ch { + return effect(work, fp); + } + effect(visible, fp); + let r = fp.rect.intersect(&work.rect); + for y in r.y0..r.y1 { + for x in r.x0..r.x1 { + if fp.at(x, y) > 0.0 { + work.px_mut(x, y).copy_from_slice(visible.px(x, y)); + } + } + } + } +}