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