Merge main preserving scalar reader integration

This commit is contained in:
FredApps committed 2026-11-18 21:27:47 +01:00
commit b7c311d106
3 files changed
+55 -4

No files matched your search

+14 -4
View File
@@ -114,7 +114,7 @@ impl Psd {
// A malformed optional block only loses that block's information.
// Unsupported modern placement geometry must not silently become an
// unwarped layer or fall back to a less expressive PlLd in either order.
match self.layer_block(&mut l, &b.key, &b.data) {
match self.layer_block(&mut l, b) {
Err(e @ Error::Unsupported(_)) if matches!(&b.key, b"SoLd" | b"SoLE") => return Err(e),
_ => {}
}
@@ -140,7 +140,9 @@ impl Psd {
}
}
fn layer_block(&self, l: &mut Layer, key: &[u8; 4], b: &[u8]) -> Result<()> {
fn layer_block(&self, l: &mut Layer, block: &craft_psd::tagged::TaggedBlock) -> Result<()> {
let key = &block.key;
let b = block.data.as_slice();
let mut r = Reader::new(b);
match key {
b"luni" => {
@@ -155,8 +157,16 @@ impl Psd {
l.name = name;
}
}
b"lyid" => l.id = Some(r.u32()?),
b"iOpa" => l.fill_opacity = r.u8()?,
b"lyid" | b"iOpa" => {
use craft_psd::tagged::BlockData;
// Borrow the original block; short fields keep the previous value,
// while complete prefixes retain the shared parser's trailing-byte policy.
match block.parsed().transpose().map_err(|e| error(&e))? {
Some(BlockData::LayerId(id)) => l.id = Some(id),
Some(BlockData::FillOpacity(opacity)) => l.fill_opacity = opacity,
_ => {}
}
}
b"lsct" | b"lsdk" => {
use craft_psd::tagged::{SectionDivider, SectionType};
let divider = SectionDivider::from_bytes_recovering_blend(b).map_err(|e| error(&e))?;
+1
View File
@@ -6,6 +6,7 @@ mod fixed_adjustments;
mod fixed_writers;
mod integration;
mod opt;
mod scalar_blocks;
mod section_divider;
mod shared_corpus;
mod tysh_prefix;
+40
View File
@@ -0,0 +1,40 @@
use super::*;
fn scalars(blocks: Vec<([u8; 4], Vec<u8>)>) -> (Option<u32>, u8, u8) {
let mut doc = WDoc::new(1, 1);
doc.layers = vec![WLayer::empty("Scalars", blocks).with_opacity(37)];
let layer = Psd::parse(write(&doc)).unwrap().layers.remove(0);
(layer.id, layer.fill_opacity, layer.opacity)
}
#[test]
fn scalar_prefixes_keep_defaults_and_ignore_trailing_bytes() {
assert_eq!(scalars(vec![]), (None, 255, 37));
let bytes = [0x89, 0xab, 0xcd, 0xef, 255, 0, 23];
for end in 0..=bytes.len() {
assert_eq!(scalars(vec![(*b"lyid", bytes[..end].to_vec())]), (if end < 4 { None } else { Some(0x89ab_cdef) }, 255, 37), "lyid prefix {end}");
assert_eq!(scalars(vec![(*b"iOpa", bytes[..end].to_vec())]), (None, if end == 0 { 255 } else { 0x89 }, 37), "iOpa prefix {end}");
}
for id in [0u32, 1, 0x8000_0000, u32::MAX] {
for opacity in [0, 1, 254, 255] {
assert_eq!(scalars(vec![(*b"lyid", id.to_be_bytes().to_vec()), (*b"iOpa", vec![opacity])]), (Some(id), opacity, 37));
}
}
}
#[test]
fn repeated_scalar_blocks_preserve_prior_values_on_short_fields_in_both_orders() {
for reverse in [false, true] {
for short in 0..4 {
let mut initial = vec![(*b"lyid", vec![0, 0, 0, 42]), (*b"iOpa", vec![99])];
if reverse {
initial.reverse();
}
let mut blocks = initial.clone();
blocks.extend([(*b"lyid", vec![255; short]), (*b"iOpa", vec![]), (*b"zzzz", vec![0; 4])]);
assert_eq!(scalars(blocks.clone()), (Some(42), 99, 37));
blocks.extend([(*b"iOpa", vec![0, 77]), (*b"lyid", vec![0, 0, 0, 7, 255])]);
assert_eq!(scalars(blocks), (Some(7), 0, 37));
}
}
}