diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rs index dbb2e57790f..e8eb48d622c 100644 --- a/crates/paint/src/brush_tests.rs +++ b/crates/paint/src/brush_tests.rs @@ -176,9 +176,11 @@ fn pencil_dabs_sit_on_the_pixel_grid() { assert_eq!(s.rgba(x, 9)[3], 1.0, "x {x}"); assert_eq!(s.rgba(x, 8)[3] + s.rgba(x, 10)[3], 0.0, "x {x}"); } - // A 4 px dab stays inside its cursor square. + // A 4 px dab stays inside its cursor square. In 8 bit: a 32-bit target paints a smaller + // aliased tip, nothing at 4 px (#17560). let b = BrushSettings { size: 4.0, ..b }; - let s = paint(&b, &[StrokePoint::new(10.2, 5.7, 1.0)], 24, 16); + let mut s = Surface::new(PixelFormat::RGBA8); + render_stroke(&mut s, &b, &[StrokePoint::new(10.2, 5.7, 1.0)], None, false, 1.0); let [x0, y0, x1, y1] = grid_square(10.2, 5.7, 4.0); for y in 0..16 { for x in 0..24 { diff --git a/crates/paint/src/pencil_32bit_tip_tests.rs b/crates/paint/src/pencil_32bit_tip_tests.rs new file mode 100644 index 00000000000..fd698bde0ca --- /dev/null +++ b/crates/paint/src/pencil_32bit_tip_tests.rs @@ -0,0 +1,177 @@ +//! #17560: the Pencil's hard round tip in 32-bit RGB and Grayscale documents. The reference paints +//! a smaller disc there: its radius is two and a half pixels less than the tip's, so sizes 2 and 4 +//! paint nothing, 1 and 3 one pixel, and 9, 13, 21 and 50 discs 5, 9, 17 and 44 px across, with a +//! one-pixel spike at the top and bottom. 8 and 16 bit are unchanged. +//! +//! The click lands in pixel (499, 299), left of its centre: where the reference's click at +//! (500, 300) puts odd tips (centred on that pixel) and even ones (on its top-left corner). + +use photocraft_color::{ColorMode, PixelFormat, SampleType}; +use photocraft_geom::{Point, Rect}; +use photocraft_raster::Surface; + +use crate::brush::BrushSettings; +use crate::render::{BrushContext, render_stroke}; +use crate::{Dab, StrokePoint}; + +const RGB32: PixelFormat = PixelFormat::RGBA32F; +const GRAY32: PixelFormat = PixelFormat { mode: ColorMode::Grayscale, sample: SampleType::F32, alpha: true }; +const GRAY16: PixelFormat = PixelFormat { mode: ColorMode::Grayscale, sample: SampleType::U16, alpha: true }; + +fn pencil(size: f32) -> BrushSettings { + BrushSettings { aliased: true, hardness: 1.0, size, pressure_size: false, spacing: 0.25, ..Default::default() } +} + +fn click(fmt: PixelFormat, size: f32) -> Surface { + let mut s = Surface::new(fmt); + render_stroke(&mut s, &pencil(size), &[StrokePoint::new(499.4, 299.4, 1.0)], None, false, 1.0); + s +} + +fn painted(s: &Surface, x: i32, y: i32) -> bool { + s.rgba(x, y)[3] > 0.0 +} + +/// The painted pixels' bounds (right and bottom exclusive), or `None` for nothing painted. +fn bounds(s: &Surface) -> Option<[i32; 4]> { + let mut b: Option<[i32; 4]> = None; + for y in 250..350 { + for x in 450..550 { + if painted(s, x, y) { + let r = b.get_or_insert([x, y, x + 1, y + 1]); + *r = [r[0].min(x), r[1].min(y), r[2].max(x + 1), r[3].max(y + 1)]; + } + } + } + b +} + +/// The rows of `s` over `x0..x1`, `y0..y1`, as `#` and `.`. +fn rows(s: &Surface, x0: i32, x1: i32, y0: i32, y1: i32) -> Vec { + (y0..y1).map(|y| (x0..x1).map(|x| if painted(s, x, y) { '#' } else { '.' }).collect()).collect() +} + +/// The reference's bounds per size in 32 bit (`None`: nothing painted). +const BOUNDS_32: [(f32, Option<[i32; 4]>); 8] = [ + (1.0, Some([499, 299, 500, 300])), + (2.0, None), + (3.0, Some([499, 299, 500, 300])), + (4.0, None), + (9.0, Some([497, 297, 502, 302])), + (13.0, Some([495, 295, 504, 304])), + (21.0, Some([491, 291, 508, 308])), + (50.0, Some([477, 277, 521, 321])), +]; + +/// The reference's bounds per size in 8 and 16 bit, which this change keeps. +const BOUNDS_8: [(f32, [i32; 4]); 8] = [ + (1.0, [499, 299, 500, 300]), + (2.0, [498, 298, 500, 300]), + (3.0, [498, 298, 501, 301]), + (4.0, [497, 297, 501, 301]), + (9.0, [495, 295, 504, 304]), + (13.0, [493, 293, 506, 306]), + (21.0, [489, 289, 510, 310]), + (50.0, [474, 274, 524, 324]), +]; + +#[test] +fn thirty_two_bit_click_bounds_match_the_reference() { + for fmt in [RGB32, GRAY32] { + for (size, want) in BOUNDS_32 { + assert_eq!(bounds(&click(fmt, size)), want, "{fmt:?} size {size}"); + } + } +} + +#[test] +fn thirty_two_bit_shapes_match_the_reference() { + for fmt in [RGB32, GRAY32] { + let s = click(fmt, 9.0); + assert_eq!(rows(&s, 494, 507, 297, 302), [".....#.......", "....###......", "...#####.....", "....###......", ".....#......."], "{fmt:?} size 9"); + let s = click(fmt, 13.0); + assert_eq!( + rows(&s, 492, 509, 295, 304), + [ + ".......#.........", + ".....#####.......", + "....#######......", + "....#######......", + "...#########.....", + "....#######......", + "....#######......", + ".....#####.......", + ".......#.........", + ], + "{fmt:?} size 13" + ); + let s = click(fmt, 21.0); + let widths: Vec = rows(&s, 480, 520, 291, 308).iter().map(|r| r.matches('#').count()).collect(); + assert_eq!(widths, [1, 7, 11, 13, 13, 15, 15, 15, 17, 15, 15, 15, 13, 13, 11, 7, 1], "{fmt:?} size 21"); + assert!(painted(&s, 499, 291) && painted(&s, 499, 307), "{fmt:?}: the spikes are at x 499"); + assert!((491..508).all(|x| painted(&s, x, 299)), "{fmt:?}: the wide row is y 299, x 491..507"); + } +} + +#[test] +fn thirty_two_bit_pixels_take_the_full_value() { + let s = click(RGB32, 21.0); + for y in 285..315 { + for x in 485..515 { + let a = s.rgba(x, y)[3]; + assert!(a == 0.0 || a == 1.0, "({x},{y}) = {a}"); + } + } +} + +#[test] +fn thirty_two_bit_drags_along_a_row() { + // Along pixel row 299, where the reference's drag along y 300 lands (see the module docs). + for (size, rows_painted) in [(1.0, Some((299, 300))), (9.0, Some((297, 302))), (2.0, None), (4.0, None)] { + let mut s = Surface::new(RGB32); + render_stroke(&mut s, &pencil(size), &[StrokePoint::new(450.4, 299.4, 1.0), StrokePoint::new(650.4, 299.4, 1.0)], None, false, 1.0); + let b = s.content_bounds(); + match rows_painted { + Some((y0, y1)) => assert_eq!((b.y0, b.y1), (y0, y1), "size {size}: {b:?}"), + None => assert!(b.is_empty() || (b.y0..b.y1).all(|y| (440..660).all(|x| !painted(&s, x, y))), "size {size}: {b:?}"), + } + } +} + +#[test] +fn eight_and_sixteen_bit_are_unchanged() { + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::GRAYA8, GRAY16] { + for (size, want) in BOUNDS_8 { + assert_eq!(bounds(&click(fmt, size)), Some(want), "{fmt:?} size {size}"); + } + } +} + +/// One dab of `b` rasterized for a target of `fmt` (`None`: no format, as before #17560). +fn dab(b: &BrushSettings, d: &Dab, fmt: Option) -> (Rect, Vec) { + let ctx = BrushContext::new(b).for_format(fmt); + let r = ctx.dab_rect(d, false); + let mut v = Vec::new(); + ctx.rasterize(d, false, r, &mut v); + (r, v) +} + +/// Not measured, so unchanged in 32 bit: a soft (anti-aliased) brush, a pencil tip at 50 % +/// roundness or at an angle, and a square pencil tip. Lab and CMYK aren't 32-bit modes here. +#[test] +fn other_tips_keep_their_size_in_32_bit() { + let at = Dab::round(Point::new(499.4, 299.4), 4.5, 1.0); + let cases = [ + (BrushSettings { aliased: false, ..pencil(9.0) }, at), + (pencil(9.0), Dab { roundness: 0.5, ..at }), + (pencil(9.0), Dab { angle: 0.3, ..at }), + (BrushSettings { square: true, ..pencil(9.0) }, at), + ]; + for (b, d) in cases { + for fmt in [RGB32, GRAY32] { + assert_eq!(dab(&b, &d, Some(fmt)), dab(&b, &d, None), "{fmt:?}"); + } + } + // The round pencil tip itself does change. + assert_ne!(dab(&pencil(9.0), &at, Some(RGB32)), dab(&pencil(9.0), &at, None)); +} diff --git a/crates/paint/src/render.rs b/crates/paint/src/render.rs index f0c51fa970c..a8861fa8e89 100644 --- a/crates/paint/src/render.rs +++ b/crates/paint/src/render.rs @@ -130,6 +130,14 @@ pub fn grid_square(x: f64, y: f64, diameter: f32) -> [f64; 4] { [f64::from(cx) - h, f64::from(cy) - h, f64::from(cx) + h, f64::from(cy) + h] } +/// The radius of the aliased (Pencil) round tip of `radius` in a 32-bit RGB or Grayscale +/// document: two and a half pixels less, never below 0. A pixel is painted when its centre lies +/// within it of the dab's [`grid_center`], so sizes 2 and 4 paint nothing, 1 and 3 one pixel, +/// 9 a 5 px disc and 50 a 44 px one, as the reference measures (#17560). +pub fn float_aliased_radius(radius: f32) -> f32 { + if radius.is_finite() { (radius - 2.5).max(0.0) } else { 0.0 } +} + fn smoothstep(e0: f32, e1: f32, x: f32) -> f32 { let t = ((x - e0) / (e1 - e0)).clamp(0.0, 1.0); t * t * (3.0 - 2.0 * t) @@ -214,6 +222,9 @@ pub struct BrushContext { tip: Option>, dual_tip: Option>, texture: Option>, + /// The target is a 32-bit RGB or Grayscale document, where the reference's aliased round tip + /// is smaller ([`float_aliased_radius`]). + float_target: bool, } fn prepare_pattern(b: &BrushSettings) -> Option> { @@ -244,6 +255,13 @@ pub(crate) fn rect_around(d: &Dab, reach: f32) -> Rect { } impl BrushContext { + /// This context for a target of `fmt`: a 32-bit RGB or Grayscale target gets the smaller + /// aliased round tip the reference paints there (#17560). + pub fn for_format(mut self, fmt: Option) -> Self { + self.float_target = fmt.is_some_and(|f| f.sample == SampleType::F32 && matches!(f.mode, ColorMode::Rgb | ColorMode::Grayscale)); + self + } + pub fn new(brush: &BrushSettings) -> Self { let brush = brush.bounded_for_render(); let mips = |t: &TipShape| match t.bitmap() { @@ -255,6 +273,7 @@ impl BrushContext { dual_tip: if brush.dual_brush.enabled { mips(&brush.dual_brush.tip) } else { None }, texture: prepare_pattern(&brush), brush, + float_target: false, } } @@ -354,8 +373,9 @@ impl BrushContext { }); let r = d.radius; let ro = d.roundness.max(0.5 / r).min(1.0); - let rm = (r * ro).max(0.5); - let profile = TipProfile::new(rm, hardness); + let float_round = aliased && self.float_target && mips.is_none() && !b.square && ro >= 1.0 && d.angle == 0.0; + let rm = if float_round { float_aliased_radius(r) } else { (r * ro).max(0.5) }; + let profile = TipProfile::new(rm.max(0.5), hardness); let reach = if aliased || mips.is_some() { rm } else { profile.reach().max(rm) }; let reach2 = (reach + 0.5) * (reach + 0.5); // Sampled-tip mapping. @@ -389,7 +409,7 @@ impl BrushContext { && d.alpha.is_finite() && w > 0 { - rasterize_round(d, rect, out, profile, aliased); + rasterize_round(d, rect, out, profile, aliased, rm); return; } if w == 0 { @@ -941,7 +961,7 @@ impl StrokeRenderer { let per_dab_color = brush.color_dynamics.enabled && !brush.erase && fmt.is_some(); let nc = if per_dab_color { fmt.map_or(0, |f| f.mode.color_channels()) } else { 0 }; Self { - ctx: BrushContext::new(brush), + ctx: BrushContext::new(brush).for_format(fmt), generator: DabGenerator::new(brush, zoom), cov: CoverageMap::new(nc), dual: brush.dual_brush.enabled.then(|| CoverageMap::new(0)), @@ -1451,10 +1471,9 @@ pub fn render_stroke_clipped( // With zero angle, the transform only changes signs. For circles, any rotation preserves // radial distance. Both cases can omit the transform (projection is excluded by the caller). #[inline(never)] -fn rasterize_round(d: &Dab, rect: Rect, out: &mut [f32], profile: TipProfile, aliased: bool) { +fn rasterize_round(d: &Dab, rect: Rect, out: &mut [f32], profile: TipProfile, aliased: bool, rm: f32) { let (cx, cy) = if aliased { grid_center(d.center.x, d.center.y, 2.0 * d.radius) } else { (d.center.x as f32, d.center.y as f32) }; let ro = d.roundness.max(0.5 / d.radius).min(1.0); - let rm = (d.radius * ro).max(0.5); let w = rect.width() as usize; if w == 0 { return; @@ -1656,7 +1675,8 @@ impl GeneralTip<'_> { } let dd = d2.sqrt(); let val = if aliased { if dd <= rm { 1.0 } else { 0.0 } } else { profile.coverage(dd) }; - (val, dd / rm) + // A 32-bit aliased tip can have radius 0 (one pixel or none). + (val, dd / rm.max(0.5)) } Some(m) => { let (px, py) = (u * inv_scale + tw / 2.0, -(v / ro) * inv_scale + th / 2.0); @@ -1703,3 +1723,7 @@ mod budget_tests; #[cfg(test)] #[path = "mip_cache_render_tests.rs"] mod mip_cache_tests; + +#[cfg(test)] +#[path = "pencil_32bit_tip_tests.rs"] +mod pencil_32bit_tip_tests;