Merge #17843 into the merge queue batch

Gradient: no dither noise in 16-bit RGB and Grayscale documents (#17544)
This commit is contained in:
David Mann committed 2026-11-29 12:44:27 +01:00
commit 45aa1db99d
2 files changed
+104

No files matched your search

@@ -0,0 +1,93 @@
//! #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;
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<f32> {
(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 };
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() {
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<f32> = (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<f32> = (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}");
}
}
#[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}");
}
}
+11
View File
@@ -186,6 +186,13 @@ pub fn paint_gradient(
selection: Option<&Surface>,
lock_transparency: bool,
) {
// 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,
@@ -362,6 +369,10 @@ fn rim_ring(region: &crate::selection::Region) -> Vec<bool> {
.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;