PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Flatten Image, Stamp Visible and Copy Merged keep a CMYK document's inks
SBaksa06341463831f
diff --git a/crates/compose/src/cmyk_tests.rs b/crates/compose/src/cmyk_tests.rsindex 0b73c2530..de07424dd 100644--- a/crates/compose/src/cmyk_tests.rs+++ b/crates/compose/src/cmyk_tests.rs@@ -1613,3 +1613,22 @@ fn fills_agree_on_both_routes() {assert_routes_agree(&d);}}++/// Flatten Image, Stamp Visible and Copy Merged (all [`crate::flatten_to_surface`]) keep a CMYK+/// document's inks: K-only black stays K-only instead of turning four-colour.+#[test]+fn flattening_keeps_exact_inks() {+ let mut d = doc(3, 1);+ d.layers.push(layer(&[+ (Rect::new(0, 0, 1, 1), [0.0, 0.0, 0.0, 1.0, 1.0]),+ (Rect::new(1, 0, 2, 1), [0.2, 0.4, 0.6, 0.1, 1.0]),+ (Rect::new(2, 0, 3, 1), [0.0, 0.0, 0.0, 1.0, 0.4]),+ ]));+ // Over white paper (Flatten Image) and kept transparent (Stamp Visible).+ let flat = crate::flatten_to_surface(&d, CMYK8, Some([1.0; 3]));+ assert_eq!(flat.pixel(0, 0), vec![0.0, 0.0, 0.0, 1.0]);+ assert_eq!(flat.pixel(1, 0), vec![q(0.2), q(0.4), q(0.6), q(0.1)]);+ assert_eq!(flat.pixel(2, 0), vec![0.0, 0.0, 0.0, q(0.4)], "40% K over paper");+ let stamp = crate::flatten_to_surface(&d, CMYKA8, None);+ assert_eq!(stamp.pixel(2, 0), vec![0.0, 0.0, 0.0, 1.0, q(0.4)]);+}diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rsindex c9419e82b..7ab47a39d 100644--- a/crates/compose/src/lib.rs+++ b/crates/compose/src/lib.rs@@ -232,11 +232,40 @@ pub fn band_rows_for(width: u32, requested: i32) -> i32 {/// The document's composite as a pixel surface in `fmt` (straight RGBA converted to its model/// and depth), optionally flattened over an opaque `background`; rendered and written in bands,-/// so no full-size float composite is held. Tiles equal to the default pixel are pruned.+/// so no full-size float composite is held. Tiles equal to the default pixel are pruned. A CMYK+/// document flattened to CMYK keeps its inks where its layers mix on them ([`cmyk`]); otherwise+/// its composite goes back to CMYK through its own profile, the one its layers were read through.pub fn flatten_to_surface(doc: &Document, fmt: photocraft_color::PixelFormat, background: Option<[f32; 3]>) -> Surface {let mut s = Surface::new(fmt);let n = fmt.channels();let mut data = Vec::new();+ let space = cmyk_space(doc);+ let to_cmyk = fmt.mode == photocraft_color::ColorMode::Cmyk && doc.mode == photocraft_color::ColorMode::Cmyk;+ if to_cmyk && cmyk::renders_inks(doc) {+ // White paper is no ink; another background colour goes through the profile.+ let paper = background.map(|bg| {+ if bg == [1.0; 3] { [0.0; 4] } else { photocraft_color::convert::with_cmyk_space(space.as_ref(), || photocraft_color::convert::rgb_to_cmyk(bg)) }+ });+ let _ = cmyk::render_cmyk_bands(doc, doc.bounds(), 0, |band| -> Result<(), ()> {+ data.clear();+ data.reserve(band.px.len() * n);+ for p in &band.px {+ // Over the paper (fully covered pixels keep their inks exactly), or straight.+ let ink = |c: usize| match paper {+ Some(bg) if p[4] < 1.0 => p[c] * p[4] + bg[c] * (1.0 - p[4]),+ _ => p[c],+ };+ data.extend((0..4).map(ink));+ if fmt.alpha {+ data.push(if paper.is_some() { 1.0 } else { p[4] });+ }+ }+ s.write_region(band.rect, &data);+ Ok(())+ });+ s.prune();+ return s;+ }let _ = render_bands(doc, doc.bounds(), 0, |band| -> Result<(), ()> {let band = match background {Some(bg) => band.over_background(bg),@@ -244,9 +273,11 @@ pub fn flatten_to_surface(doc: &Document, fmt: photocraft_color::PixelFormat, ba};data.clear();data.resize(band.px.len() * n, 0.0);- for (p, out) in band.px.iter().zip(data.chunks_exact_mut(n)) {- photocraft_raster::from_rgba_into(&fmt, *p, out);- }+ photocraft_color::convert::with_cmyk_space(space.as_ref(), || {+ for (p, out) in band.px.iter().zip(data.chunks_exact_mut(n)) {+ photocraft_raster::from_rgba_into(&fmt, *p, out);+ }+ });s.write_region(band.rect, &data);Ok(())});