PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge origin/main into feat/file-215-2-other
David Manne57452d94566
diff --cc crates/io/src/smart_map.rsindex 1a8c29e91,5ec4db118..244052856--- a/crates/io/src/smart_map.rs+++ b/crates/io/src/smart_map.rs@@@ -183,21 -294,45 +294,46 @@@ fn int(d: &Descriptor, key: &str) -> Op}}- /// `Some(())` when every setting of `f` is one of `keys`, each at most once: a filter with a- /// setting we don't write back (or a repeated one) stays verbatim instead of losing it on save.- fn only_keys(f: &Descriptor, keys: &[&str]) -> Option<()> {- let known = f.items.iter().all(|(k, _)| keys.iter().any(|key| k.is(key)));- (known && keys.iter().all(|key| f.items.iter().filter(|(k, _)| k.is(key)).count() <= 1)).then_some(())+ /// `v` when it lies in `lo..=hi` (the filter dialog's range); out-of-range settings are not+ /// opened as editable filters, so a re-save keeps Photoshop's value instead of a clamped one.+ fn in_range(v: f64, lo: f64, hi: f64) -> Option<f64> {+ (lo..=hi).contains(&v).then_some(v)}- /// The value of the enum `key` when its type is `type_id` (a mistyped enum is not modelled).- fn typed_enum<'a>(f: &'a Descriptor, key: &str, type_id: &str) -> Option<&'a [u8]> {- match f.get(key)? {- Value::Enumerated { type_id: t, value } if t.is(type_id) => Some(value.as_bytes()),- _ => None,- }+ /// Integer `key` of `f` when it lies in `lo..=hi`.+ fn int_in(f: &Descriptor, key: &str, lo: i32, hi: i32) -> Option<i32> {+ int(f, key).filter(|v| (lo..=hi).contains(v))+ }+-/// True when `f` holds only the given keys. A filter with a setting we don't know about (Radial-/// Blur's off-centre centre, for one) is kept verbatim rather than losing that setting.++/// True when `f` holds only the given keys, each at most once. A filter with a setting we don't++/// know about (Radial Blur's off-centre centre, for one) or a repeated one is kept verbatim++/// rather than losing that setting on save.+ fn only_keys(f: &Descriptor, keys: &[&str]) -> bool {- f.items.iter().all(|(k, _)| keys.iter().any(|x| k.is(x)))++ f.items.iter().all(|(k, _)| keys.iter().any(|x| k.is(x))) && keys.iter().all(|x| f.items.iter().filter(|(k, _)| k.is(x)).count() <= 1)+ }++ /// Our name for enum `key`'s value in `f`, from `(PSD value, our name)` pairs.+ fn enum_name(f: &Descriptor, key: &str, table: &[(&[u8; 4], &'static str)]) -> Option<&'static str> {+ let v = enum_of(f, key)?;+ table.iter().find(|(c, _)| v == &c[..]).map(|(_, n)| *n)+ }++ /// Enum `type_id` holding the PSD value of our name `p[key]` (`default` when absent).+ fn enum_param(p: &J, key: &str, default: &str, type_id: &str, table: &[(&[u8; 4], &'static str)]) -> Result<Value, String> {+ let name = match p.get(key) {+ None | Some(J::Null) => default,+ Some(v) => v.as_str().ok_or_else(|| format!("setting `{key}` is not text"))?,+ };+ let code = table.iter().find(|(_, n)| *n == name).map(|(c, _)| **c).ok_or_else(|| format!("{key} `{name}` has no PSD equivalent"))?;+ Ok(Value::Enumerated { type_id: Id::new(type_id), value: Id::Code(code) })}+ /// Smart Blur `SmBQ` / `SmBM` and Radial Blur `BlrM` / `BlrQ` values (measured in Photoshop).+ const SMART_BLUR_QUALITY: [(&[u8; 4], &str); 3] = [(b"SBQL", "low"), (b"SBQM", "medium"), (b"SBQH", "high")];+ const SMART_BLUR_MODE: [(&[u8; 4], &str); 3] = [(b"SBMN", "normal"), (b"SBME", "edgeOnly"), (b"SBMO", "overlayEdge")];+ const RADIAL_METHOD: [(&[u8; 4], &str); 2] = [(b"Spn ", "spin"), (b"Zm ", "zoom")];+ const RADIAL_QUALITY: [(&[u8; 4], &str); 3] = [(b"Drft", "draft"), (b"Gd ", "good"), (b"Bst ", "best")];+fn channel_key(d: &Descriptor) -> Option<&'static str> {let Some(Value::Reference(items)) = d.get("Chnl") else { return None };let Some(ReferenceItem::Enumerated { value, .. }) = items.first() else { return None };@@@ -291,7 -485,6 +486,9 @@@ fn long_in_range(v: Option<i32>, (lo, h/// `filter.other.custom` params for a `Cstm` filter: `Mtrx` is 25 longs, row-major 5 × 5. Values/// outside Photoshop's ranges are not modelled (the filter stays verbatim).fn custom_params(f: &Descriptor) -> Option<J> {- only_keys(f, &["Mtrx", "Scl ", "Ofst"])?;++ if !only_keys(f, &["Mtrx", "Scl ", "Ofst"]) {++ return None;++ }let Some(Value::List(m)) = f.get("Mtrx") else { return None };if m.len() != 25 {return None;@@@ -324,12 -519,62 +523,61 @@@ 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")?}),+ ("Mztn", Some(f)) => {+ let t = enum_of(f, "MztT")?;+ json!({"type": MEZZOTINT_TYPES.iter().find(|(c, _)| t == &c[..])?.1})+ }+ ("ClrH", Some(f)) => color_halftone_params(f)?,("Embs", Some(f)) => json!({"angle": int(f, "Angl")?, "height": int(f, "Hght")?, "amount": int(f, "Amnt")?}),- ("Ofst", Some(f)) => {- only_keys(f, &["Hrzn", "Vrtc", "Fl "])?;- let fill = OFFSET_FILL.iter().find(|(psd, _)| typed_enum(f, "Fl ", "FlMd") == Some(psd.as_bytes()))?.1;- json!({"horizontal": int(f, "Hrzn")?, "vertical": int(f, "Vrtc")?, "undefinedAreas": fill})+ (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_from(f, "Md ", "DfsM", &DIFFUSE_MODES)?}),+ ("TrcC", Some(f)) => json!({"level": int_in(f, "Lvl ", 0, 255)?, "edge": enum_from(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+ }+ ("Wnd ", Some(f)) => json!({+ "method": enum_from(f, "WndM", "WndM", &WIND_METHODS)?,+ "direction": enum_from(f, "Drct", "Drct", &WIND_DIRECTIONS)?,+ }),+ ("Extr", Some(f)) => json!({+ "type": enum_from(f, "ExtT", "ExtT", &EXTRUDE_TYPES)?,+ "size": int_in(f, "ExtS", EXTRUDE_SIZE.0, EXTRUDE_SIZE.1)?,+ "depth": int_in(f, "ExtD", EXTRUDE_DEPTH.0, EXTRUDE_DEPTH.1)?,+ "depthMode": enum_from(f, "ExtR", "ExtR", &EXTRUDE_DEPTHS)?,+ "solidFrontFaces": bool_at(f, "ExtF")?,+ "maskIncompleteBlocks": bool_at(f, "ExtM")?,+ }),+ ("Tls ", Some(f)) => tiles_params(f, item)?,- ("Ofst", Some(f)) => {- let fill = OFFSET_FILL.iter().find(|(psd, _)| enum_of(f, "Fl ") == Some(psd.as_bytes()))?.1;- json!({"horizontal": int(f, "Hrzn")?, "vertical": int(f, "Vrtc")?, "undefinedAreas": fill})++ ("Ofst", Some(f)) if only_keys(f, &["Hrzn", "Vrtc", "Fl "]) => {++ json!({"horizontal": int(f, "Hrzn")?, "vertical": int(f, "Vrtc")?, "undefinedAreas": enum_from(f, "Fl ", "FlMd", &OFFSET_FILL)?})}("Cstm", Some(f)) => custom_params(f)?,("Lvls", Some(f)) => levels_params(f)?,@@@ -1327,12 -2237,8 +2240,12 @@@ mod testsoffset_fltr(i(1), unit(b"#Pxl", f64::NAN), enumv("FlMd", "Wrp ")),Descriptor::new("Ofst").with("Hrzn", i(1)).with("Fl ", enumv("FlMd", "Wrp ")),Descriptor::new("Ofst"),+ // A mistyped enum, a setting we don't write back and a repeated one.+ offset_fltr(i(1), i(2), enumv("Xxxx", "Wrp ")),+ offset_fltr(i(1), i(2), enumv("FlMd", "Wrp ")).with("Xtra", i(1)),+ offset_fltr(i(1), i(2), enumv("FlMd", "Wrp ")).with("Hrzn", i(3)),] {- let item = measured_item(b"Ofst", "Offset...", fltr);+ let item = measured_named_item(b"Ofst", "Offset...", fltr);let f = filter_from_item(&item);assert_eq!(f.command, UNSUPPORTED_FILTER, "{item:?}");assert_eq!(item_for_filter(&f).unwrap().to_bytes(), item.to_bytes(), "kept byte for byte");@@@ -1412,10 -2318,8 +2325,10 @@@Descriptor::new("Cstm").with("Scl ", i(1)).with("Ofst", i(0)).with("Mtrx", i(5)),Descriptor::new("Cstm").with("Scl ", i(1)).with("Mtrx", Value::List(longs(&SHARPEN))),Descriptor::new("Cstm"),+ // A setting we don't write back.+ custom_fltr(i(1), i(0), longs(&SHARPEN)).with("Xtra", i(1)),] {- let item = measured_item(b"Cstm", "Custom...", fltr);+ let item = measured_named_item(b"Cstm", "Custom...", fltr);let f = filter_from_item(&item);assert_eq!(f.command, UNSUPPORTED_FILTER, "{item:?}");assert_eq!(item_for_filter(&f).unwrap().to_bytes(), item.to_bytes(), "kept byte for byte");