image.process\filter\convolve_bilateral_16f.mbt
89.3% · 117 / 131 points · 425 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 卷积双边 16f convolve bilateral 16f - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 卷积 + 双边滤波 16-bit/float 变体 - 7
- 8
// ========== 辅助函数 ========== - 9
- 10
///| - 11
fn read_u16_le_cb(data : Bytes, offset : Int32) -> Int32 { - 12●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 13
} - 14
- 15
///| - 16
fn write_u16_le_cb(buf : Array[Byte], offset : Int32, val : Int32) -> Unit { - 17●
buf[offset] = (val & 0xFF).to_byte() - 18●
buf[offset + 1] = ((val >> 8) & 0xFF).to_byte() - 19
} - 20
- 21
///| - 22
fn read_float_le_cb(data : Bytes, offset : Int32) -> Float { - 23●
let b0 = data[offset].to_int() - 24●
let b1 = data[offset + 1].to_int() - 25●
let b2 = data[offset + 2].to_int() - 26●
let b3 = data[offset + 3].to_int() - 27
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 28●
Float::reinterpret_from_int(bits) - 29
} - 30
- 31
///| - 32
fn write_f32_le_cb(buf : Array[Byte], offset : Int32, val : Float) -> Unit { - 33●
let bits = val.reinterpret_as_int() - 34●
buf[offset] = (bits & 0xFF).to_byte() - 35●
buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte() - 36●
buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte() - 37●
buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte() - 38
} - 39
- 40
///| - 41
fn img16_to_img8_cb(img : @types.Image16) -> @types.Image { - 42●
let w = img.width - 43
let h = img.height - 44
let ch = img.channels - 45●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 46
for i in 0..<(w * h * ch) { - 47●
let v16 = read_u16_le_cb(img.data, i * 2) - 48●
data8[i] = (v16 / 257).to_byte() - 49
} - 50
@types.Image::{ - 51
width: w, - 52
height: h, - 53
channels: ch, - 54●
data: Bytes::from_array(data8), - 55
} - 56
} - 57
- 58
///| - 59
fn imgf_to_img8_cb(img : @types.ImageF) -> @types.Image { - 60●
let w = img.width - 61
let h = img.height - 62
let ch = img.channels - 63●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 64
for i in 0..<(w * h * ch) { - 65●
let vf = read_float_le_cb(img.data, i * 4) - 66●
let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255) - 67●
data8[i] = v8.to_byte() - 68
} - 69
@types.Image::{ - 70
width: w, - 71
height: h, - 72
channels: ch, - 73●
data: Bytes::from_array(data8), - 74
} - 75
} - 76
- 77
///| - 78
fn img8_to_img16_cb(img : @types.Image) -> @types.Image16 { - 79●
let w = img.width - 80
let h = img.height - 81
let ch = img.channels - 82●
let data16 : Array[Byte] = Array::make(w * h * ch * 2, b'\x00') - 83
for i in 0..<(w * h * ch) { - 84●
let v8 = img.data[i].to_int() - 85●
write_u16_le_cb(data16, i * 2, v8 * 257) - 86
} - 87
@types.Image16::{ - 88
width: w, - 89
height: h, - 90
channels: ch, - 91●
data: Bytes::from_array(data16), - 92
} - 93
} - 94
- 95
///| - 96
fn img8_to_imgf_cb(img : @types.Image) -> @types.ImageF { - 97●
let w = img.width - 98
let h = img.height - 99
let ch = img.channels - 100●
let dataf : Array[Byte] = Array::make(w * h * ch * 4, b'\x00') - 101
for i in 0..<(w * h * ch) { - 102●
let v8 = img.data[i].to_int() - 103●
write_f32_le_cb(dataf, i * 4, Float::from_int(v8) / 255.0F) - 104
} - 105
@types.ImageF::{ - 106
width: w, - 107
height: h, - 108
channels: ch, - 109●
data: Bytes::from_array(dataf), - 110
} - 111
} - 112
- 113
// ========== 卷积 16-bit ========== - 114
- 115
///| - 116
/// 16-bit 3x3 卷积。 - 117
/// @see convolve 8-bit 版本,本函数为 Image16 变体 - 118
pub fn convolve_16( - 119
img : @types.Image16, - 120
kernel : Array[Float], - 121
bias : Float, - 122
scale : Float, - 123
) -> @types.Image16 raise @types.LoadError { - 124
// 输入验证 - 125●
if img.width <= 0 || img.height <= 0 { - 126●
raise @types.LoadError::DecodeFailed( - 127
"convolve_16: 图像尺寸必须大于0", - 128
) - 129
} - 130
if img.channels < 1 || img.channels > 4 { - 131●
raise @types.LoadError::DecodeFailed( - 132
"convolve_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
"convolve_16: 数据大小不匹配,期望 " + - 139●
expected_size.to_string() + - 140
",实际 " + - 141●
img.data.length().to_string(), - 142
) - 143
} - 144●
if kernel.length() != 9 { - 145●
raise @types.LoadError::DecodeFailed( - 146
"convolve_16: kernel必须为9个值(3x3)", - 147
) - 148
} - 149
if scale == 0.0F { - 150●
raise @types.LoadError::DecodeFailed("convolve_16: scale不能为0") - 151
} - 152
- 153●
let img8 = img16_to_img8_cb(img) - 154●
let result8 = convolve(img8, kernel, bias, scale) - 155●
img8_to_img16_cb(result8) - 156
} - 157
- 158
// ========== 卷积 float ========== - 159
- 160
///| - 161
/// float 3x3 卷积。 - 162
/// @see convolve 8-bit 版本,本函数为 ImageF 变体 - 163
pub fn convolve_f( - 164
img : @types.ImageF, - 165
kernel : Array[Float], - 166
bias : Float, - 167
scale : Float, - 168
) -> @types.ImageF raise @types.LoadError { - 169
// 输入验证 - 170●
if img.width <= 0 || img.height <= 0 { - 171●
raise @types.LoadError::DecodeFailed( - 172
"convolve_f: 图像尺寸必须大于0", - 173
) - 174
} - 175
if img.channels < 1 || img.channels > 4 { - 176●
raise @types.LoadError::DecodeFailed( - 177
"convolve_f: 通道数必须在1-4之间", - 178
) - 179
} - 180
let expected_size = img.width * img.height * img.channels * 4 - 181●
if img.data.length() != expected_size { - 182●
raise @types.LoadError::DecodeFailed( - 183
"convolve_f: 数据大小不匹配,期望 " + - 184●
expected_size.to_string() + - 185
",实际 " + - 186●
img.data.length().to_string(), - 187
) - 188
} - 189●
if kernel.length() != 9 { - 190●
raise @types.LoadError::DecodeFailed( - 191
"convolve_f: kernel必须为9个值(3x3)", - 192
) - 193
} - 194
if scale == 0.0F { - 195○
raise @types.LoadError::DecodeFailed("convolve_f: scale不能为0") - 196
} - 197
- 198●
let img8 = imgf_to_img8_cb(img) - 199●
let result8 = convolve(img8, kernel, bias, scale) - 200●
img8_to_imgf_cb(result8) - 201
} - 202
- 203
// ========== 双边滤波 16-bit ========== - 204
- 205
///| - 206
/// 16-bit 双边滤波。 - 207
/// @see bilateral_filter 8-bit 版本,本函数为 Image16 变体 - 208
pub fn bilateral_filter_16( - 209
img : @types.Image16, - 210
radius : Int32, - 211
sigma_space : Float, - 212
sigma_color : Float, - 213
) -> @types.Image16 raise @types.LoadError { - 214
// 输入验证 - 215●
if img.width <= 0 || img.height <= 0 { - 216●
raise @types.LoadError::DecodeFailed( - 217
"bilateral_filter_16: 图像尺寸必须大于0", - 218
) - 219
} - 220
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 221○
raise @types.LoadError::DecodeFailed( - 222
"bilateral_filter_16: 仅支持 channels=1/3/4", - 223
) - 224
} - 225
let expected_size = img.width * img.height * img.channels * 2 - 226●
if img.data.length() != expected_size { - 227●
raise @types.LoadError::DecodeFailed( - 228
"bilateral_filter_16: 数据大小不匹配,期望 " + - 229●
expected_size.to_string() + - 230
",实际 " + - 231●
img.data.length().to_string(), - 232
) - 233
} - 234
if radius < 1 { - 235●
raise @types.LoadError::DecodeFailed( - 236
"bilateral_filter_16: radius必须 >= 1", - 237
) - 238
} - 239
if sigma_space <= 0.0F { - 240●
raise @types.LoadError::DecodeFailed( - 241
"bilateral_filter_16: sigma_space必须大于0", - 242
) - 243
} - 244
if sigma_color <= 0.0F { - 245○
raise @types.LoadError::DecodeFailed( - 246
"bilateral_filter_16: sigma_color必须大于0", - 247
) - 248
} - 249
- 250●
let img8 = img16_to_img8_cb(img) - 251●
let result8 = bilateral_filter(img8, radius, sigma_space, sigma_color) - 252●
img8_to_img16_cb(result8) - 253
} - 254
- 255
// ========== 双边滤波 float ========== - 256
- 257
///| - 258
/// float 双边滤波。 - 259
/// @see bilateral_filter 8-bit 版本,本函数为 ImageF 变体 - 260
pub fn bilateral_filter_f( - 261
img : @types.ImageF, - 262
radius : Int32, - 263
sigma_space : Float, - 264
sigma_color : Float, - 265
) -> @types.ImageF raise @types.LoadError { - 266
// 输入验证 - 267●
if img.width <= 0 || img.height <= 0 { - 268●
raise @types.LoadError::DecodeFailed( - 269
"bilateral_filter_f: 图像尺寸必须大于0", - 270
) - 271
} - 272
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 273○
raise @types.LoadError::DecodeFailed( - 274
"bilateral_filter_f: 仅支持 channels=1/3/4", - 275
) - 276
} - 277
let expected_size = img.width * img.height * img.channels * 4 - 278●
if img.data.length() != expected_size { - 279●
raise @types.LoadError::DecodeFailed( - 280
"bilateral_filter_f: 数据大小不匹配,期望 " + - 281●
expected_size.to_string() + - 282
",实际 " + - 283●
img.data.length().to_string(), - 284
) - 285
} - 286
if radius < 1 { - 287●
raise @types.LoadError::DecodeFailed( - 288
"bilateral_filter_f: radius必须 >= 1", - 289
) - 290
} - 291
if sigma_space <= 0.0F { - 292○
raise @types.LoadError::DecodeFailed( - 293
"bilateral_filter_f: sigma_space必须大于0", - 294
) - 295
} - 296
if sigma_color <= 0.0F { - 297○
raise @types.LoadError::DecodeFailed( - 298
"bilateral_filter_f: sigma_color必须大于0", - 299
) - 300
} - 301
- 302●
let img8 = imgf_to_img8_cb(img) - 303●
let result8 = bilateral_filter(img8, radius, sigma_space, sigma_color) - 304●
img8_to_imgf_cb(result8) - 305
} - 306
- 307
// ========== 快速双边滤波 16-bit ========== - 308
- 309
///| - 310
/// 16-bit 快速双边滤波。 - 311
/// @see bilateral_filter_fast 8-bit 版本,本函数为 Image16 变体 - 312
pub fn bilateral_filter_fast_16( - 313
img : @types.Image16, - 314
radius : Int32, - 315
sigma_space : Float, - 316
sigma_color : Float, - 317
downsample : Int32, - 318
) -> @types.Image16 raise @types.LoadError { - 319
// 输入验证 - 320●
if img.width <= 0 || img.height <= 0 { - 321●
raise @types.LoadError::DecodeFailed( - 322
"bilateral_filter_fast_16: 图像尺寸必须大于0", - 323
) - 324
} - 325
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 326○
raise @types.LoadError::DecodeFailed( - 327
"bilateral_filter_fast_16: 仅支持 channels=1/3/4", - 328
) - 329
} - 330
let expected_size = img.width * img.height * img.channels * 2 - 331●
if img.data.length() != expected_size { - 332●
raise @types.LoadError::DecodeFailed( - 333
"bilateral_filter_fast_16: 数据大小不匹配,期望 " + - 334●
expected_size.to_string() + - 335
",实际 " + - 336●
img.data.length().to_string(), - 337
) - 338
} - 339
if radius < 1 { - 340●
raise @types.LoadError::DecodeFailed( - 341
"bilateral_filter_fast_16: radius必须 >= 1", - 342
) - 343
} - 344
if sigma_space <= 0.0F { - 345○
raise @types.LoadError::DecodeFailed( - 346
"bilateral_filter_fast_16: sigma_space必须大于0", - 347
) - 348
} - 349
if sigma_color <= 0.0F { - 350○
raise @types.LoadError::DecodeFailed( - 351
"bilateral_filter_fast_16: sigma_color必须大于0", - 352
) - 353
} - 354
if downsample < 1 { - 355○
raise @types.LoadError::DecodeFailed( - 356
"bilateral_filter_fast_16: downsample必须 >= 1", - 357
) - 358
} - 359
- 360●
let img8 = img16_to_img8_cb(img) - 361●
let result8 = bilateral_filter_fast( - 362
img8, radius, sigma_space, sigma_color, downsample, - 363
) - 364●
img8_to_img16_cb(result8) - 365
} - 366
- 367
// ========== 快速双边滤波 float ========== - 368
- 369
///| - 370
/// float 快速双边滤波。 - 371
/// @see bilateral_filter_fast 8-bit 版本,本函数为 ImageF 变体 - 372
pub fn bilateral_filter_fast_f( - 373
img : @types.ImageF, - 374
radius : Int32, - 375
sigma_space : Float, - 376
sigma_color : Float, - 377
downsample : Int32, - 378
) -> @types.ImageF raise @types.LoadError { - 379
// 输入验证 - 380●
if img.width <= 0 || img.height <= 0 { - 381●
raise @types.LoadError::DecodeFailed( - 382
"bilateral_filter_fast_f: 图像尺寸必须大于0", - 383
) - 384
} - 385
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 386○
raise @types.LoadError::DecodeFailed( - 387
"bilateral_filter_fast_f: 仅支持 channels=1/3/4", - 388
) - 389
} - 390
let expected_size = img.width * img.height * img.channels * 4 - 391●
if img.data.length() != expected_size { - 392●
raise @types.LoadError::DecodeFailed( - 393
"bilateral_filter_fast_f: 数据大小不匹配,期望 " + - 394●
expected_size.to_string() + - 395
",实际 " + - 396●
img.data.length().to_string(), - 397
) - 398
} - 399
if radius < 1 { - 400●
raise @types.LoadError::DecodeFailed( - 401
"bilateral_filter_fast_f: radius必须 >= 1", - 402
) - 403
} - 404
if sigma_space <= 0.0F { - 405○
raise @types.LoadError::DecodeFailed( - 406
"bilateral_filter_fast_f: sigma_space必须大于0", - 407
) - 408
} - 409
if sigma_color <= 0.0F { - 410○
raise @types.LoadError::DecodeFailed( - 411
"bilateral_filter_fast_f: sigma_color必须大于0", - 412
) - 413
} - 414
if downsample < 1 { - 415○
raise @types.LoadError::DecodeFailed( - 416
"bilateral_filter_fast_f: downsample必须 >= 1", - 417
) - 418
} - 419
- 420●
let img8 = imgf_to_img8_cb(img) - 421●
let result8 = bilateral_filter_fast( - 422
img8, radius, sigma_space, sigma_color, downsample, - 423
) - 424●
img8_to_imgf_cb(result8) - 425
}