Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

CMYK ink compositing: keep layers without an alpha channel

SBaksa47ff3a99bff5
diff --git a/crates/compose/src/native.rs b/crates/compose/src/native.rs
index 31e17f3ea..f83784292 100644
--- a/crates/compose/src/native.rs
+++ b/crates/compose/src/native.rs
@@ -189,7 +189,10 @@ impl<'a> InkStack<'a> {
match item {
Item::Layer(surf, opacity, mask) => {
let opacity = *opacity;
- if opacity <= 0.0 || (surf.content_bounds().intersect(&rect).is_empty() && surf.default_pixel().iter().all(|v| *v == 0.0)) {
+ // Nothing to draw only where the surface is transparent: without an alpha
+ // channel its default pixel is opaque (no ink is blank paper, not nothing).
+ let clear = surf.format().alpha && surf.default_pixel().last().is_some_and(|a| *a <= 0.0);
+ if opacity <= 0.0 || (clear && surf.content_bounds().intersect(&rect).is_empty()) {
continue;
}
let sf = surf.format();
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index ddd487154..bad0c50e7 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -742,6 +742,19 @@ fn cmyk_ink_compositing_inlines_open_groups() {
assert!(close4(px(&d, 3, 1), [c[0], c[1], c[2], 1.0]), "outside the group's layer");
}
+/// A CMYK layer without an alpha channel is opaque everywhere, its untouched pixels blank paper
+/// (no ink): the ink compositor keeps it under an adjustment rather than skipping it as empty.
+#[test]
+fn cmyk_ink_compositing_keeps_blank_paper_without_alpha() {
+ let mut d = Document::new("paper", Size::new(4, 4), ColorMode::Cmyk, SampleType::U8);
+ d.layers.push(Layer::raster("paper", PixelFormat::new(ColorMode::Cmyk, SampleType::U8, false)));
+ d.layers.push(Layer::new("t", LayerContent::Adjustment(Adjustment::Threshold { level: 0.5 })));
+ assert!(native::render_cmyk(&d, d.bounds(), 64).is_some());
+ // Paper is white, so Threshold keeps it paper.
+ let white = photocraft_color::convert::cmyk_to_rgb([0.0; 4]);
+ assert!(close4(px(&d, 2, 2), [white[0], white[1], white[2], 1.0]), "{:?}", px(&d, 2, 2));
+}
+
#[test]
fn cmyk_document_renders_via_rgb() {
let mut d = Document::new("c", Size::new(2, 2), ColorMode::Cmyk, SampleType::U8);