image.process\transform\pyramid_16f.mbt

96.6% · 172 / 178 points · 456 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 变体 Image pyramid 16-bit/float variants
  3. 3///
  4. 4/// 16位高斯/拉普拉斯金字塔 gaussian_pyramid_16 / laplacian_pyramid_16
  5. 5/// 16位/浮点高斯/拉普拉斯金字塔 16-bit/float Gaussian/Laplacian pyramid
  6. 6// 图像金字塔 16-bit/float 变体
  7. 7// pyr_down / pyr_up
  8. 8
  9. 9///|
  10. 10fn read_u16_p(data : Bytes, offset : Int32) -> Int32 {
  11. 11● data[offset].to_int() | (data[offset + 1].to_int() << 8)
  12. 12}
  13. 13
  14. 14///|
  15. 15fn write_u16_p(data : Array[Byte], offset : Int32, v : Int32) -> Unit {
  16. 16● data[offset] = (v & 0xFF).to_byte()
  17. 17● data[offset + 1] = ((v >> 8) & 0xFF).to_byte()
  18. 18}
  19. 19
  20. 20///|
  21. 21fn read_f32_p(data : Bytes, offset : Int32) -> Float {
  22. 22● let b0 = data[offset].to_int()
  23. 23● let b1 = data[offset + 1].to_int()
  24. 24● let b2 = data[offset + 2].to_int()
  25. 25● let b3 = data[offset + 3].to_int()
  26. 26 let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
  27. 27● Float::reinterpret_from_int(bits)
  28. 28}
  29. 29
  30. 30///|
  31. 31fn write_f32_p(data : Array[Byte], offset : Int32, v : Float) -> Unit {
  32. 32● let bits = v.reinterpret_as_int()
  33. 33● data[offset] = (bits & 0xFF).to_byte()
  34. 34● data[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  35. 35● data[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  36. 36● data[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
  37. 37}
  38. 38
  39. 39// ========== pyr_down 16-bit ==========
  40. 40
  41. 41///|
  42. 42/// 高斯金字塔下采样 16-bit 版本。
  43. 43/// @see pyr_down 8-bit 版本,本函数为 Image16 变体
  44. 44pub fn pyr_down_16(
  45. 45 img : @types.Image16,
  46. 46) -> @types.Image16 raise @types.LoadError {
  47. 47● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  48. 48● raise @types.LoadError::DecodeFailed("pyr_down_16 仅支持 channels=1/3/4")
  49. 49 }
  50. 50 let ch = img.channels
  51. 51 let w = img.width
  52. 52 let h = img.height
  53. 53 let new_w = w / 2
  54. 54 let new_h = h / 2
  55. 55 if new_w == 0 || new_h == 0 {
  56. 56● raise @types.LoadError::DecodeFailed("图像太小,无法下采样")
  57. 57 }
  58. 58● let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  59. 59 for i in 0..<(w * h * ch) {
  60. 60● data_int[i] = read_u16_p(img.data, i * 2)
  61. 61 }
  62. 62● let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00')
  63. 63 for y in 0..<new_h {
  64. 64● for x in 0..<new_w {
  65. 65● for c in 0..<ch {
  66. 66● let mut sum = 0
  67. 67 let mut count = 0
  68. 68 for dy in 0..<2 {
  69. 69● for dx in 0..<2 {
  70. 70● let sx = x * 2 + dx
  71. 71 let sy = y * 2 + dy
  72. 72 if sx < w && sy < h {
  73. 73● sum += data_int[(sy * w + sx) * ch + c]
  74. 74 count += 1
  75. 75 }
  76. 76 }
  77. 77 }
  78. 78● write_u16_p(out, ((y * new_w + x) * ch + c) * 2, sum / count)
  79. 79 }
  80. 80 }
  81. 81 }
  82. 82 @types.Image16::{
  83. 83 width: new_w,
  84. 84 height: new_h,
  85. 85 channels: ch,
  86. 86● data: Bytes::from_array(out),
  87. 87 }
  88. 88}
  89. 89
  90. 90// ========== pyr_down float ==========
  91. 91
  92. 92///|
  93. 93/// 高斯金字塔下采样 float 版本。
  94. 94/// @see pyr_down 8-bit 版本,本函数为 ImageF 变体
  95. 95pub fn pyr_down_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError {
  96. 96● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  97. 97● raise @types.LoadError::DecodeFailed("pyr_down_f 仅支持 channels=1/3/4")
  98. 98 }
  99. 99 let ch = img.channels
  100. 100 let w = img.width
  101. 101 let h = img.height
  102. 102 let new_w = w / 2
  103. 103 let new_h = h / 2
  104. 104 if new_w == 0 || new_h == 0 {
  105. 105● raise @types.LoadError::DecodeFailed("图像太小,无法下采样")
  106. 106 }
  107. 107● let data_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  108. 108 for i in 0..<(w * h * ch) {
  109. 109● data_f[i] = read_f32_p(img.data, i * 4)
  110. 110 }
  111. 111● let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00')
  112. 112 for y in 0..<new_h {
  113. 113● for x in 0..<new_w {
  114. 114● for c in 0..<ch {
  115. 115● let mut sum = 0.0F
  116. 116 let mut count = 0
  117. 117 for dy in 0..<2 {
  118. 118● for dx in 0..<2 {
  119. 119● let sx = x * 2 + dx
  120. 120 let sy = y * 2 + dy
  121. 121 if sx < w && sy < h {
  122. 122● sum += data_f[(sy * w + sx) * ch + c]
  123. 123 count += 1
  124. 124 }
  125. 125 }
  126. 126 }
  127. 127● write_f32_p(
  128. 128 out,
  129. 129 ((y * new_w + x) * ch + c) * 4,
  130. 130● sum / Float::from_int(count),
  131. 131 )
  132. 132 }
  133. 133 }
  134. 134 }
  135. 135 @types.ImageF::{
  136. 136 width: new_w,
  137. 137 height: new_h,
  138. 138 channels: ch,
  139. 139● data: Bytes::from_array(out),
  140. 140 }
  141. 141}
  142. 142
  143. 143// ========== pyr_up 16-bit ==========
  144. 144
  145. 145///|
  146. 146/// 拉普拉斯金字塔上采样 16-bit 版本。
  147. 147/// @see pyr_up 8-bit 版本,本函数为 Image16 变体
  148. 148pub fn pyr_up_16(img : @types.Image16) -> @types.Image16 raise @types.LoadError {
  149. 149● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  150. 150● raise @types.LoadError::DecodeFailed("pyr_up_16 仅支持 channels=1/3/4")
  151. 151 }
  152. 152 let ch = img.channels
  153. 153 let w = img.width
  154. 154 let h = img.height
  155. 155 let new_w = w * 2
  156. 156 let new_h = h * 2
  157. 157● let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  158. 158 for i in 0..<(w * h * ch) {
  159. 159● data_int[i] = read_u16_p(img.data, i * 2)
  160. 160 }
  161. 161● let x0_arr : Array[Int32] = Array::make(new_w, 0)
  162. 162● let x1_arr : Array[Int32] = Array::make(new_w, 0)
  163. 163● let wx_arr : Array[Float] = Array::make(new_w, 0.0F)
  164. 164 for x in 0..<new_w {
  165. 165● let fx = (Float::from_int(x) - 0.5F) / 2.0F
  166. 166● let x0 = @types.clamp_int(fx.to_int(), 0, w - 1)
  167. 167 x0_arr[x] = x0
  168. 168● x1_arr[x] = @types.clamp_int(x0 + 1, 0, w - 1)
  169. 169● wx_arr[x] = fx - Float::from_int(x0)
  170. 170 }
  171. 171● let y0_arr : Array[Int32] = Array::make(new_h, 0)
  172. 172● let y1_arr : Array[Int32] = Array::make(new_h, 0)
  173. 173● let wy_arr : Array[Float] = Array::make(new_h, 0.0F)
  174. 174 for y in 0..<new_h {
  175. 175● let fy = (Float::from_int(y) - 0.5F) / 2.0F
  176. 176● let y0 = @types.clamp_int(fy.to_int(), 0, h - 1)
  177. 177 y0_arr[y] = y0
  178. 178● y1_arr[y] = @types.clamp_int(y0 + 1, 0, h - 1)
  179. 179● wy_arr[y] = fy - Float::from_int(y0)
  180. 180 }
  181. 181● let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00')
  182. 182 for y in 0..<new_h {
  183. 183● let y0 = y0_arr[y]
  184. 184 let y1 = y1_arr[y]
  185. 185 let wy = wy_arr[y]
  186. 186 let wy_inv = 1.0F - wy
  187. 187 let row0 = y0 * w
  188. 188 let row1 = y1 * w
  189. 189 for x in 0..<new_w {
  190. 190● let x0 = x0_arr[x]
  191. 191 let x1 = x1_arr[x]
  192. 192 let wx = wx_arr[x]
  193. 193 let wx_inv = 1.0F - wx
  194. 194 for c in 0..<ch {
  195. 195● let v00 = Float::from_int(data_int[(row0 + x0) * ch + c])
  196. 196● let v01 = Float::from_int(data_int[(row0 + x1) * ch + c])
  197. 197● let v10 = Float::from_int(data_int[(row1 + x0) * ch + c])
  198. 198● let v11 = Float::from_int(data_int[(row1 + x1) * ch + c])
  199. 199 let top = v00 * wx_inv + v01 * wx
  200. 200 let bot = v10 * wx_inv + v11 * wx
  201. 201 let v = top * wy_inv + bot * wy
  202. 202 let clamped = if v < 0.0F {
  203. 203● 0
  204. 204● } else if v > 65535.0F {
  205. 205○ 65535
  206. 206 } else {
  207. 207● (v + 0.5F).to_int()
  208. 208 }
  209. 209● write_u16_p(out, ((y * new_w + x) * ch + c) * 2, clamped)
  210. 210 }
  211. 211 }
  212. 212 }
  213. 213 @types.Image16::{
  214. 214 width: new_w,
  215. 215 height: new_h,
  216. 216 channels: ch,
  217. 217● data: Bytes::from_array(out),
  218. 218 }
  219. 219}
  220. 220
  221. 221// ========== pyr_up float ==========
  222. 222
  223. 223///|
  224. 224/// 拉普拉斯金字塔上采样 float 版本。
  225. 225/// @see pyr_up 8-bit 版本,本函数为 ImageF 变体
  226. 226pub fn pyr_up_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError {
  227. 227● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  228. 228● raise @types.LoadError::DecodeFailed("pyr_up_f 仅支持 channels=1/3/4")
  229. 229 }
  230. 230 let ch = img.channels
  231. 231 let w = img.width
  232. 232 let h = img.height
  233. 233 let new_w = w * 2
  234. 234 let new_h = h * 2
  235. 235● let data_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  236. 236 for i in 0..<(w * h * ch) {
  237. 237● data_f[i] = read_f32_p(img.data, i * 4)
  238. 238 }
  239. 239● let x0_arr : Array[Int32] = Array::make(new_w, 0)
  240. 240● let x1_arr : Array[Int32] = Array::make(new_w, 0)
  241. 241● let wx_arr : Array[Float] = Array::make(new_w, 0.0F)
  242. 242 for x in 0..<new_w {
  243. 243● let fx = (Float::from_int(x) - 0.5F) / 2.0F
  244. 244● let x0 = @types.clamp_int(fx.to_int(), 0, w - 1)
  245. 245 x0_arr[x] = x0
  246. 246● x1_arr[x] = @types.clamp_int(x0 + 1, 0, w - 1)
  247. 247● wx_arr[x] = fx - Float::from_int(x0)
  248. 248 }
  249. 249● let y0_arr : Array[Int32] = Array::make(new_h, 0)
  250. 250● let y1_arr : Array[Int32] = Array::make(new_h, 0)
  251. 251● let wy_arr : Array[Float] = Array::make(new_h, 0.0F)
  252. 252 for y in 0..<new_h {
  253. 253● let fy = (Float::from_int(y) - 0.5F) / 2.0F
  254. 254● let y0 = @types.clamp_int(fy.to_int(), 0, h - 1)
  255. 255 y0_arr[y] = y0
  256. 256● y1_arr[y] = @types.clamp_int(y0 + 1, 0, h - 1)
  257. 257● wy_arr[y] = fy - Float::from_int(y0)
  258. 258 }
  259. 259● let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00')
  260. 260 for y in 0..<new_h {
  261. 261● let y0 = y0_arr[y]
  262. 262 let y1 = y1_arr[y]
  263. 263 let wy = wy_arr[y]
  264. 264 let wy_inv = 1.0F - wy
  265. 265 let row0 = y0 * w
  266. 266 let row1 = y1 * w
  267. 267 for x in 0..<new_w {
  268. 268● let x0 = x0_arr[x]
  269. 269 let x1 = x1_arr[x]
  270. 270 let wx = wx_arr[x]
  271. 271 let wx_inv = 1.0F - wx
  272. 272 for c in 0..<ch {
  273. 273● let v00 = data_f[(row0 + x0) * ch + c]
  274. 274 let v01 = data_f[(row0 + x1) * ch + c]
  275. 275 let v10 = data_f[(row1 + x0) * ch + c]
  276. 276 let v11 = data_f[(row1 + x1) * ch + c]
  277. 277 let top = v00 * wx_inv + v01 * wx
  278. 278 let bot = v10 * wx_inv + v11 * wx
  279. 279 let v = top * wy_inv + bot * wy
  280. 280● write_f32_p(out, ((y * new_w + x) * ch + c) * 4, v)
  281. 281 }
  282. 282 }
  283. 283 }
  284. 284 @types.ImageF::{
  285. 285 width: new_w,
  286. 286 height: new_h,
  287. 287 channels: ch,
  288. 288● data: Bytes::from_array(out),
  289. 289 }
  290. 290}
  291. 291
  292. 292// ========== laplacian_diff 16-bit ==========
  293. 293
  294. 294///|
  295. 295fn laplacian_diff_p16(a : @types.Image16, b : @types.Image16) -> @types.Image16 {
  296. 296● let ch = a.channels
  297. 297 let w = a.width
  298. 298 let h = a.height
  299. 299● let a_int : Array[Int32] = Array::make(w * h * ch, 0)
  300. 300 for i in 0..<(w * h * ch) {
  301. 301● a_int[i] = read_u16_p(a.data, i * 2)
  302. 302 }
  303. 303 let bw = b.width
  304. 304 let bh = b.height
  305. 305● let b_int : Array[Int32] = Array::make(bw * bh * ch, 0)
  306. 306 for i in 0..<(bw * bh * ch) {
  307. 307● b_int[i] = read_u16_p(b.data, i * 2)
  308. 308 }
  309. 309● let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  310. 310 for y in 0..<h {
  311. 311● let by = y * bh / h
  312. 312 for x in 0..<w {
  313. 313● let bx = x * bw / w
  314. 314 for c in 0..<ch {
  315. 315● let va = a_int[(y * w + x) * ch + c]
  316. 316 let vb = b_int[(by * bw + bx) * ch + c]
  317. 317 let diff = va - vb + 32768
  318. 318 let clamped = if diff < 0 {
  319. 319● 0
  320. 320● } else if diff > 65535 {
  321. 321○ 65535
  322. 322 } else {
  323. 323● diff
  324. 324 }
  325. 325● write_u16_p(out, ((y * w + x) * ch + c) * 2, clamped)
  326. 326 }
  327. 327 }
  328. 328 }
  329. 329 @types.Image16::{
  330. 330 width: w,
  331. 331 height: h,
  332. 332 channels: ch,
  333. 333● data: Bytes::from_array(out),
  334. 334 }
  335. 335}
  336. 336
  337. 337// ========== laplacian_diff float ==========
  338. 338
  339. 339///|
  340. 340fn laplacian_diff_pf(a : @types.ImageF, b : @types.ImageF) -> @types.ImageF {
  341. 341● let ch = a.channels
  342. 342 let w = a.width
  343. 343 let h = a.height
  344. 344● let a_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  345. 345 for i in 0..<(w * h * ch) {
  346. 346● a_f[i] = read_f32_p(a.data, i * 4)
  347. 347 }
  348. 348 let bw = b.width
  349. 349 let bh = b.height
  350. 350● let b_f : Array[Float] = Array::make(bw * bh * ch, 0.0F)
  351. 351 for i in 0..<(bw * bh * ch) {
  352. 352● b_f[i] = read_f32_p(b.data, i * 4)
  353. 353 }
  354. 354● let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  355. 355 for y in 0..<h {
  356. 356● let by = y * bh / h
  357. 357 for x in 0..<w {
  358. 358● let bx = x * bw / w
  359. 359 for c in 0..<ch {
  360. 360● let va = a_f[(y * w + x) * ch + c]
  361. 361 let vb = b_f[(by * bw + bx) * ch + c]
  362. 362 let diff = va - vb + 0.5F
  363. 363● write_f32_p(out, ((y * w + x) * ch + c) * 4, diff)
  364. 364 }
  365. 365 }
  366. 366 }
  367. 367 @types.ImageF::{
  368. 368 width: w,
  369. 369 height: h,
  370. 370 channels: ch,
  371. 371● data: Bytes::from_array(out),
  372. 372 }
  373. 373}
  374. 374
  375. 375// ========== build_gaussian_pyramid 16-bit ==========
  376. 376
  377. 377///|
  378. 378/// 构建高斯金字塔 16-bit 版本。
  379. 379/// @see build_gaussian_pyramid 8-bit 版本,本函数为 Image16 变体
  380. 380pub fn build_gaussian_pyramid_16(
  381. 381 img : @types.Image16,
  382. 382 levels : Int32,
  383. 383) -> Array[@types.Image16] raise @types.LoadError {
  384. 384● let result : Array[@types.Image16] = [img]
  385. 385 let mut current = img
  386. 386◐ let max_levels = if levels == 0 { 100 } else { levels }
  387. 387 for _ in 1..<max_levels {
  388. 388● if current.width < 2 || current.height < 2 {
  389. 389○ break
  390. 390 }
  391. 391● current = pyr_down_16(current)
  392. 392● result.push(current)
  393. 393 }
  394. 394 result
  395. 395}
  396. 396
  397. 397// ========== build_gaussian_pyramid float ==========
  398. 398
  399. 399///|
  400. 400/// 构建高斯金字塔 float 版本。
  401. 401/// @see build_gaussian_pyramid 8-bit 版本,本函数为 ImageF 变体
  402. 402pub fn build_gaussian_pyramid_f(
  403. 403 img : @types.ImageF,
  404. 404 levels : Int32,
  405. 405) -> Array[@types.ImageF] raise @types.LoadError {
  406. 406● let result : Array[@types.ImageF] = [img]
  407. 407 let mut current = img
  408. 408◐ let max_levels = if levels == 0 { 100 } else { levels }
  409. 409 for _ in 1..<max_levels {
  410. 410● if current.width < 2 || current.height < 2 {
  411. 411○ break
  412. 412 }
  413. 413● current = pyr_down_f(current)
  414. 414● result.push(current)
  415. 415 }
  416. 416 result
  417. 417}
  418. 418
  419. 419// ========== build_laplacian_pyramid 16-bit ==========
  420. 420
  421. 421///|
  422. 422/// 构建拉普拉斯金字塔 16-bit 版本。
  423. 423/// @see build_laplacian_pyramid 8-bit 版本,本函数为 Image16 变体
  424. 424pub fn build_laplacian_pyramid_16(
  425. 425 img : @types.Image16,
  426. 426 levels : Int32,
  427. 427) -> Array[@types.Image16] raise @types.LoadError {
  428. 428● let gaussian = build_gaussian_pyramid_16(img, levels)
  429. 429 let result : Array[@types.Image16] = []
  430. 430● for i in 0..<(gaussian.length() - 1) {
  431. 431● let upsampled = pyr_up_16(gaussian[i + 1])
  432. 432● let lap = laplacian_diff_p16(gaussian[i], upsampled)
  433. 433● result.push(lap)
  434. 434 }
  435. 435● result.push(gaussian[gaussian.length() - 1])
  436. 436 result
  437. 437}
  438. 438
  439. 439// ========== build_laplacian_pyramid float ==========
  440. 440
  441. 441///|
  442. 442/// 构建拉普拉斯金字塔 float 版本。
  443. 443pub fn build_laplacian_pyramid_float(
  444. 444 img : @types.ImageF,
  445. 445 levels : Int32,
  446. 446) -> Array[@types.ImageF] raise @types.LoadError {
  447. 447● let gaussian = build_gaussian_pyramid_f(img, levels)
  448. 448 let result : Array[@types.ImageF] = []
  449. 449● for i in 0..<(gaussian.length() - 1) {
  450. 450● let upsampled = pyr_up_f(gaussian[i + 1])
  451. 451● let lap = laplacian_diff_pf(gaussian[i], upsampled)
  452. 452● result.push(lap)
  453. 453 }
  454. 454● result.push(gaussian[gaussian.length() - 1])
  455. 455 result
  456. 456}