image.process\transform\resize_cover.mbt
95% · 38 / 40 points · 96 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 缩放覆盖 resize cover - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 高级 Resize 操作(纯 MoonBit):resize_to_cover / resize_to_contain - 7
// 含裁剪和填充逻辑,属于高级算法层 - 8
- 9
///| - 10
/// Resize 并裁剪到精确尺寸(cover 模式)。 - 11
/// 先按宽高比缩放使图像"覆盖"目标尺寸,然后居中裁剪多余部分。 - 12
pub fn resize_to_cover( - 13
img : @types.Image, - 14
target_w : Int32, - 15
target_h : Int32, - 16
) -> @types.Image raise @types.LoadError { - 17●
if target_w <= 0 || target_h <= 0 { - 18●
raise @types.LoadError::DecodeFailed("目标尺寸必须为正数") - 19
} - 20●
let scale_w = Float::from_int(target_w) / Float::from_int(img.width) - 21●
let scale_h = Float::from_int(target_h) / Float::from_int(img.height) - 22◐
let scale = if scale_w > scale_h { scale_w } else { scale_h } - 23●
let scaled_w = (Float::from_int(img.width) * scale).to_int() - 24●
let scaled_h = (Float::from_int(img.height) * scale).to_int() - 25●
let scaled = @pure_util.resize_pure(img, scaled_w, scaled_h) - 26
let x = (scaled_w - target_w) / 2 - 27
let y = (scaled_h - target_h) / 2 - 28●
crop(scaled, x, y, target_w, target_h) - 29
} - 30
- 31
///| - 32
/// Resize 到目标尺寸内(contain 模式),保持宽高比,不足部分用 fill_color 填充。 - 33
pub fn resize_to_contain( - 34
img : @types.Image, - 35
target_w : Int32, - 36
target_h : Int32, - 37
fill_color : Array[Byte], - 38
) -> @types.Image raise @types.LoadError { - 39●
if target_w <= 0 || target_h <= 0 { - 40●
raise @types.LoadError::DecodeFailed("目标尺寸必须为正数") - 41
} - 42●
let scale_w = Float::from_int(target_w) / Float::from_int(img.width) - 43●
let scale_h = Float::from_int(target_h) / Float::from_int(img.height) - 44●
let scale = if scale_w < scale_h { scale_w } else { scale_h } - 45●
let scaled_w = (Float::from_int(img.width) * scale).to_int() - 46●
let scaled_h = (Float::from_int(img.height) * scale).to_int() - 47●
let scaled = @pure_util.resize_pure(img, scaled_w, scaled_h) - 48
let px = (target_w - scaled_w) / 2 - 49
let py = (target_h - scaled_h) / 2 - 50●
pad_image(scaled, px, py, fill_color) - 51
} - 52
- 53
///| - 54
/// 内部 pad 辅助函数:在图像四周填充指定颜色,生成新尺寸 (width+2*px, height+2*py) 的新图像。 - 55
fn pad_image( - 56
img : @types.Image, - 57
px : Int32, - 58
py : Int32, - 59
pad_color : Array[Byte], - 60
) -> @types.Image raise @types.LoadError { - 61●
if px < 0 || py < 0 { - 62○
raise @types.LoadError::DecodeFailed("pad 填充必须非负") - 63
} - 64
let ch = img.channels - 65
let new_w = img.width + 2 * px - 66
let new_h = img.height + 2 * py - 67●
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00') - 68●
let color_len = pad_color.length() - 69
if color_len == 0 { - 70●
raise @types.LoadError::DecodeFailed("pad_color 不能为空") - 71
} - 72
// 填充 - 73
for y in 0..<new_h { - 74●
for x in 0..<new_w { - 75●
let dst_off = (y * new_w + x) * ch - 76
if x >= px && x < px + img.width && y >= py && y < py + img.height { - 77
// 拷贝原始像素 - 78●
let src_off = ((y - py) * img.width + (x - px)) * ch - 79
for c in 0..<ch { - 80●
out[dst_off + c] = img.data[src_off + c] - 81
} - 82
} else { - 83
// 填充颜色 - 84●
for c in 0..<ch { - 85●
out[dst_off + c] = pad_color[c % color_len] - 86
} - 87
} - 88
} - 89
} - 90
@types.Image::{ - 91
width: new_w, - 92
height: new_h, - 93
channels: ch, - 94●
data: Bytes::from_array(out), - 95
} - 96
}