PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(compose): cover clipped Dissolve adjustments
Ni-zava221622ad474
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rsindex 38c7dcfcc..cebc6a64a 100644--- a/crates/compose/src/lib.rs+++ b/crates/compose/src/lib.rs@@ -1793,6 +1793,14 @@ fn composite_atop_any(layer: &Layer, base: &mut Buffer, cx: &Ctx) {for (i, p) in base.px.iter_mut().enumerate() {let k = layer.opacity * layer.fill_opacity * mask_k(&mv, i);let a = adjusted.px[i];+ if layer.blend == BlendMode::Dissolve {+ let x = rect.x0 + (i % rect.width() as usize) as i32;+ let y = rect.y0 + (i / rect.width() as usize) as i32;+ if dissolve_noise(x, y) < k {+ *p = [a[0], a[1], a[2], p[3]];+ }+ continue;+ }let bl = blend::blend_rgb(layer.blend, [p[0], p[1], p[2]], [a[0], a[1], a[2]]);for c in 0..3 {p[c] += (bl[c] - p[c]) * k;diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rsindex d02ddb700..1d967e6fc 100644--- a/crates/compose/src/tests.rs+++ b/crates/compose/src/tests.rs@@ -3329,3 +3329,49 @@ fn dissolve_color_overlay_preserves_binary_layer_coverage() {let continuous = render(&d, d.bounds()).px;assert!(continuous.iter().all(|p| (p[0] - 0.5).abs() < E));}++#[test]+fn clipped_dissolve_adjustment_preserves_alpha_and_document_coverage() {+ for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] {+ let mut d = Document::new("clipped adjustment", Size::new(272, 12), ColorMode::Rgb, fmt.sample);+ let bounds = Rect::new(-3, -2, 272, 12);+ let mut base = Layer::raster("base", fmt);+ base.surface_mut().unwrap().fill_rect(bounds, &[1.0, 1.0, 1.0, 0.5]);+ d.layers.push(base);+ let original = render(&d, bounds);+ let mut adjustment = Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert));+ adjustment.clipped = true;+ adjustment.blend = BlendMode::Dissolve;+ adjustment.opacity = 0.6;+ adjustment.fill_opacity = 0.5;+ let mut mask = photocraft_raster::Surface::new(PixelFormat::GRAY8);+ mask.fill_rect(Rect::new(-3, -2, 260, 12), &[1.0]);+ adjustment.mask = Some(LayerMask { surface: mask, enabled: true, linked: true, density: 1.0, feather: 0.0 });+ d.layers.push(adjustment);+ let whole = render(&d, bounds);+ let mut changed = 0;+ for y in bounds.y0..bounds.y1 {+ for x in bounds.x0..bounds.x1 {+ let p = whole.get(x, y);+ let value = if x < 260 && dissolve_noise(x, y) < 0.3 {+ changed += 1;+ 0.0+ } else {+ 1.0+ };+ assert!(close4(p, [value, value, value, original.get(x, y)[3]]), "{fmt:?} at ({x}, {y}): {p:?}");+ }+ }+ assert!(changed > 0 && changed < whole.px.len());+ let region = Rect::new(250, 1, 270, 10);+ let part = render(&d, region);+ for y in region.y0..region.y1 {+ for x in region.x0..region.x1 {+ assert!(close4(part.get(x, y), whole.get(x, y)), "{fmt:?} region at ({x}, {y})");+ }+ }+ d.layers.last_mut().unwrap().blend = BlendMode::Normal;+ let normal = render(&d, bounds);+ assert!(normal.px.iter().any(|p| p[0] > 0.01 && p[0] < 0.99), "Normal keeps continuous mixing");+ }+}diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgslindex 439cbafe3..c49561853 100644--- a/crates/gpu/src/compose.wgsl+++ b/crates/gpu/src/compose.wgsl@@ -851,6 +851,7 @@ fn fs_adjust(in: VOut) -> @location(0) vec4<f32> {}// Mix an adjusted result B back over the original A with blend mode, opacity and mask.+// Both ordinary and clipped adjustments use this pass, retaining the original alpha.@fragmentfn fs_adjmix(in: VOut) -> @location(0) vec4<f32> {let p = local(in.pos);diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rsindex b40d3786a..3f59aa710 100644--- a/crates/gpu/tests/parity.rs+++ b/crates/gpu/tests/parity.rs@@ -2632,7 +2632,6 @@ fn pass_through_clipping_base_with_an_adjustment() {check(&mut g, &d, "pass-through clipping base with an adjustment");}-/// An adjustment has no standalone source alpha: its opacity and mask gate binary coverage.#[test]fn dissolve_adjustment_matches_cpu() {@@ -2671,3 +2670,21 @@ fn dissolve_effects_stack_matches_cpu() {d.layers.push(styled);check(&mut g, &d, "dissolve effects");}++#[test]+fn dissolve_clipped_adjustment_matches_cpu() {+ let Some(mut g) = gpu() else { return };+ for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] {+ let mut d = Document::new("clipped adjustment", Size::new(272, 24), ColorMode::Rgb, fmt.sample);+ let base = noise_layer("base", fmt, Rect::new(-3, -2, 268, 22), 47, 0.0);+ let mut adjustment = Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert));+ adjustment.clipped = true;+ adjustment.blend = BlendMode::Dissolve;+ adjustment.opacity = 0.6;+ adjustment.fill_opacity = 0.5;+ adjustment.mask = Some(mask(Rect::new(0, 0, 260, 24), 48, 0.0));+ d.layers.extend([base, adjustment]);+ check(&mut g, &d, &format!("clipped Dissolve adjustment {fmt:?}"));+ diff_rect(&mut g, &d, Rect::new(250, 1, 270, 20), "clipped adjustment offset region").unwrap_or_else(|e| panic!("{e}"));+ }+}