mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge PR #5937: fix(compose): prevent outside stroke from bleeding into opaque character interior (#5861)
This commit is contained in:
commit
a79d8cb9f6
4 files changed
+93
-21
No files matched your search
@@ -2234,23 +2234,8 @@ pub(crate) fn composite_with_effects_prepared(
|
||||
let mut share = vec![0.0f32; w * h];
|
||||
for (i, sh) in share.iter_mut().enumerate() {
|
||||
let band = (out_w + 0.5 - d[i]).clamp(0.0, 1.0);
|
||||
let k = if maps.outline {
|
||||
band * outline_share(shape.v[i], lay.px[i][3])
|
||||
} else if inside(shape.v[i]) {
|
||||
if vector_shape {
|
||||
0.0
|
||||
} else {
|
||||
// The stroke lies outside the layer's pixels. Beneath the layer it may show
|
||||
// only through the part of the pixel the shape doesn't cover (1 - a); the
|
||||
// layer composited on top covers c = a × fill, so the share beneath is
|
||||
// (1 - a) / (1 - c). At 100 % fill that is 1 (the layer hides the rest, as
|
||||
// before); at 0 % fill the interior stays clear (Fill 0 % + Outside stroke).
|
||||
let c = lay.px[i][3].clamp(0.0, 1.0);
|
||||
if c >= 1.0 { 1.0 } else { ((1.0 - shape.v[i].clamp(0.0, 1.0)) / (1.0 - c)).clamp(0.0, 1.0) }
|
||||
}
|
||||
} else {
|
||||
band
|
||||
};
|
||||
let k =
|
||||
if inside(shape.v[i]) { if vector_shape && !maps.outline { 0.0 } else { band * outline_share(shape.v[i], lay.px[i][3]) } } else { band };
|
||||
let mut k = k * st.common.opacity;
|
||||
if st.common.blend == BlendMode::Dissolve {
|
||||
let (x, y) = (i64::from(big.x0) + (i % w) as i64, i64::from(big.y0) + (i / w) as i64);
|
||||
@@ -2339,6 +2324,9 @@ pub(crate) fn composite_with_effects_prepared(
|
||||
/// layer, `share + l (1 - share) = (1 - cov) + l`.
|
||||
pub fn outline_share(cov: f32, l: f32) -> f32 {
|
||||
let outside = (1.0 - cov.clamp(0.0, 1.0)).max(0.0);
|
||||
if outside <= 1e-6 {
|
||||
return 0.0;
|
||||
}
|
||||
let rest = 1.0 - l.clamp(0.0, 1.0);
|
||||
if rest <= 1e-6 { 1.0 } else { (outside / rest).min(1.0) }
|
||||
}
|
||||
|
||||
@@ -92,6 +92,7 @@ fn outline_share_treats_stroke_and_shape_as_disjoint_areas() {
|
||||
// Outside the shape the stroke covers the pixel; inside it nothing.
|
||||
assert_eq!(effects::outline_share(0.0, 0.0), 1.0);
|
||||
assert_eq!(effects::outline_share(1.0, 0.3), 0.0);
|
||||
assert_eq!(effects::outline_share(1.0, 1.0), 0.0);
|
||||
// Beneath an opaque edge pixel the result ends opaque: share + l (1 - share) = 1 - cov + l.
|
||||
let (cov, l) = (0.6, 0.6);
|
||||
let s = effects::outline_share(cov, l);
|
||||
@@ -256,3 +257,69 @@ fn hairline_vector_stroke_keeps_the_files_edge_pixel_colour() {
|
||||
assert!(close4(px(&d, 20, 20), [fill, fill, fill, 1.0]), "{sample:?} {:?}", px(&d, 20, 20));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outside_stroke_on_raster_layer_does_not_paint_inside_opaque_pixels() {
|
||||
let mut d = doc(40, 40);
|
||||
let mut pixels = photocraft_raster::Surface::new(PixelFormat::RGBA8);
|
||||
pixels.fill_rect(Rect::new(15, 15, 25, 25), &[1.0, 0.0, 0.0, 1.0]);
|
||||
let mut l = Layer::new("raster", LayerContent::Raster(pixels));
|
||||
l.effects.items = vec![stroke(3.0, StrokePosition::Outside, blue())];
|
||||
d.layers.push(l);
|
||||
let center = px(&d, 20, 20);
|
||||
assert!(close4(center, [1.0, 0.0, 0.0, 1.0]), "interior must stay red, got: {center:?}");
|
||||
let stroke_pixel = px(&d, 13, 20);
|
||||
assert!(close4(stroke_pixel, [0.0, 0.0, 1.0, 1.0]), "outside stroke must be blue, got: {stroke_pixel:?}");
|
||||
}
|
||||
|
||||
/// Beneath an opaque pixel the Outside stroke has no share, so a Multiply layer blended as a
|
||||
/// group with its interior effects (where the layer's mode meets what lies beneath it inside the
|
||||
/// style) multiplies the backdrop there, not the stroke colour (red × blue would be black), for a
|
||||
/// raster layer and a filled shape alike.
|
||||
#[test]
|
||||
fn outside_stroke_under_multiply_layer_leaves_interior_to_backdrop() {
|
||||
let mut pixels = photocraft_raster::Surface::new(PixelFormat::RGBA8);
|
||||
pixels.fill_rect(Rect::new(15, 15, 25, 25), &[1.0, 0.0, 0.0, 1.0]);
|
||||
let layers = [("raster", Layer::new("raster", LayerContent::Raster(pixels))), ("shape", shape(15.0, 25.0, Fill::Solid(Color::rgb(1.0, 0.0, 0.0)), None))];
|
||||
for (what, mut l) in layers {
|
||||
let mut d = doc(40, 40);
|
||||
l.blend = BlendMode::Multiply;
|
||||
l.advanced.blend_interior = true;
|
||||
l.effects.items = vec![stroke(3.0, StrokePosition::Outside, blue())];
|
||||
d.layers.push(l);
|
||||
let center = px(&d, 20, 20);
|
||||
assert!(close4(center, [1.0, 0.0, 0.0, 1.0]), "{what}: interior must be red × white, got: {center:?}");
|
||||
let stroke_pixel = px(&d, 13, 20);
|
||||
assert!(close4(stroke_pixel, [0.0, 0.0, 1.0, 1.0]), "{what}: outside stroke must be blue, got: {stroke_pixel:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outside_stroke_on_zero_fill_raster_layer_keeps_interior_clear() {
|
||||
let mut d = Document::new("t", Size::new(40, 40), ColorMode::Rgb, SampleType::U8);
|
||||
let mut pixels = photocraft_raster::Surface::new(PixelFormat::RGBA8);
|
||||
pixels.fill_rect(Rect::new(15, 15, 25, 25), &[1.0, 0.0, 0.0, 1.0]);
|
||||
let mut l = Layer::new("raster", LayerContent::Raster(pixels));
|
||||
l.fill_opacity = 0.0;
|
||||
l.effects.items = vec![stroke(3.0, StrokePosition::Outside, blue())];
|
||||
d.layers.push(l);
|
||||
let center = px(&d, 20, 20);
|
||||
assert_eq!(center[3], 0.0, "interior with 0% fill must stay clear: {center:?}");
|
||||
let stroke_pixel = px(&d, 13, 20);
|
||||
assert!(close4(stroke_pixel, [0.0, 0.0, 1.0, 1.0]), "outside stroke must be blue: {stroke_pixel:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outside_stroke_on_partial_fill_raster_layer_keeps_interior_free_of_stroke() {
|
||||
let mut d = Document::new("t", Size::new(40, 40), ColorMode::Rgb, SampleType::U8);
|
||||
let mut pixels = photocraft_raster::Surface::new(PixelFormat::RGBA8);
|
||||
pixels.fill_rect(Rect::new(15, 15, 25, 25), &[1.0, 0.0, 0.0, 1.0]);
|
||||
let mut l = Layer::new("raster", LayerContent::Raster(pixels));
|
||||
l.fill_opacity = 0.5;
|
||||
l.effects.items = vec![stroke(3.0, StrokePosition::Outside, blue())];
|
||||
d.layers.push(l);
|
||||
let center = px(&d, 20, 20);
|
||||
assert!(close4(center, [1.0, 0.0, 0.0, 0.5]), "interior must be 50% red: {center:?}");
|
||||
let stroke_pixel = px(&d, 13, 20);
|
||||
assert!(close4(stroke_pixel, [0.0, 0.0, 1.0, 1.0]), "outside stroke must be blue: {stroke_pixel:?}");
|
||||
}
|
||||
@@ -1187,6 +1187,7 @@ fn fs_fxinit(in: VOut) -> @location(0) vec4<f32> {
|
||||
// beneath a layer of alpha `l` (disjoint areas).
|
||||
fn outline_share(cov: f32, l: f32) -> f32 {
|
||||
let outside = max(1.0 - clamp(cov, 0.0, 1.0), 0.0);
|
||||
if (outside <= 1e-6) { return 0.0; }
|
||||
let rest = 1.0 - clamp(l, 0.0, 1.0);
|
||||
if (rest <= 1e-6) { return 1.0; }
|
||||
return min(outside / rest, 1.0);
|
||||
@@ -1252,13 +1253,11 @@ fn fs_fxstroke(in: VOut) -> @location(0) vec4<f32> {
|
||||
k = k * outline_share(a, lay_a);
|
||||
} else if (a > INSIDE_EPS) {
|
||||
// Pixel layers: the stroke shows beneath only through the part the shape leaves
|
||||
// (`effects.rs`): (1 - a) / (1 - layer alpha), 1 under an opaque layer.
|
||||
// (`effects.rs`): (1 - a) / (1 - layer alpha), none beneath a fully covered pixel.
|
||||
if (vector) {
|
||||
k = 0.0;
|
||||
} else if (lay_a >= 1.0) {
|
||||
k = 1.0;
|
||||
} else {
|
||||
k = clamp((1.0 - clamp(a, 0.0, 1.0)) / (1.0 - lay_a), 0.0, 1.0);
|
||||
k = k * outline_share(a, lay_a);
|
||||
}
|
||||
}
|
||||
var cov = k * op.opacity;
|
||||
|
||||
@@ -1878,6 +1878,24 @@ fn layer_effects_opacity_fill_blend_and_off_canvas() {
|
||||
d.layers.push(l);
|
||||
fx_check(&mut g, &d, &format!("outside stroke at fill {fill}"));
|
||||
}
|
||||
// An Outside stroke has no share beneath fully opaque pixels (#5861): non-Normal layers with
|
||||
// an Outside stroke on a soft blob and a hard square. (Blend Interior Effects as Group, where
|
||||
// that share shows through the layer's mode, is composited on the CPU.)
|
||||
for blend in [BlendMode::Multiply, BlendMode::Screen] {
|
||||
let mut d = fx_doc(80, 80, SampleType::U8);
|
||||
let mut l = blob("ring", d.pixel_format(), 40.0, 40.0, 20.0, [0.9, 0.1, 0.1]);
|
||||
l.effects.items = vec![Effect::Stroke(stroke(4.0, StrokePosition::Outside, FxPaint::Color(Color::rgb(0.1, 0.2, 0.9))))];
|
||||
l.blend = blend;
|
||||
d.layers.push(l);
|
||||
fx_check(&mut g, &d, &format!("outside stroke under a {blend:?} blob"));
|
||||
let mut d = fx_doc(40, 40, SampleType::U8);
|
||||
let mut l = Layer::raster("square", d.pixel_format());
|
||||
l.surface_mut().unwrap().fill_rect(Rect::new(15, 15, 25, 25), &[1.0, 0.0, 0.0, 1.0]);
|
||||
l.effects.items = vec![Effect::Stroke(stroke(3.0, StrokePosition::Outside, FxPaint::Color(Color::rgb(0.0, 0.0, 1.0))))];
|
||||
l.blend = blend;
|
||||
d.layers.push(l);
|
||||
fx_check(&mut g, &d, &format!("outside stroke under a {blend:?} square"));
|
||||
}
|
||||
// Shapes partly off the canvas (their effects reach back in) and a masked effect layer.
|
||||
let mut d = fx_doc(90, 70, SampleType::U8);
|
||||
let mut l = blob("edge", d.pixel_format(), 4.0, 66.0, 18.0, [0.9, 0.9, 0.2]);
|
||||
|
||||
Reference in new issue
Block a user