Skip to content

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

CMYK compositing: type, shape, smart object and fill layers on their ink values

SBaksa46c78f82fbdd
diff --git a/crates/compose/src/cmyk.rs b/crates/compose/src/cmyk.rs
index c97b27d4e..281ff7229 100644
--- a/crates/compose/src/cmyk.rs
+++ b/crates/compose/src/cmyk.rs
@@ -3,10 +3,13 @@
//! and back through the profile.
//!
//! This path covers the layer stacks whose blending is the same in every colour model: Normal
-//! pixel layers with opacity, fill opacity and masks, isolated and pass-through groups, and
-//! clipping groups. [`supports`] says whether a document stays on it; anything else (type,
-//! shapes, fills, effects, adjustments, other blend modes, knockouts, channel restrictions,
-//! Blend If) is left to the RGB compositor, whose result callers convert back to CMYK.
+//! layers with opacity, fill opacity and masks (pixel, type, shape and smart object layers
+//! through their rendered pixels, solid colour fills, and fills carrying their rendered pixels),
+//! isolated and pass-through groups, and clipping groups. Type layers mix their edges in the
+//! compositor's text gamma, on brightness. [`supports`] says whether a document stays on it;
+//! anything else (gradient and pattern fills without rendered pixels, effects, adjustments,
+//! other blend modes, knockouts, channel restrictions, Blend If) is left to the RGB compositor,
+//! whose result callers convert back to CMYK.
//!
//! Buffers hold straight-alpha `[C, M, Y, K, A]` in 0..1 ink (0 = no ink).
@@ -74,8 +77,22 @@ fn plain(l: &Layer) -> bool {
&& l.blend_if.is_default()
}
-fn cmyk_raster(l: &Layer) -> bool {
- matches!(&l.content, LayerContent::Raster(s) if s.format().mode == ColorMode::Cmyk)
+/// A layer whose own pixels this path reads in CMYK: pixel layers, and type, shape and smart
+/// object layers through their rendered pixels (rendered in the document's model, so a colour
+/// given in CMYK is drawn with exactly those inks); solid colour fills, and fills carrying the
+/// pixels they were rendered to. Gradient and pattern fills without those pixels aren't.
+fn cmyk_pixels(l: &Layer) -> bool {
+ let cmyk = |s: &photocraft_raster::Surface| s.format().mode == ColorMode::Cmyk;
+ match &l.content {
+ LayerContent::Raster(s) => cmyk(s),
+ LayerContent::Text(_) | LayerContent::Shape(_) | LayerContent::Smart(_) => l.surface().is_none_or(cmyk),
+ LayerContent::Fill(f) => match &l.fill_cache {
+ Some(c) if c.fill == *f => cmyk(&c.surface),
+ // Solid colours and gradients render natively in CMYK; patterns don't.
+ _ => !matches!(f, photocraft_doc::Fill::Pattern { .. }),
+ },
+ _ => false,
+ }
}
fn layer_supported(l: &Layer, clipped: &[Layer], depth: u32) -> bool {
@@ -83,7 +100,12 @@ fn layer_supported(l: &Layer, clipped: &[Layer], depth: u32) -> bool {
return false;
}
let content = match &l.content {
- LayerContent::Raster(_) => cmyk_raster(l) && l.blend == BlendMode::Normal,
+ // A stroked shape with clipped layers draws its stroke above them (the compositor's
+ // split path), which this path doesn't do.
+ LayerContent::Shape(sh) if sh.stroke.is_some() && clipped.iter().any(|c| c.visible) => false,
+ LayerContent::Raster(_) | LayerContent::Text(_) | LayerContent::Shape(_) | LayerContent::Smart(_) | LayerContent::Fill(_) => {
+ cmyk_pixels(l) && l.blend == BlendMode::Normal
+ }
LayerContent::Group(g) => {
g.artboard.is_none()
&& matches!(l.blend, BlendMode::Normal | BlendMode::PassThrough)
@@ -93,7 +115,7 @@ fn layer_supported(l: &Layer, clipped: &[Layer], depth: u32) -> bool {
}
_ => false,
};
- content && clipped.iter().filter(|c| c.visible).all(|c| plain(c) && cmyk_raster(c) && c.blend == BlendMode::Normal)
+ content && clipped.iter().filter(|c| c.visible).all(|c| plain(c) && cmyk_pixels(c) && c.blend == BlendMode::Normal)
}
/// A group whose children composite straight into the backdrop (the compositor's rule).
@@ -126,10 +148,61 @@ fn clean(v: f32) -> f32 {
if v.is_finite() { v } else { 0.0 }
}
-/// A CMYK pixel layer's own pixels over `rect`, masks applied.
-fn raster_content(l: &Layer, rect: Rect, cx: &Ctx) -> CmykBuffer {
+/// [`normal`] with coverage mixed in the type-layer `gamma` space (1 = plain [`normal`]), as
+/// the compositor mixes type edges. The space is one of light, so inks are mixed as their
+/// brightness (1 − ink); fully covered pixels are the source's inks exactly.
+#[inline]
+pub fn normal_gamma(backdrop: Cmyka, source: Cmyka, opacity: f32, gamma: f32) -> Cmyka {
+ if gamma == 1.0 {
+ return normal(backdrop, source, opacity);
+ }
+ let ab = backdrop[4];
+ let as_ = source[4] * opacity;
+ if as_ <= 0.0 {
+ return backdrop;
+ }
+ let ao = as_ + ab * (1.0 - as_);
+ if ao <= 0.0 {
+ return [0.0; 5];
+ }
+ // Normal: the blended colour is the source's, so its weight joins the source's.
+ let (wb, ws) = ((1.0 - as_) * ab / ao, as_ / ao);
+ let g = |ink: f32| super::psblend::text_encode(1.0 - ink, gamma);
+ // A covered pixel, or a channel both sides agree on, is exactly that ink: the round trip
+ // through the gamma space would only add rounding (K-only type would gain a trace of CMY).
+ let c = |i: usize| {
+ if wb <= 0.0 || backdrop[i] == source[i] { source[i] } else { 1.0 - super::psblend::text_decode(wb * g(backdrop[i]) + ws * g(source[i]), gamma) }
+ };
+ [c(0), c(1), c(2), c(3), ao]
+}
+
+/// A layer's own CMYK pixels over `rect` (see [`cmyk_pixels`]), masks applied.
+fn layer_content(l: &Layer, rect: Rect, cx: &Ctx) -> CmykBuffer {
let mut out = CmykBuffer::transparent(rect);
- if let LayerContent::Raster(s) = &l.content {
+ let rendered;
+ let surface = match &l.content {
+ LayerContent::Fill(f) => match &l.fill_cache {
+ Some(c) if c.fill == *f => Some(&c.surface),
+ _ => {
+ // The compositor's own CMYK rendering of the fill, at the document's depth (a
+ // colour given in CMYK keeps its inks); a solid colour is one pixel, spread out.
+ let solid = matches!(f, photocraft_doc::Fill::Solid(_));
+ let at = if solid { Rect::new(0, 0, 1, 1) } else { rect };
+ rendered = super::gradient_fill::render_cmyk_fill(f, at, super::fill_frame(l, cx.canvas), cx.depth);
+ match &rendered {
+ Some(s) if solid => {
+ let p = s.pixel(0, 0);
+ let v = |i: usize| p.get(i).copied().map_or(0.0, clean);
+ out.px.fill([v(0), v(1), v(2), v(3), v(4)]);
+ None
+ }
+ r => r.as_ref(),
+ }
+ }
+ },
+ _ => l.surface(),
+ };
+ if let Some(s) = surface {
let ch = s.format().channels();
let alpha = s.format().alpha;
let mut v = Vec::new();
@@ -167,23 +240,24 @@ fn mask_at(m: &Option<Vec<f32>>, i: usize) -> f32 {
/// the base keeps its alpha.
fn composite_clipped(clipped: &[Layer], base: &mut CmykBuffer, cx: &Ctx) {
for c in clipped.iter().filter(|c| c.visible) {
- let content = raster_content(c, base.rect, cx);
+ let content = layer_content(c, base.rect, cx);
let opacity = c.opacity * c.fill_opacity;
+ let gamma = super::text_gamma(c);
for (p, s) in base.px.iter_mut().zip(&content.px) {
let alpha = p[4];
if alpha <= 0.0 {
continue;
}
- let r = normal([p[0], p[1], p[2], p[3], 1.0], *s, opacity);
+ let r = normal_gamma([p[0], p[1], p[2], p[3], 1.0], *s, opacity, gamma);
*p = [r[0], r[1], r[2], r[3], alpha];
}
}
}
-fn blend_into(backdrop: &mut CmykBuffer, src: &CmykBuffer, opacity: f32) {
+fn blend_into(backdrop: &mut CmykBuffer, src: &CmykBuffer, opacity: f32, gamma: f32) {
for (b, s) in backdrop.px.iter_mut().zip(&src.px) {
if s[4] > 0.0 {
- *b = normal(*b, *s, opacity);
+ *b = normal_gamma(*b, *s, opacity, gamma);
}
}
}
@@ -230,12 +304,12 @@ fn composite_layer(l: &Layer, clipped: &[Layer], backdrop: &mut CmykBuffer, cx:
composite_stack(&g.children, &mut content, cx);
apply_mask(l, &mut content, cx);
composite_clipped(clipped, &mut content, cx);
- blend_into(backdrop, &content, opacity);
+ blend_into(backdrop, &content, opacity, 1.0);
return;
}
- let mut content = raster_content(l, rect, cx);
+ let mut content = layer_content(l, rect, cx);
composite_clipped(clipped, &mut content, cx);
- blend_into(backdrop, &content, opacity);
+ blend_into(backdrop, &content, opacity, super::text_gamma(l));
}
fn render_tile(doc: &Document, rect: Rect, cx: &Ctx) -> CmykBuffer {
diff --git a/crates/compose/src/cmyk_tests.rs b/crates/compose/src/cmyk_tests.rs
index d9e25fded..089c0a424 100644
--- a/crates/compose/src/cmyk_tests.rs
+++ b/crates/compose/src/cmyk_tests.rs
@@ -71,12 +71,50 @@ fn ref_mask(l: &Layer, x: i32, y: i32) -> f32 {
}
fn ref_own(l: &Layer, x: i32, y: i32) -> Cmyka {
- let Some(s) = l.surface() else { return [0.0; 5] };
+ let s = match &l.content {
+ LayerContent::Fill(f) => match &l.fill_cache {
+ Some(c) if c.fill == *f => Some(&c.surface),
+ _ => {
+ // A solid colour's inks and alpha, stored at the (8-bit) document's levels.
+ let photocraft_doc::Fill::Solid(color) = f else { return [0.0; 5] };
+ let c = color.in_mode(ColorMode::Cmyk);
+ return [q(c.c[0]), q(c.c[1]), q(c.c[2]), q(c.c[3]), q(color.alpha) * ref_mask(l, x, y)];
+ }
+ },
+ _ => l.surface(),
+ };
+ let Some(s) = s else { return [0.0; 5] };
let p = s.pixel(x, y);
let a = if s.format().alpha { p[4] } else { 1.0 };
[p[0], p[1], p[2], p[3], a * ref_mask(l, x, y)]
}
+/// Source-over with coverage mixed in the type gamma, on brightness (1 − ink), worked out from
+/// the colour transfer functions directly.
+fn over_text(b: Cmyka, s: Cmyka, opacity: f32) -> Cmyka {
+ let gamma = crate::psblend::text_gamma();
+ let sa = s[4] * opacity;
+ if sa <= 0.0 {
+ return b;
+ }
+ let a = sa + b[4] * (1.0 - sa);
+ if a <= 0.0 {
+ return [0.0; 5];
+ }
+ let enc = |ink: f32| photocraft_color::convert::srgb_to_linear((1.0 - ink).max(0.0)).powf(1.0 / gamma);
+ let dec = |v: f32| 1.0 - photocraft_color::convert::linear_to_srgb(v.max(0.0).powf(gamma));
+ let mut o = [0.0; 5];
+ for i in 0..4 {
+ o[i] = dec((1.0 - sa) * b[4] / a * enc(b[i]) + sa / a * enc(s[i]));
+ }
+ o[4] = a;
+ o
+}
+
+fn over_layer(l: &Layer, b: Cmyka, s: Cmyka, opacity: f32) -> Cmyka {
+ if matches!(l.content, LayerContent::Text(_)) { over_text(b, s, opacity) } else { over(b, s, opacity) }
+}
+
fn ref_stack(layers: &[Layer], x: i32, y: i32, mut b: Cmyka) -> Cmyka {
let mut i = 0;
while i < layers.len() {
@@ -124,11 +162,11 @@ fn ref_layer(l: &Layer, clipped: &[Layer], x: i32, y: i32, b: Cmyka) -> Cmyka {
for c in clipped.iter().filter(|c| c.visible) {
if content[4] > 0.0 {
let a = content[4];
- let r = over([content[0], content[1], content[2], content[3], 1.0], ref_own(c, x, y), c.opacity * c.fill_opacity);
+ let r = over_layer(c, [content[0], content[1], content[2], content[3], 1.0], ref_own(c, x, y), c.opacity * c.fill_opacity);
content = [r[0], r[1], r[2], r[3], a];
}
}
- over(b, content, op)
+ over_layer(l, b, content, op)
}
fn assert_matches_reference(d: &Document) {
@@ -315,10 +353,20 @@ struct Spec {
#[derive(Debug, Clone)]
enum LayerSpec {
- Pixels { fills: Vec<(Rect, Cmyka)>, opacity: f32, fill: f32, clipped: bool, visible: bool, mask: Option<MaskSpec> },
+ Pixels { kind: Kind, fills: Vec<(Rect, Cmyka)>, opacity: f32, fill: f32, clipped: bool, visible: bool, mask: Option<MaskSpec> },
Group { children: Vec<LayerSpec>, pass_through: bool, opacity: f32, fill: f32, mask: Option<MaskSpec> },
}
+/// What kind of layer a [`LayerSpec::Pixels`] becomes: its pixels as a pixel layer, as a type or
+/// shape layer's rendered pixels, or a solid colour fill in the first fill's colour.
+#[derive(Debug, Clone, Copy)]
+enum Kind {
+ Raster,
+ Text,
+ Shape,
+ Solid,
+}
+
#[derive(Debug, Clone)]
struct MaskSpec {
rects: Vec<(Rect, f32)>,
@@ -354,8 +402,10 @@ fn pixels(w: i32, h: i32) -> impl Strategy<Value = LayerSpec> {
any::<bool>(),
prop::bool::weighted(0.9),
mask_spec(w, h),
+ prop_oneof![Just(Kind::Raster), Just(Kind::Text), Just(Kind::Shape), Just(Kind::Solid)],
)
- .prop_map(|(fills, opacity, fill, clipped, visible, mask)| LayerSpec::Pixels {
+ .prop_map(|(fills, opacity, fill, clipped, visible, mask, kind)| LayerSpec::Pixels {
+ kind,
fills: fills.into_iter().map(|(r, (c, m, y, k, a))| (r, [c, m, y, k, a])).collect(),
opacity,
fill,
@@ -379,8 +429,18 @@ fn spec() -> impl Strategy<Value = Spec> {
fn build(s: &LayerSpec) -> Layer {
let make_mask = |m: &MaskSpec| mask(&m.rects, m.default, m.density, m.enabled);
match s {
- LayerSpec::Pixels { fills, opacity, fill, clipped, visible, mask } => {
- let mut l = layer(fills);
+ LayerSpec::Pixels { kind, fills, opacity, fill, clipped, visible, mask } => {
+ let pixels = layer(fills);
+ let mut l = match kind {
+ Kind::Raster => pixels,
+ Kind::Text => Layer::new("t", LayerContent::Text(photocraft_doc::TextLayer { cache: pixels.surface().cloned(), ..Default::default() })),
+ Kind::Shape => Layer::new("s", LayerContent::Shape(photocraft_doc::ShapeLayer { cache: pixels.surface().cloned(), ..Default::default() })),
+ Kind::Solid => {
+ let [c, m, y, k, a] = fills.first().map_or([0.0; 5], |f| f.1);
+ let color = photocraft_color::Color { mode: ColorMode::Cmyk, c: [c, m, y, k], alpha: a };
+ Layer::new("f", LayerContent::Fill(photocraft_doc::Fill::Solid(color)))
+ }
+ };
(l.opacity, l.fill_opacity, l.clipped, l.visible) = (*opacity, *fill, *clipped, *visible);
l.mask = mask.as_ref().map(make_mask);
l
@@ -452,3 +512,117 @@ fn opaque_results_look_the_same_as_the_rgb_compositor() {
}
});
}
+
+// ---- stage 2: type, shape, smart object and fill layers ----------------------------------------
+
+fn text_layer(rects: &[(Rect, Cmyka)]) -> Layer {
+ let cache = layer(rects).surface().cloned();
+ Layer::new("t", LayerContent::Text(photocraft_doc::TextLayer { cache, ..Default::default() }))
+}
+
+fn cmyk_color(c: [f32; 4], alpha: f32) -> photocraft_color::Color {
+ photocraft_color::Color { mode: ColorMode::Cmyk, c, alpha }
+}
+
+/// K-only type keeps K-only glyphs: fully covered pixels are exactly K100, and anti-aliased edges
+/// mix on brightness in the type gamma, adding no cyan, magenta or yellow.
+#[test]
+fn type_interiors_are_exact_and_edges_add_no_other_ink() {
+ let mut d = doc(3, 1);
+ d.layers.push(paper(3, 1, [0.0; 4]));
+ d.layers.push(text_layer(&[(Rect::new(0, 0, 1, 1), [0.0, 0.0, 0.0, 1.0, 1.0]), (Rect::new(1, 0, 2, 1), [0.0, 0.0, 0.0, 1.0, 0.5])]));
+ let got = render_cmyk(&d, d.bounds()).unwrap_or_else(|| panic!("supported"));
+ assert_eq!(got.px[0], [0.0, 0.0, 0.0, 1.0, 1.0], "glyph interior");
+ let edge = got.px[1];
+ assert_eq!(edge[..3], [0.0; 3], "an edge adds no CMY: {edge:?}");
+ let want = over_text([0.0, 0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 1.0, q(0.5)], 1.0);
+ assert!((edge[3] - want[3]).abs() < 1e-6, "{edge:?} vs {want:?}");
+ // The gamma lightens dark edges compared with plain mixing.
+ assert!(edge[3] < q(0.5), "{edge:?}");
+ assert_eq!(got.px[2], [0.0, 0.0, 0.0, 0.0, 1.0], "paper");
+}
+
+#[test]
+fn solid_and_rendered_fills_keep_their_inks() {
+ let mut d = doc(4, 1);
+ d.layers.push(paper(4, 1, [0.0; 4]));
+ let mut solid = Layer::new("f", LayerContent::Fill(photocraft_doc::Fill::Solid(cmyk_color([0.1, 0.2, 0.3, 0.4], 1.0))));
+ solid.mask = Some(mask(&[(Rect::new(2, 0, 4, 1), 0.0)], 1.0, 1.0, true));
+ d.layers.push(solid);
+ // A gradient fill carrying the pixels it was rendered to (as a PSD supplies them).
+ let gradient = photocraft_doc::Fill::gradient(
+ vec![(0.0, cmyk_color([1.0, 0.0, 0.0, 0.0], 1.0)), (1.0, cmyk_color([0.0, 0.0, 0.0, 1.0], 1.0))],
+ 0.0,
+ 1.0,
+ Default::default(),
+ false,
+ );
+ let rendered = layer(&[(Rect::new(3, 0, 4, 1), [0.9, 0.0, 0.5, 0.0, 1.0])]);
+ let mut cached = Layer::new("g", LayerContent::Fill(gradient.clone()));
+ cached.fill_cache = rendered.surface().map(|s| photocraft_doc::FillCache { fill: gradient, surface: s.clone() });
+ d.layers.push(cached);
+ let got = render_cmyk(&d, d.bounds()).unwrap_or_else(|| panic!("supported"));
+ assert_eq!(got.px[0], [q(0.1), q(0.2), q(0.3), q(0.4), 1.0], "the colour's own inks, at the document's levels");
+ assert_eq!(got.px[2], [0.0, 0.0, 0.0, 0.0, 1.0], "masked out: paper");
+ assert_eq!(got.px[3], [q(0.9), 0.0, q(0.5), 0.0, 1.0], "the fill's rendered pixels");
+ assert_matches_reference(&d);
+}
+
+#[test]
+fn stage_two_layers_that_stay_on_the_rgb_compositor() {
+ let base = || {
+ let mut d = doc(2, 2);
+ d.layers.push(paper(2, 2, [0.0; 4]));
+ d
+ };
+ // A pattern fill without rendered pixels.
+ let mut d = base();
+ let p = photocraft_doc::Fill::Pattern { name: "p".into(), scale: 1.0, id: "p".into(), angle: 0.0, link: true, phase: (0.0, 0.0) };
+ d.layers.push(Layer::new("p", LayerContent::Fill(p)));
+ assert!(!supports(&d), "uncached pattern fill");
+ // Type rendered in RGB (not the document's model).
+ let mut d = base();
+ let mut rgb = Layer::raster("x", PixelFormat::RGBA8);
+ if let Some(s) = rgb.surface_mut() {
+ s.fill_rect(Rect::new(0, 0, 1, 1), &[0.0, 0.0, 0.0, 1.0]);
+ }
+ d.layers.push(Layer::new("t", LayerContent::Text(photocraft_doc::TextLayer { cache: rgb.surface().cloned(), ..Default::default() })));
+ assert!(!supports(&d), "RGB type pixels");
+ // A stroked shape with a clipped layer (its stroke goes above the clipped layers).
+ let mut d = base();
+ let shape = photocraft_doc::ShapeLayer { stroke: Some(Default::default()), cache: layer(&[]).surface().cloned(), ..Default::default() };
+ d.layers.push(Layer::new("s", LayerContent::Shape(shape)));
+ let mut clip = layer(&[(Rect::new(0, 0, 2, 2), [0.5; 5])]);
+ clip.clipped = true;
+ d.layers.push(clip);
+ assert!(!supports(&d), "stroked shape with clipped layers");
+ if let Some(c) = d.layers.last_mut() {
+ c.visible = false;
+ }
+ assert!(supports(&d), "without a visible clipped layer the stroked shape is plain pixels");
+}
+
+/// A gradient fill without rendered pixels is the compositor's own CMYK ramp: between cyan and
+/// black it holds only those two inks, pixel for pixel as the compositor renders it.
+#[test]
+fn uncached_gradient_fills_are_cmyk_ramps() {
+ let mut d = doc(8, 2);
+ let g = photocraft_doc::Fill::gradient(
+ vec![(0.0, cmyk_color([1.0, 0.0, 0.0, 0.0], 1.0)), (1.0, cmyk_color([0.0, 0.0, 0.0, 1.0], 1.0))],
+ 0.0,
+ 1.0,
+ Default::default(),
+ false,
+ );
+ d.layers.push(Layer::new("g", LayerContent::Fill(g.clone())));
+ assert!(supports(&d));
+ let got = render_cmyk(&d, d.bounds()).unwrap_or_else(|| panic!("supported"));
+ let frame = crate::fill_frame(&d.layers[0], d.bounds());
+ let want = crate::gradient_fill::render_cmyk_fill(&g, d.bounds(), frame, SampleType::U8).unwrap_or_else(|| panic!("ramp"));
+ for (i, p) in got.px.iter().enumerate() {
+ let (x, y) = ((i % 8) as i32, (i / 8) as i32);
+ assert_eq!(p[1..3], [0.0, 0.0], "only cyan and black: {p:?}");
+ assert_eq!(p.to_vec(), want.pixel(x, y), "({x},{y})");
+ }
+ assert!(got.px[0][0] > got.px[7][0] && got.px[0][3] < got.px[7][3], "cyan to black");
+}