image.codec\util\resize.mbt
100% · 58 / 58 points · 115 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/resize - 3
/// - 4
/// 编解码工具模块 - 5
/// 编解码工具模块 Codec utility module - 6
// 纯 MoonBit 图像缩放(全目标可用:native/wasm/js) - 7
// 仅依赖 @types,不依赖 @core/FFI - 8
// 支持最近邻 + 双线性插值 - 9
// v190.0.0 性能优化:预计算坐标权重 + 消除 data_int 拷贝 - 10
- 11
///| - 12
/// resize 8-bit 图像(纯 MoonBit,全目标可用)。 - 13
/// 使用双线性插值(filter = PointSample 时使用最近邻)。 - 14
/// edge 参数目前统一按 Clamp 处理(边缘像素重复)。 - 15
/// v190.0.0 优化:预计算坐标权重数组,消除 data_int 转换, - 16
/// 直接在 Bytes 上操作,减少 O(n) 内存拷贝和重复浮点运算。 - 17
pub fn resize_pure( - 18
img : @types.Image, - 19
out_w : Int32, - 20
out_h : Int32, - 21
filter~ : @types.ResizeFilter = @types.ResizeFilter::Default, - 22
edge~ : @types.ResizeEdge = @types.ResizeEdge::Clamp, - 23
) -> @types.Image raise @types.LoadError { - 24●
let _ = edge - 25
if out_w <= 0 || out_h <= 0 { - 26●
raise @types.LoadError::DecodeFailed("resize 输出尺寸必须为正数") - 27
} - 28
let ch = img.channels - 29
let w = img.width - 30
let h = img.height - 31●
let scale_x = Float::from_int(w) / Float::from_int(out_w) - 32●
let scale_y = Float::from_int(h) / Float::from_int(out_h) - 33●
let use_nearest = match filter { - 34●
PointSample => true - 35●
_ => false - 36
} - 37
let w_minus_1 = w - 1 - 38
let h_minus_1 = h - 1 - 39●
let out : Array[Byte] = Array::make(out_w * out_h * ch, b'\x00') - 40
if use_nearest { - 41●
let sx_xi : Array[Int32] = Array::make(out_w, 0) - 42
for dx in 0..<out_w { - 43●
let sx = (Float::from_int(dx) + 0.5F) * scale_x - 0.5F - 44●
sx_xi[dx] = (sx + 0.5F).to_int().clamp(min=0, max=w_minus_1) - 45
} - 46●
let sy_yi : Array[Int32] = Array::make(out_h, 0) - 47
for dy in 0..<out_h { - 48●
let sy = (Float::from_int(dy) + 0.5F) * scale_y - 0.5F - 49●
sy_yi[dy] = (sy + 0.5F).to_int().clamp(min=0, max=h_minus_1) - 50
} - 51
for dy in 0..<out_h { - 52●
let yi = sy_yi[dy] - 53
for dx in 0..<out_w { - 54●
let xi = sx_xi[dx] - 55
let src_idx = (yi * w + xi) * ch - 56
let dst_idx = (dy * out_w + dx) * ch - 57
for ci in 0..<ch { - 58●
out[dst_idx + ci] = img.data[src_idx + ci] - 59
} - 60
} - 61
} - 62
} else { - 63●
let sx_x0c : Array[Int32] = Array::make(out_w, 0) - 64●
let sx_x1 : Array[Int32] = Array::make(out_w, 0) - 65●
let sx_fx : Array[Float] = Array::make(out_w, 0.0F) - 66
for dx in 0..<out_w { - 67●
let sx = (Float::from_int(dx) + 0.5F) * scale_x - 0.5F - 68●
let x0 = sx.to_int() - 69●
sx_x0c[dx] = x0.clamp(min=0, max=w_minus_1) - 70●
sx_x1[dx] = (x0 + 1).clamp(min=0, max=w_minus_1) - 71●
sx_fx[dx] = sx - Float::from_int(x0) - 72
} - 73●
let sy_y0c : Array[Int32] = Array::make(out_h, 0) - 74●
let sy_y1 : Array[Int32] = Array::make(out_h, 0) - 75●
let sy_fy : Array[Float] = Array::make(out_h, 0.0F) - 76
for dy in 0..<out_h { - 77●
let sy = (Float::from_int(dy) + 0.5F) * scale_y - 0.5F - 78●
let y0 = sy.to_int() - 79●
sy_y0c[dy] = y0.clamp(min=0, max=h_minus_1) - 80●
sy_y1[dy] = (y0 + 1).clamp(min=0, max=h_minus_1) - 81●
sy_fy[dy] = sy - Float::from_int(y0) - 82
} - 83
for dy in 0..<out_h { - 84●
let y0c = sy_y0c[dy] - 85
let y1 = sy_y1[dy] - 86
let fy = sy_fy[dy] - 87
let one_minus_fy = 1.0F - fy - 88
for dx in 0..<out_w { - 89●
let x0c = sx_x0c[dx] - 90
let x1 = sx_x1[dx] - 91
let fx = sx_fx[dx] - 92
let one_minus_fx = 1.0F - fx - 93
let dst_idx = (dy * out_w + dx) * ch - 94
for ci in 0..<ch { - 95●
let p00 = img.data[(y0c * w + x0c) * ch + ci].to_int() - 96●
let p10 = img.data[(y0c * w + x1) * ch + ci].to_int() - 97●
let p01 = img.data[(y1 * w + x0c) * ch + ci].to_int() - 98●
let p11 = img.data[(y1 * w + x1) * ch + ci].to_int() - 99●
let top = Float::from_int(p00) * one_minus_fx + - 100●
Float::from_int(p10) * fx - 101●
let bot = Float::from_int(p01) * one_minus_fx + - 102●
Float::from_int(p11) * fx - 103●
let val = (top * one_minus_fy + bot * fy + 0.5F).to_int() - 104●
out[dst_idx + ci] = val.clamp(min=0, max=255).to_byte() - 105
} - 106
} - 107
} - 108
} - 109
@types.Image::{ - 110
width: out_w, - 111
height: out_h, - 112
channels: ch, - 113●
data: Bytes::from_array(out), - 114
} - 115
}