mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #1896 from storytold/community/1462-labmix
Compose: restore the blend-space flags instead of clearing them after a tile (#1462 carry)
This commit is contained in:
3 files changed
+50
-7
No files matched your search
@@ -181,14 +181,10 @@ fn render_tiled_with(doc: &Document, rect: Rect, tile: i32, cx: &Ctx) -> Buffer
|
||||
}
|
||||
|
||||
/// 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,
|
||||
|
||||
@@ -81,6 +81,33 @@ thread_local! {
|
||||
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 {
|
||||
|
||||
@@ -1654,6 +1654,26 @@ fn photo_filter_matches_photoshop() {
|
||||
}
|
||||
}
|
||||
|
||||
#[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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Vibrance of an 8-bit sRGB colour (0–255 in and out).
|
||||
fn vibrance_255(c: [f32; 3], vibrance: f32, saturation: f32) -> [f32; 3] {
|
||||
let mut buf = Buffer::filled(Rect::new(0, 0, 1, 1), [c[0] / 255.0, c[1] / 255.0, c[2] / 255.0, 1.0]);
|
||||
|
||||
Reference in new issue
Block a user