PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Edit › Stroke: whole opaque pixels, Center like Photoshop, and strokes on the canvas edge (#2025)
kayrockscreenprinting0493d06aaad0
diff --git a/crates/engine/src/extra_cmds.rs b/crates/engine/src/extra_cmds.rsindex b424fe84d..658e75e1e 100644--- a/crates/engine/src/extra_cmds.rs+++ b/crates/engine/src/extra_cmds.rs@@ -122,7 +122,8 @@ fn map_pixels(s: &mut Session, label: &str, p: &Value, f: impl FnOnce(Rect, &mut/// Edit › Stroke: a band along the selection edge (or the layer's opaque edge without a selection).fn stroke(s: &mut Session, p: &Value) -> Result<Value> {- let width = p.get("width").and_then(Value::as_f64).unwrap_or(1.0).clamp(1.0, 250.0) as f32;+ // Whole pixels, rounded up, as in Photoshop (0.5 px strokes 1 px).+ let width = p.get("width").and_then(Value::as_f64).filter(|w| w.is_finite()).unwrap_or(1.0).clamp(1.0, 250.0).ceil() as usize;let color = color_param(p, "color", s.tools.foreground);let mode = match p.get("mode") {None | Some(Value::Null) => photocraft_color::BlendMode::Normal,@@ -161,12 +162,42 @@ fn stroke(s: &mut Session, p: &Value) -> Result<Value> {px.iter().map(|p| p[3]).collect()}};- let (outer, inner) = match location.as_str() {- "inside" => (edge.clone(), sel::contract(&edge, w, h, width)),- "outside" => (sel::expand(&edge, w, h, width), edge.clone()),- _ => (sel::expand(&edge, w, h, width / 2.0), sel::contract(&edge, w, h, width / 2.0)),+ // Past the canvas nothing is selected, so the canvas edge is a selection edge like any+ // other (Select All, then Stroke › Inside, frames the canvas): stroke on a padded copy.+ let pad = width + 1;+ let (pw, ph) = (w + 2 * pad, h + 2 * pad);+ let mut padded = vec![0.0f32; pw * ph];+ for (row, src) in padded.chunks_exact_mut(pw).skip(pad).zip(edge.chunks_exact(w.max(1))) {+ if let Some(dst) = row.get_mut(pad..pad + w) {+ dst.copy_from_slice(src);+ }+ }+ // Whole-pixel bands, fully opaque: Center puts the odd pixel inside (1 px is all inside).+ let (out_px, in_px) = match location.as_str() {+ "inside" => (0, width),+ "outside" => (width, 0),+ _ => (width / 2, width.div_ceil(2)),};- let band: Vec<f32> = outer.iter().zip(&inner).map(|(o, i)| (o - i).clamp(0.0, 1.0)).collect();+ // A hard-edged selection strokes hard, matching Photoshop pixel for pixel on curves:+ // outside, a pixel is stroked when its centre is less than width + 1 from a selected+ // pixel's; inside, less than width + ½ from an unselected pixel's (fitted to Photoshop's+ // strokes of a 14 px circle: 6 px Outside; 3, 5 and 6 px Inside). Both give whole+ // pixels on straight edges. Soft selections keep soft edges.+ let hard = padded.iter().all(|v| *v <= 0.0 || *v >= 1.0);+ let in_r = if hard && in_px > 0 { in_px as f32 - 0.5 } else { in_px as f32 };+ let mut outer = sel::expand(&padded, pw, ph, out_px as f32);+ let mut inner = sel::contract(&padded, pw, ph, in_r);+ if hard {+ outer.iter_mut().for_each(|v| *v = if *v > 0.0 { 1.0 } else { 0.0 });+ inner.iter_mut().for_each(|v| *v = if *v >= 1.0 { 1.0 } else { 0.0 });+ }+ let band: Vec<f32> = outer+ .chunks_exact(pw)+ .zip(inner.chunks_exact(pw))+ .skip(pad)+ .take(h)+ .flat_map(|(o, i)| o.iter().zip(i).skip(pad).take(w).map(|(o, i)| (o - i).clamp(0.0, 1.0)))+ .collect();let band = sel::mask_to_surface(&band, canvas);let lock = doc.effective_locks(id).transparency;let surf = crate::commands::paint_surface(doc, id, &Value::Null)?;@@ -908,6 +939,236 @@ mod tests {assert_eq!(pixel(&s, 15, 15), vec![0.0, 0.0, 1.0, 1.0]);}+ /// Photoshop's Edit › Stroke on a selection (checked by hand): Center puts ceil(w/2) px inside+ /// and floor(w/2) outside (1 px is all inside), fractional widths round up, and every stroked+ /// pixel is fully opaque, on every edge, horizontal ones included. Inside and Outside are+ /// w px on their side.+ #[test]+ fn stroke_widths_are_whole_opaque_pixels_like_photoshop() {+ // Selection x 10..30, y 10..30; the edge pixels inside are 10 and 29 on each axis.+ let band = |loc: &str, w: i32| -> (i32, i32) {+ match loc {+ "inside" => (w, 0),+ "outside" => (0, w),+ _ => ((w + 1) / 2, w / 2),+ }+ };+ let mut wrong = Vec::new();+ for depth in [8, 16, 32] {+ for loc in ["center", "inside", "outside"] {+ for (param, w) in [(json!(1), 1), (json!(2), 2), (json!(3), 3), (json!(4), 4), (json!(5), 5), (json!(0.5), 1), (json!(2.5), 3)] {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 50, "height": 50, "depth": depth})).unwrap();+ s.execute("layer.new.layer", json!({})).unwrap();+ s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();+ s.execute("edit.stroke", json!({"width": param, "color": "#ff0000", "location": loc})).unwrap();+ let (inside, outside) = band(loc, w);+ // (edge pixel inside, step inward) for left, right, top and bottom; the other+ // coordinate is the middle of the edge.+ for (name, first_in, inward, horizontal) in+ [("left", 10, 1, false), ("right", 29, -1, false), ("top", 10, 1, true), ("bottom", 29, -1, true)]+ {+ for k in -7..7 {+ // k ≥ 0: k px inside from the edge; k < 0: -k px outside.+ let c = first_in + k * inward;+ let (x, y) = if horizontal { (20, c) } else { (c, 20) };+ let want = if (k >= 0 && k < inside) || (k < 0 && -k <= outside) { 1.0 } else { 0.0 };+ let a = pixel(&s, x, y)[3];+ if (a - want).abs() > 0.004 {+ wrong.push(format!("{depth}-bit {loc} width {param} {name} edge, {k:+} px: alpha {a}, want {want}"));+ }+ }+ }+ }+ }+ }+ assert!(wrong.is_empty(), "{} pixels differ from Photoshop:\n{}", wrong.len(), wrong.iter().take(40).cloned().collect::<Vec<_>>().join("\n"));+ }++ /// Photoshop's 6 px Outside stroke of a 14 px circle selection made without anti-aliasing,+ /// read pixel by pixel from Photoshop (`#` stroked, `.` not; the hole is the selection). A+ /// hard selection gives a hard stroke: a pixel is stroked when its centre is less than+ /// width + 1 from a selected pixel's centre (no partial pixels on the curve).+ const PS_CIRCLE_OUTSIDE_6: [&str; 31] = [+ "..................................",+ "..................................",+ "...........############...........",+ "..........##############..........",+ ".........################.........",+ "........##################........",+ ".......####################.......",+ "......######################......",+ ".....#########......#########.....",+ "....#########........#########....",+ "....########..........########....",+ "....#######............#######....",+ "....######..............######....",+ "....######..............######....",+ "....######..............######....",+ "....######..............######....",+ "....######..............######....",+ "....######..............######....",+ "....#######............#######....",+ "....########..........########....",+ "....#########........#########....",+ ".....#########......#########.....",+ "......######################......",+ ".......####################.......",+ "........##################........",+ ".........################.........",+ "..........##############..........",+ "...........############...........",+ "..................................",+ "..................................",+ "..................................",+ ];++ /// Photoshop's 3 px Inside stroke of the same 14 px hard circle (the selection is the ring+ /// plus its hole). Inside, a pixel is stroked when its centre is less than width + ½ from an+ /// unselected pixel's: that offset is the one fitting this, the 5 px and the 6 px samples+ /// (any value from +0.41 to +0.60), so 6 px fills this circle (its deepest pixel is 6.4 px in).+ const PS_CIRCLE_INSIDE_3: [&str; 17] = [+ "...................",+ "......######.......",+ ".....########......",+ "....##########.....",+ "...####....####....",+ "..####......####...",+ "..###........###...",+ "..###........###...",+ "..###........###...",+ "..###........###...",+ "..####......####...",+ "...####....####....",+ "....##########.....",+ ".....########......",+ "......######.......",+ "...................",+ "...................",+ ];++ /// The selection of a Photoshop sample grid: an Outside stroke's hole, or an Inside stroke's+ /// ring and hole.+ fn grid_selection(rows: &[&str], outside: bool) -> Vec<f32> {+ let (w, h) = (rows[0].len(), rows.len());+ let mut mask = vec![0.0f32; w * h];+ for (y, row) in rows.iter().enumerate() {+ let b = row.as_bytes();+ if let (Some(first), Some(last)) = (b.iter().position(|c| *c == b'#'), b.iter().rposition(|c| *c == b'#')) {+ for x in first..=last {+ if !outside || b[x] == b'.' {+ mask[y * w + x] = 1.0;+ }+ }+ }+ }+ mask+ }++ /// Strokes `mask` as the selection of a `w` × `h` layer and returns its alpha per pixel.+ fn stroke_alpha(mask: &[f32], w: usize, h: usize, depth: u32, params: Value) -> Vec<f32> {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": w, "height": h, "depth": depth})).unwrap();+ s.execute("layer.new.layer", json!({})).unwrap();+ s.edit("select", |doc, _| {+ doc.selection = Some(sel::mask_to_surface(mask, doc.bounds()));+ Ok(())+ })+ .unwrap();+ s.execute("edit.stroke", params).unwrap();+ (0..h).flat_map(|y| (0..w).map(move |x| (x, y))).map(|(x, y)| pixel(&s, x as i32, y as i32)[3]).collect()+ }++ #[test]+ fn a_hard_circle_strokes_inside_like_photoshop() {+ let rows = PS_CIRCLE_INSIDE_3;+ let (w, h) = (rows[0].len(), rows.len());+ let mask = grid_selection(&rows, false);+ for depth in [8, 16, 32] {+ let got = stroke_alpha(&mask, w, h, depth, json!({"width": 3, "color": "#000000", "location": "inside"}));+ let wrong: Vec<String> = rows+ .iter()+ .enumerate()+ .flat_map(|(y, row)| row.bytes().enumerate().map(move |(x, c)| (x, y, c)))+ .filter_map(|(x, y, c)| {+ let want = if c == b'#' { 1.0 } else { 0.0 };+ let a = got[y * w + x];+ ((a - want).abs() > 0.004).then(|| format!("({x}, {y}): {a}, Photoshop {want}"))+ })+ .collect();+ assert!(wrong.is_empty(), "{depth}-bit: {} pixels differ from Photoshop: {wrong:?}", wrong.len());+ // Checked in Photoshop: a 5 px Inside stroke leaves a plus-shaped hole of 12 pixels in+ // the middle (rows 2, 4, 4, 2 wide), and 6 px fills the whole circle.+ let hole = [(8, 6), (9, 6), (7, 7), (8, 7), (9, 7), (10, 7), (7, 8), (8, 8), (9, 8), (10, 8), (8, 9), (9, 9)];+ let five = stroke_alpha(&mask, w, h, depth, json!({"width": 5, "color": "#000000", "location": "inside"}));+ for (i, m) in mask.iter().enumerate() {+ let stroked = *m > 0.0 && !hole.contains(&(i % w, i / w));+ assert_eq!(five[i] > 0.996, stroked, "{depth}-bit 5 px Inside at ({}, {})", i % w, i / w);+ }+ let full = stroke_alpha(&mask, w, h, depth, json!({"width": 6, "color": "#000000", "location": "inside"}));+ for (i, m) in mask.iter().enumerate() {+ assert_eq!(full[i] > 0.996, *m > 0.0, "{depth}-bit 6 px Inside at ({}, {})", i % w, i / w);+ }+ }+ }++ #[test]+ fn a_hard_circle_strokes_like_photoshop() {+ let rows = PS_CIRCLE_OUTSIDE_6;+ let (w, h) = (rows[0].len(), rows.len());+ // The selection: the `.` cells enclosed by the ring on each row.+ let mask = grid_selection(&rows, true);+ for depth in [8, 16, 32] {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": w, "height": h, "depth": depth})).unwrap();+ s.execute("layer.new.layer", json!({})).unwrap();+ s.edit("select", |doc, _| {+ doc.selection = Some(sel::mask_to_surface(&mask, doc.bounds()));+ Ok(())+ })+ .unwrap();+ s.execute("edit.stroke", json!({"width": 6, "color": "#000000", "location": "outside"})).unwrap();+ let mut wrong = Vec::new();+ for (y, row) in rows.iter().enumerate() {+ for (x, c) in row.bytes().enumerate() {+ let want = if c == b'#' { 1.0 } else { 0.0 };+ let a = pixel(&s, x as i32, y as i32)[3];+ if (a - want).abs() > 0.004 {+ wrong.push(format!("({x}, {y}): {a}, Photoshop {want}"));+ }+ }+ }+ assert!(wrong.is_empty(), "{depth}-bit: {} pixels differ from Photoshop: {wrong:?}", wrong.len());+ }+ }++ /// Select All, then Stroke › Inside draws a border around the canvas (a common Photoshop+ /// technique): the canvas edge is a selection edge like any other, on all four sides.+ #[test]+ fn stroking_a_selection_at_the_canvas_edge_draws_that_edge() {+ for depth in [8, 16, 32] {+ for (loc, w, inside) in [("inside", 3, 3), ("center", 3, 2), ("center", 1, 1)] {+ let mut s = session(depth);+ s.execute("select.all", json!({})).unwrap();+ s.execute("edit.stroke", json!({"width": w, "color": "#ff0000", "location": loc})).unwrap();+ // 40 × 30 canvas: k px in from each side, in the middle of the side.+ for name in ["left", "right", "top", "bottom"] {+ for k in 0..6 {+ let (x, y) = match name {+ "left" => (k, 15),+ "right" => (39 - k, 15),+ "top" => (20, k),+ _ => (20, 29 - k),+ };+ let want = if k < inside { 1.0 } else { 0.0 };+ let a = pixel(&s, x, y)[3];+ assert!((a - want).abs() < 0.004, "{depth}-bit {loc} {w} px, {name} side, {k} px in: alpha {a}, want {want}");+ }+ }+ }+ }+ }+#[test]fn stroke_uses_fill_blending_and_preserves_transparency_at_every_depth() {for depth in [8, 16, 32] {