psd: add resource writers for alpha names and clipping path (2999) (#2429)

This commit is contained in:
Dylan M. Taylor
2026-11-19 16:25:53 -05:00
committed by GitHub
parent dc6adc3fbd
commit 9d5c9cce69
4 changed files with 83 additions and 0 deletions
+19
View File
@@ -68,6 +68,19 @@ pub fn alternate_spot_colors(data: &[u8]) -> impl ExactSizeIterator<Item = Alter
})
}
/// Encode unpadded legacy Pascal channel names (resource 1006).
///
/// Each name is encoded with [`crate::resources::encode_legacy_name`] (printable ASCII, max 255
/// bytes) and prefixed with its 1-byte length without per-string padding.
pub fn pascal_names<'a>(names: impl IntoIterator<Item = &'a str>) -> Vec<u8> {
let mut out = Vec::new();
for name in names {
let legacy = crate::io::encode_legacy_name(name);
crate::io::write_pascal(&mut out, &legacy, 1);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
@@ -108,4 +121,10 @@ mod tests {
bytes[2..4].copy_from_slice(&0u16.to_be_bytes());
assert_eq!(alternate_spot_colors(&bytes).len(), 0);
}
#[test]
fn pascal_names_encodes_unpadded_legacy_strings() {
assert_eq!(pascal_names(["Spot Color 1"]), b"\x0cSpot Color 1");
assert_eq!(pascal_names(["café", "A\nB"]), b"\x04caf?\x03A?B");
}
}
+27
View File
@@ -252,6 +252,24 @@ impl VectorMaskBlock {
}
}
/// Extract the raw Pascal-string path name bytes from a Clipping Path Name resource (2999).
///
/// Returns `None` if the resource is empty or truncated before the declared name length.
pub fn clipping_path_name(data: &[u8]) -> Option<&[u8]> {
let len = usize::from(*data.first()?);
data.get(1..1 + len)
}
/// Encode a Clipping Path Name resource (2999): legacy Pascal name followed by 16.16 unsigned
/// fixed-point flatness.
pub fn clipping_path_resource(name: &str, flatness: f32) -> Vec<u8> {
let legacy = crate::io::encode_legacy_name(name);
let mut out = Vec::with_capacity(1 + legacy.len() + 4);
crate::io::write_pascal(&mut out, &legacy, 1);
out.extend_from_slice(&((flatness.max(0.0) * 65536.0) as u32).to_be_bytes());
out
}
#[cfg(test)]
mod tests {
use super::*;
@@ -339,4 +357,13 @@ mod tests {
assert_eq!(s.len(), 2);
assert_eq!(s[0].knots.len(), 3);
}
#[test]
fn clipping_path_resource_roundtrip_and_truncation() {
let bytes = clipping_path_resource("Outline", 1.5);
assert_eq!(bytes, b"\x07Outline\x00\x01\x80\x00");
assert_eq!(clipping_path_name(&bytes), Some(b"Outline".as_slice()));
assert_eq!(clipping_path_name(&[]), None);
assert_eq!(clipping_path_name(&[5, b'a', b'b']), None);
}
}
+14
View File
@@ -8,10 +8,14 @@ use crate::io::{Reader, WriteExt, write_pascal};
pub mod ids {
/// Resolution info (`ResolutionInfo` structure).
pub const RESOLUTION_INFO: u16 = 1005;
/// Alpha channel names as unpadded Pascal strings.
pub const ALPHA_NAMES: u16 = 1006;
/// Target layer index.
pub const LAYER_STATE: u16 = 1024;
/// Layer group ids (one u16 per layer).
pub const LAYER_GROUP_INFO: u16 = 1026;
/// Grid and guides (legacy v1).
pub const GUIDES: u16 = 1032;
/// Legacy (Photoshop 4) thumbnail, BGR.
pub const THUMBNAIL_PS4: u16 = 1033;
/// Thumbnail (JFIF).
@@ -20,6 +24,8 @@ pub mod ids {
pub const GLOBAL_ANGLE: u16 = 1037;
/// ICC profile.
pub const ICC_PROFILE: u16 = 1039;
/// Unicode alpha channel names.
pub const UNICODE_ALPHA_NAMES: u16 = 1045;
/// Global altitude.
pub const GLOBAL_ALTITUDE: u16 = 1049;
/// Version info (has real merged data flag).
@@ -32,6 +38,14 @@ pub mod ids {
pub const TIMELINE_INFO: u16 = 1075;
/// Selected layer ids (`u16` count followed by `u32` ids).
pub const LAYER_SELECTION_IDS: u16 = 1069;
/// Clipping path name and flatness.
pub const CLIPPING_PATH: u16 = 2999;
}
/// Encode a name for a legacy Pascal field: printable ASCII up to 255 bytes, with other
/// characters replaced by `?`.
pub fn encode_legacy_name(s: &str) -> Vec<u8> {
crate::io::encode_legacy_name(s)
}
/// One image resource block.
+23
View File
@@ -24,6 +24,20 @@ pub fn records(mut data: &[u8]) -> impl Iterator<Item = &[u8]> {
})
}
/// Encode counted NUL-terminated big-endian UTF-16 name records (resource 1045).
pub fn write<'a>(names: impl IntoIterator<Item = &'a str>) -> Vec<u8> {
let mut out = Vec::new();
for name in names {
let count = name.encode_utf16().count().saturating_add(1);
let count_u32 = u32::try_from(count).unwrap_or(u32::MAX);
out.extend_from_slice(&count_u32.to_be_bytes());
for unit in name.encode_utf16().take(count_u32 as usize - 1).chain(std::iter::once(0)) {
out.extend_from_slice(&unit.to_be_bytes());
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
@@ -51,4 +65,13 @@ mod tests {
assert_eq!(records(&first[..end]).count(), 0);
}
}
#[test]
fn write_roundtrips_with_nul_terminator() {
let bytes = write(["Alpha 1", "café"]);
let decoded: Vec<_> = records(&bytes).collect();
assert_eq!(decoded.len(), 2);
assert_eq!(decoded[0], [0, b'A', 0, b'l', 0, b'p', 0, b'h', 0, b'a', 0, b' ', 0, b'1', 0, 0]);
assert_eq!(decoded[1], [0, b'c', 0, b'a', 0, b'f', 0, 0xe9, 0, 0]);
}
}