From a089c3f312beb6a3ebb1694f15ce66a162c7295c Mon Sep 17 00:00:00 2001 From: FredApps Date: Wed, 18 Nov 2026 21:23:52 +0100 Subject: [PATCH] Delegate raw adjustment writers to craft-psd --- Cargo.lock | 2 +- Cargo.toml | 2 +- crates/psd/src/tests.rs | 1 + crates/psd/src/tests/fixed_writers.rs | 26 ++++++++++++++++++++++++++ crates/psd/src/write.rs | 16 ++++------------ 5 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 crates/psd/src/tests/fixed_writers.rs diff --git a/Cargo.lock b/Cargo.lock index 92dc7b239..431e35b39 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=62f7a7e4777d9b60f55a18a8865c73bb12297f38#62f7a7e4777d9b60f55a18a8865c73bb12297f38" +source = "git+https://github.com/storytold/craft-libs?rev=5c0abaaa4418ccffbf6717c983c1dec9fa8a7741#5c0abaaa4418ccffbf6717c983c1dec9fa8a7741" dependencies = [ "encoding_rs", "flate2", diff --git a/Cargo.toml b/Cargo.toml index 81d390d38..4f4ef770d 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 = "62f7a7e4777d9b60f55a18a8865c73bb12297f38" } +craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5c0abaaa4418ccffbf6717c983c1dec9fa8a7741" } 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/tests.rs b/crates/psd/src/tests.rs index 915b48ed2..0a7d23ce1 100644 --- a/crates/psd/src/tests.rs +++ b/crates/psd/src/tests.rs @@ -3,6 +3,7 @@ use super::*; use crate::pixels::srgb_encode; mod fixed_adjustments; +mod fixed_writers; mod integration; mod opt; mod section_divider; diff --git a/crates/psd/src/tests/fixed_writers.rs b/crates/psd/src/tests/fixed_writers.rs new file mode 100644 index 000000000..129b501d8 --- /dev/null +++ b/crates/psd/src/tests/fixed_writers.rs @@ -0,0 +1,26 @@ +use super::*; + +#[test] +fn fixed_adjustment_writers_keep_exact_bytes_through_full_psd() { + let cases = [ + (brightness_block(i16::MIN, i16::MAX), *b"brit", vec![0x80, 0, 0x7f, 0xff, 0, 0x7f, 0]), + (brightness_block(-1, 0), *b"brit", vec![0xff, 0xff, 0, 0, 0, 0x7f, 0]), + (exposure_block(0.0, -0.0, f32::INFINITY), *b"expA", vec![0, 1, 0, 0, 0, 0, 0x80, 0, 0, 0, 0x7f, 0x80, 0, 0]), + ( + exposure_block(f32::NEG_INFINITY, f32::from_bits(0x7fc1_2345), f32::from_bits(0xffc5_4321)), + *b"expA", + vec![0, 1, 0xff, 0x80, 0, 0, 0x7f, 0xc1, 0x23, 0x45, 0xff, 0xc5, 0x43, 0x21], + ), + (exposure_block(1.0, -2.5, 0.125), *b"expA", vec![0, 1, 0x3f, 0x80, 0, 0, 0xc0, 0x20, 0, 0, 0x3e, 0, 0, 0]), + ]; + for (block, key, expected) in cases { + assert_eq!(block.0, key); + assert_eq!(block.1, expected); + let mut doc = WDoc::new(1, 1); + doc.layers = vec![WLayer::empty("raw adjustment", vec![block])]; + let bytes = write(&doc); + let file = craft_psd::PsdFile::from_bytes(&bytes).unwrap(); + let emitted = file.layer_info.as_ref().unwrap().layers[0].block(&key).unwrap(); + assert_eq!(emitted.data, expected); + } +} diff --git a/crates/psd/src/write.rs b/crates/psd/src/write.rs index e5ec285b8..b8067fc92 100644 --- a/crates/psd/src/write.rs +++ b/crates/psd/src/write.rs @@ -465,21 +465,13 @@ pub fn invert_block() -> ([u8; 4], Vec) { } pub fn brightness_block(brightness: i16, contrast: i16) -> ([u8; 4], Vec) { - let mut d = vec![]; - for v in [brightness, contrast, 127] { - d.extend_from_slice(&v.to_be_bytes()); - } - d.push(0); - (*b"brit", d) + let block = craft_psd::adjust::brightness_contrast_block(brightness, contrast); + (block.key, block.data) } pub fn exposure_block(exposure: f32, offset: f32, gamma: f32) -> ([u8; 4], Vec) { - let mut d = vec![]; - put_u16(&mut d, 1); - for v in [exposure, offset, gamma] { - d.extend_from_slice(&v.to_be_bytes()); - } - (*b"expA", d) + let block = craft_psd::adjust::exposure_block(exposure, offset, gamma); + (block.key, block.data) } /// `vmsk`: a vector mask from closed polygons/curves given as (in, anchor, out) knots in pixels.