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

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

Photoshop writes layer locks with the artboard bit at 0x08 (a Background
layer carries 0x0D). PhotoCraft read it at 0x10, so 0x0D came back as
0x05 and the lock bytes drifted on every re-save. Map artboard to bit 3
on both read and write; still accept 0x10 on read so older PhotoCraft
files keep their lock, rewriting it at 0x08.

Co-Authored-By: Qwen 3.8 Flash <[email protected]>

* style(io): fix PSD artboard lock review comments

Co-authored-by: Copilot <[email protected]>

Copilot-Session: e5e4c3e6-e406-4010-b05d-5042f9f4e4fc

---------

Co-authored-by: Qwen 3.8 Flash <[email protected]>
Copilot-Session: e5e4c3e6-e406-4010-b05d-5042f9f4e4fc
This commit is contained in:
authored and GitHub committed 2026-10-07 07:08:00 -04:00
1 parent 395a52e7f1
commit d07eecf66f
2 files changed
+25 -5

No files matched your search

+12 -5
View File
@@ -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);
}
+13
View File
@@ -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};