image.process\transform\transform.mbt
86.3% · 44 / 51 points · 241 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 变换 transform - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 图像变换操作(纯 MoonBit):crop / rotate / flip - 7
// 支持 Image / Image16 / ImageF 三种类型 - 8
- 9
///| - 10
/// 裁剪 8-bit Image 的指定区域 - 11
pub fn crop( - 12
img : @types.Image, - 13
x : Int32, - 14
y : Int32, - 15
w : Int32, - 16
h : Int32, - 17
) -> @types.Image raise @types.LoadError { - 18●
if x < 0 || - 19
y < 0 || - 20
w <= 0 || - 21
h <= 0 || - 22
x + w > img.width || - 23
y + h > img.height { - 24●
raise @types.LoadError::DecodeFailed("crop 区域越界") - 25
} - 26
let ch = img.channels - 27●
let out : Array[Byte] = Array::make(w * h * ch, b'\x00') - 28
for row in 0..<h { - 29●
let src_off = ((y + row) * img.width + x) * ch - 30
let dst_off = row * w * ch - 31
for i in 0..<(w * ch) { - 32●
out[dst_off + i] = img.data[src_off + i] - 33
} - 34
} - 35
@types.Image::{ - 36
width: w, - 37
height: h, - 38
channels: ch, - 39●
data: Bytes::from_array(out), - 40
} - 41
} - 42
- 43
///| - 44
/// 按 `Region` 裁剪,越界部分自动裁掉(不报错)。空区域返回 0×0 图。 - 45
/// 内部走零复制的 `ImageRegion` 视图,只为目标像素分配一次内存。 - 46
pub fn crop_region(img : @types.Image, region : @types.Region) -> @types.Image { - 47○
img.region_with(region).to_image() - 48
} - 49
- 50
///| - 51
/// 顺时针旋转 90 度 - 52
pub fn rotate_90(img : @types.Image) -> @types.Image { - 53●
let ch = img.channels - 54
let new_w = img.height - 55
let new_h = img.width - 56●
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00') - 57
for y in 0..<img.height { - 58●
for x in 0..<img.width { - 59●
let nx = img.height - 1 - y - 60
let ny = x - 61
let src = (y * img.width + x) * ch - 62
let dst = (ny * new_w + nx) * ch - 63
for c in 0..<ch { - 64●
out[dst + c] = img.data[src + c] - 65
} - 66
} - 67
} - 68
@types.Image::{ - 69
width: new_w, - 70
height: new_h, - 71
channels: ch, - 72●
data: Bytes::from_array(out), - 73
} - 74
} - 75
- 76
///| - 77
/// 旋转 180 度 - 78
pub fn rotate_180(img : @types.Image) -> @types.Image { - 79●
let ch = img.channels - 80●
let out : Array[Byte] = Array::make(img.width * img.height * ch, b'\x00') - 81
for y in 0..<img.height { - 82●
for x in 0..<img.width { - 83●
let nx = img.width - 1 - x - 84
let ny = img.height - 1 - y - 85
let src = (y * img.width + x) * ch - 86
let dst = (ny * img.width + nx) * ch - 87
for c in 0..<ch { - 88●
out[dst + c] = img.data[src + c] - 89
} - 90
} - 91
} - 92
@types.Image::{ - 93
width: img.width, - 94
height: img.height, - 95
channels: ch, - 96●
data: Bytes::from_array(out), - 97
} - 98
} - 99
- 100
///| - 101
/// 顺时针旋转 270 度(逆时针 90 度) - 102
pub fn rotate_270(img : @types.Image) -> @types.Image { - 103●
let ch = img.channels - 104
let new_w = img.height - 105
let new_h = img.width - 106●
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00') - 107
for y in 0..<img.height { - 108●
for x in 0..<img.width { - 109●
let nx = y - 110
let ny = img.width - 1 - x - 111
let src = (y * img.width + x) * ch - 112
let dst = (ny * new_w + nx) * ch - 113
for c in 0..<ch { - 114●
out[dst + c] = img.data[src + c] - 115
} - 116
} - 117
} - 118
@types.Image::{ - 119
width: new_w, - 120
height: new_h, - 121
channels: ch, - 122●
data: Bytes::from_array(out), - 123
} - 124
} - 125
- 126
///| - 127
/// 水平翻转(左右镜像) - 128
pub fn flip_horizontal(img : @types.Image) -> @types.Image { - 129●
let ch = img.channels - 130●
let out : Array[Byte] = Array::make(img.width * img.height * ch, b'\x00') - 131
for y in 0..<img.height { - 132●
for x in 0..<img.width { - 133●
let nx = img.width - 1 - x - 134
let src = (y * img.width + x) * ch - 135
let dst = (y * img.width + nx) * ch - 136
for c in 0..<ch { - 137●
out[dst + c] = img.data[src + c] - 138
} - 139
} - 140
} - 141
@types.Image::{ - 142
width: img.width, - 143
height: img.height, - 144
channels: ch, - 145●
data: Bytes::from_array(out), - 146
} - 147
} - 148
- 149
///| - 150
/// 垂直翻转(上下镜像) - 151
pub fn flip_vertical(img : @types.Image) -> @types.Image { - 152●
let ch = img.channels - 153
let w = img.width - 154
let h = img.height - 155●
let out : Array[Byte] = Array::make(w * h * ch, b'\x00') - 156
for y in 0..<h { - 157●
for x in 0..<w { - 158●
let src_off = ((h - 1 - y) * w + x) * ch - 159
let dst_off = (y * w + x) * ch - 160
for c in 0..<ch { - 161●
out[dst_off + c] = img.data[src_off + c] - 162
} - 163
} - 164
} - 165
@types.Image::{ - 166
width: w, - 167
height: h, - 168
channels: ch, - 169●
data: Bytes::from_array(out), - 170
} - 171
} - 172
- 173
///| - 174
/// 裁剪 16-bit Image16 - 175
/// @see crop 8-bit 版本,本函数为 Image16 变体 - 176
pub fn crop_16( - 177
img : @types.Image16, - 178
x : Int32, - 179
y : Int32, - 180
w : Int32, - 181
h : Int32, - 182
) -> @types.Image16 raise @types.LoadError { - 183●
if x < 0 || - 184
y < 0 || - 185
w <= 0 || - 186
h <= 0 || - 187
x + w > img.width || - 188
y + h > img.height { - 189●
raise @types.LoadError::DecodeFailed("crop 区域越界") - 190
} - 191
let ch = img.channels - 192●
let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00') - 193
for row in 0..<h { - 194●
let src_off = ((y + row) * img.width + x) * ch * 2 - 195
let dst_off = row * w * ch * 2 - 196
for i in 0..<(w * ch * 2) { - 197●
out[dst_off + i] = img.data[src_off + i] - 198
} - 199
} - 200
@types.Image16::{ - 201
width: w, - 202
height: h, - 203
channels: ch, - 204●
data: Bytes::from_array(out), - 205
} - 206
} - 207
- 208
///| - 209
/// 裁剪 float ImageF - 210
/// @see crop 8-bit 版本,本函数为 ImageF 变体 - 211
pub fn crop_f( - 212
img : @types.ImageF, - 213
x : Int32, - 214
y : Int32, - 215
w : Int32, - 216
h : Int32, - 217
) -> @types.ImageF raise @types.LoadError { - 218●
if x < 0 || - 219
y < 0 || - 220
w <= 0 || - 221
h <= 0 || - 222
x + w > img.width || - 223
y + h > img.height { - 224●
raise @types.LoadError::DecodeFailed("crop 区域越界") - 225
} - 226
let ch = img.channels - 227○
let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00') - 228
for row in 0..<h { - 229○
let src_off = ((y + row) * img.width + x) * ch * 4 - 230
let dst_off = row * w * ch * 4 - 231
for i in 0..<(w * ch * 4) { - 232○
out[dst_off + i] = img.data[src_off + i] - 233
} - 234
} - 235
@types.ImageF::{ - 236
width: w, - 237
height: h, - 238
channels: ch, - 239○
data: Bytes::from_array(out), - 240
} - 241
}