image.domain\util\image_compose.mbt
100% · 28 / 28 points · 132 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_compose - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 图像拼接与平铺(纯 MoonBit):hstack / vstack / tile / transpose - 7
- 8
///| - 9
/// 水平拼接。两图高度和通道数必须相同。 - 10
pub fn hstack( - 11
left : @types.Image, - 12
right : @types.Image, - 13
) -> @types.Image raise @types.LoadError { - 14●
if left.height != right.height || left.channels != right.channels { - 15●
raise @types.LoadError::DecodeFailed( - 16
"hstack 两图高度/通道数必须相同", - 17
) - 18
} - 19
let ch = left.channels - 20
let new_w = left.width + right.width - 21
let h = left.height - 22●
let out : Array[Byte] = Array::make(new_w * h * ch, b'\x00') - 23
for y in 0..<h { - 24●
for x in 0..<left.width { - 25●
let src_off = (y * left.width + x) * ch - 26
let dst_off = (y * new_w + x) * ch - 27
for c in 0..<ch { - 28●
out[dst_off + c] = left.data[src_off + c] - 29
} - 30
} - 31
for x in 0..<right.width { - 32●
let src_off = (y * right.width + x) * ch - 33
let dst_off = (y * new_w + left.width + x) * ch - 34
for c in 0..<ch { - 35●
out[dst_off + c] = right.data[src_off + c] - 36
} - 37
} - 38
} - 39
@types.Image::{ - 40
width: new_w, - 41
height: h, - 42
channels: ch, - 43●
data: Bytes::from_array(out), - 44
} - 45
} - 46
- 47
///| - 48
/// 垂直拼接。两图宽度和通道数必须相同。 - 49
pub fn vstack( - 50
top : @types.Image, - 51
bottom : @types.Image, - 52
) -> @types.Image raise @types.LoadError { - 53●
if top.width != bottom.width || top.channels != bottom.channels { - 54●
raise @types.LoadError::DecodeFailed( - 55
"vstack 两图宽度/通道数必须相同", - 56
) - 57
} - 58
let ch = top.channels - 59
let w = top.width - 60
let new_h = top.height + bottom.height - 61●
let out : Array[Byte] = Array::make(w * new_h * ch, b'\x00') - 62
for i in 0..<(w * top.height * ch) { - 63●
out[i] = top.data[i] - 64
} - 65
let top_size = w * top.height * ch - 66
for i in 0..<(w * bottom.height * ch) { - 67●
out[top_size + i] = bottom.data[i] - 68
} - 69
@types.Image::{ - 70
width: w, - 71
height: new_h, - 72
channels: ch, - 73●
data: Bytes::from_array(out), - 74
} - 75
} - 76
- 77
///| - 78
/// 平铺图像。将 img 在 x 方向重复 cols 次,y 方向重复 rows 次。 - 79
pub fn tile( - 80
img : @types.Image, - 81
cols : Int32, - 82
rows : Int32, - 83
) -> @types.Image raise @types.LoadError { - 84●
if cols <= 0 || rows <= 0 { - 85●
raise @types.LoadError::DecodeFailed("tile cols/rows 必须为正数") - 86
} - 87
let ch = img.channels - 88
let new_w = img.width * cols - 89
let new_h = img.height * rows - 90●
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00') - 91
for y in 0..<new_h { - 92●
for x in 0..<new_w { - 93●
let src_x = x % img.width - 94
let src_y = y % img.height - 95
let src_off = (src_y * img.width + src_x) * ch - 96
let dst_off = (y * new_w + x) * ch - 97
for c in 0..<ch { - 98●
out[dst_off + c] = img.data[src_off + c] - 99
} - 100
} - 101
} - 102
@types.Image::{ - 103
width: new_w, - 104
height: new_h, - 105
channels: ch, - 106●
data: Bytes::from_array(out), - 107
} - 108
} - 109
- 110
///| - 111
/// 转置(沿主对角线翻转)。宽高互换。 - 112
pub fn transpose(img : @types.Image) -> @types.Image { - 113●
let ch = img.channels - 114
let new_w = img.height - 115
let new_h = img.width - 116●
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00') - 117
for y in 0..<img.height { - 118●
for x in 0..<img.width { - 119●
let src_off = (y * img.width + x) * ch - 120
let dst_off = (x * new_w + y) * ch - 121
for c in 0..<ch { - 122●
out[dst_off + c] = img.data[src_off + c] - 123
} - 124
} - 125
} - 126
@types.Image::{ - 127
width: new_w, - 128
height: new_h, - 129
channels: ch, - 130●
data: Bytes::from_array(out), - 131
} - 132
}