mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #10579 from yoriswind/codex/monochrome-noise
Reuse monochrome noise samples across color channels
This commit is contained in:
2 files changed
+60
-27
No files matched your search
+18
-12
@@ -32,18 +32,24 @@ pub(crate) fn add(src: &Image, out: Rect, ctx: &Ctx, amount: f32, dist: Distribu
|
||||
continue;
|
||||
}
|
||||
let (x, y) = (out.x0 + (i % w) as i32, out.y0 + (i / w) as i32);
|
||||
for (c, pv) in px.iter_mut().enumerate().take(cc) {
|
||||
let ch = if mono { 0 } else { c as u32 };
|
||||
let v = match dist {
|
||||
Distribution::Uniform => (hash01(x, y, ch, seed) - 0.5) * 2.0 * a,
|
||||
Distribution::Gaussian => {
|
||||
let u1 = hash01(x, y, ch * 2 + 101, seed).max(1e-7);
|
||||
let u2 = hash01(x, y, ch * 2 + 102, seed);
|
||||
(-2.0 * u1.ln()).sqrt() * (std::f32::consts::TAU * u2).cos() * a * 0.5
|
||||
}
|
||||
};
|
||||
// Integer storage clamps on write; float surfaces keep the value.
|
||||
*pv += v;
|
||||
let value = |ch| match dist {
|
||||
Distribution::Uniform => (hash01(x, y, ch, seed) - 0.5) * 2.0 * a,
|
||||
Distribution::Gaussian => {
|
||||
let u1 = hash01(x, y, ch * 2 + 101, seed).max(1e-7);
|
||||
let u2 = hash01(x, y, ch * 2 + 102, seed);
|
||||
(-2.0 * u1.ln()).sqrt() * (std::f32::consts::TAU * u2).cos() * a * 0.5
|
||||
}
|
||||
};
|
||||
// Integer storage clamps on write; float surfaces keep the value.
|
||||
if mono {
|
||||
let v = value(0);
|
||||
for pv in px.iter_mut().take(cc) {
|
||||
*pv += v;
|
||||
}
|
||||
} else {
|
||||
for (c, pv) in px.iter_mut().enumerate().take(cc) {
|
||||
*pv += value(c as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
res
|
||||
|
||||
+42
-15
@@ -110,6 +110,8 @@ fn all_filters() -> Vec<FilterParams> {
|
||||
FilterParams::SmartSharpen { amount: 100.0, radius: 1.0, reduce_noise: 20.0 },
|
||||
FilterParams::HighPass { radius: 3.0 },
|
||||
FilterParams::AddNoise { amount: 25.0, distribution: Distribution::Gaussian, monochromatic: false, seed: 3 },
|
||||
FilterParams::AddNoise { amount: 25.0, distribution: Distribution::Uniform, monochromatic: true, seed: 3 },
|
||||
FilterParams::AddNoise { amount: 25.0, distribution: Distribution::Gaussian, monochromatic: true, seed: 3 },
|
||||
FilterParams::Median { radius: 2.0 },
|
||||
FilterParams::DustAndScratches { radius: 2.0, threshold: 10.0 },
|
||||
FilterParams::Minimum { radius: 1.0, preserve: Preserve::Squareness },
|
||||
@@ -291,24 +293,49 @@ fn high_pass_of_flat_is_mid_gray() {
|
||||
assert!((p[0] - 0.5).abs() < 1e-5 && (p[1] - 0.5).abs() < 1e-5);
|
||||
}
|
||||
|
||||
/// Both noise distributions preserve alpha and share a seeded value across color channels.
|
||||
#[test]
|
||||
fn noise_is_deterministic_monochrome_and_centred() {
|
||||
let s = flat(SampleType::F32, R, [0.5, 0.5, 0.5, 1.0]);
|
||||
let p = FilterParams::AddNoise { amount: 50.0, distribution: Distribution::Uniform, monochromatic: true, seed: 1 };
|
||||
let a = run(&s, &p);
|
||||
assert_eq!(a, run(&s, &p));
|
||||
let v = a.read_region(R);
|
||||
let mut mean = 0.0;
|
||||
for px in v.as_chunks::<4>().0 {
|
||||
assert_eq!(px[0], px[1]);
|
||||
assert_eq!(px[1], px[2]);
|
||||
assert!((px[0] - 0.5).abs() <= 0.25 + 1e-6);
|
||||
mean += px[0];
|
||||
for distribution in [Distribution::Uniform, Distribution::Gaussian] {
|
||||
let s = flat(SampleType::F32, R, [0.5, 0.5, 0.5, 1.0]);
|
||||
let p = FilterParams::AddNoise { amount: 50.0, distribution, monochromatic: true, seed: 1 };
|
||||
let a = run(&s, &p);
|
||||
assert_eq!(a, run(&s, &p));
|
||||
let v = a.read_region(R);
|
||||
let mut mean = 0.0;
|
||||
for px in v.as_chunks::<4>().0 {
|
||||
assert_eq!(px[0], px[1]);
|
||||
assert_eq!(px[1], px[2]);
|
||||
if matches!(distribution, Distribution::Uniform) {
|
||||
assert!((px[0] - 0.5).abs() <= 0.25 + 1e-6);
|
||||
}
|
||||
assert_eq!(px[3], 1.0);
|
||||
mean += px[0];
|
||||
}
|
||||
mean /= (v.len() / 4) as f32;
|
||||
assert!((mean - 0.5).abs() < 0.03, "{mean}");
|
||||
let other_seed = run(&s, &FilterParams::AddNoise { amount: 50.0, distribution, monochromatic: true, seed: 2 });
|
||||
assert_ne!(a, other_seed);
|
||||
}
|
||||
}
|
||||
|
||||
/// RGB8 goldens from 799e1296's original Add Noise, before monochrome sampling was hoisted.
|
||||
#[test]
|
||||
fn monochrome_noise_matches_original_rgb8_bytes() {
|
||||
let area = Rect::new(0, 0, 3, 2);
|
||||
let mut source = Surface::new(PixelFormat::new(ColorMode::Rgb, SampleType::U8, false));
|
||||
source.fill_rect(area, &[0.5, 0.5, 0.5]);
|
||||
let rgb_bytes = |surface: &Surface| surface.read_region(area).iter().map(|v| (v * 255.0).round() as u8).collect::<Vec<_>>();
|
||||
let cases: [(Distribution, [u8; 18]); 2] = [
|
||||
(Distribution::Uniform, [65, 65, 65, 96, 96, 96, 134, 134, 134, 67, 67, 67, 86, 86, 86, 118, 118, 118]),
|
||||
(Distribution::Gaussian, [105, 105, 105, 104, 104, 104, 117, 117, 117, 103, 103, 103, 167, 167, 167, 136, 136, 136]),
|
||||
];
|
||||
for (distribution, expected) in cases {
|
||||
let monochrome = apply(&source, &FilterParams::AddNoise { amount: 50.0, distribution, monochromatic: true, seed: 1 }, area, area, None);
|
||||
assert_eq!(rgb_bytes(&monochrome).as_slice(), expected.as_slice(), "{distribution:?}");
|
||||
let colored = apply(&source, &FilterParams::AddNoise { amount: 50.0, distribution, monochromatic: false, seed: 1 }, area, area, None);
|
||||
assert!(rgb_bytes(&colored).as_chunks::<3>().0.iter().any(|pixel| pixel[0] != pixel[1] || pixel[1] != pixel[2]), "{distribution:?}");
|
||||
}
|
||||
mean /= (v.len() / 4) as f32;
|
||||
assert!((mean - 0.5).abs() < 0.03, "{mean}");
|
||||
let other_seed = run(&s, &FilterParams::AddNoise { amount: 50.0, distribution: Distribution::Uniform, monochromatic: true, seed: 2 });
|
||||
assert_ne!(a, other_seed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user