image.process\transform\geometry.mbt
97.1% · 66 / 68 points · 200 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 几何变换 geometry - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 几何变换(纯 MoonBit):仿射变换 + 任意角度旋转 - 7
// 使用双线性插值,边界 clamp 到边缘像素 - 8
- 9
///| - 10
/// 双线性插值采样。在 (x, y) 处采样通道 c 的值。 - 11
fn sample_bilinear( - 12
data : Array[Int32], - 13
w : Int32, - 14
h : Int32, - 15
ch : Int32, - 16
c : Int32, - 17
x : Float, - 18
y : Float, - 19
) -> Int32 { - 20●
let x0 = x.to_int() - 21●
let y0 = y.to_int() - 22
let w_minus_1 = w - 1 - 23
let h_minus_1 = h - 1 - 24●
let x0c = x0.clamp(min=0, max=w_minus_1) - 25●
let y0c = y0.clamp(min=0, max=h_minus_1) - 26●
let x1 = (x0 + 1).clamp(min=0, max=w_minus_1) - 27●
let y1 = (y0 + 1).clamp(min=0, max=h_minus_1) - 28●
let fx = x - Float::from_int(x0) - 29●
let fy = y - Float::from_int(y0) - 30
let p00 = data[(y0c * w + x0c) * ch + c] - 31
let p10 = data[(y0c * w + x1) * ch + c] - 32
let p01 = data[(y1 * w + x0c) * ch + c] - 33
let p11 = data[(y1 * w + x1) * ch + c] - 34●
let top = Float::from_int(p00) * (1.0F - fx) + Float::from_int(p10) * fx - 35●
let bot = Float::from_int(p01) * (1.0F - fx) + Float::from_int(p11) * fx - 36●
(top * (1.0F - fy) + bot * fy + 0.5F).to_int() - 37
} - 38
- 39
///| - 40
/// 仿射变换。matrix = (a, b, c, d, e, f) 映射 dst→src: - 41
/// 仿射逆映射 src_x = a * dst_x + b * dst_y + c - 42
/// 仿射逆映射 src_y = d * dst_x + e * dst_y + f - 43
/// 使用双线性插值,超出边界的像素 clamp 到边缘。 - 44
/// 输出尺寸为 new_w × new_h,通道数与输入相同。 - 45
pub fn warp_affine( - 46
img : @types.Image, - 47
matrix : (Float, Float, Float, Float, Float, Float), - 48
new_w : Int32, - 49
new_h : Int32, - 50
) -> @types.Image { - 51●
let (a, b, c, d, e, f) = matrix - 52
let ch = img.channels - 53
let w = img.width - 54
let h = img.height - 55●
let data_int : Array[Int32] = Array::make(w * h * ch, 0) - 56
for i in 0..<(w * h * ch) { - 57●
data_int[i] = img.data[i].to_int() - 58
} - 59●
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00') - 60
for dy in 0..<new_h { - 61●
let fdy = Float::from_int(dy) - 62
for dx in 0..<new_w { - 63●
let sx = a * Float::from_int(dx) + b * fdy + c - 64●
let sy = d * Float::from_int(dx) + e * fdy + f - 65
for ci in 0..<ch { - 66●
let val = sample_bilinear(data_int, w, h, ch, ci, sx, sy) - 67●
out[(dy * new_w + dx) * ch + ci] = val.clamp(min=0, max=255).to_byte() - 68
} - 69
} - 70
} - 71
@types.Image::{ - 72
width: new_w, - 73
height: new_h, - 74
channels: ch, - 75●
data: Bytes::from_array(out), - 76
} - 77
} - 78
- 79
///| - 80
/// 任意角度旋转(绕中心,保留原尺寸,角落填充黑色)。 - 81
/// angle 单位为度,正值逆时针旋转。使用双线性插值。 - 82
pub fn rotate(img : @types.Image, angle_degrees : Float) -> @types.Image { - 83●
let w = img.width - 84
let h = img.height - 85
let ch = img.channels - 86
let rad = angle_degrees * 3.14159265F / 180.0F - 87●
let cos_a = @math.cosf(rad) - 88●
let sin_a = @math.sinf(rad) - 89●
let cx = Float::from_int(w - 1) / 2.0F - 90●
let cy = Float::from_int(h - 1) / 2.0F - 91
// 逆变换:dst → src(旋转 -angle) - 92
// src_x = cos*(dx-cx) + sin*(dy-cy) + cx - 93
// src_y = -sin*(dx-cx) + cos*(dy-cy) + cy - 94
// 即 matrix = (cos, sin, cx-cos*cx-sin*cy, -sin, cos, cy+sin*cx-cos*cy) - 95
let tx = cx - cos_a * cx - sin_a * cy - 96
let ty = cy + sin_a * cx - cos_a * cy - 97●
let data_int : Array[Int32] = Array::make(w * h * ch, 0) - 98
for i in 0..<(w * h * ch) { - 99●
data_int[i] = img.data[i].to_int() - 100
} - 101●
let w_max = Float::from_int(w) - 0.5F - 102●
let h_max = Float::from_int(h) - 0.5F - 103●
let w_minus_1_f = Float::from_int(w - 1) - 104●
let h_minus_1_f = Float::from_int(h - 1) - 105●
let out : Array[Byte] = Array::make(w * h * ch, b'\x00') - 106
for dy in 0..<h { - 107●
let fdy = Float::from_int(dy) - 108
for dx in 0..<w { - 109●
let fdx = Float::from_int(dx) - 110
let sx = cos_a * fdx + sin_a * fdy + tx - 111
let sy = -sin_a * fdx + cos_a * fdy + ty - 112
// 超出边界的像素填黑(用 0.5 容差避免浮点误差) - 113
if sx < -0.5F || sx > w_max || sy < -0.5F || sy > h_max { - 114
// 已经是 0 - 115●
continue - 116
} - 117●
let sx_c = sx.clamp(min=0.0F, max=w_minus_1_f) - 118●
let sy_c = sy.clamp(min=0.0F, max=h_minus_1_f) - 119
for ci in 0..<ch { - 120●
let val = sample_bilinear(data_int, w, h, ch, ci, sx_c, sy_c) - 121●
out[(dy * w + dx) * ch + ci] = val.clamp(min=0, max=255).to_byte() - 122
} - 123
} - 124
} - 125
@types.Image::{ - 126
width: w, - 127
height: h, - 128
channels: ch, - 129●
data: Bytes::from_array(out), - 130
} - 131
} - 132
- 133
///| - 134
/// 2×3 仿射变换矩阵 (a, b, c, d, e, f) - 135
/// 正向仿射变换 x' = a*x + b*y + c - 136
/// 正向仿射变换 y' = d*x + e*y + f - 137
pub(all) struct AffineMatrix { - 138
a : Float - 139
b : Float - 140
c : Float - 141
d : Float - 142
e : Float - 143
f : Float - 144
} - 145
- 146
///| - 147
/// 从 3 对源点→目标点计算 2×3 仿射变换矩阵。 - 148
pub fn get_affine_transform( - 149
src : Array[(Float, Float)], - 150
dst : Array[(Float, Float)], - 151
) -> AffineMatrix? { - 152●
if src.length() != 3 || dst.length() != 3 { - 153○
return None - 154
} - 155
// 6×6 线性方程组 - 156●
let a : Array[Array[Float]] = Array::make(6, []) - 157●
let b : Array[Float] = Array::make(6, 0.0F) - 158
for i in 0..<3 { - 159●
let (sx, sy) = src[i] - 160
let (dx, dy) = dst[i] - 161
a[i * 2] = [sx, sy, 1.0F, 0.0F, 0.0F, 0.0F] - 162
b[i * 2] = dx - 163
a[i * 2 + 1] = [0.0F, 0.0F, 0.0F, sx, sy, 1.0F] - 164
b[i * 2 + 1] = dy - 165
} - 166●
let sol = solve_linear_system(a, b, 6) - 167●
match sol { - 168○
None => None - 169
Some(v) => - 170●
Some(AffineMatrix::{ - 171
a: v[0], - 172
b: v[1], - 173
c: v[2], - 174
d: v[3], - 175
e: v[4], - 176
f: v[5], - 177
}) - 178
} - 179
} - 180
- 181
///| - 182
/// 从中心点、角度和缩放计算 2×3 旋转矩阵。 - 183
/// angle 为弧度,scale 为缩放因子。 - 184
pub fn get_rotation_matrix_2d( - 185
center : (Float, Float), - 186
angle : Float, - 187
scale : Float, - 188
) -> AffineMatrix { - 189●
let (cx, cy) = center - 190●
let alpha = scale * @math.cosf(angle) - 191●
let beta = scale * @math.sinf(angle) - 192
AffineMatrix::{ - 193
a: alpha, - 194
b: beta, - 195
c: (1.0F - alpha) * cx - beta * cy, - 196
d: -beta, - 197
e: alpha, - 198
f: beta * cx + (1.0F - alpha) * cy, - 199
} - 200
}