PhotoCraft: An open-source, photo editor written by scratch in pure Rust
PSD: Distort smart filters and Invert open and save like Photoshop
abc2cbb464a334ca36
diff --git a/crates/io/src/smart_map.rs b/crates/io/src/smart_map.rsindex ab9349fbd..cb100ca55 100644--- a/crates/io/src/smart_map.rs+++ b/crates/io/src/smart_map.rs@@ -144,7 +144,7 @@ const fn code(c: &[u8; 4]) -> i32 {i32::from_be_bytes(*c)}-const KNOWN: [Known; 14] = [+const KNOWN: [Known; 22] = [Known { class: "GsnB", filter_id: code(b"GsnB"), name: "Gaussian Blur...", command: "filter.blur.gaussianBlur" },Known { class: "boxblur", filter_id: 840, name: "Box Blur...", command: "filter.blur.boxBlur" },Known { class: "MtnB", filter_id: code(b"MtnB"), name: "Motion Blur...", command: "filter.blur.motionBlur" },@@ -160,8 +160,40 @@ const KNOWN: [Known; 14] = [Known { class: "Crvs", filter_id: code(b"Crvs"), name: "Curves...", command: "image.adjustments.curves" },// Shadows/Highlights at its defaults stores no `Fltr` (`hasoptions` false).Known { class: "adaptCorrect", filter_id: 781, name: "Shado&ws/Highlights...", command: "image.adjustments.shadowsHighlights" },+ // Distort (Photoshop 27, `corpus`-style check: one smart object per filter saved from Photoshop+ // and read back). Shear isn't mapped yet: its curve lives on Photoshop's own grid.+ Known { class: "Pnch", filter_id: code(b"Pnch"), name: "Pinch...", command: "filter.distort.pinch" },+ Known { class: "Plr ", filter_id: code(b"Plr "), name: "Polar Coordinates...", command: "filter.distort.polarCoordinates" },+ Known { class: "Rple", filter_id: code(b"Rple"), name: "Ripple...", command: "filter.distort.ripple" },+ Known { class: "Sphr", filter_id: code(b"Sphr"), name: "Spherize...", command: "filter.distort.spherize" },+ Known { class: "Twrl", filter_id: code(b"Twrl"), name: "Twirl...", command: "filter.distort.twirl" },+ Known { class: "Wave", filter_id: code(b"Wave"), name: "Wave...", command: "filter.distort.wave" },+ Known { class: "ZgZg", filter_id: code(b"ZgZg"), name: "ZigZag...", command: "filter.distort.zigZag" },+ // Image › Adjustments › Invert on a smart object: no settings, so no `Fltr` (yet `hasoptions`).+ Known { class: "Invr", filter_id: code(b"Invr"), name: "Invert", command: "image.adjustments.invert" },];+/// Photoshop enum codes ↔ PhotoCraft parameter values, per filter setting.+const RIPPLE_SIZE: [(&[u8; 4], &str); 3] = [(b"Sml ", "small"), (b"Mdm ", "medium"), (b"Lrg ", "large")];+const SPHERIZE_MODE: [(&[u8; 4], &str); 3] = [(b"Nrml", "normal"), (b"HrzO", "horizontalOnly"), (b"VrtO", "verticalOnly")];+const POLAR_MODE: [(&[u8; 4], &str); 2] = [(b"RctP", "rectangularToPolar"), (b"PlrR", "polarToRectangular")];+const ZIGZAG_STYLE: [(&[u8; 4], &str); 3] = [(b"PndR", "pondRipples"), (b"OtFr", "outFromCenter"), (b"ArnC", "aroundCenter")];+const WAVE_TYPE: [(&[u8; 4], &str); 3] = [(b"WvSn", "sine"), (b"WvTr", "triangle"), (b"WvSq", "square")];+const UNDEFINED_AREAS: [(&[u8; 4], &str); 2] = [(b"WrpA", "wrap"), (b"RptE", "repeat")];++/// The PhotoCraft value of the enum `key` in `f` (`None`: absent or a code we don't know).+fn enum_param(f: &Descriptor, key: &str, table: &[(&[u8; 4], &'static str)]) -> Option<&'static str> {+ let code = enum_of(f, key)?;+ table.iter().find(|(c, _)| c.as_slice() == code).map(|(_, v)| *v)+}++/// The enum value Photoshop stores for PhotoCraft's `p[key]` (its `default` when unset).+fn enum_value(p: &J, key: &str, default: &str, type_id: &str, table: &[(&[u8; 4], &str)]) -> Result<Value, String> {+ let v = p.get(key).and_then(J::as_str).unwrap_or(default);+ let (code, _) = table.iter().find(|(_, name)| *name == v).ok_or_else(|| format!("{key} `{v}` has no PSD equivalent"))?;+ Ok(Value::Enumerated { type_id: Id::new(type_id), value: Id::Code(**code) })+}+fn known_by_command(command: &str) -> Option<&'static Known> {KNOWN.iter().find(|k| k.command == command)}@@ -263,8 +295,31 @@ fn curves_params(f: &Descriptor) -> Option<J> {fn known_params(class: &str, f: Option<&Descriptor>) -> Option<(&'static str, J)> {let k = KNOWN.iter().find(|k| k.class == class)?;let params = match (class, f) {- ("adaptCorrect", None) => json!({}),+ ("adaptCorrect" | "Invr", None) => json!({}),(_, None) => return None,+ ("Pnch", Some(f)) => json!({"amount": int(f, "Amnt")?}),+ ("Twrl", Some(f)) => json!({"angle": int(f, "Angl")?}),+ ("Rple", Some(f)) => json!({"amount": int(f, "Amnt")?, "size": enum_param(f, "RplS", &RIPPLE_SIZE)?}),+ ("Sphr", Some(f)) => json!({"amount": int(f, "Amnt")?, "mode": enum_param(f, "SphM", &SPHERIZE_MODE)?}),+ ("Plr ", Some(f)) => json!({"mode": enum_param(f, "Cnvr", &POLAR_MODE)?}),+ ("ZgZg", Some(f)) => json!({"amount": int(f, "Amnt")?, "ridges": int(f, "NmbR")?, "style": enum_param(f, "ZZTy", &ZIGZAG_STYLE)?}),+ ("Wave", Some(f)) => {+ // PhotoCraft's Wave has no Scale: a scaled wave stays verbatim rather than lose it.+ if int(f, "SclH")? != 100 || int(f, "SclV")? != 100 {+ return None;+ }+ json!({+ "type": enum_param(f, "Wvtp", &WAVE_TYPE)?,+ "generators": int(f, "NmbG")?,+ "wavelengthMin": int(f, "WLMn")?,+ "wavelengthMax": int(f, "WLMx")?,+ "amplitudeMin": int(f, "AmMn")?,+ "amplitudeMax": int(f, "AmMx")?,+ "undefinedAreas": enum_param(f, "UndA", &UNDEFINED_AREAS)?,+ // `RndS` is a signed long; the seed is its bit pattern.+ "seed": (int(f, "RndS")? as u32),+ })+ }("GsnB" | "boxblur" | "Mdn " | "HghP" | "Mxm " | "Mnm ", Some(f)) => json!({"radius": px(f, "Rds ")?}),("MtnB", Some(f)) => json!({"angle": int(f, "Angl")?, "distance": px(f, "Dstn")?}),("UnsM", Some(f)) => json!({"amount": px(f, "Amnt")?, "radius": px(f, "Rds ")?, "threshold": int(f, "Thsh")?}),@@ -455,6 +510,30 @@ fn fltr_for(k: &Known, p: &J) -> Result<Option<Descriptor>, String> {}return Ok(None);}+ // Keys and order as Photoshop writes them.+ "Pnch" => d.with("Amnt", pi(p, "amount", 50.0)),+ "Twrl" => d.with("Angl", pi(p, "angle", 50.0)),+ "Rple" => d.with("Amnt", pi(p, "amount", 100.0)).with("RplS", enum_value(p, "size", "medium", "RplS", &RIPPLE_SIZE)?),+ "Sphr" => d.with("Amnt", pi(p, "amount", 100.0)).with("SphM", enum_value(p, "mode", "normal", "SphM", &SPHERIZE_MODE)?),+ "Plr " => d.with("Cnvr", enum_value(p, "mode", "rectangularToPolar", "Cnvr", &POLAR_MODE)?),+ "ZgZg" => d+ .with("Amnt", pi(p, "amount", 10.0))+ .with("NmbR", pi(p, "ridges", 5.0))+ .with("ZZTy", enum_value(p, "style", "pondRipples", "ZZTy", &ZIGZAG_STYLE)?),+ "Wave" => {+ let seed = p.get("seed").and_then(J::as_u64).and_then(|s| u32::try_from(s).ok()).unwrap_or(0);+ d.with("Wvtp", enum_value(p, "type", "sine", "Wvtp", &WAVE_TYPE)?)+ .with("NmbG", pi(p, "generators", 5.0))+ .with("WLMn", pi(p, "wavelengthMin", 10.0))+ .with("WLMx", pi(p, "wavelengthMax", 120.0))+ .with("AmMn", pi(p, "amplitudeMin", 5.0))+ .with("AmMx", pi(p, "amplitudeMax", 35.0))+ .with("SclH", Value::Integer(100))+ .with("SclV", Value::Integer(100))+ .with("UndA", enum_value(p, "undefinedAreas", "wrap", "UndA", &UNDEFINED_AREAS)?)+ .with("RndS", Value::Integer(seed as i32))+ }+ "Invr" => return Ok(None),_ => return Err("not a recognized filter".into()),}))}@@ -486,7 +565,8 @@ pub fn item_for_filter(f: &SmartFilter) -> Result<Descriptor, String> {.with("Nm ", Value::Text(UnicodeString::new_nul(k.name))).with("blendOptions", blend_options(f)).with("enab", Value::Boolean(f.visible))- .with("hasoptions", Value::Boolean(fltr.is_some()))+ // Photoshop marks Invert as having options although it stores none.+ .with("hasoptions", Value::Boolean(fltr.is_some() || k.class == "Invr")).with("FrgC", rgbc(0.0)).with("BckC", rgbc(255.0));if let Some(x) = fltr {@@ -878,6 +958,18 @@ mod tests {sf("image.adjustments.shadowsHighlights", json!({})),// A pencil-drawn curve: 256 output levels (`Mpng`).sf("image.adjustments.curves", json!({"pencil": true, "points": (0..256).rev().collect::<Vec<i32>>()})),+ sf("filter.distort.pinch", json!({"amount": -28})),+ sf("filter.distort.polarCoordinates", json!({"mode": "rectangularToPolar"})),+ sf("filter.distort.ripple", json!({"amount": 250, "size": "small"})),+ sf("filter.distort.spherize", json!({"amount": 60, "mode": "verticalOnly"})),+ sf("filter.distort.twirl", json!({"angle": -355})),+ sf(+ "filter.distort.wave",+ json!({"type": "square", "generators": 3, "wavelengthMin": 20, "wavelengthMax": 90, "amplitudeMin": 4, "amplitudeMax": 30,+ "undefinedAreas": "repeat", "seed": 4_000_000_000u32}),+ ),+ sf("filter.distort.zigZag", json!({"amount": 30, "ridges": 7, "style": "aroundCenter"})),+ sf("image.adjustments.invert", json!({})),];for mut f in filters {f.blend = BlendMode::Overlay;@@ -890,17 +982,117 @@ mod tests {}}+ /// The `Fltr` descriptors Photoshop 27 wrote for these filters (one smart object each, saved as+ /// PSD and read back), with the settings PhotoCraft reads from them.+ const PHOTOSHOP_FLTR: [(&str, &str, &str); 6] = [+ ("filter.distort.pinch", r#"{"amount":-28}"#, "0000000600500069006e00630068000000000000506e63680000000100000000416d6e746c6f6e67ffffffe4"),+ ("filter.distort.twirl", r#"{"angle":-355}"#, "000000060054007700690072006c0000000000005477726c0000000100000000416e676c6c6f6e67fffffe9d"),+ (+ "filter.distort.ripple",+ r#"{"amount":-273,"size":"large"}"#,+ "000000070052006900700070006c006500000000000052706c650000000200000000416d6e746c6f6e67fffffeef0000000052706c53656e756d0000000052706c53000000004c726720",+ ),+ (+ "filter.distort.spherize",+ r#"{"amount":-1,"mode":"horizontalOnly"}"#,+ "00000009005300700068006500720069007a0065000000000000537068720000000200000000416d6e746c6f6e67ffffffff000000005370684d656e756d000000005370684d0000000048727a4f",+ ),+ (+ "filter.distort.polarCoordinates",+ r#"{"mode":"polarToRectangular"}"#,+ "000000120050006f006c0061007200200043006f006f007200640069006e0061007400650073000000000000506c72200000000100000000436e7672656e756d00000000436e767200000000506c7252",+ ),+ (+ "filter.distort.zigZag",+ r#"{"amount":-42,"ridges":11,"style":"pondRipples"}"#,+ "00000007005a00690067005a006100670000000000005a675a670000000300000000416d6e746c6f6e67ffffffd6000000004e6d62526c6f6e670000000b000000005a5a5479656e756d000000005a5a547900000000506e6452",+ ),+ ];++ /// Photoshop's Wave with Scale 100% / 28%: PhotoCraft's Wave has no Scale.+ const PHOTOSHOP_WAVE: &str = "000000050057006100760065000000000000576176650000000a0000000057767470656e756d00000000577674700000000057765472000000004e6d62476c6f6e670000014c00000000574c4d6e6c6f6e67000000d500000000574c4d786c6f6e67000000d600000000416d4d6e6c6f6e67000000f000000000416d4d786c6f6e67000000f10000000053636c486c6f6e67000000640000000053636c566c6f6e670000001c00000000556e6441656e756d00000000556e6441000000005772704100000000526e64536c6f6e670013624a";++ fn photoshop_item(fltr: Descriptor) -> Descriptor {+ let id = code(fltr.class_id.as_bytes().try_into().unwrap());+ Descriptor::new("filterFX")+ .with("Nm ", Value::Text(UnicodeString::new_nul("from Photoshop")))+ .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("Fltr", Value::Descriptor(fltr))+ .with("filterID", Value::Integer(id))+ }++ /// Distort smart filters from a Photoshop file open as their PhotoCraft filters, and the ones+ /// PhotoCraft writes carry the same settings, keys and order as Photoshop's.+ #[test]+ fn photoshop_distort_filters_read_and_write_like_photoshop() {+ for (command, params, hex) in PHOTOSHOP_FLTR {+ let ps = Descriptor::from_bytes(&from_hex(hex).unwrap()).unwrap();+ let f = filter_from_item(&photoshop_item(ps.clone()));+ let want: J = serde_json::from_str(params).unwrap();+ assert_eq!((f.command.as_str(), &f.params), (command, &want), "{command}");+ let ours = match item_for_filter(&f).unwrap().get("Fltr") {+ Some(Value::Descriptor(d)) => d.clone(),+ other => panic!("{command}: no Fltr ({other:?})"),+ };+ assert_eq!((&ours.class_id, &ours.items), (&ps.class_id, &ps.items), "{command}: same keys, values and order");+ }+ // A scaled wave can't be modelled, so it stays Photoshop's, unchanged.+ let wave = Descriptor::from_bytes(&from_hex(PHOTOSHOP_WAVE).unwrap()).unwrap();+ let item = photoshop_item(wave);+ let f = filter_from_item(&item);+ assert_eq!(f.command, UNSUPPORTED_FILTER);+ assert_eq!(item_for_filter(&f).unwrap().get("Fltr"), item.get("Fltr"));+ // Unscaled, it opens as PhotoCraft's Wave (Triangle, the other settings as stored).+ let mut unscaled = match item.get("Fltr") {+ Some(Value::Descriptor(d)) => d.clone(),+ _ => unreachable!(),+ };+ set(&mut unscaled, "SclV", Value::Integer(100));+ let f = filter_from_item(&photoshop_item(unscaled.clone()));+ assert_eq!(+ (f.command.as_str(), &f.params),+ (+ "filter.distort.wave",+ &json!({"type": "triangle", "generators": 332, "wavelengthMin": 213, "wavelengthMax": 214, "amplitudeMin": 240, "amplitudeMax": 241,+ "undefinedAreas": "wrap", "seed": 1270346})+ )+ );+ assert_eq!(item_for_filter(&f).unwrap().get("Fltr"), Some(&Value::Descriptor(unscaled)));+ }++ /// Invert on a smart object: Photoshop stores no `Fltr`, but `hasoptions` and its filter id.+ #[test]+ fn invert_smart_filter_reads_and_writes_like_photoshop() {+ let item = Descriptor::new("filterFX")+ .with("Nm ", Value::Text(UnicodeString::new_nul("Invert")))+ .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(255.0))+ .with("BckC", rgbc(0.0))+ .with("filterID", Value::Integer(code(b"Invr")));+ let f = filter_from_item(&item);+ assert_eq!((f.command.as_str(), &f.params), ("image.adjustments.invert", &json!({})));+ let back = item_for_filter(&f).unwrap();+ assert_eq!(back.get("Fltr"), None);+ assert_eq!(back.get("hasoptions"), Some(&Value::Boolean(true)));+ assert_eq!(back.get("filterID"), Some(&Value::Integer(code(b"Invr"))));+ assert_eq!(back.get("Nm "), Some(&Value::Text(UnicodeString::new_nul("Invert"))));+ }+#[test]fn unknown_filters_keep_their_descriptor() {let item = Descriptor::new("filterFX")- .with("Nm ", Value::Text(UnicodeString::new_nul("Twirl...")))+ .with("Nm ", Value::Text(UnicodeString::new_nul("Crystallize..."))).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("Fltr", Value::Descriptor(Descriptor::new("Twrl").with("Angl", Value::Integer(50))))- .with("filterID", Value::Integer(code(b"Twrl")));+ .with("Fltr", Value::Descriptor(Descriptor::new("Crst").with("ClSz", Value::Integer(10))))+ .with("filterID", Value::Integer(code(b"Crst")));let f = filter_from_item(&item);assert_eq!(f.command, UNSUPPORTED_FILTER);- assert_eq!(f.params["name"], "Twirl...");+ assert_eq!(f.params["name"], "Crystallize...");assert_eq!(item_for_filter(&f).unwrap(), item);// Hidden and re-blended: only those keys change.let mut g = f.clone();@@ -917,7 +1109,8 @@ mod tests {fn unwritable_filters_are_reported_not_fatal() {let stack = FilterStack {filters: vec![- sf("filter.distort.twirl", json!({"angle": 50})),+ // Shear's curve isn't mapped to Photoshop's grid yet.+ sf("filter.distort.shear", json!({"amount": 40})),sf("filter.blur.gaussianBlur", json!({"radius": 2.0})),sf(UNSUPPORTED_FILTER, json!({"psd": "zz"})),sf("image.adjustments.levels", json!({"cyan": {"gamma": 2.0}})),diff --git a/docs/file-format-parity.md b/docs/file-format-parity.mdindex cb2ca13ac..18a898b59 100644--- a/docs/file-format-parity.md+++ b/docs/file-format-parity.md@@ -1,6 +1,6 @@# File-format parity-> **Last reviewed:** 2026-10-12 · **Last updated:** 2026-10-13 · **Change:** minor (palette PNGs open in Indexed Color mode, #4804; earlier: CMYK Invert, Posterize and Threshold on the inks; 16-bit Lab Invert; Photoshop-oracle floor 145/258; `.atn` import maps 112 events; DDS textures open, #1327; DPX read and write, #1049; #2465 fixed: CS2 placed layers with only a `PlLd` block open their contents; Photoshop Actions `.atn` read and write; `.shc` contour files read and write; earlier the same day: indexed BMP palette mode; Save keeps a JPEG's quality and a WebP's encoding) · **Target:** Adobe Photoshop 2026+> **Last reviewed:** 2026-10-12 · **Last updated:** 2026-10-13 · **Change:** minor (PSD smart filters: Pinch, Polar Coordinates, Ripple, Spherize, Twirl, Wave and ZigZag, and Invert, read and written like Photoshop; palette PNGs open in Indexed Color mode, #4804; earlier: CMYK Invert, Posterize and Threshold on the inks; 16-bit Lab Invert; Photoshop-oracle floor 145/258; `.atn` import maps 112 events; DDS textures open, #1327; DPX read and write, #1049; #2465 fixed: CS2 placed layers with only a `PlLd` block open their contents; Photoshop Actions `.atn` read and write; `.shc` contour files read and write; earlier the same day: indexed BMP palette mode; Save keeps a JPEG's quality and a WebP's encoding) · **Target:** Adobe Photoshop 2026Every format Photoshop 2026 (27.11.0) reads or writes, with PhotoCraft's support, fidelity andtests. Photoshop's list comes from its `Info.plist` `CFBundleDocumentTypes`, the PiPL read/write@@ -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 145/258, io 146/170, psd-tools 236/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 (22 mapped: blurs, Unsharp Mask, noise, Other, Mosaic, Emboss, Levels, Curves, Shadows/Highlights, Invert and the Distort filters but Shear; rest verbatim), slices, comps, Advanced Blending. Oracle floors: Photoshop-authored 145/258, io 146/170, psd-tools 236/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) |@@ -142,6 +142,7 @@ remaining gaps in #1606 stay tracked with per-sample corpus ceilings.| Date | Change | Summary ||---|---|---|+| 2026-10-13 | minor | PSD smart filters: Pinch, Polar Coordinates, Ripple, Spherize, Twirl, Wave, ZigZag and Invert open as editable filters and are saved instead of dropped (checked against a Photoshop 27 file); a scaled Wave and Shear stay verbatim || 2026-10-13 | minor | Palette PNGs open in Indexed Color mode (#4804) || 2026-10-12 | minor | `.atn`: 112 events mapped (Select and Mask, Filter Gallery Spatter, Ripple, Crystallize, Pattern Fill layers, mask targeting, Step Backward, points, Bitmap) || 2026-10-12 | minor | DDS textures open, read-only (#1327) |