diff --git a/crates/psd/src/smart.rs b/crates/psd/src/smart.rs index 9a95c61f3..7ed444f45 100644 --- a/crates/psd/src/smart.rs +++ b/crates/psd/src/smart.rs @@ -74,11 +74,17 @@ fn quad_from(t: [f64; 8]) -> [[f64; 2]; 4] { /// The patch boundaries of a custom envelope: a list of numbers, or (as Photoshop writes a /// `quiltWarp`) an object array whose body holds them under the same key. -fn quilt_slices(env: &Descriptor, key: &str) -> Vec { - match env.get(key) { - Some(Value::ObjectArray(o)) => o.body.get_numbers(key), - _ => env.get_numbers(key), - } +fn quilt_slices(env: &Descriptor, key: &str) -> Result> { + let Some(value) = env.get(key) else { return Ok(vec![]) }; + // Filtering nonnumbers would change the number and positions of patches. + // Only absence, not a malformed or empty present value, means one patch. + let value = if let Value::ObjectArray(o) = value { o.body.get(key) } else { Some(value) }; + let values = match value { + Some(Value::UnitFloats { values, .. }) => Some(values.clone()), + Some(Value::List(values)) => values.iter().map(Value::as_number).collect::>>(), + _ => None, + }; + values.filter(|values| !values.is_empty()).ok_or_else(|| PsdError::Unsupported("invalid quilt slice values".into())) } /// The public mesh has no slice coordinates, so only uniform divisions can be @@ -90,18 +96,23 @@ fn quilt_patches(slices: &[f64]) -> Result { if slices.len() < 2 || slices.iter().any(|v| !v.is_finite()) { return Err(PsdError::Unsupported("invalid quilt divisions".into())); } - let step = slices[1] - slices[0]; - if !step.is_finite() || step <= 0.0 { + let patches = slices.len() - 1; + let extent = slices[patches] - slices[0]; + if !extent.is_finite() || extent <= 0.0 { return Err(PsdError::Unsupported("invalid quilt divisions".into())); } - let tolerance = step.abs() * 1e-9; - if slices.windows(2).any(|pair| { - let width = pair[1] - pair[0]; - !width.is_finite() || width <= 0.0 || (width - step).abs() > tolerance - }) { + // Compare against the same endpoint interpolation the writer uses. At large + // coordinate offsets a uniform grid can have differently rounded adjacent widths. + let tolerance = (extent / patches as f64) * 1e-9; + if slices.windows(2).any(|pair| pair[1] <= pair[0]) + || slices.iter().enumerate().any(|(i, value)| { + let expected = slices[0] + extent * (i as f64 / patches as f64); + (value - expected).abs() > tolerance + }) + { return Err(PsdError::Unsupported("unequal quilt divisions".into())); } - Ok(slices.len() - 1) + Ok(patches) } impl Warp { /// Reads a `warp` descriptor; `None` for no warp (the identity, or a custom warp without a @@ -128,8 +139,8 @@ impl Warp { style, }; if let Some(env) = d.get_object("customEnvelopeWarp") { - let slice_x = quilt_slices(env, "quiltSliceX"); - let slice_y = quilt_slices(env, "quiltSliceY"); + let slice_x = quilt_slices(env, "quiltSliceX")?; + let slice_y = quilt_slices(env, "quiltSliceY")?; let cols = quilt_patches(&slice_x)?; let rows = quilt_patches(&slice_y)?; if !slice_x.is_empty() || !slice_y.is_empty() { @@ -1065,8 +1076,44 @@ mod tests { } assert_eq!(quilt_patches(&[]).unwrap(), 1); assert_eq!(quilt_patches(&[0.0, 10.0, 20.0]).unwrap(), 2); + assert_eq!(quilt_patches(&[1e16, 1e16 + 2.0, 1e16 + 6.0, 1e16 + 8.0]).unwrap(), 3); + assert!(quilt_patches(&[1e16, 1e16 + 4.0, 1e16 + 6.0, 1e16 + 8.0]).is_err()); } + #[test] + fn quilt_slice_values_are_strict_in_lists_and_object_arrays() { + let list = Value::List(vec![Value::Integer(0), Value::Double(10.0), Value::UnitFloat { unit: *b"#Pxl", value: 20.0 }]); + let unfl = Value::UnitFloats { unit: *b"#Pxl", values: vec![0.0, 10.0, 20.0] }; + let bad_list = Value::List(vec![Value::Double(0.0), Value::Text(UnicodeString::new("bad")), Value::Double(10.0), Value::Double(20.0)]); + for wrapped in [false, true] { + for (value, valid) in [ + (list.clone(), true), + (unfl.clone(), true), + (bad_list.clone(), false), + (Value::List(vec![]), false), + (Value::UnitFloats { unit: *b"#Pxl", values: vec![] }, false), + (Value::Integer(1), false), + ] { + let value = if wrapped { + Value::ObjectArray(ObjectArray { prefix: 3, body: Descriptor::new("UntF").with("quiltSliceX", value) }) + } else { + value + }; + let mut d = quilt_descriptor(vec![0.0, 10.0, 20.0], vec![0.0, 10.0]); + let Value::Descriptor(env) = &mut d.items.iter_mut().find(|(key, _)| key.is("customEnvelopeWarp")).unwrap().1 else { + panic!("envelope") + }; + env.items.iter_mut().find(|(key, _)| key.is("quiltSliceX")).unwrap().1 = value; + assert_eq!(Warp::try_from_descriptor(&d).is_ok(), valid); + let bytes = placed_layer_data("slices", [0.0; 8], |parent| parent.with("quiltWarp", Value::Descriptor(d))); + assert_eq!(SmartObject::from_placed_layer_data(&bytes).is_ok(), valid); + } + } + let absent = Descriptor::new("customEnvelopeWarp"); + assert!(quilt_slices(&absent, "quiltSliceX").unwrap().is_empty()); + let missing_body = absent.with("quiltSliceX", Value::ObjectArray(ObjectArray { prefix: 0, body: Descriptor::new("UntF") })); + assert!(quilt_slices(&missing_body, "quiltSliceX").is_err()); + } #[test] fn quilt_endpoints_define_or_match_the_normalization_bounds() { let equal = quilt_descriptor(vec![0.0, 10.0, 20.0], vec![0.0, 10.0]); diff --git a/crates/psd/src/smart/write.rs b/crates/psd/src/smart/write.rs index 413b0f406..0c9eb4588 100644 --- a/crates/psd/src/smart/write.rs +++ b/crates/psd/src/smart/write.rs @@ -78,7 +78,10 @@ impl Warp { /// Style, bend, distortions, axis, bounds and order: the keys a `warp` and a `quiltWarp` /// share, in Photoshop's order. fn head(&self, class: &str) -> Descriptor { - let [l, t, r, b] = self.bounds; + self.head_with_bounds(class, self.bounds) + } + + fn head_with_bounds(&self, class: &str, [l, t, r, b]: [f64; 4]) -> Descriptor { let bounds = Descriptor::new("classFloatRect") .with("Top ", Value::Double(t)) .with("Left", Value::Double(l)) @@ -119,15 +122,20 @@ impl Warp { /// The `quiltWarp` descriptor of a split warp, in Photoshop's layout: the control points, /// `deformNumCols` × `deformNumRows` of them, and the patch boundaries (`quiltSliceX` / - /// `quiltSliceY`) evenly across the warp's bounds. `None` for any other warp. + /// `quiltSliceY`) evenly across the warp's bounds. Invalid axes use 0..1 because this standalone + /// method has no content size; the placed-layer writer supplies that size when available. + /// `None` for any other warp. pub fn quilt_descriptor(&self) -> Option { if !self.is_split() { return None; } - let [l, t, r, b] = self.bounds; + // The header and the divisions must describe the same rectangle, including + // when a caller leaves bounds at their default or supplies an invalid axis. + let axis = |lo: f64, hi: f64| if (hi - lo).is_finite() && hi > lo { (lo, hi) } else { (0.0, 1.0) }; + let (l, r) = axis(self.bounds[0], self.bounds[2]); + let (t, b) = axis(self.bounds[1], self.bounds[3]); let slices = |key: &str, n: usize, lo: f64, hi: f64| { - let (lo, hi) = if (hi - lo).is_finite() && hi > lo { (lo, hi) } else { (0.0, 1.0) }; - let values: Vec = (0..=n).map(|i| lo + (hi - lo) * i as f64 / n as f64).collect(); + let values: Vec = (0..=n).map(|i| lo + (hi - lo) * (i as f64 / n as f64)).collect(); let prefix = values.len() as u32; Value::ObjectArray(ObjectArray { prefix, body: Descriptor::new("UntF").with(key, Value::UnitFloats { unit: *b"#Pxl", values }) }) }; @@ -137,7 +145,7 @@ impl Warp { .with("meshPoints", self.mesh_points()); let count = |n: usize| Value::Integer(i32::try_from(n.saturating_mul(3).saturating_add(1)).unwrap_or(i32::MAX)); Some( - self.head("quiltWarp") + self.head_with_bounds("quiltWarp", [l, t, r, b]) .with("deformNumRows", count(self.rows)) .with("deformNumCols", count(self.cols)) .with("customEnvelopeWarp", Value::Descriptor(envelope)), @@ -159,7 +167,13 @@ impl SmartObject { /// The warp written, over the content (or `[0, 0, 0, 0]` without a size) when there is none. fn warp_or_none(&self) -> Warp { let [w, h] = self.size.unwrap_or([0.0; 2]); - self.warp.clone().unwrap_or_else(|| Warp::none([0.0, 0.0, w, h])) + let mut warp = self.warp.clone().unwrap_or_else(|| Warp::none([0.0, 0.0, w, h])); + // Match Warp::apply's whole-rectangle fallback before encoding explicit + // quilt slices. Otherwise default bounds would change the mapping on readback. + if warp.is_split() && !(warp.bounds[2] > warp.bounds[0] && warp.bounds[3] > warp.bounds[1]) { + warp.bounds = [0.0, 0.0, w, h]; + } + warp } /// The data of a `SoLd` block (`soLD`, version 4, a version-16 descriptor, padded to 4). @@ -290,6 +304,49 @@ mod tests { ] } + #[test] + fn quilt_writer_uses_one_rectangle_for_header_slices_and_roundtrip() { + for (bounds, expected) in [ + ([0.0; 4], [0.0, 0.0, 1.0, 1.0]), + ([2.0, 3.0, 22.0, 13.0], [2.0, 3.0, 22.0, 13.0]), + ([2.0, 3.0, 22.0, 3.0], [2.0, 0.0, 22.0, 1.0]), + ([f64::NAN, 3.0, 22.0, 13.0], [0.0, 3.0, 1.0, 13.0]), + ([-f64::MAX, 0.0, f64::MAX, 1.0], [0.0, 0.0, 1.0, 1.0]), + ([0.0, 0.0, 1e308, 10.0], [0.0, 0.0, 1e308, 10.0]), + ] { + let warp = Warp { style: "warpCustom".into(), bounds, cols: 2, rows: 1, mesh: grid(2, 1, 20.0, 10.0), ..Warp::default() }; + let descriptor = warp.quilt_descriptor().unwrap(); + let back = Warp::try_from_descriptor(&descriptor).unwrap().unwrap(); + assert_eq!(back.bounds, expected); + assert_eq!((back.cols, back.rows, &back.mesh), (2, 1, &warp.mesh)); + assert_eq!(back.quilt_descriptor().unwrap(), descriptor); + } + let high_offset = Warp { + style: "warpCustom".into(), + bounds: [1e16, 0.0, 1e16 + 8.0, 10.0], + cols: 3, + rows: 1, + mesh: grid(3, 1, 20.0, 10.0), + ..Warp::default() + }; + assert_eq!(Warp::try_from_descriptor(&high_offset.quilt_descriptor().unwrap()).unwrap(), Some(high_offset)); + // At the placed-layer boundary content size is available: preserve the + // evaluator's fallback mapping, not merely the ability to parse the output. + for bounds in [[0.0; 4], [2.0, 3.0, 22.0, 3.0], [f64::NAN, 0.0, 20.0, 10.0]] { + let warp = Warp { style: "warpCustom".into(), bounds, cols: 2, rows: 1, mesh: grid(2, 1, 20.0, 10.0), ..Warp::default() }; + let so = SmartObject { size: Some([20.0, 10.0]), ..placed(Some(warp.clone())) }; + let bytes = so.to_placed_layer_data(&PlacedLayerOptions::default()); + let back = SmartObject::from_placed_layer_data(&bytes).unwrap(); + let parsed = back.warp.as_ref().unwrap(); + assert_eq!(parsed.bounds, [0.0, 0.0, 20.0, 10.0]); + for p in [[0.0, 0.0], [5.0, 4.0], [10.0, 5.0], [20.0, 10.0]] { + assert_eq!(parsed.apply(p, 20.0, 10.0), warp.apply(p, 20.0, 10.0)); + } + let template = VersionedDescriptor::parse_prefix(&bytes[8..]).unwrap().0.descriptor; + let rewritten = back.to_placed_layer_data(&PlacedLayerOptions { template: Some(&template), ..Default::default() }); + assert_eq!(SmartObject::from_placed_layer_data(&rewritten).unwrap(), back); + } + } #[test] fn templates_drop_unsupported_quilts_but_retain_valid_none() { let bounds = [0.0, 0.0, 20.0, 10.0];