Skip to content

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

PSD: read/write the artboard lock at Photoshop's lspf bit 3 (#395) (#406)

Alex Gorevskid07eecf66f49
diff --git a/crates/io/src/blocks.rs b/crates/io/src/blocks.rs
index cff39bb76..bb1393203 100644
--- a/crates/io/src/blocks.rs
+++ b/crates/io/src/blocks.rs
@@ -46,15 +46,16 @@ pub fn ranges_from_blend_if(b: &BlendIf, channels: usize) -> BlendingRanges {
}
/// `lspf` bits (Adobe spec: bit 0 transparency, 1 composite, 2 position).
-/// Artboard (bit 3... here `0x10` as observed by ag-psd) and "all" (bit 31)
-/// are undocumented.
+/// Artboard (bit 3, `0x08`, as reference files write it) and "all" (bit 31) are undocumented.
+/// Older PhotoCraft builds wrote the artboard lock at bit 4 (`0x10`); that value is still
+/// accepted on read so those files keep their lock, and is rewritten at `0x08`.
pub fn locks_from_lspf(v: u32) -> Locks {
- Locks { transparency: v & 1 != 0, pixels: v & 2 != 0, position: v & 4 != 0, artboard: v & 0x10 != 0, all: v & 0x8000_0000 != 0 }
+ Locks { transparency: v & 1 != 0, pixels: v & 2 != 0, position: v & 4 != 0, artboard: v & 0x08 != 0 || v & 0x10 != 0, all: v & 0x8000_0000 != 0 }
}
/// Inverse of [`locks_from_lspf`].
pub fn lspf_from_locks(l: &Locks) -> u32 {
- u32::from(l.transparency) | u32::from(l.pixels) << 1 | u32::from(l.position) << 2 | u32::from(l.artboard) << 4 | u32::from(l.all) << 31
+ u32::from(l.transparency) | u32::from(l.pixels) << 1 | u32::from(l.position) << 2 | u32::from(l.artboard) << 3 | u32::from(l.all) << 31
}
/// `lclr` index → label.
@@ -693,9 +694,15 @@ mod tests {
#[test]
fn locks_and_labels() {
- for bits in [0u32, 1, 2, 4, 0x10, 0x8000_0000, 0x8000_0017] {
+ // Reference values round-trip byte for byte; Background layers carry 0x0D
+ // (transparency + position + the bit-3 lock reference files write).
+ for bits in [0u32, 1, 2, 4, 8, 0x0D, 0x8000_0000, 0x8000_000F] {
assert_eq!(lspf_from_locks(&locks_from_lspf(bits)), bits);
}
+ // Older PhotoCraft files wrote the artboard lock at 0x10; it reads back as artboard
+ // and is rewritten at the reference value 0x08.
+ assert!(locks_from_lspf(0x10).artboard);
+ assert_eq!(lspf_from_locks(&locks_from_lspf(0x10)), 0x08);
for i in 0..8 {
assert_eq!(label_index(label_from_index(i)), i);
}
diff --git a/crates/io/tests/psd_structure.rs b/crates/io/tests/psd_structure.rs
index 2d47830e4..2660461da 100644
--- a/crates/io/tests/psd_structure.rs
+++ b/crates/io/tests/psd_structure.rs
@@ -265,6 +265,19 @@ fn locks_and_labels_from_psd() {
assert_eq!(l.label, photocraft_doc::LabelColor::Yellow);
}
+#[test]
+fn artboard_lock_round_trips_byte_for_byte() {
+ // Reference files write a Background layer's locks as lspf = 0x0D (transparency + position +
+ // the bit-3 lock). PhotoCraft read the artboard lock at bit 4, so 0x0D came back as 0x05
+ // and the lock bytes drifted on every re-save.
+ use photocraft_psd::TaggedBlock;
+ let mut f = testgen::small(Version::Psd, Compression::Raw);
+ f.layers_mut()[0].blocks.push(TaggedBlock::protection(0x0D));
+ let g = via_doc(&f);
+ let lspf = g.layers().iter().find_map(|l| l.block(b"lspf")).expect("exported layer should carry an lspf block");
+ assert_eq!(lspf.data, 0x0Du32.to_be_bytes());
+}
+
#[test]
fn adjustment_and_fill_layers_from_psd() {
use photocraft_psd::{LayerRecord, TaggedBlock};