From 8b531863a362427bb07bae57c06318373161fba1 Mon Sep 17 00:00:00 2001 From: qvest-ssels <214787280+qvest-ssels@users.noreply.github.com> Date: Sun, 29 Nov 2026 10:05:27 +0100 Subject: [PATCH 1/3] Gradient: no dither noise in 16-bit documents (#17544) The Gradient tool's dither added one 8-bit step of noise per pixel in every depth. In 16-bit documents the reference's dither on and off give the same smooth ramp, so 16-bit RGB and Grayscale now paint without it. 8 and 32 bit keep their noise. --- .../algo/src/gradient_dither_16bit_tests.rs | 66 +++++++++++++++++++ crates/algo/src/paint.rs | 7 ++ 2 files changed, 73 insertions(+) create mode 100644 crates/algo/src/gradient_dither_16bit_tests.rs diff --git a/crates/algo/src/gradient_dither_16bit_tests.rs b/crates/algo/src/gradient_dither_16bit_tests.rs new file mode 100644 index 0000000000..272f0169b9 --- /dev/null +++ b/crates/algo/src/gradient_dither_16bit_tests.rs @@ -0,0 +1,66 @@ +//! #17544: the Gradient tool's dither adds no noise in 16-bit documents, where the reference's +//! dither on and dither off give the same smooth ramp. 8 bit keeps its one-step noise, and 32 bit +//! keeps today's behaviour (not measured). + +use photocraft_color::{ColorMode, PixelFormat, SampleType}; +use photocraft_raster::Surface; + +use super::{BlendMode, GradientShape, Rect, paint_gradient}; + +/// A black → white Linear gradient over 800 × 600, from (100, 300) to (700, 300), in `fmt`. +fn painted(fmt: PixelFormat, dither: bool) -> Surface { + let mut s = Surface::new(fmt); + let stops = [(0.0, [0.0, 0.0, 0.0, 1.0]), (1.0, [1.0, 1.0, 1.0, 1.0])]; + paint_gradient(&mut s, Rect::new(0, 0, 800, 600), (100.0, 300.0), (700.0, 300.0), GradientShape::Linear, &stops, false, 1.0, BlendMode::Normal, dither, None, false); + s +} + +/// The pixels of a 32 × 32 patch around the ramp's middle, all channels. +fn patch(s: &Surface) -> Vec { + (290..322).flat_map(|y| (384..416).flat_map(move |x| s.pixel(x, y))).collect() +} + +const RGB16: PixelFormat = PixelFormat::RGBA16; +const GRAY16: PixelFormat = PixelFormat { mode: ColorMode::Grayscale, sample: SampleType::U16, alpha: true }; +const GRAY32: PixelFormat = PixelFormat { mode: ColorMode::Grayscale, sample: SampleType::F32, alpha: true }; + +#[test] +fn rgb_16_bit_dither_on_paints_what_dither_off_paints() { + assert_eq!(patch(&painted(RGB16, true)), patch(&painted(RGB16, false))); +} + +#[test] +fn rgb_16_bit_dithered_ramp_is_smooth() { + // Along the ramp every step is the same small rise; down a column nothing changes. + let s = painted(RGB16, true); + let row: Vec = (400..406).map(|x| s.pixel(x, 300)[0] * 255.0).collect(); + for w in row.windows(2) { + let step = w[1] - w[0]; + assert!((0.3..0.55).contains(&step), "x row {row:?}"); + } + let col: Vec = (300..306).map(|y| s.pixel(400, y)[0]).collect(); + assert!(col.iter().all(|v| *v == col[0]), "column {col:?}"); +} + +#[test] +fn gray_16_bit_follows_rgb_16_bit() { + assert_eq!(patch(&painted(GRAY16, true)), patch(&painted(GRAY16, false))); +} + +#[test] +fn eight_bit_still_dithers() { + for fmt in [PixelFormat::RGBA8, PixelFormat::GRAYA8] { + assert_ne!(patch(&painted(fmt, true)), patch(&painted(fmt, false)), "{fmt:?}"); + } +} + +#[test] +fn thirty_two_bit_keeps_its_noise() { + for fmt in [PixelFormat::RGBA32F, GRAY32] { + let (on, off) = (patch(&painted(fmt, true)), patch(&painted(fmt, false))); + assert_ne!(on, off, "{fmt:?}"); + // Today's amplitude: at most half an 8-bit step either way. + let most = on.iter().zip(&off).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(most <= 0.5 / 255.0 + 1e-6, "{fmt:?}: {most}"); + } +} diff --git a/crates/algo/src/paint.rs b/crates/algo/src/paint.rs index fb7f005106..042209fac2 100644 --- a/crates/algo/src/paint.rs +++ b/crates/algo/src/paint.rs @@ -186,6 +186,9 @@ pub fn paint_gradient( selection: Option<&Surface>, lock_transparency: bool, ) { + // 16-bit documents get no dither noise: the reference's dither on and off give the same + // smooth ramp there (#17544). 8 and 32 bit keep the one 8-bit step of noise. + let dither = dither && s.format().sample != photocraft_color::SampleType::U16; composite_area( s, area, @@ -362,6 +365,10 @@ fn rim_ring(region: &crate::selection::Region) -> Vec { .collect() } +#[cfg(test)] +#[path = "gradient_dither_16bit_tests.rs"] +mod gradient_dither_16bit_tests; + #[cfg(test)] #[path = "paint_gradient_sample_tests.rs"] mod paint_gradient_sample_tests; From e4be0abd1a307853212602b063745d7e82075f7d Mon Sep 17 00:00:00 2001 From: qvest-ssels <214787280+qvest-ssels@users.noreply.github.com> Date: Sun, 29 Nov 2026 10:18:09 +0100 Subject: [PATCH 2/3] Gradient dither tests: cargo fmt --- crates/algo/src/gradient_dither_16bit_tests.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/algo/src/gradient_dither_16bit_tests.rs b/crates/algo/src/gradient_dither_16bit_tests.rs index 272f0169b9..48da35d031 100644 --- a/crates/algo/src/gradient_dither_16bit_tests.rs +++ b/crates/algo/src/gradient_dither_16bit_tests.rs @@ -11,7 +11,20 @@ use super::{BlendMode, GradientShape, Rect, paint_gradient}; fn painted(fmt: PixelFormat, dither: bool) -> Surface { let mut s = Surface::new(fmt); let stops = [(0.0, [0.0, 0.0, 0.0, 1.0]), (1.0, [1.0, 1.0, 1.0, 1.0])]; - paint_gradient(&mut s, Rect::new(0, 0, 800, 600), (100.0, 300.0), (700.0, 300.0), GradientShape::Linear, &stops, false, 1.0, BlendMode::Normal, dither, None, false); + paint_gradient( + &mut s, + Rect::new(0, 0, 800, 600), + (100.0, 300.0), + (700.0, 300.0), + GradientShape::Linear, + &stops, + false, + 1.0, + BlendMode::Normal, + dither, + None, + false, + ); s } From 269175bfe26766b856f44f4d9520cb5b85825ad9 Mon Sep 17 00:00:00 2001 From: qvest-ssels <214787280+qvest-ssels@users.noreply.github.com> Date: Sun, 29 Nov 2026 11:33:00 +0100 Subject: [PATCH 3/3] Gradient dither: skip the noise only in 16-bit RGB and Grayscale (#17544) The issue measured RGB 16 and Grayscale 16. CMYK 16 and Lab 16 were not measured, so they keep today's dither; a test checks that they still do. --- .../algo/src/gradient_dither_16bit_tests.rs | 20 ++++++++++++++++--- crates/algo/src/paint.rs | 10 +++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/crates/algo/src/gradient_dither_16bit_tests.rs b/crates/algo/src/gradient_dither_16bit_tests.rs index 48da35d031..64736f455f 100644 --- a/crates/algo/src/gradient_dither_16bit_tests.rs +++ b/crates/algo/src/gradient_dither_16bit_tests.rs @@ -1,6 +1,6 @@ -//! #17544: the Gradient tool's dither adds no noise in 16-bit documents, where the reference's -//! dither on and dither off give the same smooth ramp. 8 bit keeps its one-step noise, and 32 bit -//! keeps today's behaviour (not measured). +//! #17544: the Gradient tool's dither adds no noise in 16-bit RGB and Grayscale documents, where +//! the reference's dither on and dither off give the same smooth ramp. 8 bit keeps its one-step +//! noise, and 32 bit and 16-bit CMYK and Lab keep today's behaviour (not measured). use photocraft_color::{ColorMode, PixelFormat, SampleType}; use photocraft_raster::Surface; @@ -36,6 +36,8 @@ fn patch(s: &Surface) -> Vec { const RGB16: PixelFormat = PixelFormat::RGBA16; const GRAY16: PixelFormat = PixelFormat { mode: ColorMode::Grayscale, sample: SampleType::U16, alpha: true }; const GRAY32: PixelFormat = PixelFormat { mode: ColorMode::Grayscale, sample: SampleType::F32, alpha: true }; +const CMYK16: PixelFormat = PixelFormat { mode: ColorMode::Cmyk, sample: SampleType::U16, alpha: true }; +const LAB16: PixelFormat = PixelFormat { mode: ColorMode::Lab, sample: SampleType::U16, alpha: true }; #[test] fn rgb_16_bit_dither_on_paints_what_dither_off_paints() { @@ -77,3 +79,15 @@ fn thirty_two_bit_keeps_its_noise() { assert!(most <= 0.5 / 255.0 + 1e-6, "{fmt:?}: {most}"); } } + +#[test] +fn cmyk_and_lab_16_bit_keep_their_noise() { + // Not measured in the issue, so these keep today's dither. The noise is added to the RGB + // colour before the conversion, so a channel moves by less than one 8-bit step. + for fmt in [CMYK16, LAB16] { + let (on, off) = (patch(&painted(fmt, true)), patch(&painted(fmt, false))); + assert_ne!(on, off, "{fmt:?}"); + let most = on.iter().zip(&off).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(most < 1.0 / 255.0, "{fmt:?}: {most}"); + } +} diff --git a/crates/algo/src/paint.rs b/crates/algo/src/paint.rs index 042209fac2..8ae73b48a7 100644 --- a/crates/algo/src/paint.rs +++ b/crates/algo/src/paint.rs @@ -186,9 +186,13 @@ pub fn paint_gradient( selection: Option<&Surface>, lock_transparency: bool, ) { - // 16-bit documents get no dither noise: the reference's dither on and off give the same - // smooth ramp there (#17544). 8 and 32 bit keep the one 8-bit step of noise. - let dither = dither && s.format().sample != photocraft_color::SampleType::U16; + // 16-bit RGB and Grayscale get no dither noise: the reference's dither on and off give the + // same smooth ramp there (#17544). Other 16-bit models (CMYK, Lab) weren't measured and keep + // the noise, as do 8 and 32 bit. + let fmt = s.format(); + 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; composite_area( s, area,