From 52af7995f901e2a3d1541dcbcf2e21238d5b18fb Mon Sep 17 00:00:00 2001 From: yoriswind Date: Tue, 1 Dec 2026 02:43:59 -0800 Subject: [PATCH] Match the eight-bit Classic Gradient Hard Mix threshold --- .../gradient_hard_mix_rounded_source_tests.rs | 61 ++++++++++++++++ .../algo/src/gradient_hard_mix_tie_tests.rs | 73 +++++++++++++++++++ crates/algo/src/paint.rs | 45 +++++++++++- 3 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 crates/algo/src/gradient_hard_mix_rounded_source_tests.rs create mode 100644 crates/algo/src/gradient_hard_mix_tie_tests.rs diff --git a/crates/algo/src/gradient_hard_mix_rounded_source_tests.rs b/crates/algo/src/gradient_hard_mix_rounded_source_tests.rs new file mode 100644 index 0000000000..b36237d42e --- /dev/null +++ b/crates/algo/src/gradient_hard_mix_rounded_source_tests.rs @@ -0,0 +1,61 @@ +use photocraft_color::{PixelFormat, blend, dither_noise}; +use photocraft_raster::Surface; + +use super::{BlendMode, FillMode, GradientShape, Rect, paint_gradient, sample_stops, tool_gradient_t}; + +const WIDTH: i32 = 2560; +const STOPS: [(f32, [f32; 4]); 2] = [(0.0, [0.0, 0.0, 0.0, 1.0]), (1.0, [1.0, 1.0, 1.0, 1.0])]; + +fn row(format: PixelFormat, base: u8, mode: BlendMode, dither: bool) -> Surface { + let mut surface = Surface::new(format); + let area = Rect::new(0, 0, WIDTH, 1); + let gray = f32::from(base) / 255.0; + surface.fill_rect(area, &[gray, gray, gray, 1.0]); + paint_gradient(&mut surface, area, (0.0, 0.0), (WIDTH as f32, 0.0), GradientShape::Linear, &STOPS, false, 1.0, FillMode::Blend(mode), dither, None, false); + surface +} + +#[test] +fn gradient_hard_mix_switches_at_the_normal_ramp_byte_boundary() { + let normal = row(PixelFormat::RGBA8, 0, BlendMode::Normal, false); + for (base, first_white) in [(128, 127), (100, 156)] { + let hard_mix = row(PixelFormat::RGBA8, base, BlendMode::HardMix, false); + let mut saw = [0usize; 2]; + for x in 0..WIDTH { + let normal_level = (normal.pixel(x, 0)[0] * 255.0).round() as u8; + let white = normal_level >= first_white; + assert_eq!(hard_mix.pixel(x, 0)[0], f32::from(u8::from(white)), "base {base}, x={x}, normal {normal_level}"); + if normal_level == first_white - 1 || normal_level == first_white { + saw[usize::from(white)] += 1; + } + } + assert!(saw.into_iter().all(|pixels| pixels > 1), "both tested colour runs have more than one pixel"); + } +} + +#[test] +fn dithering_deep_rgb_and_other_modes_keep_the_existing_float_composite() { + for (format, mode, dither) in [ + (PixelFormat::RGBA8, BlendMode::HardMix, true), + (PixelFormat::RGBA16, BlendMode::HardMix, false), + (PixelFormat::RGBA32F, BlendMode::HardMix, false), + (PixelFormat::RGBA8, BlendMode::Multiply, false), + ] { + let actual = row(format, 100, mode, dither); + let mut expected = Surface::new(format); + expected.write_pixel(0, 0, &[100.0 / 255.0, 100.0 / 255.0, 100.0 / 255.0, 1.0]); + let base = expected.rgba(0, 0); + for x in 0..WIDTH { + let t = tool_gradient_t(GradientShape::Linear, (0.0, 0.0), (WIDTH as f32, 0.0), x as f32 - 0.5, -0.5); + let mut color = sample_stops(&STOPS, t); + if dither { + let noise = (dither_noise(x, 0) - 0.5) / 255.0; + for channel in &mut color[..3] { + *channel = (*channel + noise).clamp(0.0, 1.0); + } + } + expected.write_pixel(x, 0, &blend::composite(mode, base, color, 1.0)); + assert_eq!(actual.pixel(x, 0), expected.pixel(x, 0), "{format:?} {mode:?}, dither={dither}, x={x}"); + } + } +} diff --git a/crates/algo/src/gradient_hard_mix_tie_tests.rs b/crates/algo/src/gradient_hard_mix_tie_tests.rs new file mode 100644 index 0000000000..b17295c854 --- /dev/null +++ b/crates/algo/src/gradient_hard_mix_tie_tests.rs @@ -0,0 +1,73 @@ +use std::cmp::Ordering; + +use photocraft_color::PixelFormat; +use photocraft_raster::Surface; + +use super::{BlendMode, FillMode, GradientShape, Rect, paint_gradient}; + +#[test] +fn gradient_hard_mix_matches_all_u8_pairs_and_uses_each_rgb_channel() { + let mut surface = Surface::new(PixelFormat::RGBA8); + for base in 0..256 { + let gray = base as f32 / 255.0; + surface.fill_rect(Rect::new(0, base, 256, base + 1), &[gray, gray, gray, 1.0]); + } + let blue = 200.0 / 255.0; + paint_gradient( + &mut surface, + Rect::new(0, 0, 256, 256), + (-0.5, 0.0), + (254.5, 0.0), + GradientShape::Linear, + &[(0.0, [0.0, 1.0, blue, 1.0]), (1.0, [1.0, 0.0, blue, 1.0])], + false, + 1.0, + FillMode::Blend(BlendMode::HardMix), + false, + None, + false, + ); + for base in 0..256 { + for source in 0..256 { + let expected = [source, 255 - source, 200].map(|channel| match (base + channel).cmp(&255) { + Ordering::Less => 0.0, + Ordering::Greater => 1.0, + Ordering::Equal => f32::from(u8::from(channel < 128)), + }); + let actual = surface.pixel(source, base); + assert_eq!(actual[..3], expected, "base {base}, red source {source}"); + assert_eq!(actual[3], 1.0); + } + } +} + +#[test] +fn gradient_hard_mix_preserves_source_over_and_locked_alpha() { + for locked in [false, true] { + let mut surface = Surface::new(PixelFormat::RGBA8); + surface.write_pixel(1, 0, &[127.0 / 255.0, 127.0 / 255.0, 127.0 / 255.0, 128.0 / 255.0]); + let color = [128.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 1.0]; + paint_gradient( + &mut surface, + Rect::new(0, 0, 2, 1), + (0.0, 0.0), + (1.0, 0.0), + GradientShape::Linear, + &[(0.0, color), (1.0, color)], + false, + 0.5, + FillMode::Blend(BlendMode::HardMix), + false, + None, + locked, + ); + let byte = |x, channel: usize| (surface.pixel(x, 0)[channel] * 255.0).round() as u8; + if locked { + assert_eq!(byte(0, 3), 0); + assert_eq!((byte(1, 0), byte(1, 3)), (64, 128)); + } else { + assert_eq!((byte(0, 0), byte(0, 3)), (128, 128)); + assert_eq!((byte(1, 0), byte(1, 3)), (85, 192)); + } + } +} diff --git a/crates/algo/src/paint.rs b/crates/algo/src/paint.rs index 39e4770117..31bfc386da 100644 --- a/crates/algo/src/paint.rs +++ b/crates/algo/src/paint.rs @@ -78,6 +78,7 @@ fn composite_area( mode: FillMode, coverage: impl Fn(i32, i32) -> f32 + Sync, color: impl Fn(i32, i32) -> [f32; 4] + Sync, + blend_pixel: impl Fn(BlendMode, [f32; 4], [f32; 4], f32, bool) -> [f32; 4] + Sync, lock_transparency: bool, inks: Option<[f32; 4]>, rim: Option<&(dyn Fn(i32, i32) -> f32 + Sync)>, @@ -123,8 +124,7 @@ fn composite_area( continue; } let o = match mode { - FillMode::Blend(m) if lock_transparency => blend::composite_atop(m, d, color(x, y), k), - FillMode::Blend(m) => blend::composite(m, d, color(x, y), k), + FillMode::Blend(m) => blend_pixel(m, d, color(x, y), k, lock_transparency), // Locked transparency keeps the alpha, so Clear leaves the pixel as it is. FillMode::Clear if lock_transparency => d, FillMode::Clear => [d[0], d[1], d[2], d[3] * (1.0 - k.min(1.0))], @@ -157,6 +157,10 @@ fn composite_area( } } +fn composite_blend(mode: BlendMode, backdrop: [f32; 4], source: [f32; 4], coverage: f32, lock_transparency: bool) -> [f32; 4] { + if lock_transparency { blend::composite_atop(mode, backdrop, source, coverage) } else { blend::composite(mode, backdrop, source, coverage) } +} + /// [`FillMode::Behind`] for one pixel: `src` laid under `d` at coverage `k`, with the Brush's /// Behind formula (`photocraft_paint`'s render): the fill's share is `src α × k × (1 − d α)`. fn behind(d: [f32; 4], src: [f32; 4], k: f32) -> [f32; 4] { @@ -194,6 +198,10 @@ pub fn paint_gradient( let smooth_16 = fmt.sample == photocraft_color::SampleType::U16 && matches!(fmt.mode, photocraft_color::ColorMode::Rgb | photocraft_color::ColorMode::Grayscale); let dither = dither && !smooth_16; + let hard_mix_rgb8 = mode == FillMode::Blend(BlendMode::HardMix) + && fmt.mode == photocraft_color::ColorMode::Rgb + && fmt.sample == photocraft_color::SampleType::U8 + && !dither; composite_area( s, area, @@ -212,12 +220,35 @@ pub fn paint_gradient( } c }, + |mode, backdrop, source, coverage, locked| { + if hard_mix_rgb8 { + composite_hard_mix_gradient(backdrop, source, coverage, locked) + } else { + composite_blend(mode, backdrop, source, coverage, locked) + } + }, lock_transparency, None, None, ); } +fn composite_hard_mix_gradient(backdrop: [f32; 4], mut source: [f32; 4], coverage: f32, locked: bool) -> [f32; 4] { + // Round the comparison exactly as a Normal RGB8 gradient would store it. Equal sums + // take white for a dark source and black for a light source. + let quantize = |v: f32| (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u16; + let blend_weight = if locked { 1.0 } else { backdrop[3] }; + for (color, base) in source[..3].iter_mut().zip(&backdrop[..3]) { + let s = quantize(*color); + let sum = quantize(*base) + s; + let mixed = if sum > 255 || (sum == 255 && s <= 127) { 1.0 } else { 0.0 }; + // In source-over, only the overlap uses the blend. The uncovered part keeps the + // unblended gradient colour. Normal then applies the existing coverage and alpha math. + *color = (1.0 - blend_weight) * *color + blend_weight * mixed; + } + composite_blend(BlendMode::Normal, backdrop, source, coverage, locked) +} + /// Paint bucket: fills the region similar to the seed pixel (on the layer's /// own pixels) within `area`, respecting the selection. #[allow(clippy::too_many_arguments)] @@ -352,7 +383,7 @@ fn fill_region( if hit == Some(&true) { strength(x, y) } else { 0.0 } }; let rim: Option<&(dyn Fn(i32, i32) -> f32 + Sync)> = if use_rim { Some(&on_ring) } else { None }; - composite_area(s, b, mode, |x, y| region.at(x, y) * strength(x, y), src, lock_transparency, inks, rim); + composite_area(s, b, mode, |x, y| region.at(x, y) * strength(x, y), src, composite_blend, lock_transparency, inks, rim); } /// Over `region.bbox` (row-major): the pixels the region leaves uncovered that touch one of its @@ -378,6 +409,14 @@ mod gradient_dither_16bit_tests; #[path = "paint_gradient_sample_tests.rs"] mod paint_gradient_sample_tests; +#[cfg(test)] +#[path = "gradient_hard_mix_tie_tests.rs"] +mod gradient_hard_mix_tie_tests; + +#[cfg(test)] +#[path = "gradient_hard_mix_rounded_source_tests.rs"] +mod gradient_hard_mix_rounded_source_tests; + #[cfg(test)] mod tests { use super::*;