image.process\transform\seam_carving_16f.mbt
96.2% · 175 / 182 points · 556 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # Seam Carving 16f seam carving 16f - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 接缝裁剪(Seam Carving)16-bit/float 变体 - 7
- 8
///| - 9
/// 计算图像能量图 16-bit 版本 - 10
/// @see compute_energy 8-bit 版本,本函数为 Image16 变体 - 11
pub fn compute_energy_16(img : @types.Image16) -> Array[Double] { - 12●
let w = img.width - 13
let h = img.height - 14
let ch = img.channels - 15●
let energy = Array::make(w * h, 0.0) - 16
for y in 0..<h { - 17●
for x in 0..<w { - 18●
let ym = if y == 0 { 0 } else { y - 1 } - 19●
let yp = if y == h - 1 { h - 1 } else { y + 1 } - 20●
let xm = if x == 0 { 0 } else { x - 1 } - 21●
let xp = if x == w - 1 { w - 1 } else { x + 1 } - 22
let mut gx = 0.0 - 23
let mut gy = 0.0 - 24
for c in 0..<ch { - 25●
let left = img.data[((y * w + xm) * ch + c) * 2].to_int() | - 26●
(img.data[((y * w + xm) * ch + c) * 2 + 1].to_int() << 8) - 27●
let right = img.data[((y * w + xp) * ch + c) * 2].to_int() | - 28●
(img.data[((y * w + xp) * ch + c) * 2 + 1].to_int() << 8) - 29●
let top = img.data[((ym * w + x) * ch + c) * 2].to_int() | - 30●
(img.data[((ym * w + x) * ch + c) * 2 + 1].to_int() << 8) - 31●
let bottom = img.data[((yp * w + x) * ch + c) * 2].to_int() | - 32●
(img.data[((yp * w + x) * ch + c) * 2 + 1].to_int() << 8) - 33●
gx += (right - left).to_double() - 34●
gy += (bottom - top).to_double() - 35
} - 36●
let abs_gx = if gx < 0.0 { -gx } else { gx } - 37●
let abs_gy = if gy < 0.0 { -gy } else { gy } - 38
energy[y * w + x] = abs_gx + abs_gy - 39
} - 40
} - 41
energy - 42
} - 43
- 44
///| - 45
/// 计算图像能量图 float 版本 - 46
/// @see compute_energy 8-bit 版本,本函数为 ImageF 变体 - 47
pub fn compute_energy_f(img : @types.ImageF) -> Array[Double] { - 48●
let w = img.width - 49
let h = img.height - 50
let ch = img.channels - 51●
let energy = Array::make(w * h, 0.0) - 52
for y in 0..<h { - 53●
for x in 0..<w { - 54●
let ym = if y == 0 { 0 } else { y - 1 } - 55●
let yp = if y == h - 1 { h - 1 } else { y + 1 } - 56●
let xm = if x == 0 { 0 } else { x - 1 } - 57●
let xp = if x == w - 1 { w - 1 } else { x + 1 } - 58
let mut gx = 0.0 - 59
let mut gy = 0.0 - 60
for c in 0..<ch { - 61●
let left_bits = img.data[((y * w + xm) * ch + c) * 4].to_int() | - 62●
(img.data[((y * w + xm) * ch + c) * 4 + 1].to_int() << 8) | - 63●
(img.data[((y * w + xm) * ch + c) * 4 + 2].to_int() << 16) | - 64●
(img.data[((y * w + xm) * ch + c) * 4 + 3].to_int() << 24) - 65●
let right_bits = img.data[((y * w + xp) * ch + c) * 4].to_int() | - 66●
(img.data[((y * w + xp) * ch + c) * 4 + 1].to_int() << 8) | - 67●
(img.data[((y * w + xp) * ch + c) * 4 + 2].to_int() << 16) | - 68●
(img.data[((y * w + xp) * ch + c) * 4 + 3].to_int() << 24) - 69●
let top_bits = img.data[((ym * w + x) * ch + c) * 4].to_int() | - 70●
(img.data[((ym * w + x) * ch + c) * 4 + 1].to_int() << 8) | - 71●
(img.data[((ym * w + x) * ch + c) * 4 + 2].to_int() << 16) | - 72●
(img.data[((ym * w + x) * ch + c) * 4 + 3].to_int() << 24) - 73●
let bottom_bits = img.data[((yp * w + x) * ch + c) * 4].to_int() | - 74●
(img.data[((yp * w + x) * ch + c) * 4 + 1].to_int() << 8) | - 75●
(img.data[((yp * w + x) * ch + c) * 4 + 2].to_int() << 16) | - 76●
(img.data[((yp * w + x) * ch + c) * 4 + 3].to_int() << 24) - 77●
let left = Float::reinterpret_from_int(left_bits) - 78●
let right = Float::reinterpret_from_int(right_bits) - 79●
let top = Float::reinterpret_from_int(top_bits) - 80●
let bottom = Float::reinterpret_from_int(bottom_bits) - 81●
gx += (right - left).to_double() - 82●
gy += (bottom - top).to_double() - 83
} - 84●
let abs_gx = if gx < 0.0 { -gx } else { gx } - 85●
let abs_gy = if gy < 0.0 { -gy } else { gy } - 86
energy[y * w + x] = abs_gx + abs_gy - 87
} - 88
} - 89
energy - 90
} - 91
- 92
///| - 93
/// 移除垂直 seam 16-bit 版本 - 94
/// @see remove_vertical_seam 8-bit 版本,本函数为 Image16 变体 - 95
pub fn remove_vertical_seam_16( - 96
img : @types.Image16, - 97
seam : Array[Int32], - 98
) -> @types.Image16 { - 99●
let w = img.width - 100
let h = img.height - 101
let ch = img.channels - 102
let new_w = w - 1 - 103●
let out : Array[Byte] = Array::make(new_w * h * ch * 2, b'\x00') - 104
for y in 0..<h { - 105●
let seam_x = seam[y] - 106
for x in 0..<seam_x { - 107●
for c in 0..<ch { - 108●
out[((y * new_w + x) * ch + c) * 2] = img.data[((y * w + x) * ch + c) * - 109
2] - 110
out[((y * new_w + x) * ch + c) * 2 + 1] = img.data[( - 111
(y * w + x) * ch + c - 112
) * - 113
2 + - 114
1] - 115
} - 116
} - 117
for x in seam_x..<new_w { - 118●
for c in 0..<ch { - 119●
out[((y * new_w + x) * ch + c) * 2] = img.data[( - 120
(y * w + x + 1) * ch + c - 121
) * - 122
2] - 123
out[((y * new_w + x) * ch + c) * 2 + 1] = img.data[( - 124
(y * w + x + 1) * ch + c - 125
) * - 126
2 + - 127
1] - 128
} - 129
} - 130
} - 131
@types.Image16::{ - 132
width: new_w, - 133
height: h, - 134
channels: ch, - 135●
data: Bytes::from_array(out), - 136
} - 137
} - 138
- 139
///| - 140
/// 移除垂直 seam float 版本 - 141
/// @see remove_vertical_seam 8-bit 版本,本函数为 ImageF 变体 - 142
pub fn remove_vertical_seam_f( - 143
img : @types.ImageF, - 144
seam : Array[Int32], - 145
) -> @types.ImageF { - 146●
let w = img.width - 147
let h = img.height - 148
let ch = img.channels - 149
let new_w = w - 1 - 150●
let out : Array[Byte] = Array::make(new_w * h * ch * 4, b'\x00') - 151
for y in 0..<h { - 152●
let seam_x = seam[y] - 153
for x in 0..<seam_x { - 154●
for c in 0..<ch { - 155●
for b in 0..<4 { - 156●
out[((y * new_w + x) * ch + c) * 4 + b] = img.data[( - 157
(y * w + x) * ch + c - 158
) * - 159
4 + - 160
b] - 161
} - 162
} - 163
} - 164
for x in seam_x..<new_w { - 165●
for c in 0..<ch { - 166●
for b in 0..<4 { - 167●
out[((y * new_w + x) * ch + c) * 4 + b] = img.data[( - 168
(y * w + x + 1) * ch + c - 169
) * - 170
4 + - 171
b] - 172
} - 173
} - 174
} - 175
} - 176
@types.ImageF::{ - 177
width: new_w, - 178
height: h, - 179
channels: ch, - 180●
data: Bytes::from_array(out), - 181
} - 182
} - 183
- 184
///| - 185
/// 移除水平 seam 16-bit 版本 - 186
/// @see remove_horizontal_seam 8-bit 版本,本函数为 Image16 变体 - 187
pub fn remove_horizontal_seam_16( - 188
img : @types.Image16, - 189
seam : Array[Int32], - 190
) -> @types.Image16 { - 191●
let w = img.width - 192
let h = img.height - 193
let ch = img.channels - 194
let new_h = h - 1 - 195●
let out : Array[Byte] = Array::make(w * new_h * ch * 2, b'\x00') - 196
for x in 0..<w { - 197●
let seam_y = seam[x] - 198
for y in 0..<seam_y { - 199●
for c in 0..<ch { - 200●
out[((y * w + x) * ch + c) * 2] = img.data[((y * w + x) * ch + c) * 2] - 201
out[((y * w + x) * ch + c) * 2 + 1] = img.data[((y * w + x) * ch + c) * - 202
2 + - 203
1] - 204
} - 205
} - 206
for y in seam_y..<new_h { - 207●
for c in 0..<ch { - 208●
out[((y * w + x) * ch + c) * 2] = img.data[(((y + 1) * w + x) * ch + c) * - 209
2] - 210
out[((y * w + x) * ch + c) * 2 + 1] = img.data[( - 211
((y + 1) * w + x) * ch + c - 212
) * - 213
2 + - 214
1] - 215
} - 216
} - 217
} - 218
@types.Image16::{ - 219
width: w, - 220
height: new_h, - 221
channels: ch, - 222●
data: Bytes::from_array(out), - 223
} - 224
} - 225
- 226
///| - 227
/// 移除水平 seam float 版本 - 228
/// @see remove_horizontal_seam 8-bit 版本,本函数为 ImageF 变体 - 229
pub fn remove_horizontal_seam_f( - 230
img : @types.ImageF, - 231
seam : Array[Int32], - 232
) -> @types.ImageF { - 233●
let w = img.width - 234
let h = img.height - 235
let ch = img.channels - 236
let new_h = h - 1 - 237●
let out : Array[Byte] = Array::make(w * new_h * ch * 4, b'\x00') - 238
for x in 0..<w { - 239●
let seam_y = seam[x] - 240
for y in 0..<seam_y { - 241●
for c in 0..<ch { - 242●
for b in 0..<4 { - 243●
out[((y * w + x) * ch + c) * 4 + b] = img.data[((y * w + x) * ch + c) * - 244
4 + - 245
b] - 246
} - 247
} - 248
} - 249
for y in seam_y..<new_h { - 250●
for c in 0..<ch { - 251●
for b in 0..<4 { - 252●
out[((y * w + x) * ch + c) * 4 + b] = img.data[( - 253
((y + 1) * w + x) * ch + c - 254
) * - 255
4 + - 256
b] - 257
} - 258
} - 259
} - 260
} - 261
@types.ImageF::{ - 262
width: w, - 263
height: new_h, - 264
channels: ch, - 265●
data: Bytes::from_array(out), - 266
} - 267
} - 268
- 269
///| - 270
/// 插入垂直 seam 16-bit 版本 - 271
fn insert_vertical_seam_16( - 272
img : @types.Image16, - 273
seam : Array[Int32], - 274
) -> @types.Image16 { - 275●
let w = img.width - 276
let h = img.height - 277
let ch = img.channels - 278
let new_w = w + 1 - 279●
let out : Array[Byte] = Array::make(new_w * h * ch * 2, b'\x00') - 280
for y in 0..<h { - 281●
let seam_x = seam[y] - 282
for x in 0..<seam_x { - 283○
for c in 0..<ch { - 284○
out[((y * new_w + x) * ch + c) * 2] = img.data[((y * w + x) * ch + c) * - 285
2] - 286
out[((y * new_w + x) * ch + c) * 2 + 1] = img.data[( - 287
(y * w + x) * ch + c - 288
) * - 289
2 + - 290
1] - 291
} - 292
} - 293
for c in 0..<ch { - 294●
out[((y * new_w + seam_x) * ch + c) * 2] = img.data[( - 295
(y * w + seam_x) * ch + c - 296
) * - 297
2] - 298
out[((y * new_w + seam_x) * ch + c) * 2 + 1] = img.data[( - 299
(y * w + seam_x) * ch + c - 300
) * - 301
2 + - 302
1] - 303
} - 304
for x in seam_x..<w { - 305●
for c in 0..<ch { - 306●
out[((y * new_w + x + 1) * ch + c) * 2] = img.data[( - 307
(y * w + x) * ch + c - 308
) * - 309
2] - 310
out[((y * new_w + x + 1) * ch + c) * 2 + 1] = img.data[( - 311
(y * w + x) * ch + c - 312
) * - 313
2 + - 314
1] - 315
} - 316
} - 317
} - 318
@types.Image16::{ - 319
width: new_w, - 320
height: h, - 321
channels: ch, - 322●
data: Bytes::from_array(out), - 323
} - 324
} - 325
- 326
///| - 327
/// 插入垂直 seam float 版本 - 328
fn insert_vertical_seam_f( - 329
img : @types.ImageF, - 330
seam : Array[Int32], - 331
) -> @types.ImageF { - 332●
let w = img.width - 333
let h = img.height - 334
let ch = img.channels - 335
let new_w = w + 1 - 336●
let out : Array[Byte] = Array::make(new_w * h * ch * 4, b'\x00') - 337
for y in 0..<h { - 338●
let seam_x = seam[y] - 339
for x in 0..<seam_x { - 340○
for c in 0..<ch { - 341○
for b in 0..<4 { - 342○
out[((y * new_w + x) * ch + c) * 4 + b] = img.data[( - 343
(y * w + x) * ch + c - 344
) * - 345
4 + - 346
b] - 347
} - 348
} - 349
} - 350
for c in 0..<ch { - 351●
for b in 0..<4 { - 352●
out[((y * new_w + seam_x) * ch + c) * 4 + b] = img.data[( - 353
(y * w + seam_x) * ch + c - 354
) * - 355
4 + - 356
b] - 357
} - 358
} - 359
for x in seam_x..<w { - 360●
for c in 0..<ch { - 361●
for b in 0..<4 { - 362●
out[((y * new_w + x + 1) * ch + c) * 4 + b] = img.data[( - 363
(y * w + x) * ch + c - 364
) * - 365
4 + - 366
b] - 367
} - 368
} - 369
} - 370
} - 371
@types.ImageF::{ - 372
width: new_w, - 373
height: h, - 374
channels: ch, - 375●
data: Bytes::from_array(out), - 376
} - 377
} - 378
- 379
///| - 380
/// 插入水平 seam 16-bit 版本 - 381
fn insert_horizontal_seam_16( - 382
img : @types.Image16, - 383
seam : Array[Int32], - 384
) -> @types.Image16 { - 385●
let w = img.width - 386
let h = img.height - 387
let ch = img.channels - 388
let new_h = h + 1 - 389●
let out : Array[Byte] = Array::make(w * new_h * ch * 2, b'\x00') - 390
for x in 0..<w { - 391●
let seam_y = seam[x] - 392
for y in 0..<seam_y { - 393○
for c in 0..<ch { - 394○
out[((y * w + x) * ch + c) * 2] = img.data[((y * w + x) * ch + c) * 2] - 395
out[((y * w + x) * ch + c) * 2 + 1] = img.data[((y * w + x) * ch + c) * - 396
2 + - 397
1] - 398
} - 399
} - 400
for c in 0..<ch { - 401●
out[((seam_y * w + x) * ch + c) * 2] = img.data[( - 402
(seam_y * w + x) * ch + c - 403
) * - 404
2] - 405
out[((seam_y * w + x) * ch + c) * 2 + 1] = img.data[( - 406
(seam_y * w + x) * ch + c - 407
) * - 408
2 + - 409
1] - 410
} - 411
for y in seam_y..<h { - 412●
for c in 0..<ch { - 413●
out[(((y + 1) * w + x) * ch + c) * 2] = img.data[((y * w + x) * ch + c) * - 414
2] - 415
out[(((y + 1) * w + x) * ch + c) * 2 + 1] = img.data[( - 416
(y * w + x) * ch + c - 417
) * - 418
2 + - 419
1] - 420
} - 421
} - 422
} - 423
@types.Image16::{ - 424
width: w, - 425
height: new_h, - 426
channels: ch, - 427●
data: Bytes::from_array(out), - 428
} - 429
} - 430
- 431
///| - 432
/// 插入水平 seam float 版本 - 433
fn insert_horizontal_seam_f( - 434
img : @types.ImageF, - 435
seam : Array[Int32], - 436
) -> @types.ImageF { - 437●
let w = img.width - 438
let h = img.height - 439
let ch = img.channels - 440
let new_h = h + 1 - 441●
let out : Array[Byte] = Array::make(w * new_h * ch * 4, b'\x00') - 442
for x in 0..<w { - 443●
let seam_y = seam[x] - 444
for y in 0..<seam_y { - 445●
for c in 0..<ch { - 446●
for b in 0..<4 { - 447●
out[((y * w + x) * ch + c) * 4 + b] = img.data[((y * w + x) * ch + c) * - 448
4 + - 449
b] - 450
} - 451
} - 452
} - 453
for c in 0..<ch { - 454●
for b in 0..<4 { - 455●
out[((seam_y * w + x) * ch + c) * 4 + b] = img.data[( - 456
(seam_y * w + x) * ch + c - 457
) * - 458
4 + - 459
b] - 460
} - 461
} - 462
for y in seam_y..<h { - 463●
for c in 0..<ch { - 464●
for b in 0..<4 { - 465●
out[(((y + 1) * w + x) * ch + c) * 4 + b] = img.data[( - 466
(y * w + x) * ch + c - 467
) * - 468
4 + - 469
b] - 470
} - 471
} - 472
} - 473
} - 474
@types.ImageF::{ - 475
width: w, - 476
height: new_h, - 477
channels: ch, - 478●
data: Bytes::from_array(out), - 479
} - 480
} - 481
- 482
///| - 483
/// 内容感知缩放 16-bit 版本 - 484
/// @see seam_carve_resize 8-bit 版本,本函数为 Image16 变体 - 485
pub fn seam_carve_resize_16( - 486
img : @types.Image16, - 487
new_w : Int32, - 488
new_h : Int32, - 489
) -> @types.Image16 raise @types.LoadError { - 490●
if new_w <= 0 || new_h <= 0 { - 491●
raise @types.LoadError::DecodeFailed("目标尺寸必须为正数") - 492
} - 493
if new_w > img.width * 2 || new_h > img.height * 2 { - 494●
raise @types.LoadError::DecodeFailed("放大不能超过原尺寸的 2 倍") - 495
} - 496
let mut result = img - 497
while result.width > new_w { - 498●
let energy = compute_energy_16(result) - 499●
let seam = find_vertical_seam(energy, result.width, result.height) - 500●
result = remove_vertical_seam_16(result, seam) - 501
} - 502
while result.width < new_w { - 503●
let energy = compute_energy_16(result) - 504●
let seam = find_vertical_seam(energy, result.width, result.height) - 505●
result = insert_vertical_seam_16(result, seam) - 506
} - 507
while result.height > new_h { - 508●
let energy = compute_energy_16(result) - 509●
let seam = find_horizontal_seam(energy, result.width, result.height) - 510●
result = remove_horizontal_seam_16(result, seam) - 511
} - 512
while result.height < new_h { - 513●
let energy = compute_energy_16(result) - 514●
let seam = find_horizontal_seam(energy, result.width, result.height) - 515●
result = insert_horizontal_seam_16(result, seam) - 516
} - 517
result - 518
} - 519
- 520
///| - 521
/// 内容感知缩放 float 版本 - 522
/// @see seam_carve_resize 8-bit 版本,本函数为 ImageF 变体 - 523
pub fn seam_carve_resize_f( - 524
img : @types.ImageF, - 525
new_w : Int32, - 526
new_h : Int32, - 527
) -> @types.ImageF raise @types.LoadError { - 528●
if new_w <= 0 || new_h <= 0 { - 529●
raise @types.LoadError::DecodeFailed("目标尺寸必须为正数") - 530
} - 531
if new_w > img.width * 2 || new_h > img.height * 2 { - 532●
raise @types.LoadError::DecodeFailed("放大不能超过原尺寸的 2 倍") - 533
} - 534
let mut result = img - 535
while result.width > new_w { - 536●
let energy = compute_energy_f(result) - 537●
let seam = find_vertical_seam(energy, result.width, result.height) - 538●
result = remove_vertical_seam_f(result, seam) - 539
} - 540
while result.width < new_w { - 541●
let energy = compute_energy_f(result) - 542●
let seam = find_vertical_seam(energy, result.width, result.height) - 543●
result = insert_vertical_seam_f(result, seam) - 544
} - 545
while result.height > new_h { - 546●
let energy = compute_energy_f(result) - 547●
let seam = find_horizontal_seam(energy, result.width, result.height) - 548●
result = remove_horizontal_seam_f(result, seam) - 549
} - 550
while result.height < new_h { - 551●
let energy = compute_energy_f(result) - 552●
let seam = find_horizontal_seam(energy, result.width, result.height) - 553●
result = insert_horizontal_seam_f(result, seam) - 554
} - 555
result - 556
}