image.domain\util\pixel_advanced.mbt
100% · 32 / 32 points · 124 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 高级像素操作 util/pixel_advanced - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 高级像素操作(纯 MoonBit):pixelate / replace_color / swap_channels - 7
- 8
///| - 9
/// 马赛克/像素化效果。将图像分成 block_size × block_size 的块,每块取左上角像素值。 - 10
pub fn pixelate( - 11
img : @types.Image, - 12
block_size : Int32, - 13
) -> @types.Image raise @types.LoadError { - 14●
if block_size <= 0 { - 15●
raise @types.LoadError::DecodeFailed("block_size 必须为正数") - 16
} - 17
let ch = img.channels - 18
let w = img.width - 19
let h = img.height - 20●
let out : Array[Byte] = Array::make(w * h * ch, b'\x00') - 21
for by in 0..<h { - 22●
for bx in 0..<w { - 23●
let src_x = bx / block_size * block_size - 24
let src_y = by / block_size * block_size - 25
let src_off = (src_y * w + src_x) * ch - 26
let dst_off = (by * w + bx) * ch - 27
for c in 0..<ch { - 28●
out[dst_off + c] = img.data[src_off + c] - 29
} - 30
} - 31
} - 32
@types.Image::{ - 33
width: w, - 34
height: h, - 35
channels: ch, - 36●
data: Bytes::from_array(out), - 37
} - 38
} - 39
- 40
///| - 41
/// 颜色替换。将 old_color 替换为 new_color(精确匹配或容差匹配)。 - 42
/// old_color 和 new_color 长度应等于 channels。 - 43
pub fn replace_color( - 44
img : @types.Image, - 45
old_color : Array[Byte], - 46
new_color : Array[Byte], - 47
tolerance : Int32, - 48
) -> @types.Image raise @types.LoadError { - 49●
let ch = img.channels - 50●
if old_color.length() < ch || new_color.length() < ch { - 51●
raise @types.LoadError::DecodeFailed("颜色数组长度必须 >= channels") - 52
} - 53
let n = img.width * img.height - 54●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 55
for i in 0..<n { - 56●
let off = i * ch - 57●
let is_match = check_color_match(img.data, off, ch, old_color, tolerance) - 58
if is_match { - 59●
for c in 0..<ch { - 60●
out[off + c] = new_color[c] - 61
} - 62
} else { - 63●
for c in 0..<ch { - 64●
out[off + c] = img.data[off + c] - 65
} - 66
} - 67
} - 68
@types.Image::{ - 69
width: img.width, - 70
height: img.height, - 71
channels: ch, - 72●
data: Bytes::from_array(out), - 73
} - 74
} - 75
- 76
///| - 77
/// 交换两个通道。例如 swap_channels(img, 0, 2) 交换 R 和 B。 - 78
pub fn swap_channels( - 79
img : @types.Image, - 80
ch1 : Int32, - 81
ch2 : Int32, - 82
) -> @types.Image raise @types.LoadError { - 83●
if ch1 < 0 || ch1 >= img.channels || ch2 < 0 || ch2 >= img.channels { - 84●
raise @types.LoadError::DecodeFailed("通道索引越界") - 85
} - 86
if ch1 == ch2 { - 87●
return img - 88
} - 89
let ch = img.channels - 90
let n = img.width * img.height - 91●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 92
for i in 0..<n { - 93●
let off = i * ch - 94
for c in 0..<ch { - 95●
out[off + c] = img.data[off + c] - 96
} - 97
let tmp = out[off + ch1] - 98
out[off + ch1] = out[off + ch2] - 99
out[off + ch2] = tmp - 100
} - 101
@types.Image::{ - 102
width: img.width, - 103
height: img.height, - 104
channels: ch, - 105●
data: Bytes::from_array(out), - 106
} - 107
} - 108
- 109
///| - 110
fn check_color_match( - 111
data : Bytes, - 112
off : Int32, - 113
ch : Int32, - 114
target : Array[Byte], - 115
tolerance : Int32, - 116
) -> Bool { - 117●
for c in 0..<ch { - 118●
let diff = (data[off + c].to_int() - target[c].to_int()).abs() - 119
if diff > tolerance { - 120●
return false - 121
} - 122
} - 123
true - 124
}