PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Color Table: keep the transparent index inside the palette (#4186) (#4187)
David Mann6c0dcf428b3f
diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rsindex 596530349..319a6e533 100644--- a/crates/engine/src/mode_cmds.rs+++ b/crates/engine/src/mode_cmds.rs@@ -321,8 +321,14 @@ fn color_table(s: &mut Session, p: &Value) -> Result<Value> {}match p.get("transparent") {Some(Value::Null) => table.transparent = None,- Some(v) => table.transparent = v.as_u64().filter(|&i| (i as usize) < table.colors.len()).map(|i| i as u8).or(table.transparent),- None => {}+ Some(v) => {+ let len = table.colors.len();+ let i =+ v.as_u64().filter(|&i| i < len as u64).ok_or_else(|| bad(CMD, format!("transparent: an index from 0 to {} or null", len.saturating_sub(1))))?;+ table.transparent = u8::try_from(i).ok();+ }+ // A shrunk table can't keep a transparent entry past its end (#4186).+ None => table.transparent = table.transparent.filter(|&i| usize::from(i) < table.colors.len()),}let report = |t: &ColorTable| json!({"colors": t.colors.iter().map(|c| hex(*c)).collect::<Vec<_>>(), "transparent": t.transparent});if table == cur {diff --git a/crates/engine/src/mode_cmds/tests.rs b/crates/engine/src/mode_cmds/tests.rsindex 1fbed4018..8ac4e6d33 100644--- a/crates/engine/src/mode_cmds/tests.rs+++ b/crates/engine/src/mode_cmds/tests.rs@@ -409,3 +409,66 @@ fn duotone_to_rgb_inks_fill_layer_colours() {assert!((g - w).abs() < 0.02, "{c:?}");}}++/// Three-entry Indexed doc (red, blue, transparent at index 2) from an RGB doc with clear pixels.+fn indexed_with_transparency() -> Session {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 8, "height": 8, "background": "transparent"})).unwrap();+ paint(&mut s, |x, _| {+ if x < 3 {+ [1.0, 0.0, 0.0, 1.0]+ } else if x < 6 {+ [0.0, 0.0, 1.0, 1.0]+ } else {+ [0.0; 4]+ }+ });+ s.execute("image.mode.indexedColor", json!({"palette": "exact", "colors": 4, "forced": "none", "dither": "none"})).unwrap();+ assert_eq!(doc(&s).color_table.as_ref().unwrap().transparent, Some(2));+ s+}++#[test]+fn shrinking_the_color_table_drops_a_transparent_index_past_its_end() {+ // #4186: the transparent index stayed at 2 after the table shrank to 2 entries, so the palette+ // PNG export refused the table.+ let mut s = indexed_with_transparency();+ s.execute("image.mode.colorTable", json!({"colors": ["#000000", "#ffffff"]})).unwrap();+ let d = doc(&s);+ let t = d.color_table.as_ref().unwrap();+ assert_eq!(t.colors.len(), 2);+ assert!(t.transparent.is_none_or(|i| usize::from(i) < t.colors.len()), "stale transparent index {:?}", t.transparent);+ // The pixels end up exactly as with an explicit `"transparent": null`.+ let mut cleared = indexed_with_transparency();+ cleared.execute("image.mode.colorTable", json!({"colors": ["#000000", "#ffffff"], "transparent": null})).unwrap();+ assert_eq!(doc(&cleared).color_table.as_ref(), Some(t));+ let (a, b) = (d.layers[0].surface().unwrap(), doc(&cleared).layers[0].surface().unwrap());+ for y in 0..8 {+ for x in 0..8 {+ assert_eq!(a.rgba(x, y), b.rgba(x, y), "({x},{y})");+ }+ }+ let png = photocraft_io::export(d, "png", &Default::default()).expect("palette PNG export");+ assert!(png.warnings.iter().any(|w| w.contains("palette PNG")), "{:?}", png.warnings);+ // Undo restores the transparent entry.+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(doc(&s).color_table.as_ref().unwrap().transparent, Some(2));+}++#[test]+fn color_table_rejects_an_invalid_transparent_index_without_editing() {+ let mut s = indexed_with_transparency();+ let before = doc(&s).color_table.clone();+ let n = s.active().unwrap().history.past_len();+ for t in [json!(999), json!(3), json!(-1), json!(1.5), json!("0"), json!(true)] {+ assert!(s.execute("image.mode.colorTable", json!({"transparent": t})).is_err(), "{t}");+ // Validated against the new colours, before any edit.+ assert!(s.execute("image.mode.colorTable", json!({"colors": ["#000000", "#ffffff"], "transparent": t})).is_err(), "{t}");+ }+ assert!(s.execute("image.mode.colorTable", json!({"colors": ["#000000", "#ffffff"], "transparent": 2})).is_err());+ assert_eq!(doc(&s).color_table, before);+ assert_eq!(s.active().unwrap().history.past_len(), n);+ // A valid index still applies.+ s.execute("image.mode.colorTable", json!({"transparent": 0})).unwrap();+ assert_eq!(doc(&s).color_table.as_ref().unwrap().transparent, Some(0));+}