Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Integration: merge main, resolve conflicts with #6913's split warps (#5769)

David Mann9a6b3cb3bcad
diff --cc crates/io/src/blocks.rs
index 087f2121d,46335deaf..cc1fb9c6d
--- a/crates/io/src/blocks.rs
+++ b/crates/io/src/blocks.rs
@@@ -525,23 -559,25 +559,27 @@@ pub(crate) fn with_pattern_placement(mu
)
}
- /// The warp of a placed layer (`SoLd`/`SoLE` `warp` descriptor): style, bend, distortions,
- /// axis, bounds and, for `warpCustom`, the 4 × 4 `customEnvelopeWarp` mesh, for `warpCylinder`
- /// its seven `warpValues`. Read-only: the raw block is what gets written back. `None` when absent
- /// or `warpNone` with no mesh. A `PlLd` record keeps its warp elsewhere: see [`parse_plld_warp`].
+ /// The warp of a placed layer (`SoLd`/`SoLE` `quiltWarp` or `warp` descriptor): style, bend,
+ /// distortions, axis, bounds and, for `warpCustom`, the `customEnvelopeWarp` mesh: one 4 × 4
-/// patch, or the multi-patch grid Photoshop's split warp writes as `quiltWarp`. Read-only: the
-/// raw block is what gets written back. `None` when absent or `warpNone` with no mesh. A `PlLd`
-/// record keeps its warp elsewhere: see [`parse_plld_warp`].
++/// patch, or the multi-patch grid Photoshop's split warp writes as `quiltWarp`; for
++/// `warpCylinder` its seven `warpValues`. Read-only: the raw block is what gets written back.
++/// `None` when absent or `warpNone` with no mesh. A `PlLd` record keeps its warp elsewhere: see
++/// [`parse_plld_warp`].
pub fn parse_placed_warp(key: &[u8; 4], data: &[u8]) -> Option<photocraft_geom::warp::Warp> {
if key != b"SoLd" && key != b"SoLE" {
return None;
}
let d = data.get(8..).and_then(parse_prefix_versioned)?;
- warp_of(get_desc(&d, "warp")?).flatten()
+ // A split (quilt) warp is stored under `quiltWarp`; `warp` then holds no usable mesh.
+ warp_of(get_desc(&d, "quiltWarp").or_else(|| get_desc(&d, "warp"))?).flatten()
}
- /// A `warp` descriptor (the `SoLd` key, or the whole descriptor after a `PlLd` record's corners)
- /// as a warp: `Some(None)` for no warp (`warpNone`, or a warp that moves nothing), `None` when it
- /// can't be read (an unknown style, missing bounds, a mesh that isn't 4 × 4, a cylinder that
- /// isn't a horizontal one without perspective, the only kind measured).
- fn warp_of(w: &Descriptor) -> Option<Option<photocraft_geom::warp::Warp>> {
+ /// A `warp` or `quiltWarp` descriptor (the `SoLd` key, or the whole descriptor after a `PlLd`
+ /// record's corners) as a warp: `Some(None)` for no warp (`warpNone`, or a warp that moves
+ /// nothing), `None` when it can't be read (an unknown style, missing bounds, a mesh that is
-/// neither one 4 × 4 patch nor a consistent quilt grid).
++/// neither one 4 × 4 patch nor a consistent quilt grid, a cylinder that isn't a horizontal one
++/// without perspective, the only kind measured).
+ pub(crate) fn warp_of(w: &Descriptor) -> Option<Option<photocraft_geom::warp::Warp>> {
use photocraft_geom::warp::{BezierMesh, Warp, WarpStyle};
let style = enum_of(w, "warpStyle").and_then(|v| WarpStyle::parse(&String::from_utf8_lossy(v)))?;
if style == WarpStyle::None {
@@@ -605,18 -695,12 +708,14 @@@ pub(crate) fn parse_prefix_versioned(da
}
/// The source-space box Photoshop maps onto a placed layer's `Trnf` quad: the bounding box of a
- /// single-patch custom warp's 16 control points or of a cylinder's mesh (#4112), which may spill
- /// past the source, else the source rect `0..w × 0..h`. Mapping the source rect instead counts the
- /// overhang twice and stretches the placement. A degenerate or non-finite box falls back to the
- /// source rect.
-/// custom warp's control points (one patch or a split grid), which may spill past the source,
-/// else the source rect `0..w × 0..h`. Mapping the source rect instead counts the overhang twice
-/// and stretches the placement. A degenerate or non-finite box falls back to the source rect.
++/// custom warp's control points (one patch or a split grid) or of a cylinder's mesh (#4112),
++/// which may spill past the source, else the source rect `0..w × 0..h`. Mapping the source rect
++/// instead counts the overhang twice and stretches the placement. A degenerate or non-finite box
++/// falls back to the source rect.
pub(crate) fn placed_frame(warp: Option<&photocraft_geom::warp::Warp>, w: f64, h: f64) -> [f64; 4] {
+ use photocraft_geom::warp::WarpStyle;
let rect = [0.0, 0.0, w, h];
- let Some(m) = warp
- .and_then(|wp| wp.mesh.as_ref().map(|m| (wp.style, m)))
- .filter(|(style, m)| (*style == WarpStyle::Custom && m.points.len() == 16) || *style == WarpStyle::Cylinder)
- .map(|(_, m)| m)
- else {
- let Some(m) = warp.filter(|wp| wp.style == photocraft_geom::warp::WarpStyle::Custom).and_then(|wp| wp.mesh.as_ref()).filter(|m| m.is_valid()) else {
++ let Some(m) = warp.filter(|wp| matches!(wp.style, WarpStyle::Custom | WarpStyle::Cylinder)).and_then(|wp| wp.mesh.as_ref()).filter(|m| m.is_valid()) else {
return rect;
};
let inf = f64::INFINITY;
@@@ -672,17 -746,17 +771,17 @@@ pub fn corners_affine(p: &[f64; 8], fra
/// The transform and (Distort/Perspective) projective placement of a `PlLd` record's corners `p`
/// for a source of `size` warped by `warp`: the inverse of [`crate::smart_map::plld_bytes`]'s
--/// quad. As for `SoLd`'s `Trnf` (#3677), affine corners frame a single-patch custom warp's
++/// quad. As for `SoLd`'s `Trnf` (#3677), affine corners frame a custom warp's or a cylinder's
/// control-point box (which may spill past the source), else the source rect; projective corners
-/// frame the source rect, as `parse_smart_perspective` reads them. `PlLd` stores no source size:
-/// without the source file (`None`) the layer is taken as placed at 100 %, the edge lengths being
-/// its size (a custom warp's box needs no size); position and rotation stay right, only Replace
-/// Contents and re-renders depend on the scale. Import and export (which keeps an unedited record
-/// verbatim) both place a `PlLd` layer with this.
+/// frame [`perspective_frame`], as `parse_smart_perspective` reads them. `PlLd` stores no source
+/// size: without the source file (`None`) the layer is taken as placed at 100 %, the edge lengths
+/// being its size (a custom warp's box needs no size); position and rotation stay right, only
+/// Replace Contents and re-renders depend on the scale. Import and export (which keeps an unedited
+/// record verbatim) both place a `PlLd` layer with this.
pub fn plld_transform(p: &[f64; 8], size: Option<(f64, f64)>, warp: Option<&photocraft_geom::warp::Warp>) -> (Affine, Option<[f64; 9]>) {
let (w, h) = size.unwrap_or_else(|| ((p[2] - p[0]).hypot(p[3] - p[1]), (p[6] - p[0]).hypot(p[7] - p[1])));
- let perspective = corners_perspective([[p[0], p[1]], [p[2], p[3]], [p[4], p[5]], [p[6], p[7]]], (w, h));
- let frame = if perspective.is_some() { [0.0, 0.0, w, h] } else { placed_frame(warp, w, h) };
+ let perspective = corners_perspective_in([[p[0], p[1]], [p[2], p[3]], [p[4], p[5]], [p[6], p[7]]], perspective_frame(warp, w, h));
+ let frame = if perspective.is_some() { perspective_frame(warp, w, h) } else { placed_frame(warp, w, h) };
(corners_affine(p, frame).unwrap_or(Affine::IDENTITY), perspective)
}
@@@ -757,8 -831,7 +856,9 @@@ pub fn parse_smart_perspective(key: &[u
(p.len() == 8 && p.iter().all(|v| v.is_finite())).then(|| [[p[0], p[1]], [p[2], p[3]], [p[4], p[5]], [p[6], p[7]]])
};
let q = quad("nonAffineTransform").or_else(|| quad("Trnf"))?;
- let warp = get_desc(&d, "warp").and_then(warp_of).flatten();
- corners_perspective(q, (w, h))
++ // The warp `parse_placed_warp` reads: a split warp's `quiltWarp` comes first.
++ let warp = get_desc(&d, "quiltWarp").or_else(|| get_desc(&d, "warp")).and_then(warp_of).flatten();
+ corners_perspective_in(q, perspective_frame(warp.as_ref(), w, h))
}
/// The projective placement of a `w` × `h` source onto corners `q` that aren't a parallelogram;
@@@ -1274,121 -1486,6 +1520,143 @@@ mod more_tests
assert_eq!(placed_frame(None, 50.0, 25.0), [0.0, 0.0, 50.0, 25.0]);
}
+ /// A `SoLd` placed on `quad` (`Trnf` and `nonAffineTransform`) for a 1000 × 1000 source,
+ /// warped by `warp`.
+ fn sold_placed(quad: [f64; 8], warp: Descriptor) -> Vec<u8> {
+ let list = Value::List(quad.iter().map(|v| Value::Double(*v)).collect());
+ let d = Descriptor::new("null")
+ .with("Trnf", list.clone())
+ .with("nonAffineTransform", list)
+ .with("Sz ", Value::Descriptor(Descriptor::new("Pnt ").with("Wdth", Value::Double(1000.0)).with("Hght", Value::Double(1000.0))))
+ .with("warp", Value::Descriptor(warp));
+ let mut data = b"soLD".to_vec();
+ data.extend_from_slice(&4u32.to_be_bytes());
+ data.extend(VersionedDescriptor::new(d).to_bytes());
+ data
+ }
+
+ /// A `warpCylinder` descriptor as #4112 describes it: horizontal, no perspective, seven
+ /// `warpValues` in the space of `bounds`.
+ fn cylinder_desc(values: Value) -> Descriptor {
+ let e = |t: &str, v: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(v) };
+ let bounds = Descriptor::new("classFloatRect")
+ .with("Top ", Value::Double(0.0))
+ .with("Left", Value::Double(0.0))
+ .with("Btom", Value::Double(1000.0))
+ .with("Rght", Value::Double(1000.0));
+ Descriptor::new("warp")
+ .with("warpStyle", e("warpStyle", "warpCylinder"))
+ .with("warpValue", Value::Double(0.0))
+ .with("warpValues", values)
+ .with("warpPerspective", Value::Double(0.0))
+ .with("warpPerspectiveOther", Value::Double(0.0))
+ .with("warpRotate", e("Ornt", "Hrzn"))
+ .with("bounds", Value::Descriptor(bounds))
+ }
+
+ const CYL: [f64; 7] = [100.0, 800.0, 900.0, 200.0, 0.3, 0.3, 0.5];
+
+ fn doubles(v: &[f64]) -> Value {
+ Value::List(v.iter().map(|x| Value::Double(*x)).collect())
+ }
+
++ /// A projective placement is framed by the warp `parse_placed_warp` imports: an unreadable
++ /// `quiltWarp` next to a cylinder under `warp` imports no warp, so the source rect is framed.
++ #[test]
++ fn perspective_frame_follows_the_warp_parse_placed_warp_reads() {
++ let quad = [10.0, 20.0, 410.0, 60.0, 400.0, 480.0, 30.0, 520.0];
++ let list = Value::List(quad.iter().map(|v| Value::Double(*v)).collect());
++ let unreadable =
++ Descriptor::new("quiltWarp").with("warpStyle", Value::Enumerated { type_id: Id::new("warpStyle"), value: Id::new("warpSomethingNew") });
++ let d = Descriptor::new("null")
++ .with("Trnf", list.clone())
++ .with("nonAffineTransform", list)
++ .with("Sz ", Value::Descriptor(Descriptor::new("Pnt ").with("Wdth", Value::Double(1000.0)).with("Hght", Value::Double(1000.0))))
++ .with("quiltWarp", Value::Descriptor(unreadable))
++ .with("warp", Value::Descriptor(cylinder_desc(doubles(&CYL))));
++ let mut data = b"soLD".to_vec();
++ data.extend_from_slice(&4u32.to_be_bytes());
++ data.extend(VersionedDescriptor::new(d).to_bytes());
++ assert_eq!(parse_placed_warp(b"SoLd", &data), None);
++ let h = photocraft_algo::transform::Homography(parse_smart_perspective(b"SoLd", &data).expect("projective"));
++ let (px, py) = h.apply(1000.0, 1000.0);
++ assert!((px - 400.0).abs() < 1e-6 && (py - 480.0).abs() < 1e-6, "{px},{py}");
++ }
+ /// #4112: a `warpCylinder` placed layer comes in as the cylinder its seven values describe,
+ /// instead of no warp (which re-rendered the contents flat).
+ #[test]
+ fn placed_cylinder_warp_is_read() {
+ use photocraft_geom::warp::{Warp, WarpStyle};
+ let quad = [0.0, 0.0, 800.0, 0.0, 800.0, 800.0, 0.0, 800.0];
+ let want = Warp::cylinder(&CYL, [0.0, 0.0, 1000.0, 1000.0]).unwrap();
+ let w = parse_placed_warp(b"SoLd", &sold_placed(quad, cylinder_desc(doubles(&CYL)))).expect("a cylinder");
+ assert_eq!(w, want);
+ assert_eq!(w.style, WarpStyle::Cylinder);
+ // Values written as one unit-float list read the same.
+ let floats = Value::UnitFloats { unit: *b"#Pxl", values: CYL.to_vec() };
+ assert_eq!(parse_placed_warp(b"SoLE", &sold_placed(quad, cylinder_desc(floats))), Some(want));
+ }
+
+ /// Cylinders nobody has measured (vertical, with perspective) and broken values stay
+ /// unreadable, as every cylinder was before: the layer keeps the pixels Photoshop stored.
+ #[test]
+ fn placed_cylinder_warps_that_cant_be_read() {
+ let quad = [0.0, 0.0, 800.0, 0.0, 800.0, 800.0, 0.0, 800.0];
+ let read = |d: Descriptor| parse_placed_warp(b"SoLd", &sold_placed(quad, d));
+ let e = |t: &str, v: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(v) };
+ let with = |key: &str, v: Value| {
+ let mut d = cylinder_desc(doubles(&CYL));
+ d.items.iter_mut().filter(|(k, _)| k.is(key)).for_each(|(_, old)| *old = v.clone());
+ d
+ };
+ assert!(read(with("warpRotate", e("Ornt", "Vrtc"))).is_none(), "vertical");
+ assert!(read(with("warpPerspective", Value::Double(20.0))).is_none(), "perspective");
+ assert!(read(with("warpPerspectiveOther", Value::Double(-5.0))).is_none(), "the other perspective");
+ assert!(read(cylinder_desc(doubles(&CYL[..6]))).is_none(), "six values");
+ assert!(read(cylinder_desc(Value::List(vec![Value::Text(UnicodeString::new_nul("x")); 7]))).is_none(), "not numbers");
+ assert!(read(cylinder_desc(Value::Double(1.0))).is_none(), "one number");
+ let mut flipped = CYL;
+ flipped.swap(0, 2);
+ assert!(read(cylinder_desc(doubles(&flipped))).is_none(), "silhouettes swapped");
+ let mut no_values = cylinder_desc(doubles(&CYL));
+ no_values.items.retain(|(k, _)| !k.is("warpValues"));
+ assert!(read(no_values).is_none(), "no warpValues");
+ }
+
+ /// Like a custom warp's (#3677), a cylinder's control-point box is what the placement quad
+ /// frames, for affine corners and for projective ones (`nonAffineTransform`).
+ #[test]
+ fn placed_cylinder_quad_frames_its_control_box() {
+ use photocraft_geom::warp::Warp;
+ let cb = Warp::cylinder(&CYL, [0.0, 0.0, 1000.0, 1000.0]).unwrap().mesh.unwrap().control_bounds();
+ // Affine: the box lands on the quad.
+ let quad = [10.0, 20.0, 410.0, 20.0, 410.0, 520.0, 10.0, 520.0];
+ let (_, a) = parse_smart(b"SoLd", &sold_placed(quad, cylinder_desc(doubles(&CYL))));
+ let [m0, m1, m2, m3, m4, m5] = a.m;
+ let map = |x: f64, y: f64| (m0 * x + m2 * y + m4, m1 * x + m3 * y + m5);
+ for ((x, y), (qx, qy)) in
+ [(cb[0], cb[1]), (cb[2], cb[1]), (cb[2], cb[3]), (cb[0], cb[3])].into_iter().zip(quad.as_chunks::<2>().0.iter().map(|p| (p[0], p[1])))
+ {
+ let (px, py) = map(x, y);
+ assert!((px - qx).abs() < 1e-9 && (py - qy).abs() < 1e-9, "{x},{y} → {px},{py}, not {qx},{qy}");
+ }
+ assert!(parse_smart_perspective(b"SoLd", &sold_placed(quad, cylinder_desc(doubles(&CYL)))).is_none(), "affine");
+ // Projective: the same box lands on the four corners.
+ let quad = [10.0, 20.0, 410.0, 60.0, 400.0, 480.0, 30.0, 520.0];
+ let h = photocraft_algo::transform::Homography(parse_smart_perspective(b"SoLd", &sold_placed(quad, cylinder_desc(doubles(&CYL)))).expect("projective"));
+ for ((x, y), (qx, qy)) in
+ [(cb[0], cb[1]), (cb[2], cb[1]), (cb[2], cb[3]), (cb[0], cb[3])].into_iter().zip(quad.as_chunks::<2>().0.iter().map(|p| (p[0], p[1])))
+ {
+ let (px, py) = h.apply(x, y);
+ assert!((px - qx).abs() < 1e-6 && (py - qy).abs() < 1e-6, "{x},{y} → {px},{py}, not {qx},{qy}");
+ }
+ // Without a warp a projective quad still frames the source rect.
+ let none = Descriptor::new("warp").with("warpStyle", Value::Enumerated { type_id: Id::new("warpStyle"), value: Id::new("warpNone") });
+ let h = photocraft_algo::transform::Homography(parse_smart_perspective(b"SoLd", &sold_placed(quad, none)).expect("projective"));
+ let (px, py) = h.apply(1000.0, 1000.0);
+ assert!((px - 400.0).abs() < 1e-6 && (py - 480.0).abs() < 1e-6);
+ }
+
#[test]
fn effects_switch() {
let mut lfx = vec![0, 0, 0, 0];
diff --cc crates/io/src/smart_map.rs
index 92cd32266,57f8c5d7d..9c8849280
--- a/crates/io/src/smart_map.rs
+++ b/crates/io/src/smart_map.rs
@@@ -691,17 -701,17 +701,17 @@@ pub struct PlacedSpec<'a>
}
/// Where the frame corners land (top-left, top-right, bottom-right, bottom-left), as x, y pairs.
- /// The frame is [`crate::blocks::placed_frame`] for an affine placement (the inverse of
- /// `parse_smart`), [`crate::blocks::perspective_frame`] for a projective one (as
- /// `parse_smart_perspective` reads it).
- fn quad_points(s: &PlacedSpec<'_>) -> [f64; 8] {
+ /// The frame is [`crate::blocks::placed_frame`] of `warp`, the warp the block stores, for an
-/// affine placement (the inverse of `parse_smart`), the source rect for a projective one (as
-/// `parse_smart_perspective` reads it).
++/// affine placement (the inverse of `parse_smart`), [`crate::blocks::perspective_frame`] of it
++/// for a projective one (as `parse_smart_perspective` reads it).
+ fn quad_points(s: &PlacedSpec<'_>, warp: Option<&Warp>) -> [f64; 8] {
let (w, h) = s.size;
let [a, b, c, d, e, f] = s.transform.m;
let map = |x: f64, y: f64| match &s.perspective {
Some(p) => photocraft_algo::transform::Homography(*p).apply(x, y),
None => (a * x + c * y + e, b * x + d * y + f),
};
- let [x0, y0, x1, y1] = if s.perspective.is_some() { crate::blocks::perspective_frame(s.warp, w, h) } else { crate::blocks::placed_frame(s.warp, w, h) };
- let [x0, y0, x1, y1] = if s.perspective.is_some() { [0.0, 0.0, w, h] } else { crate::blocks::placed_frame(warp, w, h) };
++ let [x0, y0, x1, y1] = if s.perspective.is_some() { crate::blocks::perspective_frame(warp, w, h) } else { crate::blocks::placed_frame(warp, w, h) };
let mut out = [0.0; 8];
for (i, (x, y)) in [(x0, y0), (x1, y0), (x1, y1), (x0, y1)].into_iter().enumerate() {
(out[2 * i], out[2 * i + 1]) = map(x, y);
@@@ -726,8 -736,54 +736,55 @@@ fn enumv(t: &str, v: &str) -> Value
Value::Enumerated { type_id: Id::new(t), value: Id::new(v) }
}
- /// The `warp` descriptor (also the `PlLd` warp). Multi-patch meshes have no 1 × 1 envelope and
- /// are written as no warp, with a message. A cylinder is written as its seven `warpValues`.
+ /// The mesh of a split warp: a custom warp of more than one patch, which `SoLd` stores as
+ /// `quiltWarp` and `PlLd` can't store.
+ fn split_mesh(w: Option<&Warp>) -> Option<&BezierMesh> {
+ w.filter(|w| w.style == WarpStyle::Custom).and_then(|w| w.mesh.as_ref()).filter(|m| m.points.len() != 16 && m.is_valid())
+ }
+
+ /// The warp `SoLd` stores: none, with a message, for a split warp of more patches along a side
+ /// than a `quiltWarp` is read with, so its corners are framed by the source rect as on reopen.
+ fn sold_warp<'a>(w: Option<&'a Warp>, warnings: &mut Vec<String>) -> Option<&'a Warp> {
+ let too_big = split_mesh(w).is_some_and(|m| m.us.len().max(m.vs.len()) > crate::blocks::QUILT_MAX_PATCHES + 1);
+ if too_big {
+ warnings.push(format!("a split warp of more than {} patches along a side was written to PSD without its warp", crate::blocks::QUILT_MAX_PATCHES));
+ return None;
+ }
+ w
+ }
+
+ /// Style, bend, distortions, axis, bounds and order: the keys a `warp` and a `quiltWarp`
+ /// descriptor share, in Photoshop's order.
+ fn warp_head(class: &str, w: &Warp) -> Descriptor {
+ let [x0, y0, x1, y1] = w.bounds;
+ let bounds = Descriptor::new("classFloatRect")
+ .with("Top ", Value::Double(y0))
+ .with("Left", Value::Double(x0))
+ .with("Btom", Value::Double(y1))
+ .with("Rght", Value::Double(x1));
- Descriptor::new(class)
- .with("warpStyle", enumv("warpStyle", w.style.psd_name()))
- .with("warpValue", Value::Double(w.bend))
- .with("warpPerspective", Value::Double(w.h_distort))
++ let d = Descriptor::new(class).with("warpStyle", enumv("warpStyle", w.style.psd_name())).with("warpValue", Value::Double(w.bend));
++ // A cylinder is stored as its seven `warpValues` (#4112).
++ let d = if w.style == WarpStyle::Cylinder { d.with("warpValues", Value::List(w.values.iter().copied().map(Value::Double).collect())) } else { d };
++ d.with("warpPerspective", Value::Double(w.h_distort))
+ .with("warpPerspectiveOther", Value::Double(w.v_distort))
+ .with("warpRotate", enumv("Ornt", if w.vertical { "Vrtc" } else { "Hrzn" }))
+ .with("bounds", Value::Descriptor(bounds))
+ .with("uOrder", Value::Integer(4))
+ .with("vOrder", Value::Integer(4))
+ }
+
+ /// The control points of a custom warp: an object array of their x and y.
+ fn mesh_points(m: &BezierMesh) -> Value {
+ let body = Descriptor::new("rationalPoint")
+ .with("Hrzn", Value::UnitFloats { unit: *b"#Pxl", values: m.points.iter().map(|p| p[0]).collect() })
+ .with("Vrtc", Value::UnitFloats { unit: *b"#Pxl", values: m.points.iter().map(|p| p[1]).collect() });
+ Value::ObjectArray(ObjectArray { prefix: m.points.len() as u32, body })
+ }
+
+ /// The `warp` descriptor (also the `PlLd` warp). A split warp is written as no warp: `SoLd`
+ /// keeps it in `quiltWarp` ([`quilt_desc`]), next to this `warpNone`, as Photoshop does. A
-/// custom warp without a usable mesh is written as no warp, with a message.
++/// custom warp without a usable mesh is written as no warp, with a message. A cylinder is
++/// written as its seven `warpValues`.
fn warp_desc(w: Option<&Warp>, size: (f64, f64), warnings: &mut Vec<String>) -> Value {
let none = Warp::none([0.0, 0.0, size.0, size.1]);
let mut w = w.unwrap_or(&none).clone();
@@@ -1463,56 -1761,9 +1762,56 @@@ mod tests
assert_eq!(q, [100.0, 200.0, 240.0, 200.0, 240.0, 260.0, 100.0, 260.0]);
assert_eq!(p.transform, transform);
// The imported quad reads as unmoved, so the template's `Trnf` is kept as stored.
- assert!(!quad_moved(&p.descriptor, &spec));
+ assert!(!quad_moved(&p.descriptor, &spec, spec.warp));
}
+ /// #4112: a regenerated `SoLd` (after Replace Contents, a move…) writes a cylinder back as
+ /// `warpCylinder` and its seven values, not as no warp, and frames its control-point box with
+ /// the quad, affine or projective, so import places it where it was.
+ #[test]
+ fn cylinder_warp_round_trips_through_sold() {
+ let v = [100.0, 800.0, 900.0, 200.0, 0.3, 0.3, 0.5];
+ let warp = Warp::cylinder(&v, [0.0, 0.0, 1000.0, 1000.0]).unwrap();
+ let transform = Affine { m: [0.5, 0.0, 0.0, 0.5, 40.0, 30.0] };
+ let mut spec =
+ PlacedSpec { idnt: "i", placed: "p", transform, perspective: None, size: (1000.0, 1000.0), dpi: 72.0, warp: Some(&warp), filter_fx: None };
+ let mut warnings = Vec::new();
+ let sold = sold_bytes(None, &spec, &mut warnings);
+ assert!(warnings.is_empty(), "{warnings:?}");
+ let p = parse_sold(&sold).expect("parses");
+ let Some(Value::Descriptor(w)) = p.descriptor.get("warp") else { panic!("no warp") };
+ assert_eq!(crate::blocks::enum_of(w, "warpStyle"), Some(b"warpCylinder".as_slice()));
+ let Some(Value::List(values)) = w.get("warpValues") else { panic!("no warpValues") };
+ assert_eq!(values.iter().filter_map(|x| num(Some(x))).collect::<Vec<_>>(), v);
+ assert!(w.get("customEnvelopeWarp").is_none());
+ assert_eq!(crate::blocks::parse_placed_warp(b"SoLd", &sold), Some(warp.clone()));
+ for (a, b) in p.transform.m.iter().zip(transform.m) {
+ assert!((a - b).abs() < 1e-9, "{:?}", p.transform);
+ }
+ // The quad is the image of the control-point box, not of the source rect.
+ let cb = warp.mesh.as_ref().unwrap().control_bounds();
+ let Some(Value::List(q)) = p.descriptor.get("Trnf") else { panic!("no Trnf") };
+ let q: Vec<f64> = q.iter().filter_map(|x| num(Some(x))).collect();
+ let want = [cb[0], cb[1], cb[2], cb[1], cb[2], cb[3], cb[0], cb[3]].map(|c| 0.5 * c);
+ for (i, (a, b)) in q.iter().zip(want).enumerate() {
+ let shift = if i % 2 == 0 { 40.0 } else { 30.0 };
+ assert!((a - (b + shift)).abs() < 1e-9, "corner {i}: {q:?}");
+ }
- assert!(!quad_moved(&p.descriptor, &spec));
++ assert!(!quad_moved(&p.descriptor, &spec, spec.warp));
+ // A projective placement comes back as the same map.
+ let h = photocraft_algo::transform::Homography::rect_to_quad(cb, [[10.0, 20.0], [410.0, 60.0], [400.0, 480.0], [30.0, 520.0]]).unwrap();
+ spec.perspective = Some(h.0);
+ let sold = sold_bytes(None, &spec, &mut warnings);
+ let back = crate::blocks::parse_smart_perspective(b"SoLd", &sold).expect("projective");
+ let n = |m: [f64; 9]| m.map(|x| x / m[8]);
+ for (a, b) in n(back).iter().zip(n(h.0)) {
+ assert!((a - b).abs() < 1e-9 * (1.0 + b.abs()), "{back:?} vs {:?}", h.0);
+ }
+ let plld = plld_bytes(&spec, &mut warnings);
+ assert_eq!(crate::blocks::parse_plld_warp(&plld), crate::blocks::PlldWarp::Warp(warp));
+ assert!(warnings.is_empty(), "{warnings:?}");
+ }
+
#[test]
fn hex_and_uuid() {
assert_eq!(from_hex(&to_hex(&[0, 1, 0xab, 0xff])), Some(vec![0, 1, 0xab, 0xff]));
diff --cc docs/gaps.md
index 2ef5cac62,ab42d0f8e..226b67b2c
--- a/docs/gaps.md
+++ b/docs/gaps.md
@@@ -272,8 -274,8 +276,9 @@@ what an agent can't finish alone
| Date | Change | Summary |
|---|---|---|
+ | 2026-10-15 | minor | G1 / #5972 A1: render only provably constant-RGB `GdFl` noise fills; variable noise synthesis remains open |
| 2026-10-18 | minor | G9: with no selection, Copy takes fill and adjustment layers whole and Paste recreates them (#2344); UI-217-23 stays partial (a single pixel layer copies its pixels only); estimate unchanged |
+| 2026-10-14 | minor | G1: PSD smart objects keep Transform Warp's Cylinder through Replace Contents, Relink, Edit Contents and re-saves (#4112) |
| 2026-10-14 | minor | G1: a PSD smart object's filter mask shows when the file opens (Photoshop stores the filtered pixels unmasked); export stores them that way too; Photoshop-oracle floor 185 → 188 of 258 |
| 2026-10-13 | minor | G14: D90 image area measured (4288 × 2848 at (10, 10)); gap remains open |
| 2026-10-14 | minor | Corpus floors raised to the measured results of the CMYK, Lab, 32-bit, Levels, Threshold, Posterize, feather and pass-through fixes landed together: Photoshop-authored 165 → 185 of 258, io 146 → 148 of 170, psd-tools 242 → 249 of 309 |