image.domain\util\image_combine.mbt

97.8% · 44 / 45 points · 130 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_combine
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 图像混合与翻转(纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// Alpha 混合两张图像。
  10. 10/// alpha: 0.0 = 完全显示 img1, 1.0 = 完全显示 img2
  11. 11/// 要求两张图像尺寸和通道数相同。
  12. 12pub fn alpha_blend(
  13. 13 img1 : @types.Image,
  14. 14 img2 : @types.Image,
  15. 15 alpha : Float,
  16. 16) -> @types.Image raise @types.LoadError {
  17. 17● if img1.width != img2.width || img1.height != img2.height {
  18. 18● raise @types.LoadError::DecodeFailed("alpha_blend: 图像尺寸不同")
  19. 19 }
  20. 20 if img1.channels != img2.channels {
  21. 21● raise @types.LoadError::DecodeFailed("alpha_blend: 图像通道数不同")
  22. 22 }
  23. 23 let n = img1.width * img1.height * img1.channels
  24. 24 let result : Array[Byte] = []
  25. 25● let a = alpha.clamp(min=0.0F, max=1.0F)
  26. 26 for i in 0..<n {
  27. 27● let v1 = img1.data[i].to_int()
  28. 28● let v2 = img2.data[i].to_int()
  29. 29● let blended = (Float::from_int(v1) * (1.0F - a) + Float::from_int(v2) * a).to_int()
  30. 30● result.push(blended.clamp(min=0, max=255).to_byte())
  31. 31 }
  32. 32 @types.Image::{
  33. 33 width: img1.width,
  34. 34 height: img1.height,
  35. 35 channels: img1.channels,
  36. 36● data: Bytes::from_array(result),
  37. 37 }
  38. 38}
  39. 39
  40. 40///|
  41. 41/// 使用掩码混合两张图像。
  42. 42/// 掩码为单通道灰度图,白色(255)显示 img2,黑色(0)显示 img1。
  43. 43/// 要求掩码与图像尺寸相同。
  44. 44pub fn mask_blend(
  45. 45 img1 : @types.Image,
  46. 46 img2 : @types.Image,
  47. 47 mask : @types.Image,
  48. 48) -> @types.Image raise @types.LoadError {
  49. 49● if img1.width != img2.width || img1.height != img2.height {
  50. 50● raise @types.LoadError::DecodeFailed("mask_blend: 图像尺寸不同")
  51. 51 }
  52. 52 if img1.channels != img2.channels {
  53. 53○ raise @types.LoadError::DecodeFailed("mask_blend: 图像通道数不同")
  54. 54 }
  55. 55 if mask.channels != 1 {
  56. 56● raise @types.LoadError::DecodeFailed("mask_blend: 掩码必须为单通道")
  57. 57 }
  58. 58 if mask.width != img1.width || mask.height != img1.height {
  59. 59● raise @types.LoadError::DecodeFailed("mask_blend: 掩码尺寸不同")
  60. 60 }
  61. 61 let w = img1.width
  62. 62 let h = img1.height
  63. 63 let ch = img1.channels
  64. 64 let result : Array[Byte] = []
  65. 65 for y in 0..<h {
  66. 66● for x in 0..<w {
  67. 67● let m = Float::from_int(mask.data[y * w + x].to_int()) / 255.0F
  68. 68 for c in 0..<ch {
  69. 69● let idx = (y * w + x) * ch + c
  70. 70● let v1 = img1.data[idx].to_int()
  71. 71● let v2 = img2.data[idx].to_int()
  72. 72● let blended = (Float::from_int(v1) * (1.0F - m) +
  73. 73● Float::from_int(v2) * m).to_int()
  74. 74● result.push(blended.clamp(min=0, max=255).to_byte())
  75. 75 }
  76. 76 }
  77. 77 }
  78. 78 @types.Image::{
  79. 79 width: w,
  80. 80 height: h,
  81. 81 channels: ch,
  82. 82● data: Bytes::from_array(result),
  83. 83 }
  84. 84}
  85. 85
  86. 86///|
  87. 87/// 水平翻转图像。
  88. 88pub fn flip_horizontal(img : @types.Image) -> @types.Image {
  89. 89● let w = img.width
  90. 90 let h = img.height
  91. 91 let ch = img.channels
  92. 92 let result : Array[Byte] = []
  93. 93 for y in 0..<h {
  94. 94● for x in 0..<w {
  95. 95● let src_x = w - 1 - x
  96. 96 for c in 0..<ch {
  97. 97● result.push(img.data[(y * w + src_x) * ch + c])
  98. 98 }
  99. 99 }
  100. 100 }
  101. 101 @types.Image::{
  102. 102 width: w,
  103. 103 height: h,
  104. 104 channels: ch,
  105. 105● data: Bytes::from_array(result),
  106. 106 }
  107. 107}
  108. 108
  109. 109///|
  110. 110/// 垂直翻转图像。
  111. 111pub fn flip_vertical(img : @types.Image) -> @types.Image {
  112. 112● let w = img.width
  113. 113 let h = img.height
  114. 114 let ch = img.channels
  115. 115 let result : Array[Byte] = []
  116. 116 for y in 0..<h {
  117. 117● let src_y = h - 1 - y
  118. 118 for x in 0..<w {
  119. 119● for c in 0..<ch {
  120. 120● result.push(img.data[(src_y * w + x) * ch + c])
  121. 121 }
  122. 122 }
  123. 123 }
  124. 124 @types.Image::{
  125. 125 width: w,
  126. 126 height: h,
  127. 127 channels: ch,
  128. 128● data: Bytes::from_array(result),
  129. 129 }
  130. 130}