Skip to content

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

Compose: restore the Lab-mix flag instead of clearing it after a tile (#1112)

Claude Opus 5.5f1433f4ab296
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index d1d44e975..a073325fa 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -104,26 +104,18 @@ fn render_tiled_with(doc: &Document, rect: Rect, tile: i32, cx: &Ctx) -> Buffer
if rect.width() as i32 <= tile && rect.height() as i32 <= tile {
return photocraft_color::convert::with_cmyk_space(cmyk, || {
let mut buf = multichannel::backdrop(doc, rect);
- psblend::LAB_MIX.with(|l| l.set(lab));
- composite_stack(&doc.layers, &mut buf, cx);
- psblend::LAB_MIX.with(|l| l.set(false));
+ psblend::with_lab_mix(lab, || composite_stack(&doc.layers, &mut buf, cx));
buf
});
}
// Effect maps are built once, here, before any tile needs them (#276).
prepare_effects(&doc.layers, rect, cx, |f| {
- photocraft_color::convert::with_cmyk_space(cmyk, || {
- psblend::LAB_MIX.with(|l| l.set(lab));
- f();
- psblend::LAB_MIX.with(|l| l.set(false));
- });
+ photocraft_color::convert::with_cmyk_space(cmyk, || psblend::with_lab_mix(lab, f));
});
let run = |t: Rect| {
photocraft_color::convert::with_cmyk_space(cmyk, || {
let mut b = multichannel::backdrop(doc, t);
- psblend::LAB_MIX.with(|l| l.set(lab));
- composite_stack(&doc.layers, &mut b, cx);
- psblend::LAB_MIX.with(|l| l.set(false));
+ psblend::with_lab_mix(lab, || composite_stack(&doc.layers, &mut b, cx));
b
})
};
diff --git a/crates/compose/src/psblend.rs b/crates/compose/src/psblend.rs
index c3698bb4b..c98335298 100644
--- a/crates/compose/src/psblend.rs
+++ b/crates/compose/src/psblend.rs
@@ -78,6 +78,20 @@ thread_local! {
pub static LAB_MIX: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
+/// Runs `f` with [`LAB_MIX`] set to `lab`, then restores the value it had (also on unwind). A
+/// nested rayon job can run another tile on this thread mid-composite; clearing the flag there
+/// made the rest of the outer tile mix in sRGB (#1112).
+pub fn with_lab_mix<R>(lab: bool, f: impl FnOnce() -> R) -> R {
+ struct Restore(bool);
+ impl Drop for Restore {
+ fn drop(&mut self) {
+ LAB_MIX.with(|l| l.set(self.0));
+ }
+ }
+ let _restore = Restore(LAB_MIX.with(|l| l.replace(lab)));
+ f()
+}
+
/// `B(Cb, Cs)` with Photoshop's variants.
pub fn blend_rgb(mode: BlendMode, cb: [f32; 3], cs: [f32; 3]) -> [f32; 3] {
match mode {
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index f6e143c1e..a7b8c8b7c 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -1565,3 +1565,23 @@ fn photo_filter_matches_photoshop() {
assert!(got.iter().zip(ps).all(|(g, p)| (g - p).abs() <= 2.5), "F32 {v:?}: got {got:?} want {ps:?}");
}
}
+
+#[test]
+fn rendering_restores_the_lab_mix_flag_it_found() {
+ // #1112: a render set LAB_MIX for its document, then cleared it. A nested rayon job (a
+ // gradient fill, say) can let a thread render another tile in the middle of an outer Lab
+ // tile, and clearing the flag made the rest of that outer tile mix in sRGB.
+ for mode in [ColorMode::Lab, ColorMode::Rgb] {
+ let doc = Document::with_background("t", Size::new(8, 8), mode, SampleType::U8, Color::WHITE);
+ // Single-tile and multi-tile paths (the latter also prepares effect maps under the flag).
+ for tile in [RENDER_TILE, 4] {
+ for outer in [true, false] {
+ psblend::LAB_MIX.with(|l| l.set(outer));
+ let _ = render_tiled(&doc, doc.bounds(), tile);
+ let after = psblend::LAB_MIX.with(|l| l.get());
+ psblend::LAB_MIX.with(|l| l.set(false));
+ assert_eq!(after, outer, "{mode:?} with tile {tile} inside a tile that had LAB_MIX = {outer}");
+ }
+ }
+ }
+}