image.process\transform\transform.mbt

86.3% · 44 / 51 points · 241 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/// # 变换 transform
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 图像变换操作(纯 MoonBit):crop / rotate / flip
  7. 7// 支持 Image / Image16 / ImageF 三种类型
  8. 8
  9. 9///|
  10. 10/// 裁剪 8-bit Image 的指定区域
  11. 11pub fn crop(
  12. 12 img : @types.Image,
  13. 13 x : Int32,
  14. 14 y : Int32,
  15. 15 w : Int32,
  16. 16 h : Int32,
  17. 17) -> @types.Image raise @types.LoadError {
  18. 18● if x < 0 ||
  19. 19 y < 0 ||
  20. 20 w <= 0 ||
  21. 21 h <= 0 ||
  22. 22 x + w > img.width ||
  23. 23 y + h > img.height {
  24. 24● raise @types.LoadError::DecodeFailed("crop 区域越界")
  25. 25 }
  26. 26 let ch = img.channels
  27. 27● let out : Array[Byte] = Array::make(w * h * ch, b'\x00')
  28. 28 for row in 0..<h {
  29. 29● let src_off = ((y + row) * img.width + x) * ch
  30. 30 let dst_off = row * w * ch
  31. 31 for i in 0..<(w * ch) {
  32. 32● out[dst_off + i] = img.data[src_off + i]
  33. 33 }
  34. 34 }
  35. 35 @types.Image::{
  36. 36 width: w,
  37. 37 height: h,
  38. 38 channels: ch,
  39. 39● data: Bytes::from_array(out),
  40. 40 }
  41. 41}
  42. 42
  43. 43///|
  44. 44/// 按 `Region` 裁剪,越界部分自动裁掉(不报错)。空区域返回 0×0 图。
  45. 45/// 内部走零复制的 `ImageRegion` 视图,只为目标像素分配一次内存。
  46. 46pub fn crop_region(img : @types.Image, region : @types.Region) -> @types.Image {
  47. 47○ img.region_with(region).to_image()
  48. 48}
  49. 49
  50. 50///|
  51. 51/// 顺时针旋转 90 度
  52. 52pub fn rotate_90(img : @types.Image) -> @types.Image {
  53. 53● let ch = img.channels
  54. 54 let new_w = img.height
  55. 55 let new_h = img.width
  56. 56● let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00')
  57. 57 for y in 0..<img.height {
  58. 58● for x in 0..<img.width {
  59. 59● let nx = img.height - 1 - y
  60. 60 let ny = x
  61. 61 let src = (y * img.width + x) * ch
  62. 62 let dst = (ny * new_w + nx) * ch
  63. 63 for c in 0..<ch {
  64. 64● out[dst + c] = img.data[src + c]
  65. 65 }
  66. 66 }
  67. 67 }
  68. 68 @types.Image::{
  69. 69 width: new_w,
  70. 70 height: new_h,
  71. 71 channels: ch,
  72. 72● data: Bytes::from_array(out),
  73. 73 }
  74. 74}
  75. 75
  76. 76///|
  77. 77/// 旋转 180 度
  78. 78pub fn rotate_180(img : @types.Image) -> @types.Image {
  79. 79● let ch = img.channels
  80. 80● let out : Array[Byte] = Array::make(img.width * img.height * ch, b'\x00')
  81. 81 for y in 0..<img.height {
  82. 82● for x in 0..<img.width {
  83. 83● let nx = img.width - 1 - x
  84. 84 let ny = img.height - 1 - y
  85. 85 let src = (y * img.width + x) * ch
  86. 86 let dst = (ny * img.width + nx) * ch
  87. 87 for c in 0..<ch {
  88. 88● out[dst + c] = img.data[src + c]
  89. 89 }
  90. 90 }
  91. 91 }
  92. 92 @types.Image::{
  93. 93 width: img.width,
  94. 94 height: img.height,
  95. 95 channels: ch,
  96. 96● data: Bytes::from_array(out),
  97. 97 }
  98. 98}
  99. 99
  100. 100///|
  101. 101/// 顺时针旋转 270 度(逆时针 90 度)
  102. 102pub fn rotate_270(img : @types.Image) -> @types.Image {
  103. 103● let ch = img.channels
  104. 104 let new_w = img.height
  105. 105 let new_h = img.width
  106. 106● let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00')
  107. 107 for y in 0..<img.height {
  108. 108● for x in 0..<img.width {
  109. 109● let nx = y
  110. 110 let ny = img.width - 1 - x
  111. 111 let src = (y * img.width + x) * ch
  112. 112 let dst = (ny * new_w + nx) * ch
  113. 113 for c in 0..<ch {
  114. 114● out[dst + c] = img.data[src + c]
  115. 115 }
  116. 116 }
  117. 117 }
  118. 118 @types.Image::{
  119. 119 width: new_w,
  120. 120 height: new_h,
  121. 121 channels: ch,
  122. 122● data: Bytes::from_array(out),
  123. 123 }
  124. 124}
  125. 125
  126. 126///|
  127. 127/// 水平翻转(左右镜像)
  128. 128pub fn flip_horizontal(img : @types.Image) -> @types.Image {
  129. 129● let ch = img.channels
  130. 130● let out : Array[Byte] = Array::make(img.width * img.height * ch, b'\x00')
  131. 131 for y in 0..<img.height {
  132. 132● for x in 0..<img.width {
  133. 133● let nx = img.width - 1 - x
  134. 134 let src = (y * img.width + x) * ch
  135. 135 let dst = (y * img.width + nx) * ch
  136. 136 for c in 0..<ch {
  137. 137● out[dst + c] = img.data[src + c]
  138. 138 }
  139. 139 }
  140. 140 }
  141. 141 @types.Image::{
  142. 142 width: img.width,
  143. 143 height: img.height,
  144. 144 channels: ch,
  145. 145● data: Bytes::from_array(out),
  146. 146 }
  147. 147}
  148. 148
  149. 149///|
  150. 150/// 垂直翻转(上下镜像)
  151. 151pub fn flip_vertical(img : @types.Image) -> @types.Image {
  152. 152● let ch = img.channels
  153. 153 let w = img.width
  154. 154 let h = img.height
  155. 155● let out : Array[Byte] = Array::make(w * h * ch, b'\x00')
  156. 156 for y in 0..<h {
  157. 157● for x in 0..<w {
  158. 158● let src_off = ((h - 1 - y) * w + x) * ch
  159. 159 let dst_off = (y * w + x) * ch
  160. 160 for c in 0..<ch {
  161. 161● out[dst_off + c] = img.data[src_off + c]
  162. 162 }
  163. 163 }
  164. 164 }
  165. 165 @types.Image::{
  166. 166 width: w,
  167. 167 height: h,
  168. 168 channels: ch,
  169. 169● data: Bytes::from_array(out),
  170. 170 }
  171. 171}
  172. 172
  173. 173///|
  174. 174/// 裁剪 16-bit Image16
  175. 175/// @see crop 8-bit 版本,本函数为 Image16 变体
  176. 176pub fn crop_16(
  177. 177 img : @types.Image16,
  178. 178 x : Int32,
  179. 179 y : Int32,
  180. 180 w : Int32,
  181. 181 h : Int32,
  182. 182) -> @types.Image16 raise @types.LoadError {
  183. 183● if x < 0 ||
  184. 184 y < 0 ||
  185. 185 w <= 0 ||
  186. 186 h <= 0 ||
  187. 187 x + w > img.width ||
  188. 188 y + h > img.height {
  189. 189● raise @types.LoadError::DecodeFailed("crop 区域越界")
  190. 190 }
  191. 191 let ch = img.channels
  192. 192● let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  193. 193 for row in 0..<h {
  194. 194● let src_off = ((y + row) * img.width + x) * ch * 2
  195. 195 let dst_off = row * w * ch * 2
  196. 196 for i in 0..<(w * ch * 2) {
  197. 197● out[dst_off + i] = img.data[src_off + i]
  198. 198 }
  199. 199 }
  200. 200 @types.Image16::{
  201. 201 width: w,
  202. 202 height: h,
  203. 203 channels: ch,
  204. 204● data: Bytes::from_array(out),
  205. 205 }
  206. 206}
  207. 207
  208. 208///|
  209. 209/// 裁剪 float ImageF
  210. 210/// @see crop 8-bit 版本,本函数为 ImageF 变体
  211. 211pub fn crop_f(
  212. 212 img : @types.ImageF,
  213. 213 x : Int32,
  214. 214 y : Int32,
  215. 215 w : Int32,
  216. 216 h : Int32,
  217. 217) -> @types.ImageF raise @types.LoadError {
  218. 218● if x < 0 ||
  219. 219 y < 0 ||
  220. 220 w <= 0 ||
  221. 221 h <= 0 ||
  222. 222 x + w > img.width ||
  223. 223 y + h > img.height {
  224. 224● raise @types.LoadError::DecodeFailed("crop 区域越界")
  225. 225 }
  226. 226 let ch = img.channels
  227. 227○ let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  228. 228 for row in 0..<h {
  229. 229○ let src_off = ((y + row) * img.width + x) * ch * 4
  230. 230 let dst_off = row * w * ch * 4
  231. 231 for i in 0..<(w * ch * 4) {
  232. 232○ out[dst_off + i] = img.data[src_off + i]
  233. 233 }
  234. 234 }
  235. 235 @types.ImageF::{
  236. 236 width: w,
  237. 237 height: h,
  238. 238 channels: ch,
  239. 239○ data: Bytes::from_array(out),
  240. 240 }
  241. 241}