From e22984536c2cc6df786bc71e68828145528a8ea8 Mon Sep 17 00:00:00 2001 From: xwize Date: Fri, 23 Oct 2026 09:43:30 +0100 Subject: [PATCH 1/5] Indexed Color: keep Brush strokes inside the colour table An Indexed Color document keeps its pixels expanded to RGB with the palette as side data, and the Brush rendered straight onto that surface. A red stroke on a black/white document therefore left opaque red pixels, and soft edges or partial opacity left blends, all outside the colour table, while the document still reported Indexed mode. Photoshop (and GIMP, which documents it) paint the nearest table colour instead. The stroke commit now maps the damaged rectangle to the nearest table entry with the same no-dither quantiser the Indexed Color conversion uses. That keeps the table's transparent entry binary as well. The live preview applies the same step, so the canvas shows the pixels the commit keeps. This covers paint.stroke and paint.pencil, including erasing. Masks and channels are gray targets and are left alone. Fixes #8669 Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/brush_cmds.rs | 14 ++++++- crates/engine/src/mode_cmds.rs | 28 ++++++++++++- crates/engine/src/mode_cmds/tests.rs | 61 ++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/brush_cmds.rs b/crates/engine/src/brush_cmds.rs index feb238cf402..18024bbd3d7 100644 --- a/crates/engine/src/brush_cmds.rs +++ b/crates/engine/src/brush_cmds.rs @@ -293,6 +293,7 @@ fn stroke_with(s: &mut Session, cmd: &str, p: &Value, label: &str, brush: BrushS return Ok(l.bounds); } let sel = doc.selection.clone(); + let table = crate::mode_cmds::paint_color_table(doc, p); let (surf, lock) = crate::channel_cmds::target_surface(doc, id, p)?; let mut brush = brush; erase_locked(&mut brush, lock, bg); @@ -323,6 +324,9 @@ fn stroke_with(s: &mut Session, cmd: &str, p: &Value, label: &str, brush: BrushS } else { render_stroke(surf, &brush, &pts, sel.as_ref(), lock, zoom) }; + if let Some(table) = &table { + crate::mode_cmds::keep_in_color_table(table, surf, damage); + } Ok(damage) }) })?; @@ -583,7 +587,7 @@ impl LiveStroke { check_coords(pts, &self.cmd)?; self.points.extend_from_slice(pts); let cmyk = self.cmyk.clone(); - photocraft_color::convert::with_cmyk_space(cmyk.as_ref(), || { + let dmg = photocraft_color::convert::with_cmyk_space(cmyk.as_ref(), || -> Result { self.renderer.push(pts); if let Some(symmetry) = &self.symmetry { for ((mirror, distinct), reflected) in self.mirrors.iter_mut().zip(&mut self.mirror_distinct).zip(symmetry.reflected_passes(pts)) { @@ -629,7 +633,13 @@ impl LiveStroke { dmg = dmg.union(&self.tail); } Ok(dmg) - }) + })?; + // The preview shows the pixels the commit keeps (see `stroke_with`). + if let Some(table) = crate::mode_cmds::paint_color_table(&self.doc, &self.params) { + let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.layer, &self.params)?; + crate::mode_cmds::keep_in_color_table(&table, surf, dmg); + } + Ok(dmg) } } diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rs index 180f940e917..87954e437bb 100644 --- a/crates/engine/src/mode_cmds.rs +++ b/crates/engine/src/mode_cmds.rs @@ -12,7 +12,7 @@ use photocraft_algo::transform::{Homography, Interp}; use photocraft_color::{ColorMode, PixelFormat, SampleType}; use photocraft_doc::adjust::CurvePoint; use photocraft_doc::{ColorTable, Document, Duotone, DuotoneInk, Layer, LayerContent, LayerId, Size}; -use photocraft_geom::Affine; +use photocraft_geom::{Affine, Rect}; use photocraft_raster::Surface; use serde_json::{Value, json}; @@ -391,6 +391,32 @@ fn color_table(s: &mut Session, p: &Value) -> Result { Ok(out) } +/// The colour table painting on `p`'s target must stay inside: an Indexed Color document's, +/// when the target is layer pixels (masks and channels are gray, not indexed). +pub(crate) fn paint_color_table(doc: &Document, p: &Value) -> Option { + let pixels = crate::channel_cmds::target_of(p) == crate::channel_cmds::Target::Pixels; + doc.color_table.clone().filter(|_| pixels && doc.mode == ColorMode::Indexed) +} + +/// Map the pixels of `r` to their nearest colour-table entry, as the Indexed Color conversion +/// does with no dither, so painting never leaves the table (#8669). +pub(crate) fn keep_in_color_table(table: &ColorTable, surf: &mut Surface, r: Rect) { + let r = r.intersect(&surf.tile_bounds()); + if r.is_empty() || table.colors.is_empty() { + return; + } + let fmt = surf.format(); + let mut data = surf.read_region(r); + let mut px: Vec<[f32; 4]> = data.chunks_exact(fmt.channels()).map(|q| photocraft_raster::to_rgba(&fmt, q)).collect(); + quantize::quantize(&mut px, r.width() as usize, &table.colors, Dither::None, 0.0, table.transparent.map(usize::from)); + let mut enc = [0.0f32; 8]; + for (q, c) in data.chunks_exact_mut(fmt.channels()).zip(px) { + photocraft_raster::from_rgba_into(&fmt, c, &mut enc); + q.iter_mut().zip(enc).for_each(|(d, v)| *d = v); + } + surf.write_region(r, &data); +} + // ---------- Bitmap ---------- /// Bitmap's Output resolution range (ppi): what a new document may have. diff --git a/crates/engine/src/mode_cmds/tests.rs b/crates/engine/src/mode_cmds/tests.rs index 84d816a3492..a9ec8cca72c 100644 --- a/crates/engine/src/mode_cmds/tests.rs +++ b/crates/engine/src/mode_cmds/tests.rs @@ -696,3 +696,64 @@ fn color_table_rejects_an_invalid_transparent_index_without_editing() { s.execute("image.mode.colorTable", json!({"transparent": 0})).unwrap(); assert_eq!(doc(&s).color_table.as_ref().unwrap().transparent, Some(0)); } + +/// A white 16×16 document in Indexed Color with the two-entry black/white table (#8669's repro). +fn black_white_indexed(transparency: bool) -> Session { + let mut s = Session::new(); + let background = if transparency { "transparent" } else { "white" }; + s.execute("file.new", json!({"width": 16, "height": 16, "background": background})).unwrap(); + paint(&mut s, |x, _| { + if x < 2 { + [0.0; 4] + } else if x < 4 { + [0.0, 0.0, 0.0, 1.0] + } else { + [1.0; 4] + } + }); + s.execute("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "transparency": transparency, "dither": "none"})).unwrap(); + s +} + +#[test] +fn brush_stroke_stays_in_indexed_color_table() { + let mut s = black_white_indexed(false); + assert_eq!(doc(&s).color_table.as_ref().unwrap().colors, vec![[0, 0, 0], [255, 255, 255]]); + s.execute("paint.stroke", json!({"points": [[8, 8, 1]], "size": 8, "color": "#ff0000", "opacity": 1, "flow": 1})).unwrap(); + // Red's nearest entry is black. + assert_eq!(doc(&s).layers[0].surface().unwrap().rgba(8, 8), [0.0, 0.0, 0.0, 1.0]); + // Soft edges and partial opacity blend, and the blends map back onto the table too. + s.execute("paint.stroke", json!({"points": [[4, 12], [14, 12]], "size": 6, "hardness": 0.0, "color": "#808080", "opacity": 0.5})).unwrap(); + let table = doc(&s).color_table.clone().unwrap(); + assert!(colours(doc(&s)).iter().all(|c| table.colors.contains(c)), "off-table pixels: {:?}", colours(doc(&s))); + assert_eq!(doc(&s).mode, ColorMode::Indexed); +} + +#[test] +fn brush_stroke_keeps_indexed_transparency_binary() { + let mut s = black_white_indexed(true); + let table = doc(&s).color_table.clone().unwrap(); + assert!(table.transparent.is_some()); + s.execute("paint.stroke", json!({"points": [[0, 8], [15, 8]], "size": 8, "hardness": 0.0, "color": "#ff0000", "opacity": 0.7})).unwrap(); + let d = doc(&s); + let surf = d.layers[0].surface().unwrap(); + for (x, y) in (0..16).flat_map(|y| (0..16).map(move |x| (x, y))) { + let a = surf.rgba(x, y)[3]; + assert!(a == 0.0 || a == 1.0, "alpha {a} at ({x},{y})"); + } + assert!(colours(d).iter().all(|c| table.colors.contains(c)), "off-table pixels: {:?}", colours(d)); +} + +#[test] +fn live_brush_preview_matches_indexed_commit() { + let mut s = black_white_indexed(false); + let params = json!({"points": [[4, 8], [12, 8]], "size": 7, "hardness": 0.0, "color": "#ff0000", "opacity": 0.6, "smoothing": 0.0, "seed": 1}); + let preview = crate::brush_cmds::LiveStroke::begin(&s, ¶ms).unwrap(); + let table = doc(&s).color_table.clone().unwrap(); + assert!(colours(&preview.doc).iter().all(|c| table.colors.contains(c)), "preview off the table: {:?}", colours(&preview.doc)); + s.execute("paint.stroke", params).unwrap(); + let (live, committed) = (preview.doc.layers[0].surface().unwrap(), doc(&s).layers[0].surface().unwrap()); + for (x, y) in (0..16).flat_map(|y| (0..16).map(move |x| (x, y))) { + assert_eq!(live.rgba(x, y), committed.rgba(x, y), "preview/commit at ({x},{y})"); + } +} From 33e8eedde878b007aecac7446d4c2fba1ed3cf85 Mon Sep 17 00:00:00 2001 From: xwize Date: Fri, 23 Oct 2026 10:18:39 +0100 Subject: [PATCH 2/5] Contributors: credit xwize as James Jones Co-Authored-By: Claude Opus 5.5 --- contributors/people.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contributors/people.toml b/contributors/people.toml index 30077a230f6..e78d134864c 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -251,3 +251,6 @@ sync_github_name = true [people.benandez] real_name = "Martin Ball" + +[people.xwize] +real_name = "James Jones" From d73604860ed1cd00db4c26becfbfd806dfdc9af3 Mon Sep 17 00:00:00 2001 From: xwize Date: Fri, 23 Oct 2026 10:33:48 +0100 Subject: [PATCH 3/5] Indexed Color: remap only the pixels a stroke changed, a tile at a time Review follow-up. The palette step quantised the whole stroke damage rectangle as one dense buffer. A long diagonal stroke on a large canvas has a rectangle near the size of the document, so this allocated about 28 bytes per pixel of it. It also remapped pixels inside that rectangle the brush never touched, which can already be off the table (Paint Bucket doesn't snap yet). keep_in_color_table now walks the rectangle tile by tile and maps only the pixels that differ from the surface before the stroke (a cheap clone: tiles are shared until written). The live preview compares against its own pre-stroke surface the same way. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/brush_cmds.rs | 7 +++-- crates/engine/src/mode_cmds.rs | 44 ++++++++++++++++++++-------- crates/engine/src/mode_cmds/tests.rs | 18 ++++++++++++ 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/brush_cmds.rs b/crates/engine/src/brush_cmds.rs index 18024bbd3d7..102dc7de043 100644 --- a/crates/engine/src/brush_cmds.rs +++ b/crates/engine/src/brush_cmds.rs @@ -300,6 +300,7 @@ fn stroke_with(s: &mut Session, cmd: &str, p: &Value, label: &str, brush: BrushS if auto_erase { apply_auto_erase(&mut brush, surf, pts.first(), fg, bg); } + let unpainted = table.as_ref().map(|_| surf.clone()); let damage = if let Some(axis) = &symmetry { let reflected = axis.reflected_passes(&pts); let copies: Vec<_> = reflected @@ -324,8 +325,8 @@ fn stroke_with(s: &mut Session, cmd: &str, p: &Value, label: &str, brush: BrushS } else { render_stroke(surf, &brush, &pts, sel.as_ref(), lock, zoom) }; - if let Some(table) = &table { - crate::mode_cmds::keep_in_color_table(table, surf, damage); + if let (Some(table), Some(pre)) = (&table, &unpainted) { + crate::mode_cmds::keep_in_color_table(table, pre, surf, damage); } Ok(damage) }) @@ -637,7 +638,7 @@ impl LiveStroke { // The preview shows the pixels the commit keeps (see `stroke_with`). if let Some(table) = crate::mode_cmds::paint_color_table(&self.doc, &self.params) { let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.layer, &self.params)?; - crate::mode_cmds::keep_in_color_table(&table, surf, dmg); + crate::mode_cmds::keep_in_color_table(&table, &self.pre, surf, dmg); } Ok(dmg) } diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rs index 87954e437bb..77738bfc670 100644 --- a/crates/engine/src/mode_cmds.rs +++ b/crates/engine/src/mode_cmds.rs @@ -398,23 +398,43 @@ pub(crate) fn paint_color_table(doc: &Document, p: &Value) -> Option doc.color_table.clone().filter(|_| pixels && doc.mode == ColorMode::Indexed) } -/// Map the pixels of `r` to their nearest colour-table entry, as the Indexed Color conversion -/// does with no dither, so painting never leaves the table (#8669). -pub(crate) fn keep_in_color_table(table: &ColorTable, surf: &mut Surface, r: Rect) { - let r = r.intersect(&surf.tile_bounds()); - if r.is_empty() || table.colors.is_empty() { +/// Map the pixels of `r` that the stroke changed (they differ from `pre`) to their nearest +/// colour-table entry, as the Indexed Color conversion does with no dither, so painting never +/// leaves the table (#8669). One tile at a time: a long stroke's bounds can span the canvas. +pub(crate) fn keep_in_color_table(table: &ColorTable, pre: &Surface, surf: &mut Surface, r: Rect) { + if table.colors.is_empty() { return; } let fmt = surf.format(); - let mut data = surf.read_region(r); - let mut px: Vec<[f32; 4]> = data.chunks_exact(fmt.channels()).map(|q| photocraft_raster::to_rgba(&fmt, q)).collect(); - quantize::quantize(&mut px, r.width() as usize, &table.colors, Dither::None, 0.0, table.transparent.map(usize::from)); + let n = fmt.channels(); let mut enc = [0.0f32; 8]; - for (q, c) in data.chunks_exact_mut(fmt.channels()).zip(px) { - photocraft_raster::from_rgba_into(&fmt, c, &mut enc); - q.iter_mut().zip(enc).for_each(|(d, v)| *d = v); + for tc in r.tiles() { + let t = tc.rect().intersect(&r); + if t.is_empty() || !surf.has_tiles_in(t) { + continue; + } + let (before, mut data) = (pre.read_region(t), surf.read_region(t)); + let (mut changed, mut px) = (Vec::new(), Vec::new()); + for (i, (q, b)) in data.chunks_exact(n).zip(before.chunks_exact(n)).enumerate() { + if q != b { + changed.push(i); + px.push(photocraft_raster::to_rgba(&fmt, q)); + } + } + if px.is_empty() { + continue; + } + let w = px.len(); + quantize::quantize(&mut px, w, &table.colors, Dither::None, 0.0, table.transparent.map(usize::from)); + let mut mapped = changed.into_iter().zip(px).peekable(); + for (i, q) in data.chunks_exact_mut(n).enumerate() { + if let Some((_, c)) = mapped.next_if(|(j, _)| *j == i) { + photocraft_raster::from_rgba_into(&fmt, c, &mut enc); + q.iter_mut().zip(enc).for_each(|(d, v)| *d = v); + } + } + surf.write_region(t, &data); } - surf.write_region(r, &data); } // ---------- Bitmap ---------- diff --git a/crates/engine/src/mode_cmds/tests.rs b/crates/engine/src/mode_cmds/tests.rs index a9ec8cca72c..cf6ae1f7931 100644 --- a/crates/engine/src/mode_cmds/tests.rs +++ b/crates/engine/src/mode_cmds/tests.rs @@ -757,3 +757,21 @@ fn live_brush_preview_matches_indexed_commit() { assert_eq!(live.rgba(x, y), committed.rgba(x, y), "preview/commit at ({x},{y})"); } } + +#[test] +fn indexed_brush_stroke_leaves_pixels_it_did_not_paint() { + let mut s = black_white_indexed(false); + // An off-table pixel inside the stroke's bounds but away from its dabs (Paint Bucket can + // still leave one) is not the stroke's to remap. + s.edit("setup", |doc, active| { + let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + let px = photocraft_raster::from_rgba(&surf.format(), [1.0, 0.0, 0.0, 1.0]); + surf.write_region(Rect::new(13, 2, 14, 3), &px); + Ok(()) + }) + .unwrap(); + s.execute("paint.stroke", json!({"points": [[2, 2], [14, 14]], "size": 2, "hardness": 1.0, "color": "#ff0000", "smoothing": 0.0})).unwrap(); + let surf = doc(&s).layers[0].surface().unwrap(); + assert_eq!(surf.rgba(13, 2), [1.0, 0.0, 0.0, 1.0]); + assert_eq!(surf.rgba(8, 8), [0.0, 0.0, 0.0, 1.0]); +} From 68ca48ddebf62317ffa64d4e423a0fbaa66d4965 Mon Sep 17 00:00:00 2001 From: xwize Date: Fri, 23 Oct 2026 10:46:57 +0100 Subject: [PATCH 4/5] Indexed Color: Pencil Auto Erase compares against the painted colour Review follow-up. In an Indexed document an off-table foreground paints as its nearest table entry. Auto Erase still compared the stroke's start pixel with the raw foreground, so a second stroke that started on what the first one painted never switched to the background colour. Both the commit and the live preview now compare against the foreground as the table paints it. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/brush_cmds.rs | 4 ++++ crates/engine/src/mode_cmds.rs | 10 ++++++++++ crates/engine/src/mode_cmds/tests.rs | 13 +++++++++++++ 3 files changed, 27 insertions(+) diff --git a/crates/engine/src/brush_cmds.rs b/crates/engine/src/brush_cmds.rs index 102dc7de043..ea097f73508 100644 --- a/crates/engine/src/brush_cmds.rs +++ b/crates/engine/src/brush_cmds.rs @@ -298,6 +298,8 @@ fn stroke_with(s: &mut Session, cmd: &str, p: &Value, label: &str, brush: BrushS let mut brush = brush; erase_locked(&mut brush, lock, bg); if auto_erase { + // A stroke in the table's colour starts on what the foreground painted as. + let fg = table.as_ref().map_or(fg, |t| crate::mode_cmds::table_color(t, fg)); apply_auto_erase(&mut brush, surf, pts.first(), fg, bg); } let unpainted = table.as_ref().map(|_| surf.clone()); @@ -521,9 +523,11 @@ impl LiveStroke { let base = (st.doc.id, st.revision); let mut doc = (*st.doc).clone(); let sel = doc.selection.clone(); + let table = crate::mode_cmds::paint_color_table(&doc, p); let (surf, lock) = crate::channel_cmds::target_surface(&mut doc, layer, p)?; erase_locked(&mut brush, lock, s.tools.background); if pencil && flag(p, "autoErase", false) { + let fg = table.as_ref().map_or(fg, |t| crate::mode_cmds::table_color(t, fg)); apply_auto_erase(&mut brush, surf, pts.first(), fg, s.tools.background); } let renderer = StrokeRenderer::new(&brush, Some(surf.format()), zoom); diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rs index 77738bfc670..2da2e67eca3 100644 --- a/crates/engine/src/mode_cmds.rs +++ b/crates/engine/src/mode_cmds.rs @@ -398,6 +398,16 @@ pub(crate) fn paint_color_table(doc: &Document, p: &Value) -> Option doc.color_table.clone().filter(|_| pixels && doc.mode == ColorMode::Indexed) } +/// The colour `c` paints as in a document with `table` (see [`keep_in_color_table`]). +pub(crate) fn table_color(table: &ColorTable, c: [f32; 4]) -> [f32; 4] { + let mut px = [c]; + if !table.colors.is_empty() { + quantize::quantize(&mut px, 1, &table.colors, Dither::None, 0.0, table.transparent.map(usize::from)); + } + let [c] = px; + c +} + /// Map the pixels of `r` that the stroke changed (they differ from `pre`) to their nearest /// colour-table entry, as the Indexed Color conversion does with no dither, so painting never /// leaves the table (#8669). One tile at a time: a long stroke's bounds can span the canvas. diff --git a/crates/engine/src/mode_cmds/tests.rs b/crates/engine/src/mode_cmds/tests.rs index cf6ae1f7931..df672c6a3e4 100644 --- a/crates/engine/src/mode_cmds/tests.rs +++ b/crates/engine/src/mode_cmds/tests.rs @@ -775,3 +775,16 @@ fn indexed_brush_stroke_leaves_pixels_it_did_not_paint() { assert_eq!(surf.rgba(13, 2), [1.0, 0.0, 0.0, 1.0]); assert_eq!(surf.rgba(8, 8), [0.0, 0.0, 0.0, 1.0]); } + +#[test] +fn pencil_auto_erase_knows_the_indexed_foreground() { + let mut s = black_white_indexed(false); + let pencil = json!({"points": [[8, 8]], "size": 3, "color": "#ff0000", "autoErase": true}); + s.execute("paint.pencil", pencil.clone()).unwrap(); + assert_eq!(doc(&s).layers[0].surface().unwrap().rgba(8, 8), [0.0, 0.0, 0.0, 1.0]); + // Red painted as black, so a stroke starting there auto-erases to the (white) background. + let preview = crate::brush_cmds::LiveStroke::begin_with(&s, "paint.pencil", &pencil).unwrap(); + assert_eq!(preview.doc.layers[0].surface().unwrap().rgba(8, 8), [1.0, 1.0, 1.0, 1.0]); + s.execute("paint.pencil", pencil).unwrap(); + assert_eq!(doc(&s).layers[0].surface().unwrap().rgba(8, 8), [1.0, 1.0, 1.0, 1.0]); +} From ba22b57e83f582668cfd1b4be66a69e7bc7d5414 Mon Sep 17 00:00:00 2001 From: xwize Date: Fri, 23 Oct 2026 10:50:41 +0100 Subject: [PATCH 5/5] Contributors: drop the people.toml entry from this PR It isn't part of the Indexed Color fix; the credit can come separately. Co-Authored-By: Claude Opus 5.5 --- contributors/people.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/contributors/people.toml b/contributors/people.toml index e78d134864c..30077a230f6 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -251,6 +251,3 @@ sync_github_name = true [people.benandez] real_name = "Martin Ball" - -[people.xwize] -real_name = "James Jones"