image.codec\color\color_convert.mbt

100% · 77 / 77 points · 258 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/// # 色彩转换 color/color_convert
  3. 3///
  4. 4/// 编解码工具模块 Codec utility module
  5. 5
  6. 6// 纯 MoonBit 色彩模型转换(全目标可用:native/wasm/js)
  7. 7// 仅依赖 @types,不依赖 @core/FFI
  8. 8// 移植自 src/process/color/color_convert.mbt:1-162 的 5 个 8-bit Image 色彩转换函数
  9. 9
  10. 10///|
  11. 11/// 转为灰度图(channels=1,纯 MoonBit,全目标可用) Convert to grayscale (channels=1, pure MoonBit, all targets)
  12. 12/// 使用 ITU-R BT.601 亮度公式: Y = 0.299R + 0.587G + 0.114B Uses ITU-R BT.601 luma formula: Y = 0.299R + 0.587G + 0.114B
  13. 13///
  14. 14/// ```mbt check
  15. 15/// test "doc_to_grayscale_pure" {
  16. 16/// let data = Bytes::from_array([b'\xff', b'\x00', b'\x00'])
  17. 17/// let img = @types.Image::{ width: 1, height: 1, channels: 3, data, }
  18. 18/// let gray = @color.to_grayscale_pure(img)
  19. 19/// assert_eq(gray.channels, 1)
  20. 20/// assert_eq(gray.width, 1)
  21. 21/// }
  22. 22/// ```
  23. 23pub fn to_grayscale_pure(img : @types.Image) -> @types.Image {
  24. 24● if img.channels == 1 {
  25. 25● return img
  26. 26 }
  27. 27 let pixel_count = img.width * img.height
  28. 28 let ch = img.channels
  29. 29● let data_int : Array[Int32] = Array::make(pixel_count * ch, 0)
  30. 30 for i in 0..<(pixel_count * ch) {
  31. 31● data_int[i] = img.data[i].to_int()
  32. 32 }
  33. 33● let out : Array[Byte] = Array::make(pixel_count, b'\x00')
  34. 34 for i in 0..<pixel_count {
  35. 35● let r = data_int[i * ch]
  36. 36 let g = data_int[i * ch + 1]
  37. 37 let b = data_int[i * ch + 2]
  38. 38 let gray = (r * 299 + g * 587 + b * 114) / 1000
  39. 39● out[i] = gray.to_byte()
  40. 40 }
  41. 41 @types.Image::{
  42. 42 width: img.width,
  43. 43 height: img.height,
  44. 44 channels: 1,
  45. 45● data: Bytes::from_array(out),
  46. 46 }
  47. 47}
  48. 48
  49. 49///|
  50. 50/// 去除 alpha 通道(RGBA → RGB,channels=3,纯 MoonBit,全目标可用)
  51. 51pub fn to_rgb_pure(img : @types.Image) -> @types.Image {
  52. 52● if img.channels == 3 {
  53. 53● return img
  54. 54 }
  55. 55 if img.channels < 3 {
  56. 56● return img
  57. 57 }
  58. 58 let pixel_count = img.width * img.height
  59. 59● let out : Array[Byte] = Array::make(pixel_count * 3, b'\x00')
  60. 60 for i in 0..<pixel_count {
  61. 61● out[i * 3] = img.data[i * img.channels]
  62. 62 out[i * 3 + 1] = img.data[i * img.channels + 1]
  63. 63 out[i * 3 + 2] = img.data[i * img.channels + 2]
  64. 64 }
  65. 65 @types.Image::{
  66. 66 width: img.width,
  67. 67 height: img.height,
  68. 68 channels: 3,
  69. 69● data: Bytes::from_array(out),
  70. 70 }
  71. 71}
  72. 72
  73. 73///|
  74. 74/// 添加 alpha 通道(RGB → RGBA,channels=4,alpha=255,纯 MoonBit,全目标可用)
  75. 75pub fn to_rgba_pure(img : @types.Image) -> @types.Image {
  76. 76● if img.channels == 4 {
  77. 77● return img
  78. 78 }
  79. 79 let pixel_count = img.width * img.height
  80. 80● let out : Array[Byte] = Array::make(pixel_count * 4, b'\x00')
  81. 81 for i in 0..<pixel_count {
  82. 82● let src_ch = img.channels
  83. 83 out[i * 4] = img.data[i * src_ch]
  84. 84 out[i * 4 + 1] = if src_ch >= 2 {
  85. 85● img.data[i * src_ch + 1]
  86. 86 } else {
  87. 87● b'\x00'
  88. 88 }
  89. 89 out[i * 4 + 2] = if src_ch >= 3 {
  90. 90● img.data[i * src_ch + 2]
  91. 91 } else {
  92. 92● b'\x00'
  93. 93 }
  94. 94 out[i * 4 + 3] = b'\xFF'
  95. 95 }
  96. 96 @types.Image::{
  97. 97 width: img.width,
  98. 98 height: img.height,
  99. 99 channels: 4,
  100. 100● data: Bytes::from_array(out),
  101. 101 }
  102. 102}
  103. 103
  104. 104///|
  105. 105/// 按 stb_image 语义转换通道数(纯 MoonBit,全目标可用) Convert channels following stb_image semantics (pure MoonBit, all targets)
  106. 106/// - 1: 灰度(BT.601 亮度公式) Grayscale (BT.601 luma formula)
  107. 107/// - 2: 灰度 + alpha(alpha=255 若源无 alpha) Grayscale + alpha (alpha=255 if source has no alpha)
  108. 108/// - 3: RGB(丢弃 alpha,灰度展开为 R=G=B) RGB (discard alpha, grayscale expanded to R=G=B)
  109. 109/// - 4: RGBA(alpha=255 若源无 alpha,灰度展开为 R=G=B) RGBA (alpha=255 if source has no alpha, grayscale expanded to R=G=B)
  110. 110///
  111. 111/// ```mbt check
  112. 112/// test "doc_convert_channels_pure" {
  113. 113/// try {
  114. 114/// let data = Bytes::from_array([b'\xff', b'\x00', b'\x00'])
  115. 115/// let img = @types.Image::{ width: 1, height: 1, channels: 3, data, }
  116. 116/// let rgba = @color.convert_channels_pure(img, 4)
  117. 117/// assert_eq(rgba.channels, 4)
  118. 118/// } catch {
  119. 119/// _ => assert_true(false)
  120. 120/// }
  121. 121/// }
  122. 122/// ```
  123. 123pub fn convert_channels_pure(
  124. 124 img : @types.Image,
  125. 125 req_channels : Int32,
  126. 126) -> @types.Image raise @types.LoadError {
  127. 127● if req_channels == img.channels {
  128. 128● return img
  129. 129 }
  130. 130 let pixel_count = img.width * img.height
  131. 131 let src_ch = img.channels
  132. 132● match req_channels {
  133. 133● 1 => to_grayscale_pure(img)
  134. 134● 2 => {
  135. 135● let out : Array[Byte] = Array::make(pixel_count * 2, b'\x00')
  136. 136 for i in 0..<pixel_count {
  137. 137● let gray = if src_ch >= 3 {
  138. 138● let r = img.data[i * src_ch].to_int()
  139. 139● let g = img.data[i * src_ch + 1].to_int()
  140. 140● let b = img.data[i * src_ch + 2].to_int()
  141. 141 (r * 299 + g * 587 + b * 114) / 1000
  142. 142 } else {
  143. 143● img.data[i * src_ch].to_int()
  144. 144 }
  145. 145 let alpha = if src_ch == 2 || src_ch == 4 {
  146. 146● img.data[i * src_ch + src_ch - 1].to_int()
  147. 147 } else {
  148. 148● 255
  149. 149 }
  150. 150● out[i * 2] = gray.to_byte()
  151. 151● out[i * 2 + 1] = alpha.to_byte()
  152. 152 }
  153. 153 @types.Image::{
  154. 154 width: img.width,
  155. 155 height: img.height,
  156. 156 channels: 2,
  157. 157● data: Bytes::from_array(out),
  158. 158 }
  159. 159 }
  160. 160 3 =>
  161. 161● if src_ch == 1 {
  162. 162● let out : Array[Byte] = Array::make(pixel_count * 3, b'\x00')
  163. 163 for i in 0..<pixel_count {
  164. 164● let v = img.data[i]
  165. 165 out[i * 3] = v
  166. 166 out[i * 3 + 1] = v
  167. 167 out[i * 3 + 2] = v
  168. 168 }
  169. 169 @types.Image::{
  170. 170 width: img.width,
  171. 171 height: img.height,
  172. 172 channels: 3,
  173. 173● data: Bytes::from_array(out),
  174. 174 }
  175. 175 } else {
  176. 176● to_rgb_pure(img)
  177. 177 }
  178. 178● 4 => to_rgba_pure(img)
  179. 179● _ => raise @types.LoadError::DecodeFailed("不支持的 req_channels 值")
  180. 180 }
  181. 181}
  182. 182
  183. 183///|
  184. 184/// 预乘 alpha(RGBA → RGBA premultiplied,纯 MoonBit,全目标可用)
  185. 185/// 要求 channels=4
  186. 186pub fn premultiply_alpha_pure(
  187. 187 img : @types.Image,
  188. 188) -> @types.Image raise @types.LoadError {
  189. 189● if img.channels != 4 {
  190. 190● raise @types.LoadError::DecodeFailed(
  191. 191 "premultiply_alpha_pure 需要 channels=4 (RGBA)",
  192. 192 )
  193. 193 }
  194. 194 let pixel_count = img.width * img.height
  195. 195● let data_int : Array[Int32] = Array::make(pixel_count * 4, 0)
  196. 196 for i in 0..<(pixel_count * 4) {
  197. 197● data_int[i] = img.data[i].to_int()
  198. 198 }
  199. 199● let out : Array[Byte] = Array::make(pixel_count * 4, b'\x00')
  200. 200 for i in 0..<pixel_count {
  201. 201● let r = data_int[i * 4]
  202. 202 let g = data_int[i * 4 + 1]
  203. 203 let b = data_int[i * 4 + 2]
  204. 204 let a = data_int[i * 4 + 3]
  205. 205● out[i * 4] = (r * a / 255).to_byte()
  206. 206● out[i * 4 + 1] = (g * a / 255).to_byte()
  207. 207● out[i * 4 + 2] = (b * a / 255).to_byte()
  208. 208● out[i * 4 + 3] = a.to_byte()
  209. 209 }
  210. 210 @types.Image::{
  211. 211 width: img.width,
  212. 212 height: img.height,
  213. 213 channels: 4,
  214. 214● data: Bytes::from_array(out),
  215. 215 }
  216. 216}
  217. 217
  218. 218///|
  219. 219/// 反预乘 alpha(RGBA premA → RGBA unpremultiplied,纯 MoonBit,全目标可用)
  220. 220/// 要求 channels=4
  221. 221pub fn unpremultiply_alpha_pure(
  222. 222 img : @types.Image,
  223. 223) -> @types.Image raise @types.LoadError {
  224. 224● if img.channels != 4 {
  225. 225● raise @types.LoadError::DecodeFailed(
  226. 226 "unpremultiply_alpha_pure 需要 channels=4 (RGBA)",
  227. 227 )
  228. 228 }
  229. 229 let pixel_count = img.width * img.height
  230. 230● let data_int : Array[Int32] = Array::make(pixel_count * 4, 0)
  231. 231 for i in 0..<(pixel_count * 4) {
  232. 232● data_int[i] = img.data[i].to_int()
  233. 233 }
  234. 234● let out : Array[Byte] = Array::make(pixel_count * 4, b'\x00')
  235. 235 for i in 0..<pixel_count {
  236. 236● let r = data_int[i * 4]
  237. 237 let g = data_int[i * 4 + 1]
  238. 238 let b = data_int[i * 4 + 2]
  239. 239 let a = data_int[i * 4 + 3]
  240. 240 if a == 0 {
  241. 241● out[i * 4] = b'\x00'
  242. 242 out[i * 4 + 1] = b'\x00'
  243. 243 out[i * 4 + 2] = b'\x00'
  244. 244 out[i * 4 + 3] = b'\x00'
  245. 245 } else {
  246. 246● out[i * 4] = (r * 255 / a).to_byte()
  247. 247● out[i * 4 + 1] = (g * 255 / a).to_byte()
  248. 248● out[i * 4 + 2] = (b * 255 / a).to_byte()
  249. 249● out[i * 4 + 3] = a.to_byte()
  250. 250 }
  251. 251 }
  252. 252 @types.Image::{
  253. 253 width: img.width,
  254. 254 height: img.height,
  255. 255 channels: 4,
  256. 256● data: Bytes::from_array(out),
  257. 257 }
  258. 258}