image.domain\util\image_noise.mbt

97.6% · 40 / 41 points · 105 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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