mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #14603 from davidmann4/fix/14473-apply-image-preserve-transparency
Apply Image: Preserve Transparency blends fully and keeps the alpha (#14473)
This commit is contained in:
2 files changed
+122
-7
No files matched your search
@@ -0,0 +1,104 @@
|
||||
//! #14473: Apply Image with Preserve Transparency gives a semi-transparent pixel the full blend
|
||||
//! result and keeps its alpha, as the reference application does.
|
||||
|
||||
use crate::Session;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
/// Target "target" (document 0): white Background, layer "Top" transparent except a 0/255/0
|
||||
/// square at alpha 128 over (10,4)-(19,13). Source "source" (document 1): 128/128/128.
|
||||
fn session(depth: u32) -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 40, "height": 20, "depth": depth, "name": "target"})).unwrap();
|
||||
s.execute("file.new", json!({"width": 40, "height": 20, "depth": depth, "name": "source"})).unwrap();
|
||||
s.execute("edit.fill", json!({"color": "#808080"})).unwrap();
|
||||
s.set_active(0);
|
||||
s.execute("layer.new.layer", json!({"name": "Top"})).unwrap();
|
||||
s.execute("select.rect", json!({"x": 10, "y": 4, "width": 10, "height": 10, "mode": "replace"})).unwrap();
|
||||
s.execute("edit.fill", json!({"color": "#00ff00", "opacity": 50})).unwrap();
|
||||
s.execute("select.deselect", json!({})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn px(s: &Session, x: i32, y: i32) -> Vec<f32> {
|
||||
let d = s.active().unwrap();
|
||||
d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().pixel(x, y)
|
||||
}
|
||||
|
||||
fn apply(s: &mut Session, extra: Value) {
|
||||
let mut p = json!({"source": {"document": 1, "layer": "merged", "channel": "composite"}});
|
||||
if let (Some(p), Some(extra)) = (p.as_object_mut(), extra.as_object()) {
|
||||
p.extend(extra.clone());
|
||||
}
|
||||
s.execute("image.applyImage", p).unwrap();
|
||||
}
|
||||
|
||||
/// Checks RGBA in 0..=255 levels, one level of slack (the fill's 50% alpha is 127.5).
|
||||
fn assert_rgba(got: &[f32], want: [f32; 4], what: &str) {
|
||||
let got: Vec<f32> = got.iter().map(|v| v * 255.0).collect();
|
||||
assert!(got.iter().zip(want).all(|(g, w)| (g - w).abs() <= 1.01), "{what}: {got:?}, want {want:?}");
|
||||
}
|
||||
|
||||
const GRAY: f32 = 128.0;
|
||||
const HALF: f32 = 127.5;
|
||||
|
||||
#[test]
|
||||
fn normal_with_preserve_transparency_takes_the_source_and_keeps_alpha() {
|
||||
for depth in [8, 16, 32] {
|
||||
let mut s = session(depth);
|
||||
apply(&mut s, json!({"blending": "normal", "preserveTransparency": true}));
|
||||
assert_rgba(&px(&s, 15, 8), [GRAY, GRAY, GRAY, HALF], &format!("depth {depth}: green pixel"));
|
||||
assert_rgba(&px(&s, 2, 2), [0.0; 4], &format!("depth {depth}: transparent pixel"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiply_with_preserve_transparency_blends_fully_and_keeps_alpha() {
|
||||
for depth in [8, 16, 32] {
|
||||
let mut s = session(depth);
|
||||
apply(&mut s, json!({"blending": "multiply", "preserveTransparency": true}));
|
||||
assert_rgba(&px(&s, 15, 8), [0.0, GRAY, 0.0, HALF], &format!("depth {depth}: green pixel"));
|
||||
assert_rgba(&px(&s, 2, 2), [0.0; 4], &format!("depth {depth}: transparent pixel"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opacity_and_selection_still_scale_the_preserved_blend() {
|
||||
let mut s = session(8);
|
||||
// 50% Normal: the colour moves half way to the source, the alpha stays.
|
||||
apply(&mut s, json!({"blending": "normal", "opacity": 50, "preserveTransparency": true}));
|
||||
assert_rgba(&px(&s, 15, 8), [64.0, 191.5, 64.0, HALF], "opacity 50");
|
||||
s.undo();
|
||||
// Outside the selection nothing changes.
|
||||
s.execute("select.rect", json!({"x": 10, "y": 4, "width": 5, "height": 10, "mode": "replace"})).unwrap();
|
||||
apply(&mut s, json!({"blending": "normal", "preserveTransparency": true}));
|
||||
assert_rgba(&px(&s, 12, 8), [GRAY, GRAY, GRAY, HALF], "inside the selection");
|
||||
assert_rgba(&px(&s, 17, 8), [0.0, 255.0, 0.0, HALF], "outside the selection");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mask_still_scales_the_preserved_blend() {
|
||||
let mut s = session(8);
|
||||
// A 50% gray alpha channel as the mask halves the effect, like 50% opacity.
|
||||
s.execute("channel.new", json!({"fill": "white"})).unwrap();
|
||||
s.execute("channel.target", json!({"channel": 0})).unwrap();
|
||||
s.execute("edit.fill", json!({"color": "#808080"})).unwrap();
|
||||
s.execute("channel.target", json!({"channel": "composite"})).unwrap();
|
||||
apply(&mut s, json!({"blending": "normal", "preserveTransparency": true, "mask": {"document": 0, "channel": 0}}));
|
||||
assert_rgba(&px(&s, 15, 8), [64.0, 191.5, 64.0, HALF], "mask at 50%");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn without_preserve_transparency_the_results_stay() {
|
||||
let mut s = session(8);
|
||||
apply(&mut s, json!({"blending": "normal"}));
|
||||
assert_rgba(&px(&s, 15, 8), [GRAY, GRAY, GRAY, 255.0], "normal, green pixel");
|
||||
assert_rgba(&px(&s, 2, 2), [GRAY, GRAY, GRAY, 255.0], "normal, transparent pixel");
|
||||
s.undo();
|
||||
apply(&mut s, json!({"blending": "multiply"}));
|
||||
assert_rgba(&px(&s, 15, 8), [64.0, GRAY, 64.0, 255.0], "multiply, green pixel");
|
||||
assert_rgba(&px(&s, 2, 2), [GRAY, GRAY, GRAY, 255.0], "multiply, transparent pixel");
|
||||
s.undo();
|
||||
apply(&mut s, json!({"blending": "normal", "opacity": 50}));
|
||||
assert_rgba(&px(&s, 15, 8), [85.0, 170.0, 85.0, 191.5], "normal 50%, green pixel");
|
||||
assert_rgba(&px(&s, 2, 2), [GRAY, GRAY, GRAY, HALF], "normal 50%, transparent pixel");
|
||||
}
|
||||
@@ -4,6 +4,10 @@ use super::refs::{Blending, bad, doc_index, mask_plane, source, source_spec};
|
||||
use super::targeting::{Target, is_channel_target, is_spot_target, target_of, target_surface};
|
||||
use super::{AlphaChannel, ColorMode, EngineError, Result, SelectionMode, Session, Value, json, sel};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "apply_image_preserve_tests.rs"]
|
||||
mod apply_image_preserve_tests;
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Apply Image / Calculations
|
||||
|
||||
@@ -66,11 +70,9 @@ pub(super) fn apply_image(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mut v = surf.read_region(area);
|
||||
for (i, px) in v.chunks_exact_mut(n).enumerate() {
|
||||
let base_a = if sf.alpha { px[colors].clamp(0.0, 1.0) } else { 1.0 };
|
||||
let mut w = weight(i);
|
||||
if preserve {
|
||||
w *= base_a;
|
||||
}
|
||||
if w <= 0.0 {
|
||||
let w = weight(i);
|
||||
// Preserve Transparency leaves transparent pixels as they are.
|
||||
if w <= 0.0 || (preserve && base_a <= 0.0) {
|
||||
continue;
|
||||
}
|
||||
// Painted over the layer like a brush: the blend only acts where the layer has
|
||||
@@ -85,8 +87,17 @@ pub(super) fn apply_image(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let k = if src_mode == ColorMode::Cmyk && sf.mode != ColorMode::Cmyk { src.get(3).map_or(1.0, |pl| tone(src_mode, pl[i])) } else { 1.0 };
|
||||
let top = if src.len() > 1 { tone(src_mode, src.get(c).unwrap_or(&src[0])[i]) * k } else { src[0][i] };
|
||||
let base = tone(sf.mode, *b);
|
||||
let blended = top + (blending.apply(base, top) - top) * base_a;
|
||||
*b = tone(sf.mode, if preserve { base + (blended - base) * w } else { (w * blended + base_a * (1.0 - w) * base) / out_a });
|
||||
// With Preserve Transparency the colour takes the full blend and only the alpha
|
||||
// stays the layer's (a pixel at 50% alpha turns fully to the source in Normal).
|
||||
*b = tone(
|
||||
sf.mode,
|
||||
if preserve {
|
||||
base + (blending.apply(base, top) - base) * w
|
||||
} else {
|
||||
let blended = top + (blending.apply(base, top) - top) * base_a;
|
||||
(w * blended + base_a * (1.0 - w) * base) / out_a
|
||||
},
|
||||
);
|
||||
}
|
||||
if sf.alpha {
|
||||
px[colors] = out_a;
|
||||
|
||||
Reference in new issue
Block a user