mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 01:18:43 +00:00
Pattern Stamp: grey Behind in the options bar on the Background; Impressionist keeps its pixels (#12879)
- The options bar's Mode dropdown greys Behind when the Background is the active layer, as the Shift+right-click menu does: the entry is drawn disabled, a click on it picks nothing, and the wheel and the keys step past it. On a pixel layer it is enabled. Only the Pattern Stamp's dropdown uses the new greyed-option variant of the dropdown widget. - Behind with Impressionist was not measured, so an Impressionist stroke again gives exactly the pixels it gives in Normal, committed and live, when Behind is picked. The per-dab Behind code is removed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
8228c3649c
commit
5d6a491060
9 files changed
+229
-93
No files matched your search
@@ -5,7 +5,9 @@
|
||||
//! with a seeded RNG so a replay of the same points is identical.
|
||||
//!
|
||||
//! `"mode": "behind"` is the painting-only Behind mode, as the Brush's: the pattern goes beneath
|
||||
//! the layer's pixels, so it shows only where the layer is transparent (#12879).
|
||||
//! the layer's pixels, so it shows only where the layer is transparent (#12879). Behind with
|
||||
//! Impressionist was not measured: those strokes lay their dabs over the pixels in Normal, as
|
||||
//! they did before Behind existed.
|
||||
|
||||
use photocraft_color::{BlendMode, PixelFormat};
|
||||
use photocraft_compose::pattern::{Placement, Tile, render};
|
||||
@@ -33,7 +35,8 @@ pub(super) struct Setup {
|
||||
pub(super) stroke: Stroke,
|
||||
pub(super) id: Option<LayerId>,
|
||||
pub(super) mode: BlendMode,
|
||||
/// Behind: paint beneath the layer's pixels (`mode` is then Normal).
|
||||
/// Behind: paint beneath the layer's pixels (`mode` is then Normal). Aligned strokes only:
|
||||
/// Impressionist dabs are laid in `mode`.
|
||||
pub(super) behind: bool,
|
||||
aligned: bool,
|
||||
pub(super) impressionist: bool,
|
||||
@@ -79,30 +82,14 @@ impl Setup {
|
||||
placement(self.phase, self.scale, self.angle)
|
||||
}
|
||||
|
||||
/// One Impressionist dab's edit of a working copy in `fmt`. With Behind, `lock` is the
|
||||
/// target's transparency lock: nothing can go beneath locked pixels.
|
||||
fn impressionist_effect(&self, fmt: PixelFormat, lock: bool) -> super::DabEffect {
|
||||
let (tile, phase, scale, angle, seed) = (self.tile.clone(), self.phase, self.scale, self.angle, self.seed);
|
||||
/// One Impressionist dab's edit of a working copy in `fmt`.
|
||||
fn impressionist_effect(&self, fmt: PixelFormat) -> super::DabEffect {
|
||||
let (tile, phase, scale, angle, seed, mode) = (self.tile.clone(), self.phase, self.scale, self.angle, self.seed, self.mode);
|
||||
let (tw, th) = (self.pat.width as f32 * scale, self.pat.height as f32 * scale);
|
||||
let lay = match (self.behind, alpha_index(&fmt)) {
|
||||
(false, _) => Lay::Over(self.mode),
|
||||
(true, Some(a)) if !lock => Lay::Behind(a),
|
||||
// No transparency to paint into (the Background, a mask or channel, a locked layer).
|
||||
(true, _) => return Box::new(|_, _| {}),
|
||||
};
|
||||
Box::new(move |work, fp| stamp_dab(work, &fmt, &tile, fp, phase, scale, angle, seed, tw, th, lay))
|
||||
Box::new(move |work, fp| stamp_dab(work, &fmt, &tile, fp, phase, scale, angle, seed, tw, th, mode))
|
||||
}
|
||||
}
|
||||
|
||||
/// How one Impressionist dab meets the working pixels.
|
||||
#[derive(Clone, Copy)]
|
||||
enum Lay {
|
||||
/// Over them, in this blend mode.
|
||||
Over(BlendMode),
|
||||
/// Beneath them; the alpha channel's index.
|
||||
Behind(usize),
|
||||
}
|
||||
|
||||
/// `src` beneath `dst` at coverage `k`, in native channels with alpha at `a`: the Brush's Behind.
|
||||
/// Opaque pixels are unchanged; partly transparent ones keep their colour's share.
|
||||
fn under_native(a: usize, dst: &mut [f32], src: &[f32], k: f32) {
|
||||
@@ -175,7 +162,7 @@ pub(super) fn run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let sel = pre.selection.as_ref();
|
||||
if st.impressionist {
|
||||
// Halo is the dab footprint only: Impressionist jitters phase, it does not blur.
|
||||
let effect = st.impressionist_effect(surf.format(), lock);
|
||||
let effect = st.impressionist_effect(surf.format());
|
||||
Ok(apply_dab_stroke(surf, &st.stroke, sel, lock, 0, effect))
|
||||
} else {
|
||||
paint_aligned(surf, &st, sel, lock, CMD)
|
||||
@@ -202,9 +189,7 @@ pub fn live_impressionist(s: &Session, p: &Value) -> Result<super::LiveDab> {
|
||||
}
|
||||
let (stroke, id) = (st.stroke.clone(), st.id);
|
||||
let doc = (*s.active().ok_or(crate::EngineError::NoDocument)?.doc).clone();
|
||||
// Behind paints nothing beneath locked pixels, as the commit.
|
||||
let lock = st.behind && crate::channel_cmds::target_surface(&mut doc.clone(), id, p)?.1;
|
||||
super::LiveDab::with_effect_on(doc, CMD, p, stroke, id, 0, |pre| st.impressionist_effect(pre.format(), lock))?.with_tail_preview()
|
||||
super::LiveDab::with_effect_on(doc, CMD, p, stroke, id, 0, |pre| st.impressionist_effect(pre.format()))?.with_tail_preview()
|
||||
}
|
||||
|
||||
/// The pattern under destination `rect` in `fmt` (native channels): what an aligned stroke paints
|
||||
@@ -315,7 +300,7 @@ fn stamp_dab(
|
||||
seed: u64,
|
||||
tw: f32,
|
||||
th: f32,
|
||||
lay: Lay,
|
||||
mode: BlendMode,
|
||||
) {
|
||||
let r = fp.rect.intersect(&work.rect);
|
||||
if r.is_empty() {
|
||||
@@ -340,24 +325,16 @@ fn stamp_dab(
|
||||
let Some(px) = rgba.get(oy.saturating_mul(rw).saturating_add(ox)) else { continue };
|
||||
from_rgba_into(fmt, *px, &mut enc);
|
||||
let dst = work.px_mut(x, y);
|
||||
match lay {
|
||||
Lay::Behind(a) => {
|
||||
if let Some(src) = enc.get(..n) {
|
||||
under_native(a, dst, src, k);
|
||||
}
|
||||
if mode == BlendMode::Normal {
|
||||
if let Some(src) = enc.get(..n) {
|
||||
over_native(fmt, dst, src, k, false);
|
||||
}
|
||||
Lay::Over(BlendMode::Normal) => {
|
||||
if let Some(src) = enc.get(..n) {
|
||||
over_native(fmt, dst, src, k, false);
|
||||
}
|
||||
}
|
||||
Lay::Over(mode) => {
|
||||
let d = to_rgba(fmt, dst);
|
||||
let o = photocraft_color::blend::composite(mode, d, *px, k);
|
||||
from_rgba_into(fmt, o, &mut enc);
|
||||
if let Some(src) = enc.get(..n) {
|
||||
dst.copy_from_slice(src);
|
||||
}
|
||||
} else {
|
||||
let d = to_rgba(fmt, dst);
|
||||
let o = photocraft_color::blend::composite(mode, d, *px, k);
|
||||
from_rgba_into(fmt, o, &mut enc);
|
||||
if let Some(src) = enc.get(..n) {
|
||||
dst.copy_from_slice(src);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! #12879: the Pattern Stamp's Behind mode (`"mode": "behind"`). The pattern goes beneath the
|
||||
//! layer's pixels: it shows where the layer is transparent and leaves existing pixels alone,
|
||||
//! Aligned on and off, as the Brush's Behind does. The live stroke shows what the commit paints,
|
||||
//! and every other mode paints what it painted before.
|
||||
//! and every other mode paints what it painted before. Behind with Impressionist was not
|
||||
//! measured, so an Impressionist stroke paints what it paints in Normal.
|
||||
|
||||
use photocraft_paint::StrokePoint;
|
||||
use serde_json::json;
|
||||
@@ -153,16 +154,10 @@ fn behind_paints_nothing_without_transparency_to_paint_into() {
|
||||
})
|
||||
.unwrap();
|
||||
let before = layer(&s);
|
||||
for impressionist in [false, true] {
|
||||
stamp(&mut s, json!({"mode": "behind", "impressionist": impressionist}));
|
||||
assert_eq!(layer(&s), before, "locked transparency, impressionist {impressionist}");
|
||||
let live = if impressionist {
|
||||
live_impressionist(&s, &stroke(json!({"mode": "behind", "impressionist": true}))).unwrap().doc.clone()
|
||||
} else {
|
||||
super::super::LiveRetouch::begin(&s, "paint.patternStamp", &stroke(json!({"mode": "behind"}))).unwrap().doc.clone()
|
||||
};
|
||||
assert_eq!(pixels(&live, &s), before, "the live stroke too, impressionist {impressionist}");
|
||||
}
|
||||
stamp(&mut s, json!({"mode": "behind"}));
|
||||
assert_eq!(layer(&s), before, "locked transparency");
|
||||
let live = super::super::LiveRetouch::begin(&s, "paint.patternStamp", &stroke(json!({"mode": "behind"}))).unwrap().doc.clone();
|
||||
assert_eq!(pixels(&live, &s), before, "the live stroke too");
|
||||
// The Background has no transparency.
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": W, "height": H})).unwrap();
|
||||
@@ -171,54 +166,63 @@ fn behind_paints_nothing_without_transparency_to_paint_into() {
|
||||
assert_eq!(layer(&s), before, "the Background");
|
||||
}
|
||||
|
||||
/// Impressionist dabs go beneath the pixels too.
|
||||
/// Behind with Impressionist was not measured: the stroke gives the pixels it gives in Normal,
|
||||
/// committed and live, on a half-filled layer and with transparency locked.
|
||||
#[test]
|
||||
fn behind_with_impressionist_leaves_existing_pixels_alone() {
|
||||
fn impressionist_paints_as_in_normal_when_behind_is_picked() {
|
||||
let all = [[12.0, 24.0], [30.0, 20.0], [40.0, 28.0], [52.0, 24.0]];
|
||||
let opts = |v: &[[f64; 2]], mode: &str| json!({"points": v, "size": 14, "hardness": 40, "opacity": 70, "pattern": "Bricks", "impressionist": true, "mode": mode, "seed": 11});
|
||||
for depth in [8, 16] {
|
||||
let mut s = session(depth, half);
|
||||
let before = layer(&s);
|
||||
stamp(&mut s, json!({"mode": "behind", "impressionist": true, "pattern": "Bricks"}));
|
||||
let got = layer(&s);
|
||||
let mut painted = 0;
|
||||
for y in 0..H {
|
||||
for x in 0..W {
|
||||
if x < W / 2 {
|
||||
assert_eq!(at(&got, x, y), at(&before, x, y), "{depth}-bit ({x}, {y})");
|
||||
} else {
|
||||
painted += usize::from(at(&got, x, y)[3] > 0.99);
|
||||
for lock in [false, true] {
|
||||
let run = |mode: &str| {
|
||||
let mut s = session(depth, half);
|
||||
if lock {
|
||||
s.edit("lock", |doc, active| {
|
||||
doc.layer_mut(active.unwrap()).unwrap().locks.transparency = true;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
let before = layer(&s);
|
||||
let mut live = live_impressionist(&s, &opts(&all[..1], mode)).unwrap();
|
||||
live.push(&all[1..].iter().map(|q| StrokePoint::new(q[0], q[1], 1.0)).collect::<Vec<_>>()).unwrap();
|
||||
let shown = pixels(&live.doc, &s);
|
||||
s.execute("paint.patternStamp", opts(&all, mode)).unwrap();
|
||||
(before, shown, layer(&s))
|
||||
};
|
||||
let (before, live_behind, behind) = run("behind");
|
||||
let (_, live_normal, normal) = run("normal");
|
||||
let tag = format!("{depth}-bit lock {lock}");
|
||||
assert_eq!(behind, normal, "{tag}: the committed stroke");
|
||||
assert_eq!(live_behind, live_normal, "{tag}: the live stroke");
|
||||
assert_eq!(live_behind, behind, "{tag}: live is the commit");
|
||||
// As in Normal, the dabs go over the filled half.
|
||||
assert!((0..W / 2).any(|x| at(&behind, x, 24) != at(&before, x, 24)), "{tag}: painted over the pixels");
|
||||
}
|
||||
assert!(painted > 200, "{depth}-bit: {painted} px");
|
||||
}
|
||||
}
|
||||
|
||||
/// The live stroke, fed point by point, shows what the commit paints.
|
||||
/// The live aligned stroke, fed point by point, shows what the commit paints.
|
||||
#[test]
|
||||
fn the_live_behind_stroke_matches_the_commit() {
|
||||
let all = [[12.0, 24.0], [30.0, 20.0], [40.0, 28.0], [52.0, 24.0]];
|
||||
let pts = |v: &[[f64; 2]]| v.iter().map(|q| StrokePoint::new(q[0], q[1], 1.0)).collect::<Vec<_>>();
|
||||
for depth in [8, 16] {
|
||||
for (impressionist, aligned) in [(false, true), (false, false), (true, true)] {
|
||||
for aligned in [true, false] {
|
||||
let mut s = session(depth, |x, y| if x < W / 2 { RED } else { [0.2, 0.4, 0.9, 0.25 * ((x + y) % 4) as f32] });
|
||||
let before = layer(&s);
|
||||
let opts = |v: &[[f64; 2]]| {
|
||||
json!({"points": v, "size": 14, "hardness": 40, "opacity": 70, "pattern": "Checkerboard", "scale": 150, "angle": 20,
|
||||
"aligned": aligned, "impressionist": impressionist, "mode": "behind", "seed": 11})
|
||||
"aligned": aligned, "mode": "behind", "seed": 11})
|
||||
};
|
||||
let shown = if impressionist {
|
||||
let mut live = live_impressionist(&s, &opts(&all[..1])).unwrap();
|
||||
live.push(&pts(&all[1..3])).unwrap();
|
||||
live.push(&pts(&all[3..])).unwrap();
|
||||
live.doc.clone()
|
||||
} else {
|
||||
let shown = {
|
||||
let mut live = super::super::LiveRetouch::begin(&s, "paint.patternStamp", &opts(&all[..1])).unwrap();
|
||||
live.push(&pts(&all[1..3])).unwrap();
|
||||
live.push(&pts(&all[3..])).unwrap();
|
||||
live.doc.clone()
|
||||
};
|
||||
s.execute("paint.patternStamp", opts(&all)).unwrap();
|
||||
let tag = format!("{depth}-bit impressionist {impressionist} aligned {aligned}");
|
||||
let tag = format!("{depth}-bit aligned {aligned}");
|
||||
let committed = layer(&s);
|
||||
assert_eq!(pixels(&shown, &s), committed, "{tag}");
|
||||
assert_ne!(committed, before, "{tag}: the stroke painted");
|
||||
|
||||
Reference in new issue
Block a user