856f865cf98d
transform_fx_tests.rs
//! Free Transform previews a styled layer with its style (#3653), through the real session and
//! canvas: at every bit depth and colour mode, for pixel layers and groups, following edits made
//! while the box is open, and matching what the commit leaves.
use egui::ColorImage;
use photocraft_doc::{Layer, LayerId};
use serde_json::json;
use crate::PhotocraftApp;
use craft::imaging::ui::transform_preview::StylePlan;
/// A 200 × 160 document with a 40 × 20 blue block at (60, 60) on its own layer, wearing a 4 px
/// red outside Stroke.
fn app(mode: &str, depth: u32) -> Box<PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 200, "height": 160, "mode": mode, "depth": depth})).unwrap();
app.run("layer.new.layer", json!({"name": "styled"})).unwrap();
app.run("select.rect", json!({"x": 60, "y": 60, "width": 40, "height": 20})).unwrap();
app.run("edit.fill", json!({"color": "#0000ff"})).unwrap();
app.run("select.deselect", json!({})).unwrap();
app.run("layer.layerStyle.stroke", json!({"size": 4, "color": "#ff0000", "position": "outside"})).unwrap();
app.sync_views();
app
}
fn begin(app: &mut PhotocraftApp) {
let app_ctx = app.ctx().clone();
crate::transform_tool::begin(app, &app_ctx).unwrap();
}
/// What the box draws for the styled layer at `zoom`: `None` when it draws the bare pixels.
fn composite(app: &PhotocraftApp, zoom: f32) -> Option<(ColorImage, StylePlan)> {
crate::transform_tool::styled_composite(app, zoom)
}
/// The composite at a document point, as unmultiplied `[r, g, b, a]` in 0..=1. The image covers `plan.shown`, in work
/// pixels.
fn at(c: &(ColorImage, StylePlan), x: f64, y: f64) -> [f32; 4] {
let (image, plan) = c;
let px = (x * plan.scale).floor() as i32 - plan.shown.x0;
let py = (y * plan.scale).floor() as i32 - plan.shown.y0;
if px < 0 || py < 0 || px as usize >= image.size[0] || py as usize >= image.size[1] {
return [0.0; 4];
}
image.pixels[py as usize * image.size[0] + px as usize].to_srgba_unmultiplied().map(|v| f32::from(v) / 255.0)
}
fn set_quad(app: &mut PhotocraftApp, quad: [[f64; 2]; 4]) {
app.ui.transform.as_mut().unwrap().edit_box(|b| b.quad = quad.map(|[x, y]| (x, y).into()));
}
fn active(app: &PhotocraftApp) -> LayerId {
app.session.active().unwrap().active_layer.unwrap()
}
fn edit_layer(app: &mut PhotocraftApp, f: impl FnOnce(&mut Layer)) {
let id = active(app);
app.session
.edit("edit", |doc, _| {
f(doc.layer_mut(id).unwrap());
Ok(())
})
.unwrap();
}
/// The effects compose document colours: a monitor change alters the shown texture (the preview cache's rule) but
/// never the composite the effects read.
#[test]
fn styled_texture_tracks_monitor_changes_without_recoloring_effect_inputs() {
let mut app = app("rgb", 16);
begin(&mut app);
let raw = composite(&app, 1.0).unwrap().0;
for monitor in ["srgb", "display-p3", "srgb"] {
app.session.color.settings.monitor_profile = monitor.into();
crate::canvas::testing::frame(&mut app);
assert_eq!(composite(&app, 1.0).unwrap().0.pixels, raw.pixels, "monitor={monitor}: effects still use document colors");
}
}
#[test]
fn the_style_shows_while_the_box_is_open_at_every_depth_and_mode() {
for (mode, depth) in [("rgb", 8), ("rgb", 16), ("rgb", 32), ("gray", 8), ("gray", 16), ("cmyk", 8), ("cmyk", 16), ("lab", 8)] {
let mut app = app(mode, depth);
begin(&mut app);
let c = composite(&app, 1.0).unwrap_or_else(|| panic!("{mode}/{depth}: styled"));
let (fill, stroke) = (at(&c, 80.0, 70.0), at(&c, 57.0, 70.0));
assert!(fill[3] > 0.99 && stroke[3] > 0.99, "{mode}/{depth}: {fill:?} {stroke:?}");
assert!(fill.iter().zip(stroke).any(|(a, b)| (a - b).abs() > 0.1), "{mode}/{depth}: the stroke has its own colour: {fill:?} {stroke:?}");
if mode == "rgb" {
assert!(stroke[0] > 0.9 && stroke[2] < 0.1 && fill[2] > 0.9 && fill[0] < 0.1, "{depth}: {fill:?} {stroke:?}");
}
assert_eq!(at(&c, 53.0, 70.0)[3], 0.0, "{mode}/{depth}: nothing past the stroke");
// Twice the size about the middle: the block is 40..120 × 50..90 and the stroke is
// still 4 px, drawn round the new edge.
set_quad(&mut app, [[40.0, 50.0], [120.0, 50.0], [120.0, 90.0], [40.0, 90.0]]);
let c = composite(&app, 1.0).unwrap();
assert!(at(&c, 80.0, 70.0)[3] > 0.99 && at(&c, 37.0, 70.0)[3] > 0.99 && at(&c, 122.0, 70.0)[3] > 0.99, "{mode}/{depth}");
assert!(at(&c, 37.0, 70.0).iter().zip(stroke).all(|(a, b)| (a - b).abs() < 0.02), "{mode}/{depth}: the same stroke");
assert_eq!(at(&c, 33.0, 70.0)[3], 0.0, "{mode}/{depth}: the stroke did not grow with the box");
assert_eq!(at(&c, 126.0, 70.0)[3], 0.0, "{mode}/{depth}");
}
}
/// The preview is the layer as the commit leaves it: same stroke, same shadow, same place.
#[test]
fn the_preview_matches_the_committed_layer() {
for (mode, depth) in [("rgb", 8), ("rgb", 16), ("rgb", 32), ("gray", 8)] {
let mut app = app(mode, depth);
app.run("layer.layerStyle.dropShadow", json!({"distance": 9, "size": 4, "opacity": 80, "add": true})).unwrap();
begin(&mut app);
// One and a half times the size, turned 20° about the middle of the block (80, 70).
let (sin, cos) = 20f64.to_radians().sin_cos();
let quad = [[-30.0, -15.0], [30.0, -15.0], [30.0, 15.0], [-30.0, 15.0]].map(|[x, y]: [f64; 2]| [80.0 + x * cos - y * sin, 70.0 + x * sin + y * cos]);
set_quad(&mut app, quad);
let (shown, plan) = composite(&app, 1.0).unwrap();
crate::transform_tool::commit(&mut app);
assert!(app.ui.transform.is_none(), "{mode}/{depth}: committed");
// The committed layer alone, over transparency.
let st = app.session.active().unwrap();
let mut solo = (*st.doc).clone();
let id = st.active_layer.unwrap();
for l in solo.layers.iter_mut() {
l.visible = l.id == id;
}
let s = plan.shown;
let committed = photocraft_compose::render(&solo, photocraft_geom::Rect::new(s.x0, s.y0, s.x1, s.y1));
let premultiplied = |p: &[f32; 4]| [p[0] * p[3], p[1] * p[3], p[2] * p[3], p[3]];
let (mut sum, mut off) = (0.0f64, 0usize);
for (a, b) in shown.pixels.iter().zip(&committed.px) {
let a = a.to_array().map(|v| f32::from(v) / 255.0);
let d = a.iter().zip(premultiplied(b)).map(|(x, y)| (x - y).abs()).fold(0.0f32, f32::max);
sum += f64::from(d);
off += usize::from(d > 0.25);
}
let n = shown.pixels.len();
assert_eq!(n, committed.px.len(), "{mode}/{depth}: the preview covers what the commit renders");
assert!(n > 4000, "{mode}/{depth}: {n}");
// The commit resamples bicubic and the preview bilinear, and the preview holds 8 bits, so soft edges differ a
// little; a stroke of another width or a shadow falling elsewhere would differ over whole bands.
assert!(sum / n as f64 <= 0.006, "{mode}/{depth}: mean difference {}", sum / n as f64);
assert!(off * 500 <= n, "{mode}/{depth}: {off} of {n} pixels differ");
// Something is there to compare: fill, stroke and shadow.
assert!(committed.px.iter().filter(|p| p[3] > 0.5).count() > 2000, "{mode}/{depth}");
}
}
/// Groups (like type, shapes and smart objects) preview from a flattened render of the layer,
/// which is read without the layer's own style and opacity: the box applies them itself.
#[test]
fn a_styled_group_previews_with_its_style_once() {
let mut app = app("rgb", 8);
app.session
.edit("group", |doc, active| {
let id = active.unwrap();
let at = doc.layers.iter().position(|l| l.id == id).unwrap();
let mut child = doc.layers.remove(at);
let style = std::mem::take(&mut child.effects.items);
let mut group = Layer::group("group", vec![child]);
group.effects.items = style;
group.opacity = 0.5;
*active = Some(group.id);
doc.layers.insert(at, group);
Ok(())
})
.unwrap();
begin(&mut app);
let pv = app.transform_preview.as_ref().unwrap();
// The texels are the bare pixels over their own bounds: no stroke, no opacity yet.
assert_eq!(pv.textures.size(), [40, 20]);
let (texels, _) = pv.textures.level_image(0).unwrap();
assert!(texels.pixels.iter().all(|p| *p == egui::Color32::from_rgb(0, 0, 255)), "{:?}", texels.pixels.first());
// The composite has the stroke round them, all of it at the group's 50 %.
let c = composite(&app, 1.0).unwrap();
let (fill, stroke) = (at(&c, 80.0, 70.0), at(&c, 57.0, 70.0));
assert!((fill[3] - 0.5).abs() < 0.01 && fill[2] > 0.9, "{fill:?}");
assert!((stroke[3] - 0.5).abs() < 0.01 && stroke[0] > 0.9, "{stroke:?}");
assert_eq!(at(&c, 53.0, 70.0)[3], 0.0);
}
#[test]
fn edits_made_while_the_box_is_open_show_in_the_styled_preview() {
let mut app = app("rgb", 8);
begin(&mut app);
assert!(composite(&app, 1.0).is_some());
// Opacity.
edit_layer(&mut app, |l| l.opacity = 0.3);
let c = composite(&app, 1.0).unwrap();
assert!((at(&c, 57.0, 70.0)[3] - 0.3).abs() < 0.01 && (at(&c, 80.0, 70.0)[3] - 0.3).abs() < 0.01);
// Fill 0 %: the pixels go and the stroke stays (the outline-only look).
edit_layer(&mut app, |l| {
l.opacity = 1.0;
l.fill_opacity = 0.0;
});
let c = composite(&app, 1.0).unwrap();
assert_eq!(at(&c, 80.0, 70.0)[3], 0.0);
assert!(at(&c, 57.0, 70.0)[3] > 0.99);
// A wider stroke.
app.run("layer.layerStyle.stroke", json!({"size": 10, "color": "#ff0000", "position": "outside"})).unwrap();
assert!(at(&composite(&app, 1.0).unwrap(), 52.0, 70.0)[3] > 0.99);
// The style switched off, then the layer hidden: the box draws the bare pixels (or nothing).
edit_layer(&mut app, |l| l.effects.enabled = false);
assert!(composite(&app, 1.0).is_none());
edit_layer(&mut app, |l| l.effects.enabled = true);
assert!(composite(&app, 1.0).is_some());
edit_layer(&mut app, |l| l.visible = false);
assert!(composite(&app, 1.0).is_none());
}
#[test]
fn layers_without_a_style_and_other_boxes_keep_the_plain_preview() {
// No style: nothing to composite, also if one is added once the box is open (its texels
// were not read for it).
let mut plain = app("rgb", 8);
edit_layer(&mut plain, |l| l.effects.items.clear());
begin(&mut plain);
assert!(composite(&plain, 1.0).is_none());
plain.run("layer.layerStyle.stroke", json!({"size": 4, "color": "#ff0000"})).unwrap();
assert!(composite(&plain, 1.0).is_none());
// The selected part of a styled layer: its style belongs to that part and the rest together.
let mut part = app("rgb", 8);
part.run("select.rect", json!({"x": 60, "y": 60, "width": 20, "height": 20})).unwrap();
begin(&mut part);
let r = part.ui.transform.as_ref().unwrap().session.rect();
assert_eq!([r.x0, r.y0, r.x1, r.y1], [60.0, 60.0, 80.0, 80.0]);
assert!(composite(&part, 1.0).is_none());
// Transform Selection moves an outline, not the layer.
let mut outline = app("rgb", 8);
outline.run("select.rect", json!({"x": 60, "y": 60, "width": 20, "height": 20})).unwrap();
crate::transform_tool::begin_selection(&mut outline, &egui::Context::default()).unwrap();
assert!(composite(&outline, 1.0).is_none());
// Warp mode draws its own mesh.
let mut warp = app("rgb", 8);
crate::transform_tool::begin_warp(&mut warp, &egui::Context::default()).unwrap();
assert!(composite(&warp, 1.0).is_none());
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.