Integration: parallel brush composite keeps the document's CMYK profile (#7218)

The document's CMYK profile is active per thread (with_cmyk_space), and the
parallel composite ran its tiles on rayon workers outside that scope. On a
CMYK document with its own profile, every blend mode other than Normal and
Dissolve then converted those tiles through the built-in coated CMYK: a 40 px
Multiply line 1100 px long (17+ tiles, parallel at the default threshold)
differed from the serial path in 256384 samples. The workers now enter the
caller's CMYK space, as the compositor and gpu crates do.

Test: a_parallel_composite_converts_through_the_documents_cmyk_profile fails
without the fix.

Co-authored-by: Unemars <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
committed 2026-10-22 20:31:34 +02:00
1 parent 8c5ec444db
commit 360e0920fc
4 files changed
+48 -1

No files matched your search

Generated
+1
View File
@@ -4942,6 +4942,7 @@ dependencies = [
name = "photocraft-paint"
version = "0.11.1"
dependencies = [
"photocraft-cms",
"photocraft-color",
"photocraft-geom",
"photocraft-raster",
+1
View File
@@ -18,4 +18,5 @@ serde = { workspace = true }
rayon = { workspace = true }
[dev-dependencies]
photocraft-cms = { workspace = true }
serde_json = { workspace = true }
+39
View File
@@ -1660,6 +1660,45 @@ fn large_tips_render_the_same_in_parallel_and_serially() {
}
}
/// A CMYK document with its own profile: blend modes other than Normal convert each pixel through
/// that profile, which is active per thread, so a stroke composited in parallel gives the pixels the
/// serial path gives (the workers enter the caller's CMYK space too).
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_parallel_composite_converts_through_the_documents_cmyk_profile() {
use photocraft_cms::synth::{CmykParams, cmyk_profile};
use photocraft_color::convert::{CmykSpace, with_cmyk_space};
let profile =
cmyk_profile(&CmykParams { description: "Brush Test CMYK".into(), tvi: [0.26, 0.26, 0.26, 0.3], grid_a2b: 5, grid_b2a: 9, ..Default::default() });
let bytes = profile.to_bytes();
let Some(space) = CmykSpace::for_profile(Some(&bytes)) else { panic!("the synthetic profile is a CMYK space") };
let fmt = PixelFormat::new(ColorMode::Cmyk, SampleType::U16, true);
// A 40 px Multiply line over 1100 px dirties more than 16 tiles: the composite's batch is
// worth the threads at the default threshold, and always at threshold 0.
let b = BrushSettings { size: 40.0, hardness: 0.5, mode: BlendMode::Multiply, color: [0.2, 0.4, 0.9, 1.0], ..brush() };
let pts = line(40.0, 1140.0, 60.0);
let full = Rect::new(0, 0, 1200, 120);
let render = |parallel_from: usize| -> Vec<f32> {
crate::render::set_parallel_min_pixels(parallel_from);
let mut s = Surface::new(fmt);
s.fill_rect(full, &[0.1, 0.5, 0.3, 0.05, 1.0]);
with_cmyk_space(Some(&space), || render_stroke(&mut s, &b, &pts, None, false, 1.0));
s.read_region(full)
};
struct Restore;
impl Drop for Restore {
fn drop(&mut self) {
crate::render::set_parallel_min_pixels(crate::render::DEFAULT_PARALLEL_MIN_PIXELS);
}
}
let _restore = Restore;
let parallel = render(0);
let serial = render(usize::MAX);
let diff = parallel.iter().zip(&serial).filter(|(a, b)| a != b).count();
assert_eq!(diff, 0, "{diff} samples differ between the parallel and the serial composite in a profiled CMYK document");
assert!(serial.chunks(5).any(|p| (p[1] - 0.5).abs() > 0.05), "the stroke painted something");
}
/// Photoshop (measured): a 100 px brush with a 20 px Dual Brush tip resized to 50 / 2 / 1 px has a
/// 10 / 0.4 / 0.2 px dual tip, unrounded and with no floor, and back at 100 px it is 20 px again; past
/// 5000 px the dual tip is capped there and the proportion is lost (#4809).
+7 -1
View File
@@ -1111,9 +1111,12 @@ impl StrokeRenderer {
// pending buffers few, and the pool keeps them from one composite to the next.
const BATCH: usize = 256;
let mut pool = std::mem::take(&mut self.regions.0);
// The document's CMYK profile is active per thread: the workers enter the caller's, or a
// blend mode other than Normal would convert their tiles through the built-in coated CMYK.
let cmyk = photocraft_color::convert::active_cmyk_space();
for batch in keys.chunks(BATCH) {
let mut items: Vec<((i32, i32), Rect, Vec<f32>)> = batch.iter().map(|&key| (key, Rect::EMPTY, pool.pop().unwrap_or_default())).collect();
for_each_item(&mut items, batch.len() * (COV_TILE * COV_TILE) as usize, |(key, done, region)| {
let composite_tile = |(key, done, region): &mut ((i32, i32), Rect, Vec<f32>)| {
let (tx, ty) = *key;
let Some(tile) = self.cov.tiles.get(&(tx, ty)) else { return };
let tr = Rect::new(tx * COV_TILE, ty * COV_TILE, (tx + 1) * COV_TILE, (ty + 1) * COV_TILE).intersect(&bounds);
@@ -1190,6 +1193,9 @@ impl StrokeRenderer {
}
}
*done = tr;
};
for_each_item(&mut items, batch.len() * (COV_TILE * COV_TILE) as usize, |item| {
photocraft_color::convert::with_cmyk_space(cmyk.as_ref(), || composite_tile(item))
});
for (_, tr, region) in items {
if !tr.is_empty() {