mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Limit Lab Replace Color work to selected bounds
This commit is contained in:
1 parent
be36a01846
commit
75ab50544e
2 files changed
+47
-5
No files matched your search
@@ -2,6 +2,8 @@
|
||||
use photocraft_algo::{selection, tone};
|
||||
use photocraft_cms::{Builtin, TransformOptions};
|
||||
use photocraft_doc::SampleType;
|
||||
use photocraft_geom::Rect;
|
||||
use photocraft_raster::Surface;
|
||||
use serde_json::Value;
|
||||
|
||||
use super::{bad, num};
|
||||
@@ -9,6 +11,14 @@ use crate::{EngineError, Result, Session};
|
||||
|
||||
const CMD: &str = "image.adjustments.replaceColor";
|
||||
|
||||
/// Unstored pixels can still be selected when the mask has a nonzero default.
|
||||
fn edit_bounds(content: Rect, selected: Option<&Surface>) -> Rect {
|
||||
match selected {
|
||||
Some(mask) if mask.default_pixel().first() == Some(&0.0) => content.intersect(&mask.content_bounds()),
|
||||
_ => content,
|
||||
}
|
||||
}
|
||||
|
||||
/// Validate original precision before checking order and converting to native f32.
|
||||
fn bound(p: &Value, key: &str) -> Result<[f64; 3]> {
|
||||
let values = p.get(key).and_then(Value::as_array).filter(|a| a.len() == 3).ok_or_else(|| bad(CMD, format!("`{key}` must be three finite Lab numbers")))?;
|
||||
@@ -72,7 +82,7 @@ pub(super) fn apply(s: &mut Session, p: &Value) -> Result<Option<Value>> {
|
||||
s.edit("Replace Color", |doc, _| {
|
||||
let selected = doc.selection.clone();
|
||||
let surface = crate::commands::paint_surface(doc, id, &Value::Null)?;
|
||||
let rect = surface.content_bounds();
|
||||
let rect = edit_bounds(surface.content_bounds(), selected.as_ref());
|
||||
if rect.is_empty() {
|
||||
return Ok(Value::Null);
|
||||
}
|
||||
@@ -112,3 +122,24 @@ pub(super) fn apply(s: &mut Session, p: &Value) -> Result<Option<Value>> {
|
||||
})
|
||||
.map(Some)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use photocraft_color::PixelFormat;
|
||||
|
||||
#[test]
|
||||
fn selected_work_bounds_preserve_implicit_selection_and_clip_explicit_pixels() {
|
||||
let content = Rect::new(-100, -100, 100, 100);
|
||||
let mut mask = Surface::new(PixelFormat::GRAY8);
|
||||
assert_eq!(edit_bounds(content, Some(&mask)), Rect::EMPTY);
|
||||
mask.fill_rect(Rect::new(98, 96, 110, 105), &[0.5]);
|
||||
assert_eq!(edit_bounds(content, Some(&mask)), Rect::new(98, 96, 100, 100));
|
||||
assert_eq!(edit_bounds(content, None), content);
|
||||
for coverage in [0.5, 1.0] {
|
||||
let mut mask = Surface::with_default(PixelFormat::GRAY8, &[coverage]);
|
||||
mask.fill_rect(Rect::new(2, 2, 3, 3), &[0.0]);
|
||||
assert_eq!(edit_bounds(content, Some(&mask)), content, "stored holes do not bound selected pixels");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -336,7 +336,7 @@ fn invalid_replace_descriptors_and_lossy_exports_are_refused() {
|
||||
|
||||
#[test]
|
||||
fn recorded_replace_color_clamps_shifts_and_plays_the_lab_box_at_every_depth() {
|
||||
for depth in [8, 16, 32] {
|
||||
for (depth, inverse) in [8, 16, 32].into_iter().flat_map(|depth| [(depth, false), (depth, true)]) {
|
||||
let mut d = replace();
|
||||
for key in ["Mnm ", "Mxm "] {
|
||||
let o = object(&mut d, key);
|
||||
@@ -353,13 +353,24 @@ fn recorded_replace_color_clamps_shifts_and_plays_the_lab_box_at_every_depth() {
|
||||
assert_eq!(imported.actions[0].steps[0].1["lightness"], 100);
|
||||
let mut s = fixture("lab", depth);
|
||||
set_lab_pixels(&mut s);
|
||||
s.execute("select.rect", json!({"x":0,"y":0,"width":2,"height":1})).unwrap();
|
||||
s.execute("select.rect", json!({"x":1,"y":0,"width":1,"height":1})).unwrap();
|
||||
if inverse {
|
||||
let mut mask = photocraft_raster::Surface::with_default(photocraft_doc::PixelFormat::GRAY8, &[1.0]);
|
||||
mask.fill_rect(photocraft_doc::Rect::new(1, 0, 2, 1), &[0.0]);
|
||||
std::sync::Arc::make_mut(&mut s.active_mut().unwrap().doc).selection = Some(mask);
|
||||
}
|
||||
let before = raw(&s);
|
||||
s.actions.list = imported.actions;
|
||||
play(&mut s);
|
||||
let after = raw(&s);
|
||||
assert!(after[0] > 0.99 && after[4] > 0.99);
|
||||
assert_eq!(&after[8..], &before[8..], "selection must bound the edit");
|
||||
if inverse {
|
||||
assert!(after[0] > 0.99 && after[8] > 0.99);
|
||||
assert_eq!(&after[4..8], &before[4..8], "the selection hole must stay unchanged");
|
||||
} else {
|
||||
assert!(after[4] > 0.99);
|
||||
assert_eq!(&after[..4], &before[..4], "pixels before the selection must stay unchanged");
|
||||
assert_eq!(&after[8..], &before[8..], "pixels after the selection must stay unchanged");
|
||||
}
|
||||
assert!(s.undo());
|
||||
assert_eq!(raw(&s), before);
|
||||
let descriptor = saved(&s).descriptor.unwrap();
|
||||
|
||||
Reference in new issue
Block a user