mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge PR #8823 into integration batch
This commit is contained in:
commit
aecd686fef
3 files changed
+166
-3
No files matched your search
@@ -293,12 +293,16 @@ 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);
|
||||
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());
|
||||
let damage = if let Some(axis) = &symmetry {
|
||||
let reflected = axis.reflected_passes(&pts);
|
||||
let copies: Vec<_> = reflected
|
||||
@@ -323,6 +327,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), Some(pre)) = (&table, &unpainted) {
|
||||
crate::mode_cmds::keep_in_color_table(table, pre, surf, damage);
|
||||
}
|
||||
Ok(damage)
|
||||
})
|
||||
})?;
|
||||
@@ -516,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);
|
||||
@@ -583,7 +592,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 +638,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, &self.pre, 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,62 @@ 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)
|
||||
}
|
||||
|
||||
/// 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.
|
||||
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 n = fmt.channels();
|
||||
let mut enc = [0.0f32; 8];
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- Bitmap ----------
|
||||
|
||||
/// Bitmap's Output resolution range (ppi): what a new document may have.
|
||||
|
||||
@@ -696,3 +696,95 @@ 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})");
|
||||
}
|
||||
}
|
||||
|
||||
#[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]);
|
||||
}
|
||||
|
||||
#[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]);
|
||||
}
|
||||
Reference in new issue
Block a user