mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-19 16:45:28 +00:00
psd: read vector mask records through craft-psd
This commit is contained in:
1 parent
3f1cd00745
commit
c149bb513f
5 files changed
+108
-30
No files matched your search
Generated
+1
-1
@@ -1449,7 +1449,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-psd"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e1fc4b1376148197c2cfd2381eeb400e2731269a#e1fc4b1376148197c2cfd2381eeb400e2731269a"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a557fed94e0f528c281b4bab241eb518b7f756ac#a557fed94e0f528c281b4bab241eb518b7f756ac"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"flate2",
|
||||
|
||||
+1
-1
@@ -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 = "e1fc4b1376148197c2cfd2381eeb400e2731269a" }
|
||||
craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "a557fed94e0f528c281b4bab241eb518b7f756ac" }
|
||||
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" }
|
||||
|
||||
+20
-28
@@ -247,45 +247,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<VectorMask> {
|
||||
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<f64> { 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<Node> {
|
||||
|
||||
@@ -5,6 +5,7 @@ use crate::pixels::srgb_encode;
|
||||
mod integration;
|
||||
mod opt;
|
||||
mod shared_corpus;
|
||||
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()
|
||||
|
||||
@@ -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<u8> {
|
||||
let mut out = selector.to_be_bytes().to_vec();
|
||||
out.extend_from_slice(&data);
|
||||
out
|
||||
}
|
||||
|
||||
fn length(selector: u16, count: u16) -> Vec<u8> {
|
||||
let mut data = [0; 24];
|
||||
data[..2].copy_from_slice(&count.to_be_bytes());
|
||||
record(selector, data)
|
||||
}
|
||||
|
||||
fn knot(selector: u16) -> Vec<u8> {
|
||||
let mut data = [0; 24];
|
||||
// Stored y then x, including negative and outside-document coordinates.
|
||||
for (chunk, value) in data.chunks_exact_mut(4).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<u8>) -> Option<VectorMask> {
|
||||
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<u8> {
|
||||
[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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user