mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Keep Color Sampler points on their pixel after flips and turns (#6720)
Found while testing crates/engine/src/commands.rs:117-120 (turn) with crates/engine/src/canvas_geom.rs:224 (Turn::affine); applied in crates/engine/src/lib.rs:807-814 (Session::edit) Co-authored-by: Claude Haiku 5.5 <[email protected]>
This commit is contained in:
1 parent
0296c8e0b5
commit
dfb4aa70ae
2 files changed
+33
-1
No files matched your search
@@ -117,7 +117,10 @@ fn has_selection(s: &Session) -> std::result::Result<(), String> {
|
||||
fn turn(s: &mut Session, label: &str, t: crate::canvas_geom::Turn) -> Result<Value> {
|
||||
if let Some(d) = s.active() {
|
||||
let a = t.affine(f64::from(d.doc.size.width), f64::from(d.doc.size.height));
|
||||
s.set_sampler_map(a);
|
||||
// Sampler points are pixel indices, not pixel edges: map the pixel's centre, so the point
|
||||
// lands on the same pixel `Turn::pixel_map` moves the pixels to (`w - 1 - x`).
|
||||
let half = photocraft_geom::Affine::translate(0.5, 0.5);
|
||||
s.set_sampler_map(half.then(&a).then(&photocraft_geom::Affine::translate(-0.5, -0.5)));
|
||||
}
|
||||
s.edit(label, |doc, _| {
|
||||
crate::canvas_geom::turn_canvas(doc, t);
|
||||
|
||||
@@ -377,6 +377,35 @@ mod tests {
|
||||
assert!((x - 40.0).abs() < 1e-6 && (y - 50.0).abs() < 1e-6, "{pos:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flips_and_right_angle_turns_keep_points_on_their_pixel() {
|
||||
use crate::canvas_geom::Turn;
|
||||
for (id, turn) in [
|
||||
("image.imageRotation.flipCanvasHorizontal", Turn::FlipHorizontal),
|
||||
("image.imageRotation.flipCanvasVertical", Turn::FlipVertical),
|
||||
("image.imageRotation.180", Turn::Rotate180),
|
||||
("image.imageRotation.90cw", Turn::Cw90),
|
||||
("image.imageRotation.90ccw", Turn::Ccw90),
|
||||
] {
|
||||
// A black pixel at (3, 3) of a white 100×80 document, with a point on it.
|
||||
let mut s = session();
|
||||
s.edit("paint", |doc, _| {
|
||||
let surf = doc.layers[0].surface_mut().unwrap();
|
||||
let black = photocraft_raster::from_rgba(&surf.format(), [0.0, 0.0, 0.0, 1.0]);
|
||||
surf.fill_rect(Rect::new(3, 3, 4, 4), &black);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("view.colorSamplers.add", json!({"x": 3, "y": 3})).unwrap();
|
||||
s.execute(id, json!({})).unwrap();
|
||||
let (x, y) = turn.pixel_map(100, 80)(3, 3);
|
||||
let p = points(&mut s);
|
||||
assert_eq!(p.len(), 1, "{id}");
|
||||
assert_eq!(p[0]["position"], json!([f64::from(x), f64::from(y)]), "{id}");
|
||||
assert_eq!(p[0]["rgb"], json!([0, 0, 0]), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_rotated_crop_moves_points() {
|
||||
// The crop frame is centred on the point, so the point lands at the new canvas centre.
|
||||
|
||||
Reference in new issue
Block a user