mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Fill: keep the alpha of pixels an Indexed fill preserves (#17472)
With Preserve Transparency or locked transparency, the colour-table step after an Indexed Color fill snaps each changed pixel's colour as if it were opaque and keeps its alpha, instead of letting the table's transparent entry clear a mostly transparent pixel or make a mostly opaque one solid. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
7615898b7d
commit
451f70dee7
3 files changed
+45
-14
No files matched your search
@@ -204,8 +204,10 @@ pub fn blend_into(surf: &mut Surface, area: Rect, source: &Source, sel: Option<&
|
||||
}
|
||||
|
||||
/// Map the pixels of `r` that differ from `pre` to their nearest entry of `table`, with no dither,
|
||||
/// so a fill in an Indexed Color document never leaves the colour table. One tile at a time.
|
||||
pub fn keep_in_color_table(table: &photocraft_doc::ColorTable, pre: &Surface, surf: &mut Surface, r: Rect) {
|
||||
/// so a fill in an Indexed Color document never leaves the colour table. One tile at a time. With
|
||||
/// `keep_alpha` (Preserve Transparency, locked transparency) each pixel keeps its alpha and only its
|
||||
/// colour snaps, as if opaque; otherwise the table's transparent entry may take a mostly clear pixel.
|
||||
pub fn keep_in_color_table(table: &photocraft_doc::ColorTable, pre: &Surface, surf: &mut Surface, r: Rect, keep_alpha: bool) {
|
||||
use photocraft_algo::quantize::{Dither, quantize};
|
||||
if table.colors.is_empty() {
|
||||
return;
|
||||
@@ -230,7 +232,11 @@ pub fn keep_in_color_table(table: &photocraft_doc::ColorTable, pre: &Surface, su
|
||||
continue;
|
||||
}
|
||||
let w = px.len();
|
||||
let alphas: Vec<f32> = if keep_alpha { px.iter_mut().map(|c| std::mem::replace(&mut c[3], 1.0)).collect() } else { Vec::new() };
|
||||
quantize(&mut px, w, &table.colors, Dither::None, 0.0, table.transparent.map(usize::from));
|
||||
if keep_alpha {
|
||||
px.iter_mut().zip(&alphas).for_each(|(c, a)| c[3] = *a);
|
||||
}
|
||||
let mut mapped = changed.into_iter().zip(px).peekable();
|
||||
let mut moved = false;
|
||||
for (i, q) in data.chunks_exact_mut(n).enumerate() {
|
||||
|
||||
@@ -197,7 +197,7 @@ pub fn fill(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
// Patterns, history, Content-Aware and the other blend modes snap per pixel.
|
||||
if let (Some(t), Some(pre)) = (&table, &pre) {
|
||||
keep_in_color_table(t, pre, surf, area);
|
||||
keep_in_color_table(t, pre, surf, area, preserve || lock);
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
|
||||
@@ -179,20 +179,26 @@ fn other_colour_modes_still_blend_and_keep_the_colour() {
|
||||
assert_eq!([0, 1, 2].map(|i| (c[i] * 255.0).round() as u8), [200, 30, 30], "an RGB fill keeps its colour");
|
||||
}
|
||||
|
||||
/// A blue 200 × 100 Indexed document whose table has a transparent entry (which maps a mostly
|
||||
/// transparent colour to transparency) besides red and blue, with the issue's selection.
|
||||
fn indexed_with_transparent_entry() -> Session {
|
||||
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
|
||||
}
|
||||
|
||||
#[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);
|
||||
let mut s = indexed_with_transparent_entry();
|
||||
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");
|
||||
@@ -222,3 +228,22 @@ fn a_whole_canvas_fill_keeps_its_shared_solid_tiles() {
|
||||
assert_eq!(tiles.len(), 4);
|
||||
assert!(tiles.iter().all(|t| std::sync::Arc::ptr_eq(t, tiles[0])), "the filled tiles share one buffer");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserve_transparency_keeps_a_partly_transparent_pixel_alpha() {
|
||||
let mut s = indexed_with_transparent_entry();
|
||||
// Pixels a quarter and three quarters opaque inside the selection.
|
||||
let id = s.active().unwrap().active_layer.unwrap();
|
||||
for (x, a) in [(60, 0.25), (61, 0.75)] {
|
||||
let doc = std::sync::Arc::make_mut(s.active_mut().unwrap().doc_mut());
|
||||
doc.layer_mut(id).unwrap().surface_mut().unwrap().write_region(Rect::new(x, 50, x + 1, 51), &[0.0, 0.0, 1.0, a]);
|
||||
}
|
||||
s.execute(CMD, json!({"contents": "color", "color": "#ff1010", "preserveTransparency": true})).unwrap();
|
||||
let st = s.active().unwrap();
|
||||
let surf = st.doc.layer(id).unwrap().surface().unwrap();
|
||||
for (x, a) in [(60, 0.25), (61, 0.75)] {
|
||||
let c = surf.rgba(x, 50);
|
||||
assert!((c[3] - a).abs() < 0.01, "({x}, 50) keeps its alpha {a}, got {}", c[3]);
|
||||
assert_eq!([0, 1, 2].map(|i| (c[i] * 255.0).round() as u8), RED, "({x}, 50) takes the table colour");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user