image.process\transform\geometry_16f.mbt

100% · 132 / 132 points · 343 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/// # 高级几何变换 16-bit/float 变体 Advanced geometry transforms 16-bit/float variants
  3. 3///
  4. 4/// 16位透视/Seam Carving perspective_transform_16 / seam_carve_16
  5. 5/// 16位/浮点透视/Seam Carving 16-bit/float perspective/seam carving
  6. 6// 高级几何变换 16-bit/float 变体
  7. 7// warp_affine / warp_perspective / resize
  8. 8
  9. 9// ========== 辅助函数 ==========
  10. 10
  11. 11///|
  12. 12fn read_u16_le_geo(data : Bytes, offset : Int32) -> Int32 {
  13. 13● data[offset].to_int() + data[offset + 1].to_int() * 256
  14. 14}
  15. 15
  16. 16///|
  17. 17fn write_u16_le_geo(buf : Array[Byte], offset : Int32, val : Int32) -> Unit {
  18. 18● buf[offset] = (val & 0xFF).to_byte()
  19. 19● buf[offset + 1] = ((val >> 8) & 0xFF).to_byte()
  20. 20}
  21. 21
  22. 22///|
  23. 23fn read_float_le_geo(data : Bytes, offset : Int32) -> Float {
  24. 24● let b0 = data[offset].to_int()
  25. 25● let b1 = data[offset + 1].to_int()
  26. 26● let b2 = data[offset + 2].to_int()
  27. 27● let b3 = data[offset + 3].to_int()
  28. 28 let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
  29. 29● Float::reinterpret_from_int(bits)
  30. 30}
  31. 31
  32. 32///|
  33. 33fn write_f32_le_geo(buf : Array[Byte], offset : Int32, val : Float) -> Unit {
  34. 34● let bits = val.reinterpret_as_int()
  35. 35● buf[offset] = (bits & 0xFF).to_byte()
  36. 36● buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  37. 37● buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  38. 38● buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
  39. 39}
  40. 40
  41. 41///|
  42. 42fn sample_bilinear_16(
  43. 43 data : Array[Int32],
  44. 44 w : Int32,
  45. 45 h : Int32,
  46. 46 ch : Int32,
  47. 47 c : Int32,
  48. 48 x : Float,
  49. 49 y : Float,
  50. 50) -> Int32 {
  51. 51● let x0 = x.to_int()
  52. 52● let y0 = y.to_int()
  53. 53 let w_minus_1 = w - 1
  54. 54 let h_minus_1 = h - 1
  55. 55● let x0c = @types.clamp_int(x0, 0, w_minus_1)
  56. 56● let y0c = @types.clamp_int(y0, 0, h_minus_1)
  57. 57● let x1 = @types.clamp_int(x0 + 1, 0, w_minus_1)
  58. 58● let y1 = @types.clamp_int(y0 + 1, 0, h_minus_1)
  59. 59● let fx = x - Float::from_int(x0)
  60. 60● let fy = y - Float::from_int(y0)
  61. 61 let p00 = data[(y0c * w + x0c) * ch + c]
  62. 62 let p10 = data[(y0c * w + x1) * ch + c]
  63. 63 let p01 = data[(y1 * w + x0c) * ch + c]
  64. 64 let p11 = data[(y1 * w + x1) * ch + c]
  65. 65● let top = Float::from_int(p00) * (1.0F - fx) + Float::from_int(p10) * fx
  66. 66● let bot = Float::from_int(p01) * (1.0F - fx) + Float::from_int(p11) * fx
  67. 67● (top * (1.0F - fy) + bot * fy + 0.5F).to_int()
  68. 68}
  69. 69
  70. 70///|
  71. 71fn sample_bilinear_f(
  72. 72 data : Array[Float],
  73. 73 w : Int32,
  74. 74 h : Int32,
  75. 75 ch : Int32,
  76. 76 c : Int32,
  77. 77 x : Float,
  78. 78 y : Float,
  79. 79) -> Float {
  80. 80● let x0 = x.to_int()
  81. 81● let y0 = y.to_int()
  82. 82 let w_minus_1 = w - 1
  83. 83 let h_minus_1 = h - 1
  84. 84● let x0c = @types.clamp_int(x0, 0, w_minus_1)
  85. 85● let y0c = @types.clamp_int(y0, 0, h_minus_1)
  86. 86● let x1 = @types.clamp_int(x0 + 1, 0, w_minus_1)
  87. 87● let y1 = @types.clamp_int(y0 + 1, 0, h_minus_1)
  88. 88● let fx = x - Float::from_int(x0)
  89. 89● let fy = y - Float::from_int(y0)
  90. 90 let p00 = data[(y0c * w + x0c) * ch + c]
  91. 91 let p10 = data[(y0c * w + x1) * ch + c]
  92. 92 let p01 = data[(y1 * w + x0c) * ch + c]
  93. 93 let p11 = data[(y1 * w + x1) * ch + c]
  94. 94 let top = p00 * (1.0F - fx) + p10 * fx
  95. 95 let bot = p01 * (1.0F - fx) + p11 * fx
  96. 96 top * (1.0F - fy) + bot * fy
  97. 97}
  98. 98
  99. 99// ========== warp_affine 16-bit ==========
  100. 100
  101. 101///|
  102. 102/// 16-bit 仿射变换。
  103. 103/// @see warp_affine 8-bit 版本,本函数为 Image16 变体
  104. 104pub fn warp_affine_16(
  105. 105 img : @types.Image16,
  106. 106 matrix : (Float, Float, Float, Float, Float, Float),
  107. 107 new_w : Int32,
  108. 108 new_h : Int32,
  109. 109) -> @types.Image16 {
  110. 110● let (a, b, c, d, e, f) = matrix
  111. 111 let ch = img.channels
  112. 112 let w = img.width
  113. 113 let h = img.height
  114. 114● let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  115. 115 for i in 0..<(w * h * ch) {
  116. 116● data_int[i] = read_u16_le_geo(img.data, i * 2)
  117. 117 }
  118. 118● let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00')
  119. 119 for dy in 0..<new_h {
  120. 120● let fdy = Float::from_int(dy)
  121. 121 for dx in 0..<new_w {
  122. 122● let sx = a * Float::from_int(dx) + b * fdy + c
  123. 123● let sy = d * Float::from_int(dx) + e * fdy + f
  124. 124 for ci in 0..<ch {
  125. 125● let val = sample_bilinear_16(data_int, w, h, ch, ci, sx, sy)
  126. 126● write_u16_le_geo(
  127. 127 out,
  128. 128 (dy * new_w + dx) * ch * 2 + ci * 2,
  129. 129● @types.clamp_int(val, 0, 65535),
  130. 130 )
  131. 131 }
  132. 132 }
  133. 133 }
  134. 134 @types.Image16::{
  135. 135 width: new_w,
  136. 136 height: new_h,
  137. 137 channels: ch,
  138. 138● data: Bytes::from_array(out),
  139. 139 }
  140. 140}
  141. 141
  142. 142// ========== warp_affine float ==========
  143. 143
  144. 144///|
  145. 145/// float 仿射变换。
  146. 146/// @see warp_affine 8-bit 版本,本函数为 ImageF 变体
  147. 147pub fn warp_affine_f(
  148. 148 img : @types.ImageF,
  149. 149 matrix : (Float, Float, Float, Float, Float, Float),
  150. 150 new_w : Int32,
  151. 151 new_h : Int32,
  152. 152) -> @types.ImageF {
  153. 153● let (a, b, c, d, e, f) = matrix
  154. 154 let ch = img.channels
  155. 155 let w = img.width
  156. 156 let h = img.height
  157. 157● let data_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  158. 158 for i in 0..<(w * h * ch) {
  159. 159● data_f[i] = read_float_le_geo(img.data, i * 4)
  160. 160 }
  161. 161● let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00')
  162. 162 for dy in 0..<new_h {
  163. 163● let fdy = Float::from_int(dy)
  164. 164 for dx in 0..<new_w {
  165. 165● let sx = a * Float::from_int(dx) + b * fdy + c
  166. 166● let sy = d * Float::from_int(dx) + e * fdy + f
  167. 167 for ci in 0..<ch {
  168. 168● let val = sample_bilinear_f(data_f, w, h, ch, ci, sx, sy)
  169. 169● write_f32_le_geo(out, (dy * new_w + dx) * ch * 4 + ci * 4, val)
  170. 170 }
  171. 171 }
  172. 172 }
  173. 173 @types.ImageF::{
  174. 174 width: new_w,
  175. 175 height: new_h,
  176. 176 channels: ch,
  177. 177● data: Bytes::from_array(out),
  178. 178 }
  179. 179}
  180. 180
  181. 181// ========== warp_perspective 16-bit ==========
  182. 182
  183. 183///|
  184. 184/// 16-bit 透视变换。
  185. 185/// @see warp_perspective 8-bit 版本,本函数为 Image16 变体
  186. 186pub fn warp_perspective_16(
  187. 187 img : @types.Image16,
  188. 188 matrix : PerspectiveMatrix,
  189. 189 new_w : Int32,
  190. 190 new_h : Int32,
  191. 191) -> @types.Image16 {
  192. 192● let ch = img.channels
  193. 193 let w = img.width
  194. 194 let h = img.height
  195. 195● let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  196. 196 for i in 0..<(w * h * ch) {
  197. 197● data_int[i] = read_u16_le_geo(img.data, i * 2)
  198. 198 }
  199. 199● let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00')
  200. 200 for dy in 0..<new_h {
  201. 201● let fdy = Float::from_int(dy)
  202. 202 for dx in 0..<new_w {
  203. 203● let fdx = Float::from_int(dx)
  204. 204 let denom = matrix.m20 * fdx + matrix.m21 * fdy + matrix.m22
  205. 205 let sx = (matrix.m00 * fdx + matrix.m01 * fdy + matrix.m02) / denom
  206. 206 let sy = (matrix.m10 * fdx + matrix.m11 * fdy + matrix.m12) / denom
  207. 207 for ci in 0..<ch {
  208. 208● let val = sample_bilinear_16(data_int, w, h, ch, ci, sx, sy)
  209. 209● write_u16_le_geo(
  210. 210 out,
  211. 211 (dy * new_w + dx) * ch * 2 + ci * 2,
  212. 212● @types.clamp_int(val, 0, 65535),
  213. 213 )
  214. 214 }
  215. 215 }
  216. 216 }
  217. 217 @types.Image16::{
  218. 218 width: new_w,
  219. 219 height: new_h,
  220. 220 channels: ch,
  221. 221● data: Bytes::from_array(out),
  222. 222 }
  223. 223}
  224. 224
  225. 225// ========== warp_perspective float ==========
  226. 226
  227. 227///|
  228. 228/// float 透视变换。
  229. 229/// @see warp_perspective 8-bit 版本,本函数为 ImageF 变体
  230. 230pub fn warp_perspective_f(
  231. 231 img : @types.ImageF,
  232. 232 matrix : PerspectiveMatrix,
  233. 233 new_w : Int32,
  234. 234 new_h : Int32,
  235. 235) -> @types.ImageF {
  236. 236● let ch = img.channels
  237. 237 let w = img.width
  238. 238 let h = img.height
  239. 239● let data_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  240. 240 for i in 0..<(w * h * ch) {
  241. 241● data_f[i] = read_float_le_geo(img.data, i * 4)
  242. 242 }
  243. 243● let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00')
  244. 244 for dy in 0..<new_h {
  245. 245● let fdy = Float::from_int(dy)
  246. 246 for dx in 0..<new_w {
  247. 247● let fdx = Float::from_int(dx)
  248. 248 let denom = matrix.m20 * fdx + matrix.m21 * fdy + matrix.m22
  249. 249 let sx = (matrix.m00 * fdx + matrix.m01 * fdy + matrix.m02) / denom
  250. 250 let sy = (matrix.m10 * fdx + matrix.m11 * fdy + matrix.m12) / denom
  251. 251 for ci in 0..<ch {
  252. 252● let val = sample_bilinear_f(data_f, w, h, ch, ci, sx, sy)
  253. 253● write_f32_le_geo(out, (dy * new_w + dx) * ch * 4 + ci * 4, val)
  254. 254 }
  255. 255 }
  256. 256 }
  257. 257 @types.ImageF::{
  258. 258 width: new_w,
  259. 259 height: new_h,
  260. 260 channels: ch,
  261. 261● data: Bytes::from_array(out),
  262. 262 }
  263. 263}
  264. 264
  265. 265// ========== resize 16-bit ==========
  266. 266
  267. 267///|
  268. 268/// 16-bit 图像缩放。
  269. 269/// @see resize 8-bit 版本,本函数为 Image16 变体
  270. 270pub fn resize_16(
  271. 271 img : @types.Image16,
  272. 272 new_w : Int32,
  273. 273 new_h : Int32,
  274. 274) -> @types.Image16 {
  275. 275● let ch = img.channels
  276. 276 let w = img.width
  277. 277 let h = img.height
  278. 278● let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  279. 279 for i in 0..<(w * h * ch) {
  280. 280● data_int[i] = read_u16_le_geo(img.data, i * 2)
  281. 281 }
  282. 282● let scale_x = Float::from_int(w) / Float::from_int(new_w)
  283. 283● let scale_y = Float::from_int(h) / Float::from_int(new_h)
  284. 284● let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00')
  285. 285 for dy in 0..<new_h {
  286. 286● for dx in 0..<new_w {
  287. 287● let sx = (Float::from_int(dx) + 0.5F) * scale_x - 0.5F
  288. 288● let sy = (Float::from_int(dy) + 0.5F) * scale_y - 0.5F
  289. 289 for ci in 0..<ch {
  290. 290● let val = sample_bilinear_16(data_int, w, h, ch, ci, sx, sy)
  291. 291● write_u16_le_geo(
  292. 292 out,
  293. 293 (dy * new_w + dx) * ch * 2 + ci * 2,
  294. 294● @types.clamp_int(val, 0, 65535),
  295. 295 )
  296. 296 }
  297. 297 }
  298. 298 }
  299. 299 @types.Image16::{
  300. 300 width: new_w,
  301. 301 height: new_h,
  302. 302 channels: ch,
  303. 303● data: Bytes::from_array(out),
  304. 304 }
  305. 305}
  306. 306
  307. 307// ========== resize float ==========
  308. 308
  309. 309///|
  310. 310/// float 图像缩放。
  311. 311/// @see resize 8-bit 版本,本函数为 ImageF 变体
  312. 312pub fn resize_f(
  313. 313 img : @types.ImageF,
  314. 314 new_w : Int32,
  315. 315 new_h : Int32,
  316. 316) -> @types.ImageF {
  317. 317● let ch = img.channels
  318. 318 let w = img.width
  319. 319 let h = img.height
  320. 320● let data_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  321. 321 for i in 0..<(w * h * ch) {
  322. 322● data_f[i] = read_float_le_geo(img.data, i * 4)
  323. 323 }
  324. 324● let scale_x = Float::from_int(w) / Float::from_int(new_w)
  325. 325● let scale_y = Float::from_int(h) / Float::from_int(new_h)
  326. 326● let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00')
  327. 327 for dy in 0..<new_h {
  328. 328● for dx in 0..<new_w {
  329. 329● let sx = (Float::from_int(dx) + 0.5F) * scale_x - 0.5F
  330. 330● let sy = (Float::from_int(dy) + 0.5F) * scale_y - 0.5F
  331. 331 for ci in 0..<ch {
  332. 332● let val = sample_bilinear_f(data_f, w, h, ch, ci, sx, sy)
  333. 333● write_f32_le_geo(out, (dy * new_w + dx) * ch * 4 + ci * 4, val)
  334. 334 }
  335. 335 }
  336. 336 }
  337. 337 @types.ImageF::{
  338. 338 width: new_w,
  339. 339 height: new_h,
  340. 340 channels: ch,
  341. 341● data: Bytes::from_array(out),
  342. 342 }
  343. 343}