From 524d608b50f933ec0cc3b5a7cd4d2a97038eca56 Mon Sep 17 00:00:00 2001 From: Matthias Schlich Date: Wed, 14 Oct 2026 21:13:20 +0200 Subject: [PATCH] feat(psd): editable Filter Gallery smart filters (Chrome) (#3426) * feat(psd): map Filter Gallery smart filters to filter.filterGallery (Chrome) A PSD smart filter from Filter > Filter Gallery (`GEfc`) was kept verbatim as psd.unsupportedFilter, so it could not be edited. Its effect stack (`GEfs` list of `GEfc` effects with `GEfk` effect enum and `GELv` visibility) now maps to `filter.filterGallery {effects, foreground, background}` when every effect and setting is modelled, and is written back as `GEfc` on export. Layout taken from a Photopea-authored PSD. The table starts with Chrome (`Chrm`: `Dtl ` detail, `Smth` smoothness). Stacks with other effects or unknown settings stay verbatim as before. * feat(smart-filters): edit Filter Gallery smart filters in the gallery dialog Double-clicking a Filter Gallery smart filter in the Layers panel did nothing. It now opens the Filter Gallery dialog with the recorded effect stack and colours, previewing on the pixels below that filter; OK updates the filter in place with layer.smartFilter.setParams (one history step). * fix(psd): read and write Photoshop's inline Filter Gallery effect Photoshop 27 writes a Filter Gallery smart filter with a single effect inline in `Fltr` (`GEfc { GEfk, }`) rather than as a `GEfs` list, so such files still opened as psd.unsupportedFilter. Both layouts are read now. A single visible effect is written inline as Photoshop does; several or hidden effects keep the list, which is the only layout with a visibility flag (`GELv`). --------- Co-authored-by: Brandon Thomas Co-authored-by: Claude Opus 5.5 --- crates/io/src/smart_map.rs | 306 +++++++++++++++++++++++++++++++ crates/ui-egui/src/gallery_ui.rs | 125 +++++++++++-- crates/ui-egui/src/smart_ui.rs | 8 + docs/file-format-parity.md | 3 +- 4 files changed, 426 insertions(+), 16 deletions(-) diff --git a/crates/io/src/smart_map.rs b/crates/io/src/smart_map.rs index ab9349fbde..6eeda61a13 100644 --- a/crates/io/src/smart_map.rs +++ b/crates/io/src/smart_map.rs @@ -289,6 +289,153 @@ fn known_params(class: &str, f: Option<&Descriptor>) -> Option<(&'static str, J) Some((k.command, params)) } +// ---------- Filter Gallery ---------- +// +// Filter › Filter Gallery stores its effect stack in a `GEfc` filter (filterID `GEfc`). Photoshop +// writes a single effect inline; Photopea writes a list (also for one effect): +// +// ```text +// Fltr = GEfc { GEfk = GEft enum (the effect), } (Photoshop) +// Fltr = GEfc { GEfs = [ GEfc { , GEfk = GEft enum, GELv = visible } ] } (Photopea) +// ``` +// +// The list is in application order, like `filter.filterGallery`'s `effects`. The item's +// `FrgC`/`BckC` are the colours the Sketch effects read. Only effects whose settings we know are +// mapped; a stack with any other effect (or setting) stays an [`UNSUPPORTED_FILTER`]. + +const GALLERY_CLASS: &str = "GEfc"; +const GALLERY_COMMAND: &str = "filter.filterGallery"; +const GALLERY_NAME: &str = "Filter Gallery..."; + +/// One gallery effect: our filter key, Photoshop's `GEft` code and its settings (PSD key, our key). +struct GalleryCode { + key: &'static str, + code: &'static str, + settings: &'static [(&'static str, &'static str)], +} + +const GALLERY_EFFECTS: [GalleryCode; 1] = [GalleryCode { key: "chrome", code: "Chrm", settings: &[("Dtl ", "detail"), ("Smth", "smoothness")] }]; + +/// An `RGBC` descriptor (0–255 channels) as straight `[r, g, b, a]`, 0–1. +fn rgbc_colour(d: &Descriptor) -> Option { + let c = |k: &str| px(d, k).map(|v| (v / 255.0).clamp(0.0, 1.0)); + Some(json!([c("Rd ")?, c("Grn ")?, c("Bl ")?, 1.0])) +} + +/// `filter.filterGallery` params for a `GEfc` filter item; `None` when an effect or setting is +/// not modelled. +fn gallery_params(item: &Descriptor, f: &Descriptor) -> Option { + let list: Vec<&Descriptor> = match f.get("GEfs") { + Some(Value::List(list)) => list + .iter() + .map(|v| match v { + Value::Descriptor(e) => Some(e), + _ => None, + }) + .collect::>()?, + Some(_) => return None, + None if f.get("GEfk").is_some() => vec![f], + None => return None, + }; + let mut effects = Vec::with_capacity(list.len()); + for e in list { + let code = enum_of(e, "GEfk")?; + let g = GALLERY_EFFECTS.iter().find(|g| g.code.as_bytes() == code)?; + let mut params = Map::new(); + for (k, v) in &e.items { + if k.is("GEfk") || k.is("GELv") { + continue; + } + let (_, ours) = g.settings.iter().find(|(psd, _)| k.is(psd))?; + params.insert((*ours).into(), json!(int_value(v)?)); + } + effects.push(json!({"filter": g.key, "params": params, "visible": bool_or(e, "GELv", true)})); + } + if effects.is_empty() { + return None; + } + let mut out = Map::new(); + out.insert("effects".into(), J::Array(effects)); + for (psd, ours) in [("FrgC", "foreground"), ("BckC", "background")] { + if let Some(c) = get_desc(item, psd).and_then(rgbc_colour) { + out.insert(ours.into(), c); + } + } + Some(J::Object(out)) +} + +fn colour_rgb(v: &J) -> Option<[f64; 3]> { + match v { + J::Array(a) => Some([a.first()?.as_f64()?, a.get(1)?.as_f64()?, a.get(2)?.as_f64()?]), + J::String(h) if h.len() == 7 && h.starts_with('#') => { + let ch = |j: usize| h.get(j..j + 2).and_then(|s| u8::from_str_radix(s, 16).ok()).map(|v| f64::from(v) / 255.0); + Some([ch(1)?, ch(3)?, ch(5)?]) + } + _ => None, + } +} + +/// A `[r, g, b(, a)]` (0–1) or `#rrggbb` colour param as an `RGBC` descriptor (0–255). +fn colour_rgbc(v: Option<&J>, default: f64) -> Result { + let Some(v) = v else { return Ok(rgbc(default)) }; + let rgb = colour_rgb(v).filter(|c| c.iter().all(|v| v.is_finite())).ok_or("a colour is not [r, g, b] or #rrggbb")?; + let c = |v: f64| Value::Double(v.clamp(0.0, 1.0) * 255.0); + Ok(Value::Descriptor(Descriptor::new("RGBC").with("Rd ", c(rgb[0])).with("Grn ", c(rgb[1])).with("Bl ", c(rgb[2])))) +} + +/// The `filterFXList` item for a `filter.filterGallery` smart filter. +fn gallery_item(f: &SmartFilter) -> Result { + let p = &f.params; + if let Some(k) = p.as_object().and_then(|o| o.keys().find(|k| !is_meta(k) && !["effects", "foreground", "background"].contains(&k.as_str()))) { + return Err(format!("setting `{k}` has no PSD equivalent")); + } + let list = p.get("effects").and_then(J::as_array).ok_or("it has no effects")?; + let mut out = Vec::with_capacity(list.len()); + for e in list { + let key = e.get("filter").and_then(J::as_str).ok_or("an effect has no `filter`")?; + let g = GALLERY_EFFECTS.iter().find(|g| g.key == key).ok_or_else(|| format!("gallery effect `{key}` has no PSD equivalent"))?; + let params = e.get("params").filter(|v| v.is_object()).unwrap_or(e); + if let Some(k) = params.as_object().and_then(|o| { + o.keys().find(|k| !is_meta(k) && !["filter", "params", "visible"].contains(&k.as_str()) && !g.settings.iter().any(|(_, ours)| ours == k)) + }) { + return Err(format!("gallery setting `{key}.{k}` has no PSD equivalent")); + } + let defaults = photocraft_algo::GalleryFilter::from_key(key).map(photocraft_algo::GalleryEffect::new); + let settings: Vec<(&str, Value)> = + g.settings.iter().map(|(psd, ours)| (*psd, pi(params, ours, defaults.as_ref().map_or(0.0, |x| f64::from(x.get(ours)))))).collect(); + out.push((g.code, settings, e.get("visible").and_then(J::as_bool).unwrap_or(true))); + } + let gallery = || { + let mut d = Descriptor::new(GALLERY_CLASS); + d.name = UnicodeString::new_nul("Filter Gallery"); + d + }; + // One visible effect: inline, as Photoshop writes it. Otherwise the effect list. + let fltr = match out.as_slice() { + [] => return Err("it has no effects".into()), + [(code, settings, true)] => settings.iter().fold(gallery().with("GEfk", enumv("GEft", code)), |d, (k, v)| d.with(k, v.clone())), + _ => { + let list = out + .into_iter() + .map(|(code, settings, visible)| { + let d = settings.into_iter().fold(gallery(), |d, (k, v)| d.with(k, v)); + Value::Descriptor(d.with("GEfk", enumv("GEft", code)).with("GELv", Value::Boolean(visible))) + }) + .collect(); + gallery().with("GEfs", Value::List(list)) + } + }; + Ok(Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul(GALLERY_NAME))) + .with("blendOptions", blend_options(f)) + .with("enab", Value::Boolean(f.visible)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", colour_rgbc(p.get("foreground"), 0.0)?) + .with("BckC", colour_rgbc(p.get("background"), 255.0)?) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(code(b"GEfc")))) +} + /// Lowercase hex of `bytes`. fn to_hex(bytes: &[u8]) -> String { const H: &[u8; 16] = b"0123456789abcdef"; @@ -322,6 +469,7 @@ pub fn filter_from_item(item: &Descriptor) -> SmartFilter { let known = match class.as_deref() { Some(camera_raw::CLASS) => camera_raw::import_params(item).map(|p| (camera_raw::COMMAND, p)), Some(displace::CLASS) => displace::import_params(item).map(|p| (displace::COMMAND, p)), + Some(GALLERY_CLASS) => fltr.and_then(|f| gallery_params(item, f)).map(|p| (GALLERY_COMMAND, p)), c => c.and_then(|c| known_params(c, fltr)), }; let (command, params) = match known { @@ -473,6 +621,9 @@ pub fn item_for_filter(f: &SmartFilter) -> Result { set(&mut d, "enab", Value::Boolean(f.visible)); return Ok(d); } + if f.command == GALLERY_COMMAND { + return gallery_item(f); + } if f.command == UNSUPPORTED_FILTER { let raw = f.params.get("psd").and_then(J::as_str).and_then(from_hex).ok_or("its filter data is missing")?; let mut d = Descriptor::from_bytes(&raw).map_err(|e| format!("its filter data is unreadable ({e})"))?; @@ -913,6 +1064,161 @@ mod tests { assert_eq!(filter_from_item(&odd).command, UNSUPPORTED_FILTER); } + /// A Filter Gallery item as Photopea writes it (Chrome, Detail 12, Smoothness 4). + fn gallery_item_with(effects: Vec) -> Descriptor { + let rgb = + |v: f64| Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(v)).with("Grn ", Value::Double(v)).with("Bl ", Value::Double(v))); + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Filtergalerie"))) + .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "normal")))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgb(0.0)) + .with("BckC", rgb(255.0)) + .with("Fltr", Value::Descriptor(Descriptor::new("GEfc").with("GEfs", Value::List(effects.into_iter().map(Value::Descriptor).collect())))) + .with("filterID", Value::Integer(code(b"GEfc"))) + } + + fn chrome(detail: i32, smoothness: i32, visible: bool) -> Descriptor { + Descriptor::new("GEfc") + .with("Dtl ", Value::Integer(detail)) + .with("Smth", Value::Integer(smoothness)) + .with("GEfk", enumv("GEft", "Chrm")) + .with("GELv", Value::Boolean(visible)) + } + + #[test] + fn filter_gallery_chrome_imports_as_editable_gallery() { + let f = filter_from_item(&gallery_item_with(vec![chrome(12, 4, true), chrome(3, 9, false)])); + assert_eq!(f.command, "filter.filterGallery"); + assert_eq!( + f.params, + json!({ + "effects": [ + {"filter": "chrome", "params": {"detail": 12, "smoothness": 4}, "visible": true}, + {"filter": "chrome", "params": {"detail": 3, "smoothness": 9}, "visible": false}, + ], + "foreground": [0.0, 0.0, 0.0, 1.0], + "background": [1.0, 1.0, 1.0, 1.0], + }) + ); + // Written back and read again: unchanged, also when re-blended and hidden. + let mut g = f.clone(); + g.blend = BlendMode::Multiply; + g.opacity = 0.5; + g.visible = false; + let d = item_for_filter(&g).unwrap(); + assert_eq!(filter_from_item(&Descriptor::from_bytes(&d.to_bytes()).unwrap()), g); + let fltr = get_desc(&d, "Fltr").unwrap(); + assert!(fltr.class_id.is("GEfc")); + assert_eq!(int(&d, "filterID"), Some(code(b"GEfc"))); + } + + /// A single effect as Photoshop 27 writes it: inline in `Fltr`, no `GEfs` list or `GELv`. + fn photoshop_chrome(detail: i32, smoothness: i32) -> Descriptor { + let rgb = + |v: f64| Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(v)).with("Grn ", Value::Double(v)).with("Bl ", Value::Double(v))); + let mut fltr = + Descriptor::new("GEfc").with("GEfk", enumv("GEft", "Chrm")).with("Dtl ", Value::Integer(detail)).with("Smth", Value::Integer(smoothness)); + fltr.name = UnicodeString::new_nul("Filter Gallery"); + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Filter Gallery..."))) + .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "Nrml")))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgb(255.0)) + .with("BckC", rgb(0.0)) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(code(b"GEfc"))) + } + + #[test] + fn filter_gallery_photoshop_inline_effect_imports_and_writes_back_inline() { + let item = photoshop_chrome(10, 4); + let f = filter_from_item(&item); + assert_eq!(f.command, "filter.filterGallery"); + assert_eq!( + f.params, + json!({ + "effects": [{"filter": "chrome", "params": {"detail": 10, "smoothness": 4}, "visible": true}], + "foreground": [1.0, 1.0, 1.0, 1.0], + "background": [0.0, 0.0, 0.0, 1.0], + }) + ); + // Written back in Photoshop's own layout. + let d = item_for_filter(&f).unwrap(); + assert_eq!(get_desc(&d, "Fltr"), get_desc(&item, "Fltr")); + assert_eq!((get_desc(&d, "FrgC"), get_desc(&d, "BckC")), (get_desc(&item, "FrgC"), get_desc(&item, "BckC"))); + assert_eq!(filter_from_item(&Descriptor::from_bytes(&d.to_bytes()).unwrap()), f); + // A hidden single effect needs the list (only it has a visibility flag). + let mut hidden = f.clone(); + hidden.params["effects"][0]["visible"] = json!(false); + let d = item_for_filter(&hidden).unwrap(); + assert!(get_desc(&d, "Fltr").unwrap().get("GEfs").is_some()); + assert_eq!(filter_from_item(&d), hidden); + // Inline effects we don't model stay verbatim. + let mut odd = photoshop_chrome(10, 4); + set(&mut odd, "Fltr", Value::Descriptor(Descriptor::new("GEfc").with("GEfk", enumv("GEft", "Ctot")).with("Dtl ", Value::Integer(1)))); + assert_eq!(filter_from_item(&odd).command, UNSUPPORTED_FILTER); + let mut extra = photoshop_chrome(10, 4); + set(&mut extra, "Fltr", Value::Descriptor(Descriptor::new("GEfc").with("GEfk", enumv("GEft", "Chrm")).with("Xtra", Value::Integer(1)))); + assert_eq!(filter_from_item(&extra).command, UNSUPPORTED_FILTER); + } + + #[test] + fn gallery_table_matches_the_gallery_filters() { + for g in &GALLERY_EFFECTS { + let f = photocraft_algo::GalleryFilter::from_key(g.key).unwrap(); + let keys: Vec<&str> = f.params().iter().map(|p| p.key).collect(); + for (_, ours) in g.settings { + assert!(keys.contains(ours), "{}.{ours}", g.key); + } + } + } + + #[test] + fn filter_gallery_written_with_defaults_and_colours() { + let f = sf("filter.filterGallery", json!({"effects": [{"filter": "chrome"}], "foreground": "#ff0000", "background": [0.0, 0.0, 1.0, 1.0]})); + let back = filter_from_item(&item_for_filter(&f).unwrap()); + assert_eq!(back.params["effects"][0]["params"], json!({"detail": 4, "smoothness": 7})); + assert_eq!(back.params["foreground"], json!([1.0, 0.0, 0.0, 1.0])); + assert_eq!(back.params["background"], json!([0.0, 0.0, 1.0, 1.0])); + } + + #[test] + fn filter_gallery_we_dont_model_is_kept_verbatim() { + // Another effect, an extra setting, a wrong-typed setting, no effects, a non-list. + let other = Descriptor::new("GEfc").with("GEfk", enumv("GEft", "Ctot")).with("GELv", Value::Boolean(true)); + let extra = chrome(4, 7, true).with("Xtra", Value::Integer(1)); + let typed = Descriptor::new("GEfc").with("Dtl ", Value::Double(4.0)).with("GEfk", enumv("GEft", "Chrm")); + for effects in [vec![chrome(4, 7, true), other], vec![extra], vec![typed], vec![]] { + let item = gallery_item_with(effects); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER); + assert_eq!(item_for_filter(&f).unwrap(), item); + } + let odd = Descriptor::new("filterFX").with("Fltr", Value::Descriptor(Descriptor::new("GEfc").with("GEfs", Value::Integer(3)))); + assert_eq!(filter_from_item(&odd).command, UNSUPPORTED_FILTER); + let bare = Descriptor::new("filterFX").with("filterID", Value::Integer(code(b"GEfc"))); + assert_eq!(filter_from_item(&bare).command, UNSUPPORTED_FILTER); + } + + #[test] + fn filter_gallery_without_psd_equivalent_is_reported() { + for p in [ + json!({"effects": [{"filter": "cutout"}]}), + json!({"effects": [{"filter": "chrome", "params": {"sparkle": 1}}]}), + json!({"effects": [{"params": {}}]}), + json!({"effects": []}), + json!({}), + json!({"effects": [{"filter": "chrome"}], "list": true}), + json!({"effects": [{"filter": "chrome"}], "foreground": "red"}), + json!(null), + ] { + assert!(item_for_filter(&sf("filter.filterGallery", p.clone())).is_err(), "{p}"); + } + } + #[test] fn unwritable_filters_are_reported_not_fatal() { let stack = FilterStack { diff --git a/crates/ui-egui/src/gallery_ui.rs b/crates/ui-egui/src/gallery_ui.rs index 83928e29b4..89eac953e9 100644 --- a/crates/ui-egui/src/gallery_ui.rs +++ b/crates/ui-egui/src/gallery_ui.rs @@ -92,6 +92,8 @@ pub struct GalleryDialog { thumb_src: Surface, thumbs: Vec>, pub preview_ms: f64, + /// Editing smart filter `index` of the layer: OK updates it instead of adding a filter. + pub smart_filter: Option, history: EditHistory>, } @@ -104,6 +106,7 @@ impl GalleryDialog { "zoom": if self.zoom > 0.0 { json!(self.zoom) } else { json!("fit") }, "previewMs": self.preview_ms, "rendering": self.rendering().is_some(), + "smartFilter": self.smart_filter, }) } @@ -269,18 +272,12 @@ fn image(surf: &Surface, w: usize, h: usize) -> egui::ColorImage { egui::ColorImage::new([w, h], px.iter().map(|p| Color32::from_rgba_unmultiplied(p[0], p[1], p[2], if alpha { p[3] } else { 255 })).collect()) } -/// Opens the dialog on the active layer, starting from the last gallery stack used. -pub fn open(app: &mut PhotocraftApp) -> Result<(), String> { - let (layer, src, _) = crate::distort_ui::active_pixels(app)?; - let st = app.session.active().ok_or("no document")?; - let canvas = st.doc.bounds(); - let layer_name = st.doc.layer(layer).map(|l| l.name.clone()).unwrap_or_default(); - let last = app.session.journal.iter().rev().find(|(id, _)| id == "filter.filterGallery").map(|(_, p)| p.clone()); - let mut effects: Vec = last - .as_ref() - .and_then(|p| p.get("effects").and_then(Value::as_array).cloned()) - .unwrap_or_default() - .iter() +/// The effect layers of a `filter.filterGallery` params object (unknown filters are skipped). +fn effects_from(p: &Value) -> Vec { + p.get("effects") + .and_then(Value::as_array) + .into_iter() + .flatten() .filter_map(|e| { let f = GalleryFilter::from_key(e.get("filter")?.as_str()?)?; let mut l = EffectLayer::new(f); @@ -290,7 +287,56 @@ pub fn open(app: &mut PhotocraftApp) -> Result<(), String> { l.visible = e.get("visible").and_then(Value::as_bool).unwrap_or(true); Some(l) }) - .collect(); + .collect() +} + +/// A recorded `[r, g, b, a]` colour param. +fn colour_param(p: &Value, key: &str) -> Option<[f32; 4]> { + let a = p.get(key)?.as_array()?; + let c = |j: usize, d: f32| a.get(j).and_then(Value::as_f64).map_or(Some(d), |v| Some(v as f32).filter(|v| v.is_finite())); + Some([c(0, 0.0)?, c(1, 0.0)?, c(2, 0.0)?, c(3, 1.0)?]) +} + +/// Opens the dialog on the active layer, starting from the last gallery stack used. +pub fn open(app: &mut PhotocraftApp) -> Result<(), String> { + let (layer, src, _) = crate::distort_ui::active_pixels(app)?; + let last = app.session.journal.iter().rev().find(|(id, _)| id == "filter.filterGallery").map(|(_, p)| p.clone()); + let effects = last.as_ref().map(effects_from).unwrap_or_default(); + let (fg, bg) = (app.session.tools.foreground, app.session.tools.background); + open_on(app, layer, src, effects, [fg, bg], None) +} + +/// Re-opens Filter Gallery smart filter `index` of smart object `layer` with its recorded stack, +/// previewing on the pixels below it; OK updates that filter (`layer.smartFilter.setParams`). +pub fn open_smart_filter(app: &mut PhotocraftApp, layer: LayerId, index: usize) -> Result<(), String> { + let st = app.session.active().ok_or("no document")?; + let Some(photocraft_doc::LayerContent::Smart(sm)) = st.doc.layer(layer).map(|l| &l.content) else { + return Err("the layer is not a smart object".into()); + }; + let filter = sm.smart_filters.get(index).ok_or("no such smart filter")?; + if filter.command != "filter.filterGallery" { + return Err("the smart filter is not a Filter Gallery filter".into()); + } + let p = filter.params.clone(); + let src = photocraft_engine::smart_cmds::render_below_filter(&st.doc, sm, index) + .map_err(|e| e.to_string())? + .ok_or("the smart object's contents are unavailable (missing linked file?)")?; + let fg = colour_param(&p, "foreground").unwrap_or(app.session.tools.foreground); + let bg = colour_param(&p, "background").unwrap_or(app.session.tools.background); + open_on(app, layer, src, effects_from(&p), [fg, bg], Some(index)) +} + +fn open_on( + app: &mut PhotocraftApp, + layer: LayerId, + src: Surface, + mut effects: Vec, + colours: [[f32; 4]; 2], + smart_filter: Option, +) -> Result<(), String> { + let st = app.session.active().ok_or("no document")?; + let canvas = st.doc.bounds(); + let layer_name = st.doc.layer(layer).map(|l| l.name.clone()).unwrap_or_default(); if effects.is_empty() { effects.push(EffectLayer::new(GalleryFilter::ColoredPencil)); } @@ -312,8 +358,8 @@ pub fn open(app: &mut PhotocraftApp) -> Result<(), String> { effects, selected: sel, open: open_cats, - foreground: app.session.tools.foreground, - background: app.session.tools.background, + foreground: colours[0], + background: colours[1], zoom: 0.0, center: [(canvas.x0 + canvas.x1) as f32 / 2.0, (canvas.y0 + canvas.y1) as f32 / 2.0], tex: None, @@ -325,6 +371,7 @@ pub fn open(app: &mut PhotocraftApp) -> Result<(), String> { thumb_src, thumbs: vec![None; GalleryFilter::ALL.len()], preview_ms: 0.0, + smart_filter, history, }); Ok(()) @@ -336,6 +383,9 @@ pub fn commit(app: &mut PhotocraftApp) -> Result { if !d.effects.iter().any(|e| e.visible) { return Ok(json!({"committed": false})); } + if let Some(index) = d.smart_filter { + return app.run("layer.smartFilter.setParams", json!({"layer": d.layer.0, "index": index, "params": d.params()})); + } let mut p = d.params(); p["layer"] = json!(d.layer.0); app.run("filter.filterGallery", p) @@ -859,6 +909,51 @@ mod tests { assert_eq!(app.session.active().unwrap().history.past_len(), before + 1, "cancel records nothing"); } + #[test] + fn smart_filter_double_click_edits_the_stored_gallery_stack() { + let ctx = egui::Context::default(); + let mut app = app(); + app.run("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + let stack = json!({"effects": [{"filter": "chrome", "params": {"detail": 2, "smoothness": 9}, "visible": true}], "foreground": [1.0, 0.0, 0.0, 1.0], "background": [0.0, 0.0, 1.0, 1.0]}); + app.run("filter.filterGallery", stack).unwrap(); + app.run("filter.blur.gaussianBlur", json!({"radius": 2.0})).unwrap(); + let layer = app.session.active().unwrap().active_layer.unwrap(); + let smart = |app: &PhotocraftApp| match &app.session.active().unwrap().doc.layer(layer).unwrap().content { + photocraft_doc::LayerContent::Smart(sm) => sm.clone(), + _ => panic!("not a smart object"), + }; + let steps = app.session.active().unwrap().history.past_len(); + + let sm = smart(&app); + crate::smart_ui::open_editor(&mut app, &ctx, layer.0, &sm, 0); + assert!(app.ui.dialogs.is_empty(), "no generic parameter form"); + let d = app.distort.gallery.as_ref().expect("the gallery dialog opens"); + assert_eq!((d.smart_filter, d.foreground, d.background), (Some(0), [1.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0])); + assert_eq!(d.effects.len(), 1); + assert_eq!((d.effects[0].filter, &d.effects[0].params["detail"]), (GalleryFilter::Chrome, &json!(2))); + // The preview starts from the pixels below the filter (no blur, no chrome). + let below = photocraft_engine::smart_cmds::render_below_filter(&app.session.active().unwrap().doc, &sm, 0).unwrap().unwrap(); + let d = app.distort.gallery.as_ref().unwrap(); + assert_eq!(d.src.read_region(d.canvas), below.read_region(d.canvas)); + + control(&mut app, &json!({"params": {"detail": 7}})).unwrap(); + control(&mut app, &json!({"commit": true})).unwrap(); + assert!(app.distort.gallery.is_none()); + let sm = smart(&app); + assert_eq!(sm.smart_filters.len(), 2, "edited in place, not added"); + assert_eq!(sm.smart_filters[0].command, "filter.filterGallery"); + assert_eq!(sm.smart_filters[0].params["effects"][0]["params"]["detail"], json!(7)); + assert_eq!(sm.smart_filters[0].params["foreground"], json!([1.0, 0.0, 0.0, 1.0])); + assert_eq!(app.session.active().unwrap().history.past_len(), steps + 1); + app.run("edit.undo", json!({})).unwrap(); + assert_eq!(smart(&app).smart_filters[0].params["effects"][0]["params"]["detail"], json!(2)); + + // Not a gallery filter / no such filter: an error, no dialog. + assert!(open_smart_filter(&mut app, layer, 1).is_err()); + assert!(open_smart_filter(&mut app, layer, 9).is_err()); + assert!(app.distort.gallery.is_none()); + } + #[test] fn preview_and_thumbnails_render() { let ctx = egui::Context::default(); diff --git a/crates/ui-egui/src/smart_ui.rs b/crates/ui-egui/src/smart_ui.rs index 48b2f0a72f..deee9a7d17 100644 --- a/crates/ui-egui/src/smart_ui.rs +++ b/crates/ui-egui/src/smart_ui.rs @@ -287,6 +287,14 @@ pub fn open_editor(app: &mut PhotocraftApp, ctx: &egui::Context, layer: u64, sm: } return; } + // So has the Filter Gallery. + if f.command == "filter.filterGallery" { + if let Err(e) = crate::gallery_ui::open_smart_filter(app, photocraft_doc::LayerId(layer), index) { + app.ui.status = e; + app.ui.status_error = true; + } + return; + } if !crate::filter_dialog::has_dialog(&f.command) { return; } diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 3543671cbf..2651f4c4d1 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -21,7 +21,7 @@ XL are missing (gap G10). Remaining: 300–500 h including PSD fidelity. | Format | Photoshop | PhotoCraft read | PhotoCraft write | Fidelity and tests | |---|---|---|---|---| -| PSD | R/W | yes | yes | 1/8/16/32-bit; Bitmap, Gray, Indexed, RGB, CMYK, Multichannel, Lab, Duotone; layers, groups, masks, vector masks, adjustment and fill layers, effects, text, shapes, smart objects (SoLd/lnk2; CS2 PlLd/lnkD read with their warp, kept as is or re-saved as SoLd once edited), smart filters (14 mapped, rest verbatim), slices, comps, Advanced Blending. Oracle floors: Photoshop-authored 185/258, io 148/170, psd-tools 249/309; round trip ≥ 169/170, 308/309, 258/256. Known failures: #1281, #2469 (#2374, text layers, closed 2026-10-10; #2465, CS2 `PlLd`-only smart objects, fixed 2026-10-12) | +| PSD | R/W | yes | yes | 1/8/16/32-bit; Bitmap, Gray, Indexed, RGB, CMYK, Multichannel, Lab, Duotone; layers, groups, masks, vector masks, adjustment and fill layers, effects, text, shapes, smart objects (SoLd/lnk2; CS2 PlLd/lnkD read with their warp, kept as is or re-saved as SoLd once edited), smart filters (14 mapped plus Filter Gallery with Chrome, rest verbatim), slices, comps, Advanced Blending. Oracle floors: Photoshop-authored 185/258, io 148/170, psd-tools 249/309; round trip ≥ 169/170, 308/309, 258/256. Known failures: #1281, #2469 (#2374, text layers, closed 2026-10-10; #2465, CS2 `PlLd`-only smart objects, fixed 2026-10-12) | | PSB | R/W | yes | yes | Auto-switches above 2 GB (REL-212-2); 4–5 GB fixtures generated (FILE-216-2); > 4 GB fails on Windows (#375) | | Cloud PSD (.psdc) | R/W | no | no | Out of scope (Adobe cloud) | | TIFF | R/W | yes | yes | 8/16/32f, Gray/RGB/CMYK, BigTIFF, every IFD, banded parallel decode; layered TIFF both byte orders (FILE-215-7: extra alpha channels not written) | @@ -168,5 +168,6 @@ remaining gaps in #1606 stay tracked with per-sample corpus ceilings. | 2026-10-12 | minor | Presets table: Contours `.shc` read and write through the Contour Editor (FILE-215-15); `.csh` and `.tpl` stay missing | | 2026-10-12 | minor | Palette BMPs open in Indexed Color mode (#3802) | | 2026-10-12 | minor | Save writes a JPEG at its own estimated quality and a WebP lossy or lossless as opened (#2399) | +| 2026-10-11 | minor | PSD smart filters: Filter Gallery (`GEfc`) maps to `filter.filterGallery` when every effect is modelled (Chrome so far), editable from the Layers panel | | 2026-10-11 | minor | D5100 image area and fitted-look preservation in Camera Raw tuning | | 2026-10-10 | major | First version: Photoshop 2026 27.11.0's formats from its bundle, against our codecs; Affinity notes moved from `roadmap.md` |