Merge pull request #15210 from storytold/cutover/psd-channel-resources

io: use shared PSD DisplayInfo and alternate spot records
This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 08:09:16 +01:00
commit ca014193a3
3 files changed
+30 -32

No files matched your search

Generated
+12
View File
@@ -1639,6 +1639,17 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-psd"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=5e2bc54a48c9f03e7ce4eca42775ce5cc051f5d6#5e2bc54a48c9f03e7ce4eca42775ce5cc051f5d6"
dependencies = [
"encoding_rs",
"flate2",
"rayon",
"thiserror 2.0.21",
]
[[package]]
name = "craft-psd"
version = "0.1.0"
@@ -5753,6 +5764,7 @@ dependencies = [
"craft-pdn",
"craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f)",
"craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=589842f0da5c5a11e71f39b70092dcb918b92f39)",
"craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=5e2bc54a48c9f03e7ce4eca42775ce5cc051f5d6)",
"craft-psd 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b9c22554ce0fd2caccdb53b417082a2ff2ef178f)",
"craft-svg",
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ad8faa1c136ce289e9f132c9137d0cd913bd4ef6)",
+1
View File
@@ -29,6 +29,7 @@ photocraft-doc = { workspace = true }
photocraft-compose = { workspace = true }
photocraft-algo = { workspace = true }
craft-psd = { workspace = true }
craft-psd-channels = { package = "craft-psd", git = "https://github.com/storytold/craft-libs", rev = "5e2bc54a48c9f03e7ce4eca42775ce5cc051f5d6", version = "0.1.0" }
craft-psd-duotone = { package = "craft-psd", version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "b9c22554ce0fd2caccdb53b417082a2ff2ef178f" }
craft-psd-guides = { package = "craft-psd", version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "589842f0da5c5a11e71f39b70092dcb918b92f39" }
photocraft-codecs = { workspace = true }
+17 -32
View File
@@ -59,34 +59,30 @@ pub(crate) fn display_info(channels: &[&AlphaChannel], transparency: bool) -> Ve
None => (c.color, c.opacity, if c.indicates == ColorIndicates::SelectedAreas { 0 } else { 1 }),
};
let rgb = color.to_rgb();
out.extend_from_slice(&SPACE_RGB.to_be_bytes());
for v in [rgb[0], rgb[1], rgb[2], 0.0] {
out.extend_from_slice(&((v.clamp(0.0, 1.0) * 65535.0).round() as u16).to_be_bytes());
}
out.extend_from_slice(&((opacity.clamp(0.0, 1.0) * 100.0).round() as u16).to_be_bytes());
out.push(kind);
out.extend_from_slice(
&craft_psd_channels::channel_resources::DisplayInfo {
space: SPACE_RGB,
components: [rgb[0], rgb[1], rgb[2], 0.0].map(|v| (v.clamp(0.0, 1.0) * 65535.0).round() as u16),
opacity: (opacity.clamp(0.0, 1.0) * 100.0).round() as u16,
kind,
}
.to_bytes(),
);
}
out
}
/// The records of a DisplayInfo resource (`versioned` = 1077: 13-byte records after a version;
/// else the older 1007 with 14-byte records). A trailing partial record is left out.
fn display_records(data: &[u8], versioned: bool) -> std::slice::ChunksExact<'_, u8> {
let (body, size) = if versioned { (data.get(4..).unwrap_or(&[]), 13) } else { (data, 14) };
body.chunks_exact(size)
}
/// Which of the extra channels are spot channels (kind 2), after skipping `skip` records (the
/// merged transparency channel's), in the same records [`apply_display_info`] reads. A channel
/// without a record is not a spot.
pub(crate) fn spot_flags(data: &[u8], versioned: bool, skip: usize) -> Vec<bool> {
display_records(data, versioned).skip(skip).map(|rec| rec.get(12) == Some(&2)).collect()
craft_psd_channels::channel_resources::display_info(data, versioned).skip(skip).map(|rec| rec.kind == 2).collect()
}
/// The colour space of each extra channel's DisplayInfo record, after skipping `skip` records,
/// in the same records [`apply_display_info`] reads.
pub(crate) fn display_spaces(data: &[u8], versioned: bool, skip: usize) -> Vec<u16> {
display_records(data, versioned).skip(skip).map(|rec| u16::from_be_bytes([rec[0], rec[1]])).collect()
craft_psd_channels::channel_resources::display_info(data, versioned).skip(skip).map(|rec| rec.space).collect()
}
/// The spot colours of an Alternate Spot Colors resource (1067) by channel identifier, those in
@@ -94,17 +90,8 @@ pub(crate) fn display_spaces(data: &[u8], versioned: bool, skip: usize) -> Vec<u
/// DisplayInfo colour in a space it does not document (psd-tools cmyk-alpha-spot.psd: 0x2331)
/// and its ink here, in Lab.
pub(crate) fn alternate_spot_colors(data: &[u8]) -> Vec<(u32, Color)> {
let count = data.get(2..4).map_or(0, |c| usize::from(u16::from_be_bytes([c[0], c[1]])));
let body = data.get(4..).unwrap_or_default();
body.as_chunks::<14>()
.0
.iter()
.take(count)
.filter_map(|rec| {
let u = |i: usize| u16::from_be_bytes([rec[i], rec[i + 1]]);
let id = u32::from_be_bytes([rec[0], rec[1], rec[2], rec[3]]);
decode_known_color(u(4), [u(6), u(8), u(10), u(12)]).map(|c| (id, c))
})
craft_psd_channels::channel_resources::alternate_spot_colors(data)
.filter_map(|rec| decode_known_color(rec.space, rec.components).map(|c| (rec.id, c)))
.collect()
}
@@ -120,12 +107,10 @@ pub(crate) fn display_info_marks_spot(data: &[u8]) -> bool {
/// older 1007 with 14-byte records) to the imported channels in order, after skipping `skip`
/// records (the merged transparency channel's).
pub(crate) fn apply_display_info(data: &[u8], versioned: bool, skip: usize, channels: &mut [AlphaChannel]) {
for (c, rec) in channels.iter_mut().zip(display_records(data, versioned).skip(skip)) {
let u = |i: usize| u16::from_be_bytes([rec[i], rec[i + 1]]);
let comps = [u(2), u(4), u(6), u(8)];
let color = decode_color(u(0), comps);
let opacity = f32::from(u(10).min(100)) / 100.0;
match rec[12] {
for (c, rec) in channels.iter_mut().zip(craft_psd_channels::channel_resources::display_info(data, versioned).skip(skip)) {
let color = decode_color(rec.space, rec.components);
let opacity = f32::from(rec.opacity.min(100)) / 100.0;
match rec.kind {
2 => c.spot = Some((color, opacity)),
k => {
// An alpha channel, not an ink (a Multichannel import starts every channel as one).