PhotoCraft: An open-source, photo editor written by scratch in pure Rust
PSD export: CMYK merged image mixed on ink values
SBaksad500c0361cef
diff --git a/crates/io/src/psd_export.rs b/crates/io/src/psd_export.rsindex b872019c9..d861022e6 100644--- a/crates/io/src/psd_export.rs+++ b/crates/io/src/psd_export.rs@@ -1076,6 +1076,9 @@ fn cmyk_of(fmt: &PixelFormat) -> bool {/// The merged image's planes (colour channels, then alpha), big-endian, matted against white/// when `matte`; with whether any pixel needs the alpha channel and whether any is below 1.fn merged_planes(doc: &Document, fmt: &PixelFormat, cmyk: bool, matte: bool) -> (Vec<u8>, bool, bool) {+ if cmyk && let Some(planes) = merged_ink_planes(doc, fmt.sample, matte) {+ return planes;+ }let sample = fmt.sample;let cc = fmt.mode.color_channels();let bps = sample.bytes();@@ -1128,6 +1131,51 @@ fn merged_planes(doc: &Document, fmt: &PixelFormat, cmyk: bool, matte: bool) ->(planes, has_alpha, translucent)}+/// [`merged_planes`] of a CMYK document whose layers are mixed on their ink values+/// (`photocraft_compose::cmyk`), so opaque pixels keep their exact inks; `None` for documents+/// that composite in RGB.+fn merged_ink_planes(doc: &Document, sample: SampleType, matte: bool) -> Option<(Vec<u8>, bool, bool)> {+ let bps = sample.bytes();+ let n = doc.size.area() as usize;+ let plane = n * bps;+ let mut planes = vec![0u8; plane * 5];+ let (mut has_alpha, mut translucent) = (false, false);+ let canvas = doc.bounds();+ let w = canvas.width() as usize;+ let done = photocraft_compose::cmyk::render_cmyk_bands(doc, canvas, 0, |band| -> Result<(), ()> {+ // Retain alpha whenever it differs from opaque at the stored precision.+ has_alpha |= band.px.iter().any(|p| match sample {+ SampleType::U8 => q255(p[4]) < 255,+ SampleType::U16 => (p[4].clamp(0.0, 1.0) * 65535.0 + 0.5) as u16 != u16::MAX,+ SampleType::F32 => p[4] != 1.0,+ });+ translucent |= band.px.iter().any(|p| p[4] < 1.0);+ let start = (band.rect.y0 - canvas.y0) as usize * w;+ let parts = crate::pixels::par_map(crate::pixels::bands(band.px.len()), |range| {+ let mut out: Vec<Vec<u8>> = (0..5).map(|_| Vec::with_capacity(range.len() * bps)).collect();+ for p in band.px.get(range.clone()).unwrap_or(&[]) {+ for (c, o) in out.iter_mut().enumerate() {+ // Inks matted against white paper (no ink) like Photoshop (see+ // `pixels::matte`), and stored inverted as PSD's CMYK is.+ let v = if c < 4 { 1.0 - if matte { crate::pixels::matte(p[c], p[4], 0.0) } else { p[c] } } else { p[4] };+ encode_be(v, sample, o);+ }+ }+ (range.start, out)+ });+ for (at, part) in parts {+ for (c, src) in part.iter().enumerate() {+ let o = c * plane + (start + at) * bps;+ if let Some(dst) = planes.get_mut(o..o + src.len()) {+ dst.copy_from_slice(src);+ }+ }+ }+ Ok(())+ })?;+ done.ok().map(|()| (planes, has_alpha, translucent))+}+/// Converts a document to a PSD file model (see [`document_to_psd_with`]).pub fn document_to_psd(doc: &Document) -> PsdFile {document_to_psd_with(doc, &PsdExportOptions::default()).0diff --git a/crates/io/tests/cmyk_export.rs b/crates/io/tests/cmyk_export.rsindex 67d5ed77e..6d0b0c88a 100644--- a/crates/io/tests/cmyk_export.rs+++ b/crates/io/tests/cmyk_export.rs@@ -213,3 +213,25 @@ fn layer_effects_export_their_inks() {assert_eq!(pixel(&img, 3, 3), vec![0.0, 0.0, 0.0, 1.0], "type");}+#[test]+fn psd_merged_image_keeps_exact_inks() {+ // The PSD's merged image (what other apps show and print) is mixed on the inks too: K-only+ // black under a Multiply layer stays K-only, and the four-colour patch keeps its inks.+ let mut d = layered(SampleType::U8);+ let mut m = fill(SampleType::U8, true, &[(Rect::new(0, 0, 12, 8), [0.0, 0.4, 0.0, 0.0, 1.0])]);+ m.blend = BlendMode::Multiply;+ d.layers.push(m);+ let r = export(&d, "a.psd", &ExportOptions::default()).unwrap_or_else(|e| panic!("{e}"));+ let f = photocraft_psd::PsdFile::from_bytes(&r.bytes).unwrap_or_else(|e| panic!("{e}"));+ let planes = f.decode_merged().unwrap_or_else(|e| panic!("{e}"));+ let n = 12 * 8;+ // Planar, inverted (255 = no ink).+ let ink = |c: usize, x: usize, y: usize| 255 - i32::from(planes.get(c * n + y * 12 + x).copied().unwrap_or(0));+ let q = |v: f32| (v * 255.0).round() as i32;+ assert_eq!([ink(0, 2, 3), ink(1, 2, 3), ink(2, 2, 3), ink(3, 2, 3)], [0, q(0.4), 0, 255], "K-only black with magenta multiplied in");+ let mul = |b: f32, s: f32| b + s - b * s;+ let qb = |v: f32| ((v * 255.0).round()) / 255.0;+ let want = [q(0.2), q(mul(qb(0.4), qb(0.4))), q(0.6), q(0.1)];+ let got = [ink(0, 8, 1), ink(1, 8, 1), ink(2, 8, 1), ink(3, 8, 1)];+ assert!(got.iter().zip(want).all(|(g, w)| (g - w).abs() <= 1), "{got:?} vs {want:?}");+}