mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-28 14:18:22 -08:00
Merge #17422 into the merge queue batch
Make brush Noise a light seeded grain
This commit is contained in:
commit
e6d0f67107
5 files changed
+88
-15
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);
|
||||
|
||||
Reference in new issue
Block a user