From 27ce4e9fa2edc6835f386cf65d8938d4440082fc Mon Sep 17 00:00:00 2001 From: FredApps Date: Thu, 19 Nov 2026 01:02:42 +0100 Subject: [PATCH] psd: share hue and levels writer framing --- Cargo.lock | 2 +- Cargo.toml | 2 +- crates/psd/src/tests/fixed_writers.rs | 44 +++++++++++++++++++++++++++ crates/psd/src/write.rs | 24 +++------------ 4 files changed, 50 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f1481e105..c8bb8f50f 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=027416ac9f36b60229c595f942d36bf74f0ed4d2#027416ac9f36b60229c595f942d36bf74f0ed4d2" +source = "git+https://github.com/storytold/craft-libs?rev=fa4741d0bbba8da303dd1bb1a93cbcb78e7747a0#fa4741d0bbba8da303dd1bb1a93cbcb78e7747a0" dependencies = [ "encoding_rs", "flate2", diff --git a/Cargo.toml b/Cargo.toml index 148d344e6..832eeef8c 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 = "027416ac9f36b60229c595f942d36bf74f0ed4d2" } +craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "fa4741d0bbba8da303dd1bb1a93cbcb78e7747a0" } 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/fixed_writers.rs b/crates/psd/src/tests/fixed_writers.rs index 129b501d8..4aac3c9c2 100644 --- a/crates/psd/src/tests/fixed_writers.rs +++ b/crates/psd/src/tests/fixed_writers.rs @@ -24,3 +24,47 @@ fn fixed_adjustment_writers_keep_exact_bytes_through_full_psd() { assert_eq!(emitted.data, expected); } } + +fn assert_written_payload(block: ([u8; 4], Vec), key: [u8; 4], expected: Vec) { + 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(); + assert_eq!(file.layer_info.as_ref().unwrap().layers[0].block(&key).unwrap().data, expected); +} + +#[test] +fn hue_writer_keeps_all_six_unused_ranges_and_signed_master_values() { + let mut expected = vec![0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0x7f, 0xff, 0xff, 0xff]; + expected.extend([0; 144]); + assert_written_payload(hue_sat_block(i16::MIN, i16::MAX, -1), *b"hue2", expected); + let mut expected = vec![0; 160]; + expected[1] = 2; + assert_written_payload(hue_sat_block(0, 0, 0), *b"hue2", expected); +} + +#[test] +fn levels_writer_preserves_gamma_conversion_and_all_default_records() { + // Expected bytes are constants, not another execution of the conversion. + for (gamma, gamma_bytes) in [ + (0.125, [0, 13]), + (-0.125, [255, 243]), + (0.0, [0, 0]), + (-0.0, [0, 0]), + (f64::NAN, [0, 0]), + (f64::INFINITY, [127, 255]), + (f64::NEG_INFINITY, [128, 0]), + (f64::MAX, [127, 255]), + (-f64::MAX, [128, 0]), + (400.0, [127, 255]), + (-400.0, [128, 0]), + (1.0, [0, 100]), + ] { + let mut expected = vec![0, 2, 0x80, 0, 0x7f, 0xff, 0xff, 0xff, 0, 0]; + expected.extend(gamma_bytes); + expected.extend([0, 0, 0, 255, 0, 0, 0, 255, 0, 100].repeat(28)); + assert_written_payload(levels_block(i16::MIN, i16::MAX, -1, 0, gamma), *b"levl", expected); + } +} diff --git a/crates/psd/src/write.rs b/crates/psd/src/write.rs index b8067fc92..1004e28b5 100644 --- a/crates/psd/src/write.rs +++ b/crates/psd/src/write.rs @@ -434,30 +434,14 @@ pub fn solid_color_block(rgb: [f64; 3]) -> ([u8; 4], Vec) { /// `hue2`: Hue/Saturation (master). pub fn hue_sat_block(hue: i16, sat: i16, light: i16) -> ([u8; 4], Vec) { - let mut d = vec![]; - put_u16(&mut d, 2); - d.extend_from_slice(&[0, 0]); - for v in [0i16, 0, 0, hue, sat, light] { - d.extend_from_slice(&v.to_be_bytes()); - } - // Six colour ranges (unused). - d.resize(d.len() + 6 * 24, 0); - (*b"hue2", d) + let block = craft_psd::adjust::hue_saturation_block(hue, sat, light); + (block.key, block.data) } /// `levl`: Levels (composite record). pub fn levels_block(in_black: i16, in_white: i16, out_black: i16, out_white: i16, gamma: f64) -> ([u8; 4], Vec) { - let mut d = vec![]; - put_u16(&mut d, 2); - for v in [in_black, in_white, out_black, out_white, (gamma * 100.0).round() as i16] { - d.extend_from_slice(&v.to_be_bytes()); - } - for _ in 0..28 { - for v in [0i16, 255, 0, 255, 100] { - d.extend_from_slice(&v.to_be_bytes()); - } - } - (*b"levl", d) + let block = craft_psd::adjust::levels_block(in_black, in_white, out_black, out_white, (gamma * 100.0).round() as i16); + (block.key, block.data) } pub fn invert_block() -> ([u8; 4], Vec) {