From 282ac1e70707df8bd243df1035fed25a810bf042 Mon Sep 17 00:00:00 2001 From: qvest-ssels <214787280+qvest-ssels@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:36:38 +0100 Subject: [PATCH] Sharpen: sharpen a layer's transparency too, keeping the colour With Protect Detail off, local_sharpen now applies the colour channels' unsharp-mask step to alpha as well, clamped to 0..1, instead of copying it through. Colour is blurred premultiplied, so a one-colour layer keeps its colour and only alpha moves; opaque-next-to-transparent and opaque layers are unchanged. Protect Detail on, 16- and 32-bit layers and Lock transparent pixels keep their alpha, as the issue leaves them unmeasured. The exact reference values need #12939's 8-neighbour kernel. Part of #12940. --- crates/algo/src/retouch.rs | 9 +- crates/algo/tests/it/retouch_sharpen_exact.rs | 2 +- .../engine-paint/src/cmd/retouch_cmds/dab.rs | 11 +- .../cmd/retouch_cmds/sharpen_alpha_tests.rs | 157 ++++++++++++++++++ 4 files changed, 175 insertions(+), 4 deletions(-) create mode 100644 crates/engine-paint/src/cmd/retouch_cmds/sharpen_alpha_tests.rs diff --git a/crates/algo/src/retouch.rs b/crates/algo/src/retouch.rs index 69ed15194c..a523b77086 100644 --- a/crates/algo/src/retouch.rs +++ b/crates/algo/src/retouch.rs @@ -301,7 +301,12 @@ const PROTECT_DETAIL_LIMIT: f32 = 6.0 / 255.0; /// low halo a few pixels wide, and each difference is soft-limited to `L` = 6 levels: /// `v + amount·d·L/(L + |d|)`. Fine detail is sharpened about as much as without it, while a /// strong edge or a lone bright pixel moves by less than `L` per pass instead of overshooting. -/// Uniform areas stay exactly as they are. Alpha is left unchanged. +/// Uniform areas stay exactly as they are. +/// +/// Without `protect_detail`, alpha is sharpened by the same rule as colour and clamped to 0..1 +/// (#12940): a semi-transparent edge gets crisper while the colour stays, since the colour is +/// blurred premultiplied. An opaque pixel next to a transparent one stays as it is, because the +/// clamp holds both at their limits. With `protect_detail`, alpha is left unchanged (not measured). #[allow(clippy::too_many_arguments)] pub fn local_sharpen( data: &[f32], @@ -332,7 +337,7 @@ pub fn local_sharpen( for c in 0..ch { let v = data[i + c]; if Some(c) == alpha { - dst[o + c] = v; + dst[o + c] = if protect_detail { v } else { (v + amount * (v - blur_row[o + c])).clamp(0.0, 1.0) }; continue; } let d = v - blur_row[o + c]; diff --git a/crates/algo/tests/it/retouch_sharpen_exact.rs b/crates/algo/tests/it/retouch_sharpen_exact.rs index 499b60d6ac..c5a3d8448b 100644 --- a/crates/algo/tests/it/retouch_sharpen_exact.rs +++ b/crates/algo/tests/it/retouch_sharpen_exact.rs @@ -62,7 +62,7 @@ fn scalar_local_sharpen( for c in 0..ch { let v = data[i + c]; if Some(c) == alpha { - out[o + c] = v; + out[o + c] = if protect_detail { v } else { (v + amount * (v - blur[o + c])).clamp(0.0, 1.0) }; continue; } let d = v - blur[o + c]; diff --git a/crates/engine-paint/src/cmd/retouch_cmds/dab.rs b/crates/engine-paint/src/cmd/retouch_cmds/dab.rs index 11dc337871..9b6c4f4a2f 100644 --- a/crates/engine-paint/src/cmd/retouch_cmds/dab.rs +++ b/crates/engine-paint/src/cmd/retouch_cmds/dab.rs @@ -316,6 +316,9 @@ impl DabTool { } Self::Focus { sharpen, strength, protect, sigma } => { let a = alpha_index(&fmt); + // Sharpen sharpens alpha too (#12940), measured at 8 bits only: 16- and 32-bit + // layers keep their alpha as before. + let keep_alpha = if sharpen && fmt.sample != SampleType::U8 { a } else { None }; let mut shares = ProtectDetailShares::default(); Box::new(move |work, fp| { let r = fp.rect.intersect(&work.rect); @@ -348,7 +351,9 @@ impl DabTool { let i = (x - wx0) as usize * n; let px = &mut line[i..i + n]; for c in 0..n { - px[c] += (out[o + c] - px[c]) * k; + if Some(c) != keep_alpha { + px[c] += (out[o + c] - px[c]) * k; + } } } }); @@ -828,3 +833,7 @@ mod smudge_refuses_indexed_tests; #[cfg(test)] #[path = "smudge_mode_tests.rs"] mod smudge_mode_tests; + +#[cfg(test)] +#[path = "sharpen_alpha_tests.rs"] +mod sharpen_alpha_tests; diff --git a/crates/engine-paint/src/cmd/retouch_cmds/sharpen_alpha_tests.rs b/crates/engine-paint/src/cmd/retouch_cmds/sharpen_alpha_tests.rs new file mode 100644 index 0000000000..b8c0664410 --- /dev/null +++ b/crates/engine-paint/src/cmd/retouch_cmds/sharpen_alpha_tests.rs @@ -0,0 +1,157 @@ +//! #12940: with Protect Detail off, the Sharpen tool sharpens a layer's transparency by the same +//! rule as its colour, and keeps the colour. The reference test layer is (200,40,40) at alpha 64 +//! for x 300..399 and alpha 191 for x 400..499, y 300..349, clicked once at 400,320 with a 40 px +//! hard tip at Strength 100 %. +//! +//! The reference's exact alpha (40 at x 399, 215 at x 400, x 398 and 401 unchanged) comes from the +//! 8-neighbour kernel of #12939; until that lands, alpha follows today's colour kernel. + +use serde_json::json; + +use super::*; + +const RED: [f32; 3] = [200.0 / 255.0, 40.0 / 255.0, 40.0 / 255.0]; + +/// An 800 × 600 RGB document of `depth` whose active layer is `f(x, y)` (straight RGBA, 0..1). +fn doc(depth: u64, transparent: bool, f: impl Fn(i32, i32) -> [f32; 4]) -> Session { + let mut s = crate::session(); + let mut p = json!({"width": 800, "height": 600, "depth": depth, "mode": "rgb"}); + if transparent { + p["background"] = json!("transparent"); + } + s.execute("file.new", p).unwrap(); + s.edit("setup", |doc, active| { + let b = doc.bounds(); + let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + let fmt = surf.format(); + let mut data = Vec::new(); + for y in b.y0..b.y1 { + for x in b.x0..b.x1 { + data.extend(photocraft_raster::from_rgba(&fmt, f(x, y))); + } + } + surf.write_region(b, &data); + Ok(()) + }) + .unwrap(); + s +} + +/// The reference layer: red at alpha 64 | 191, transparent elsewhere. +fn steps(depth: u64) -> Session { + doc(depth, true, |x, y| { + let a = match (x, y) { + (300..=399, 300..=349) => 64.0, + (400..=499, 300..=349) => 191.0, + _ => 0.0, + }; + [RED[0], RED[1], RED[2], a / 255.0] + }) +} + +fn click(s: &mut Session, at: [i32; 2], extra: Value) { + let mut p = json!({"points": [at], "size": 40, "hardness": 100, "strength": 100, "protectDetail": false}); + for (k, v) in extra.as_object().unwrap() { + p[k] = v.clone(); + } + s.execute("paint.sharpen", p).unwrap(); +} + +/// The active layer's pixel as 8-bit RGBA. +fn px(s: &Session, x: i32, y: i32) -> [i32; 4] { + let d = s.active().unwrap(); + d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().rgba(x, y).map(|v| (v * 255.0).round() as i32) +} + +fn row(s: &Session, xs: std::ops::Range, y: i32) -> Vec<[i32; 4]> { + xs.map(|x| px(s, x, y)).collect() +} + +#[test] +fn a_click_sharpens_the_alpha_boundary_and_keeps_the_colour() { + let mut s = steps(8); + click(&mut s, [400, 320], json!({})); + let got = row(&s, 397..403, 320); + for p in &got { + assert!((p[0] - 200).abs() <= 1 && (p[1] - 40).abs() <= 1 && (p[2] - 40).abs() <= 1, "colour must stay 200,40,40: {got:?}"); + } + let alpha: Vec = got.iter().map(|p| p[3]).collect(); + // The low side goes down and the high side up, as with colour (the reference: 64 · 64 · 40 · + // 215 · 191 · 191 with #12939's kernel). Pinned from today's sigma-1 kernel. + assert!(alpha[2] < 64 && alpha[3] > 191, "alpha boundary not sharpened: {alpha:?}"); + assert_eq!(alpha, PINNED_ALPHA, "alpha x 397..402"); +} + +/// Alpha at x 397..402 after the reference click, with today's kernel. +const PINNED_ALPHA: [i32; 6] = [63, 57, 26, 229, 198, 192]; + +#[test] +fn opaque_next_to_transparent_stays_as_it_is() { + // A red square on an otherwise empty layer; a click on its left edge. + let mut s = doc(8, true, |x, y| { + let a = if (100..200).contains(&x) && (100..200).contains(&y) { 1.0 } else { 0.0 }; + [RED[0], RED[1], RED[2], a] + }); + let before = row(&s, 80..120, 150); + click(&mut s, [100, 150], json!({})); + let after = row(&s, 80..120, 150); + for (b, a) in before.iter().zip(&after) { + assert_eq!(b[3], a[3], "alpha changed: {before:?} -> {after:?}"); + if b[3] == 255 { + assert_eq!(b, a, "an opaque pixel changed: {before:?} -> {after:?}"); + } + } +} + +#[test] +fn an_opaque_layer_keeps_todays_result() { + // The epic's grey 96 | 160 step on the Background: pinned from main 038d035110. + let mut s = doc(8, false, |x, _| { + let g = if x < 400 { 96.0 } else { 160.0 } / 255.0; + [g, g, g, 1.0] + }); + click(&mut s, [400, 320], json!({})); + let grey: Vec = row(&s, 396..404, 320).iter().map(|p| p[0]).collect(); + assert_eq!(grey, [96, 96, 92, 77, 179, 164, 160, 160]); +} + +#[test] +fn lock_transparent_pixels_keeps_the_alpha() { + let mut s = steps(8); + s.execute("layer.setProps", json!({"locks": {"transparency": true}})).unwrap(); + let before = row(&s, 390..410, 320); + click(&mut s, [400, 320], json!({})); + let after = row(&s, 390..410, 320); + assert_eq!(before.iter().map(|p| p[3]).collect::>(), after.iter().map(|p| p[3]).collect::>()); +} + +#[test] +fn protect_detail_keeps_the_alpha() { + // Protect Detail on across a semi-transparent boundary was not measured: alpha stays. + let mut s = steps(8); + let before = row(&s, 390..410, 320); + click(&mut s, [400, 320], json!({"protectDetail": true})); + let after = row(&s, 390..410, 320); + assert_eq!(before.iter().map(|p| p[3]).collect::>(), after.iter().map(|p| p[3]).collect::>()); +} + +#[test] +fn sixteen_and_32_bit_keep_the_alpha() { + // Alpha at 16 and 32 bit was not measured: it stays as it was. + for depth in [16, 32] { + let mut s = steps(depth); + let before = row(&s, 390..410, 320); + click(&mut s, [400, 320], json!({})); + let after = row(&s, 390..410, 320); + assert_eq!(before.iter().map(|p| p[3]).collect::>(), after.iter().map(|p| p[3]).collect::>(), "{depth}-bit"); + } +} + +#[test] +fn blur_is_unchanged() { + // Blur already blurred alpha; this change is Sharpen's alone. + let mut s = steps(16); + s.execute("paint.blur", json!({"points": [[400, 320]], "size": 40, "hardness": 100, "strength": 100})).unwrap(); + let alpha: Vec = row(&s, 397..403, 320).iter().map(|p| p[3]).collect(); + assert!(alpha[2] > 64 && alpha[3] < 191, "16-bit Blur still blurs alpha: {alpha:?}"); +}