mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
8 files changed
+281
-29
No files matched your search
@@ -0,0 +1,66 @@
|
||||
//! #17317: own 60 px strokes on an 800x400 logical frame, sampled without 8-bit quantization.
|
||||
|
||||
use photocraft_color::PixelFormat;
|
||||
use photocraft_raster::Surface;
|
||||
|
||||
use crate::{BrushSettings, StrokePoint, render_stroke};
|
||||
|
||||
fn paint(noise: bool, hardness: f32, seed: u64) -> Surface {
|
||||
let brush = BrushSettings { size: 60.0, hardness, noise, seed, ..BrushSettings::default() };
|
||||
let mut surface = Surface::new(PixelFormat::RGBA32F);
|
||||
render_stroke(&mut surface, &brush, &[StrokePoint::new(100.0, 100.0, 1.0), StrokePoint::new(400.0, 100.0, 1.0)], None, false, 1.0);
|
||||
surface
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn soft_noise_is_light_and_mean_neutral_over_the_complete_stroke_rectangle() {
|
||||
let plain = paint(false, 0.0, 17317);
|
||||
let noisy = paint(true, 0.0, 17317);
|
||||
let (mut sum, mut squares, mut count) = (0.0, 0.0, 0usize);
|
||||
// Fixed complete soft-tip support rectangle: 51.792 px reach plus pixel sampling,
|
||||
// around the whole100..400 stroke. Include every pixel, not only favourable alpha bins.
|
||||
for y in 48..152 {
|
||||
for x in 48..452 {
|
||||
let delta = 255.0 * f64::from(plain.rgba(x, y)[3] - noisy.rgba(x, y)[3]);
|
||||
sum += delta;
|
||||
squares += delta * delta;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
let mean = sum / count as f64;
|
||||
let deviation = (squares / count as f64 - mean * mean).max(0.0).sqrt();
|
||||
assert!(mean.abs() <= 2.0, "Noise minus plain mean {mean}");
|
||||
assert!((10.0..=20.0).contains(&deviation), "Noise minus plain std {deviation}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn noise_leaves_the_hard_interior_and_the_empty_frame_unchanged() {
|
||||
let plain = paint(false, 1.0, 17317);
|
||||
let noisy = paint(true, 1.0, 17317);
|
||||
for y in 80..120 {
|
||||
for x in 100..400 {
|
||||
assert_eq!(noisy.rgba(x, y), plain.rgba(x, y), "hard interior {x},{y}");
|
||||
}
|
||||
}
|
||||
for (x, y) in [(0, 0), (100, 40), (400, 160), (799, 399)] {
|
||||
assert_eq!(noisy.rgba(x, y), plain.rgba(x, y));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn noise_replays_its_seed_and_different_seeds_get_different_grain() {
|
||||
let first = paint(true, 0.0, 1);
|
||||
let replay = paint(true, 0.0, 1);
|
||||
let other = paint(true, 0.0, 2);
|
||||
let plain_first = paint(false, 0.0, 1);
|
||||
let plain_other = paint(false, 0.0, 2);
|
||||
let mut changed = false;
|
||||
for y in 48..152 {
|
||||
for x in 48..452 {
|
||||
assert_eq!(first.rgba(x, y), replay.rgba(x, y));
|
||||
assert_eq!(plain_first.rgba(x, y), plain_other.rgba(x, y), "Noise off stays seed-independent");
|
||||
changed |= first.rgba(x, y) != other.rgba(x, y);
|
||||
}
|
||||
}
|
||||
assert!(changed, "different seeds must change the grain");
|
||||
}
|
||||
@@ -855,20 +855,23 @@ fn wet_edges_and_noise() {
|
||||
let hb = BrushSettings { hardness: 1.0, ..b };
|
||||
let d = paint(&hb, &[StrokePoint::new(50.0, 50.0, 1.0)], 100, 100);
|
||||
assert!(d.rgba(50 + 18, 50)[3] > d.rgba(50, 50)[3] + 0.2);
|
||||
// Noise: soft tip coverage becomes grainy (more pixels near 0/1).
|
||||
// Noise: soft coverage gets a light grain, not the old near-binary dither.
|
||||
let soft = BrushSettings { size: 40.0, hardness: 0.0, ..brush() };
|
||||
let a = paint(&soft, &[StrokePoint::new(50.0, 50.0, 1.0)], 100, 100);
|
||||
let n = paint(&BrushSettings { noise: true, ..soft }, &[StrokePoint::new(50.0, 50.0, 1.0)], 100, 100);
|
||||
let extreme = |s: &Surface| {
|
||||
(30..70)
|
||||
.flat_map(|y| (30..70).map(move |x| (x, y)))
|
||||
.filter(|&(x, y)| {
|
||||
let v = s.rgba(x, y)[3];
|
||||
!(0.15..=0.85).contains(&v)
|
||||
})
|
||||
.count()
|
||||
};
|
||||
assert!(extreme(&n) > extreme(&a) + 100, "{} vs {}", extreme(&n), extreme(&a));
|
||||
let (mut sum, mut squares, mut count) = (0.0, 0.0, 0usize);
|
||||
for y in 30..70 {
|
||||
for x in 30..70 {
|
||||
let delta = 255.0 * f64::from(a.rgba(x, y)[3] - n.rgba(x, y)[3]);
|
||||
sum += delta;
|
||||
squares += delta * delta;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
let mean = sum / count as f64;
|
||||
let deviation = (squares / count as f64 - mean * mean).max(0.0).sqrt();
|
||||
assert!(mean.abs() <= 2.0, "Noise mean {mean}");
|
||||
assert!((10.0..=20.0).contains(&deviation), "Noise std {deviation}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -418,5 +418,8 @@ mod mixer_load_supply_tests;
|
||||
#[cfg(test)]
|
||||
mod smoothing_catch_up_default_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod brush_noise_strength_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod brush_size_fade_end_tests;
|
||||
@@ -1672,9 +1672,10 @@ impl GeneralTip<'_> {
|
||||
continue;
|
||||
}
|
||||
if noise && val < 1.0 {
|
||||
let n = hash2(x, y, NOISE_SALT);
|
||||
let n = hash2(x, y, NOISE_SALT ^ b.seed);
|
||||
let hard = if n < val { 1.0 } else { 0.0 };
|
||||
val += (hard - val) * 0.7;
|
||||
// A light unbiased grain, rather than near-binary dither. Solid pixels skip this.
|
||||
val += (hard - val) * 0.15;
|
||||
}
|
||||
if tex_tip {
|
||||
val = texture_combine(b.texture.mode, val, ctx.texture_at(x, y), d.depth);
|
||||
|
||||
@@ -110,9 +110,9 @@ impl BrushContext {
|
||||
continue;
|
||||
}
|
||||
if noise && val < 1.0 {
|
||||
let n = hash2(x, y, NOISE_SALT);
|
||||
let n = hash2(x, y, NOISE_SALT ^ b.seed);
|
||||
let hard = if n < val { 1.0 } else { 0.0 };
|
||||
val += (hard - val) * 0.7;
|
||||
val += (hard - val) * 0.15;
|
||||
}
|
||||
if tex_tip {
|
||||
val = mask_combine(b.texture.mode, val, self.texture_at(x, y), d.depth);
|
||||
|
||||
@@ -1017,6 +1017,9 @@ mod warp_text_dialog_tests;
|
||||
#[cfg(test)]
|
||||
mod options_bar_mode_wheel_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod options_bar_percent_arrow_repeat_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod field_wheel_app_tests;
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
//! #16806: Up and Down in the painting tools' % fields (Opacity, Flow, Smoothing; the Mixer Brush's
|
||||
//! Wet, Load, Mix and Flow) step on every press. The brush keeps these as `f32` fractions, so 0.51
|
||||
//! reads back as 50.999999..., which the field took for a value off its grid and stepped to 51 again.
|
||||
|
||||
use egui::accesskit::Role;
|
||||
use egui::{Key, Modifiers, vec2};
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::state::Tool;
|
||||
|
||||
/// The options bar of a new document with `tool`.
|
||||
fn bar(tool: Tool) -> Harness<'static, Box<PhotocraftApp>> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 80, "height": 80})).unwrap();
|
||||
app.ui.tool = tool;
|
||||
crate::paint_mouse::sync_tool_brush(&mut app);
|
||||
crate::theme::install_fonts(app.ctx());
|
||||
crate::theme::apply(app.ctx(), &crate::theme::AppTheme::pro());
|
||||
let mut h = Harness::builder().with_context(app.ctx().clone()).with_size(vec2(2400.0, 300.0)).with_step_dt(1.0 / 60.0).build_ui_state(
|
||||
move |ui, app: &mut Box<PhotocraftApp>| {
|
||||
crate::panels::options_bar(app, ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
h.run_steps(4);
|
||||
h
|
||||
}
|
||||
|
||||
/// The field named `label`, with or without its caption's colon.
|
||||
fn field<'a>(h: &'a Harness<'static, Box<PhotocraftApp>>, label: &str) -> egui_kittest::Node<'a> {
|
||||
h.query_all_by_role(Role::SpinButton)
|
||||
.find(|n| n.accesskit_node().label().is_some_and(|l| l.trim_end_matches(':') == label))
|
||||
.unwrap_or_else(|| panic!("the {label} field"))
|
||||
}
|
||||
|
||||
/// The value the field named `label` holds, to a billionth (the field's own grid tolerance), so
|
||||
/// 50.999999 (an `f32` read as it is) stays apart from 51.
|
||||
fn shown(h: &Harness<'static, Box<PhotocraftApp>>, label: &str) -> f64 {
|
||||
let value = field(h, label).accesskit_node().numeric_value().unwrap_or_else(|| panic!("the {label} field's value"));
|
||||
(value * 1e9).round() / 1e9
|
||||
}
|
||||
|
||||
/// Clicks into the field named `label`, types `text` over it and presses Enter; the focus stays in it.
|
||||
fn type_into(h: &mut Harness<'static, Box<PhotocraftApp>>, label: &str, text: &str) {
|
||||
let at = field(h, label).rect().center();
|
||||
h.hover_at(at);
|
||||
h.run_steps(1);
|
||||
h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
|
||||
h.run_steps(1);
|
||||
h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });
|
||||
h.run_steps(1);
|
||||
h.key_press_modifiers(Modifiers::COMMAND, Key::A);
|
||||
h.event(egui::Event::Text(text.into()));
|
||||
h.run_steps(1);
|
||||
h.key_press(Key::Enter);
|
||||
h.run_steps(3);
|
||||
}
|
||||
|
||||
/// Presses `key` with `mods` `times` times, letting the bar redraw from the stored value in between.
|
||||
fn press(h: &mut Harness<'static, Box<PhotocraftApp>>, key: Key, mods: Modifiers, times: usize) {
|
||||
for _ in 0..times {
|
||||
h.key_press_modifiers(mods, key);
|
||||
h.run_steps(3);
|
||||
}
|
||||
}
|
||||
|
||||
/// From 50, Up x3 shows 53 and Down x3 (from 50 again) shows 47 in the field named `label`.
|
||||
fn steps_every_press(tool: Tool, label: &str) {
|
||||
let mut h = bar(tool);
|
||||
type_into(&mut h, label, "50");
|
||||
assert_eq!(shown(&h, label), 50.0, "{tool:?} {label} typed 50");
|
||||
press(&mut h, Key::ArrowUp, Modifiers::NONE, 3);
|
||||
assert_eq!(shown(&h, label), 53.0, "{tool:?} {label}: 50, Up x3");
|
||||
type_into(&mut h, label, "50");
|
||||
press(&mut h, Key::ArrowDown, Modifiers::NONE, 3);
|
||||
assert_eq!(shown(&h, label), 47.0, "{tool:?} {label}: 50, Down x3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn brush_opacity_flow_and_smoothing_step_on_every_press() {
|
||||
for label in ["Opacity", "Flow", "Smoothing"] {
|
||||
steps_every_press(Tool::Brush, label);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pencil_and_eraser_percent_fields_step_on_every_press() {
|
||||
steps_every_press(Tool::Pencil, "Opacity");
|
||||
steps_every_press(Tool::Pencil, "Smoothing");
|
||||
steps_every_press(Tool::Eraser, "Opacity");
|
||||
steps_every_press(Tool::Eraser, "Flow");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mixer_brush_percent_fields_step_on_every_press() {
|
||||
for label in ["Wet", "Load", "Mix", "Flow"] {
|
||||
steps_every_press(Tool::MixerBrush, label);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shift_up_steps_ten_on_every_press() {
|
||||
let mut h = bar(Tool::Brush);
|
||||
type_into(&mut h, "Opacity", "50");
|
||||
press(&mut h, Key::ArrowUp, Modifiers::SHIFT, 2);
|
||||
assert_eq!(shown(&h, "Opacity"), 70.0, "50, Shift+Up x2");
|
||||
assert_eq!((h.state().session.tools.brush.opacity * 100.0).round(), 70.0, "the brush holds 70 %");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_f32_opacity_of_0_51_steps_up_twice_to_53() {
|
||||
let mut h = bar(Tool::Brush);
|
||||
type_into(&mut h, "Opacity", "40");
|
||||
h.state_mut().session.tools.brush.opacity = 0.51_f32;
|
||||
h.run_steps(3);
|
||||
assert_eq!(shown(&h, "Opacity"), 51.0);
|
||||
press(&mut h, Key::ArrowUp, Modifiers::NONE, 2);
|
||||
assert_eq!(shown(&h, "Opacity"), 53.0, "0.51 (f32), Up x2");
|
||||
assert_eq!((h.state().session.tools.brush.opacity * 100.0).round(), 53.0, "the brush holds 53 %");
|
||||
}
|
||||
@@ -113,10 +113,10 @@ fn paint_values(app: &PhotocraftApp, tool: Tool, b: &BrushSettings) -> PaintValu
|
||||
PaintMode::Blend(b.mode)
|
||||
};
|
||||
let mut v = PaintValues::new(mode);
|
||||
v.opacity = f64::from(b.opacity);
|
||||
v.flow = f64::from(b.flow);
|
||||
v.opacity = fraction(b.opacity);
|
||||
v.flow = fraction(b.flow);
|
||||
let mut smoothing = Smoothing::default();
|
||||
smoothing.amount = f64::from(b.smoothing.amount);
|
||||
smoothing.amount = fraction(b.smoothing.amount);
|
||||
smoothing.pulled_string = b.smoothing.pulled_string;
|
||||
smoothing.catch_up = b.smoothing.catch_up;
|
||||
smoothing.catch_up_on_end = b.smoothing.catch_up_on_end;
|
||||
@@ -130,10 +130,10 @@ fn paint_values(app: &PhotocraftApp, tool: Tool, b: &BrushSettings) -> PaintValu
|
||||
let mut mixer = MixerValues::default();
|
||||
mixer.load_after_stroke = b.mixer.load_after_stroke;
|
||||
mixer.clean_after_stroke = b.mixer.clean_after_stroke;
|
||||
mixer.wet = f64::from(b.mixer.wet);
|
||||
mixer.load = f64::from(b.mixer.load);
|
||||
mixer.mix = f64::from(b.mixer.mix);
|
||||
mixer.flow = f64::from(b.mixer.flow);
|
||||
mixer.wet = fraction(b.mixer.wet);
|
||||
mixer.load = fraction(b.mixer.load);
|
||||
mixer.mix = fraction(b.mixer.mix);
|
||||
mixer.flow = fraction(b.mixer.flow);
|
||||
mixer.sample_all_layers = b.mixer.sample_all_layers;
|
||||
mixer.combination = o.mixer_combination;
|
||||
mixer.current_load = mixer_load(app);
|
||||
@@ -142,6 +142,14 @@ fn paint_values(app: &PhotocraftApp, tool: Tool, b: &BrushSettings) -> PaintValu
|
||||
v
|
||||
}
|
||||
|
||||
/// A fraction the brush keeps as `f32`, read without its float noise. 0.51 as `f32` reads back as
|
||||
/// 0.50999999..., which the bar's whole-percent fields take for a value off their step grid, so Up
|
||||
/// stepped it to 51 again and only the first press moved the value (#16806). Six decimals keep every
|
||||
/// value the fields can set (a tenth of a percent at the finest).
|
||||
fn fraction(x: f32) -> f64 {
|
||||
(f64::from(x) * 1e6).round() / 1e6
|
||||
}
|
||||
|
||||
/// What the Mixer Brush holds, as the Current Brush Load swatch shows it: the sampled tip image of an Alt-click while
|
||||
/// the foreground stays, else the loaded colour, else nothing (a clean brush).
|
||||
fn mixer_load(app: &PhotocraftApp) -> MixerLoad {
|
||||
@@ -169,14 +177,18 @@ fn apply_values(app: &mut PhotocraftApp, tool: Tool, brush: &mut BrushSettings,
|
||||
// PLAN: PaintBar's Opacity and Flow fields range over 0..=100, so a typed 0 on the Eraser reports no
|
||||
// `out_of_range` and shows no alert (the old bar's range started at 1); pn.C18's owner decides the range.
|
||||
const MIN_FRACTION: f32 = 0.01;
|
||||
brush.opacity = (after.opacity as f32).max(MIN_FRACTION);
|
||||
if tool != Tool::MixerBrush {
|
||||
// The bar shows the fractions rounded (`fraction`), so an untouched one is left as the brush
|
||||
// holds it rather than written back rounded.
|
||||
if after.opacity != before.opacity {
|
||||
brush.opacity = (after.opacity as f32).max(MIN_FRACTION);
|
||||
}
|
||||
if tool != Tool::MixerBrush && after.flow != before.flow {
|
||||
brush.flow = (after.flow as f32).max(MIN_FRACTION);
|
||||
}
|
||||
if after.smoothing.amount != before.smoothing.amount {
|
||||
brush.smoothing.enabled = true;
|
||||
brush.smoothing.amount = after.smoothing.amount as f32;
|
||||
}
|
||||
brush.smoothing.amount = after.smoothing.amount as f32;
|
||||
brush.smoothing.pulled_string = after.smoothing.pulled_string;
|
||||
brush.smoothing.catch_up = after.smoothing.catch_up;
|
||||
brush.smoothing.catch_up_on_end = after.smoothing.catch_up_on_end;
|
||||
@@ -187,10 +199,16 @@ fn apply_values(app: &mut PhotocraftApp, tool: Tool, brush: &mut BrushSettings,
|
||||
let m = &mut brush.mixer;
|
||||
m.load_after_stroke = after.mixer.load_after_stroke;
|
||||
m.clean_after_stroke = after.mixer.clean_after_stroke;
|
||||
m.wet = after.mixer.wet as f32;
|
||||
m.load = after.mixer.load as f32;
|
||||
m.mix = after.mixer.mix as f32;
|
||||
m.flow = after.mixer.flow as f32;
|
||||
for (field, was, now) in [
|
||||
(&mut m.wet, before.mixer.wet, after.mixer.wet),
|
||||
(&mut m.load, before.mixer.load, after.mixer.load),
|
||||
(&mut m.mix, before.mixer.mix, after.mixer.mix),
|
||||
(&mut m.flow, before.mixer.flow, after.mixer.flow),
|
||||
] {
|
||||
if now != was {
|
||||
*field = now as f32;
|
||||
}
|
||||
}
|
||||
m.sample_all_layers = after.mixer.sample_all_layers;
|
||||
m.solid_colors_only = after.mixer.solid_colors_only;
|
||||
let o = &mut app.ui.tool_options;
|
||||
@@ -358,6 +376,41 @@ mod mixer_blend_combination_tests;
|
||||
#[path = "../../mixer_load_solid_colors_tests.rs"]
|
||||
mod mixer_load_solid_colors_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod apply_values_tests {
|
||||
use super::{apply_values, paint_values};
|
||||
use crate::PhotocraftApp;
|
||||
use crate::state::Tool;
|
||||
|
||||
/// An edit of an unrelated control (Pressure for Size) leaves the brush's fractions exactly as
|
||||
/// they were, not rounded to the six decimals the bar shows them with.
|
||||
#[test]
|
||||
fn an_unrelated_edit_keeps_the_untouched_fractions_exact() {
|
||||
for tool in [Tool::Brush, Tool::MixerBrush] {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
let mut brush = (*app.session.tools.brush).clone();
|
||||
let odd = 0.123_456_78_f32;
|
||||
brush.opacity = odd;
|
||||
brush.flow = odd;
|
||||
brush.smoothing.amount = odd;
|
||||
brush.mixer.wet = odd;
|
||||
brush.mixer.load = odd;
|
||||
brush.mixer.mix = odd;
|
||||
brush.mixer.flow = odd;
|
||||
let before = paint_values(&app, tool, &brush);
|
||||
let mut after = before.clone();
|
||||
after.pressure_size = !after.pressure_size;
|
||||
let kept = brush.clone();
|
||||
apply_values(&mut app, tool, &mut brush, &before, &after);
|
||||
assert_eq!(brush.pressure_size, after.pressure_size, "{tool:?}: the edit lands");
|
||||
let fractions =
|
||||
|b: &super::BrushSettings| [b.opacity, b.flow, b.smoothing.amount, b.mixer.wet, b.mixer.load, b.mixer.mix, b.mixer.flow].map(f32::to_bits);
|
||||
assert_eq!(fractions(&brush), fractions(&kept), "{tool:?}: untouched fractions stay exact");
|
||||
assert_eq!(brush.smoothing.enabled, kept.smoothing.enabled, "{tool:?}: smoothing stays as it was");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod mixer_load_image_tests {
|
||||
use super::mixer_load_image;
|
||||
|
||||
Reference in new issue
Block a user