image.process\filter\filter.mbt

90.1% · 182 / 202 points · 539 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/// # 滤波 filter
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 图像滤波/卷积(纯 MoonBit)
  7. 7// box_blur / gaussian_blur / sharpen / convolve
  8. 8
  9. 9///|
  10. 10/// 盒模糊(均值滤波)。radius 为模糊半径,窗口大小 = 2*radius+1。
  11. 11/// 使用可分离滑动窗口,O(n) 复杂度。仅模糊颜色通道,alpha 不变。
  12. 12pub fn box_blur(
  13. 13 img : @types.Image,
  14. 14 radius : Int32,
  15. 15) -> @types.Image raise @types.LoadError {
  16. 16 // 输入验证
  17. 17● if img.width <= 0 || img.height <= 0 {
  18. 18● raise @types.LoadError::DecodeFailed("box_blur: 图像尺寸必须大于0")
  19. 19 }
  20. 20 if img.channels < 1 || img.channels > 4 {
  21. 21● raise @types.LoadError::DecodeFailed(
  22. 22 "box_blur: 通道数必须在1-4之间",
  23. 23 )
  24. 24 }
  25. 25 let expected_size = img.width * img.height * img.channels
  26. 26● if img.data.length() != expected_size {
  27. 27● raise @types.LoadError::DecodeFailed(
  28. 28 "box_blur: 数据大小不匹配,期望 " +
  29. 29● expected_size.to_string() +
  30. 30 ",实际 " +
  31. 31● img.data.length().to_string(),
  32. 32 )
  33. 33 }
  34. 34 if radius < 0 {
  35. 35● raise @types.LoadError::DecodeFailed("box_blur: radius不能为负")
  36. 36 }
  37. 37 if radius <= 0 {
  38. 38● return img
  39. 39 }
  40. 40 let w = img.width
  41. 41 let h = img.height
  42. 42 let ch = img.channels
  43. 43● let color_ch = if ch >= 3 { 3 } else { ch }
  44. 44 let n = w * h
  45. 45 let win_size = 2 * radius + 1
  46. 46 // 水平模糊:img -> temp
  47. 47● let temp : Array[Int32] = Array::make(n * color_ch, 0)
  48. 48 for y in 0..<h {
  49. 49● for c in 0..<color_ch {
  50. 50● let mut sum = 0
  51. 51 for dx in 0..<win_size {
  52. 52● let x = @types.clamp_coord(dx - radius, w)
  53. 53● sum += img.data[(y * w + x) * ch + c].to_int()
  54. 54 }
  55. 55 for x in 0..<w {
  56. 56● temp[(y * w + x) * color_ch + c] = sum / win_size
  57. 57● let x_out = @types.clamp_coord(x - radius, w)
  58. 58● let x_in = @types.clamp_coord(x + radius + 1, w)
  59. 59 sum = sum -
  60. 60● img.data[(y * w + x_out) * ch + c].to_int() +
  61. 61● img.data[(y * w + x_in) * ch + c].to_int()
  62. 62 }
  63. 63 }
  64. 64 }
  65. 65 // 垂直模糊:temp -> out
  66. 66● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  67. 67 for x in 0..<w {
  68. 68● for c in 0..<color_ch {
  69. 69● let mut sum = 0
  70. 70 for dy in 0..<win_size {
  71. 71● let y = @types.clamp_coord(dy - radius, h)
  72. 72 sum += temp[(y * w + x) * color_ch + c]
  73. 73 }
  74. 74 for y in 0..<h {
  75. 75● out[(y * w + x) * ch + c] = (sum / win_size).to_byte()
  76. 76● let y_out = @types.clamp_coord(y - radius, h)
  77. 77● let y_in = @types.clamp_coord(y + radius + 1, h)
  78. 78 sum = sum -
  79. 79 temp[(y_out * w + x) * color_ch + c] +
  80. 80 temp[(y_in * w + x) * color_ch + c]
  81. 81 }
  82. 82 }
  83. 83 }
  84. 84 // 复制 alpha
  85. 85 if ch == 4 {
  86. 86● for i in 0..<n {
  87. 87● out[i * ch + 3] = img.data[i * ch + 3]
  88. 88 }
  89. 89 }
  90. 90 @types.Image::{
  91. 91 width: w,
  92. 92 height: h,
  93. 93 channels: ch,
  94. 94● data: Bytes::from_array(out),
  95. 95 }
  96. 96}
  97. 97
  98. 98///|
  99. 99/// 高斯模糊。radius 为核半径,sigma 为标准差。
  100. 100/// 使用可分离高斯核,O(n*radius) 复杂度。仅模糊颜色通道,alpha 不变。
  101. 101/// 高斯模糊。radius 为核半径,sigma 为标准差。 Gaussian blur. radius is kernel radius, sigma is standard deviation.
  102. 102/// 使用可分离高斯核,O(n*radius) 复杂度。仅模糊颜色通道,alpha 不变。 Uses separable Gaussian kernel, O(n*radius) complexity. Only blurs color channels, alpha unchanged.
  103. 103///
  104. 104/// ```mbt check
  105. 105/// test "doc_gaussian_blur" {
  106. 106/// try {
  107. 107/// let data = Bytes::from_array([b'\xff', b'\x00', b'\x00'])
  108. 108/// let img = @types.Image::{ width: 1, height: 1, channels: 3, data, }
  109. 109/// let blurred = @filter.gaussian_blur(img, 1, 1.0)
  110. 110/// assert_eq(blurred.width, 1)
  111. 111/// } catch {
  112. 112/// _ => assert_true(false)
  113. 113/// }
  114. 114/// }
  115. 115/// ```
  116. 116pub fn gaussian_blur(
  117. 117 img : @types.Image,
  118. 118 radius : Int32,
  119. 119 sigma : Float,
  120. 120) -> @types.Image raise @types.LoadError {
  121. 121 // 输入验证
  122. 122● if img.width <= 0 || img.height <= 0 {
  123. 123● raise @types.LoadError::DecodeFailed(
  124. 124 "gaussian_blur: 图像尺寸必须大于0",
  125. 125 )
  126. 126 }
  127. 127 if img.channels < 1 || img.channels > 4 {
  128. 128● raise @types.LoadError::DecodeFailed(
  129. 129 "gaussian_blur: 通道数必须在1-4之间",
  130. 130 )
  131. 131 }
  132. 132 let expected_size = img.width * img.height * img.channels
  133. 133● if img.data.length() != expected_size {
  134. 134○ raise @types.LoadError::DecodeFailed(
  135. 135 "gaussian_blur: 数据大小不匹配,期望 " +
  136. 136○ expected_size.to_string() +
  137. 137 ",实际 " +
  138. 138○ img.data.length().to_string(),
  139. 139 )
  140. 140 }
  141. 141 if radius < 0 {
  142. 142● raise @types.LoadError::DecodeFailed("gaussian_blur: radius不能为负")
  143. 143 }
  144. 144 if sigma <= 0.0F {
  145. 145● raise @types.LoadError::DecodeFailed("gaussian_blur: sigma必须大于0")
  146. 146 }
  147. 147 if radius <= 0 {
  148. 148● return img
  149. 149 }
  150. 150 let w = img.width
  151. 151 let h = img.height
  152. 152 let ch = img.channels
  153. 153● let color_ch = if ch >= 3 { 3 } else { ch }
  154. 154 let n = w * h
  155. 155 let win_size = 2 * radius + 1
  156. 156 // 生成 1D 高斯核
  157. 157● let kernel : Array[Float] = Array::make(win_size, 0.0F)
  158. 158 let two_sigma_sq = 2.0F * sigma * sigma
  159. 159 let mut ksum = 0.0F
  160. 160 for i in 0..<win_size {
  161. 161● let x = Float::from_int(i - radius)
  162. 162● let val = @math.expf(-(x * x) / two_sigma_sq)
  163. 163 kernel[i] = val
  164. 164 ksum += val
  165. 165 }
  166. 166 // 归一化
  167. 167 for i in 0..<win_size {
  168. 168● kernel[i] = kernel[i] / ksum
  169. 169 }
  170. 170 // 预计算像素 Float 数组,避免内层循环重复类型转换
  171. 171● let src : Array[Float] = Array::make(n * color_ch, 0.0F)
  172. 172 for i in 0..<n {
  173. 173● for c in 0..<color_ch {
  174. 174● src[i * color_ch + c] = Float::from_int(img.data[i * ch + c].to_int())
  175. 175 }
  176. 176 }
  177. 177 // 水平模糊:src -> temp (Float),分离内部/边界区域
  178. 178● let temp : Array[Float] = Array::make(n * color_ch, 0.0F)
  179. 179● let x_inner_start = if radius >= w { w } else { radius }
  180. 180● let x_inner_end = if radius >= w { w } else { w - radius }
  181. 181 for y in 0..<h {
  182. 182● let row_base = y * w
  183. 183 for c in 0..<color_ch {
  184. 184 // 内部区域:x >= radius && x < w - radius,无需 @types.clamp_coord
  185. 185● for x in x_inner_start..<x_inner_end {
  186. 186● let mut acc = 0.0F
  187. 187 let base = (row_base + x - radius) * color_ch + c
  188. 188 for k in 0..<win_size {
  189. 189● acc += kernel[k] * src[base + k * color_ch]
  190. 190 }
  191. 191 temp[(row_base + x) * color_ch + c] = acc
  192. 192 }
  193. 193 // 左边界:x < radius
  194. 194 for x in 0..<x_inner_start {
  195. 195● let mut acc = 0.0F
  196. 196 for k in 0..<win_size {
  197. 197● let sx = @types.clamp_coord(x + k - radius, w)
  198. 198 acc += kernel[k] * src[(row_base + sx) * color_ch + c]
  199. 199 }
  200. 200 temp[(row_base + x) * color_ch + c] = acc
  201. 201 }
  202. 202 // 右边界:x >= w - radius
  203. 203 for x in x_inner_end..<w {
  204. 204● let mut acc = 0.0F
  205. 205 for k in 0..<win_size {
  206. 206● let sx = @types.clamp_coord(x + k - radius, w)
  207. 207 acc += kernel[k] * src[(row_base + sx) * color_ch + c]
  208. 208 }
  209. 209 temp[(row_base + x) * color_ch + c] = acc
  210. 210 }
  211. 211 }
  212. 212 }
  213. 213 // 垂直模糊:temp -> out,分离内部/边界区域
  214. 214● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  215. 215● let y_inner_start = if radius >= h { h } else { radius }
  216. 216● let y_inner_end = if radius >= h { h } else { h - radius }
  217. 217 for x in 0..<w {
  218. 218● for c in 0..<color_ch {
  219. 219 // 内部区域:y >= radius && y < h - radius
  220. 220● for y in y_inner_start..<y_inner_end {
  221. 221● let mut acc = 0.0F
  222. 222 let base = ((y - radius) * w + x) * color_ch + c
  223. 223 for k in 0..<win_size {
  224. 224● acc += kernel[k] * temp[base + k * w * color_ch]
  225. 225 }
  226. 226 out[(y * w + x) * ch + c] = (acc + 0.5F)
  227. 227● .to_int()
  228. 228● .clamp(min=0, max=255)
  229. 229● .to_byte()
  230. 230 }
  231. 231 // 上边界:y < radius
  232. 232 for y in 0..<y_inner_start {
  233. 233● let mut acc = 0.0F
  234. 234 for k in 0..<win_size {
  235. 235● let sy = @types.clamp_coord(y + k - radius, h)
  236. 236 acc += kernel[k] * temp[(sy * w + x) * color_ch + c]
  237. 237 }
  238. 238 out[(y * w + x) * ch + c] = (acc + 0.5F)
  239. 239● .to_int()
  240. 240● .clamp(min=0, max=255)
  241. 241● .to_byte()
  242. 242 }
  243. 243 // 下边界:y >= h - radius
  244. 244 for y in y_inner_end..<h {
  245. 245● let mut acc = 0.0F
  246. 246 for k in 0..<win_size {
  247. 247● let sy = @types.clamp_coord(y + k - radius, h)
  248. 248 acc += kernel[k] * temp[(sy * w + x) * color_ch + c]
  249. 249 }
  250. 250 out[(y * w + x) * ch + c] = (acc + 0.5F)
  251. 251● .to_int()
  252. 252● .clamp(min=0, max=255)
  253. 253● .to_byte()
  254. 254 }
  255. 255 }
  256. 256 }
  257. 257 // 复制 alpha
  258. 258 if ch == 4 {
  259. 259○ for i in 0..<n {
  260. 260○ out[i * ch + 3] = img.data[i * ch + 3]
  261. 261 }
  262. 262 }
  263. 263 @types.Image::{
  264. 264 width: w,
  265. 265 height: h,
  266. 266 channels: ch,
  267. 267● data: Bytes::from_array(out),
  268. 268 }
  269. 269}
  270. 270
  271. 271///|
  272. 272/// 锐化。amount 控制锐化强度(0.0=原图,1.0=强锐化)。
  273. 273/// 使用拉普拉斯锐化卷积核。仅处理颜色通道,alpha 不变。
  274. 274pub fn sharpen(
  275. 275 img : @types.Image,
  276. 276 amount : Float,
  277. 277) -> @types.Image raise @types.LoadError {
  278. 278 // 输入验证
  279. 279● if img.width <= 0 || img.height <= 0 {
  280. 280● raise @types.LoadError::DecodeFailed("sharpen: 图像尺寸必须大于0")
  281. 281 }
  282. 282 if img.channels < 1 || img.channels > 4 {
  283. 283● raise @types.LoadError::DecodeFailed("sharpen: 通道数必须在1-4之间")
  284. 284 }
  285. 285 let expected_size = img.width * img.height * img.channels
  286. 286● if img.data.length() != expected_size {
  287. 287○ raise @types.LoadError::DecodeFailed(
  288. 288 "sharpen: 数据大小不匹配,期望 " +
  289. 289○ expected_size.to_string() +
  290. 290 ",实际 " +
  291. 291○ img.data.length().to_string(),
  292. 292 )
  293. 293 }
  294. 294 if amount < 0.0F {
  295. 295● raise @types.LoadError::DecodeFailed("sharpen: amount不能为负")
  296. 296 }
  297. 297
  298. 298 let w = img.width
  299. 299 let h = img.height
  300. 300 let ch = img.channels
  301. 301● let color_ch = if ch >= 3 { 3 } else { ch }
  302. 302 let n = w * h
  303. 303 let center = 1.0F + 4.0F * amount
  304. 304● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  305. 305 // 预计算像素 Int 数组
  306. 306● let px : Array[Int32] = Array::make(n * ch, 0)
  307. 307 for i in 0..<(n * ch) {
  308. 308● px[i] = img.data[i].to_int()
  309. 309 }
  310. 310 // 内部区域:1 <= x < w-1 && 1 <= y < h-1,无需 @types.clamp_coord
  311. 311 for y in 1..<(h - 1) {
  312. 312● for x in 1..<(w - 1) {
  313. 313● let base = (y * w + x) * ch
  314. 314 let base_l = base - ch
  315. 315 let base_r = base + ch
  316. 316 let base_t = base - w * ch
  317. 317 let base_b = base + w * ch
  318. 318 for c in 0..<color_ch {
  319. 319● let cx = px[base + c]
  320. 320 let left = px[base_l + c]
  321. 321 let right = px[base_r + c]
  322. 322 let top = px[base_t + c]
  323. 323 let bottom = px[base_b + c]
  324. 324● let val = Float::from_int(cx) * center -
  325. 325● amount * Float::from_int(left + right + top + bottom)
  326. 326● out[base + c] = (val + 0.5F).to_int().clamp(min=0, max=255).to_byte()
  327. 327 }
  328. 328 }
  329. 329 }
  330. 330 // 边界区域:x==0 || x==w-1 || y==0 || y==h-1
  331. 331 for y in 0..<h {
  332. 332● for x in 0..<w {
  333. 333● if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 {
  334. 334● continue
  335. 335 }
  336. 336 let base = (y * w + x) * ch
  337. 337 for c in 0..<color_ch {
  338. 338● let cx = px[base + c]
  339. 339● let x_l = @types.clamp_coord(x - 1, w)
  340. 340● let x_r = @types.clamp_coord(x + 1, w)
  341. 341● let y_t = @types.clamp_coord(y - 1, h)
  342. 342● let y_b = @types.clamp_coord(y + 1, h)
  343. 343 let left = px[(y * w + x_l) * ch + c]
  344. 344 let right = px[(y * w + x_r) * ch + c]
  345. 345 let top = px[(y_t * w + x) * ch + c]
  346. 346 let bottom = px[(y_b * w + x) * ch + c]
  347. 347● let val = Float::from_int(cx) * center -
  348. 348● amount * Float::from_int(left + right + top + bottom)
  349. 349● out[base + c] = (val + 0.5F).to_int().clamp(min=0, max=255).to_byte()
  350. 350 }
  351. 351 }
  352. 352 }
  353. 353 // 复制 alpha
  354. 354 if ch == 4 {
  355. 355● for i in 0..<n {
  356. 356● out[i * ch + 3] = img.data[i * ch + 3]
  357. 357 }
  358. 358 }
  359. 359 @types.Image::{
  360. 360 width: w,
  361. 361 height: h,
  362. 362 channels: ch,
  363. 363● data: Bytes::from_array(out),
  364. 364 }
  365. 365}
  366. 366
  367. 367///|
  368. 368/// 中值滤波。ksize 为窗口大小(奇数)。有效去除椒盐噪声。
  369. 369/// 对每个像素,取 ksize×ksize 邻域的中值。仅滤波颜色通道,alpha 不变。
  370. 370pub fn median_blur(
  371. 371 img : @types.Image,
  372. 372 ksize : Int32,
  373. 373) -> @types.Image raise @types.LoadError {
  374. 374 // 输入验证
  375. 375● if img.width <= 0 || img.height <= 0 {
  376. 376● raise @types.LoadError::DecodeFailed(
  377. 377 "median_blur: 图像尺寸必须大于0",
  378. 378 )
  379. 379 }
  380. 380 if img.channels < 1 || img.channels > 4 {
  381. 381● raise @types.LoadError::DecodeFailed(
  382. 382 "median_blur: 通道数必须在1-4之间",
  383. 383 )
  384. 384 }
  385. 385 let expected_size = img.width * img.height * img.channels
  386. 386● if img.data.length() != expected_size {
  387. 387○ raise @types.LoadError::DecodeFailed(
  388. 388 "median_blur: 数据大小不匹配,期望 " +
  389. 389○ expected_size.to_string() +
  390. 390 ",实际 " +
  391. 391○ img.data.length().to_string(),
  392. 392 )
  393. 393 }
  394. 394 if ksize <= 0 {
  395. 395● raise @types.LoadError::DecodeFailed("median_blur: ksize必须大于0")
  396. 396 }
  397. 397 if ksize % 2 == 0 {
  398. 398● raise @types.LoadError::DecodeFailed("median_blur: ksize必须为奇数")
  399. 399 }
  400. 400 if ksize <= 1 {
  401. 401● return img
  402. 402 }
  403. 403 let radius = ksize / 2
  404. 404 let w = img.width
  405. 405 let h = img.height
  406. 406 let ch = img.channels
  407. 407● let color_ch = if ch >= 3 { 3 } else { ch }
  408. 408 let n = w * h
  409. 409● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  410. 410● let buf : Array[Int32] = Array::make(ksize * ksize, 0)
  411. 411 for y in 0..<h {
  412. 412● for x in 0..<w {
  413. 413● for c in 0..<color_ch {
  414. 414● let mut idx = 0
  415. 415 for dy in -radius..<=radius {
  416. 416● for dx in -radius..<=radius {
  417. 417● let sx = @types.clamp_coord(x + dx, w)
  418. 418● let sy = @types.clamp_coord(y + dy, h)
  419. 419● buf[idx] = img.data[(sy * w + sx) * ch + c].to_int()
  420. 420 idx += 1
  421. 421 }
  422. 422 }
  423. 423 // 部分排序找中值(不需要完整排序)
  424. 424 let mid = ksize * ksize / 2
  425. 425 let mut lo = 0
  426. 426 let mut hi = ksize * ksize - 1
  427. 427 while lo < hi {
  428. 428● let pivot = buf[(lo + hi) / 2]
  429. 429 let mut i = lo
  430. 430 let mut j = hi
  431. 431 while i <= j {
  432. 432● while buf[i] < pivot {
  433. 433● i += 1
  434. 434 }
  435. 435 while buf[j] > pivot {
  436. 436● j -= 1
  437. 437 }
  438. 438 if i <= j {
  439. 439● let tmp = buf[i]
  440. 440 buf[i] = buf[j]
  441. 441 buf[j] = tmp
  442. 442 i += 1
  443. 443 j -= 1
  444. 444 }
  445. 445 }
  446. 446 if mid <= j {
  447. 447● hi = j
  448. 448● } else if mid >= i {
  449. 449● lo = i
  450. 450 } else {
  451. 451● lo = mid
  452. 452 hi = mid
  453. 453 }
  454. 454 }
  455. 455● out[(y * w + x) * ch + c] = buf[mid].to_byte()
  456. 456 }
  457. 457 }
  458. 458 }
  459. 459 // 复制 alpha
  460. 460 if ch == 4 {
  461. 461● for i in 0..<n {
  462. 462● out[i * ch + 3] = img.data[i * ch + 3]
  463. 463 }
  464. 464 }
  465. 465 @types.Image::{
  466. 466 width: w,
  467. 467 height: h,
  468. 468 channels: ch,
  469. 469● data: Bytes::from_array(out),
  470. 470 }
  471. 471}
  472. 472
  473. 473///|
  474. 474/// 通用卷积。kernel 为 3×3 卷积核(9 个 Float 值,行优先)。
  475. 475/// 边缘采用 clamp 策略。bias 为偏移量,scale 为缩放因子。
  476. 476pub fn convolve(
  477. 477 img : @types.Image,
  478. 478 kernel : Array[Float],
  479. 479 bias : Float,
  480. 480 scale : Float,
  481. 481) -> @types.Image raise @types.LoadError {
  482. 482 // 输入验证
  483. 483● if img.width <= 0 || img.height <= 0 {
  484. 484● raise @types.LoadError::DecodeFailed("convolve: 图像尺寸必须大于0")
  485. 485 }
  486. 486 if img.channels < 1 || img.channels > 4 {
  487. 487● raise @types.LoadError::DecodeFailed(
  488. 488 "convolve: 通道数必须在1-4之间",
  489. 489 )
  490. 490 }
  491. 491 let expected_size = img.width * img.height * img.channels
  492. 492● if img.data.length() != expected_size {
  493. 493○ raise @types.LoadError::DecodeFailed(
  494. 494 "convolve: 数据大小不匹配,期望 " +
  495. 495○ expected_size.to_string() +
  496. 496 ",实际 " +
  497. 497○ img.data.length().to_string(),
  498. 498 )
  499. 499 }
  500. 500● if kernel.length() != 9 {
  501. 501○ raise @types.LoadError::DecodeFailed(
  502. 502 "convolve: kernel 必须为 9 个值(3x3)",
  503. 503 )
  504. 504 }
  505. 505 if scale == 0.0F {
  506. 506● raise @types.LoadError::DecodeFailed("convolve: scale不能为0")
  507. 507 }
  508. 508 let ch = img.channels
  509. 509 let w = img.width
  510. 510 let h = img.height
  511. 511● let color_ch = if ch >= 3 { 3 } else { ch }
  512. 512● let out : Array[Byte] = Array::make(w * h * ch, b'\x00')
  513. 513 for y in 0..<h {
  514. 514● for x in 0..<w {
  515. 515● for c in 0..<color_ch {
  516. 516● let mut acc = 0.0F
  517. 517 for ky in 0..<3 {
  518. 518● for kx in 0..<3 {
  519. 519● let sx = @types.clamp_int(x + kx - 1, 0, w - 1)
  520. 520● let sy = @types.clamp_int(y + ky - 1, 0, h - 1)
  521. 521● let v = Float::from_int(img.data[(sy * w + sx) * ch + c].to_int())
  522. 522 acc += kernel[ky * 3 + kx] * v
  523. 523 }
  524. 524 }
  525. 525● let result = (acc * scale + bias).to_int()
  526. 526● out[(y * w + x) * ch + c] = @types.clamp_byte(result)
  527. 527 }
  528. 528 if ch == 4 {
  529. 529○ out[(y * w + x) * ch + 3] = img.data[(y * w + x) * ch + 3]
  530. 530 }
  531. 531 }
  532. 532 }
  533. 533 @types.Image::{
  534. 534 width: w,
  535. 535 height: h,
  536. 536 channels: ch,
  537. 537● data: Bytes::from_array(out),
  538. 538 }
  539. 539}