image.domain\util\image_noise.mbt
97.6% · 40 / 41 points · 105 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 图像噪声 util/image_noise - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 噪声生成(纯 MoonBit):add_noise_gaussian / add_noise_salt_pepper - 7
// 使用简单的 LCG 伪随机数生成器,种子固定以保证可测试性 - 8
- 9
///| - 10
/// 简单 LCG 伪随机数生成器状态 - 11
priv struct LCG { - 12
mut state : UInt32 - 13
} - 14
- 15
///| - 16
fn lcg_next(rng : LCG) -> UInt32 { - 17●
rng.state = rng.state * 1103515245U + 12345U - 18
rng.state - 19
} - 20
- 21
///| - 22
fn lcg_float(rng : LCG) -> Float { - 23●
let v = lcg_next(rng) - 24●
Float::from_int((v & 0xFFFFFFU).reinterpret_as_int()) / 16777216.0F - 25
} - 26
- 27
///| - 28
/// Box-Muller 变换生成标准正态分布随机数 - 29
fn lcg_gaussian(rng : LCG) -> Float { - 30●
let u1 = lcg_float(rng) - 31●
let u2 = lcg_float(rng) - 32◐
let safe_u1 = if u1 < 0.0001F { 0.0001F } else { u1 } - 33
// ln(x) = log2(x) * ln(2), sqrt(x) = powf(x, 0.5) - 34●
let ln_u1 = @math.log2(safe_u1.to_double()) * 0.6931471805599453 - 35●
let r = Float::from_double(ln_u1 * -2.0).sqrt() - 36●
r * @math.cosf(2.0F * 3.14159265F * u2) - 37
} - 38
- 39
///| - 40
- 41
///| - 42
/// 添加高斯噪声。sigma 为标准差,seed 为随机种子。 - 43
/// 仅影响颜色通道,alpha 通道不变。 - 44
pub fn add_noise_gaussian( - 45
img : @types.Image, - 46
sigma : Float, - 47
seed : UInt32, - 48
) -> @types.Image { - 49●
let rng = LCG::{ state: seed, } - 50
let n = img.width * img.height - 51
let ch = img.channels - 52●
let color_ch = if ch >= 3 { 3 } else { ch } - 53●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 54
for i in 0..<n { - 55●
for c in 0..<color_ch { - 56●
let noise = lcg_gaussian(rng) * sigma - 57●
let v = img.data[i * ch + c].to_int() + noise.to_int() - 58●
out[i * ch + c] = @types.clamp_byte(v) - 59
} - 60
if ch == 4 { - 61●
out[i * ch + 3] = img.data[i * ch + 3] - 62
} - 63
} - 64
@types.Image::{ - 65
width: img.width, - 66
height: img.height, - 67
channels: ch, - 68●
data: Bytes::from_array(out), - 69
} - 70
} - 71
- 72
///| - 73
/// 添加椒盐噪声。prob 为噪声概率(0.0~1.0),seed 为随机种子。 - 74
/// 被选中的像素随机设为 0(椒)或 255(盐)。 - 75
pub fn add_noise_salt_pepper( - 76
img : @types.Image, - 77
prob : Float, - 78
seed : UInt32, - 79
) -> @types.Image { - 80●
let rng = LCG::{ state: seed, } - 81
let n = img.width * img.height - 82
let ch = img.channels - 83●
let color_ch = if ch >= 3 { 3 } else { ch } - 84●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 85
for i in 0..<n { - 86●
let r = lcg_float(rng) - 87
if r < prob { - 88●
let salt = lcg_float(rng) < 0.5F - 89●
let val = if salt { b'\xFF' } else { b'\x00' } - 90
for c in 0..<color_ch { - 91●
out[i * ch + c] = val - 92
} - 93
} else { - 94●
for c in 0..<ch { - 95●
out[i * ch + c] = img.data[i * ch + c] - 96
} - 97
} - 98
} - 99
@types.Image::{ - 100
width: img.width, - 101
height: img.height, - 102
channels: ch, - 103●
data: Bytes::from_array(out), - 104
} - 105
}