image.process\transform\draw.mbt

99.1% · 116 / 117 points · 398 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/// # 绘图 draw
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 图像合成操作(纯 MoonBit):draw_over / draw_copy
  7. 7
  8. 8///|
  9. 9/// 将 src 图像直接拷贝到 dst 的 (x, y) 位置
  10. 10/// 返回新的 Image,不修改原图。要求 dst 和 src channels 相同。
  11. 11pub fn draw_copy(
  12. 12 dst : @types.Image,
  13. 13 src : @types.Image,
  14. 14 x : Int32,
  15. 15 y : Int32,
  16. 16) -> @types.Image raise @types.LoadError {
  17. 17● if dst.channels != src.channels {
  18. 18● raise @types.LoadError::DecodeFailed(
  19. 19 "draw_copy 要求 dst 和 src channels 相同",
  20. 20 )
  21. 21 }
  22. 22 if x < 0 || y < 0 || x + src.width > dst.width || y + src.height > dst.height {
  23. 23● raise @types.LoadError::DecodeFailed("draw_copy 区域越界")
  24. 24 }
  25. 25 let ch = dst.channels
  26. 26 let out : Array[Byte] = []
  27. 27● for i in 0..<dst.data.length() {
  28. 28● out.push(dst.data[i])
  29. 29 }
  30. 30 for row in 0..<src.height {
  31. 31● for col in 0..<src.width {
  32. 32● let src_off = (row * src.width + col) * ch
  33. 33 let dst_off = ((y + row) * dst.width + (x + col)) * ch
  34. 34 for c in 0..<ch {
  35. 35● out[dst_off + c] = src.data[src_off + c]
  36. 36 }
  37. 37 }
  38. 38 }
  39. 39 @types.Image::{
  40. 40 width: dst.width,
  41. 41 height: dst.height,
  42. 42 channels: ch,
  43. 43● data: Bytes::from_array(out),
  44. 44 }
  45. 45}
  46. 46
  47. 47///|
  48. 48/// Alpha 混合合成(source over destination)
  49. 49/// 将 src 图像合成到 dst 的 (x, y) 位置
  50. 50/// 要求 dst 和 src 均为 RGBA (channels=4)
  51. 51/// 公式: result = src * src_a + dst * (1 - src_a)
  52. 52pub fn draw_over(
  53. 53 dst : @types.Image,
  54. 54 src : @types.Image,
  55. 55 x : Int32,
  56. 56 y : Int32,
  57. 57) -> @types.Image raise @types.LoadError {
  58. 58● if dst.channels != 4 || src.channels != 4 {
  59. 59● raise @types.LoadError::DecodeFailed(
  60. 60 "draw_over 要求 dst 和 src 均为 RGBA (channels=4)",
  61. 61 )
  62. 62 }
  63. 63 if x < 0 || y < 0 || x + src.width > dst.width || y + src.height > dst.height {
  64. 64● raise @types.LoadError::DecodeFailed("draw_over 区域越界")
  65. 65 }
  66. 66 let out : Array[Byte] = []
  67. 67● for i in 0..<dst.data.length() {
  68. 68● out.push(dst.data[i])
  69. 69 }
  70. 70 let src_n = src.width * src.height
  71. 71● let src_int : Array[Int32] = Array::make(src_n * 4, 0)
  72. 72 for i in 0..<(src_n * 4) {
  73. 73● src_int[i] = src.data[i].to_int()
  74. 74 }
  75. 75 for row in 0..<src.height {
  76. 76● for col in 0..<src.width {
  77. 77● let src_off = (row * src.width + col) * 4
  78. 78 let dst_off = ((y + row) * dst.width + (x + col)) * 4
  79. 79 let sr = src_int[src_off]
  80. 80 let sg = src_int[src_off + 1]
  81. 81 let sb = src_int[src_off + 2]
  82. 82 let sa = src_int[src_off + 3]
  83. 83● let dr = out[dst_off].to_int()
  84. 84● let dg = out[dst_off + 1].to_int()
  85. 85● let db = out[dst_off + 2].to_int()
  86. 86● let da = out[dst_off + 3].to_int()
  87. 87 let inv_sa = 255 - sa
  88. 88 let rr = (sr * sa + dr * inv_sa) / 255
  89. 89 let rg = (sg * sa + dg * inv_sa) / 255
  90. 90 let rb = (sb * sa + db * inv_sa) / 255
  91. 91 let ra = sa + da * inv_sa / 255
  92. 92● out[dst_off] = rr.to_byte()
  93. 93● out[dst_off + 1] = rg.to_byte()
  94. 94● out[dst_off + 2] = rb.to_byte()
  95. 95● out[dst_off + 3] = ra.to_byte()
  96. 96 }
  97. 97 }
  98. 98 @types.Image::{
  99. 99 width: dst.width,
  100. 100 height: dst.height,
  101. 101 channels: 4,
  102. 102● data: Bytes::from_array(out),
  103. 103 }
  104. 104}
  105. 105
  106. 106///|
  107. 107fn put_pixel(
  108. 108 out : Array[Byte],
  109. 109 w : Int32,
  110. 110 h : Int32,
  111. 111 ch : Int32,
  112. 112 x : Int32,
  113. 113 y : Int32,
  114. 114 color : Array[Byte],
  115. 115) -> Unit {
  116. 116● if x < 0 || x >= w || y < 0 || y >= h {
  117. 117● return
  118. 118 }
  119. 119 let off = (y * w + x) * ch
  120. 120 for c in 0..<ch {
  121. 121● if c < color.length() {
  122. 122● out[off + c] = color[c]
  123. 123 }
  124. 124 }
  125. 125}
  126. 126
  127. 127///|
  128. 128/// 画线段(Bresenham 算法)。color 为颜色数组(如 [R,G,B])。thickness 为线宽(默认 1)。
  129. 129pub fn draw_line(
  130. 130 img : @types.Image,
  131. 131 x1 : Int32,
  132. 132 y1 : Int32,
  133. 133 x2 : Int32,
  134. 134 y2 : Int32,
  135. 135 color : Array[Byte],
  136. 136 thickness~ : Int32 = 1,
  137. 137) -> @types.Image {
  138. 138● let w = img.width
  139. 139 let h = img.height
  140. 140 let ch = img.channels
  141. 141 let out : Array[Byte] = []
  142. 142● for i in 0..<img.data.length() {
  143. 143● out.push(img.data[i])
  144. 144 }
  145. 145 let t = thickness / 2
  146. 146● let dx = (x2 - x1).abs()
  147. 147● let dy = (y2 - y1).abs()
  148. 148● let sx = if x1 < x2 { 1 } else { -1 }
  149. 149● let sy = if y1 < y2 { 1 } else { -1 }
  150. 150 let mut err = dx - dy
  151. 151 let mut x = x1
  152. 152 let mut y = y1
  153. 153 while true {
  154. 154● for ty in -t..<=t {
  155. 155● for tx in -t..<=t {
  156. 156● put_pixel(out, w, h, ch, x + tx, y + ty, color)
  157. 157 }
  158. 158 }
  159. 159 if x == x2 && y == y2 {
  160. 160● break
  161. 161 }
  162. 162 let e2 = 2 * err
  163. 163 if e2 > -dy {
  164. 164● err -= dy
  165. 165 x += sx
  166. 166 }
  167. 167 if e2 < dx {
  168. 168● err += dx
  169. 169 y += sy
  170. 170 }
  171. 171 }
  172. 172 @types.Image::{
  173. 173 width: w,
  174. 174 height: h,
  175. 175 channels: ch,
  176. 176● data: Bytes::from_array(out),
  177. 177 }
  178. 178}
  179. 179
  180. 180///|
  181. 181/// 画矩形。color 为颜色数组。thickness 为线宽(默认 1)。fill 为是否填充(默认 false)。
  182. 182pub fn draw_rectangle(
  183. 183 img : @types.Image,
  184. 184 x : Int32,
  185. 185 y : Int32,
  186. 186 rw : Int32,
  187. 187 rh : Int32,
  188. 188 color : Array[Byte],
  189. 189 thickness~ : Int32 = 1,
  190. 190 fill~ : Bool = false,
  191. 191) -> @types.Image {
  192. 192● let w = img.width
  193. 193 let h = img.height
  194. 194 let ch = img.channels
  195. 195 let out : Array[Byte] = []
  196. 196● for i in 0..<img.data.length() {
  197. 197● out.push(img.data[i])
  198. 198 }
  199. 199 if fill {
  200. 200● for py in y..<(y + rh) {
  201. 201● for px in x..<(x + rw) {
  202. 202● put_pixel(out, w, h, ch, px, py, color)
  203. 203 }
  204. 204 }
  205. 205 } else {
  206. 206● let result = draw_line(
  207. 207 @types.Image::{
  208. 208 width: w,
  209. 209 height: h,
  210. 210 channels: ch,
  211. 211● data: Bytes::from_array(out),
  212. 212 },
  213. 213 x,
  214. 214 y,
  215. 215 x + rw - 1,
  216. 216 y,
  217. 217 color,
  218. 218 thickness~,
  219. 219 )
  220. 220● let result2 = draw_line(
  221. 221 result,
  222. 222 x + rw - 1,
  223. 223 y,
  224. 224 x + rw - 1,
  225. 225 y + rh - 1,
  226. 226 color,
  227. 227 thickness~,
  228. 228 )
  229. 229● let result3 = draw_line(
  230. 230 result2,
  231. 231 x,
  232. 232 y + rh - 1,
  233. 233 x + rw - 1,
  234. 234 y + rh - 1,
  235. 235 color,
  236. 236 thickness~,
  237. 237 )
  238. 238● let result4 = draw_line(result3, x, y, x, y + rh - 1, color, thickness~)
  239. 239 return result4
  240. 240 }
  241. 241 @types.Image::{
  242. 242 width: w,
  243. 243 height: h,
  244. 244 channels: ch,
  245. 245● data: Bytes::from_array(out),
  246. 246 }
  247. 247}
  248. 248
  249. 249///|
  250. 250/// 画圆(中点画圆算法)。color 为颜色数组。thickness 为线宽(默认 1)。fill 为是否填充(默认 false)。
  251. 251pub fn draw_circle(
  252. 252 img : @types.Image,
  253. 253 cx : Int32,
  254. 254 cy : Int32,
  255. 255 r : Int32,
  256. 256 color : Array[Byte],
  257. 257 thickness~ : Int32 = 1,
  258. 258 fill~ : Bool = false,
  259. 259) -> @types.Image {
  260. 260● let w = img.width
  261. 261 let h = img.height
  262. 262 let ch = img.channels
  263. 263 let out : Array[Byte] = []
  264. 264● for i in 0..<img.data.length() {
  265. 265● out.push(img.data[i])
  266. 266 }
  267. 267 if fill {
  268. 268● for py in -r..<=r {
  269. 269● let px_max = Float::from_int(r * r - py * py).sqrt().to_int()
  270. 270 for px in -px_max..<=px_max {
  271. 271● put_pixel(out, w, h, ch, cx + px, cy + py, color)
  272. 272 }
  273. 273 }
  274. 274 } else {
  275. 275● let mut x = r
  276. 276 let mut y = 0
  277. 277 let mut d = 1 - r
  278. 278 let t = thickness / 2
  279. 279 while x >= y {
  280. 280● for dx in -t..<=t {
  281. 281● put_pixel(out, w, h, ch, cx + x + dx, cy + y, color)
  282. 282● put_pixel(out, w, h, ch, cx - x + dx, cy + y, color)
  283. 283● put_pixel(out, w, h, ch, cx + x + dx, cy - y, color)
  284. 284● put_pixel(out, w, h, ch, cx - x + dx, cy - y, color)
  285. 285● put_pixel(out, w, h, ch, cx + y + dx, cy + x, color)
  286. 286● put_pixel(out, w, h, ch, cx - y + dx, cy + x, color)
  287. 287● put_pixel(out, w, h, ch, cx + y + dx, cy - x, color)
  288. 288● put_pixel(out, w, h, ch, cx - y + dx, cy - x, color)
  289. 289 }
  290. 290 y += 1
  291. 291 if d <= 0 {
  292. 292● d += 2 * y + 1
  293. 293 } else {
  294. 294● x -= 1
  295. 295 d += 2 * (y - x) + 1
  296. 296 }
  297. 297 }
  298. 298 }
  299. 299 @types.Image::{
  300. 300 width: w,
  301. 301 height: h,
  302. 302 channels: ch,
  303. 303● data: Bytes::from_array(out),
  304. 304 }
  305. 305}
  306. 306
  307. 307///|
  308. 308/// 画多边形。points 为顶点数组 [(x1,y1), (x2,y2), ...]。color 为颜色数组。thickness 为线宽(默认 1)。fill 为是否填充(默认 false)。
  309. 309pub fn draw_polygon(
  310. 310 img : @types.Image,
  311. 311 points : Array[(Int32, Int32)],
  312. 312 color : Array[Byte],
  313. 313 thickness~ : Int32 = 1,
  314. 314 fill~ : Bool = false,
  315. 315) -> @types.Image {
  316. 316● let w = img.width
  317. 317 let h = img.height
  318. 318 let ch = img.channels
  319. 319 let out : Array[Byte] = []
  320. 320● for i in 0..<img.data.length() {
  321. 321● out.push(img.data[i])
  322. 322 }
  323. 323● let n = points.length()
  324. 324 if n < 2 {
  325. 325● return @types.Image::{
  326. 326 width: w,
  327. 327 height: h,
  328. 328 channels: ch,
  329. 329● data: Bytes::from_array(out),
  330. 330 }
  331. 331 }
  332. 332 // 画轮廓
  333. 333 let mut result = @types.Image::{
  334. 334 width: w,
  335. 335 height: h,
  336. 336 channels: ch,
  337. 337● data: Bytes::from_array(out),
  338. 338 }
  339. 339 for i in 0..<n {
  340. 340● let (x1, y1) = points[i]
  341. 341 let (x2, y2) = points[(i + 1) % n]
  342. 342● result = draw_line(result, x1, y1, x2, y2, color, thickness~)
  343. 343 }
  344. 344 if fill {
  345. 345 // 扫描线填充
  346. 346● let fill_out : Array[Byte] = []
  347. 347● for i in 0..<result.data.length() {
  348. 348● fill_out.push(result.data[i])
  349. 349 }
  350. 350 let mut y_min = h
  351. 351 let mut y_max = 0
  352. 352 for i in 0..<n {
  353. 353● let (_, py) = points[i]
  354. 354 if py < y_min {
  355. 355● y_min = py
  356. 356 }
  357. 357 if py > y_max {
  358. 358● y_max = py
  359. 359 }
  360. 360 }
  361. 361 for y in y_min..<=y_max {
  362. 362 // 求交点
  363. 363● let xs : Array[Int32] = []
  364. 364 for i in 0..<n {
  365. 365● let (x1, y1) = points[i]
  366. 366 let (x2, y2) = points[(i + 1) % n]
  367. 367 if y1 == y2 {
  368. 368● continue
  369. 369 }
  370. 370 if y < y1 && y < y2 {
  371. 371○ continue
  372. 372 }
  373. 373 if y >= y1 && y >= y2 {
  374. 374● continue
  375. 375 }
  376. 376 let x = x1 + (y - y1) * (x2 - x1) / (y2 - y1)
  377. 377● xs.push(x)
  378. 378 }
  379. 379 // 排序交点
  380. 380● xs.sort()
  381. 381 // 填充相邻对
  382. 382 let mut j = 0
  383. 383● while j + 1 < xs.length() {
  384. 384● for x in xs[j]..<=xs[j + 1] {
  385. 385● put_pixel(fill_out, w, h, ch, x, y, color)
  386. 386 }
  387. 387 j += 2
  388. 388 }
  389. 389 }
  390. 390 return @types.Image::{
  391. 391 width: w,
  392. 392 height: h,
  393. 393 channels: ch,
  394. 394● data: Bytes::from_array(fill_out),
  395. 395 }
  396. 396 }
  397. 397 result
  398. 398}