diff --git a/Cargo.lock b/Cargo.lock index e10293d96..92dc7b239 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1463,7 +1463,7 @@ dependencies = [ [[package]] name = "craft-psd" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=d04bd2c4fbf49e399c7e0cd7a16feebebadd739c#d04bd2c4fbf49e399c7e0cd7a16feebebadd739c" +source = "git+https://github.com/storytold/craft-libs?rev=62f7a7e4777d9b60f55a18a8865c73bb12297f38#62f7a7e4777d9b60f55a18a8865c73bb12297f38" dependencies = [ "encoding_rs", "flate2", diff --git a/Cargo.toml b/Cargo.toml index c007d1628..81d390d38 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs" craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", default-features = false, features = ["read"] } craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } -craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "d04bd2c4fbf49e399c7e0cd7a16feebebadd739c" } +craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "62f7a7e4777d9b60f55a18a8865c73bb12297f38" } craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "77686f24fe8665c21369f1b593f4d65a973e2a45" } craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } diff --git a/crates/psd/src/parse.rs b/crates/psd/src/parse.rs index 473d849c9..bfb2be58d 100644 --- a/crates/psd/src/parse.rs +++ b/crates/psd/src/parse.rs @@ -232,45 +232,37 @@ impl Psd { l.smart_object = Some(placed::from_craft_smart(craft_psd::smart::SmartObject::from_placed_layer(b).map_err(|e| error(&e))?)); } b"vmsk" | b"vsms" => { - let _ver = r.u32()?; - let flags = r.u32()?; - l.vector_mask = Some(self.vector_paths(&mut r, flags)?); + let block = craft_psd::path::VectorMaskBlock::from_bytes(b).map_err(|e| error(&e))?; + l.vector_mask = Some(self.vector_paths(&block)); } _ => {} } Ok(()) } - fn vector_paths(&self, r: &mut Reader, flags: u32) -> Result { + fn vector_paths(&self, block: &craft_psd::path::VectorMaskBlock) -> VectorMask { + use craft_psd::path::{VectorMaskBlock, selector}; let (w, h) = (self.width as f64, self.height as f64); - let mut vm = VectorMask { inverted: flags & 1 != 0, disabled: flags & 4 != 0, subpaths: vec![] }; - let fixed = |r: &mut Reader| -> Result { Ok(r.i32()? as f64 / (1 << 24) as f64) }; - while r.remaining() >= 26 { - let sel = r.u16()?; - match sel { - 0 | 3 => { - let _n = r.u16()?; - r.skip(22)?; - vm.subpaths.push(SubPath { closed: sel == 0, knots: vec![] }); - } - 1 | 2 | 4 | 5 => { - let mut pts = [[0.0; 2]; 3]; - for p in &mut pts { - let y = fixed(r)?; - let x = fixed(r)?; - *p = [x * w, y * h]; - } - if let Some(sp) = vm.subpaths.last_mut() { - sp.knots.push(pts); - } - } - _ => r.skip(24)?, + let mut vm = VectorMask { + inverted: block.flags & VectorMaskBlock::FLAG_INVERT != 0, + disabled: block.flags & VectorMaskBlock::FLAG_DISABLED != 0, + subpaths: vec![], + }; + // Keep Effect's recovery policy: ignore declared counts and orphan knots, and + // keep mixed open/closed knot selectors in the last declared subpath. + // PathData::subpaths deliberately has a different, more permissive policy. + for record in &block.path.records { + if record.is_length() { + vm.subpaths.push(SubPath { closed: record.selector == selector::CLOSED_LENGTH, knots: vec![] }); + } else if let Some(knot) = record.knot() + && let Some(sp) = vm.subpaths.last_mut() + { + sp.knots.push([knot.pre, knot.anchor, knot.post].map(|(x, y)| [x * w, y * h])); } } vm.subpaths.retain(|s| !s.knots.is_empty()); - Ok(vm) + vm } - /// The layer tree, top of the stack first. Groups nest at most [`MAX_GROUP_DEPTH`] deep (see /// [`Psd::tree_report`]). pub fn tree(&self) -> Vec { diff --git a/crates/psd/src/tests.rs b/crates/psd/src/tests.rs index 628a30bff..c07077f29 100644 --- a/crates/psd/src/tests.rs +++ b/crates/psd/src/tests.rs @@ -8,6 +8,7 @@ mod opt; mod section_divider; mod shared_corpus; mod unicode_name; +mod vector_records; fn gradient(w: u32, h: u32) -> Vec<[f32; 4]> { (0..w * h).map(|i| [(i % w) as f32 / (w - 1).max(1) as f32, (i / w) as f32 / (h - 1).max(1) as f32, 0.25, 1.0]).collect() diff --git a/crates/psd/src/tests/vector_records.rs b/crates/psd/src/tests/vector_records.rs new file mode 100644 index 000000000..531650835 --- /dev/null +++ b/crates/psd/src/tests/vector_records.rs @@ -0,0 +1,85 @@ +use super::*; + +// Independent wire fixtures: do not use the shared path encoder to test its decoder. +fn record(selector: u16, data: [u8; 24]) -> Vec { + let mut out = selector.to_be_bytes().to_vec(); + out.extend_from_slice(&data); + out +} + +fn length(selector: u16, count: u16) -> Vec { + let mut data = [0; 24]; + data[..2].copy_from_slice(&count.to_be_bytes()); + record(selector, data) +} + +fn knot(selector: u16) -> Vec { + let mut data = [0; 24]; + // Stored y then x, including negative and outside-document coordinates. + for (chunk, value) in data.as_chunks_mut::<4>().0.iter_mut().zip([-(1i32 << 23), 1 << 22, 1 << 24, -(1 << 24), 1 << 23, 1 << 25]) { + chunk.copy_from_slice(&value.to_be_bytes()); + } + record(selector, data) +} + +fn parse_mask(key: [u8; 4], bytes: Vec) -> Option { + let mut doc = WDoc::new(64, 32); + doc.layers = vec![WLayer::empty("mask", vec![(key, bytes)])]; + Psd::parse(write(&doc)).unwrap().layers.remove(0).vector_mask +} + +fn header(version: u32, flags: u32) -> Vec { + [version.to_be_bytes(), flags.to_be_bytes()].concat() +} + +#[test] +fn vector_masks_preserve_record_recovery_and_dimension_mapping() { + for key in [*b"vmsk", *b"vsms"] { + for version in [3, 99] { + for tail in 0..26 { + let mut bytes = header(version, 1 | 2 | 4 | 0x80); + bytes.extend(knot(1)); // Orphan knots are ignored, not made into a path. + bytes.extend(length(0, u16::MAX)); // Empty subpath is removed. + bytes.extend(length(3, 0)); // Counts do not control app grouping. + for selector in [1, 2, 4, 5] { + bytes.extend(knot(selector)); // Keep mixed selectors in the open path. + } + bytes.extend(record(0xffff, [0xff; 24])); + bytes.extend(length(0, 1)); + bytes.extend(knot(4)); + bytes.extend(knot(2)); // More knots than declared still belong to this path. + bytes.extend(length(3, 1)); // Empty final path is removed too. + bytes.extend(std::iter::repeat_n(0xff, tail)); + let mask = parse_mask(key, bytes).unwrap(); + assert!(mask.inverted && mask.disabled); + assert_eq!(mask.subpaths.len(), 2); + assert!(!mask.subpaths[0].closed); + assert!(mask.subpaths[1].closed); + assert_eq!(mask.subpaths[0].knots.len(), 4); + assert_eq!(mask.subpaths[1].knots.len(), 2); + for path in mask.subpaths { + for points in path.knots { + assert_eq!(points, [[16.0, -16.0], [-64.0, 32.0], [128.0, 16.0]]); + } + } + } + } + } +} + +#[test] +fn vector_masks_preserve_short_header_and_empty_path_recovery() { + for key in [*b"vmsk", *b"vsms"] { + let bytes = header(3, 0); + for len in 0..8 { + assert!(parse_mask(key, bytes[..len].to_vec()).is_none()); + } + for records in [vec![], knot(5), length(3, u16::MAX), record(8, [0xff; 24])] { + let mut bytes = header(3, 2 | 0x80); + bytes.extend(records); + let mask = parse_mask(key, bytes).unwrap(); + assert!(!mask.inverted && !mask.disabled); + assert!(mask.subpaths.is_empty()); + } + } +}