mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge PR #15840: fix(engine): reserve transparency in Exact indexed conversion
This commit is contained in:
commit
f17f0de50c
3 files changed
+126
No files matched your search
@@ -435,6 +435,10 @@ fn indexed_color(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
let has_clear = transparency && px.iter().any(|q| q[3] < 0.5);
|
||||
let mut pal = quantize::build_palette(&px, kind, if has_clear { colors.saturating_sub(1).max(2) } else { colors }, forced).map_err(|e| bad(CMD, e))?;
|
||||
// Exact must reserve one palette slot for clear pixels.
|
||||
if kind == PaletteKind::Exact && has_clear && pal.len() >= 256 {
|
||||
return Err(bad(CMD, "Exact with transparency needs 255 or fewer opaque palette colours plus one transparent entry"));
|
||||
}
|
||||
let transparent = if has_clear && pal.len() < 256 {
|
||||
pal.push([255, 255, 255]);
|
||||
Some(pal.len() - 1)
|
||||
|
||||
@@ -439,6 +439,105 @@ fn indexed_matte_without_transparency_fills_clear_pixels_and_rejects_bad_input_a
|
||||
assert_eq!(doc(&s).color_table.as_ref().unwrap().transparent, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_exact_rejects_full_opaque_palette_with_clear_pixels_atomically() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 257, "height": 1, "background": "transparent"})).unwrap();
|
||||
paint(&mut s, |x, _| if x < 256 { [x as f32 / 255.0, x as f32 / 255.0, x as f32 / 255.0, 1.0] } else { [0.0; 4] });
|
||||
let before = doc(&s).layers[0].surface().unwrap().to_interleaved(doc(&s).bounds());
|
||||
let history = s.active().unwrap().history.past_len();
|
||||
let revision = s.active().unwrap().revision;
|
||||
let error = s.execute("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "dither": "none", "transparency": true})).unwrap_err();
|
||||
let message = error.to_string();
|
||||
assert!(message.contains("255") && message.contains("transparent entry"), "{message}");
|
||||
assert_eq!(doc(&s).mode, ColorMode::Rgb);
|
||||
assert!(doc(&s).color_table.is_none());
|
||||
assert_eq!(doc(&s).layers[0].surface().unwrap().to_interleaved(doc(&s).bounds()), before);
|
||||
assert_eq!(s.active().unwrap().history.past_len(), history);
|
||||
assert_eq!(s.active().unwrap().revision, revision);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_exact_reserves_transparency_only_when_needed_and_available() {
|
||||
for (opaque, clear) in [(255u32, true), (256, false)] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": opaque + u32::from(clear), "height": 1, "background": "transparent"})).unwrap();
|
||||
paint(&mut s, |x, _| if x < opaque as i32 { [x as f32 / 255.0, x as f32 / 255.0, x as f32 / 255.0, 1.0] } else { [0.0; 4] });
|
||||
let result = s.execute("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "dither": "none", "transparency": true})).unwrap();
|
||||
assert_eq!(doc(&s).mode, ColorMode::Indexed);
|
||||
let table = doc(&s).color_table.as_ref().unwrap();
|
||||
assert_eq!(table.colors.len(), 256);
|
||||
assert_eq!(table.transparent, if clear { Some(255) } else { None });
|
||||
assert_eq!(result["transparentIndex"], if clear { json!(255) } else { Value::Null });
|
||||
let surface = doc(&s).layers[0].surface().unwrap();
|
||||
for x in 0..opaque as i32 {
|
||||
let gray = x as f32 / 255.0;
|
||||
assert_eq!(surface.rgba(x, 0), [gray, gray, gray, 1.0]);
|
||||
}
|
||||
if clear {
|
||||
assert_eq!(surface.rgba(opaque as i32, 0)[3], 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_exact_failure_restores_a_floating_copy_and_redo() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 258, "height": 1, "background": "transparent"})).unwrap();
|
||||
paint(&mut s, |x, _| if x < 256 { [x as f32 / 255.0, x as f32 / 255.0, x as f32 / 255.0, 1.0] } else { [0.0; 4] });
|
||||
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 1, "height": 1})).unwrap();
|
||||
s.edit("redo marker", |doc, _| {
|
||||
doc.name.push_str(" changed");
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert!(s.undo());
|
||||
s.execute("select.float", json!({"dx": 256, "copy": true})).unwrap();
|
||||
let before = s.active().unwrap().clone();
|
||||
assert!(before.history.can_redo());
|
||||
let piece = crate::float_cmds::floating(&before).unwrap();
|
||||
let pixels = before.doc.layers[0].surface().unwrap().to_interleaved(before.doc.bounds());
|
||||
|
||||
let error = s.execute("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "dither": "none", "transparency": true})).unwrap_err();
|
||||
assert!(error.to_string().contains("255"), "{error}");
|
||||
let after = s.active().unwrap();
|
||||
let restored = crate::float_cmds::floating(after).unwrap();
|
||||
assert_eq!(after.doc.id, before.doc.id);
|
||||
assert_eq!(after.doc.mode, ColorMode::Rgb);
|
||||
assert!(std::sync::Arc::ptr_eq(&after.doc, &before.doc));
|
||||
assert_eq!(after.doc.layers[0].surface().unwrap().to_interleaved(after.doc.bounds()), pixels);
|
||||
assert_eq!((restored.layer, restored.offset, restored.revision, restored.moved), (piece.layer, piece.offset, piece.revision, piece.moved));
|
||||
assert!(std::sync::Arc::ptr_eq(&restored.parts, &piece.parts));
|
||||
assert_eq!(after.revision, before.revision);
|
||||
assert_eq!(after.history.past_len(), before.history.past_len());
|
||||
assert!(after.history.can_redo());
|
||||
assert_eq!(after.active_layer, before.active_layer);
|
||||
assert_eq!(after.selected_layers, before.selected_layers);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_exact_success_drops_the_floating_copy_before_counting_colours() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 257, "height": 1, "background": "transparent"})).unwrap();
|
||||
paint(&mut s, |x, _| if x < 256 { [x as f32 / 255.0, x as f32 / 255.0, x as f32 / 255.0, 1.0] } else { [0.0; 4] });
|
||||
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 1, "height": 1})).unwrap();
|
||||
s.execute("select.float", json!({"dx": 256, "copy": true})).unwrap();
|
||||
let history = s.active().unwrap().history.past_len();
|
||||
let result = s.execute("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "dither": "none", "transparency": true})).unwrap();
|
||||
assert_eq!(doc(&s).mode, ColorMode::Indexed);
|
||||
assert_eq!(doc(&s).color_table.as_ref().unwrap().colors.len(), 256);
|
||||
assert_eq!(result["transparentIndex"], Value::Null);
|
||||
assert!(crate::float_cmds::floating(s.active().unwrap()).is_none());
|
||||
assert_eq!(doc(&s).layers[0].surface().unwrap().rgba(256, 0), [0.0, 0.0, 0.0, 1.0]);
|
||||
assert_eq!(s.active().unwrap().history.past_len(), history + 2, "drop and conversion remain separate history steps");
|
||||
assert!(s.undo());
|
||||
assert_eq!(doc(&s).mode, ColorMode::Rgb);
|
||||
assert_eq!(doc(&s).layers[0].surface().unwrap().rgba(256, 0)[3], 1.0);
|
||||
assert!(s.undo());
|
||||
assert_eq!(doc(&s).layers[0].surface().unwrap().rgba(256, 0)[3], 0.0);
|
||||
assert_eq!(s.active().unwrap().history.past_len(), history);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indexed_exact_transparency_and_sources() {
|
||||
// Two colours + transparency: exact palette with a transparent entry.
|
||||
|
||||
@@ -1721,6 +1721,29 @@ impl Session {
|
||||
// authorization, validation, floating-selection and admission failures.
|
||||
let live_commit = self.live_commit.take();
|
||||
self.sync_scratch_state();
|
||||
// A refused Exact conversion must also undo its automatic floating-selection drop.
|
||||
let checkpoint = if id == "image.mode.indexedColor" && params.get("palette").and_then(Value::as_str) == Some("exact") {
|
||||
self.active.and_then(|index| {
|
||||
self.docs
|
||||
.get(index)
|
||||
.filter(|state| crate::float_cmds::floating(state).is_some())
|
||||
.map(|state| (index, state.clone(), self.action_history.clone()))
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let result = self.dispatch_with_commit(id, params, background, live_commit);
|
||||
if result.is_err()
|
||||
&& let Some((index, state, action_history)) = checkpoint
|
||||
&& self.docs.get(index).is_some_and(|current| current.doc.id == state.doc.id)
|
||||
{
|
||||
self.docs[index] = state;
|
||||
self.action_history = action_history;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn dispatch_with_commit(&mut self, id: &str, params: Value, background: bool, live_commit: Option<crate::brush_cmds::LiveCommit>) -> Result<Started> {
|
||||
let spec = crate::registry::spec(id)?;
|
||||
// Params are named fields: anything but an object (or null, meaning none) is a caller
|
||||
// mistake, not "use every default".
|
||||
|
||||
Reference in new issue
Block a user