mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 09:26:43 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
ff47388101
commit
e22984536c
3 files changed
+100
-3
No files matched your search
@@ -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<Rect> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Value> {
|
||||
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<ColorTable> {
|
||||
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.
|
||||
|
||||
@@ -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})");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user