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