image.process\filter\advanced_16f.mbt
98.1% · 156 / 159 points · 494 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 变体 Advanced filtering 16-bit/float variants - 3
/// - 4
/// 16位/浮点双边/NLM 滤波 bilateral_filter_16 / non_local_means_16 - 5
/// 16位/浮点双边/NLM 滤波 16-bit/float bilateral/NLM filtering - 6
// 高级算法 16-bit/float 变体(filter 包) - 7
// inpaint / NLM 去噪 支持 Image16 / ImageF - 8
- 9
// ========== 辅助函数 ========== - 10
- 11
///| - 12
fn read_u16_le_adv(data : Bytes, offset : Int32) -> Int32 { - 13●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 14
} - 15
- 16
///| - 17
fn write_u16_le_adv(buf : Array[Byte], offset : Int32, val : Int32) -> Unit { - 18●
buf[offset] = (val & 0xFF).to_byte() - 19●
buf[offset + 1] = ((val >> 8) & 0xFF).to_byte() - 20
} - 21
- 22
///| - 23
fn read_float_le_adv(data : Bytes, offset : Int32) -> Float { - 24●
let b0 = data[offset].to_int() - 25●
let b1 = data[offset + 1].to_int() - 26●
let b2 = data[offset + 2].to_int() - 27●
let b3 = data[offset + 3].to_int() - 28
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 29●
Float::reinterpret_from_int(bits) - 30
} - 31
- 32
///| - 33
fn write_f32_le_adv(buf : Array[Byte], offset : Int32, val : Float) -> Unit { - 34●
let bits = val.reinterpret_as_int() - 35●
buf[offset] = (bits & 0xFF).to_byte() - 36●
buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte() - 37●
buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte() - 38●
buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte() - 39
} - 40
- 41
///| - 42
fn img16_to_img8_adv(img : @types.Image16) -> @types.Image { - 43●
let w = img.width - 44
let h = img.height - 45
let ch = img.channels - 46●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 47
for i in 0..<(w * h * ch) { - 48●
let v16 = read_u16_le_adv(img.data, i * 2) - 49●
data8[i] = (v16 / 257).to_byte() - 50
} - 51
@types.Image::{ - 52
width: w, - 53
height: h, - 54
channels: ch, - 55●
data: Bytes::from_array(data8), - 56
} - 57
} - 58
- 59
///| - 60
fn imgf_to_img8_adv(img : @types.ImageF) -> @types.Image { - 61●
let w = img.width - 62
let h = img.height - 63
let ch = img.channels - 64●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 65
for i in 0..<(w * h * ch) { - 66●
let vf = read_float_le_adv(img.data, i * 4) - 67●
let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255) - 68●
data8[i] = v8.to_byte() - 69
} - 70
@types.Image::{ - 71
width: w, - 72
height: h, - 73
channels: ch, - 74●
data: Bytes::from_array(data8), - 75
} - 76
} - 77
- 78
///| - 79
fn img8_to_img16_adv(img : @types.Image) -> @types.Image16 { - 80●
let w = img.width - 81
let h = img.height - 82
let ch = img.channels - 83●
let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00') - 84
for i in 0..<(w * h * ch) { - 85●
let v8 = img.data[i].to_int() - 86●
write_u16_le_adv(out, i * 2, v8 * 257) - 87
} - 88
@types.Image16::{ - 89
width: w, - 90
height: h, - 91
channels: ch, - 92●
data: Bytes::from_array(out), - 93
} - 94
} - 95
- 96
///| - 97
fn img8_to_imgf_adv(img : @types.Image) -> @types.ImageF { - 98●
let w = img.width - 99
let h = img.height - 100
let ch = img.channels - 101●
let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00') - 102
for i in 0..<(w * h * ch) { - 103●
let v8 = img.data[i].to_int() - 104●
write_f32_le_adv(out, i * 4, Float::from_int(v8) / 255.0F) - 105
} - 106
@types.ImageF::{ - 107
width: w, - 108
height: h, - 109
channels: ch, - 110●
data: Bytes::from_array(out), - 111
} - 112
} - 113
- 114
// ========== Inpaint 16-bit ========== - 115
- 116
///| - 117
/// 16-bit 图像修复。 - 118
/// @see inpaint 8-bit 版本,本函数为 Image16 变体 - 119
pub fn inpaint_16( - 120
img : @types.Image16, - 121
mask : @types.Image16, - 122
iterations~ : Int32 = 100, - 123
) -> @types.Image16 raise @types.LoadError { - 124
// 输入验证 - 125●
if img.width <= 0 || img.height <= 0 { - 126●
raise @types.LoadError::DecodeFailed( - 127
"inpaint_16: 图像尺寸必须大于0", - 128
) - 129
} - 130
if img.channels < 1 || img.channels > 4 { - 131●
raise @types.LoadError::DecodeFailed( - 132
"inpaint_16: 通道数必须在1-4之间", - 133
) - 134
} - 135
let expected_size = img.width * img.height * img.channels * 2 - 136●
if img.data.length() != expected_size { - 137●
raise @types.LoadError::DecodeFailed( - 138
"inpaint_16: 图像数据大小不匹配,期望 " + - 139●
expected_size.to_string() + - 140
",实际 " + - 141●
img.data.length().to_string(), - 142
) - 143
} - 144
if mask.width != img.width || mask.height != img.height { - 145●
raise @types.LoadError::DecodeFailed( - 146
"inpaint_16: 掩码图像尺寸必须与原图一致", - 147
) - 148
} - 149
if iterations <= 0 { - 150●
raise @types.LoadError::DecodeFailed("inpaint_16: iterations必须大于0") - 151
} - 152
- 153●
let img8 = img16_to_img8_adv(img) - 154●
let mask8 = img16_to_img8_adv(mask) - 155●
let out8 = inpaint(img8, mask8, iterations~) - 156●
img8_to_img16_adv(out8) - 157
} - 158
- 159
///| - 160
/// 16-bit 快速图像修复。 - 161
/// @see inpaint_fast 8-bit 版本,本函数为 Image16 变体 - 162
pub fn inpaint_fast_16( - 163
img : @types.Image16, - 164
mask : @types.Image16, - 165
radius~ : Int32 = 5, - 166
) -> @types.Image16 raise @types.LoadError { - 167
// 输入验证 - 168●
if img.width <= 0 || img.height <= 0 { - 169●
raise @types.LoadError::DecodeFailed( - 170
"inpaint_fast_16: 图像尺寸必须大于0", - 171
) - 172
} - 173
if img.channels < 1 || img.channels > 4 { - 174●
raise @types.LoadError::DecodeFailed( - 175
"inpaint_fast_16: 通道数必须在1-4之间", - 176
) - 177
} - 178
let expected_size = img.width * img.height * img.channels * 2 - 179●
if img.data.length() != expected_size { - 180●
raise @types.LoadError::DecodeFailed( - 181
"inpaint_fast_16: 图像数据大小不匹配,期望 " + - 182●
expected_size.to_string() + - 183
",实际 " + - 184●
img.data.length().to_string(), - 185
) - 186
} - 187
if mask.width != img.width || mask.height != img.height { - 188○
raise @types.LoadError::DecodeFailed( - 189
"inpaint_fast_16: 掩码图像尺寸必须与原图一致", - 190
) - 191
} - 192
if radius <= 0 { - 193●
raise @types.LoadError::DecodeFailed("inpaint_fast_16: radius必须大于0") - 194
} - 195
- 196●
let img8 = img16_to_img8_adv(img) - 197●
let mask8 = img16_to_img8_adv(mask) - 198●
let out8 = inpaint_fast(img8, mask8, radius~) - 199●
img8_to_img16_adv(out8) - 200
} - 201
- 202
// ========== Inpaint float ========== - 203
- 204
///| - 205
/// float 图像修复。 - 206
/// @see inpaint 8-bit 版本,本函数为 ImageF 变体 - 207
pub fn inpaint_f( - 208
img : @types.ImageF, - 209
mask : @types.ImageF, - 210
iterations~ : Int32 = 100, - 211
) -> @types.ImageF raise @types.LoadError { - 212
// 输入验证 - 213●
if img.width <= 0 || img.height <= 0 { - 214●
raise @types.LoadError::DecodeFailed("inpaint_f: 图像尺寸必须大于0") - 215
} - 216
if img.channels < 1 || img.channels > 4 { - 217●
raise @types.LoadError::DecodeFailed( - 218
"inpaint_f: 通道数必须在1-4之间", - 219
) - 220
} - 221
let expected_size = img.width * img.height * img.channels * 4 - 222●
if img.data.length() != expected_size { - 223●
raise @types.LoadError::DecodeFailed( - 224
"inpaint_f: 图像数据大小不匹配,期望 " + - 225●
expected_size.to_string() + - 226
",实际 " + - 227●
img.data.length().to_string(), - 228
) - 229
} - 230
if mask.width != img.width || mask.height != img.height { - 231○
raise @types.LoadError::DecodeFailed( - 232
"inpaint_f: 掩码图像尺寸必须与原图一致", - 233
) - 234
} - 235
if iterations <= 0 { - 236●
raise @types.LoadError::DecodeFailed("inpaint_f: iterations必须大于0") - 237
} - 238
- 239●
let img8 = imgf_to_img8_adv(img) - 240●
let mask8 = imgf_to_img8_adv(mask) - 241●
let out8 = inpaint(img8, mask8, iterations~) - 242●
img8_to_imgf_adv(out8) - 243
} - 244
- 245
///| - 246
/// float 快速图像修复。 - 247
/// @see inpaint_fast 8-bit 版本,本函数为 ImageF 变体 - 248
pub fn inpaint_fast_f( - 249
img : @types.ImageF, - 250
mask : @types.ImageF, - 251
radius~ : Int32 = 5, - 252
) -> @types.ImageF raise @types.LoadError { - 253
// 输入验证 - 254●
if img.width <= 0 || img.height <= 0 { - 255●
raise @types.LoadError::DecodeFailed( - 256
"inpaint_fast_f: 图像尺寸必须大于0", - 257
) - 258
} - 259
if img.channels < 1 || img.channels > 4 { - 260●
raise @types.LoadError::DecodeFailed( - 261
"inpaint_fast_f: 通道数必须在1-4之间", - 262
) - 263
} - 264
let expected_size = img.width * img.height * img.channels * 4 - 265●
if img.data.length() != expected_size { - 266●
raise @types.LoadError::DecodeFailed( - 267
"inpaint_fast_f: 图像数据大小不匹配,期望 " + - 268●
expected_size.to_string() + - 269
",实际 " + - 270●
img.data.length().to_string(), - 271
) - 272
} - 273
if mask.width != img.width || mask.height != img.height { - 274○
raise @types.LoadError::DecodeFailed( - 275
"inpaint_fast_f: 掩码图像尺寸必须与原图一致", - 276
) - 277
} - 278
if radius <= 0 { - 279●
raise @types.LoadError::DecodeFailed("inpaint_fast_f: radius必须大于0") - 280
} - 281
- 282●
let img8 = imgf_to_img8_adv(img) - 283●
let mask8 = imgf_to_img8_adv(mask) - 284●
let out8 = inpaint_fast(img8, mask8, radius~) - 285●
img8_to_imgf_adv(out8) - 286
} - 287
- 288
// ========== NLM 16-bit ========== - 289
- 290
///| - 291
/// 16-bit 非局部均值去噪。 - 292
/// @see nlm_denoise 8-bit 版本,本函数为 Image16 变体 - 293
pub fn nlm_denoise_16( - 294
img : @types.Image16, - 295
patch_size~ : Int32 = 3, - 296
search_size~ : Int32 = 7, - 297
h~ : Int32 = 10, - 298
) -> @types.Image16 raise @types.LoadError { - 299
// 输入验证 - 300●
if img.width <= 0 || img.height <= 0 { - 301●
raise @types.LoadError::DecodeFailed( - 302
"nlm_denoise_16: 图像尺寸必须大于0", - 303
) - 304
} - 305
if img.channels < 1 || img.channels > 4 { - 306●
raise @types.LoadError::DecodeFailed( - 307
"nlm_denoise_16: 通道数必须在1-4之间", - 308
) - 309
} - 310
let expected_size = img.width * img.height * img.channels * 2 - 311●
if img.data.length() != expected_size { - 312●
raise @types.LoadError::DecodeFailed( - 313
"nlm_denoise_16: 数据大小不匹配,期望 " + - 314●
expected_size.to_string() + - 315
",实际 " + - 316●
img.data.length().to_string(), - 317
) - 318
} - 319
if patch_size < 1 || patch_size > 10 { - 320●
raise @types.LoadError::DecodeFailed( - 321
"nlm_denoise_16: patch_size必须在1-10之间", - 322
) - 323
} - 324
if search_size < 1 || search_size > 20 { - 325●
raise @types.LoadError::DecodeFailed( - 326
"nlm_denoise_16: search_size必须在1-20之间", - 327
) - 328
} - 329
if h < 1 { - 330●
raise @types.LoadError::DecodeFailed("nlm_denoise_16: h必须 >= 1") - 331
} - 332
- 333●
let img8 = img16_to_img8_adv(img) - 334●
let out8 = nlm_denoise(img8, patch_size~, search_size~, h~) - 335●
img8_to_img16_adv(out8) - 336
} - 337
- 338
///| - 339
/// 16-bit 快速非局部均值去噪。 - 340
/// @see nlm_denoise_fast 8-bit 版本,本函数为 Image16 变体 - 341
pub fn nlm_denoise_fast_16( - 342
img : @types.Image16, - 343
patch_size~ : Int32 = 3, - 344
search_size~ : Int32 = 7, - 345
h~ : Int32 = 10, - 346
step~ : Int32 = 2, - 347
) -> @types.Image16 raise @types.LoadError { - 348
// 输入验证 - 349●
if img.width <= 0 || img.height <= 0 { - 350●
raise @types.LoadError::DecodeFailed( - 351
"nlm_denoise_fast_16: 图像尺寸必须大于0", - 352
) - 353
} - 354
if img.channels < 1 || img.channels > 4 { - 355●
raise @types.LoadError::DecodeFailed( - 356
"nlm_denoise_fast_16: 通道数必须在1-4之间", - 357
) - 358
} - 359
let expected_size = img.width * img.height * img.channels * 2 - 360●
if img.data.length() != expected_size { - 361●
raise @types.LoadError::DecodeFailed( - 362
"nlm_denoise_fast_16: 数据大小不匹配,期望 " + - 363●
expected_size.to_string() + - 364
",实际 " + - 365●
img.data.length().to_string(), - 366
) - 367
} - 368
if patch_size < 1 || patch_size > 10 { - 369●
raise @types.LoadError::DecodeFailed( - 370
"nlm_denoise_fast_16: patch_size必须在1-10之间", - 371
) - 372
} - 373
if search_size < 1 || search_size > 20 { - 374●
raise @types.LoadError::DecodeFailed( - 375
"nlm_denoise_fast_16: search_size必须在1-20之间", - 376
) - 377
} - 378
if h < 1 { - 379●
raise @types.LoadError::DecodeFailed("nlm_denoise_fast_16: h必须 >= 1") - 380
} - 381
if step < 1 || step > 5 { - 382●
raise @types.LoadError::DecodeFailed( - 383
"nlm_denoise_fast_16: step必须在1-5之间", - 384
) - 385
} - 386
- 387●
let img8 = img16_to_img8_adv(img) - 388●
let out8 = nlm_denoise_fast(img8, patch_size~, search_size~, h~, step~) - 389●
img8_to_img16_adv(out8) - 390
} - 391
- 392
// ========== NLM float ========== - 393
- 394
///| - 395
/// float 非局部均值去噪。 - 396
/// @see nlm_denoise 8-bit 版本,本函数为 ImageF 变体 - 397
pub fn nlm_denoise_f( - 398
img : @types.ImageF, - 399
patch_size~ : Int32 = 3, - 400
search_size~ : Int32 = 7, - 401
h~ : Int32 = 10, - 402
) -> @types.ImageF raise @types.LoadError { - 403
// 输入验证 - 404●
if img.width <= 0 || img.height <= 0 { - 405●
raise @types.LoadError::DecodeFailed( - 406
"nlm_denoise_f: 图像尺寸必须大于0", - 407
) - 408
} - 409
if img.channels < 1 || img.channels > 4 { - 410●
raise @types.LoadError::DecodeFailed( - 411
"nlm_denoise_f: 通道数必须在1-4之间", - 412
) - 413
} - 414
let expected_size = img.width * img.height * img.channels * 4 - 415●
if img.data.length() != expected_size { - 416●
raise @types.LoadError::DecodeFailed( - 417
"nlm_denoise_f: 数据大小不匹配,期望 " + - 418●
expected_size.to_string() + - 419
",实际 " + - 420●
img.data.length().to_string(), - 421
) - 422
} - 423
if patch_size < 1 || patch_size > 10 { - 424●
raise @types.LoadError::DecodeFailed( - 425
"nlm_denoise_f: patch_size必须在1-10之间", - 426
) - 427
} - 428
if search_size < 1 || search_size > 20 { - 429●
raise @types.LoadError::DecodeFailed( - 430
"nlm_denoise_f: search_size必须在1-20之间", - 431
) - 432
} - 433
if h < 1 { - 434●
raise @types.LoadError::DecodeFailed("nlm_denoise_f: h必须 >= 1") - 435
} - 436
- 437●
let img8 = imgf_to_img8_adv(img) - 438●
let out8 = nlm_denoise(img8, patch_size~, search_size~, h~) - 439●
img8_to_imgf_adv(out8) - 440
} - 441
- 442
///| - 443
/// float 快速非局部均值去噪。 - 444
/// @see nlm_denoise_fast 8-bit 版本,本函数为 ImageF 变体 - 445
pub fn nlm_denoise_fast_f( - 446
img : @types.ImageF, - 447
patch_size~ : Int32 = 3, - 448
search_size~ : Int32 = 7, - 449
h~ : Int32 = 10, - 450
step~ : Int32 = 2, - 451
) -> @types.ImageF raise @types.LoadError { - 452
// 输入验证 - 453●
if img.width <= 0 || img.height <= 0 { - 454●
raise @types.LoadError::DecodeFailed( - 455
"nlm_denoise_fast_f: 图像尺寸必须大于0", - 456
) - 457
} - 458
if img.channels < 1 || img.channels > 4 { - 459●
raise @types.LoadError::DecodeFailed( - 460
"nlm_denoise_fast_f: 通道数必须在1-4之间", - 461
) - 462
} - 463
let expected_size = img.width * img.height * img.channels * 4 - 464●
if img.data.length() != expected_size { - 465●
raise @types.LoadError::DecodeFailed( - 466
"nlm_denoise_fast_f: 数据大小不匹配,期望 " + - 467●
expected_size.to_string() + - 468
",实际 " + - 469●
img.data.length().to_string(), - 470
) - 471
} - 472
if patch_size < 1 || patch_size > 10 { - 473●
raise @types.LoadError::DecodeFailed( - 474
"nlm_denoise_fast_f: patch_size必须在1-10之间", - 475
) - 476
} - 477
if search_size < 1 || search_size > 20 { - 478●
raise @types.LoadError::DecodeFailed( - 479
"nlm_denoise_fast_f: search_size必须在1-20之间", - 480
) - 481
} - 482
if h < 1 { - 483●
raise @types.LoadError::DecodeFailed("nlm_denoise_fast_f: h必须 >= 1") - 484
} - 485
if step < 1 || step > 5 { - 486●
raise @types.LoadError::DecodeFailed( - 487
"nlm_denoise_fast_f: step必须在1-5之间", - 488
) - 489
} - 490
- 491●
let img8 = imgf_to_img8_adv(img) - 492●
let out8 = nlm_denoise_fast(img8, patch_size~, search_size~, h~, step~) - 493●
img8_to_imgf_adv(out8) - 494
}