image.process\filter\filter.mbt
90.1% · 182 / 202 points · 539 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 滤波 filter - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 图像滤波/卷积(纯 MoonBit) - 7
// box_blur / gaussian_blur / sharpen / convolve - 8
- 9
///| - 10
/// 盒模糊(均值滤波)。radius 为模糊半径,窗口大小 = 2*radius+1。 - 11
/// 使用可分离滑动窗口,O(n) 复杂度。仅模糊颜色通道,alpha 不变。 - 12
pub fn box_blur( - 13
img : @types.Image, - 14
radius : Int32, - 15
) -> @types.Image raise @types.LoadError { - 16
// 输入验证 - 17●
if img.width <= 0 || img.height <= 0 { - 18●
raise @types.LoadError::DecodeFailed("box_blur: 图像尺寸必须大于0") - 19
} - 20
if img.channels < 1 || img.channels > 4 { - 21●
raise @types.LoadError::DecodeFailed( - 22
"box_blur: 通道数必须在1-4之间", - 23
) - 24
} - 25
let expected_size = img.width * img.height * img.channels - 26●
if img.data.length() != expected_size { - 27●
raise @types.LoadError::DecodeFailed( - 28
"box_blur: 数据大小不匹配,期望 " + - 29●
expected_size.to_string() + - 30
",实际 " + - 31●
img.data.length().to_string(), - 32
) - 33
} - 34
if radius < 0 { - 35●
raise @types.LoadError::DecodeFailed("box_blur: radius不能为负") - 36
} - 37
if radius <= 0 { - 38●
return img - 39
} - 40
let w = img.width - 41
let h = img.height - 42
let ch = img.channels - 43●
let color_ch = if ch >= 3 { 3 } else { ch } - 44
let n = w * h - 45
let win_size = 2 * radius + 1 - 46
// 水平模糊:img -> temp - 47●
let temp : Array[Int32] = Array::make(n * color_ch, 0) - 48
for y in 0..<h { - 49●
for c in 0..<color_ch { - 50●
let mut sum = 0 - 51
for dx in 0..<win_size { - 52●
let x = @types.clamp_coord(dx - radius, w) - 53●
sum += img.data[(y * w + x) * ch + c].to_int() - 54
} - 55
for x in 0..<w { - 56●
temp[(y * w + x) * color_ch + c] = sum / win_size - 57●
let x_out = @types.clamp_coord(x - radius, w) - 58●
let x_in = @types.clamp_coord(x + radius + 1, w) - 59
sum = sum - - 60●
img.data[(y * w + x_out) * ch + c].to_int() + - 61●
img.data[(y * w + x_in) * ch + c].to_int() - 62
} - 63
} - 64
} - 65
// 垂直模糊:temp -> out - 66●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 67
for x in 0..<w { - 68●
for c in 0..<color_ch { - 69●
let mut sum = 0 - 70
for dy in 0..<win_size { - 71●
let y = @types.clamp_coord(dy - radius, h) - 72
sum += temp[(y * w + x) * color_ch + c] - 73
} - 74
for y in 0..<h { - 75●
out[(y * w + x) * ch + c] = (sum / win_size).to_byte() - 76●
let y_out = @types.clamp_coord(y - radius, h) - 77●
let y_in = @types.clamp_coord(y + radius + 1, h) - 78
sum = sum - - 79
temp[(y_out * w + x) * color_ch + c] + - 80
temp[(y_in * w + x) * color_ch + c] - 81
} - 82
} - 83
} - 84
// 复制 alpha - 85
if ch == 4 { - 86●
for i in 0..<n { - 87●
out[i * ch + 3] = img.data[i * ch + 3] - 88
} - 89
} - 90
@types.Image::{ - 91
width: w, - 92
height: h, - 93
channels: ch, - 94●
data: Bytes::from_array(out), - 95
} - 96
} - 97
- 98
///| - 99
/// 高斯模糊。radius 为核半径,sigma 为标准差。 - 100
/// 使用可分离高斯核,O(n*radius) 复杂度。仅模糊颜色通道,alpha 不变。 - 101
/// 高斯模糊。radius 为核半径,sigma 为标准差。 Gaussian blur. radius is kernel radius, sigma is standard deviation. - 102
/// 使用可分离高斯核,O(n*radius) 复杂度。仅模糊颜色通道,alpha 不变。 Uses separable Gaussian kernel, O(n*radius) complexity. Only blurs color channels, alpha unchanged. - 103
/// - 104
/// ```mbt check - 105
/// test "doc_gaussian_blur" { - 106
/// try { - 107
/// let data = Bytes::from_array([b'\xff', b'\x00', b'\x00']) - 108
/// let img = @types.Image::{ width: 1, height: 1, channels: 3, data, } - 109
/// let blurred = @filter.gaussian_blur(img, 1, 1.0) - 110
/// assert_eq(blurred.width, 1) - 111
/// } catch { - 112
/// _ => assert_true(false) - 113
/// } - 114
/// } - 115
/// ``` - 116
pub fn gaussian_blur( - 117
img : @types.Image, - 118
radius : Int32, - 119
sigma : Float, - 120
) -> @types.Image raise @types.LoadError { - 121
// 输入验证 - 122●
if img.width <= 0 || img.height <= 0 { - 123●
raise @types.LoadError::DecodeFailed( - 124
"gaussian_blur: 图像尺寸必须大于0", - 125
) - 126
} - 127
if img.channels < 1 || img.channels > 4 { - 128●
raise @types.LoadError::DecodeFailed( - 129
"gaussian_blur: 通道数必须在1-4之间", - 130
) - 131
} - 132
let expected_size = img.width * img.height * img.channels - 133●
if img.data.length() != expected_size { - 134○
raise @types.LoadError::DecodeFailed( - 135
"gaussian_blur: 数据大小不匹配,期望 " + - 136○
expected_size.to_string() + - 137
",实际 " + - 138○
img.data.length().to_string(), - 139
) - 140
} - 141
if radius < 0 { - 142●
raise @types.LoadError::DecodeFailed("gaussian_blur: radius不能为负") - 143
} - 144
if sigma <= 0.0F { - 145●
raise @types.LoadError::DecodeFailed("gaussian_blur: sigma必须大于0") - 146
} - 147
if radius <= 0 { - 148●
return img - 149
} - 150
let w = img.width - 151
let h = img.height - 152
let ch = img.channels - 153●
let color_ch = if ch >= 3 { 3 } else { ch } - 154
let n = w * h - 155
let win_size = 2 * radius + 1 - 156
// 生成 1D 高斯核 - 157●
let kernel : Array[Float] = Array::make(win_size, 0.0F) - 158
let two_sigma_sq = 2.0F * sigma * sigma - 159
let mut ksum = 0.0F - 160
for i in 0..<win_size { - 161●
let x = Float::from_int(i - radius) - 162●
let val = @math.expf(-(x * x) / two_sigma_sq) - 163
kernel[i] = val - 164
ksum += val - 165
} - 166
// 归一化 - 167
for i in 0..<win_size { - 168●
kernel[i] = kernel[i] / ksum - 169
} - 170
// 预计算像素 Float 数组,避免内层循环重复类型转换 - 171●
let src : Array[Float] = Array::make(n * color_ch, 0.0F) - 172
for i in 0..<n { - 173●
for c in 0..<color_ch { - 174●
src[i * color_ch + c] = Float::from_int(img.data[i * ch + c].to_int()) - 175
} - 176
} - 177
// 水平模糊:src -> temp (Float),分离内部/边界区域 - 178●
let temp : Array[Float] = Array::make(n * color_ch, 0.0F) - 179●
let x_inner_start = if radius >= w { w } else { radius } - 180●
let x_inner_end = if radius >= w { w } else { w - radius } - 181
for y in 0..<h { - 182●
let row_base = y * w - 183
for c in 0..<color_ch { - 184
// 内部区域:x >= radius && x < w - radius,无需 @types.clamp_coord - 185●
for x in x_inner_start..<x_inner_end { - 186●
let mut acc = 0.0F - 187
let base = (row_base + x - radius) * color_ch + c - 188
for k in 0..<win_size { - 189●
acc += kernel[k] * src[base + k * color_ch] - 190
} - 191
temp[(row_base + x) * color_ch + c] = acc - 192
} - 193
// 左边界:x < radius - 194
for x in 0..<x_inner_start { - 195●
let mut acc = 0.0F - 196
for k in 0..<win_size { - 197●
let sx = @types.clamp_coord(x + k - radius, w) - 198
acc += kernel[k] * src[(row_base + sx) * color_ch + c] - 199
} - 200
temp[(row_base + x) * color_ch + c] = acc - 201
} - 202
// 右边界:x >= w - radius - 203
for x in x_inner_end..<w { - 204●
let mut acc = 0.0F - 205
for k in 0..<win_size { - 206●
let sx = @types.clamp_coord(x + k - radius, w) - 207
acc += kernel[k] * src[(row_base + sx) * color_ch + c] - 208
} - 209
temp[(row_base + x) * color_ch + c] = acc - 210
} - 211
} - 212
} - 213
// 垂直模糊:temp -> out,分离内部/边界区域 - 214●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 215●
let y_inner_start = if radius >= h { h } else { radius } - 216●
let y_inner_end = if radius >= h { h } else { h - radius } - 217
for x in 0..<w { - 218●
for c in 0..<color_ch { - 219
// 内部区域:y >= radius && y < h - radius - 220●
for y in y_inner_start..<y_inner_end { - 221●
let mut acc = 0.0F - 222
let base = ((y - radius) * w + x) * color_ch + c - 223
for k in 0..<win_size { - 224●
acc += kernel[k] * temp[base + k * w * color_ch] - 225
} - 226
out[(y * w + x) * ch + c] = (acc + 0.5F) - 227●
.to_int() - 228●
.clamp(min=0, max=255) - 229●
.to_byte() - 230
} - 231
// 上边界:y < radius - 232
for y in 0..<y_inner_start { - 233●
let mut acc = 0.0F - 234
for k in 0..<win_size { - 235●
let sy = @types.clamp_coord(y + k - radius, h) - 236
acc += kernel[k] * temp[(sy * w + x) * color_ch + c] - 237
} - 238
out[(y * w + x) * ch + c] = (acc + 0.5F) - 239●
.to_int() - 240●
.clamp(min=0, max=255) - 241●
.to_byte() - 242
} - 243
// 下边界:y >= h - radius - 244
for y in y_inner_end..<h { - 245●
let mut acc = 0.0F - 246
for k in 0..<win_size { - 247●
let sy = @types.clamp_coord(y + k - radius, h) - 248
acc += kernel[k] * temp[(sy * w + x) * color_ch + c] - 249
} - 250
out[(y * w + x) * ch + c] = (acc + 0.5F) - 251●
.to_int() - 252●
.clamp(min=0, max=255) - 253●
.to_byte() - 254
} - 255
} - 256
} - 257
// 复制 alpha - 258
if ch == 4 { - 259○
for i in 0..<n { - 260○
out[i * ch + 3] = img.data[i * ch + 3] - 261
} - 262
} - 263
@types.Image::{ - 264
width: w, - 265
height: h, - 266
channels: ch, - 267●
data: Bytes::from_array(out), - 268
} - 269
} - 270
- 271
///| - 272
/// 锐化。amount 控制锐化强度(0.0=原图,1.0=强锐化)。 - 273
/// 使用拉普拉斯锐化卷积核。仅处理颜色通道,alpha 不变。 - 274
pub fn sharpen( - 275
img : @types.Image, - 276
amount : Float, - 277
) -> @types.Image raise @types.LoadError { - 278
// 输入验证 - 279●
if img.width <= 0 || img.height <= 0 { - 280●
raise @types.LoadError::DecodeFailed("sharpen: 图像尺寸必须大于0") - 281
} - 282
if img.channels < 1 || img.channels > 4 { - 283●
raise @types.LoadError::DecodeFailed("sharpen: 通道数必须在1-4之间") - 284
} - 285
let expected_size = img.width * img.height * img.channels - 286●
if img.data.length() != expected_size { - 287○
raise @types.LoadError::DecodeFailed( - 288
"sharpen: 数据大小不匹配,期望 " + - 289○
expected_size.to_string() + - 290
",实际 " + - 291○
img.data.length().to_string(), - 292
) - 293
} - 294
if amount < 0.0F { - 295●
raise @types.LoadError::DecodeFailed("sharpen: amount不能为负") - 296
} - 297
- 298
let w = img.width - 299
let h = img.height - 300
let ch = img.channels - 301●
let color_ch = if ch >= 3 { 3 } else { ch } - 302
let n = w * h - 303
let center = 1.0F + 4.0F * amount - 304●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 305
// 预计算像素 Int 数组 - 306●
let px : Array[Int32] = Array::make(n * ch, 0) - 307
for i in 0..<(n * ch) { - 308●
px[i] = img.data[i].to_int() - 309
} - 310
// 内部区域:1 <= x < w-1 && 1 <= y < h-1,无需 @types.clamp_coord - 311
for y in 1..<(h - 1) { - 312●
for x in 1..<(w - 1) { - 313●
let base = (y * w + x) * ch - 314
let base_l = base - ch - 315
let base_r = base + ch - 316
let base_t = base - w * ch - 317
let base_b = base + w * ch - 318
for c in 0..<color_ch { - 319●
let cx = px[base + c] - 320
let left = px[base_l + c] - 321
let right = px[base_r + c] - 322
let top = px[base_t + c] - 323
let bottom = px[base_b + c] - 324●
let val = Float::from_int(cx) * center - - 325●
amount * Float::from_int(left + right + top + bottom) - 326●
out[base + c] = (val + 0.5F).to_int().clamp(min=0, max=255).to_byte() - 327
} - 328
} - 329
} - 330
// 边界区域:x==0 || x==w-1 || y==0 || y==h-1 - 331
for y in 0..<h { - 332●
for x in 0..<w { - 333●
if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 { - 334●
continue - 335
} - 336
let base = (y * w + x) * ch - 337
for c in 0..<color_ch { - 338●
let cx = px[base + c] - 339●
let x_l = @types.clamp_coord(x - 1, w) - 340●
let x_r = @types.clamp_coord(x + 1, w) - 341●
let y_t = @types.clamp_coord(y - 1, h) - 342●
let y_b = @types.clamp_coord(y + 1, h) - 343
let left = px[(y * w + x_l) * ch + c] - 344
let right = px[(y * w + x_r) * ch + c] - 345
let top = px[(y_t * w + x) * ch + c] - 346
let bottom = px[(y_b * w + x) * ch + c] - 347●
let val = Float::from_int(cx) * center - - 348●
amount * Float::from_int(left + right + top + bottom) - 349●
out[base + c] = (val + 0.5F).to_int().clamp(min=0, max=255).to_byte() - 350
} - 351
} - 352
} - 353
// 复制 alpha - 354
if ch == 4 { - 355●
for i in 0..<n { - 356●
out[i * ch + 3] = img.data[i * ch + 3] - 357
} - 358
} - 359
@types.Image::{ - 360
width: w, - 361
height: h, - 362
channels: ch, - 363●
data: Bytes::from_array(out), - 364
} - 365
} - 366
- 367
///| - 368
/// 中值滤波。ksize 为窗口大小(奇数)。有效去除椒盐噪声。 - 369
/// 对每个像素,取 ksize×ksize 邻域的中值。仅滤波颜色通道,alpha 不变。 - 370
pub fn median_blur( - 371
img : @types.Image, - 372
ksize : Int32, - 373
) -> @types.Image raise @types.LoadError { - 374
// 输入验证 - 375●
if img.width <= 0 || img.height <= 0 { - 376●
raise @types.LoadError::DecodeFailed( - 377
"median_blur: 图像尺寸必须大于0", - 378
) - 379
} - 380
if img.channels < 1 || img.channels > 4 { - 381●
raise @types.LoadError::DecodeFailed( - 382
"median_blur: 通道数必须在1-4之间", - 383
) - 384
} - 385
let expected_size = img.width * img.height * img.channels - 386●
if img.data.length() != expected_size { - 387○
raise @types.LoadError::DecodeFailed( - 388
"median_blur: 数据大小不匹配,期望 " + - 389○
expected_size.to_string() + - 390
",实际 " + - 391○
img.data.length().to_string(), - 392
) - 393
} - 394
if ksize <= 0 { - 395●
raise @types.LoadError::DecodeFailed("median_blur: ksize必须大于0") - 396
} - 397
if ksize % 2 == 0 { - 398●
raise @types.LoadError::DecodeFailed("median_blur: ksize必须为奇数") - 399
} - 400
if ksize <= 1 { - 401●
return img - 402
} - 403
let radius = ksize / 2 - 404
let w = img.width - 405
let h = img.height - 406
let ch = img.channels - 407●
let color_ch = if ch >= 3 { 3 } else { ch } - 408
let n = w * h - 409●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 410●
let buf : Array[Int32] = Array::make(ksize * ksize, 0) - 411
for y in 0..<h { - 412●
for x in 0..<w { - 413●
for c in 0..<color_ch { - 414●
let mut idx = 0 - 415
for dy in -radius..<=radius { - 416●
for dx in -radius..<=radius { - 417●
let sx = @types.clamp_coord(x + dx, w) - 418●
let sy = @types.clamp_coord(y + dy, h) - 419●
buf[idx] = img.data[(sy * w + sx) * ch + c].to_int() - 420
idx += 1 - 421
} - 422
} - 423
// 部分排序找中值(不需要完整排序) - 424
let mid = ksize * ksize / 2 - 425
let mut lo = 0 - 426
let mut hi = ksize * ksize - 1 - 427
while lo < hi { - 428●
let pivot = buf[(lo + hi) / 2] - 429
let mut i = lo - 430
let mut j = hi - 431
while i <= j { - 432●
while buf[i] < pivot { - 433●
i += 1 - 434
} - 435
while buf[j] > pivot { - 436●
j -= 1 - 437
} - 438
if i <= j { - 439●
let tmp = buf[i] - 440
buf[i] = buf[j] - 441
buf[j] = tmp - 442
i += 1 - 443
j -= 1 - 444
} - 445
} - 446
if mid <= j { - 447●
hi = j - 448●
} else if mid >= i { - 449●
lo = i - 450
} else { - 451●
lo = mid - 452
hi = mid - 453
} - 454
} - 455●
out[(y * w + x) * ch + c] = buf[mid].to_byte() - 456
} - 457
} - 458
} - 459
// 复制 alpha - 460
if ch == 4 { - 461●
for i in 0..<n { - 462●
out[i * ch + 3] = img.data[i * ch + 3] - 463
} - 464
} - 465
@types.Image::{ - 466
width: w, - 467
height: h, - 468
channels: ch, - 469●
data: Bytes::from_array(out), - 470
} - 471
} - 472
- 473
///| - 474
/// 通用卷积。kernel 为 3×3 卷积核(9 个 Float 值,行优先)。 - 475
/// 边缘采用 clamp 策略。bias 为偏移量,scale 为缩放因子。 - 476
pub fn convolve( - 477
img : @types.Image, - 478
kernel : Array[Float], - 479
bias : Float, - 480
scale : Float, - 481
) -> @types.Image raise @types.LoadError { - 482
// 输入验证 - 483●
if img.width <= 0 || img.height <= 0 { - 484●
raise @types.LoadError::DecodeFailed("convolve: 图像尺寸必须大于0") - 485
} - 486
if img.channels < 1 || img.channels > 4 { - 487●
raise @types.LoadError::DecodeFailed( - 488
"convolve: 通道数必须在1-4之间", - 489
) - 490
} - 491
let expected_size = img.width * img.height * img.channels - 492●
if img.data.length() != expected_size { - 493○
raise @types.LoadError::DecodeFailed( - 494
"convolve: 数据大小不匹配,期望 " + - 495○
expected_size.to_string() + - 496
",实际 " + - 497○
img.data.length().to_string(), - 498
) - 499
} - 500●
if kernel.length() != 9 { - 501○
raise @types.LoadError::DecodeFailed( - 502
"convolve: kernel 必须为 9 个值(3x3)", - 503
) - 504
} - 505
if scale == 0.0F { - 506●
raise @types.LoadError::DecodeFailed("convolve: scale不能为0") - 507
} - 508
let ch = img.channels - 509
let w = img.width - 510
let h = img.height - 511●
let color_ch = if ch >= 3 { 3 } else { ch } - 512●
let out : Array[Byte] = Array::make(w * h * ch, b'\x00') - 513
for y in 0..<h { - 514●
for x in 0..<w { - 515●
for c in 0..<color_ch { - 516●
let mut acc = 0.0F - 517
for ky in 0..<3 { - 518●
for kx in 0..<3 { - 519●
let sx = @types.clamp_int(x + kx - 1, 0, w - 1) - 520●
let sy = @types.clamp_int(y + ky - 1, 0, h - 1) - 521●
let v = Float::from_int(img.data[(sy * w + sx) * ch + c].to_int()) - 522
acc += kernel[ky * 3 + kx] * v - 523
} - 524
} - 525●
let result = (acc * scale + bias).to_int() - 526●
out[(y * w + x) * ch + c] = @types.clamp_byte(result) - 527
} - 528
if ch == 4 { - 529○
out[(y * w + x) * ch + 3] = img.data[(y * w + x) * ch + 3] - 530
} - 531
} - 532
} - 533
@types.Image::{ - 534
width: w, - 535
height: h, - 536
channels: ch, - 537●
data: Bytes::from_array(out), - 538
} - 539
}