image.domain\util\image_compose.mbt

100% · 28 / 28 points · 132 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_compose
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 图像拼接与平铺(纯 MoonBit):hstack / vstack / tile / transpose
  7. 7
  8. 8///|
  9. 9/// 水平拼接。两图高度和通道数必须相同。
  10. 10pub fn hstack(
  11. 11 left : @types.Image,
  12. 12 right : @types.Image,
  13. 13) -> @types.Image raise @types.LoadError {
  14. 14● if left.height != right.height || left.channels != right.channels {
  15. 15● raise @types.LoadError::DecodeFailed(
  16. 16 "hstack 两图高度/通道数必须相同",
  17. 17 )
  18. 18 }
  19. 19 let ch = left.channels
  20. 20 let new_w = left.width + right.width
  21. 21 let h = left.height
  22. 22● let out : Array[Byte] = Array::make(new_w * h * ch, b'\x00')
  23. 23 for y in 0..<h {
  24. 24● for x in 0..<left.width {
  25. 25● let src_off = (y * left.width + x) * ch
  26. 26 let dst_off = (y * new_w + x) * ch
  27. 27 for c in 0..<ch {
  28. 28● out[dst_off + c] = left.data[src_off + c]
  29. 29 }
  30. 30 }
  31. 31 for x in 0..<right.width {
  32. 32● let src_off = (y * right.width + x) * ch
  33. 33 let dst_off = (y * new_w + left.width + x) * ch
  34. 34 for c in 0..<ch {
  35. 35● out[dst_off + c] = right.data[src_off + c]
  36. 36 }
  37. 37 }
  38. 38 }
  39. 39 @types.Image::{
  40. 40 width: new_w,
  41. 41 height: h,
  42. 42 channels: ch,
  43. 43● data: Bytes::from_array(out),
  44. 44 }
  45. 45}
  46. 46
  47. 47///|
  48. 48/// 垂直拼接。两图宽度和通道数必须相同。
  49. 49pub fn vstack(
  50. 50 top : @types.Image,
  51. 51 bottom : @types.Image,
  52. 52) -> @types.Image raise @types.LoadError {
  53. 53● if top.width != bottom.width || top.channels != bottom.channels {
  54. 54● raise @types.LoadError::DecodeFailed(
  55. 55 "vstack 两图宽度/通道数必须相同",
  56. 56 )
  57. 57 }
  58. 58 let ch = top.channels
  59. 59 let w = top.width
  60. 60 let new_h = top.height + bottom.height
  61. 61● let out : Array[Byte] = Array::make(w * new_h * ch, b'\x00')
  62. 62 for i in 0..<(w * top.height * ch) {
  63. 63● out[i] = top.data[i]
  64. 64 }
  65. 65 let top_size = w * top.height * ch
  66. 66 for i in 0..<(w * bottom.height * ch) {
  67. 67● out[top_size + i] = bottom.data[i]
  68. 68 }
  69. 69 @types.Image::{
  70. 70 width: w,
  71. 71 height: new_h,
  72. 72 channels: ch,
  73. 73● data: Bytes::from_array(out),
  74. 74 }
  75. 75}
  76. 76
  77. 77///|
  78. 78/// 平铺图像。将 img 在 x 方向重复 cols 次,y 方向重复 rows 次。
  79. 79pub fn tile(
  80. 80 img : @types.Image,
  81. 81 cols : Int32,
  82. 82 rows : Int32,
  83. 83) -> @types.Image raise @types.LoadError {
  84. 84● if cols <= 0 || rows <= 0 {
  85. 85● raise @types.LoadError::DecodeFailed("tile cols/rows 必须为正数")
  86. 86 }
  87. 87 let ch = img.channels
  88. 88 let new_w = img.width * cols
  89. 89 let new_h = img.height * rows
  90. 90● let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00')
  91. 91 for y in 0..<new_h {
  92. 92● for x in 0..<new_w {
  93. 93● let src_x = x % img.width
  94. 94 let src_y = y % img.height
  95. 95 let src_off = (src_y * img.width + src_x) * ch
  96. 96 let dst_off = (y * new_w + x) * ch
  97. 97 for c in 0..<ch {
  98. 98● out[dst_off + c] = img.data[src_off + c]
  99. 99 }
  100. 100 }
  101. 101 }
  102. 102 @types.Image::{
  103. 103 width: new_w,
  104. 104 height: new_h,
  105. 105 channels: ch,
  106. 106● data: Bytes::from_array(out),
  107. 107 }
  108. 108}
  109. 109
  110. 110///|
  111. 111/// 转置(沿主对角线翻转)。宽高互换。
  112. 112pub fn transpose(img : @types.Image) -> @types.Image {
  113. 113● let ch = img.channels
  114. 114 let new_w = img.height
  115. 115 let new_h = img.width
  116. 116● let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00')
  117. 117 for y in 0..<img.height {
  118. 118● for x in 0..<img.width {
  119. 119● let src_off = (y * img.width + x) * ch
  120. 120 let dst_off = (x * new_w + y) * ch
  121. 121 for c in 0..<ch {
  122. 122● out[dst_off + c] = img.data[src_off + c]
  123. 123 }
  124. 124 }
  125. 125 }
  126. 126 @types.Image::{
  127. 127 width: new_w,
  128. 128 height: new_h,
  129. 129 channels: ch,
  130. 130● data: Bytes::from_array(out),
  131. 131 }
  132. 132}