diff --git a/Cargo.lock b/Cargo.lock index dacf8307d5..1fac8f1ea8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4942,6 +4942,7 @@ dependencies = [ name = "photocraft-paint" version = "0.11.1" dependencies = [ + "photocraft-cms", "photocraft-color", "photocraft-geom", "photocraft-raster", diff --git a/crates/paint/Cargo.toml b/crates/paint/Cargo.toml index 4f4ca359e6..b72880c40c 100644 --- a/crates/paint/Cargo.toml +++ b/crates/paint/Cargo.toml @@ -18,4 +18,5 @@ serde = { workspace = true } rayon = { workspace = true } [dev-dependencies] +photocraft-cms = { workspace = true } serde_json = { workspace = true } diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rs index 8edf99350a..8b9cd69219 100644 --- a/crates/paint/src/brush_tests.rs +++ b/crates/paint/src/brush_tests.rs @@ -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 { + 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). diff --git a/crates/paint/src/render.rs b/crates/paint/src/render.rs index ab38249bd9..d92f83b7ca 100644 --- a/crates/paint/src/render.rs +++ b/crates/paint/src/render.rs @@ -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)> = 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)| { 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() {