Paint: Lock Transparency blends source-atop on semi-transparent pixels (#5783)

With alpha kept (Lock Transparent Pixels, Fill's Preserve Transparency),
the brush, mixer, Fill, Gradient and Paint Bucket composited source-over
and then restored the old alpha, so a half-transparent pixel took the new
colour by as / ao instead of as. They now blend source-atop, as Photoshop
does: Co = (1-as)*Cb + as*B(Cb,Cs), alpha unchanged. New shared helper
blend::composite_atop; over_native's lock branch lerps by the source alpha.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-10-14 23:15:43 +02:00
1 parent 8d840eab59
commit 5781847da3
6 files changed
+125 -17

No files matched your search

+17 -4
View File
@@ -89,10 +89,7 @@ fn composite_area(
let i = ((y - r.y0) as usize * w + (x - r.x0) as usize) * n;
let px = &mut region[i..i + n];
let d = photocraft_raster::to_rgba(&fmt, px);
let mut o = blend::composite(blend_mode, d, color(x, y), k);
if lock_transparency {
o[3] = d[3];
}
let o = if lock_transparency { blend::composite_atop(blend_mode, d, color(x, y), k) } else { blend::composite(blend_mode, d, color(x, y), k) };
from_rgba_into(&fmt, o, &mut enc);
// A colour picked as CMYK inks replaces fully covered pixels of a CMYK target as it is.
if let Some(i) = inks
@@ -296,6 +293,22 @@ mod tests {
assert_eq!(s.pixel(5, 1)[3], 0.0, "outside the selection");
}
#[test]
fn locked_gradient_blends_source_atop_on_semi_transparent_pixels() {
// Locked half-transparent white, flat black gradient at 50%: colour halfway, alpha kept (#5783).
for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] {
for mode in [BlendMode::Normal, BlendMode::Multiply] {
let mut s = Surface::new(fmt);
let a = Rect::new(0, 0, 4, 1);
s.fill_rect(a, &[1.0, 1.0, 1.0, 0.5]);
let black = [0.0, 0.0, 0.0, 1.0];
paint_gradient(&mut s, a, (0.0, 0.0), (4.0, 0.0), GradientShape::Linear, &[(0.0, black), (1.0, black)], false, 0.5, mode, false, None, true);
let p = s.rgba(2, 0);
assert!((p[0] - 0.5).abs() < 0.01 && (p[3] - 0.5).abs() < 0.01, "{fmt:?} {mode:?} {p:?}");
}
}
}
#[test]
fn bucket_fills_contiguous_region() {
let mut s = Surface::new(PixelFormat::RGBA8);
+32
View File
@@ -460,6 +460,22 @@ pub fn composite(mode: BlendMode, backdrop: [f32; 4], source: [f32; 4], opacity:
out
}
/// [`composite`] with the backdrop's alpha kept (Lock Transparent Pixels, Fill's Preserve
/// Transparency): "source atop", `Co = (1−αs)·Cb + αs·B(Cb,Cs)`, `αo = αb`. Unlike source-over
/// followed by restoring the alpha, a half-transparent backdrop takes the new colour by `αs` alone.
pub fn composite_atop(mode: BlendMode, backdrop: [f32; 4], source: [f32; 4], opacity: f32) -> [f32; 4] {
let as_ = source[3] * opacity;
if as_ <= 0.0 {
return backdrop;
}
let b = blend_rgb(mode, [backdrop[0], backdrop[1], backdrop[2]], [source[0], source[1], source[2]]);
let mut out = backdrop;
for i in 0..3 {
out[i] = (1.0 - as_) * backdrop[i] + as_ * b[i];
}
out
}
#[cfg(test)]
mod tests {
use super::*;
@@ -816,4 +832,20 @@ mod tests {
let r = composite(BlendMode::Screen, [0.0, 0.0, 0.0, 0.5], [0.0, 0.0, 0.0, 0.5], 1.0);
assert!(close(r[3], 0.75));
}
#[test]
fn composite_atop_keeps_alpha_and_moves_colour_by_source_alpha() {
let bg = [1.0, 1.0, 1.0, 0.5];
// Half-transparent white, black at 50%: halfway, alpha kept (#5783).
let r = composite_atop(BlendMode::Normal, bg, [0.0, 0.0, 0.0, 1.0], 0.5);
assert!(close(r[0], 0.5) && close(r[2], 0.5) && r[3] == 0.5, "{r:?}");
// Non-Normal modes blend against the backdrop as it is: Multiply by black at 50% halves it.
let r = composite_atop(BlendMode::Multiply, bg, [0.0, 0.0, 0.0, 1.0], 0.5);
assert!(close(r[0], 0.5) && r[3] == 0.5, "{r:?}");
// Opaque backdrops agree with source-over.
let opaque = [0.2, 0.4, 0.6, 1.0];
assert_eq!(composite_atop(BlendMode::Screen, opaque, [0.5, 0.5, 0.5, 0.7], 0.8), composite(BlendMode::Screen, opaque, [0.5, 0.5, 0.5, 0.7], 0.8));
// A transparent source leaves the backdrop.
assert_eq!(composite_atop(BlendMode::Normal, bg, [0.0, 0.0, 0.0, 0.0], 1.0), bg);
}
}
+26 -2
View File
@@ -518,8 +518,17 @@ fn blend_tile(bytes: &mut [u8], fmt: &photocraft_color::PixelFormat, origin: Rec
continue;
}
}
let mut o = if restore && blend == BlendMode::Normal { lerp(d, c, k) } else { photocraft_color::blend::composite(blend, d, c, k) };
if keep_alpha || !fmt.alpha {
let keep = keep_alpha || !fmt.alpha;
let mut o = if restore && blend == BlendMode::Normal {
lerp(d, c, k)
} else if keep {
// Alpha kept: source atop, so a half-transparent pixel takes the new colour by the
// source alpha alone (#5783).
photocraft_color::blend::composite_atop(blend, d, c, k)
} else {
photocraft_color::blend::composite(blend, d, c, k)
};
if keep {
o[3] = d[3];
}
let m = from_rgba_into(fmt, o, &mut enc);
@@ -616,6 +625,21 @@ mod tests {
}));
}
#[test]
fn preserve_transparency_blends_source_atop_on_semi_transparent_pixels() {
// White at 50%, then black at 50% keeping alpha: Photoshop gives (127,127,127,128), the
// colour moved halfway, at every depth and in non-Normal modes too (#5783).
for depth in [8, 16, 32] {
for mode in ["normal", "multiply"] {
let mut s = Session::new();
s.execute("file.new", json!({"width": 8, "height": 8, "depth": depth, "background": "transparent"})).unwrap();
s.execute(CMD, json!({"contents": "white", "opacity": 50})).unwrap();
s.execute(CMD, json!({"contents": "black", "opacity": 50, "mode": mode, "preserveTransparency": true})).unwrap();
assert!(close(px(&s, 3, 3), [0.5, 0.5, 0.5, 0.5]), "{depth} {mode} {:?}", px(&s, 3, 3));
}
}
}
#[test]
fn history_pattern_and_content_aware() {
let mut s = session();
+18
View File
@@ -321,6 +321,24 @@ mod tests {
assert_eq!(s.pixel(70, 10)[3], 0.0);
}
#[test]
fn lock_transparency_blends_source_atop_on_semi_transparent_pixels() {
// Half-transparent white under a black stroke at 50%: the colour moves halfway (Photoshop's
// source-atop), not by the source-over weight 0.5 / 0.75 (#5783).
for f in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] {
for mode in [photocraft_color::BlendMode::Normal, photocraft_color::BlendMode::Multiply] {
let mut s = Surface::new(f);
s.fill_rect(Rect::new(0, 0, 100, 20), &[1.0, 1.0, 1.0, 0.5]);
let mut st = stroke(&[(10.0, 10.0), (90.0, 10.0)], 8.0);
st.brush.opacity = 0.5;
st.brush.mode = mode;
apply_stroke(&mut s, &st, None, true);
let p = s.rgba(50, 10);
assert!((p[0] - 0.5).abs() < 0.01 && (p[3] - 0.5).abs() < 0.01, "{f:?} {mode:?} {p:?}");
}
}
}
#[test]
fn pressure_changes_size() {
let mut st = stroke(&[(0.0, 0.0), (100.0, 0.0)], 20.0);
+5 -4
View File
@@ -1043,10 +1043,11 @@ impl StrokeRenderer {
} else {
let d = to_rgba(&fmt, px);
let sr = to_rgba(&fmt, &src[..n]);
let mut o = photocraft_color::blend::composite(b.mode, d, sr, k);
if lock_transparency {
o[3] = d[3];
}
let o = if lock_transparency {
photocraft_color::blend::composite_atop(b.mode, d, sr, k)
} else {
photocraft_color::blend::composite(b.mode, d, sr, k)
};
let mut enc = [0.0f32; 8];
from_rgba_into(&fmt, o, &mut enc);
px.copy_from_slice(&enc[..n]);
+27 -7
View File
@@ -143,15 +143,20 @@ pub fn over_native(fmt: &PixelFormat, dst: &mut [f32], src: &[f32], k: f32, lock
match alpha_index(fmt) {
Some(a) => {
let (da, sa) = (dst[a], src[a] * k);
if lock_transparency {
// Alpha kept: source atop, the colour moves by the source alpha alone (#5783).
for c in 0..a {
dst[c] += (src[c] - dst[c]) * sa;
}
return;
}
let oa = sa + da * (1.0 - sa);
if oa > 0.0 {
for c in 0..a {
dst[c] = (src[c] * sa + dst[c] * da * (1.0 - sa)) / oa;
}
}
if !lock_transparency {
dst[a] = oa;
}
dst[a] = oa;
}
None => {
for c in 0..dst.len() {
@@ -202,10 +207,8 @@ pub fn apply_coverage(
} else {
let d = to_rgba(&fmt, px);
let s = to_rgba(&fmt, src);
let mut o = photocraft_color::blend::composite(blend, d, s, k);
if lock_transparency {
o[3] = d[3];
}
let o =
if lock_transparency { photocraft_color::blend::composite_atop(blend, d, s, k) } else { photocraft_color::blend::composite(blend, d, s, k) };
let mut enc = [0.0f32; 8];
from_rgba_into(&fmt, o, &mut enc);
px.copy_from_slice(&enc[..n]);
@@ -463,6 +466,23 @@ mod tests {
}
}
#[test]
fn coverage_composite_under_lock_blends_source_atop() {
// Locked half-transparent white, black at coverage 0.5: colour halfway, alpha kept (#5783).
for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] {
for mode in [BlendMode::Normal, BlendMode::Multiply] {
let mut s = Surface::new(fmt);
s.fill_rect(Rect::new(0, 0, 4, 4), &[1.0, 1.0, 1.0, 0.5]);
let r = Rect::new(0, 0, 4, 4);
let mut paint = Region::new(r, 4);
paint.data.chunks_mut(4).for_each(|p| p.copy_from_slice(&[0.0, 0.0, 0.0, 1.0]));
apply_coverage(&mut s, r, &[1.0; 16], 0.5, None, true, &paint, mode);
let p = s.rgba(1, 1);
assert!((p[0] - 0.5).abs() < 0.01 && (p[3] - 0.5).abs() < 0.01, "{fmt:?} {mode:?} {p:?}");
}
}
}
#[test]
fn dab_stroke_runs_sequentially_and_respects_selection() {
let mut s = Surface::new(PixelFormat::GRAY8);