image.process\transform\geometry.mbt

97.1% · 66 / 68 points · 200 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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