image.domain\util\pixel_advanced.mbt

100% · 32 / 32 points · 124 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/pixel_advanced
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 高级像素操作(纯 MoonBit):pixelate / replace_color / swap_channels
  7. 7
  8. 8///|
  9. 9/// 马赛克/像素化效果。将图像分成 block_size × block_size 的块,每块取左上角像素值。
  10. 10pub fn pixelate(
  11. 11 img : @types.Image,
  12. 12 block_size : Int32,
  13. 13) -> @types.Image raise @types.LoadError {
  14. 14● if block_size <= 0 {
  15. 15● raise @types.LoadError::DecodeFailed("block_size 必须为正数")
  16. 16 }
  17. 17 let ch = img.channels
  18. 18 let w = img.width
  19. 19 let h = img.height
  20. 20● let out : Array[Byte] = Array::make(w * h * ch, b'\x00')
  21. 21 for by in 0..<h {
  22. 22● for bx in 0..<w {
  23. 23● let src_x = bx / block_size * block_size
  24. 24 let src_y = by / block_size * block_size
  25. 25 let src_off = (src_y * w + src_x) * ch
  26. 26 let dst_off = (by * w + bx) * ch
  27. 27 for c in 0..<ch {
  28. 28● out[dst_off + c] = img.data[src_off + c]
  29. 29 }
  30. 30 }
  31. 31 }
  32. 32 @types.Image::{
  33. 33 width: w,
  34. 34 height: h,
  35. 35 channels: ch,
  36. 36● data: Bytes::from_array(out),
  37. 37 }
  38. 38}
  39. 39
  40. 40///|
  41. 41/// 颜色替换。将 old_color 替换为 new_color(精确匹配或容差匹配)。
  42. 42/// old_color 和 new_color 长度应等于 channels。
  43. 43pub fn replace_color(
  44. 44 img : @types.Image,
  45. 45 old_color : Array[Byte],
  46. 46 new_color : Array[Byte],
  47. 47 tolerance : Int32,
  48. 48) -> @types.Image raise @types.LoadError {
  49. 49● let ch = img.channels
  50. 50● if old_color.length() < ch || new_color.length() < ch {
  51. 51● raise @types.LoadError::DecodeFailed("颜色数组长度必须 >= channels")
  52. 52 }
  53. 53 let n = img.width * img.height
  54. 54● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  55. 55 for i in 0..<n {
  56. 56● let off = i * ch
  57. 57● let is_match = check_color_match(img.data, off, ch, old_color, tolerance)
  58. 58 if is_match {
  59. 59● for c in 0..<ch {
  60. 60● out[off + c] = new_color[c]
  61. 61 }
  62. 62 } else {
  63. 63● for c in 0..<ch {
  64. 64● out[off + c] = img.data[off + c]
  65. 65 }
  66. 66 }
  67. 67 }
  68. 68 @types.Image::{
  69. 69 width: img.width,
  70. 70 height: img.height,
  71. 71 channels: ch,
  72. 72● data: Bytes::from_array(out),
  73. 73 }
  74. 74}
  75. 75
  76. 76///|
  77. 77/// 交换两个通道。例如 swap_channels(img, 0, 2) 交换 R 和 B。
  78. 78pub fn swap_channels(
  79. 79 img : @types.Image,
  80. 80 ch1 : Int32,
  81. 81 ch2 : Int32,
  82. 82) -> @types.Image raise @types.LoadError {
  83. 83● if ch1 < 0 || ch1 >= img.channels || ch2 < 0 || ch2 >= img.channels {
  84. 84● raise @types.LoadError::DecodeFailed("通道索引越界")
  85. 85 }
  86. 86 if ch1 == ch2 {
  87. 87● return img
  88. 88 }
  89. 89 let ch = img.channels
  90. 90 let n = img.width * img.height
  91. 91● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  92. 92 for i in 0..<n {
  93. 93● let off = i * ch
  94. 94 for c in 0..<ch {
  95. 95● out[off + c] = img.data[off + c]
  96. 96 }
  97. 97 let tmp = out[off + ch1]
  98. 98 out[off + ch1] = out[off + ch2]
  99. 99 out[off + ch2] = tmp
  100. 100 }
  101. 101 @types.Image::{
  102. 102 width: img.width,
  103. 103 height: img.height,
  104. 104 channels: ch,
  105. 105● data: Bytes::from_array(out),
  106. 106 }
  107. 107}
  108. 108
  109. 109///|
  110. 110fn check_color_match(
  111. 111 data : Bytes,
  112. 112 off : Int32,
  113. 113 ch : Int32,
  114. 114 target : Array[Byte],
  115. 115 tolerance : Int32,
  116. 116) -> Bool {
  117. 117● for c in 0..<ch {
  118. 118● let diff = (data[off + c].to_int() - target[c].to_int()).abs()
  119. 119 if diff > tolerance {
  120. 120● return false
  121. 121 }
  122. 122 }
  123. 123 true
  124. 124}