M13.2: PSD smart objects (SoLd/PlLd + lnk2 embedded files) import as placed footage of the embedded document or image
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
da0ec526f3
commit
7352b624d4
+162
-1
@@ -204,6 +204,46 @@ pub struct SubPath {
|
||||
pub knots: Vec<[[f64; 2]; 3]>,
|
||||
}
|
||||
|
||||
/// A placed layer (smart object): `SoLd` / `SoLE` (placed layer data) or the older `PlLd`.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct SmartObject {
|
||||
/// Unique id of the linked file ([`Psd::linked`]) the layer shows.
|
||||
pub uuid: String,
|
||||
/// Where the content's corners sit in document pixels: top-left, top-right, bottom-right,
|
||||
/// bottom-left.
|
||||
pub quad: [[f64; 2]; 4],
|
||||
/// The content's size in pixels (`Sz `), when recorded.
|
||||
pub size: Option<[f64; 2]>,
|
||||
}
|
||||
|
||||
impl SmartObject {
|
||||
/// The quad as an affine placement of a `w`×`h` content rectangle: (centre, scale x, scale
|
||||
/// y, rotation in degrees). Perspective and skew are dropped.
|
||||
pub fn placement(&self, w: f64, h: f64) -> ([f64; 2], f64, f64, f64) {
|
||||
let q = self.quad;
|
||||
let centre = [(q[0][0] + q[1][0] + q[2][0] + q[3][0]) / 4.0, (q[0][1] + q[1][1] + q[2][1] + q[3][1]) / 4.0];
|
||||
let top = [q[1][0] - q[0][0], q[1][1] - q[0][1]];
|
||||
let left = [q[3][0] - q[0][0], q[3][1] - q[0][1]];
|
||||
let sx = top[0].hypot(top[1]) / w.max(1e-9);
|
||||
let sy = left[0].hypot(left[1]) / h.max(1e-9);
|
||||
// Mirrored content (left edge turning the other way) keeps a negative y scale.
|
||||
let cross = top[0] * left[1] - top[1] * left[0];
|
||||
(centre, sx, if cross < 0.0 { -sy } else { sy }, top[1].atan2(top[0]).to_degrees())
|
||||
}
|
||||
}
|
||||
|
||||
/// A file embedded in (or linked from) the document: linked layer data (`lnk2`, `lnkD`,
|
||||
/// `lnk3`, `lnkE`).
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct LinkedFile {
|
||||
pub uuid: String,
|
||||
pub name: String,
|
||||
/// File type code (`8BPS`, `PNGf`, `JPEG`…), may be blank.
|
||||
pub file_type: String,
|
||||
/// Embedded data (absent for externally linked files).
|
||||
pub data: Option<std::ops::Range<usize>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct Layer {
|
||||
/// Index in [`Psd::layers`] (file order: bottom of the stack first).
|
||||
@@ -230,6 +270,8 @@ pub struct Layer {
|
||||
/// Layer effects (`lfx2` descriptor).
|
||||
pub effects: Option<Descriptor>,
|
||||
pub vector_mask: Option<VectorMask>,
|
||||
/// Smart object placement.
|
||||
pub smart_object: Option<SmartObject>,
|
||||
/// Keys of every additional-information block the layer carries.
|
||||
pub keys: Vec<[u8; 4]>,
|
||||
}
|
||||
@@ -277,6 +319,8 @@ pub struct Psd {
|
||||
pub merged_alpha: bool,
|
||||
/// Indexed colour palette (256 R, then G, then B).
|
||||
pub palette: Vec<u8>,
|
||||
/// Files embedded for smart objects.
|
||||
pub linked: Vec<LinkedFile>,
|
||||
data: Arc<[u8]>,
|
||||
merged_offset: usize,
|
||||
}
|
||||
@@ -314,7 +358,20 @@ impl Psd {
|
||||
let n = r.u32()? as usize;
|
||||
r.skip(n)?;
|
||||
// Layer and mask information.
|
||||
let mut psd = Psd { psb, width, height, channels, depth, mode, layers: vec![], merged_alpha: false, palette, data: data.clone(), merged_offset: 0 };
|
||||
let mut psd = Psd {
|
||||
psb,
|
||||
width,
|
||||
height,
|
||||
channels,
|
||||
depth,
|
||||
mode,
|
||||
layers: vec![],
|
||||
merged_alpha: false,
|
||||
palette,
|
||||
linked: vec![],
|
||||
data: data.clone(),
|
||||
merged_offset: 0,
|
||||
};
|
||||
let lm_len = r.length(psb)?;
|
||||
let lm_end = r.pos.checked_add(lm_len).filter(|&e| e <= data.len()).ok_or(Error::Truncated)?;
|
||||
if lm_len > 0 {
|
||||
@@ -330,6 +387,9 @@ impl Psd {
|
||||
}
|
||||
// Global additional layer information (16/32-bit layer info lives here).
|
||||
while let Some((key, start, end)) = next_block(&data, &mut r.pos, lm_end, psb) {
|
||||
if matches!(&key, b"lnk2" | b"lnkD" | b"lnk3" | b"lnkE") {
|
||||
psd.linked_files(&data, start, end);
|
||||
}
|
||||
if matches!(&key, b"Lr16" | b"Lr32" | b"Layr") && psd.layers.is_empty() && end > start {
|
||||
// The block holds the layer info, normally without its length field; accept
|
||||
// both layouts.
|
||||
@@ -348,6 +408,67 @@ impl Psd {
|
||||
Ok(psd)
|
||||
}
|
||||
|
||||
/// The embedded bytes of linked file `uuid`.
|
||||
pub fn linked_data(&self, uuid: &str) -> Option<&[u8]> {
|
||||
let f = self.linked.iter().find(|f| f.uuid == uuid)?;
|
||||
self.data.get(f.data.clone()?)
|
||||
}
|
||||
|
||||
/// Linked layer data records (spec: "Linked Layer"): length, kind (`liFD` embedded data,
|
||||
/// `liFE` external file, `liFA` alias), version, unique id, original file name, type,
|
||||
/// creator, data length, optional descriptors, then the data. A damaged record ends the list.
|
||||
fn linked_files(&mut self, data: &[u8], start: usize, end: usize) {
|
||||
let mut pos = start;
|
||||
while pos + 8 <= end {
|
||||
let mut r = Reader::at(&data[..end], pos);
|
||||
let Ok(len) = r.u64() else { break };
|
||||
let body = r.pos;
|
||||
let Some(rec_end) = body.checked_add(len as usize).filter(|e| *e <= end) else { break };
|
||||
let mut r = Reader::at(&data[..rec_end], body);
|
||||
let rec = (|| -> Result<LinkedFile> {
|
||||
let kind = r.tag()?;
|
||||
let version = r.u32()?;
|
||||
let n = r.u8()? as usize;
|
||||
let uuid = String::from_utf8_lossy(r.bytes(n)?).into_owned();
|
||||
let name = descriptor::read_unicode(&mut r)?;
|
||||
let file_type = String::from_utf8_lossy(&r.tag()?).trim().to_string();
|
||||
let _creator = r.tag()?;
|
||||
let dlen = r.u64()? as usize;
|
||||
if r.u8()? != 0 {
|
||||
let _v = r.u32()?;
|
||||
descriptor::read_descriptor(&mut r)?;
|
||||
}
|
||||
let mut out = LinkedFile { uuid, name, file_type, data: None };
|
||||
match &kind {
|
||||
b"liFD" => {
|
||||
let s = r.pos;
|
||||
r.skip(dlen)?;
|
||||
out.data = Some(s..s + dlen);
|
||||
}
|
||||
b"liFE" => {
|
||||
let _v = r.u32()?;
|
||||
descriptor::read_descriptor(&mut r)?;
|
||||
if version > 3 {
|
||||
r.skip(4 + 4 + 8)?;
|
||||
}
|
||||
let _size = r.u64()?;
|
||||
if version > 2 && r.remaining() >= dlen && dlen > 0 {
|
||||
let s = r.pos;
|
||||
out.data = Some(s..s + dlen);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(out)
|
||||
})();
|
||||
match rec {
|
||||
Ok(f) => self.linked.push(f),
|
||||
Err(_) => break,
|
||||
}
|
||||
pos = rec_end.div_ceil(4) * 4;
|
||||
}
|
||||
}
|
||||
|
||||
fn layer_info(&mut self, data: &[u8], start: usize, end: usize) -> Result<()> {
|
||||
let mut r = Reader::at(&data[..end], start);
|
||||
let count = r.i16()?;
|
||||
@@ -522,6 +643,46 @@ impl Psd {
|
||||
l.adjustment = Some(Adjustment::Other(String::from_utf8_lossy(key).into_owned()));
|
||||
}
|
||||
}
|
||||
b"SoLd" | b"SoLE" => {
|
||||
let _id = r.tag()?;
|
||||
let _ver = r.u32()?;
|
||||
let _dver = r.u32()?;
|
||||
let d = descriptor::read_descriptor(&mut r)?;
|
||||
let uuid = d.text("Idnt").unwrap_or_default().to_string();
|
||||
let nums = |k: &str| -> Option<Vec<f64>> {
|
||||
match d.get(k)? {
|
||||
DValue::List(l) => Some(
|
||||
l.iter()
|
||||
.filter_map(|v| match v {
|
||||
DValue::Double(x) | DValue::UnitFloat(_, x) => Some(*x),
|
||||
DValue::Integer(i) => Some(*i as f64),
|
||||
_ => None,
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
DValue::UnitFloats(_, v) => Some(v.clone()),
|
||||
_ => None,
|
||||
}
|
||||
};
|
||||
let t = nums("Trnf").filter(|t| t.len() == 8).unwrap_or_else(|| vec![0.0; 8]);
|
||||
let size = d.obj("Sz ").and_then(|s| Some([s.num("Wdth")?, s.num("Hght")?]));
|
||||
l.smart_object = Some(SmartObject { uuid, quad: [[t[0], t[1]], [t[2], t[3]], [t[4], t[5]], [t[6], t[7]]], size });
|
||||
}
|
||||
b"PlLd" if l.smart_object.is_none() => {
|
||||
let _id = r.tag()?;
|
||||
let _ver = r.u32()?;
|
||||
let n = r.u8()? as usize;
|
||||
let uuid = String::from_utf8_lossy(r.bytes(n)?).into_owned();
|
||||
let _page = r.u32()?;
|
||||
let _pages = r.u32()?;
|
||||
let _aa = r.u32()?;
|
||||
let _ty = r.u32()?;
|
||||
let mut t = [0.0; 8];
|
||||
for v in &mut t {
|
||||
*v = r.f64()?;
|
||||
}
|
||||
l.smart_object = Some(SmartObject { uuid, quad: [[t[0], t[1]], [t[2], t[3]], [t[4], t[5]], [t[6], t[7]]], size: None });
|
||||
}
|
||||
b"vmsk" | b"vsms" => {
|
||||
let _ver = r.u32()?;
|
||||
let flags = r.u32()?;
|
||||
|
||||
@@ -187,3 +187,34 @@ fn truncated_and_garbage_input_errors() {
|
||||
assert!(Psd::parse(b"8BPS\0\x09garbage".to_vec()).is_err());
|
||||
assert!(Psd::parse(b"PNG".to_vec()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smart_objects_and_embedded_files() {
|
||||
let mut inner = WDoc::new(20, 10);
|
||||
inner.layers = vec![WLayer::solid("Red", Rect::new(0, 0, 20, 10), [1.0, 0.0, 0.0, 1.0])];
|
||||
let inner_bytes = write(&inner);
|
||||
let mut d = WDoc::new(64, 48);
|
||||
// The content scaled 2× and rotated 90° clockwise: top-left at (40, 4).
|
||||
let quad = [[40.0, 4.0], [40.0, 44.0], [20.0, 44.0], [20.0, 4.0]];
|
||||
d.layers = vec![
|
||||
WLayer::solid("Back", Rect::new(0, 0, 64, 48), [0.0, 0.0, 0.0, 1.0]),
|
||||
WLayer::solid("Placed", Rect::new(20, 4, 20, 40), [0.5, 0.5, 0.5, 1.0]).with_block(smart_object_block("uuid-1", quad, [20.0, 10.0])),
|
||||
];
|
||||
d.linked = vec![("uuid-1".into(), "inner.psd".into(), *b"8BPS", inner_bytes.clone())];
|
||||
let p = Psd::parse(write(&d)).unwrap();
|
||||
assert_eq!(p.linked.len(), 1);
|
||||
assert_eq!(p.linked[0].name, "inner.psd");
|
||||
assert_eq!(p.linked[0].file_type, "8BPS");
|
||||
assert_eq!(p.linked_data("uuid-1").unwrap(), &inner_bytes[..]);
|
||||
let so = p.layers[1].smart_object.clone().unwrap();
|
||||
assert_eq!(so.uuid, "uuid-1");
|
||||
assert_eq!(so.quad, quad);
|
||||
assert_eq!(so.size, Some([20.0, 10.0]));
|
||||
let (c, sx, sy, rot) = so.placement(20.0, 10.0);
|
||||
assert_eq!(c, [30.0, 24.0]);
|
||||
assert!((sx - 2.0).abs() < 1e-9 && (sy - 2.0).abs() < 1e-9 && (rot - 90.0).abs() < 1e-9, "{sx} {sy} {rot}");
|
||||
// The embedded document parses on its own.
|
||||
let e = Psd::parse(p.linked_data("uuid-1").unwrap().to_vec()).unwrap();
|
||||
assert_eq!((e.width, e.height), (20, 10));
|
||||
assert!(p.layers[0].smart_object.is_none());
|
||||
}
|
||||
|
||||
+56
-1
@@ -103,11 +103,14 @@ pub struct WDoc {
|
||||
/// Merged image (straight RGBA, document-sized); `None` composites visible pixel layers
|
||||
/// with Normal blending.
|
||||
pub composite: Option<Vec<[f32; 4]>>,
|
||||
/// Embedded files for smart objects: (unique id, file name, type code, bytes), written as
|
||||
/// linked layer data (`lnk2`).
|
||||
pub linked: Vec<(String, String, [u8; 4], Vec<u8>)>,
|
||||
}
|
||||
|
||||
impl WDoc {
|
||||
pub fn new(width: u32, height: u32) -> WDoc {
|
||||
WDoc { width, height, depth: 8, mode: ColorMode::Rgb, rle: true, layers: vec![], composite: None }
|
||||
WDoc { width, height, depth: 8, mode: ColorMode::Rgb, rle: true, layers: vec![], composite: None, linked: vec![] }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,6 +342,13 @@ pub fn write(doc: &WDoc) -> Vec<u8> {
|
||||
lm.extend_from_slice(&li);
|
||||
u32b(&mut lm, 0);
|
||||
}
|
||||
if !doc.linked.is_empty() {
|
||||
let d = linked_block(&doc.linked);
|
||||
lm.extend_from_slice(b"8BIM");
|
||||
lm.extend_from_slice(b"lnk2");
|
||||
u32b(&mut lm, d.len() as u32);
|
||||
lm.extend_from_slice(&d);
|
||||
}
|
||||
}
|
||||
u32b(&mut out, lm.len() as u32);
|
||||
out.extend_from_slice(&lm);
|
||||
@@ -372,8 +382,53 @@ pub fn write(doc: &WDoc) -> Vec<u8> {
|
||||
out
|
||||
}
|
||||
|
||||
/// Linked layer data records (`liFD`, version 7) for embedded files.
|
||||
fn linked_block(files: &[(String, String, [u8; 4], Vec<u8>)]) -> Vec<u8> {
|
||||
let mut out = vec![];
|
||||
for (uuid, name, ty, data) in files {
|
||||
let mut rec = vec![];
|
||||
rec.extend_from_slice(b"liFD");
|
||||
u32b(&mut rec, 7);
|
||||
rec.push(uuid.len() as u8);
|
||||
rec.extend_from_slice(uuid.as_bytes());
|
||||
rec.extend_from_slice(&unicode_plain(name));
|
||||
rec.extend_from_slice(ty);
|
||||
rec.extend_from_slice(b"8BIM");
|
||||
rec.extend_from_slice(&(data.len() as u64).to_be_bytes());
|
||||
rec.push(0); // no file-open descriptor
|
||||
rec.extend_from_slice(data);
|
||||
u32b(&mut rec, 0); // child document id (empty unicode string)
|
||||
rec.extend_from_slice(&0f64.to_be_bytes()); // asset modification time
|
||||
rec.push(0); // asset locked state
|
||||
out.extend_from_slice(&(rec.len() as u64).to_be_bytes());
|
||||
out.extend_from_slice(&rec);
|
||||
while !out.len().is_multiple_of(4) {
|
||||
out.push(0);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- block helpers
|
||||
|
||||
/// `SoLd`: a placed layer showing linked file `uuid` (content `size` pixels) with its corners at
|
||||
/// `quad` (top-left, top-right, bottom-right, bottom-left).
|
||||
pub fn smart_object_block(uuid: &str, quad: [[f64; 2]; 4], size: [f64; 2]) -> ([u8; 4], Vec<u8>) {
|
||||
let list = |v: &[f64]| DValue::List(v.iter().map(|x| DValue::Double(*x)).collect());
|
||||
let t: Vec<f64> = quad.iter().flat_map(|p| [p[0], p[1]]).collect();
|
||||
let d = Descriptor::new("null")
|
||||
.with("Idnt", DValue::Text(uuid.to_string()))
|
||||
.with("placed", DValue::Text(uuid.to_string()))
|
||||
.with("Trnf", list(&t))
|
||||
.with("nonAffineTransform", list(&t))
|
||||
.with("Sz ", DValue::Descriptor(Descriptor::new("Pnt ").with("Wdth", DValue::Double(size[0])).with("Hght", DValue::Double(size[1]))));
|
||||
let mut out = b"soLD".to_vec();
|
||||
u32b(&mut out, 4);
|
||||
u32b(&mut out, 16);
|
||||
descriptor::write_descriptor(&mut out, &d);
|
||||
(*b"SoLd", out)
|
||||
}
|
||||
|
||||
/// `TySh`: a type layer with one style run; the text origin sits at `origin`.
|
||||
pub fn text_block(text: &str, origin: [f64; 2], font: &str, size: f64, color: [f64; 3], justification: u8) -> ([u8; 4], Vec<u8>) {
|
||||
let mut d = vec![];
|
||||
|
||||
Reference in New Issue
Block a user