mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Crop: an Indexed crop keeps to the colour table (#17677)
On an Indexed Color document an expanding crop fills the border with the colour table's entry nearest to the background colour (Euclidean RGB, a tie to the lower index), and a turned crop resamples with Nearest Neighbor and fills the corners the turn uncovers with that same entry, so the result has palette colours only. RGB and the other modes keep their background colour and their bicubic turn. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
0135abd227
commit
ed8d097e96
2 files changed
+156
-3
No files matched your search
@@ -0,0 +1,127 @@
|
||||
//! #17677: cropping an Indexed Color document keeps to its colour table. An expanding crop fills
|
||||
//! the border with the entry nearest to the background colour (Euclidean RGB, a tie to the lower
|
||||
//! index) and a turned crop takes the nearest pixel instead of interpolating; RGB keeps its
|
||||
//! background colour and its bicubic turn.
|
||||
|
||||
use photocraft_color::ColorMode;
|
||||
use photocraft_doc::ColorTable;
|
||||
use photocraft_geom::Rect;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// The measured palette, index 0 to 4.
|
||||
const PALETTE: [[u8; 3]; 5] = [[0, 0, 255], [0, 255, 0], [255, 255, 0], [255, 0, 0], [255, 255, 255]];
|
||||
|
||||
/// 400 x 300 px, one Background in five vertical stripes of the palette's colours (yellow in the
|
||||
/// middle), converted to Indexed Color with the colour table in the measured order.
|
||||
fn indexed() -> Session {
|
||||
let mut s = striped();
|
||||
s.execute("image.mode.indexedColor", json!({"palette": "exact", "dither": "none", "forced": "none"})).unwrap();
|
||||
s.edit("palette order", |doc, _| {
|
||||
assert_eq!(doc.mode, ColorMode::Indexed, "the setup made an Indexed Color document");
|
||||
doc.color_table = Some(ColorTable { colors: PALETTE.to_vec(), transparent: None });
|
||||
doc.color_indices = None;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn striped() -> Session {
|
||||
let mut s = crate::session();
|
||||
s.execute("file.new", json!({"width": 400, "height": 300})).unwrap();
|
||||
s.edit("stripes", |doc, _| {
|
||||
let surf = doc.layers[0].surface_mut().unwrap();
|
||||
for (i, c) in PALETTE.iter().enumerate() {
|
||||
let x = 80 * i as i32;
|
||||
surf.fill_rect(Rect::new(x, 0, x + 80, 300), &c.map(|v| f32::from(v) / 255.0).into_iter().chain([1.0]).collect::<Vec<_>>());
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn set_background(s: &mut Session, c: [u8; 3]) {
|
||||
s.tools.background = [f32::from(c[0]) / 255.0, f32::from(c[1]) / 255.0, f32::from(c[2]) / 255.0, 1.0];
|
||||
}
|
||||
|
||||
fn rgb(s: &Session, x: i32, y: i32) -> [u8; 3] {
|
||||
let p = s.active().unwrap().doc.layers[0].surface().unwrap().pixel(x, y);
|
||||
[0, 1, 2].map(|k| (p[k] * 255.0).round() as u8)
|
||||
}
|
||||
|
||||
/// Every distinct colour of the Background.
|
||||
fn colours(s: &Session) -> Vec<[u8; 3]> {
|
||||
let size = s.active().unwrap().doc.size;
|
||||
let mut seen = Vec::new();
|
||||
for y in 0..size.height as i32 {
|
||||
for x in 0..size.width as i32 {
|
||||
let c = rgb(s, x, y);
|
||||
if !seen.contains(&c) {
|
||||
seen.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
seen
|
||||
}
|
||||
|
||||
fn table(s: &Session) -> Vec<[u8; 3]> {
|
||||
s.active().unwrap().doc.color_table.as_ref().unwrap().colors.clone()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_expanding_crop_fills_the_border_with_the_nearest_palette_entry() {
|
||||
// The measured cases: background colour -> palette index.
|
||||
for (bg, index) in [([0, 255, 0], 1), ([200, 0, 0], 3), ([255, 0, 128], 3), ([128, 128, 128], 4), ([255, 0, 255], 0), ([0, 0, 0], 0)] {
|
||||
let mut s = indexed();
|
||||
set_background(&mut s, bg);
|
||||
s.execute("image.crop", json!({"x": -50, "y": -50, "width": 500, "height": 400})).unwrap();
|
||||
assert_eq!(rgb(&s, 10, 10), PALETTE[index], "background {bg:?}: the border takes index {index}");
|
||||
assert_eq!(rgb(&s, 490, 390), PALETTE[index], "background {bg:?}: the far corner too");
|
||||
assert_eq!(table(&s), PALETTE.to_vec(), "background {bg:?}: the colour table keeps its entries");
|
||||
assert_eq!(s.active().unwrap().doc.mode, ColorMode::Indexed);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_turned_crop_produces_palette_colours_only() {
|
||||
for angle in [10.0, -33.0, 1.5] {
|
||||
let mut s = indexed();
|
||||
set_background(&mut s, [255, 0, 255]);
|
||||
let steps = s.active().unwrap().history.past_len();
|
||||
// A frame that reaches past the canvas once turned, so the uncovered corners show too.
|
||||
s.execute("image.crop", json!({"x": 20, "y": 10, "width": 360, "height": 280, "angle": angle})).unwrap();
|
||||
let found = colours(&s);
|
||||
assert!(found.iter().all(|c| PALETTE.contains(c)), "angle {angle}: colours outside the palette: {found:?}");
|
||||
assert!(found.len() > 2, "angle {angle}: the stripes are still there: {found:?}");
|
||||
let size = s.active().unwrap().doc.size;
|
||||
assert_eq!(rgb(&s, size.width as i32 / 2, size.height as i32 / 2), PALETTE[2], "angle {angle}: the centre stays yellow");
|
||||
assert_eq!(table(&s), PALETTE.to_vec(), "angle {angle}: the colour table keeps its entries");
|
||||
assert_eq!(s.active().unwrap().history.past_len(), steps + 1, "angle {angle}: one history step");
|
||||
assert_eq!(s.active().unwrap().history.entries().last().map(String::as_str), Some("Crop"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_turned_crop_leaves_the_background_colour_as_it_was() {
|
||||
let mut s = indexed();
|
||||
set_background(&mut s, [255, 0, 255]);
|
||||
let before = s.tools.background;
|
||||
s.execute("image.crop", json!({"x": 20, "y": 10, "width": 360, "height": 280, "angle": 10})).unwrap();
|
||||
assert_eq!(s.tools.background, before, "the crop borrows the palette colour only for its own fill");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rgb_keeps_its_border_colour_and_its_interpolated_turn() {
|
||||
let mut s = striped();
|
||||
set_background(&mut s, [255, 0, 255]);
|
||||
s.execute("image.crop", json!({"x": -50, "y": -50, "width": 500, "height": 400})).unwrap();
|
||||
assert_eq!(rgb(&s, 10, 10), [255, 0, 255], "RGB fills the border with the background colour itself");
|
||||
|
||||
let mut s = striped();
|
||||
s.execute("image.crop", json!({"x": 20, "y": 10, "width": 360, "height": 280, "angle": 10})).unwrap();
|
||||
let found = colours(&s);
|
||||
assert!(found.iter().any(|c| !PALETTE.contains(c)), "RGB interpolates the turn: only {found:?}");
|
||||
}
|
||||
@@ -875,7 +875,7 @@ fn crop_with(s: &mut Session, p: &Value, keep_background: bool) -> Result<Value>
|
||||
return Err(EngineError::Other("nothing to crop: pass x/y/width/height or make a selection".into()));
|
||||
}
|
||||
s.set_sampler_map(target.sampler_map(r));
|
||||
let extension = s.tools.background;
|
||||
let extension = crop_extension(s);
|
||||
// Only the Crop tool's frame (an explicit rectangle) resizes a canvas-sized artboard; a
|
||||
// selection crop leaves boards as they are.
|
||||
let tool_frame = explicit.is_some();
|
||||
@@ -1096,10 +1096,17 @@ fn rotated_crop(s: &mut Session, r: Rect, angle: f64, delete: bool, content_awar
|
||||
Ok(())
|
||||
})?;
|
||||
}
|
||||
crate::mode_cmds::rotate_arbitrary_with(s, &json!({"angle": angle, "direction": "ccw"}), false, false)?;
|
||||
// An Indexed document keeps to its colour table: the turn takes the nearest pixel, and the
|
||||
// corners it uncovers get the border's palette colour (#17677).
|
||||
let extension = crop_extension(s);
|
||||
let indexed = s.active().is_some_and(|d| d.doc.mode == ColorMode::Indexed);
|
||||
let interpolation = if indexed { "nearest" } else { "bicubic" };
|
||||
let background = std::mem::replace(&mut s.tools.background, extension);
|
||||
let turned = crate::mode_cmds::rotate_arbitrary_with(s, &json!({"angle": angle, "direction": "ccw", "interpolation": interpolation}), false, false);
|
||||
s.tools.background = background;
|
||||
turned?;
|
||||
// Color Sampler points follow the crop's origin shift too (the rotation moved them above).
|
||||
s.set_sampler_map(crop_target.sampler_map(target));
|
||||
let extension = s.tools.background;
|
||||
s.edit("Crop", |doc, active| {
|
||||
crop_doc(doc, CMD, target, delete, extension)?;
|
||||
fit_canvas_artboard(doc, board);
|
||||
@@ -1116,6 +1123,21 @@ fn rotated_crop(s: &mut Session, r: Rect, angle: f64, delete: bool, content_awar
|
||||
Ok(crop_result(s, json!({ "x": r.x0, "y": r.y0, "width": r.width(), "height": r.height(), "angle": angle })))
|
||||
}
|
||||
|
||||
/// The colour a crop fills the area past the canvas with: the background colour, or on an Indexed
|
||||
/// Color document the colour table's entry nearest to it (Euclidean RGB, a tie to the lower index),
|
||||
/// so the table keeps its entries (#17677).
|
||||
fn crop_extension(s: &Session) -> [f32; 4] {
|
||||
let bg = s.tools.background;
|
||||
let Some(table) = s.active().filter(|d| d.doc.mode == ColorMode::Indexed).and_then(|d| d.doc.color_table.as_ref()) else {
|
||||
return bg;
|
||||
};
|
||||
if table.colors.is_empty() {
|
||||
return bg;
|
||||
}
|
||||
let [r, g, b] = table.rgb(table.nearest([bg[0], bg[1], bg[2]]));
|
||||
[r, g, b, bg[3]]
|
||||
}
|
||||
|
||||
/// The pixels a turned crop reads, in document coordinates before the turn: the source of the
|
||||
/// `target` frame (on the `new` canvas the rotation by `-angle` makes of `old`), grown by the widest
|
||||
/// interpolation kernel's reach so every sample the frame's pixels take is kept.
|
||||
@@ -1505,3 +1527,7 @@ mod canvas_size_noop_history_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod canvas_size_gray_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "crop_indexed_palette_tests.rs"]
|
||||
mod crop_indexed_palette_tests;
|
||||
Reference in new issue
Block a user