From 877982cb0c396dbba363c4141a606c93de0cf53e Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 23 Nov 2026 01:37:53 +0100 Subject: [PATCH] fix(compose): CMYK split stand-ins keep ink Curves off whole levels #14650 puts 8-bit RGB Curves output on whole levels. The CMYK split route renders ink Curves as RGB stand-ins, so they were rounded too, while the exact ink path is not: the two routes parted by one level (cmyk::tests::the_split_route_agrees_with_the_exact_path). The stand-in contexts now build unrounded Curves tables; RGB documents are unchanged. The shrunk failing case is kept as a proptest regression seed. Co-Authored-By: Claude Opus 5.5 --- .../proptest-regressions/cmyk/tests.txt | 7 +++++++ crates/compose/src/adjust.rs | 3 ++- crates/compose/src/adjust/tone.rs | 17 ++++++++++++++-- crates/compose/src/cmyk_split.rs | 6 +++--- crates/compose/src/ctx.rs | 20 +++++++++++++++++-- crates/compose/src/render.rs | 1 + 6 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 crates/compose/proptest-regressions/cmyk/tests.txt diff --git a/crates/compose/proptest-regressions/cmyk/tests.txt b/crates/compose/proptest-regressions/cmyk/tests.txt new file mode 100644 index 00000000000..98770e51562 --- /dev/null +++ b/crates/compose/proptest-regressions/cmyk/tests.txt @@ -0,0 +1,7 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 9cd32fd3b0d418f70a3535174d06eccab73fdd2345cf51e50b53c9f2e006043e # shrinks to s = Spec { w: 2, h: 2, layers: [Group { children: [Pixels { kind: Solid, blend: Normal, fills: [(Rect { x0: 0, y0: 0, x1: 1, y1: 1 }, [0.0, 0.0, 0.54360455, 0.0, 1.0])], opacity: 1.0, fill: 1.0, clipped: false, visible: true, mask: None }, Pixels { kind: Adjust(Curves { channel: 2, points: [(0.4490113, 1.0), (1.0, 0.0)] }), blend: Normal, fills: [(Rect { x0: 0, y0: 0, x1: 1, y1: 1 }, [0.0, 0.0, 0.0, 0.0, 0.0])], opacity: 0.6871668, fill: 1.0, clipped: false, visible: true, mask: None }], pass_through: false, blend: Normal, opacity: 1.0, fill: 1.0, mask: None }] } diff --git a/crates/compose/src/adjust.rs b/crates/compose/src/adjust.rs index 7cf4de4d34e..aa6d141627b 100644 --- a/crates/compose/src/adjust.rs +++ b/crates/compose/src/adjust.rs @@ -32,7 +32,8 @@ use tone::LUT_SIZE; pub(crate) use tone::apply_tone; pub use tone::{ TONE_SLOPE_SPAN, contour_curve_lut, curve_lut, curve_lut_mode, curve_map_lut, curve_samples, float_tone_extrapolates, float_tone_identity, levels, - levels_float, levels_gamma, levels_q, modern_brightness, modern_contrast, modern_lut, posterize, posterize_in, tone_luts, tone_luts_depth, tone_luts_q, + levels_float, levels_gamma, levels_q, modern_brightness, modern_contrast, modern_lut, posterize, posterize_in, tone_luts, tone_luts_depth, + tone_luts_depth_in, tone_luts_q, }; use tone::{lut, map_rgb}; diff --git a/crates/compose/src/adjust/tone.rs b/crates/compose/src/adjust/tone.rs index 13da25f0e7f..78fcc6fb6a0 100644 --- a/crates/compose/src/adjust/tone.rs +++ b/crates/compose/src/adjust/tone.rs @@ -38,6 +38,12 @@ pub fn tone_luts(adj: &Adjustment) -> [Vec; 4] { /// [`tone_luts`] for samples with `quantum` steps per unit (Levels works on whole levels; see /// [`levels_q`]). pub fn tone_luts_q(adj: &Adjustment, quantum: Option) -> [Vec; 4] { + tone_luts_qc(adj, quantum, true) +} + +/// [`tone_luts_q`]; `curve_levels` false leaves an 8-bit RGB Curves channel's output where the +/// spline put it instead of on a whole level (see [`tone_luts_depth_in`]). +fn tone_luts_qc(adj: &Adjustment, quantum: Option, curve_levels: bool) -> [Vec; 4] { let x = |k: usize| k as f32 / (LUT_SIZE - 1) as f32; match adj { Adjustment::Levels { master, per_channel, space, black } => { @@ -55,7 +61,7 @@ pub fn tone_luts_q(adj: &Adjustment, quantum: Option) -> [Vec; 4] { // the row is flat across a level and the table cannot blend two levels' results at a // document sample - the way [`levels_q`] is exact at every level. 16-bit and float // documents were not measured, nor CMYK and Lab curves, so only the 8-bit quantum. - let level = (*space == ToneSpace::Rgb && quantum == Some(EIGHT_BIT_LEVELS)).then_some(EIGHT_BIT_LEVELS); + let level = (curve_levels && *space == ToneSpace::Rgb && quantum == Some(EIGHT_BIT_LEVELS)).then_some(EIGHT_BIT_LEVELS); let row = |c: &[CurvePoint], level: Option| match level { Some(q) => { let channel = curve_lut_mode(c, *pencil); @@ -79,13 +85,20 @@ pub fn tone_luts_q(adj: &Adjustment, quantum: Option) -> [Vec; 4] { /// [`levels_float`], sampled on 0..1 and kept in 0..1 for LUT consumers. Float RGB rendering /// evaluates the formula directly instead, so samples and results outside that domain survive. pub fn tone_luts_depth(adj: &Adjustment, depth: Option) -> [Vec; 4] { + tone_luts_depth_in(adj, depth, true) +} + +/// [`tone_luts_depth`]; `curve_levels` false for the RGB Curves that stand in for a CMYK +/// document's ink curves on the split route (`cmyk_split`): the exact ink path doesn't put CMYK +/// curves on whole levels, so neither may their stand-ins, or the two routes part by a level. +pub fn tone_luts_depth_in(adj: &Adjustment, depth: Option, curve_levels: bool) -> [Vec; 4] { match (adj, depth) { (Adjustment::Levels { master, per_channel, space: ToneSpace::Rgb, .. }, Some(SampleType::F32)) => { let x = |k: usize| k as f32 / (LUT_SIZE - 1) as f32; let row = |c: &LevelsChannel| (0..LUT_SIZE).map(|k| levels_float(master, levels_float(c, x(k))).clamp(0.0, 1.0)).collect(); [row(&per_channel[0]), row(&per_channel[1]), row(&per_channel[2]), (0..LUT_SIZE).map(x).collect()] } - _ => tone_luts_q(adj, depth.and_then(crate::adjustment_quantum)), + _ => tone_luts_qc(adj, depth.and_then(crate::adjustment_quantum), curve_levels), } } diff --git a/crates/compose/src/cmyk_split.rs b/crates/compose/src/cmyk_split.rs index 39040c7e94c..f3bf9065cbf 100644 --- a/crates/compose/src/cmyk_split.rs +++ b/crates/compose/src/cmyk_split.rs @@ -316,8 +316,8 @@ fn combine(cmy: &Buffer, k: &Buffer) -> CmykBuffer { pub(super) fn render_bands(doc: &Document, rect: Rect, band_rows: i32, tile: i32, mut sink: impl FnMut(CmykBuffer) -> Result<(), E>) -> Result<(), E> { let (cmy, k) = (stand_in(doc, CMY), stand_in(doc, K)); let no_patterns = pattern::PreparedPatterns::new(&[], pattern::PREPARED_PATTERN_BYTES); - let a = StandIn { doc: &cmy, cx: Ctx::for_doc(&cmy, &no_patterns) }; - let b = StandIn { doc: &k, cx: Ctx::for_doc(&k, &no_patterns) }; + let a = StandIn { doc: &cmy, cx: Ctx { curve_levels: false, ..Ctx::for_doc(&cmy, &no_patterns) } }; + let b = StandIn { doc: &k, cx: Ctx { curve_levels: false, ..Ctx::for_doc(&k, &no_patterns) } }; let rows = crate::band_rows_for(rect.width(), band_rows); let mut y = rect.y0; while y < rect.y1 { @@ -336,7 +336,7 @@ pub(super) fn render_bands(doc: &Document, rect: Rect, band_rows: i32, tile: pub(super) fn render_planes(doc: &Document, rect: Rect, planes: Planes) -> Buffer { let d = stand_in(doc, planes); let no_patterns = pattern::PreparedPatterns::new(&[], pattern::PREPARED_PATTERN_BYTES); - let s = StandIn { doc: &d, cx: Ctx::for_doc(&d, &no_patterns) }; + let s = StandIn { doc: &d, cx: Ctx { curve_levels: false, ..Ctx::for_doc(&d, &no_patterns) } }; s.prepare(rect); s.tile(rect) } diff --git a/crates/compose/src/ctx.rs b/crates/compose/src/ctx.rs index 6fe541678d6..cada57212f7 100644 --- a/crates/compose/src/ctx.rs +++ b/crates/compose/src/ctx.rs @@ -60,6 +60,9 @@ pub(crate) struct Ctx<'a> { pub(crate) doc: Option<&'a Document>, /// Effect-map identities of this render's document layers (see [`LayerKeys`]). pub(crate) layer_keys: LayerKeys, + /// Whether 8-bit RGB Curves put each channel's output on a whole level + /// ([`adjust::tone_luts_depth_in`]); false for the split route's CMYK stand-ins. + pub(crate) curve_levels: bool, } /// Memo of [`layer_identity`] for the layers of the document being rendered. The identity depends @@ -130,6 +133,7 @@ impl<'a> Ctx<'a> { fx_over_budget: fx_over_budget(&doc.layers, doc.bounds()), layer_keys: Default::default(), tone_luts: prepare_tones(&doc.layers, 0), + curve_levels: true, } } @@ -143,16 +147,28 @@ impl<'a> Ctx<'a> { } pub(crate) fn apply_adjustment(&self, adj: &photocraft_doc::Adjustment, buf: &mut Buffer) { + let cached = self.tone_luts.iter().find(|(a, _)| std::ptr::eq(*a, adj)); + // Without a cached table, only a stand-in's tables (unrounded Curves) are built here; + // other documents take `apply_in`, which builds the same tables as `for_doc` would. if let photocraft_doc::Adjustment::Levels { space, .. } | photocraft_doc::Adjustment::Curves { space, .. } = adj && !matches!(adj, photocraft_doc::Adjustment::Levels { space: photocraft_doc::adjust::ToneSpace::Rgb, .. } if self.depth == photocraft_color::SampleType::F32) - && let Some((_, luts)) = self.tone_luts.iter().find(|(a, _)| std::ptr::eq(*a, adj)) + && (cached.is_some() || !self.curve_levels) { let identity = adjust::float_tone_identity(adj, Some(self.depth)); if identity == [true; 3] { return; } let extrapolate = adjust::float_tone_extrapolates(adj, Some(self.depth), self.mode); - adjust::apply_tone(buf, *space, luts.get_or_init(|| adjust::tone_luts_depth(adj, Some(self.depth))), identity, extrapolate); + let tables = || adjust::tone_luts_depth_in(adj, Some(self.depth), self.curve_levels); + let built; + let luts = match cached { + Some((_, luts)) => luts.get_or_init(tables), + None => { + built = tables(); + &built + } + }; + adjust::apply_tone(buf, *space, luts, identity, extrapolate); } else { adjust::apply_in(adj, buf, self.transfer, Some(self.depth), self.mode); } diff --git a/crates/compose/src/render.rs b/crates/compose/src/render.rs index a31e227a3dc..ba7b9b45cb9 100644 --- a/crates/compose/src/render.rs +++ b/crates/compose/src/render.rs @@ -378,6 +378,7 @@ pub fn render_layer(layer: &Layer, rect: Rect) -> Buffer { fx_clip: Default::default(), fx_over_budget: false, tone_luts: prepare_tones(std::slice::from_ref(layer), 0), + curve_levels: true, doc: None, layer_keys: Default::default(), },