Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Merge PR #6303: PSD: Diffuse, Trace Contour, Crystallize and Pointillize smart filters open and save like Photoshop (#6302)

aiongg7c92cb988cbb
diff --cc crates/io/src/smart_map.rs
index ff8e4bf5f,f10742d25..af61838d6
--- a/crates/io/src/smart_map.rs
+++ b/crates/io/src/smart_map.rs
@@@ -400,30 -304,19 +424,41 @@@ fn known_params(class: &str, f: Option<
};
json!({"amount": px(f, "Nose")?, "distribution": dist, "monochromatic": mono, "seed": int(f, "FlRs").unwrap_or(0)})
}
+ ("surfaceBlur", Some(f)) if only_keys(f, &["Rds ", "Thsh"]) => json!({
+ "radius": in_range(px(f, "Rds ")?, 1.0, 100.0)?,
+ "threshold": int_in(f, "Thsh", 2, 255)?,
+ }),
+ ("SmrB", Some(f)) if only_keys(f, &["Rds ", "Thsh", "SmBQ", "SmBM"]) => json!({
+ "radius": in_range(px(f, "Rds ")?, 0.1, 100.0)?,
+ "threshold": in_range(px(f, "Thsh")?, 0.1, 100.0)?,
+ "quality": enum_name(f, "SmBQ", &SMART_BLUR_QUALITY)?,
+ "mode": enum_name(f, "SmBM", &SMART_BLUR_MODE)?,
+ }),
+ ("RdlB", Some(f)) if only_keys(f, &["Amnt", "BlrM", "BlrQ"]) => json!({
+ "amount": int_in(f, "Amnt", 1, 100)?,
+ "method": enum_name(f, "BlrM", &RADIAL_METHOD)?,
+ "quality": enum_name(f, "BlrQ", &RADIAL_QUALITY)?,
+ }),
("Msc ", Some(f)) => json!({"cellSize": px(f, "ClSz")?}),
("Embs", Some(f)) => json!({"angle": int(f, "Angl")?, "height": int(f, "Hght")?, "amount": int(f, "Amnt")?}),
+ (SMART_SHARPEN, Some(f)) => smart_sharpen_params(f)?,
+ ("DstS", Some(f)) => {
+ if f.items.iter().any(|(k, _)| !k.is("Rds ") && !k.is("Thsh")) {
+ return None;
+ }
+ json!({"radius": int_in(f, "Rds ", 1, 500)?, "threshold": int_in(f, "Thsh", 0, 255)?})
+ }
+ ("Dfs ", Some(f)) => json!({"mode": enum_param(f, "Md ", "DfsM", &DIFFUSE_MODES)?}),
+ ("TrcC", Some(f)) => json!({"level": int_in(f, "Lvl ", 0, 255)?, "edge": enum_param(f, "Edg ", "CntE", &TRACE_EDGES)?}),
+ ("Crst", Some(f)) => json!({"cellSize": int_in(f, "ClSz", CELL_SIZE.0, CELL_SIZE.1)?}),
+ ("Pntl", Some(f)) => {
+ let mut p = json!({"cellSize": int_in(f, "ClSz", CELL_SIZE.0, CELL_SIZE.1)?});
+ // Pointillize paints the gaps in the background colour the entry carries.
+ if item.get("BckC").is_some() {
+ p["background"] = get_desc(item, "BckC").and_then(rgbc_colour)?;
+ }
+ p
+ }
("Lvls", Some(f)) => levels_params(f)?,
("Crvs", Some(f)) => curves_params(f)?,
_ => return None,
@@@ -611,10 -504,8 +646,10 @@@ pub fn filter_from_item(item: &Descript
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(hue_sat::CLASS) => fltr.and_then(hue_sat::import_params).map(|p| (hue_sat::COMMAND, p)),
+ Some(perspective_warp::CLASS) => perspective_warp::import_params(item).map(|p| (perspective_warp::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)),
+ c => c.and_then(|c| known_params(c, fltr, item)),
};
let (command, params) = match known {
Some((c, p)) => (c.to_string(), p),
@@@ -737,36 -646,13 +790,41 @@@ fn fltr_for(k: &Known, p: &J) -> Result
.with("Mnch", Value::Boolean(p.get("monochromatic").and_then(J::as_bool).unwrap_or(false)))
.with("FlRs", pi(p, "seed", 0.0))
}
+ "surfaceBlur" => d.with("Rds ", unit(b"#Pxl", pf(p, "radius", 5.0))).with("Thsh", pi(p, "threshold", 15.0)),
+ "SmrB" => d
+ .with("Rds ", Value::Double(pf(p, "radius", 3.0)))
+ .with("Thsh", Value::Double(pf(p, "threshold", 25.0)))
+ .with("SmBQ", enum_param(p, "quality", "high", "SmBQ", &SMART_BLUR_QUALITY)?)
+ .with("SmBM", enum_param(p, "mode", "normal", "SmBM", &SMART_BLUR_MODE)?),
+ "RdlB" => {
+ // Photoshop wrote no centre key for a centred blur; an off-centre one is unmeasured.
+ if ["centerX", "centerY"].iter().any(|k| (pf(p, k, 0.5) - 0.5).abs() > 1e-9) {
+ return Err("an off-centre Radial Blur centre is not written to PSD".into());
+ }
+ d.with("Amnt", pi(p, "amount", 10.0))
+ .with("BlrM", enum_param(p, "method", "spin", "BlrM", &RADIAL_METHOD)?)
+ .with("BlrQ", enum_param(p, "quality", "good", "BlrQ", &RADIAL_QUALITY)?)
+ }
"Msc " => d.with("ClSz", unit(b"#Pxl", pf(p, "cellSize", 10.0))),
"Embs" => d.with("Angl", pi(p, "angle", 135.0)).with("Hght", pi(p, "height", 3.0)).with("Amnt", pi(p, "amount", 100.0)),
+ SMART_SHARPEN => {
+ // The engine's defaults and ranges (it clamps the same way when rendering).
+ d.with("useLegacy", Value::Boolean(false))
+ .with("Amnt", unit(b"#Prc", pf(p, "amount", 100.0).clamp(1.0, 500.0)))
+ .with("Rds ", unit(b"#Pxl", pf(p, "radius", 1.0).clamp(0.1, 64.0)))
+ .with("noiseReduction", unit(b"#Prc", pf(p, "reduceNoise", 10.0).clamp(0.0, 100.0)))
+ .with("blur", enumv("blurType", "GsnB"))
+ }
+ // Photoshop stores whole pixels and levels.
+ "DstS" => {
+ let whole = |key: &str, default: f64, lo: f64, hi: f64| Value::Integer(pf(p, key, default).round().clamp(lo, hi) as i32);
+ d.with("Rds ", whole("radius", 1.0, 1.0, 500.0)).with("Thsh", whole("threshold", 0.0, 0.0, 255.0))
+ }
+ // Photoshop stores no random seed for these: `seed` is ours only and is not written.
+ "Dfs " => d.with("Md ", enum_out(p, "mode", "normal", "DfsM", &DIFFUSE_MODES)?),
+ "TrcC" => d.with("Lvl ", pi_in(p, "level", 128.0, 0, 255)).with("Edg ", enum_out(p, "edge", "lower", "CntE", &TRACE_EDGES)?),
+ "Crst" => d.with("ClSz", pi_in(p, "cellSize", 10.0, CELL_SIZE.0, CELL_SIZE.1)),
+ "Pntl" => d.with("ClSz", pi_in(p, "cellSize", 5.0, CELL_SIZE.0, CELL_SIZE.1)),
"Lvls" => d.with("Adjs", adjustments(p, false)?),
"Crvs" => d.with("Adjs", adjustments(p, true)?),
"adaptCorrect" => {
@@@ -1344,8 -1123,19 +1404,21 @@@ mod tests
sf("filter.other.minimum", json!({"radius": 2.0})),
sf("filter.pixelate.mosaic", json!({"cellSize": 8.0})),
sf("filter.stylize.emboss", json!({"angle": 135, "height": 3, "amount": 100})),
+ sf("filter.sharpen.smartSharpen", json!({"amount": 120.0, "radius": 2.5, "reduceNoise": 5.0})),
+ sf("filter.noise.dustAndScratches", json!({"radius": 4, "threshold": 20})),
+ sf("filter.stylize.diffuse", json!({"mode": "normal"})),
+ sf("filter.stylize.diffuse", json!({"mode": "darkenOnly"})),
+ sf("filter.stylize.diffuse", json!({"mode": "lightenOnly"})),
+ sf("filter.stylize.diffuse", json!({"mode": "anisotropic"})),
+ sf("filter.stylize.traceContour", json!({"level": 60, "edge": "lower"})),
+ sf("filter.stylize.traceContour", json!({"level": 128, "edge": "upper"})),
+ sf("filter.stylize.traceContour", json!({"level": 0, "edge": "upper"})),
+ sf("filter.stylize.traceContour", json!({"level": 255, "edge": "lower"})),
+ sf("filter.pixelate.crystallize", json!({"cellSize": 10})),
+ sf("filter.pixelate.crystallize", json!({"cellSize": 3})),
+ sf("filter.pixelate.crystallize", json!({"cellSize": 300})),
+ sf("filter.pixelate.pointillize", json!({"cellSize": 7, "background": [1.0, 1.0, 1.0, 1.0]})),
+ sf("filter.pixelate.pointillize", json!({"cellSize": 300, "background": [0.0, 0.5, 1.0, 1.0]})),
sf("image.adjustments.levels", json!({"inBlack": 20, "inWhite": 220, "gamma": 1.4, "red": {"inBlack": 0, "inWhite": 255, "gamma": 0.8}})),
sf("image.adjustments.curves", json!({"points": [[0.0, 0.0], [64.0, 40.0], [255.0, 255.0]], "blue": [[0.0, 20.0], [255.0, 235.0]]})),
sf("image.adjustments.shadowsHighlights", json!({})),
@@@ -1679,156 -1176,150 +1752,300 @@@
assert_eq!(filter_from_item(&odd).command, UNSUPPORTED_FILTER);
}
+ /// A `filterFXList` item with filter `fltr` and `filterID`, as Photoshop saves it.
+ fn ps_item(name: &str, fltr: Descriptor, filter_id: i32) -> Descriptor {
+ Descriptor::new("filterFX")
+ .with("Nm ", Value::Text(UnicodeString::new_nul(name)))
+ .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", rgbc(0.0))
+ .with("BckC", rgbc(255.0))
+ .with("Fltr", Value::Descriptor(fltr))
+ .with("filterID", Value::Integer(filter_id))
+ }
+
+ fn surface_blur(rds: f64, thsh: i32) -> Descriptor {
+ Descriptor::new("surfaceBlur").with("Rds ", unit(b"#Pxl", rds)).with("Thsh", Value::Integer(thsh))
+ }
+
+ fn smart_blur(rds: f64, thsh: f64, q: &str, m: &str) -> Descriptor {
+ Descriptor::new("SmrB").with("Rds ", Value::Double(rds)).with("Thsh", Value::Double(thsh)).with("SmBQ", enumv("SmBQ", q)).with("SmBM", enumv("SmBM", m))
+ }
+
+ fn radial_blur(amnt: i32, m: &str, q: &str) -> Descriptor {
+ Descriptor::new("RdlB").with("Amnt", Value::Integer(amnt)).with("BlrM", enumv("BlrM", m)).with("BlrQ", enumv("BlrQ", q))
+ }
+
+ /// The `Fltr` shapes measured in Photoshop (each filter applied to a smart object through
+ /// Action Manager, saved, and `filterFX` read back) open as our filters and are written back
+ /// with the same keys, value types, units, enums and filter ids.
+ #[test]
+ fn blur_family_matches_photoshop_shapes() {
+ let smart = code(b"SmrB");
+ let radial = code(b"RdlB");
+ assert_eq!((smart, radial), (1_399_681_602, 1_382_313_026));
+ let cases = [
+ (surface_blur(5.0, 15), 1135, "filter.blur.surfaceBlur", json!({"radius": 5.0, "threshold": 15})),
+ (surface_blur(12.0, 40), 1135, "filter.blur.surfaceBlur", json!({"radius": 12.0, "threshold": 40})),
+ (
+ smart_blur(5.0, 25.0, "SBQH", "SBMN"),
+ smart,
+ "filter.blur.smartBlur",
+ json!({"radius": 5.0, "threshold": 25.0, "quality": "high", "mode": "normal"}),
+ ),
+ (
+ smart_blur(3.0, 30.0, "SBQL", "SBME"),
+ smart,
+ "filter.blur.smartBlur",
+ json!({"radius": 3.0, "threshold": 30.0, "quality": "low", "mode": "edgeOnly"}),
+ ),
+ (
+ smart_blur(3.0, 30.0, "SBQM", "SBMO"),
+ smart,
+ "filter.blur.smartBlur",
+ json!({"radius": 3.0, "threshold": 30.0, "quality": "medium", "mode": "overlayEdge"}),
+ ),
+ (radial_blur(10, "Spn ", "Drft"), radial, "filter.blur.radialBlur", json!({"amount": 10, "method": "spin", "quality": "draft"})),
+ (radial_blur(20, "Spn ", "Bst "), radial, "filter.blur.radialBlur", json!({"amount": 20, "method": "spin", "quality": "best"})),
+ (radial_blur(20, "Zm ", "Gd "), radial, "filter.blur.radialBlur", json!({"amount": 20, "method": "zoom", "quality": "good"})),
+ ];
+ for (fltr, id, command, params) in cases {
+ let item = ps_item("x", fltr.clone(), id);
+ let f = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap());
+ assert_eq!((f.command.as_str(), &f.params), (command, &params), "{fltr:?}");
+ let out = item_for_filter(&f).unwrap();
+ assert_eq!(out.get("filterID"), Some(&Value::Integer(id)));
+ assert_eq!(out.get("hasoptions"), Some(&Value::Boolean(true)));
+ // Byte-identical filter descriptor: class id, four-char keys, types, units and enums.
+ let Some(Value::Descriptor(w)) = out.get("Fltr") else { panic!("no Fltr for {command}") };
+ assert_eq!(w.to_bytes(), fltr.to_bytes(), "{command}");
+ }
+ // Surface Blur's class is a string id; Smart Blur's and Radial Blur's are four-char codes.
+ assert_eq!(surface_blur(1.0, 2).class_id, Id::Str(b"surfaceBlur".to_vec()));
+ assert_eq!(radial_blur(1, "Spn ", "Gd ").class_id, Id::Code(*b"RdlB"));
+ // Unset options are written with the dialog defaults.
+ let fltr = |c: &str, p: J| item_for_filter(&sf(c, p)).unwrap().get("Fltr").cloned();
+ assert_eq!(fltr("filter.blur.surfaceBlur", json!({})), Some(Value::Descriptor(surface_blur(5.0, 15))));
+ assert_eq!(fltr("filter.blur.smartBlur", json!({})), Some(Value::Descriptor(smart_blur(3.0, 25.0, "SBQH", "SBMN"))));
+ assert_eq!(fltr("filter.blur.radialBlur", json!({"centerX": 0.5, "centerY": 0.5})), Some(Value::Descriptor(radial_blur(10, "Spn ", "Gd "))));
+ }
+
+ /// Malformed or unmodelled blur descriptors stay verbatim (and are written back unchanged);
+ /// settings PSD can't hold are reported, never a panic.
+ #[test]
+ fn blur_family_malformed_is_kept_verbatim() {
+ let smart = code(b"SmrB");
+ let radial = code(b"RdlB");
+ let text = |s: &str| Value::Text(UnicodeString::new_nul(s));
+ let bad = [
+ // Missing keys.
+ (Descriptor::new("surfaceBlur").with("Rds ", unit(b"#Pxl", 5.0)), 1135),
+ (Descriptor::new("SmrB").with("Rds ", Value::Double(3.0)).with("Thsh", Value::Double(25.0)), smart),
+ (Descriptor::new("RdlB").with("Amnt", Value::Integer(10)).with("BlrM", enumv("BlrM", "Spn ")), radial),
+ // Wrong types.
+ (Descriptor::new("surfaceBlur").with("Rds ", unit(b"#Pxl", 5.0)).with("Thsh", Value::Double(15.0)), 1135),
+ (
+ Descriptor::new("SmrB")
+ .with("Rds ", text("3"))
+ .with("Thsh", Value::Double(25.0))
+ .with("SmBQ", enumv("SmBQ", "SBQH"))
+ .with("SmBM", enumv("SmBM", "SBMN")),
+ smart,
+ ),
+ (Descriptor::new("RdlB").with("Amnt", text("10")).with("BlrM", enumv("BlrM", "Spn ")).with("BlrQ", enumv("BlrQ", "Gd ")), radial),
+ (Descriptor::new("RdlB").with("Amnt", Value::Integer(10)).with("BlrM", Value::Integer(1)).with("BlrQ", enumv("BlrQ", "Gd ")), radial),
+ // Out of range, NaN and infinity.
+ (surface_blur(0.5, 15), 1135),
+ (surface_blur(5.0, 300), 1135),
+ (surface_blur(f64::NAN, 15), 1135),
+ (smart_blur(f64::INFINITY, 25.0, "SBQH", "SBMN"), smart),
+ (smart_blur(3.0, 0.0, "SBQH", "SBMN"), smart),
+ (radial_blur(0, "Spn ", "Gd "), radial),
+ (radial_blur(101, "Zm ", "Gd "), radial),
+ // Unknown enum values.
+ (smart_blur(3.0, 25.0, "SBQX", "SBMN"), smart),
+ (smart_blur(3.0, 25.0, "Hgh ", "SBMN"), smart),
+ (radial_blur(10, "Twrl", "Gd "), radial),
+ (radial_blur(10, "Spn ", "Hgh "), radial),
+ // A key we don't model (an off-centre Radial Blur would carry one).
+ (radial_blur(10, "Zm ", "Gd ").with("Cntr", Value::Descriptor(Descriptor::new("Pnt ").with("Hrzn", Value::Double(10.0)))), radial),
+ (surface_blur(5.0, 15).with("Edg ", Value::Integer(1)), 1135),
+ ];
+ for (fltr, id) in bad {
+ let item = ps_item("x", fltr.clone(), id);
+ let f = filter_from_item(&item);
+ assert_eq!(f.command, UNSUPPORTED_FILTER, "{fltr:?}");
+ // Compared as bytes: a NaN setting is not equal to itself.
+ assert_eq!(item_for_filter(&f).unwrap().to_bytes(), item.to_bytes(), "{fltr:?}");
+ }
+ // Radial Blur at its defaults has no `Fltr` (measured): its values are not stored, so the
+ // entry stays verbatim instead of guessing them.
+ let no_opts = Descriptor::new("filterFX").with("hasoptions", Value::Boolean(false)).with("filterID", Value::Integer(radial));
+ assert_eq!(filter_from_item(&no_opts).command, UNSUPPORTED_FILTER);
+ // Settings with no PSD equivalent are errors when writing.
+ for (command, p) in [
+ ("filter.blur.smartBlur", json!({"quality": "ultra"})),
+ ("filter.blur.smartBlur", json!({"mode": 3})),
+ ("filter.blur.radialBlur", json!({"method": "twirl"})),
+ ("filter.blur.radialBlur", json!({"quality": ["best"]})),
+ ("filter.blur.radialBlur", json!({"centerX": 0.25})),
+ ("filter.blur.radialBlur", json!({"centerY": 1.0})),
+ ] {
+ assert!(item_for_filter(&sf(command, p.clone())).is_err(), "{command} {p}");
+ }
+ // Hostile numbers write without panicking.
+ for p in [json!({"radius": f64::MAX, "threshold": -1e300, "amount": 1e300}), json!({"radius": "x", "threshold": null}), json!(null), json!([1, 2])] {
+ for command in ["filter.blur.surfaceBlur", "filter.blur.smartBlur", "filter.blur.radialBlur"] {
+ let _ = item_for_filter(&sf(command, p.clone()));
+ }
+ }
+ }
+
+ /// A `filterFX` entry shaped as Photoshop saves it (measured in Photoshop: the filter applied
+ /// through Action Manager to a smart object, the PSD saved, `filterFX` read back).
+ fn photoshop_item(filter_id: &[u8; 4], fltr: Descriptor) -> Descriptor {
+ Descriptor::new("filterFX")
+ .with("Nm ", Value::Text(UnicodeString::new_nul("Filter...")))
+ .with("blendOptions", blend_options(&sf("", J::Null)))
+ .with("enab", Value::Boolean(true))
+ .with("hasoptions", Value::Boolean(true))
+ .with("FrgC", rgbc(0.0))
+ .with("BckC", rgbc(255.0))
+ .with("Fltr", Value::Descriptor(fltr))
+ .with("filterID", Value::Integer(code(filter_id)))
+ }
+
+ #[test]
+ fn stylize_and_pixelate_filters_open_from_photoshop_shapes() {
+ let cases = [
+ (b"Dfs ", Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "Nrml")), "filter.stylize.diffuse", json!({"mode": "normal"})),
+ (b"Dfs ", Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "DrkO")), "filter.stylize.diffuse", json!({"mode": "darkenOnly"})),
+ (b"Dfs ", Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "LghO")), "filter.stylize.diffuse", json!({"mode": "lightenOnly"})),
+ (b"Dfs ", Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "anisotropic")), "filter.stylize.diffuse", json!({"mode": "anisotropic"})),
+ (
+ b"TrcC",
+ Descriptor::new("TrcC").with("Lvl ", Value::Integer(128)).with("Edg ", enumv("CntE", "Upr ")),
+ "filter.stylize.traceContour",
+ json!({"level": 128, "edge": "upper"}),
+ ),
+ (
+ b"TrcC",
+ Descriptor::new("TrcC").with("Lvl ", Value::Integer(60)).with("Edg ", enumv("CntE", "Lwr ")),
+ "filter.stylize.traceContour",
+ json!({"level": 60, "edge": "lower"}),
+ ),
+ (b"Crst", Descriptor::new("Crst").with("ClSz", Value::Integer(10)), "filter.pixelate.crystallize", json!({"cellSize": 10})),
+ (
+ b"Pntl",
+ Descriptor::new("Pntl").with("ClSz", Value::Integer(7)),
+ "filter.pixelate.pointillize",
+ json!({"cellSize": 7, "background": [1.0, 1.0, 1.0, 1.0]}),
+ ),
+ ];
+ for (id, fltr, command, params) in cases {
+ let item = photoshop_item(id, fltr);
+ // Through bytes, as read from a file.
+ let f = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap());
+ assert_eq!((f.command.as_str(), &f.params), (command, &params));
+ // Written back in the measured shape: same class, keys, value types and filterID.
+ let back = item_for_filter(&f).unwrap();
+ for key in ["Fltr", "filterID", "BckC"] {
+ assert_eq!(back.get(key), item.get(key), "{command} {key}");
+ }
+ }
+ // The `anisotropic` mode is a long id, as Photoshop writes it.
+ let d = item_for_filter(&sf("filter.stylize.diffuse", json!({"mode": "anisotropic"}))).unwrap();
+ let Some(Value::Descriptor(fltr)) = d.get("Fltr") else { panic!("no Fltr") };
+ assert!(matches!(fltr.get("Md "), Some(Value::Enumerated { value: Id::Str(v), .. }) if v == b"anisotropic"));
+ // Pointillize takes the entry's background colour; the others ignore it.
+ let mut item = photoshop_item(b"Pntl", Descriptor::new("Pntl").with("ClSz", Value::Integer(7)));
+ set(&mut item, "BckC", rgbc(51.0));
+ let f = filter_from_item(&item);
+ assert_eq!(f.params["background"], json!([0.2, 0.2, 0.2, 1.0]));
+ assert_eq!(item_for_filter(&f).unwrap().get("BckC"), Some(&rgbc(51.0)));
+ let mut item = photoshop_item(b"Crst", Descriptor::new("Crst").with("ClSz", Value::Integer(7)));
+ set(&mut item, "BckC", rgbc(51.0));
+ assert_eq!(filter_from_item(&item).params, json!({"cellSize": 7}));
+ // Our random seed has no PSD key: it is not written.
+ let fltr = |c: &str, p: J| item_for_filter(&sf(c, p)).unwrap().get("Fltr").cloned();
+ assert_eq!(
+ fltr("filter.stylize.diffuse", json!({"mode": "normal", "seed": 7})),
+ Some(Value::Descriptor(Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "Nrml"))))
+ );
+ // Defaults and out-of-range values are written in Photoshop's ranges.
+ assert_eq!(
+ fltr("filter.stylize.traceContour", json!({})),
+ Some(Value::Descriptor(Descriptor::new("TrcC").with("Lvl ", Value::Integer(128)).with("Edg ", enumv("CntE", "Lwr "))))
+ );
+ assert_eq!(fltr("filter.pixelate.crystallize", json!({})), Some(Value::Descriptor(Descriptor::new("Crst").with("ClSz", Value::Integer(10)))));
+ assert_eq!(fltr("filter.pixelate.pointillize", json!({})), Some(Value::Descriptor(Descriptor::new("Pntl").with("ClSz", Value::Integer(5)))));
+ assert_eq!(
+ fltr("filter.pixelate.crystallize", json!({"cellSize": 1e9})),
+ Some(Value::Descriptor(Descriptor::new("Crst").with("ClSz", Value::Integer(300))))
+ );
+ assert_eq!(
+ fltr("filter.pixelate.pointillize", json!({"cellSize": "big"})),
+ Some(Value::Descriptor(Descriptor::new("Pntl").with("ClSz", Value::Integer(5))))
+ );
+ assert_eq!(
+ fltr("filter.stylize.traceContour", json!({"level": -40, "edge": "upper"})),
+ Some(Value::Descriptor(Descriptor::new("TrcC").with("Lvl ", Value::Integer(0)).with("Edg ", enumv("CntE", "Upr "))))
+ );
+ }
+
+ #[test]
+ fn malformed_stylize_and_pixelate_filters_are_kept_verbatim() {
+ let long = Value::Integer;
+ let cases = [
+ // Missing keys.
+ (b"Dfs ", Descriptor::new("Dfs ")),
+ (b"TrcC", Descriptor::new("TrcC").with("Lvl ", long(60))),
+ (b"TrcC", Descriptor::new("TrcC").with("Edg ", enumv("CntE", "Upr "))),
+ (b"Crst", Descriptor::new("Crst")),
+ (b"Pntl", Descriptor::new("Pntl")),
+ // Unknown enum values or enum types.
+ (b"Dfs ", Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "Wild"))),
+ (b"Dfs ", Descriptor::new("Dfs ").with("Md ", enumv("BlnM", "Nrml"))),
+ (b"TrcC", Descriptor::new("TrcC").with("Lvl ", long(60)).with("Edg ", enumv("CntE", "Mddl"))),
+ // Wrong types (also NaN).
+ (b"Dfs ", Descriptor::new("Dfs ").with("Md ", long(1))),
+ (b"TrcC", Descriptor::new("TrcC").with("Lvl ", Value::Double(60.0)).with("Edg ", enumv("CntE", "Upr "))),
+ (b"Crst", Descriptor::new("Crst").with("ClSz", unit(b"#Pxl", 10.0))),
+ (b"Pntl", Descriptor::new("Pntl").with("ClSz", Value::Double(f64::NAN))),
+ // Out of range.
+ (b"TrcC", Descriptor::new("TrcC").with("Lvl ", long(256)).with("Edg ", enumv("CntE", "Upr "))),
+ (b"TrcC", Descriptor::new("TrcC").with("Lvl ", long(-1)).with("Edg ", enumv("CntE", "Upr "))),
+ (b"Crst", Descriptor::new("Crst").with("ClSz", long(2))),
+ (b"Pntl", Descriptor::new("Pntl").with("ClSz", long(i32::MAX))),
+ ];
+ for (id, fltr) in cases {
+ let item = photoshop_item(id, fltr);
+ let f = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap());
+ assert_eq!(f.command, UNSUPPORTED_FILTER, "{item:?}");
+ assert_eq!(item_for_filter(&f).unwrap().to_bytes(), item.to_bytes());
+ }
+ // Saved without options (`hasoptions` false, no `Fltr`): kept verbatim.
+ let bare = Descriptor::new("filterFX").with("hasoptions", Value::Boolean(false)).with("filterID", Value::Integer(code(b"Crst")));
+ assert_eq!(filter_from_item(&bare).command, UNSUPPORTED_FILTER);
+ // A Pointillize entry whose background colour is unreadable is kept verbatim too.
+ for bad in [Value::Integer(3), Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(0.0)))] {
+ let mut item = photoshop_item(b"Pntl", Descriptor::new("Pntl").with("ClSz", Value::Integer(7)));
+ set(&mut item, "BckC", bad);
+ assert_eq!(filter_from_item(&item).command, UNSUPPORTED_FILTER);
+ }
+ // Values with no PSD equivalent are reported, not guessed.
+ for (c, p) in [
+ ("filter.stylize.diffuse", json!({"mode": "sideways"})),
+ ("filter.stylize.diffuse", json!({"mode": 3})),
+ ("filter.stylize.traceContour", json!({"edge": "middle"})),
+ ("filter.pixelate.pointillize", json!({"background": "teal"})),
+ ("filter.pixelate.pointillize", json!({"background": [1.0]})),
+ ] {
+ assert!(item_for_filter(&sf(c, p.clone())).is_err(), "{c} {p}");
+ }
+ }
+
/// A Filter Gallery item as Photopea writes it (Chrome, Detail 12, Smoothness 4).
fn gallery_item_with(effects: Vec<Descriptor>) -> Descriptor {
let rgb =
diff --cc docs/file-format-parity.md
index 4f7df2383,b13667592..a493452f3
--- a/docs/file-format-parity.md
+++ b/docs/file-format-parity.md
@@@ -143,11 -143,7 +143,12 @@@ remaining gaps in #1606 stay tracked wi
| Date | Change | Summary |
|---|---|---|
| 2026-10-15 | minor | DDS textures save (#1327): uncompressed or BC1/BC2/BC3 (DXT1/3/5) from a clean-room block encoder, optional box-filtered mip chain, legacy header; DDS Options in Save As and Save a Copy, `ddsCompression`/`ddsMipmaps` for scripts, the CLI and MCP |
+| 2026-10-16 | minor | PSD smart filters: Perspective Warp (`perspectiveWarpTransform`: `vertices`, `warpedVertices`, `quads` of vertex indices, document pixels) opens as `layer.smartObjects.perspectiveWarp`, one plane per quad, and re-renders warped; save writes the original item with only the geometry rewritten, or a new one in Photoshop's layout; inconsistent descriptors stay verbatim (FILE-215-2, #6268) |
+| 2026-10-16 | minor | PSD smart filters: Hue/Saturation (`HStr`: `Clrz`, `Adjs` = master `Hst2` plus one per edited range with `LclR` and its four range sliders, all longs, measured on a Photoshop-saved file) opens as `image.adjustments.hueSaturation`, re-renders with the smart object and saves in Photoshop's layout; layouts outside it stay verbatim (FILE-215-2, #6267) |
+| 2026-10-15 | minor | PSD smart filters without options (Blur, Blur More, Average, Despeckle, Find Edges, Facet, Fragment, Sharpen, Sharpen More, Sharpen Edges, Solarize) map both ways as Photoshop stores them: `hasoptions` false, no `Fltr`, the four-char `filterID` (FILE-215-2) |
+| 2026-10-15 | minor | PSD smart filters: Surface Blur (filterID 1135, `surfaceBlur`), Smart Blur (`SmrB`) and Radial Blur (`RdlB`) open as editable filters and save with the keys, types and enums measured in Photoshop; out-of-range, unknown-enum or extra-key entries (an off-centre Radial Blur) stay verbatim (#6298, FILE-215-2) |
+| 2026-10-15 | minor | PSD smart filters: Smart Sharpen (filterID 1131, Gaussian removal; Lens/Motion Blur and legacy entries stay verbatim) and Dust & Scratches (`DstS`) open as editable filters and save in Photoshop's measured shape (#6300) |
+ | 2026-10-15 | minor | PSD smart filters: Diffuse (`Dfs `, `DfsM` mode), Trace Contour (`TrcC`, level and `CntE` edge), Crystallize (`Crst`) and Pointillize (`Pntl`, background from `BckC`) open as editable smart filters and save in the shape measured in Photoshop (#6302, FILE-215-2) |
| 2026-10-15 | minor | PNG: a stereo PNG's `sTER` pairing (PNG Extensions 1.4.0 §3.6) is read on open and written back on save, so the file still says it is a stereo pair (#6063) |
| 2026-10-15 | minor | PSD guides: image resource 1092 (guides v2) gives guides their own colours on import; 1092 and 1097 (guide GUIDs) are written back only while they still describe the document's guides, so an edit no longer leaves Photoshop a stale second guide list (#5966) |
| 2026-10-14 | minor | PSD smart filter masks: Photoshop stores a smart object's filtered pixels without its filter mask and mixes the unfiltered ones (`FEid`) back in; import now mixes them and export writes them that way, so sf-filter-mask-hard, -gradient and sf-stack-blend-and-mask pass; Photoshop-oracle floor 185 → 188 of 258 |