PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge PR #8229: Integration batch: 8 PRs on main
aiongg1a388f8ba7dd
diff --cc crates/io/src/smart_map.rsindex f18dd444b,3230271ff..27431e864--- a/crates/io/src/smart_map.rs+++ b/crates/io/src/smart_map.rs@@@ -317,50 -314,32 +317,64 @@@ fn int_in(f: &Descriptor, key: &str, loint(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.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(|key| f.items.iter().filter(|(k, _)| k.is(key)).count() <= 1)+}++/// Our name for enum `key`'s value in `f` (of enum type `type_id`), from `(PSD value, our name)`+/// pairs. An enum of another type is not one we know, so the filter stays verbatim.+fn enum_name(f: &Descriptor, key: &str, type_id: &str, table: &[(&[u8; 4], &'static str)]) -> Option<&'static str> {+ let Some(Value::Enumerated { type_id: t, value }) = f.get(key) else { return None };+ if t.as_bytes() != type_id.as_bytes() {+ return None;+ }+ table.iter().find(|(c, _)| value.as_bytes() == &c[..]).map(|(_, n)| *n)}+ /// True when `f` holds each of the given keys once and nothing else: a repeated key would be+ /// written back only once.+ fn exact_keys(f: &Descriptor, keys: &[&str]) -> bool {+ f.items.len() == keys.len() && only_keys(f, keys) && keys.iter().all(|x| f.items.iter().any(|(k, _)| k.is(x)))+ }++/// Finite `key` of `f` stored as a plain double (the type Photoshop writes and we write back).+fn double(f: &Descriptor, key: &str) -> Option<f64> {+ match f.get(key)? {+ Value::Double(v) if v.is_finite() => Some(*v),+ _ => None,+ }+}++/// `p[key]` (`default` when absent) when it is a number in `lo..=hi`, the range the filter opens+/// with; anything else is an error, so a saved filter always opens as the same editable filter.+fn pf_range(p: &J, key: &str, default: f64, lo: f64, hi: f64) -> Result<f64, String> {+ match p.get(key) {+ None | Some(J::Null) => Ok(default),+ Some(v) => v.as_f64().filter(|x| (lo..=hi).contains(x)).ok_or_else(|| format!("setting `{key}` ({v}) is not a number in {lo}..={hi}")),+ }+}++/// Integer `p[key]` (rounded; `default` when absent) in `lo..=hi`, as a PSD long.+fn pi_range(p: &J, key: &str, default: f64, lo: i32, hi: i32) -> Result<Value, String> {+ let v = pf_range(p, key, default, f64::from(i32::MIN), f64::from(i32::MAX))?.round();+ let range = f64::from(lo)..=f64::from(hi);+ if !range.contains(&v) {+ return Err(format!("setting `{key}` ({v}) is not a number in {lo}..={hi}"));+ }+ Ok(Value::Integer(v as i32))+}++ /// The `doub` at `key` (not a long or a unit value, which a re-save would turn into a `doub`).+ fn double_at(d: &Descriptor, key: &str) -> Option<f64> {+ match d.get(key)? {+ Value::Double(v) if v.is_finite() => Some(*v),+ _ => None,+ }+ }+-/// 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) {@@@ -616,16 -606,11 +630,21 @@@ fn known_params(class: &str, f: Option<"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)?,- ("LnsF", Some(f)) => {- let centre = get_desc(f, "FlrC")?;- let lens = enum_of(f, "Lns ")?;- let lens = LENSES.iter().find(|(c, _)| lens == &c[..])?.1;- json!({"brightness": int(f, "Brgh")?, "centerX": px(centre, "Hrzn")?, "centerY": px(centre, "Vrtc")?, "lens": lens})++ // Only the settings and value types Photoshop writes (and we write back); anything else++ // keeps the filter verbatim.++ ("LnsF", Some(f)) if exact_keys(f, &["Brgh", "FlrC", "Lns "]) => {++ let centre = get_desc(f, "FlrC").filter(|c| c.class_id.is("Pnt ") && exact_keys(c, &["Hrzn", "Vrtc"]))?;++ let Some(Value::Enumerated { type_id, value }) = f.get("Lns ") else { return None };++ if !type_id.is("Lns ") {++ return None;++ }++ let lens = LENSES.iter().find(|(c, _)| value.as_bytes() == &c[..])?.1;++ json!({"brightness": int(f, "Brgh")?, "centerX": double_at(centre, "Hrzn")?, "centerY": double_at(centre, "Vrtc")?, "lens": lens})+ }("Lvls", Some(f)) => levels_params(f)?,("Crvs", Some(f)) => curves_params(f)?,_ => return None,