mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-19 16:45:28 +00:00
Merge pull request #3921 from FredApps/fix/786-psd-tagged-descriptor-adapter
psd: share recovering descriptor-block headers
This commit is contained in:
5 files changed
+110
-20
No files matched your search
Generated
+1
-1
@@ -1463,7 +1463,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-psd"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=62f7a7e4777d9b60f55a18a8865c73bb12297f38#62f7a7e4777d9b60f55a18a8865c73bb12297f38"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=c62d548b6b806beac8c1f93f2f23f4f2f3cf1c13#c62d548b6b806beac8c1f93f2f23f4f2f3cf1c13"
|
||||
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 = "62f7a7e4777d9b60f55a18a8865c73bb12297f38" }
|
||||
craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c62d548b6b806beac8c1f93f2f23f4f2f3cf1c13" }
|
||||
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" }
|
||||
|
||||
+15
-18
@@ -195,15 +195,21 @@ impl Psd {
|
||||
d.obj("bounds").or_else(|| d.obj("boundingBox")).and_then(|o| Some([o.num("Left")?, o.num("Top ")?, o.num("Rght")?, o.num("Btom")?]));
|
||||
l.text = Some(TextInfo { text, transform: t, style, bounds });
|
||||
}
|
||||
b"lfx2" => {
|
||||
let _ver = r.u32()?;
|
||||
let _dver = r.u32()?;
|
||||
l.effects = Some(descriptor::read_descriptor(&mut r)?);
|
||||
}
|
||||
b"SoCo" => {
|
||||
let _ver = r.u32()?;
|
||||
let d = descriptor::read_descriptor(&mut r)?;
|
||||
l.fill = d.color("Clr ");
|
||||
b"lfx2" | b"SoCo" | b"CgEd" => {
|
||||
if let Some(parsed) = block.descriptor_prefix_recovering_versions_with_limits(descriptor::PARSE_LIMITS) {
|
||||
let d = descriptor::from_craft_descriptor(parsed.map_err(descriptor::desc_err)?);
|
||||
match key {
|
||||
b"lfx2" => l.effects = Some(d),
|
||||
b"SoCo" => l.fill = d.color("Clr "),
|
||||
_ => {
|
||||
l.adjustment = Some(Adjustment::BrightnessContrast {
|
||||
brightness: d.num("Brgh").unwrap_or(0.0),
|
||||
contrast: d.num("Cntr").unwrap_or(0.0),
|
||||
legacy: d.bool("useLegacy").unwrap_or(false),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
b"nvrt" => l.adjustment = Some(Adjustment::Invert),
|
||||
b"brit" | b"hue2" | b"hue " | b"levl" | b"expA" => {
|
||||
@@ -216,15 +222,6 @@ impl Psd {
|
||||
}
|
||||
}
|
||||
}
|
||||
b"CgEd" => {
|
||||
let _ver = r.u32()?;
|
||||
let d = descriptor::read_descriptor(&mut r)?;
|
||||
l.adjustment = Some(Adjustment::BrightnessContrast {
|
||||
brightness: d.num("Brgh").unwrap_or(0.0),
|
||||
contrast: d.num("Cntr").unwrap_or(0.0),
|
||||
legacy: d.bool("useLegacy").unwrap_or(false),
|
||||
});
|
||||
}
|
||||
b"curv" | b"blnc" | b"post" | b"thrs" | b"selc" | b"mixr" | b"phfl" | b"vibA" | b"blwh" | b"grdm" | b"clrL" if l.adjustment.is_none() => {
|
||||
l.adjustment = Some(Adjustment::Other(String::from_utf8_lossy(key).into_owned()));
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ mod opt;
|
||||
mod scalar_blocks;
|
||||
mod section_divider;
|
||||
mod shared_corpus;
|
||||
mod tagged_descriptors;
|
||||
mod tysh_prefix;
|
||||
mod unicode_name;
|
||||
mod vector_records;
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
use super::*;
|
||||
use craft_psd::descriptor::{Descriptor as Shared, UnicodeString, Value};
|
||||
|
||||
fn bytes(key: [u8; 4], d: Shared, version: u32) -> Vec<u8> {
|
||||
let mut b = version.to_be_bytes().repeat(if key == *b"lfx2" { 2 } else { 1 });
|
||||
b.extend(d.to_bytes());
|
||||
b
|
||||
}
|
||||
|
||||
fn state(blocks: Vec<([u8; 4], Vec<u8>)>) -> (Option<descriptor::Descriptor>, Option<[f64; 3]>, Option<Adjustment>) {
|
||||
let mut doc = WDoc::new(1, 1);
|
||||
doc.layers = vec![WLayer::empty("Descriptor", blocks)];
|
||||
let layer = Psd::parse(write(&doc)).unwrap().layers.remove(0);
|
||||
(layer.effects, layer.fill, layer.adjustment)
|
||||
}
|
||||
|
||||
fn value(key: [u8; 4]) -> Shared {
|
||||
match &key {
|
||||
b"lfx2" => Shared::new("null").with("name", Value::Text(UnicodeString::new("effect"))),
|
||||
b"SoCo" => Shared::new("null").with(
|
||||
"Clr ",
|
||||
Value::Descriptor(Shared::new("RGBC").with("Rd ", Value::Double(255.0)).with("Grn ", Value::Double(127.5)).with("Bl ", Value::Double(0.0))),
|
||||
),
|
||||
_ => Shared::new("null").with("Brgh", Value::Double(12.0)).with("Cntr", Value::Double(-4.0)).with("useLegacy", Value::Boolean(true)),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tagged_descriptor_headers_ignore_versions_and_tails_and_preserve_short_block_state() {
|
||||
for key in [*b"lfx2", *b"SoCo", *b"CgEd"] {
|
||||
let good = bytes(key, value(key), 16);
|
||||
let expected = state(vec![(key, good.clone())]);
|
||||
match &key {
|
||||
b"lfx2" => assert_eq!(expected.0.as_ref().unwrap().text("name"), Some("effect")),
|
||||
b"SoCo" => assert_eq!(expected.1, Some([1.0, 0.5, 0.0])),
|
||||
_ => assert_eq!(expected.2, Some(Adjustment::BrightnessContrast { brightness: 12.0, contrast: -4.0, legacy: true })),
|
||||
}
|
||||
for cut in 0..good.len() {
|
||||
assert_eq!(state(vec![(key, good[..cut].to_vec())]), (None, None, None), "{key:?} cut {cut}");
|
||||
assert_eq!(state(vec![(key, good.clone()), (key, good[..cut].to_vec())]), expected);
|
||||
}
|
||||
for version in [0, 99, u32::MAX] {
|
||||
for tail in [vec![], vec![255], vec![1, 2, 3, 4, 5]] {
|
||||
let mut b = bytes(key, value(key), version);
|
||||
b.extend(tail);
|
||||
assert_eq!(state(vec![(key, b)]), expected);
|
||||
}
|
||||
}
|
||||
let empty = bytes(key, Shared::new("null"), 0);
|
||||
let replaced = state(vec![(key, good), (key, empty)]);
|
||||
match &key {
|
||||
b"lfx2" => assert!(replaced.0.unwrap().items.is_empty()),
|
||||
b"SoCo" => assert_eq!(replaced.1, None),
|
||||
_ => assert_eq!(replaced.2, Some(Adjustment::BrightnessContrast { brightness: 0.0, contrast: 0.0, legacy: false })),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tagged_descriptors_keep_normalization_and_adjustment_precedence() {
|
||||
let d = Shared::new("null").with("Brgh", Value::Double(f64::NAN)).with("Cntr", Value::UnitFloat { unit: *b"#Prc", value: f64::INFINITY });
|
||||
let cged = bytes(*b"CgEd", d, 99);
|
||||
let expected = Some(Adjustment::BrightnessContrast { brightness: 0.0, contrast: 0.0, legacy: false });
|
||||
assert_eq!(state(vec![(*b"nvrt", vec![]), (*b"CgEd", cged.clone())]).2, expected);
|
||||
assert_eq!(state(vec![(*b"CgEd", cged.clone()), (*b"brit", vec![0, 42, 0, 7])]).2, expected);
|
||||
assert_eq!(state(vec![(*b"CgEd", cged), (*b"nvrt", vec![])]).2, Some(Adjustment::Invert));
|
||||
let color = Shared::new("RGBC").with("Rd ", Value::Double(f64::NEG_INFINITY)).with("Grn ", Value::Double(255.0)).with("Bl ", Value::Double(0.0));
|
||||
assert_eq!(state(vec![(*b"SoCo", bytes(*b"SoCo", Shared::new("null").with("Clr ", Value::Descriptor(color)), 0))]).1, Some([0.0, 1.0, 0.0]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tagged_descriptor_raw_fixture_and_limits_do_not_depend_on_shared_writer() {
|
||||
for key in [*b"lfx2", *b"SoCo", *b"CgEd"] {
|
||||
let mut raw = vec![255; if key == *b"lfx2" { 8 } else { 4 }];
|
||||
raw.extend([0; 8]);
|
||||
raw.extend(b"null");
|
||||
raw.extend(0u32.to_be_bytes());
|
||||
let result = state(vec![(key, raw.clone())]);
|
||||
if key == *b"lfx2" {
|
||||
assert!(result.0.is_some());
|
||||
}
|
||||
if key == *b"CgEd" {
|
||||
assert!(result.2.is_some());
|
||||
}
|
||||
let end = raw.len();
|
||||
raw[end - 4..].copy_from_slice(&100_001u32.to_be_bytes());
|
||||
let block = craft_psd::tagged::TaggedBlock::new(key, raw.clone());
|
||||
assert!(matches!(block.descriptor_prefix_recovering_versions_with_limits(descriptor::PARSE_LIMITS), Some(Err(craft_psd::PsdError::LimitExceeded(_)))));
|
||||
let good = bytes(key, value(key), 16);
|
||||
assert_eq!(state(vec![(key, good.clone()), (key, raw)]), state(vec![(key, good)]));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user