image.domain\util\image_combine.mbt
97.8% · 44 / 45 points · 130 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/image_combine - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 图像混合与翻转(纯 MoonBit) - 7
- 8
///| - 9
/// Alpha 混合两张图像。 - 10
/// alpha: 0.0 = 完全显示 img1, 1.0 = 完全显示 img2 - 11
/// 要求两张图像尺寸和通道数相同。 - 12
pub fn alpha_blend( - 13
img1 : @types.Image, - 14
img2 : @types.Image, - 15
alpha : Float, - 16
) -> @types.Image raise @types.LoadError { - 17●
if img1.width != img2.width || img1.height != img2.height { - 18●
raise @types.LoadError::DecodeFailed("alpha_blend: 图像尺寸不同") - 19
} - 20
if img1.channels != img2.channels { - 21●
raise @types.LoadError::DecodeFailed("alpha_blend: 图像通道数不同") - 22
} - 23
let n = img1.width * img1.height * img1.channels - 24
let result : Array[Byte] = [] - 25●
let a = alpha.clamp(min=0.0F, max=1.0F) - 26
for i in 0..<n { - 27●
let v1 = img1.data[i].to_int() - 28●
let v2 = img2.data[i].to_int() - 29●
let blended = (Float::from_int(v1) * (1.0F - a) + Float::from_int(v2) * a).to_int() - 30●
result.push(blended.clamp(min=0, max=255).to_byte()) - 31
} - 32
@types.Image::{ - 33
width: img1.width, - 34
height: img1.height, - 35
channels: img1.channels, - 36●
data: Bytes::from_array(result), - 37
} - 38
} - 39
- 40
///| - 41
/// 使用掩码混合两张图像。 - 42
/// 掩码为单通道灰度图,白色(255)显示 img2,黑色(0)显示 img1。 - 43
/// 要求掩码与图像尺寸相同。 - 44
pub fn mask_blend( - 45
img1 : @types.Image, - 46
img2 : @types.Image, - 47
mask : @types.Image, - 48
) -> @types.Image raise @types.LoadError { - 49●
if img1.width != img2.width || img1.height != img2.height { - 50●
raise @types.LoadError::DecodeFailed("mask_blend: 图像尺寸不同") - 51
} - 52
if img1.channels != img2.channels { - 53○
raise @types.LoadError::DecodeFailed("mask_blend: 图像通道数不同") - 54
} - 55
if mask.channels != 1 { - 56●
raise @types.LoadError::DecodeFailed("mask_blend: 掩码必须为单通道") - 57
} - 58
if mask.width != img1.width || mask.height != img1.height { - 59●
raise @types.LoadError::DecodeFailed("mask_blend: 掩码尺寸不同") - 60
} - 61
let w = img1.width - 62
let h = img1.height - 63
let ch = img1.channels - 64
let result : Array[Byte] = [] - 65
for y in 0..<h { - 66●
for x in 0..<w { - 67●
let m = Float::from_int(mask.data[y * w + x].to_int()) / 255.0F - 68
for c in 0..<ch { - 69●
let idx = (y * w + x) * ch + c - 70●
let v1 = img1.data[idx].to_int() - 71●
let v2 = img2.data[idx].to_int() - 72●
let blended = (Float::from_int(v1) * (1.0F - m) + - 73●
Float::from_int(v2) * m).to_int() - 74●
result.push(blended.clamp(min=0, max=255).to_byte()) - 75
} - 76
} - 77
} - 78
@types.Image::{ - 79
width: w, - 80
height: h, - 81
channels: ch, - 82●
data: Bytes::from_array(result), - 83
} - 84
} - 85
- 86
///| - 87
/// 水平翻转图像。 - 88
pub fn flip_horizontal(img : @types.Image) -> @types.Image { - 89●
let w = img.width - 90
let h = img.height - 91
let ch = img.channels - 92
let result : Array[Byte] = [] - 93
for y in 0..<h { - 94●
for x in 0..<w { - 95●
let src_x = w - 1 - x - 96
for c in 0..<ch { - 97●
result.push(img.data[(y * w + src_x) * ch + c]) - 98
} - 99
} - 100
} - 101
@types.Image::{ - 102
width: w, - 103
height: h, - 104
channels: ch, - 105●
data: Bytes::from_array(result), - 106
} - 107
} - 108
- 109
///| - 110
/// 垂直翻转图像。 - 111
pub fn flip_vertical(img : @types.Image) -> @types.Image { - 112●
let w = img.width - 113
let h = img.height - 114
let ch = img.channels - 115
let result : Array[Byte] = [] - 116
for y in 0..<h { - 117●
let src_y = h - 1 - y - 118
for x in 0..<w { - 119●
for c in 0..<ch { - 120●
result.push(img.data[(src_y * w + x) * ch + c]) - 121
} - 122
} - 123
} - 124
@types.Image::{ - 125
width: w, - 126
height: h, - 127
channels: ch, - 128●
data: Bytes::from_array(result), - 129
} - 130
}