Skip to content

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

Merge main into #1462; in_blend_space restores LAB_MIX and HDR instead of clearing them

Claudecc8d3c3f773e
diff --cc crates/compose/src/lib.rs
index 4f15130b6,4ab50d378..5e4c74cf8
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@@ -178,6 -180,17 +180,13 @@@ fn render_tiled_with(doc: &Document, re
out
}
+ /// Runs `f` with this thread's document blend flags set ([`psblend::LAB_MIX`], [`psblend::HDR`]),
-/// clearing them afterwards.
++/// then restores the values they had (also on unwind): a nested rayon job can run another tile
++/// on this thread mid-composite, and clearing the flags there broke the outer tile (#1112).
+ fn in_blend_space<R>(lab: bool, hdr: bool, f: impl FnOnce() -> R) -> R {
- psblend::LAB_MIX.with(|l| l.set(lab));
- psblend::HDR.with(|h| h.set(hdr));
- let r = f();
- psblend::LAB_MIX.with(|l| l.set(false));
- psblend::HDR.with(|h| h.set(false));
- r
++ psblend::with_lab_mix(lab, || psblend::with_hdr(hdr, f))
+ }
+
/// The document's own CMYK profile for reading its CMYK pixels (`None`: not a CMYK document,
/// untagged, or the built-in coated CMYK). Enter it with `photocraft_color::convert::with_cmyk_space`
/// around code that converts the document's CMYK pixels or colours to RGB.
diff --cc crates/compose/src/psblend.rs
index c98335298,5d83068c9..063e32ef5
--- a/crates/compose/src/psblend.rs
+++ b/crates/compose/src/psblend.rs
@@@ -76,22 -76,11 +76,38 @@@ thread_local!
/// (Photoshop composites Lab documents in Lab; an anti-aliased edge between two colours
/// differs by up to 14 / 255 from an sRGB mix: psd-tools stroke-color-descriptors-lab).
pub static LAB_MIX: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
+ /// Set while rendering a 32-bit float document: Linear Dodge (Add) and Divide don't clip at 1
+ /// ([`generic::blend_rgb_hdr`]), as in Photoshop's 32-bit mode; integer depths clip.
+ pub static HDR: 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()
+}
+
++/// Runs `f` with [`HDR`] set to `hdr`, then restores the value it had (also on unwind), like
++/// [`with_lab_mix`].
++pub fn with_hdr<R>(hdr: bool, f: impl FnOnce() -> R) -> R {
++ struct Restore(bool);
++ impl Drop for Restore {
++ fn drop(&mut self) {
++ HDR.with(|h| h.set(self.0));
++ }
++ }
++ let _restore = Restore(HDR.with(|h| h.replace(hdr)));
++ f()
++}
++
/// `B(Cb, Cs)` with Photoshop's variants.
pub fn blend_rgb(mode: BlendMode, cb: [f32; 3], cs: [f32; 3]) -> [f32; 3] {
match mode {