psd: reject invalid quilt bounds and discard unsupported templates

This commit is contained in:
FredApps committed 2026-11-18 08:08:01 +01:00
1 parent f8d869c1e7
commit b3960accde
2 files changed
+44 -2

No files matched your search

+15 -1
View File
@@ -134,8 +134,11 @@ impl Warp {
let rows = quilt_patches(&slice_y)?;
if !slice_x.is_empty() || !slice_y.is_empty() {
// The evaluator normalizes over bounds, not stored slice coordinates.
// Infer a missing rectangle only when both explicit axes define it.
// Infer an absent rectangle only when both explicit axes define it.
if !(w.bounds[2] > w.bounds[0] && w.bounds[3] > w.bounds[1]) {
if d.get("bounds").is_some() {
return Err(PsdError::Unsupported("invalid explicit quilt bounds".into()));
}
let (Some(&left), Some(&right), Some(&top), Some(&bottom)) = (slice_x.first(), slice_x.last(), slice_y.first(), slice_y.last())
else {
return Err(PsdError::Unsupported("quilt divisions need bounds or both axes".into()));
@@ -1074,6 +1077,17 @@ mod tests {
.with("Rght", Value::Double(40.0))
.with("Btom", Value::Double(10.0));
assert!(Warp::try_from_descriptor(&equal.with("bounds", Value::Descriptor(bounds))).is_err());
let wrong_type = quilt_descriptor(vec![0.0, 10.0, 20.0], vec![0.0, 10.0]).with("bounds", Value::Integer(1));
assert!(Warp::try_from_descriptor(&wrong_type).is_err());
let invalid_bounds = Descriptor::new("bounds")
.with("Left", Value::Double(0.0))
.with("Top ", Value::Double(0.0))
.with("Rght", Value::Double(40.0))
.with("Btom", Value::Double(0.0));
let explicit_invalid = quilt_descriptor(vec![0.0, 10.0, 20.0], vec![0.0, 10.0]).with("bounds", Value::Descriptor(invalid_bounds));
assert!(Warp::try_from_descriptor(&explicit_invalid).is_err());
let data = placed_layer_data("invalid bounds", [0.0; 8], |d| d.with("quiltWarp", Value::Descriptor(explicit_invalid)));
assert!(matches!(SmartObject::from_placed_layer_data(&data), Err(PsdError::Unsupported(_))));
let missing = quilt_descriptor(vec![0.0, 10.0, 20.0], vec![]);
assert!(Warp::try_from_descriptor(&missing).is_err());
}
+29 -1
View File
@@ -180,7 +180,8 @@ impl SmartObject {
}
set(&mut d, "warp", Value::Descriptor(warp.to_descriptor()));
// The imported `quiltWarp` stays while it still says what the warp is.
let kept = t.get_object("quiltWarp").is_some_and(|q| Warp::from_descriptor(q).as_ref() == self.warp.as_ref());
let kept =
t.get_object("quiltWarp").is_some_and(|q| Warp::try_from_descriptor(q).is_ok_and(|parsed| parsed.as_ref() == self.warp.as_ref()));
if !kept {
match quilt {
Some(q) => set_before(&mut d, "quiltWarp", Value::Descriptor(q), "warp"),
@@ -289,6 +290,33 @@ mod tests {
]
}
#[test]
fn templates_drop_unsupported_quilts_but_retain_valid_none() {
let bounds = [0.0, 0.0, 20.0, 10.0];
let unfl = |values| Value::UnitFloats { unit: *b"#Pxl", values };
let unequal =
Descriptor::new("warp").with("warpStyle", Value::Enumerated { type_id: Id::new("warpStyle"), value: Id::new("warpCustom") }).with(
"customEnvelopeWarp",
Value::Descriptor(
Descriptor::new("customEnvelopeWarp").with("quiltSliceX", unfl(vec![0.0, 5.0, 20.0])).with("quiltSliceY", unfl(vec![0.0, 10.0])),
),
);
assert!(Warp::try_from_descriptor(&unequal).is_err());
let valid_none = Warp::none(bounds).to_descriptor();
assert!(matches!(Warp::try_from_descriptor(&valid_none), Ok(None)));
for (quilt, keep) in [(unequal, false), (valid_none, true)] {
let template = Descriptor::new("null").with("quiltWarp", Value::Descriptor(quilt.clone()));
let bytes = placed(None).to_placed_layer_data(&PlacedLayerOptions {
placed: "layer",
resolution: 72.0,
filter_fx: None,
template: Some(&template),
});
let written = VersionedDescriptor::parse_prefix(&bytes[8..]).unwrap().0.descriptor;
assert_eq!(written.get_object("quiltWarp"), keep.then_some(&quilt));
assert!(SmartObject::from_placed_layer_data(&bytes).unwrap().warp.is_none());
}
}
#[test]
fn placed_layer_data_reads_back_and_passes_the_strict_check() {
let filter = Descriptor::new("filterFX")