Fill: keep Dissolve's colour alpha and shared tiles in Indexed fills (#17472)

The table snap of a one-colour fill keeps the colour's alpha, so Dissolve
still takes it as its odds when the table has a transparent entry. The
per-pixel table step writes a tile back only when the mapping changes a
sample, so a whole-canvas fill keeps its shared solid tiles.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-11-30 02:31:41 +01:00
1 parent c9ab6f5c4b
commit 8e07f4b5f5
3 files changed
+59 -6

No files matched your search

+10 -2
View File
@@ -230,13 +230,21 @@ pub fn keep_in_color_table(table: &photocraft_doc::ColorTable, pre: &Surface, su
let w = px.len();
quantize(&mut px, w, &table.colors, Dither::None, 0.0, table.transparent.map(usize::from));
let mut mapped = changed.into_iter().zip(px).peekable();
let mut moved = false;
for (i, q) in data.chunks_exact_mut(n).enumerate() {
if let Some((_, c)) = mapped.next_if(|(j, _)| *j == i) {
let m = from_rgba_into(&fmt, c, &mut enc);
q.iter_mut().zip(enc.iter().take(m)).for_each(|(d, v)| *d = *v);
for (d, v) in q.iter_mut().zip(enc.iter().take(m)) {
moved |= (*d - *v).abs() > 1e-6;
*d = *v;
}
}
}
surf.write_region(t, &data);
// Already table colours (a snapped fill colour's shared solid tiles): writing them back
// would copy every shared tile for nothing.
if moved {
surf.write_region(t, &data);
}
}
}
+5 -4
View File
@@ -187,12 +187,13 @@ fn indexed_table(doc: &photocraft_doc::Document, p: &Value) -> Option<photocraft
doc.color_table.clone().filter(|t| pixels && doc.mode == ColorMode::Indexed && !t.colors.is_empty())
}
/// `c` as its nearest entry of `table` (the transparent entry for a mostly transparent `c`).
/// `c`'s colour as its nearest entry of `table`, keeping `c`'s alpha: Dissolve takes it as its odds,
/// and the per-pixel table step maps what a partly transparent colour blends to.
fn table_color(table: &photocraft_doc::ColorTable, c: [f32; 4]) -> [f32; 4] {
let mut px = [c];
let mut px = [[c[0], c[1], c[2], 1.0]];
photocraft_algo::quantize::quantize(&mut px, 1, &table.colors, photocraft_algo::quantize::Dither::None, 0.0, table.transparent.map(usize::from));
let [c] = px;
c
let [[r, g, b, _]] = px;
[r, g, b, c[3]]
}
/// A fill colour: the inks it was picked as when it was picked as CMYK (#4196), else plain sRGB.
@@ -178,3 +178,47 @@ fn other_colour_modes_still_blend_and_keep_the_colour() {
let c = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(10, 10);
assert_eq!([0, 1, 2].map(|i| (c[i] * 255.0).round() as u8), [200, 30, 30], "an RGB fill keeps its colour");
}
#[test]
fn dissolve_takes_a_partly_transparent_colour_alpha_as_its_odds() {
// A table with a transparent entry, which maps a mostly transparent colour to transparency.
let share = |alpha: f64| {
let mut s = crate::session();
s.execute("file.new", json!({"width": 200, "height": 100, "background": "transparent"})).unwrap();
s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap();
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 10, "height": 10, "antiAlias": false})).unwrap();
s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
s.execute("select.rect", json!({"x": 190, "y": 0, "width": 10, "height": 10, "antiAlias": false})).unwrap();
s.execute("edit.clear", json!({})).unwrap();
s.execute("image.mode.indexedColor", json!({"palette": "exact", "dither": "none", "transparency": true})).unwrap();
assert!(s.active().unwrap().doc.color_table.as_ref().is_some_and(|t| t.transparent.is_some()), "the table has a transparent entry");
select(&mut s);
s.execute(CMD, json!({"contents": "color", "color": [1.0, 0.0, 0.0, alpha], "mode": "dissolve"})).unwrap();
let inside: Vec<[u8; 3]> = pixels(&s).into_iter().filter(|((x, y), _)| SEL.contains(*x, *y)).map(|(_, c)| c).collect();
assert!(inside.iter().all(|c| *c == RED || *c == BLUE), "alpha {alpha}: every pixel is the snapped fill or untouched");
inside.iter().filter(|c| **c == RED).count() as f64 / inside.len() as f64
};
let (quarter, three_quarters) = (share(0.25), share(0.75));
assert!(quarter > 0.1 && quarter < 0.4, "alpha 0.25 paints about a quarter, got {quarter}");
assert!(three_quarters > 0.6 && three_quarters < 0.9, "alpha 0.75 paints about three quarters, got {three_quarters}");
}
#[test]
fn a_whole_canvas_fill_keeps_its_shared_solid_tiles() {
// Two colours in the table, then black over the white 512 × 512 canvas: every 256 px tile is
// the fill's one shared tile, and the table step doesn't copy it per tile.
let mut s = crate::session();
s.execute("file.new", json!({"width": 512, "height": 512, "background": "white"})).unwrap();
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 10, "height": 10, "antiAlias": false})).unwrap();
s.execute("edit.fill", json!({"color": "#000000"})).unwrap();
s.execute("image.mode.indexedColor", json!({"palette": "exact", "dither": "none", "transparency": false})).unwrap();
s.execute("select.deselect", json!({})).unwrap();
// The shared tile is for a layer whose transparency may change, not the Background.
s.execute("layer.new.layer", json!({})).unwrap();
s.execute(CMD, json!({"contents": "black"})).unwrap();
let st = s.active().unwrap();
let surf = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap();
let tiles: Vec<_> = surf.tiles().map(|(_, t)| t).collect();
assert_eq!(tiles.len(), 4);
assert!(tiles.iter().all(|t| std::sync::Arc::ptr_eq(t, tiles[0])), "the filled tiles share one buffer");
}