///|
/// # 变换 transform
///
/// 图像处理算法模块
/// 图像处理算法模块 Image processing algorithm module
// 图像变换操作(纯 MoonBit):crop / rotate / flip
// 支持 Image / Image16 / ImageF 三种类型
///|
/// 裁剪 8-bit Image 的指定区域
pub fn crop(
img : @types.Image,
x : Int32,
y : Int32,
w : Int32,
h : Int32,
) -> @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.. @types.Image {
img.region_with(region).to_image()
}
///|
/// 顺时针旋转 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.. @types.Image {
let ch = img.channels
let out : Array[Byte] = Array::make(img.width * img.height * ch, b'\x00')
for y in 0.. @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.. @types.Image {
let ch = img.channels
let out : Array[Byte] = Array::make(img.width * img.height * ch, b'\x00')
for y in 0.. @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.. @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.. @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..