image.domain\util\color_map.mbt

98.3% · 57 / 58 points · 179 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/color_map
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 色彩映射与 alpha 操作(纯 MoonBit):apply_lut / gradient_map / set_alpha / fill_alpha
  7. 7
  8. 8///|
  9. 9
  10. 10///|
  11. 11/// 应用查找表(LUT)。lut 为 256 个 Byte 的映射表,将每个像素值 v 替换为 lut[v]。
  12. 12/// 仅影响颜色通道,alpha 通道不变。
  13. 13pub fn apply_lut(
  14. 14 img : @types.Image,
  15. 15 lut : Array[Byte],
  16. 16) -> @types.Image raise @types.LoadError {
  17. 17● if lut.length() < 256 {
  18. 18● raise @types.LoadError::DecodeFailed("LUT 必须有 256 个条目")
  19. 19 }
  20. 20 let n = img.width * img.height
  21. 21 let ch = img.channels
  22. 22● let color_ch = if ch >= 3 { 3 } else { ch }
  23. 23● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  24. 24 for i in 0..<n {
  25. 25● for c in 0..<color_ch {
  26. 26● out[i * ch + c] = lut[img.data[i * ch + c].to_int()]
  27. 27 }
  28. 28 if ch == 4 {
  29. 29● out[i * ch + 3] = img.data[i * ch + 3]
  30. 30 }
  31. 31 }
  32. 32 @types.Image::{
  33. 33 width: img.width,
  34. 34 height: img.height,
  35. 35 channels: ch,
  36. 36● data: Bytes::from_array(out),
  37. 37 }
  38. 38}
  39. 39
  40. 40///|
  41. 41/// 渐变映射。将图像亮度映射到 color_stops 定义的颜色渐变。
  42. 42/// color_stops 为渐变颜色点,每个点是 (position, r, g, b),position ∈ [0, 255]。
  43. 43/// 返回 RGB 图像(channels=3)。
  44. 44pub fn gradient_map(
  45. 45 img : @types.Image,
  46. 46 color_stops : Array[(Int32, Byte, Byte, Byte)],
  47. 47) -> @types.Image raise @types.LoadError {
  48. 48● if color_stops.length() < 2 {
  49. 49● raise @types.LoadError::DecodeFailed(
  50. 50 "gradient_map 至少需要 2 个颜色点",
  51. 51 )
  52. 52 }
  53. 53 let n = img.width * img.height
  54. 54● let out : Array[Byte] = Array::make(n * 3, b'\x00')
  55. 55 // 预计算 256 个亮度的映射
  56. 56● let lut : Array[(Byte, Byte, Byte)] = Array::make(
  57. 57 256,
  58. 58 (b'\x00', b'\x00', b'\x00'),
  59. 59 )
  60. 60 for v in 0..<256 {
  61. 61● lut[v] = interp_gradient(v, color_stops)
  62. 62 }
  63. 63 for i in 0..<n {
  64. 64● let ch = img.channels
  65. 65 let luminance = if ch >= 3 {
  66. 66 // RGB -> luminance (0.299R + 0.587G + 0.114B)
  67. 67● let r = img.data[i * ch].to_int()
  68. 68● let g = img.data[i * ch + 1].to_int()
  69. 69● let b = img.data[i * ch + 2].to_int()
  70. 70 (r * 77 + g * 150 + b * 29) / 256
  71. 71 } else {
  72. 72● img.data[i * ch].to_int()
  73. 73 }
  74. 74 let (r, g, b) = lut[luminance]
  75. 75 out[i * 3] = r
  76. 76 out[i * 3 + 1] = g
  77. 77 out[i * 3 + 2] = b
  78. 78 }
  79. 79 @types.Image::{
  80. 80 width: img.width,
  81. 81 height: img.height,
  82. 82 channels: 3,
  83. 83● data: Bytes::from_array(out),
  84. 84 }
  85. 85}
  86. 86
  87. 87///|
  88. 88fn interp_gradient(
  89. 89 v : Int32,
  90. 90 stops : Array[(Int32, Byte, Byte, Byte)],
  91. 91) -> (Byte, Byte, Byte) {
  92. 92● let len = stops.length()
  93. 93 if v <= stops[0].0 {
  94. 94● return (stops[0].1, stops[0].2, stops[0].3)
  95. 95 }
  96. 96 if v >= stops[len - 1].0 {
  97. 97● return (stops[len - 1].1, stops[len - 1].2, stops[len - 1].3)
  98. 98 }
  99. 99 for i in 0..<(len - 1) {
  100. 100● let (p0, r0, g0, b0) = stops[i]
  101. 101 let (p1, r1, g1, b1) = stops[i + 1]
  102. 102 if v >= p0 && v <= p1 {
  103. 103● let range = p1 - p0
  104. 104 if range == 0 {
  105. 105○ return (r0, g0, b0)
  106. 106 }
  107. 107 let t = v - p0
  108. 108● let r = r0.to_int() + (r1.to_int() - r0.to_int()) * t / range
  109. 109● let g = g0.to_int() + (g1.to_int() - g0.to_int()) * t / range
  110. 110● let b = b0.to_int() + (b1.to_int() - b0.to_int()) * t / range
  111. 111● return (@types.clamp_byte(r), @types.clamp_byte(g), @types.clamp_byte(b))
  112. 112 }
  113. 113 }
  114. 114 (b'\x00', b'\x00', b'\x00')
  115. 115}
  116. 116
  117. 117///|
  118. 118/// 设置所有像素的 alpha 通道为指定值。图像必须是 RGBA(channels=4)。
  119. 119pub fn set_alpha(
  120. 120 img : @types.Image,
  121. 121 alpha : Byte,
  122. 122) -> @types.Image raise @types.LoadError {
  123. 123● if img.channels != 4 {
  124. 124● raise @types.LoadError::DecodeFailed(
  125. 125 "set_alpha 需要 RGBA 图像(channels=4)",
  126. 126 )
  127. 127 }
  128. 128 let n = img.width * img.height
  129. 129● let out : Array[Byte] = Array::make(n * 4, b'\x00')
  130. 130 for i in 0..<n {
  131. 131● out[i * 4] = img.data[i * 4]
  132. 132 out[i * 4 + 1] = img.data[i * 4 + 1]
  133. 133 out[i * 4 + 2] = img.data[i * 4 + 2]
  134. 134 out[i * 4 + 3] = alpha
  135. 135 }
  136. 136 @types.Image::{
  137. 137 width: img.width,
  138. 138 height: img.height,
  139. 139 channels: 4,
  140. 140● data: Bytes::from_array(out),
  141. 141 }
  142. 142}
  143. 143
  144. 144///|
  145. 145/// 用指定颜色填充 alpha 通道。将 alpha < threshold 的像素的 RGB 设为指定颜色。
  146. 146pub fn fill_alpha(
  147. 147 img : @types.Image,
  148. 148 threshold : Int32,
  149. 149 fill_r : Byte,
  150. 150 fill_g : Byte,
  151. 151 fill_b : Byte,
  152. 152) -> @types.Image raise @types.LoadError {
  153. 153● if img.channels != 4 {
  154. 154● raise @types.LoadError::DecodeFailed(
  155. 155 "fill_alpha 需要 RGBA 图像(channels=4)",
  156. 156 )
  157. 157 }
  158. 158 let n = img.width * img.height
  159. 159● let out : Array[Byte] = Array::make(n * 4, b'\x00')
  160. 160 for i in 0..<n {
  161. 161● let a = img.data[i * 4 + 3].to_int()
  162. 162 if a < threshold {
  163. 163● out[i * 4] = fill_r
  164. 164 out[i * 4 + 1] = fill_g
  165. 165 out[i * 4 + 2] = fill_b
  166. 166 } else {
  167. 167● out[i * 4] = img.data[i * 4]
  168. 168 out[i * 4 + 1] = img.data[i * 4 + 1]
  169. 169 out[i * 4 + 2] = img.data[i * 4 + 2]
  170. 170 }
  171. 171 out[i * 4 + 3] = img.data[i * 4 + 3]
  172. 172 }
  173. 173 @types.Image::{
  174. 174 width: img.width,
  175. 175 height: img.height,
  176. 176 channels: 4,
  177. 177● data: Bytes::from_array(out),
  178. 178 }
  179. 179}