mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge #18005 into the merge queue batch
Brush on a Grayscale document's mask paints the foreground's grey from the document's grey profile
This commit is contained in:
commit
3f52e9cb2b
3 files changed
+103
-2
No files matched your search
@@ -0,0 +1,85 @@
|
||||
//! #17644: in a Grayscale document the Brush paints a layer mask with the foreground's grey in
|
||||
//! the document's grey space (the default Dot Gain 20 % profile), as the reference does:
|
||||
//! RGB 0,200,0 writes 153, grey 128 writes 105 and 255,0,0 writes 107, not their Rec. 601 luma
|
||||
//! (117/118, 128, 76). RGB documents keep the luma.
|
||||
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
use crate::brush_cmds::LiveStrokeExt;
|
||||
|
||||
fn session(mode: &str, depth: u32) -> Session {
|
||||
let mut s = crate::session();
|
||||
s.execute("file.new", json!({"width": 40, "height": 40, "mode": mode, "depth": depth, "background": "white"})).unwrap();
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
s.execute("layer.layerMask.revealAll", json!({})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn rgb(c: [u8; 3]) -> [f32; 4] {
|
||||
[c[0] as f32 / 255.0, c[1] as f32 / 255.0, c[2] as f32 / 255.0, 1.0]
|
||||
}
|
||||
|
||||
fn stroke_params() -> serde_json::Value {
|
||||
json!({"points": [[20, 20]], "size": 21, "hardness": 1.0, "target": "mask", "seed": 3})
|
||||
}
|
||||
|
||||
fn mask_level(s: &Session) -> i32 {
|
||||
let st = s.active().unwrap();
|
||||
let layer = st.doc.layer(st.active_layer.unwrap()).unwrap();
|
||||
(layer.mask.as_ref().expect("a layer mask").surface.sample_channel(20, 20, 0) * 255.0).round() as i32
|
||||
}
|
||||
|
||||
fn brushed(mode: &str, depth: u32, fg: [u8; 3]) -> i32 {
|
||||
let mut s = session(mode, depth);
|
||||
s.tools.foreground = rgb(fg);
|
||||
s.execute("paint.stroke", stroke_params()).unwrap();
|
||||
mask_level(&s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_grayscale_mask_takes_the_foreground_through_the_document_gray_profile() {
|
||||
for (fg, want) in [([0, 200, 0], 153), ([128, 128, 128], 105), ([255, 0, 0], 107)] {
|
||||
let got = brushed("grayscale", 8, fg);
|
||||
assert!((got - want).abs() <= 1, "fg {fg:?}: mask level {got}, reference {want}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn black_and_white_stay_black_and_white_on_a_grayscale_mask() {
|
||||
assert_eq!(brushed("grayscale", 8, [0, 0, 0]), 0);
|
||||
assert_eq!(brushed("grayscale", 8, [255, 255, 255]), 255);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_live_stroke_matches_the_committed_mask_level() {
|
||||
let mut s = session("grayscale", 8);
|
||||
s.tools.foreground = rgb([0, 200, 0]);
|
||||
let mut p = stroke_params();
|
||||
let live = crate::brush_cmds::LiveStroke::begin(&s, &p).unwrap();
|
||||
p["seed"] = json!(live.seed);
|
||||
s.offer_live_stroke(live).unwrap();
|
||||
let result = s.execute("paint.stroke", p).unwrap();
|
||||
assert_eq!(result["fromLive"], true, "the commit took the live pixels");
|
||||
assert!((mask_level(&s) - 153).abs() <= 1, "live-committed mask level {}", mask_level(&s));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rgb_documents_keep_the_luma_on_a_mask() {
|
||||
for (fg, want) in [([0, 200, 0], 117), ([128, 128, 128], 128), ([255, 0, 0], 76)] {
|
||||
let got = brushed("rgb", 8, fg);
|
||||
assert!((got - want).abs() <= 1, "fg {fg:?}: mask level {got}, luma {want}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layer_pixels_of_a_grayscale_document_are_not_changed_by_this_item() {
|
||||
// Only the mask target was measured; the Brush on Grayscale layer pixels keeps today's grey.
|
||||
let mut s = crate::session();
|
||||
s.execute("file.new", json!({"width": 40, "height": 40, "mode": "grayscale", "depth": 8, "background": "white"})).unwrap();
|
||||
s.tools.foreground = rgb([0, 200, 0]);
|
||||
s.execute("paint.stroke", json!({"points": [[20, 20]], "size": 21, "hardness": 1.0})).unwrap();
|
||||
let v = s.execute("document.pixel", json!({"x": 20, "y": 20})).unwrap();
|
||||
let g = (v[0].as_f64().unwrap() * 255.0).round() as i32;
|
||||
assert!((g - 117).abs() <= 1, "layer pixel {g}");
|
||||
}
|
||||
@@ -418,6 +418,20 @@ fn pencil_gray_in_doc(s: &Session, c: [f32; 4]) -> [f32; 4] {
|
||||
[g, g, g, c[3]]
|
||||
}
|
||||
|
||||
/// The Brush's colour on a layer mask of an 8- or 16-bit Grayscale document (#17644): the
|
||||
/// foreground's grey in the document's grey space ([`pencil_gray_in_doc`]), as the reference
|
||||
/// paints it (RGB 0,200,0 writes 153 with the default Dot Gain 20 % profile), not its luma. The
|
||||
/// Eraser (it paints the background colour), other targets, 32-bit documents and other modes
|
||||
/// (not measured) keep the colour.
|
||||
fn mask_gray_in_doc(s: &Session, p: &Value, mut brush: BrushSettings) -> BrushSettings {
|
||||
let on_mask = p.get("target").and_then(Value::as_str) == Some("mask");
|
||||
let depth_measured = s.active().is_some_and(|st| st.doc.depth != photocraft_color::SampleType::F32);
|
||||
if on_mask && !brush.erase && depth_measured {
|
||||
brush.color = pencil_gray_in_doc(s, brush.color);
|
||||
}
|
||||
brush
|
||||
}
|
||||
|
||||
/// `brush` with the Pencil's hard, aliased tip, or the Eraser's Block (`"block": true`).
|
||||
pub(crate) fn pencil_shape(mut brush: BrushSettings, p: &Value) -> Result<BrushSettings> {
|
||||
if flag(p, "block", false) {
|
||||
@@ -528,7 +542,7 @@ fn spacing_warning(brush: &BrushSettings, pts: &[StrokePoint]) -> Option<(f64, S
|
||||
/// dab only, the result also has the dab `pitch` in pixels and `warnings` saying so (#8389).
|
||||
pub fn paint_stroke(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let pts = parse_points(p, "paint.stroke")?;
|
||||
let brush = with_blend_mode(resolve_brush(s, p, "paint.stroke")?, p);
|
||||
let brush = mask_gray_in_doc(s, p, with_blend_mode(resolve_brush(s, p, "paint.stroke")?, p));
|
||||
let label = if brush.erase && !clear_mode(p) { "Eraser" } else { "Brush Tool" };
|
||||
if flag(p, "fill", false) {
|
||||
return fill_region(s, p, label, brush, pts);
|
||||
@@ -692,7 +706,7 @@ impl LiveStroke {
|
||||
"paint.pencil" => true,
|
||||
other => return Err(bad(other, "live strokes are `paint.stroke` or `paint.pencil`")),
|
||||
};
|
||||
let brush = if pencil { pencil_brush(s, p)? } else { with_blend_mode(resolve_brush(s, p, cmd)?, p) };
|
||||
let brush = if pencil { pencil_brush(s, p)? } else { mask_gray_in_doc(s, p, with_blend_mode(resolve_brush(s, p, cmd)?, p)) };
|
||||
let (seed, fg) = (brush.seed, brush.color);
|
||||
let (layer, mut brush, zoom) = stroke_target(s, p, brush)?;
|
||||
let st = s.active().ok_or(EngineError::NoDocument)?;
|
||||
|
||||
@@ -38,6 +38,8 @@ pub use install::{command_tables, install, register, session};
|
||||
#[cfg(test)]
|
||||
mod brush_clear_background_tests;
|
||||
#[cfg(test)]
|
||||
mod brush_grayscale_mask_level_tests;
|
||||
#[cfg(test)]
|
||||
mod brush_pressure_size_default_tests;
|
||||
#[cfg(test)]
|
||||
mod eraser_gray_background_tests;
|
||||
|
||||
Reference in new issue
Block a user