Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Black & White: keep 32-bit superwhites (#8537) (#8748)

HEOJUNFO44db720b22b2
diff --git a/crates/compose/src/adjust.rs b/crates/compose/src/adjust.rs
index d122a0999..d03c7a507 100644
--- a/crates/compose/src/adjust.rs
+++ b/crates/compose/src/adjust.rs
@@ -198,8 +198,11 @@ pub fn apply_depth(adj: &Adjustment, buf: &mut Buffer, transfer: Transfer, depth
}
Adjustment::BlackWhite { weights, tint } => {
let hdr = depth == Some(SampleType::F32);
+ // 32-bit greys keep their superwhites: Photoshop 27.11 maps negatives to 0 and caps
+ // neutrals at 16, but leaves 2.0 at 2.0 (#8537).
+ let hi = if hdr { 16.0 } else { 1.0 };
map_rgb(buf, |c| {
- let g = black_white_gray(c, weights);
+ let g = black_white_gray(c, weights).clamp(0.0, hi);
match tint {
// Photoshop tints like the Color blend mode: the tint's hue and saturation at
// the grey's luminosity (SetLum + ClipColor), measured in Photoshop 27.11. In
@@ -744,7 +747,8 @@ pub fn range_lightness(c: [f32; 3], l: f32) -> [f32; 3] {
/// Black & White grey value. Each colour is the sum of a grey part (its smallest channel), a
/// secondary-colour part (yellow, cyan or magenta: middle − smallest) and a primary part (red,
/// green or blue: largest − middle); the sliders (percent, Photoshop defaults 40 60 40 60 20 80 for
-/// reds, yellows, greens, cyans, blues, magentas) weight the two colour parts.
+/// reds, yellows, greens, cyans, blues, magentas) weight the two colour parts. Unclipped: the
+/// document's depth decides the range.
pub fn black_white_gray(c: [f32; 3], weights: &[f32; 6]) -> f32 {
let (r, g, b) = (c[0], c[1], c[2]);
let (max, min) = (r.max(g).max(b), r.min(g).min(b));
@@ -764,7 +768,7 @@ pub fn black_white_gray(c: [f32; 3], weights: &[f32; 6]) -> f32 {
} else {
5 // red + blue = magenta
};
- (min + (mid - min) * weights[secondary] / 100.0 + (max - mid) * weights[primary] / 100.0).clamp(0.0, 1.0)
+ min + (mid - min) * weights[secondary] / 100.0 + (max - mid) * weights[primary] / 100.0
}
/// Ordered-dither offset in -0.5..0.5 (4×4 Bayer matrix) for document pixel (x, y).
@@ -2197,6 +2201,22 @@ mod tone_tests {
assert!(black_white_gray([1.0, 0.0, 0.0], &w) > black_white_gray([1.0, 0.0, 0.0], &d));
}
+ /// #8537: 32-bit superwhites stay above 1 (Photoshop 27.11: 2.0 stays 2.0, neutrals cap at
+ /// 16, negatives go to 0); 8-bit still clips at 1.
+ #[test]
+ fn black_white_keeps_32_bit_superwhites() {
+ let adj = Adjustment::BlackWhite { weights: [40.0, 60.0, 40.0, 60.0, 20.0, 80.0], tint: None };
+ let run = |depth, c: f32| {
+ let mut b = Buffer { rect: Rect::new(0, 0, 1, 1), px: vec![[c, c, c, 1.0]] };
+ apply_depth(&adj, &mut b, Transfer::Srgb, Some(depth));
+ b.px[0][0]
+ };
+ assert_eq!(run(SampleType::F32, 2.0), 2.0);
+ assert_eq!(run(SampleType::F32, 20.0), 16.0);
+ assert_eq!(run(SampleType::F32, -0.5), 0.0);
+ assert_eq!(run(SampleType::U8, 2.0), 1.0);
+ }
+
/// Photoshop 27.11, measured: Black & White (default weights) with Tint over greys. The tint
/// is blended like the Color blend mode: its hue and saturation at the grey's luminosity.
#[test]
diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl
index c10f27d4f..a06996a21 100644
--- a/crates/gpu/src/compose.wgsl
+++ b/crates/gpu/src/compose.wgsl
@@ -751,7 +751,8 @@ fn adjust(c: vec3<f32>) -> vec3<f32> {
if (c.r >= c.g && c.r >= c.b) { primary = 0; } else if (c.g >= c.b) { primary = 2; }
var secondary = 5;
if (c.b <= c.r && c.b <= c.g) { secondary = 1; } else if (c.r <= c.g) { secondary = 3; }
- let g = clamp(mn + (mid - mn) * w[secondary] / 100.0 + (mx - mid) * w[primary] / 100.0, 0.0, 1.0);
+ // 32-bit: superwhites stay, up to Photoshop's 16
+ let g = clamp(mn + (mid - mn) * w[secondary] / 100.0 + (mx - mid) * w[primary] / 100.0, 0.0, select(1.0, 16.0, hdr));
if (p1.z > 0.5) {
if (hdr) { return set_lum(p2.rgb, g, F32_MAX); } // 32-bit: nothing clips at 1
return clamp(set_lum(p2.rgb, g, 1.0), vec3(0.0), vec3(1.0)); // Color blend of the tint
diff --git a/crates/gpu/tests/it/parity.rs b/crates/gpu/tests/it/parity.rs
index 464ad1f1a..c07b43833 100644
--- a/crates/gpu/tests/it/parity.rs
+++ b/crates/gpu/tests/it/parity.rs
@@ -65,6 +65,7 @@ fn hdr_adjustments_preserve_range_and_cpu_parity() {
Adjustment::PhotoFilter { color: [0.97, 0.76, 0.0], density: 0.4, preserve_luminosity: true },
Adjustment::PhotoFilter { color: [0.2, 0.6, 0.9], density: 0.3, preserve_luminosity: false },
Adjustment::BlackWhite { weights: [70.0, 20.0, 50.0, 10.0, 90.0, 30.0], tint: Some([0.9, 0.7, 0.5]) },
+ Adjustment::BlackWhite { weights: [40.0, 60.0, 40.0, 60.0, 20.0, 80.0], tint: None },
] {
let mut d = Document::new("HDR adjustments", Size::new(3, 1), ColorMode::Rgb, SampleType::F32);
let mut layer = Layer::raster("pixels", PixelFormat::RGBA32F);