image.process\filter\filter_16f.mbt
96.9% · 347 / 358 points · 933 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 filter 16f - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 图像滤波 16-bit/float 变体:box_blur / gaussian_blur / median_blur - 7
// 支持 Image16 / ImageF 两种类型 - 8
- 9
///| - 10
fn read_u16_le_f(data : Bytes, offset : Int32) -> Int32 { - 11●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 12
} - 13
- 14
///| - 15
fn write_u16_le_f(buf : Array[Byte], offset : Int32, val : Int32) -> Unit { - 16●
buf[offset] = (val & 0xFF).to_byte() - 17●
buf[offset + 1] = ((val >> 8) & 0xFF).to_byte() - 18
} - 19
- 20
///| - 21
fn read_f32_le_f(data : Bytes, offset : Int32) -> Float { - 22●
let b0 = data[offset].to_int() - 23●
let b1 = data[offset + 1].to_int() - 24●
let b2 = data[offset + 2].to_int() - 25●
let b3 = data[offset + 3].to_int() - 26
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 27●
Float::reinterpret_from_int(bits) - 28
} - 29
- 30
///| - 31
fn write_f32_le_f(buf : Array[Byte], offset : Int32, val : Float) -> Unit { - 32●
let bits = val.reinterpret_as_int() - 33●
buf[offset] = (bits & 0xFF).to_byte() - 34●
buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte() - 35●
buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte() - 36●
buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte() - 37
} - 38
- 39
///| - 40
/// 16-bit 盒模糊。radius 为模糊半径,窗口大小 = 2*radius+1。 - 41
/// 使用可分离滑动窗口,O(n) 复杂度。仅模糊颜色通道,alpha 不变。 - 42
/// @see box_blur 8-bit 版本,本函数为 Image16 变体 - 43
pub fn box_blur_16( - 44
img : @types.Image16, - 45
radius : Int32, - 46
) -> @types.Image16 raise @types.LoadError { - 47
// 输入验证 - 48●
if img.width <= 0 || img.height <= 0 { - 49●
raise @types.LoadError::DecodeFailed( - 50
"box_blur_16: 图像尺寸必须大于0", - 51
) - 52
} - 53
if img.channels < 1 || img.channels > 4 { - 54●
raise @types.LoadError::DecodeFailed( - 55
"box_blur_16: 通道数必须在1-4之间", - 56
) - 57
} - 58
let expected_size = img.width * img.height * img.channels * 2 - 59●
if img.data.length() != expected_size { - 60●
raise @types.LoadError::DecodeFailed( - 61
"box_blur_16: 数据大小不匹配,期望 " + - 62●
expected_size.to_string() + - 63
",实际 " + - 64●
img.data.length().to_string(), - 65
) - 66
} - 67
if radius < 0 { - 68●
raise @types.LoadError::DecodeFailed("box_blur_16: radius不能为负") - 69
} - 70
if radius <= 0 { - 71●
return img - 72
} - 73
let w = img.width - 74
let h = img.height - 75
let ch = img.channels - 76●
let color_ch = if ch >= 3 { 3 } else { ch } - 77
let n = w * h - 78
let win_size = 2 * radius + 1 - 79
let bpp = ch * 2 - 80
// 水平模糊:img -> temp - 81●
let temp : Array[Int32] = Array::make(n * color_ch, 0) - 82
for y in 0..<h { - 83●
for c in 0..<color_ch { - 84●
let mut sum = 0 - 85
for dx in 0..<win_size { - 86●
let x = @types.clamp_coord(dx - radius, w) - 87●
sum += read_u16_le_f(img.data, (y * w + x) * bpp + c * 2) - 88
} - 89
for x in 0..<w { - 90●
temp[(y * w + x) * color_ch + c] = sum / win_size - 91●
let x_out = @types.clamp_coord(x - radius, w) - 92●
let x_in = @types.clamp_coord(x + radius + 1, w) - 93
sum = sum - - 94●
read_u16_le_f(img.data, (y * w + x_out) * bpp + c * 2) + - 95●
read_u16_le_f(img.data, (y * w + x_in) * bpp + c * 2) - 96
} - 97
} - 98
} - 99
// 垂直模糊:temp -> out - 100●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 101
for x in 0..<w { - 102●
for c in 0..<color_ch { - 103●
let mut sum = 0 - 104
for dy in 0..<win_size { - 105●
let y = @types.clamp_coord(dy - radius, h) - 106
sum += temp[(y * w + x) * color_ch + c] - 107
} - 108
for y in 0..<h { - 109●
write_u16_le_f(out, (y * w + x) * bpp + c * 2, sum / win_size) - 110●
let y_out = @types.clamp_coord(y - radius, h) - 111●
let y_in = @types.clamp_coord(y + radius + 1, h) - 112
sum = sum - - 113
temp[(y_out * w + x) * color_ch + c] + - 114
temp[(y_in * w + x) * color_ch + c] - 115
} - 116
} - 117
} - 118
// 复制 alpha - 119
if ch == 4 { - 120●
for i in 0..<n { - 121●
let alpha_off = i * bpp + 6 - 122
out[alpha_off] = img.data[alpha_off] - 123
out[alpha_off + 1] = img.data[alpha_off + 1] - 124
} - 125
} - 126
@types.Image16::{ - 127
width: w, - 128
height: h, - 129
channels: ch, - 130●
data: Bytes::from_array(out), - 131
} - 132
} - 133
- 134
///| - 135
/// float 盒模糊。radius 为模糊半径,窗口大小 = 2*radius+1。 - 136
/// 使用可分离滑动窗口,O(n) 复杂度。仅模糊颜色通道,alpha 不变。 - 137
/// @see box_blur 8-bit 版本,本函数为 ImageF 变体 - 138
pub fn box_blur_f( - 139
img : @types.ImageF, - 140
radius : Int32, - 141
) -> @types.ImageF raise @types.LoadError { - 142
// 输入验证 - 143●
if img.width <= 0 || img.height <= 0 { - 144●
raise @types.LoadError::DecodeFailed( - 145
"box_blur_f: 图像尺寸必须大于0", - 146
) - 147
} - 148
if img.channels < 1 || img.channels > 4 { - 149●
raise @types.LoadError::DecodeFailed( - 150
"box_blur_f: 通道数必须在1-4之间", - 151
) - 152
} - 153
let expected_size = img.width * img.height * img.channels * 4 - 154●
if img.data.length() != expected_size { - 155●
raise @types.LoadError::DecodeFailed( - 156
"box_blur_f: 数据大小不匹配,期望 " + - 157●
expected_size.to_string() + - 158
",实际 " + - 159●
img.data.length().to_string(), - 160
) - 161
} - 162
if radius < 0 { - 163●
raise @types.LoadError::DecodeFailed("box_blur_f: radius不能为负") - 164
} - 165
if radius <= 0 { - 166●
return img - 167
} - 168
let w = img.width - 169
let h = img.height - 170
let ch = img.channels - 171●
let color_ch = if ch >= 3 { 3 } else { ch } - 172
let n = w * h - 173
let win_size = 2 * radius + 1 - 174
let bpp = ch * 4 - 175●
let win_size_f = Float::from_int(win_size) - 176
// 水平模糊:img -> temp - 177●
let temp : Array[Float] = Array::make(n * color_ch, 0.0F) - 178
for y in 0..<h { - 179●
for c in 0..<color_ch { - 180●
let mut sum = 0.0F - 181
for dx in 0..<win_size { - 182●
let x = @types.clamp_coord(dx - radius, w) - 183●
sum += read_f32_le_f(img.data, (y * w + x) * bpp + c * 4) - 184
} - 185
for x in 0..<w { - 186●
temp[(y * w + x) * color_ch + c] = sum / win_size_f - 187●
let x_out = @types.clamp_coord(x - radius, w) - 188●
let x_in = @types.clamp_coord(x + radius + 1, w) - 189
sum = sum - - 190●
read_f32_le_f(img.data, (y * w + x_out) * bpp + c * 4) + - 191●
read_f32_le_f(img.data, (y * w + x_in) * bpp + c * 4) - 192
} - 193
} - 194
} - 195
// 垂直模糊:temp -> out - 196●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 197
for x in 0..<w { - 198●
for c in 0..<color_ch { - 199●
let mut sum = 0.0F - 200
for dy in 0..<win_size { - 201●
let y = @types.clamp_coord(dy - radius, h) - 202
sum += temp[(y * w + x) * color_ch + c] - 203
} - 204
for y in 0..<h { - 205●
write_f32_le_f(out, (y * w + x) * bpp + c * 4, sum / win_size_f) - 206●
let y_out = @types.clamp_coord(y - radius, h) - 207●
let y_in = @types.clamp_coord(y + radius + 1, h) - 208
sum = sum - - 209
temp[(y_out * w + x) * color_ch + c] + - 210
temp[(y_in * w + x) * color_ch + c] - 211
} - 212
} - 213
} - 214
// 复制 alpha - 215
if ch == 4 { - 216●
for i in 0..<n { - 217●
let alpha_off = i * bpp + 12 - 218
for b in 0..<4 { - 219●
out[alpha_off + b] = img.data[alpha_off + b] - 220
} - 221
} - 222
} - 223
@types.ImageF::{ - 224
width: w, - 225
height: h, - 226
channels: ch, - 227●
data: Bytes::from_array(out), - 228
} - 229
} - 230
- 231
///| - 232
/// 16-bit 高斯模糊。radius 为核半径,sigma 为标准差。 - 233
/// 使用可分离高斯核,O(n*radius) 复杂度。仅模糊颜色通道,alpha 不变。 - 234
/// @see gaussian_blur 8-bit 版本,本函数为 Image16 变体 - 235
pub fn gaussian_blur_16( - 236
img : @types.Image16, - 237
radius : Int32, - 238
sigma : Float, - 239
) -> @types.Image16 raise @types.LoadError { - 240
// 输入验证 - 241●
if img.width <= 0 || img.height <= 0 { - 242●
raise @types.LoadError::DecodeFailed( - 243
"gaussian_blur_16: 图像尺寸必须大于0", - 244
) - 245
} - 246
if img.channels < 1 || img.channels > 4 { - 247●
raise @types.LoadError::DecodeFailed( - 248
"gaussian_blur_16: 通道数必须在1-4之间", - 249
) - 250
} - 251
let expected_size = img.width * img.height * img.channels * 2 - 252●
if img.data.length() != expected_size { - 253●
raise @types.LoadError::DecodeFailed( - 254
"gaussian_blur_16: 数据大小不匹配,期望 " + - 255●
expected_size.to_string() + - 256
",实际 " + - 257●
img.data.length().to_string(), - 258
) - 259
} - 260
if radius < 0 { - 261●
raise @types.LoadError::DecodeFailed("gaussian_blur_16: radius不能为负") - 262
} - 263
if sigma <= 0.0F { - 264●
raise @types.LoadError::DecodeFailed("gaussian_blur_16: sigma必须大于0") - 265
} - 266
if radius <= 0 { - 267●
return img - 268
} - 269
let w = img.width - 270
let h = img.height - 271
let ch = img.channels - 272●
let color_ch = if ch >= 3 { 3 } else { ch } - 273
let n = w * h - 274
let win_size = 2 * radius + 1 - 275
let bpp = ch * 2 - 276
// 生成 1D 高斯核 - 277●
let kernel : Array[Float] = Array::make(win_size, 0.0F) - 278
let two_sigma_sq = 2.0F * sigma * sigma - 279
let mut ksum = 0.0F - 280
for i in 0..<win_size { - 281●
let x = Float::from_int(i - radius) - 282●
let val = @math.expf(-(x * x) / two_sigma_sq) - 283
kernel[i] = val - 284
ksum += val - 285
} - 286
for i in 0..<win_size { - 287●
kernel[i] = kernel[i] / ksum - 288
} - 289
// 预计算像素 Float 数组 - 290●
let src : Array[Float] = Array::make(n * color_ch, 0.0F) - 291
for i in 0..<n { - 292●
for c in 0..<color_ch { - 293●
src[i * color_ch + c] = Float::from_int( - 294●
read_u16_le_f(img.data, i * bpp + c * 2), - 295
) - 296
} - 297
} - 298
// 水平模糊:src -> temp (Float) - 299●
let temp : Array[Float] = Array::make(n * color_ch, 0.0F) - 300◐
let x_inner_start = if radius >= w { w } else { radius } - 301◐
let x_inner_end = if radius >= w { w } else { w - radius } - 302
for y in 0..<h { - 303●
let row_base = y * w - 304
for c in 0..<color_ch { - 305●
for x in x_inner_start..<x_inner_end { - 306●
let mut acc = 0.0F - 307
let base = (row_base + x - radius) * color_ch + c - 308
for k in 0..<win_size { - 309●
acc += kernel[k] * src[base + k * color_ch] - 310
} - 311
temp[(row_base + x) * color_ch + c] = acc - 312
} - 313
for x in 0..<x_inner_start { - 314●
let mut acc = 0.0F - 315
for k in 0..<win_size { - 316●
let sx = @types.clamp_coord(x + k - radius, w) - 317
acc += kernel[k] * src[(row_base + sx) * color_ch + c] - 318
} - 319
temp[(row_base + x) * color_ch + c] = acc - 320
} - 321
for x in x_inner_end..<w { - 322●
let mut acc = 0.0F - 323
for k in 0..<win_size { - 324●
let sx = @types.clamp_coord(x + k - radius, w) - 325
acc += kernel[k] * src[(row_base + sx) * color_ch + c] - 326
} - 327
temp[(row_base + x) * color_ch + c] = acc - 328
} - 329
} - 330
} - 331
// 垂直模糊:temp -> out - 332●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 333◐
let y_inner_start = if radius >= h { h } else { radius } - 334◐
let y_inner_end = if radius >= h { h } else { h - radius } - 335
for x in 0..<w { - 336●
for c in 0..<color_ch { - 337●
for y in y_inner_start..<y_inner_end { - 338●
let mut acc = 0.0F - 339
let base = ((y - radius) * w + x) * color_ch + c - 340
for k in 0..<win_size { - 341●
acc += kernel[k] * temp[base + k * w * color_ch] - 342
} - 343●
let v = (acc + 0.5F).to_int().clamp(min=0, max=65535) - 344●
write_u16_le_f(out, (y * w + x) * bpp + c * 2, v) - 345
} - 346
for y in 0..<y_inner_start { - 347●
let mut acc = 0.0F - 348
for k in 0..<win_size { - 349●
let sy = @types.clamp_coord(y + k - radius, h) - 350
acc += kernel[k] * temp[(sy * w + x) * color_ch + c] - 351
} - 352●
let v = (acc + 0.5F).to_int().clamp(min=0, max=65535) - 353●
write_u16_le_f(out, (y * w + x) * bpp + c * 2, v) - 354
} - 355
for y in y_inner_end..<h { - 356●
let mut acc = 0.0F - 357
for k in 0..<win_size { - 358●
let sy = @types.clamp_coord(y + k - radius, h) - 359
acc += kernel[k] * temp[(sy * w + x) * color_ch + c] - 360
} - 361●
let v = (acc + 0.5F).to_int().clamp(min=0, max=65535) - 362●
write_u16_le_f(out, (y * w + x) * bpp + c * 2, v) - 363
} - 364
} - 365
} - 366
// 复制 alpha - 367
if ch == 4 { - 368●
for i in 0..<n { - 369●
let alpha_off = i * bpp + 6 - 370
out[alpha_off] = img.data[alpha_off] - 371
out[alpha_off + 1] = img.data[alpha_off + 1] - 372
} - 373
} - 374
@types.Image16::{ - 375
width: w, - 376
height: h, - 377
channels: ch, - 378●
data: Bytes::from_array(out), - 379
} - 380
} - 381
- 382
///| - 383
/// float 高斯模糊。radius 为核半径,sigma 为标准差。 - 384
/// 使用可分离高斯核,O(n*radius) 复杂度。仅模糊颜色通道,alpha 不变。 - 385
/// @see gaussian_blur 8-bit 版本,本函数为 ImageF 变体 - 386
pub fn gaussian_blur_f( - 387
img : @types.ImageF, - 388
radius : Int32, - 389
sigma : Float, - 390
) -> @types.ImageF raise @types.LoadError { - 391
// 输入验证 - 392●
if img.width <= 0 || img.height <= 0 { - 393●
raise @types.LoadError::DecodeFailed( - 394
"gaussian_blur_f: 图像尺寸必须大于0", - 395
) - 396
} - 397
if img.channels < 1 || img.channels > 4 { - 398●
raise @types.LoadError::DecodeFailed( - 399
"gaussian_blur_f: 通道数必须在1-4之间", - 400
) - 401
} - 402
let expected_size = img.width * img.height * img.channels * 4 - 403●
if img.data.length() != expected_size { - 404●
raise @types.LoadError::DecodeFailed( - 405
"gaussian_blur_f: 数据大小不匹配,期望 " + - 406●
expected_size.to_string() + - 407
",实际 " + - 408●
img.data.length().to_string(), - 409
) - 410
} - 411
if radius < 0 { - 412○
raise @types.LoadError::DecodeFailed("gaussian_blur_f: radius不能为负") - 413
} - 414
if sigma <= 0.0F { - 415●
raise @types.LoadError::DecodeFailed("gaussian_blur_f: sigma必须大于0") - 416
} - 417
if radius <= 0 { - 418●
return img - 419
} - 420
let w = img.width - 421
let h = img.height - 422
let ch = img.channels - 423●
let color_ch = if ch >= 3 { 3 } else { ch } - 424
let n = w * h - 425
let win_size = 2 * radius + 1 - 426
let bpp = ch * 4 - 427
// 生成 1D 高斯核 - 428●
let kernel : Array[Float] = Array::make(win_size, 0.0F) - 429
let two_sigma_sq = 2.0F * sigma * sigma - 430
let mut ksum = 0.0F - 431
for i in 0..<win_size { - 432●
let x = Float::from_int(i - radius) - 433●
let val = @math.expf(-(x * x) / two_sigma_sq) - 434
kernel[i] = val - 435
ksum += val - 436
} - 437
for i in 0..<win_size { - 438●
kernel[i] = kernel[i] / ksum - 439
} - 440
// 预计算像素 Float 数组 - 441●
let src : Array[Float] = Array::make(n * color_ch, 0.0F) - 442
for i in 0..<n { - 443●
for c in 0..<color_ch { - 444●
src[i * color_ch + c] = read_f32_le_f(img.data, i * bpp + c * 4) - 445
} - 446
} - 447
// 水平模糊:src -> temp (Float) - 448●
let temp : Array[Float] = Array::make(n * color_ch, 0.0F) - 449◐
let x_inner_start = if radius >= w { w } else { radius } - 450◐
let x_inner_end = if radius >= w { w } else { w - radius } - 451
for y in 0..<h { - 452●
let row_base = y * w - 453
for c in 0..<color_ch { - 454●
for x in x_inner_start..<x_inner_end { - 455●
let mut acc = 0.0F - 456
let base = (row_base + x - radius) * color_ch + c - 457
for k in 0..<win_size { - 458●
acc += kernel[k] * src[base + k * color_ch] - 459
} - 460
temp[(row_base + x) * color_ch + c] = acc - 461
} - 462
for x in 0..<x_inner_start { - 463●
let mut acc = 0.0F - 464
for k in 0..<win_size { - 465●
let sx = @types.clamp_coord(x + k - radius, w) - 466
acc += kernel[k] * src[(row_base + sx) * color_ch + c] - 467
} - 468
temp[(row_base + x) * color_ch + c] = acc - 469
} - 470
for x in x_inner_end..<w { - 471●
let mut acc = 0.0F - 472
for k in 0..<win_size { - 473●
let sx = @types.clamp_coord(x + k - radius, w) - 474
acc += kernel[k] * src[(row_base + sx) * color_ch + c] - 475
} - 476
temp[(row_base + x) * color_ch + c] = acc - 477
} - 478
} - 479
} - 480
// 垂直模糊:temp -> out - 481●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 482◐
let y_inner_start = if radius >= h { h } else { radius } - 483◐
let y_inner_end = if radius >= h { h } else { h - radius } - 484
for x in 0..<w { - 485●
for c in 0..<color_ch { - 486●
for y in y_inner_start..<y_inner_end { - 487●
let mut acc = 0.0F - 488
let base = ((y - radius) * w + x) * color_ch + c - 489
for k in 0..<win_size { - 490●
acc += kernel[k] * temp[base + k * w * color_ch] - 491
} - 492●
write_f32_le_f(out, (y * w + x) * bpp + c * 4, acc) - 493
} - 494
for y in 0..<y_inner_start { - 495●
let mut acc = 0.0F - 496
for k in 0..<win_size { - 497●
let sy = @types.clamp_coord(y + k - radius, h) - 498
acc += kernel[k] * temp[(sy * w + x) * color_ch + c] - 499
} - 500●
write_f32_le_f(out, (y * w + x) * bpp + c * 4, acc) - 501
} - 502
for y in y_inner_end..<h { - 503●
let mut acc = 0.0F - 504
for k in 0..<win_size { - 505●
let sy = @types.clamp_coord(y + k - radius, h) - 506
acc += kernel[k] * temp[(sy * w + x) * color_ch + c] - 507
} - 508●
write_f32_le_f(out, (y * w + x) * bpp + c * 4, acc) - 509
} - 510
} - 511
} - 512
// 复制 alpha - 513
if ch == 4 { - 514●
for i in 0..<n { - 515●
let alpha_off = i * bpp + 12 - 516
for b in 0..<4 { - 517●
out[alpha_off + b] = img.data[alpha_off + b] - 518
} - 519
} - 520
} - 521
@types.ImageF::{ - 522
width: w, - 523
height: h, - 524
channels: ch, - 525●
data: Bytes::from_array(out), - 526
} - 527
} - 528
- 529
///| - 530
/// 16-bit 中值滤波。ksize 为窗口大小(奇数)。有效去除椒盐噪声。 - 531
/// 仅滤波颜色通道,alpha 不变。 - 532
/// @see median_blur 8-bit 版本,本函数为 Image16 变体 - 533
pub fn median_blur_16( - 534
img : @types.Image16, - 535
ksize : Int32, - 536
) -> @types.Image16 raise @types.LoadError { - 537
// 输入验证 - 538●
if img.width <= 0 || img.height <= 0 { - 539●
raise @types.LoadError::DecodeFailed( - 540
"median_blur_16: 图像尺寸必须大于0", - 541
) - 542
} - 543
if img.channels < 1 || img.channels > 4 { - 544●
raise @types.LoadError::DecodeFailed( - 545
"median_blur_16: 通道数必须在1-4之间", - 546
) - 547
} - 548
let expected_size = img.width * img.height * img.channels * 2 - 549●
if img.data.length() != expected_size { - 550●
raise @types.LoadError::DecodeFailed( - 551
"median_blur_16: 数据大小不匹配,期望 " + - 552●
expected_size.to_string() + - 553
",实际 " + - 554●
img.data.length().to_string(), - 555
) - 556
} - 557
if ksize <= 0 { - 558●
raise @types.LoadError::DecodeFailed("median_blur_16: ksize必须大于0") - 559
} - 560
if ksize % 2 == 0 { - 561●
raise @types.LoadError::DecodeFailed("median_blur_16: ksize必须为奇数") - 562
} - 563
if ksize <= 1 { - 564●
return img - 565
} - 566
let radius = ksize / 2 - 567
let w = img.width - 568
let h = img.height - 569
let ch = img.channels - 570●
let color_ch = if ch >= 3 { 3 } else { ch } - 571
let n = w * h - 572
let bpp = ch * 2 - 573●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 574●
let buf : Array[Int32] = Array::make(ksize * ksize, 0) - 575
for y in 0..<h { - 576●
for x in 0..<w { - 577●
for c in 0..<color_ch { - 578●
let mut idx = 0 - 579
for dy in -radius..<=radius { - 580●
for dx in -radius..<=radius { - 581●
let sx = @types.clamp_coord(x + dx, w) - 582●
let sy = @types.clamp_coord(y + dy, h) - 583●
buf[idx] = read_u16_le_f(img.data, (sy * w + sx) * bpp + c * 2) - 584
idx += 1 - 585
} - 586
} - 587
let mid = ksize * ksize / 2 - 588
let mut lo = 0 - 589
let mut hi = ksize * ksize - 1 - 590
while lo < hi { - 591●
let pivot = buf[(lo + hi) / 2] - 592
let mut i = lo - 593
let mut j = hi - 594
while i <= j { - 595●
while buf[i] < pivot { - 596●
i += 1 - 597
} - 598
while buf[j] > pivot { - 599●
j -= 1 - 600
} - 601
if i <= j { - 602●
let tmp = buf[i] - 603
buf[i] = buf[j] - 604
buf[j] = tmp - 605
i += 1 - 606
j -= 1 - 607
} - 608
} - 609
if mid <= j { - 610●
hi = j - 611●
} else if mid >= i { - 612●
lo = i - 613
} else { - 614●
lo = mid - 615
hi = mid - 616
} - 617
} - 618●
write_u16_le_f(out, (y * w + x) * bpp + c * 2, buf[mid]) - 619
} - 620
} - 621
} - 622
// 复制 alpha - 623
if ch == 4 { - 624●
for i in 0..<n { - 625●
let alpha_off = i * bpp + 6 - 626
out[alpha_off] = img.data[alpha_off] - 627
out[alpha_off + 1] = img.data[alpha_off + 1] - 628
} - 629
} - 630
@types.Image16::{ - 631
width: w, - 632
height: h, - 633
channels: ch, - 634●
data: Bytes::from_array(out), - 635
} - 636
} - 637
- 638
///| - 639
/// float 中值滤波。ksize 为窗口大小(奇数)。有效去除椒盐噪声。 - 640
/// 仅滤波颜色通道,alpha 不变。 - 641
/// @see median_blur 8-bit 版本,本函数为 ImageF 变体 - 642
pub fn median_blur_f( - 643
img : @types.ImageF, - 644
ksize : Int32, - 645
) -> @types.ImageF raise @types.LoadError { - 646
// 输入验证 - 647●
if img.width <= 0 || img.height <= 0 { - 648●
raise @types.LoadError::DecodeFailed( - 649
"median_blur_f: 图像尺寸必须大于0", - 650
) - 651
} - 652
if img.channels < 1 || img.channels > 4 { - 653●
raise @types.LoadError::DecodeFailed( - 654
"median_blur_f: 通道数必须在1-4之间", - 655
) - 656
} - 657
let expected_size = img.width * img.height * img.channels * 4 - 658●
if img.data.length() != expected_size { - 659●
raise @types.LoadError::DecodeFailed( - 660
"median_blur_f: 数据大小不匹配,期望 " + - 661●
expected_size.to_string() + - 662
",实际 " + - 663●
img.data.length().to_string(), - 664
) - 665
} - 666
if ksize <= 0 { - 667●
raise @types.LoadError::DecodeFailed("median_blur_f: ksize必须大于0") - 668
} - 669
if ksize % 2 == 0 { - 670●
raise @types.LoadError::DecodeFailed("median_blur_f: ksize必须为奇数") - 671
} - 672
if ksize <= 1 { - 673●
return img - 674
} - 675
let radius = ksize / 2 - 676
let w = img.width - 677
let h = img.height - 678
let ch = img.channels - 679●
let color_ch = if ch >= 3 { 3 } else { ch } - 680
let n = w * h - 681
let bpp = ch * 4 - 682●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 683●
let buf : Array[Float] = Array::make(ksize * ksize, 0.0F) - 684
for y in 0..<h { - 685●
for x in 0..<w { - 686●
for c in 0..<color_ch { - 687●
let mut idx = 0 - 688
for dy in -radius..<=radius { - 689●
for dx in -radius..<=radius { - 690●
let sx = @types.clamp_coord(x + dx, w) - 691●
let sy = @types.clamp_coord(y + dy, h) - 692●
buf[idx] = read_f32_le_f(img.data, (sy * w + sx) * bpp + c * 4) - 693
idx += 1 - 694
} - 695
} - 696
// 完整排序找中值(Float 需要完整排序) - 697
let total = ksize * ksize - 698
for i in 0..<total { - 699●
for j in 0..<(total - i - 1) { - 700●
if buf[j] > buf[j + 1] { - 701●
let tmp = buf[j] - 702
buf[j] = buf[j + 1] - 703
buf[j + 1] = tmp - 704
} - 705
} - 706
} - 707●
write_f32_le_f(out, (y * w + x) * bpp + c * 4, buf[total / 2]) - 708
} - 709
} - 710
} - 711
// 复制 alpha - 712
if ch == 4 { - 713●
for i in 0..<n { - 714●
let alpha_off = i * bpp + 12 - 715
for b in 0..<4 { - 716●
out[alpha_off + b] = img.data[alpha_off + b] - 717
} - 718
} - 719
} - 720
@types.ImageF::{ - 721
width: w, - 722
height: h, - 723
channels: ch, - 724●
data: Bytes::from_array(out), - 725
} - 726
} - 727
- 728
///| - 729
/// 16-bit 锐化。amount 控制锐化强度(0.0=原图,1.0=强锐化)。 - 730
/// @see sharpen 8-bit 版本,本函数为 Image16 变体 - 731
pub fn sharpen_16( - 732
img : @types.Image16, - 733
amount : Float, - 734
) -> @types.Image16 raise @types.LoadError { - 735
// 输入验证 - 736●
if img.width <= 0 || img.height <= 0 { - 737●
raise @types.LoadError::DecodeFailed( - 738
"sharpen_16: 图像尺寸必须大于0", - 739
) - 740
} - 741
if img.channels < 1 || img.channels > 4 { - 742●
raise @types.LoadError::DecodeFailed( - 743
"sharpen_16: 通道数必须在1-4之间", - 744
) - 745
} - 746
let expected_size = img.width * img.height * img.channels * 2 - 747●
if img.data.length() != expected_size { - 748●
raise @types.LoadError::DecodeFailed( - 749
"sharpen_16: 数据大小不匹配,期望 " + - 750●
expected_size.to_string() + - 751
",实际 " + - 752●
img.data.length().to_string(), - 753
) - 754
} - 755
if amount < 0.0F { - 756○
raise @types.LoadError::DecodeFailed("sharpen_16: amount不能为负") - 757
} - 758
- 759
let w = img.width - 760
let h = img.height - 761
let ch = img.channels - 762●
let color_ch = if ch >= 3 { 3 } else { ch } - 763
let n = w * h - 764
let bpp = ch * 2 - 765
let center = 1.0F + 4.0F * amount - 766●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 767●
let px : Array[Int32] = Array::make(n * ch, 0) - 768
for i in 0..<n { - 769●
for c in 0..<ch { - 770●
px[i * ch + c] = read_u16_le_f(img.data, i * bpp + c * 2) - 771
} - 772
} - 773
for y in 1..<(h - 1) { - 774●
for x in 1..<(w - 1) { - 775●
let idx = y * w + x - 776
for c in 0..<color_ch { - 777●
let cx = px[idx * ch + c] - 778
let left = px[(idx - 1) * ch + c] - 779
let right = px[(idx + 1) * ch + c] - 780
let top = px[(idx - w) * ch + c] - 781
let bottom = px[(idx + w) * ch + c] - 782●
let val = Float::from_int(cx) * center - - 783●
amount * Float::from_int(left + right + top + bottom) - 784●
let new_val = (val + 0.5F).to_int() - 785
let clamped = if new_val < 0 { - 786●
0 - 787●
} else if new_val > 65535 { - 788●
65535 - 789
} else { - 790●
new_val - 791
} - 792●
write_u16_le_f(out, idx * bpp + c * 2, clamped) - 793
} - 794
} - 795
} - 796
for y in 0..<h { - 797●
for x in 0..<w { - 798●
if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 { - 799●
continue - 800
} - 801
let idx = y * w + x - 802●
let x_l = @types.clamp_coord(x - 1, w) - 803●
let x_r = @types.clamp_coord(x + 1, w) - 804●
let y_t = @types.clamp_coord(y - 1, h) - 805●
let y_b = @types.clamp_coord(y + 1, h) - 806
for c in 0..<color_ch { - 807●
let cx = px[idx * ch + c] - 808
let left = px[(y * w + x_l) * ch + c] - 809
let right = px[(y * w + x_r) * ch + c] - 810
let top = px[(y_t * w + x) * ch + c] - 811
let bottom = px[(y_b * w + x) * ch + c] - 812●
let val = Float::from_int(cx) * center - - 813●
amount * Float::from_int(left + right + top + bottom) - 814●
let new_val = (val + 0.5F).to_int() - 815
let clamped = if new_val < 0 { - 816●
0 - 817●
} else if new_val > 65535 { - 818●
65535 - 819
} else { - 820●
new_val - 821
} - 822●
write_u16_le_f(out, idx * bpp + c * 2, clamped) - 823
} - 824
} - 825
} - 826
if ch == 4 { - 827●
for i in 0..<n { - 828●
let alpha_off = i * bpp + 6 - 829
out[alpha_off] = img.data[alpha_off] - 830
out[alpha_off + 1] = img.data[alpha_off + 1] - 831
} - 832
} - 833
@types.Image16::{ - 834
width: w, - 835
height: h, - 836
channels: ch, - 837●
data: Bytes::from_array(out), - 838
} - 839
} - 840
- 841
///| - 842
/// float 锐化。amount 控制锐化强度(0.0=原图,1.0=强锐化)。 - 843
/// @see sharpen 8-bit 版本,本函数为 ImageF 变体 - 844
pub fn sharpen_f( - 845
img : @types.ImageF, - 846
amount : Float, - 847
) -> @types.ImageF raise @types.LoadError { - 848
// 输入验证 - 849●
if img.width <= 0 || img.height <= 0 { - 850●
raise @types.LoadError::DecodeFailed("sharpen_f: 图像尺寸必须大于0") - 851
} - 852
if img.channels < 1 || img.channels > 4 { - 853●
raise @types.LoadError::DecodeFailed( - 854
"sharpen_f: 通道数必须在1-4之间", - 855
) - 856
} - 857
let expected_size = img.width * img.height * img.channels * 4 - 858●
if img.data.length() != expected_size { - 859●
raise @types.LoadError::DecodeFailed( - 860
"sharpen_f: 数据大小不匹配,期望 " + - 861●
expected_size.to_string() + - 862
",实际 " + - 863●
img.data.length().to_string(), - 864
) - 865
} - 866
if amount < 0.0F { - 867○
raise @types.LoadError::DecodeFailed("sharpen_f: amount不能为负") - 868
} - 869
- 870
let w = img.width - 871
let h = img.height - 872
let ch = img.channels - 873●
let color_ch = if ch >= 3 { 3 } else { ch } - 874
let n = w * h - 875
let bpp = ch * 4 - 876
let center = 1.0F + 4.0F * amount - 877●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 878●
let px : Array[Float] = Array::make(n * ch, 0.0F) - 879
for i in 0..<n { - 880●
for c in 0..<ch { - 881●
px[i * ch + c] = read_f32_le_f(img.data, i * bpp + c * 4) - 882
} - 883
} - 884
for y in 1..<(h - 1) { - 885●
for x in 1..<(w - 1) { - 886●
let idx = y * w + x - 887
for c in 0..<color_ch { - 888●
let cx = px[idx * ch + c] - 889
let left = px[(idx - 1) * ch + c] - 890
let right = px[(idx + 1) * ch + c] - 891
let top = px[(idx - w) * ch + c] - 892
let bottom = px[(idx + w) * ch + c] - 893
let val = cx * center - amount * (left + right + top + bottom) - 894●
write_f32_le_f(out, idx * bpp + c * 4, val) - 895
} - 896
} - 897
} - 898
for y in 0..<h { - 899●
for x in 0..<w { - 900●
if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 { - 901●
continue - 902
} - 903
let idx = y * w + x - 904●
let x_l = @types.clamp_coord(x - 1, w) - 905●
let x_r = @types.clamp_coord(x + 1, w) - 906●
let y_t = @types.clamp_coord(y - 1, h) - 907●
let y_b = @types.clamp_coord(y + 1, h) - 908
for c in 0..<color_ch { - 909●
let cx = px[idx * ch + c] - 910
let left = px[(y * w + x_l) * ch + c] - 911
let right = px[(y * w + x_r) * ch + c] - 912
let top = px[(y_t * w + x) * ch + c] - 913
let bottom = px[(y_b * w + x) * ch + c] - 914
let val = cx * center - amount * (left + right + top + bottom) - 915●
write_f32_le_f(out, idx * bpp + c * 4, val) - 916
} - 917
} - 918
} - 919
if ch == 4 { - 920●
for i in 0..<n { - 921●
let alpha_off = i * bpp + 12 - 922
for b in 0..<4 { - 923●
out[alpha_off + b] = img.data[alpha_off + b] - 924
} - 925
} - 926
} - 927
@types.ImageF::{ - 928
width: w, - 929
height: h, - 930
channels: ch, - 931●
data: Bytes::from_array(out), - 932
} - 933
}