image.process\transform\draw_16f.mbt

94.1% · 128 / 136 points · 428 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/// # 绘制函数 16-bit/float 变体 Drawing functions 16-bit/float variants
  3. 3///
  4. 4/// 16位绘图基元 draw_line_16 / draw_rect_16
  5. 5/// 16位/浮点绘图基元 16-bit/float drawing primitives
  6. 6// 绘制函数 16-bit/float 变体
  7. 7// draw_line / draw_rectangle / draw_circle / draw_polygon
  8. 8
  9. 9// ========== 辅助函数 ==========
  10. 10
  11. 11///|
  12. 12fn write_u16_le_draw(buf : Array[Byte], offset : Int32, val : Int32) -> Unit {
  13. 13● buf[offset] = (val & 0xFF).to_byte()
  14. 14● buf[offset + 1] = ((val >> 8) & 0xFF).to_byte()
  15. 15}
  16. 16
  17. 17///|
  18. 18fn write_f32_le_draw(buf : Array[Byte], offset : Int32, val : Float) -> Unit {
  19. 19● let bits = val.reinterpret_as_int()
  20. 20● buf[offset] = (bits & 0xFF).to_byte()
  21. 21● buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  22. 22● buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  23. 23● buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
  24. 24}
  25. 25
  26. 26///|
  27. 27fn set_pixel_16(
  28. 28 buf : Array[Byte],
  29. 29 x : Int32,
  30. 30 y : Int32,
  31. 31 w : Int32,
  32. 32 ch : Int32,
  33. 33 color : Array[Int32],
  34. 34) -> Unit {
  35. 35● if x < 0 || x >= w || y < 0 {
  36. 36○ return
  37. 37 }
  38. 38 let idx = (y * w + x) * ch * 2
  39. 39 for c in 0..<ch {
  40. 40● if c < color.length() {
  41. 41● write_u16_le_draw(buf, idx + c * 2, @types.clamp_int(color[c], 0, 65535))
  42. 42 }
  43. 43 }
  44. 44}
  45. 45
  46. 46///|
  47. 47fn set_pixel_f(
  48. 48 buf : Array[Byte],
  49. 49 x : Int32,
  50. 50 y : Int32,
  51. 51 w : Int32,
  52. 52 ch : Int32,
  53. 53 color : Array[Float],
  54. 54) -> Unit {
  55. 55● if x < 0 || x >= w || y < 0 {
  56. 56○ return
  57. 57 }
  58. 58 let idx = (y * w + x) * ch * 4
  59. 59 for c in 0..<ch {
  60. 60● if c < color.length() {
  61. 61● write_f32_le_draw(buf, idx + c * 4, color[c])
  62. 62 }
  63. 63 }
  64. 64}
  65. 65
  66. 66// ========== 16-bit 绘制函数 ==========
  67. 67
  68. 68///|
  69. 69/// 16-bit 绘制直线。
  70. 70/// @see draw_line 8-bit 版本,本函数为 Image16 变体
  71. 71pub fn draw_line_16(
  72. 72 img : @types.Image16,
  73. 73 x1 : Int32,
  74. 74 y1 : Int32,
  75. 75 x2 : Int32,
  76. 76 y2 : Int32,
  77. 77 color : Array[Int32],
  78. 78 thickness~ : Int32 = 1,
  79. 79) -> @types.Image16 {
  80. 80● let w = img.width
  81. 81 let h = img.height
  82. 82 let ch = img.channels
  83. 83● let buf : Array[Byte] = img.data.to_array()
  84. 84● let dx = if x2 > x1 { x2 - x1 } else { x1 - x2 }
  85. 85● let dy = if y2 > y1 { y2 - y1 } else { y1 - y2 }
  86. 86● let sx = if x1 < x2 { 1 } else { -1 }
  87. 87● let sy = if y1 < y2 { 1 } else { -1 }
  88. 88 let mut err = dx - dy
  89. 89 let mut x = x1
  90. 90 let mut y = y1
  91. 91◐ let t = if thickness < 1 { 1 } else { thickness }
  92. 92 let mut done = false
  93. 93 while !done {
  94. 94● for ty in 0..<t {
  95. 95● for tx in 0..<t {
  96. 96● set_pixel_16(buf, x + tx, y + ty, w, ch, color)
  97. 97 }
  98. 98 }
  99. 99 if x == x2 && y == y2 {
  100. 100● done = true
  101. 101 }
  102. 102 if !done {
  103. 103● let e2 = 2 * err
  104. 104 if e2 > -dy {
  105. 105● err -= dy
  106. 106 x += sx
  107. 107 }
  108. 108 if e2 < dx {
  109. 109● err += dx
  110. 110 y += sy
  111. 111 }
  112. 112 }
  113. 113 }
  114. 114 @types.Image16::{
  115. 115 width: w,
  116. 116 height: h,
  117. 117 channels: ch,
  118. 118● data: Bytes::from_array(buf),
  119. 119 }
  120. 120}
  121. 121
  122. 122///|
  123. 123/// 16-bit 绘制矩形。
  124. 124/// @see draw_rectangle 8-bit 版本,本函数为 Image16 变体
  125. 125pub fn draw_rectangle_16(
  126. 126 img : @types.Image16,
  127. 127 x : Int32,
  128. 128 y : Int32,
  129. 129 rw : Int32,
  130. 130 rh : Int32,
  131. 131 color : Array[Int32],
  132. 132 thickness~ : Int32 = 1,
  133. 133 fill~ : Bool = false,
  134. 134) -> @types.Image16 {
  135. 135● let w = img.width
  136. 136 let h = img.height
  137. 137 let ch = img.channels
  138. 138● let buf : Array[Byte] = img.data.to_array()
  139. 139 if fill {
  140. 140● for py in y..<(y + rh) {
  141. 141● for px in x..<(x + rw) {
  142. 142● set_pixel_16(buf, px, py, w, ch, color)
  143. 143 }
  144. 144 }
  145. 145 } else {
  146. 146◐ let t = if thickness < 1 { 1 } else { thickness }
  147. 147 for px in x..<(x + rw) {
  148. 148● for ty in 0..<t {
  149. 149● set_pixel_16(buf, px, y + ty, w, ch, color)
  150. 150● set_pixel_16(buf, px, y + rh - 1 - ty, w, ch, color)
  151. 151 }
  152. 152 }
  153. 153 for py in y..<(y + rh) {
  154. 154● for tx in 0..<t {
  155. 155● set_pixel_16(buf, x + tx, py, w, ch, color)
  156. 156● set_pixel_16(buf, x + rw - 1 - tx, py, w, ch, color)
  157. 157 }
  158. 158 }
  159. 159 }
  160. 160 @types.Image16::{
  161. 161 width: w,
  162. 162 height: h,
  163. 163 channels: ch,
  164. 164● data: Bytes::from_array(buf),
  165. 165 }
  166. 166}
  167. 167
  168. 168///|
  169. 169/// 16-bit 绘制圆形。
  170. 170/// @see draw_circle 8-bit 版本,本函数为 Image16 变体
  171. 171pub fn draw_circle_16(
  172. 172 img : @types.Image16,
  173. 173 cx : Int32,
  174. 174 cy : Int32,
  175. 175 r : Int32,
  176. 176 color : Array[Int32],
  177. 177 thickness~ : Int32 = 1,
  178. 178 fill~ : Bool = false,
  179. 179) -> @types.Image16 {
  180. 180● let w = img.width
  181. 181 let h = img.height
  182. 182 let ch = img.channels
  183. 183● let buf : Array[Byte] = img.data.to_array()
  184. 184 if fill {
  185. 185● for y in (cy - r)..<=(cy + r) {
  186. 186● for x in (cx - r)..<=(cx + r) {
  187. 187● let dx = x - cx
  188. 188 let dy = y - cy
  189. 189 if dx * dx + dy * dy <= r * r {
  190. 190● set_pixel_16(buf, x, y, w, ch, color)
  191. 191 }
  192. 192 }
  193. 193 }
  194. 194 } else {
  195. 195● let _ = thickness
  196. 196 let mut x = r
  197. 197 let mut y = 0
  198. 198 let mut err = 0
  199. 199 while x >= y {
  200. 200● set_pixel_16(buf, cx + x, cy + y, w, ch, color)
  201. 201● set_pixel_16(buf, cx + y, cy + x, w, ch, color)
  202. 202● set_pixel_16(buf, cx - y, cy + x, w, ch, color)
  203. 203● set_pixel_16(buf, cx - x, cy + y, w, ch, color)
  204. 204● set_pixel_16(buf, cx - x, cy - y, w, ch, color)
  205. 205● set_pixel_16(buf, cx - y, cy - x, w, ch, color)
  206. 206● set_pixel_16(buf, cx + y, cy - x, w, ch, color)
  207. 207● set_pixel_16(buf, cx + x, cy - y, w, ch, color)
  208. 208 y += 1
  209. 209 if err <= 0 {
  210. 210● err += 2 * y + 1
  211. 211 }
  212. 212 if err > 0 {
  213. 213● x -= 1
  214. 214 err -= 2 * x + 1
  215. 215 }
  216. 216 }
  217. 217 }
  218. 218 @types.Image16::{
  219. 219 width: w,
  220. 220 height: h,
  221. 221 channels: ch,
  222. 222● data: Bytes::from_array(buf),
  223. 223 }
  224. 224}
  225. 225
  226. 226///|
  227. 227/// 16-bit 绘制多边形。
  228. 228/// @see draw_polygon 8-bit 版本,本函数为 Image16 变体
  229. 229pub fn draw_polygon_16(
  230. 230 img : @types.Image16,
  231. 231 points : Array[(Int32, Int32)],
  232. 232 color : Array[Int32],
  233. 233 thickness~ : Int32 = 1,
  234. 234) -> @types.Image16 {
  235. 235● let mut result = img
  236. 236● let n = points.length()
  237. 237 if n < 2 {
  238. 238○ return result
  239. 239 }
  240. 240 for i in 0..<n {
  241. 241● let (x1, y1) = points[i]
  242. 242 let (x2, y2) = points[(i + 1) % n]
  243. 243● result = draw_line_16(result, x1, y1, x2, y2, color, thickness~)
  244. 244 }
  245. 245 result
  246. 246}
  247. 247
  248. 248// ========== float 绘制函数 ==========
  249. 249
  250. 250///|
  251. 251/// float 绘制直线。
  252. 252/// @see draw_line 8-bit 版本,本函数为 ImageF 变体
  253. 253pub fn draw_line_f(
  254. 254 img : @types.ImageF,
  255. 255 x1 : Int32,
  256. 256 y1 : Int32,
  257. 257 x2 : Int32,
  258. 258 y2 : Int32,
  259. 259 color : Array[Float],
  260. 260 thickness~ : Int32 = 1,
  261. 261) -> @types.ImageF {
  262. 262● let w = img.width
  263. 263 let h = img.height
  264. 264 let ch = img.channels
  265. 265● let buf : Array[Byte] = img.data.to_array()
  266. 266● let dx = if x2 > x1 { x2 - x1 } else { x1 - x2 }
  267. 267● let dy = if y2 > y1 { y2 - y1 } else { y1 - y2 }
  268. 268● let sx = if x1 < x2 { 1 } else { -1 }
  269. 269● let sy = if y1 < y2 { 1 } else { -1 }
  270. 270 let mut err = dx - dy
  271. 271 let mut x = x1
  272. 272 let mut y = y1
  273. 273◐ let t = if thickness < 1 { 1 } else { thickness }
  274. 274 let mut done = false
  275. 275 while !done {
  276. 276● for ty in 0..<t {
  277. 277● for tx in 0..<t {
  278. 278● set_pixel_f(buf, x + tx, y + ty, w, ch, color)
  279. 279 }
  280. 280 }
  281. 281 if x == x2 && y == y2 {
  282. 282● done = true
  283. 283 }
  284. 284 if !done {
  285. 285● let e2 = 2 * err
  286. 286 if e2 > -dy {
  287. 287● err -= dy
  288. 288 x += sx
  289. 289 }
  290. 290 if e2 < dx {
  291. 291● err += dx
  292. 292 y += sy
  293. 293 }
  294. 294 }
  295. 295 }
  296. 296 @types.ImageF::{
  297. 297 width: w,
  298. 298 height: h,
  299. 299 channels: ch,
  300. 300● data: Bytes::from_array(buf),
  301. 301 }
  302. 302}
  303. 303
  304. 304///|
  305. 305/// float 绘制矩形。
  306. 306/// @see draw_rectangle 8-bit 版本,本函数为 ImageF 变体
  307. 307pub fn draw_rectangle_f(
  308. 308 img : @types.ImageF,
  309. 309 x : Int32,
  310. 310 y : Int32,
  311. 311 rw : Int32,
  312. 312 rh : Int32,
  313. 313 color : Array[Float],
  314. 314 thickness~ : Int32 = 1,
  315. 315 fill~ : Bool = false,
  316. 316) -> @types.ImageF {
  317. 317● let w = img.width
  318. 318 let h = img.height
  319. 319 let ch = img.channels
  320. 320● let buf : Array[Byte] = img.data.to_array()
  321. 321 if fill {
  322. 322● for py in y..<(y + rh) {
  323. 323● for px in x..<(x + rw) {
  324. 324● set_pixel_f(buf, px, py, w, ch, color)
  325. 325 }
  326. 326 }
  327. 327 } else {
  328. 328◐ let t = if thickness < 1 { 1 } else { thickness }
  329. 329 for px in x..<(x + rw) {
  330. 330● for ty in 0..<t {
  331. 331● set_pixel_f(buf, px, y + ty, w, ch, color)
  332. 332● set_pixel_f(buf, px, y + rh - 1 - ty, w, ch, color)
  333. 333 }
  334. 334 }
  335. 335 for py in y..<(y + rh) {
  336. 336● for tx in 0..<t {
  337. 337● set_pixel_f(buf, x + tx, py, w, ch, color)
  338. 338● set_pixel_f(buf, x + rw - 1 - tx, py, w, ch, color)
  339. 339 }
  340. 340 }
  341. 341 }
  342. 342 @types.ImageF::{
  343. 343 width: w,
  344. 344 height: h,
  345. 345 channels: ch,
  346. 346● data: Bytes::from_array(buf),
  347. 347 }
  348. 348}
  349. 349
  350. 350///|
  351. 351/// float 绘制圆形。
  352. 352/// @see draw_circle 8-bit 版本,本函数为 ImageF 变体
  353. 353pub fn draw_circle_f(
  354. 354 img : @types.ImageF,
  355. 355 cx : Int32,
  356. 356 cy : Int32,
  357. 357 r : Int32,
  358. 358 color : Array[Float],
  359. 359 thickness~ : Int32 = 1,
  360. 360 fill~ : Bool = false,
  361. 361) -> @types.ImageF {
  362. 362● let w = img.width
  363. 363 let h = img.height
  364. 364 let ch = img.channels
  365. 365● let buf : Array[Byte] = img.data.to_array()
  366. 366 if fill {
  367. 367● for y in (cy - r)..<=(cy + r) {
  368. 368● for x in (cx - r)..<=(cx + r) {
  369. 369● let dx = x - cx
  370. 370 let dy = y - cy
  371. 371 if dx * dx + dy * dy <= r * r {
  372. 372● set_pixel_f(buf, x, y, w, ch, color)
  373. 373 }
  374. 374 }
  375. 375 }
  376. 376 } else {
  377. 377● let _ = thickness
  378. 378 let mut x = r
  379. 379 let mut y = 0
  380. 380 let mut err = 0
  381. 381 while x >= y {
  382. 382● set_pixel_f(buf, cx + x, cy + y, w, ch, color)
  383. 383● set_pixel_f(buf, cx + y, cy + x, w, ch, color)
  384. 384● set_pixel_f(buf, cx - y, cy + x, w, ch, color)
  385. 385● set_pixel_f(buf, cx - x, cy + y, w, ch, color)
  386. 386● set_pixel_f(buf, cx - x, cy - y, w, ch, color)
  387. 387● set_pixel_f(buf, cx - y, cy - x, w, ch, color)
  388. 388● set_pixel_f(buf, cx + y, cy - x, w, ch, color)
  389. 389● set_pixel_f(buf, cx + x, cy - y, w, ch, color)
  390. 390 y += 1
  391. 391 if err <= 0 {
  392. 392● err += 2 * y + 1
  393. 393 }
  394. 394 if err > 0 {
  395. 395● x -= 1
  396. 396 err -= 2 * x + 1
  397. 397 }
  398. 398 }
  399. 399 }
  400. 400 @types.ImageF::{
  401. 401 width: w,
  402. 402 height: h,
  403. 403 channels: ch,
  404. 404● data: Bytes::from_array(buf),
  405. 405 }
  406. 406}
  407. 407
  408. 408///|
  409. 409/// float 绘制多边形。
  410. 410/// @see draw_polygon 8-bit 版本,本函数为 ImageF 变体
  411. 411pub fn draw_polygon_f(
  412. 412 img : @types.ImageF,
  413. 413 points : Array[(Int32, Int32)],
  414. 414 color : Array[Float],
  415. 415 thickness~ : Int32 = 1,
  416. 416) -> @types.ImageF {
  417. 417● let mut result = img
  418. 418● let n = points.length()
  419. 419 if n < 2 {
  420. 420○ return result
  421. 421 }
  422. 422 for i in 0..<n {
  423. 423● let (x1, y1) = points[i]
  424. 424 let (x2, y2) = points[(i + 1) % n]
  425. 425● result = draw_line_f(result, x1, y1, x2, y2, color, thickness~)
  426. 426 }
  427. 427 result
  428. 428}