Share raw PSD Duotone ink record layout

Extract the fixed layout from PhotoCraft 772cf39c09930229e11b920196f486239bbbfab8, incorporating alternate-colour handling from 2d7c62055ee47497f8cb77bbdfd3befa29718f4a. Keep document colour/curve policy out of the codec.

Co-authored-by: David Mann <daaavid.mann@gmail.com>
This commit is contained in:
Gert PellinandDavid Mann authored and Dylan M. Taylor committed 2026-11-15 13:28:14 -05:00
1 parent 8bd1412eaa
commit 689eff839d
2 files changed
+160

No files matched your search

+159
View File
@@ -0,0 +1,159 @@
//! Raw records in Photoshop's version 1 Duotone Color Mode Data.
//!
//! Layout extracted from PhotoCraft `duotone_map.rs`, originally by Gert Pellin
//! (772cf39c09930229e11b920196f486239bbbfab8), with colour-book integration by David Mann.
//! Colour conversion, curve sampling and fallback/display policy are caller-owned.
//! Parsing accepts the 412-byte record prefix as PhotoCraft did; dot gain and overprints
//! are optional trailing data, not prerequisites for interpreting the inks.
/// Full block size emitted by Photoshop and PhotoCraft.
pub const BLOCK_LEN: usize = 524;
/// Alternate Duotone Colors image resource.
pub const ALTERNATE_DUOTONE_COLORS: u16 = 1066;
/// Raw colour space and four component words, without colour interpretation.
pub type ColorRecord = [u16; 5];
/// Sentinel for an absent ink or computed overprint colour.
pub const NO_COLOR: ColorRecord = [0xffff, 0, 0, 0, 0];
/// One fixed-size ink slot, including uninterpreted reserved curve word.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InkRecord {
pub color: ColorRecord,
/// Pascal name bytes, including its length byte; text encoding belongs to the caller.
pub name: [u8; 64],
/// Thirteen signed curve samples and the reserved final word.
pub curve: [i16; 14],
}
fn word(data: &[u8], at: usize) -> Option<u16> {
let bytes = data.get(at..at.checked_add(2)?)?;
Some(u16::from_be_bytes([bytes[0], bytes[1]]))
}
/// Decode a raw colour record at a byte offset, without guessing unknown colour spaces.
pub fn color_at(data: &[u8], at: usize) -> Option<ColorRecord> {
let bytes = data.get(at..at.checked_add(10)?)?;
Some(std::array::from_fn(|i| u16::from_be_bytes([bytes[i * 2], bytes[i * 2 + 1]])))
}
/// Borrowed, validated view over at most four active ink records. No input-sized allocation.
#[derive(Clone, Copy, Debug)]
pub struct DuotoneView<'a> {
data: &'a [u8],
count: usize,
}
impl<'a> DuotoneView<'a> {
/// Accept version 1, one to four inks and the full 412-byte record prefix.
pub fn parse(data: &'a [u8]) -> Option<Self> {
if data.len() < 412 || word(data, 0)? != 1 {
return None;
}
let count = usize::from(word(data, 2)?);
(1..=4).contains(&count).then_some(Self { data, count })
}
pub fn ink_count(self) -> usize {
self.count
}
/// Copy one small fixed-size record. Inactive/out-of-range slots are not returned.
pub fn ink(self, index: usize) -> Option<InkRecord> {
if index >= self.count {
return None;
}
let name_at = 44 + 64 * index;
let name = self.data.get(name_at..name_at + 64)?.try_into().ok()?;
let curve_at = 300 + 28 * index;
let curve_bytes = self.data.get(curve_at..curve_at + 28)?;
Some(InkRecord {
color: color_at(self.data, 4 + 10 * index)?,
name,
curve: std::array::from_fn(|i| i16::from_be_bytes([curve_bytes[i * 2], curve_bytes[i * 2 + 1]])),
})
}
}
/// Read one raw alternate colour when the resource version/count permits it.
pub fn alternate_color(data: &[u8], index: usize) -> Option<ColorRecord> {
if word(data, 0)? != 1 || index >= usize::from(word(data, 2)?) {
return None;
}
color_at(data, 4usize.checked_add(index.checked_mul(10)?)?)
}
/// Serialize four raw slots and explicit trailing policy into a fixed-size block.
/// `count` must be 1..=4. Reserved bytes and inactive slots are preserved as supplied.
pub fn write_block(count: u16, inks: &[InkRecord; 4], dot_gain: u16, overprints: &[ColorRecord; 11]) -> Option<[u8; BLOCK_LEN]> {
if !(1..=4).contains(&count) {
return None;
}
let mut out = [0u8; BLOCK_LEN];
out[..2].copy_from_slice(&1u16.to_be_bytes());
out[2..4].copy_from_slice(&count.to_be_bytes());
for (i, ink) in inks.iter().enumerate() {
for (j, value) in ink.color.iter().enumerate() {
let at = 4 + 10 * i + 2 * j;
out[at..at + 2].copy_from_slice(&value.to_be_bytes());
}
out[44 + 64 * i..44 + 64 * (i + 1)].copy_from_slice(&ink.name);
for (j, value) in ink.curve.iter().enumerate() {
let at = 300 + 28 * i + 2 * j;
out[at..at + 2].copy_from_slice(&value.to_be_bytes());
}
}
out[412..414].copy_from_slice(&dot_gain.to_be_bytes());
for (i, color) in overprints.iter().enumerate() {
for (j, value) in color.iter().enumerate() {
let at = 414 + 10 * i + 2 * j;
out[at..at + 2].copy_from_slice(&value.to_be_bytes());
}
}
Some(out)
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn fixed_layout_roundtrip_preserves_raw_fields() {
let mut ink = InkRecord { color: [7, 1, 2, 3, 65535], name: [0; 64], curve: [-1; 14] };
ink.name[..4].copy_from_slice(&[3, b'I', b'n', b'k']);
ink.curve[0] = 0;
ink.curve[12] = 1000;
let slots = std::array::from_fn(|_| ink.clone());
let bytes = write_block(4, &slots, 20, &[NO_COLOR; 11]).unwrap();
assert_eq!(&bytes[..4], &[0, 1, 0, 4]);
assert_eq!(&bytes[44..48], &[3, b'I', b'n', b'k']);
assert_eq!(&bytes[412..416], &[0, 20, 255, 255]);
let view = DuotoneView::parse(&bytes);
assert_eq!(view.map(DuotoneView::ink_count), Some(4));
for i in 0..4 {
assert_eq!(view.and_then(|v| v.ink(i)), Some(ink.clone()));
}
assert!(view.and_then(|v| v.ink(usize::MAX)).is_none());
for n in 0..412 {
assert!(DuotoneView::parse(&bytes[..n]).is_none());
}
assert!(DuotoneView::parse(&bytes[..412]).is_some());
assert!(write_block(0, &slots, 20, &[NO_COLOR; 11]).is_none());
assert!(write_block(5, &slots, 20, &[NO_COLOR; 11]).is_none());
}
#[test]
fn rejects_hostile_offsets_versions_counts_and_alternate_truncation() {
let mut bytes = [0u8; BLOCK_LEN];
bytes[..4].copy_from_slice(&[0, 1, 255, 255]);
assert!(DuotoneView::parse(&bytes).is_none());
bytes[..4].copy_from_slice(&[0, 2, 0, 1]);
assert!(DuotoneView::parse(&bytes).is_none());
assert!(color_at(&bytes, usize::MAX).is_none());
let alt = [0, 1, 0, 1, 0, 7, 0, 1, 0, 2, 0, 3, 0, 4];
assert_eq!(alternate_color(&alt, 0), Some([7, 1, 2, 3, 4]));
assert!(alternate_color(&alt[..13], 0).is_none());
assert!(alternate_color(&alt, 1).is_none());
assert!(alternate_color(&alt, usize::MAX).is_none());
}
}
+1
View File
@@ -65,6 +65,7 @@ pub mod compression;
pub mod csh;
pub mod descriptor;
pub mod displace_map;
pub mod duotone;
pub mod engine_data;
pub mod error;
pub mod file;