1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// 图像变换操作(纯 MoonBit):crop / rotate / flip
// 支持 Image / Image16 / ImageF 三种类型
///|
/// 裁剪 8-bit Image 的指定区域
pub fn crop(
img : @types.Image,
x : Int,
y : Int,
w : Int,
h : Int,
) -> @types.Image raise @types.LoadError {
if x < 0 ||
y < 0 ||
w <= 0 ||
h <= 0 ||
x + w > img.width ||
y + h > img.height {
raise @types.LoadError::DecodeFailed("crop 区域越界")
}
let ch = img.channels
let out : Array[Byte] = Array::make(w * h * ch, b'\x00')
for row in 0..<h {
let src_off = ((y + row) * img.width + x) * ch
let dst_off = row * w * ch
for i in 0..<(w * ch) {
out[dst_off + i] = img.data[src_off + i]
}
}
@types.Image::{
width: w,
height: h,
channels: ch,
data: Bytes::from_array(out),
}
}
///|
/// 顺时针旋转 90 度
pub fn rotate_90(img : @types.Image) -> @types.Image {
let ch = img.channels
let new_w = img.height
let new_h = img.width
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00')
for y in 0..<img.height {
for x in 0..<img.width {
let nx = img.height - 1 - y
let ny = x
let src = (y * img.width + x) * ch
let dst = (ny * new_w + nx) * ch
for c in 0..<ch {
out[dst + c] = img.data[src + c]
}
}
}
@types.Image::{
width: new_w,
height: new_h,
channels: ch,
data: Bytes::from_array(out),
}
}
///|
/// 旋转 180 度
pub fn rotate_180(img : @types.Image) -> @types.Image {
let ch = img.channels
let out : Array[Byte] = Array::make(img.width * img.height * ch, b'\x00')
for y in 0..<img.height {
for x in 0..<img.width {
let nx = img.width - 1 - x
let ny = img.height - 1 - y
let src = (y * img.width + x) * ch
let dst = (ny * img.width + nx) * ch
for c in 0..<ch {
out[dst + c] = img.data[src + c]
}
}
}
@types.Image::{
width: img.width,
height: img.height,
channels: ch,
data: Bytes::from_array(out),
}
}
///|
/// 顺时针旋转 270 度(逆时针 90 度)
pub fn rotate_270(img : @types.Image) -> @types.Image {
let ch = img.channels
let new_w = img.height
let new_h = img.width
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00')
for y in 0..<img.height {
for x in 0..<img.width {
let nx = y
let ny = img.width - 1 - x
let src = (y * img.width + x) * ch
let dst = (ny * new_w + nx) * ch
for c in 0..<ch {
out[dst + c] = img.data[src + c]
}
}
}
@types.Image::{
width: new_w,
height: new_h,
channels: ch,
data: Bytes::from_array(out),
}
}
///|
/// 水平翻转(左右镜像)
pub fn flip_horizontal(img : @types.Image) -> @types.Image {
let ch = img.channels
let out : Array[Byte] = Array::make(img.width * img.height * ch, b'\x00')
for y in 0..<img.height {
for x in 0..<img.width {
let nx = img.width - 1 - x
let src = (y * img.width + x) * ch
let dst = (y * img.width + nx) * ch
for c in 0..<ch {
out[dst + c] = img.data[src + c]
}
}
}
@types.Image::{
width: img.width,
height: img.height,
channels: ch,
data: Bytes::from_array(out),
}
}
///|
/// 垂直翻转(上下镜像)
pub fn flip_vertical(img : @types.Image) -> @types.Image {
let ch = img.channels
let w = img.width
let h = img.height
let out : Array[Byte] = Array::make(w * h * ch, b'\x00')
for y in 0..<h {
for x in 0..<w {
let src_off = ((h - 1 - y) * w + x) * ch
let dst_off = (y * w + x) * ch
for c in 0..<ch {
out[dst_off + c] = img.data[src_off + c]
}
}
}
@types.Image::{
width: w,
height: h,
channels: ch,
data: Bytes::from_array(out),
}
}
///|
/// 裁剪 16-bit Image16
pub fn crop_16(
img : @types.Image16,
x : Int,
y : Int,
w : Int,
h : Int,
) -> @types.Image16 raise @types.LoadError {
if x < 0 ||
y < 0 ||
w <= 0 ||
h <= 0 ||
x + w > img.width ||
y + h > img.height {
raise @types.LoadError::DecodeFailed("crop 区域越界")
}
let ch = img.channels
let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
for row in 0..<h {
let src_off = ((y + row) * img.width + x) * ch * 2
let dst_off = row * w * ch * 2
for i in 0..<(w * ch * 2) {
out[dst_off + i] = img.data[src_off + i]
}
}
@types.Image16::{
width: w,
height: h,
channels: ch,
data: Bytes::from_array(out),
}
}
///|
/// 裁剪 float ImageF
pub fn crop_f(
img : @types.ImageF,
x : Int,
y : Int,
w : Int,
h : Int,
) -> @types.ImageF raise @types.LoadError {
if x < 0 ||
y < 0 ||
w <= 0 ||
h <= 0 ||
x + w > img.width ||
y + h > img.height {
raise @types.LoadError::DecodeFailed("crop 区域越界")
}
let ch = img.channels
let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
for row in 0..<h {
let src_off = ((y + row) * img.width + x) * ch * 4
let dst_off = row * w * ch * 4
for i in 0..<(w * ch * 4) {
out[dst_off + i] = img.data[src_off + i]
}
}
@types.ImageF::{
width: w,
height: h,
channels: ch,
data: Bytes::from_array(out),
}
}