From 5cd7f7f849c8039729d497a159c228d3cf912a6b Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 01:11:00 -0800 Subject: [PATCH] Save for Web: Save Color Table writes the whole image's table with no slice selected With slices set to Selected and no slice selected, Save Color Table failed with "no selected slice in the image" although the dialog's preview shows the whole image's table. Without a `slice` parameter the command no longer looks up the slices; a `slice` is still looked up among the slices `slices` names. The preview is unchanged. Docs: docs/mcp.md. --- crates/engine/src/cmd/webexport.rs | 27 ++++++++++++++++++--------- crates/engine/src/tests/webexport.rs | 23 +++++++++++++++++++++++ docs/mcp.md | 4 +++- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/webexport.rs b/crates/engine/src/cmd/webexport.rs index 0f77411cd8..8483d7d1ca 100644 --- a/crates/engine/src/cmd/webexport.rs +++ b/crates/engine/src/cmd/webexport.rs @@ -50,7 +50,7 @@ pub fn specs() -> Vec { "Save Color Table", [], None, - "{path?: the .act file (.act is added to a name without an extension), …the settings of document.exportForWeb (preset?, format?: gif|png8…), slice?: n (1-based, from slice.list; default the whole image)} write the GIF or PNG-8 color table the preview shows as a color table file: 772 bytes, the colors as written (red, green, blue) in file order, zero bytes up to 256 colors, then the number of colors and the transparent entry's index (65535 for none), two bytes each, most significant byte first → {path, colors (how many), transparentIndex (null for none), bytes, warnings}; no path → {dataBase64, colors, transparentIndex, bytes, warnings}. jpg and png24 are an error; the document and the settings do not change", + "{path?: the .act file (.act is added to a name without an extension), …the settings of document.exportForWeb (preset?, format?: gif|png8…), slice?: n (1-based, from slice.list, among the slices `slices` names; default the whole image, whatever `slices` is)} write the GIF or PNG-8 color table the preview shows as a color table file: 772 bytes, the colors as written (red, green, blue) in file order, zero bytes up to 256 colors, then the number of colors and the transparent entry's index (65535 for none), two bytes each, most significant byte first → {path, colors (how many), transparentIndex (null for none), bytes, warnings}; no path → {dataBase64, colors, transparentIndex, bytes, warnings}. jpg and png24 are an error; the document and the settings do not change", has_doc, save_color_table ), @@ -1214,7 +1214,7 @@ fn preview(s: &mut Session, p: &Value) -> Result { if !(kbps.is_finite() && kbps > 0.0) { return Err(bad(P, format!("kbps must be a positive number, not {kbps}"))); } - let Shown { o, images, warnings } = shown(s, &settings, p, P)?; + let Shown { o, images, warnings } = shown(s, &settings, p, P, true)?; let mut out = json!({ "format": settings.format.id(), "width": o.width, "height": o.height, "bytes": o.bytes.len(), "seconds": download_seconds(o.bytes.len(), kbps), "kbps": kbps, "slices": images, "warnings": warnings, @@ -1241,7 +1241,7 @@ fn preview(s: &mut Session, p: &Value) -> Result { /// The file Save writes with some settings, as the preview shows it. struct Shown { o: Optimized, - /// The images an export writes. + /// The images an export writes (0 when they were not counted). images: usize, /// The export's warnings. warnings: Vec<&'static str>, @@ -1249,8 +1249,9 @@ struct Shown { /// What Save writes with `settings` for the whole image, or for the slice `p.slice` names (1-based, /// from `slice.list`), for the command `cmd`. A `slice` that is not a whole number from 1 up is an -/// error. -fn shown(s: &mut Session, settings: &WebSettings, p: &Value, cmd: &str) -> Result { +/// error. With `count_images` false and no `slice`, the slices are not looked up: `images` is 0, +/// and selected slices with none selected are not an error. +fn shown(s: &mut Session, settings: &WebSettings, p: &Value, cmd: &str, count_images: bool) -> Result { let slice = match p.get("slice") { None | Some(Value::Null) => None, Some(v) => Some(v.as_u64().filter(|n| *n >= 1).ok_or_else(|| bad(cmd, "`slice` is a slice number from `slice.list`"))?), @@ -1261,9 +1262,17 @@ fn shown(s: &mut Session, settings: &WebSettings, p: &Value, cmd: &str) -> Resul let r = render(doc, settings).map_err(|e| bad(cmd, e))?; let warnings = undecoded(failed); let areas = doc.slice_layout(); - let selected = super::slices::selected_slices(doc, &st.selection); - let cells = cells(&areas, &selected, settings, &r).map_err(|e| bad(cmd, e))?; - let images = cells.iter().filter(|c| c.area.id.and_then(|id| doc.slice_options(id)).is_none_or(|o| o.kind == SliceKind::Image)).count().max(1); + let cells = if count_images || slice.is_some() { + let selected = super::slices::selected_slices(doc, &st.selection); + cells(&areas, &selected, settings, &r).map_err(|e| bad(cmd, e))? + } else { + vec![] + }; + let images = if count_images { + cells.iter().filter(|c| c.area.id.and_then(|id| doc.slice_options(id)).is_none_or(|o| o.kind == SliceKind::Image)).count().max(1) + } else { + 0 + }; let o = match slice { Some(n) => { let c = @@ -1371,7 +1380,7 @@ fn save_color_table(s: &mut Session, p: &Value) -> Result { let path = text_param(p, "path", P)? .filter(|p| !p.trim().is_empty()) .map(|p| if std::path::Path::new(p).extension().is_none() { format!("{p}.{COLOR_TABLE_EXT}") } else { p.to_string() }); - let Shown { o, warnings, .. } = shown(s, &settings, p, P)?; + let Shown { o, warnings, .. } = shown(s, &settings, p, P, false)?; let bytes = act_bytes(&o.colors); let mut out = json!({ "colors": o.colors.len().min(256), diff --git a/crates/engine/src/tests/webexport.rs b/crates/engine/src/tests/webexport.rs index 150ea1f67a..318acad11e 100644 --- a/crates/engine/src/tests/webexport.rs +++ b/crates/engine/src/tests/webexport.rs @@ -1101,6 +1101,29 @@ fn save_color_table_writes_the_path_and_refuses_bad_params() { let _ = std::fs::remove_dir_all(dir); } +/// With Save for Web set to the selected slices and none selected, Save Color Table still writes +/// the whole image's table, the one the dialog's preview shows. A `slice` is still looked up among +/// the selected slices. +#[test] +fn save_color_table_of_the_whole_image_needs_no_selected_slice() { + let mut s = two_colours(); + let slice = run(&mut s, "object.slice.create", json!({"x": 0, "y": 0, "width": 20, "height": 30}))["id"].as_u64().unwrap(); + run(&mut s, "object.slice.select", json!({})); + let p = json!({"format": "gif", "dither": "none", "transparency": false}); + let (_, whole) = color_table_file(&mut s, merge(&p, json!({"slices": "none"}))); + let (v, bytes) = color_table_file(&mut s, merge(&p, json!({"slices": "selected"}))); + assert_eq!(bytes, whole, "{v}"); + let number = run(&mut s, "slice.list", json!({}))["slices"] + .as_array() + .unwrap() + .iter() + .find(|l| l["id"].as_u64() == Some(slice)) + .map(|l| l["number"].clone()) + .unwrap(); + let e = s.execute("document.exportForWeb.saveColorTable", &merge(&p, json!({"slices": "selected", "slice": number}))).unwrap_err().to_string(); + assert!(e.contains("no selected slice"), "{e}"); +} + /// The preview refuses a `slice` that is not a slice number instead of previewing the whole image. #[test] fn preview_refuses_a_slice_that_is_not_a_number() { diff --git a/docs/mcp.md b/docs/mcp.md index 29b05a8090..a037c2b25b 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -10573,7 +10573,9 @@ the GIF or PNG-8 color table the preview shows as a color table file (`.act`) in specification gives (https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/): 256 entries of three bytes (red, green, blue) in file order, with zero bytes after the colors used, then the number of colors used and the index of the transparent entry, two bytes each with the most significant byte first. The file is always 772 bytes, and the index is -65535 when the table has no transparent entry. A color shifted by hand is written as its new color. `.act` is added to +65535 when the table has no transparent entry. A color shifted by hand is written as its new color. Without `slice` +the file holds the whole image's table, also when `slices` is `selected` and no slice is selected; a `slice` is looked +up among the slices `slices` names. `.act` is added to a `path` without an extension, and without `path` the file comes back as `dataBase64`. The reply is `{path, colors, transparentIndex, bytes, warnings}` (`transparentIndex` is null when there is none). JPEG and PNG-24 are errors, and the document and the settings do not change. Load Color Table… is `webExport.loadColorTable {path | dataBase64,