image.process\filter\advanced_16f.mbt

98.1% · 156 / 159 points · 494 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 filtering 16-bit/float variants
  3. 3///
  4. 4/// 16位/浮点双边/NLM 滤波 bilateral_filter_16 / non_local_means_16
  5. 5/// 16位/浮点双边/NLM 滤波 16-bit/float bilateral/NLM filtering
  6. 6// 高级算法 16-bit/float 变体(filter 包)
  7. 7// inpaint / NLM 去噪 支持 Image16 / ImageF
  8. 8
  9. 9// ========== 辅助函数 ==========
  10. 10
  11. 11///|
  12. 12fn read_u16_le_adv(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_adv(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_adv(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_adv(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 img16_to_img8_adv(img : @types.Image16) -> @types.Image {
  43. 43● let w = img.width
  44. 44 let h = img.height
  45. 45 let ch = img.channels
  46. 46● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  47. 47 for i in 0..<(w * h * ch) {
  48. 48● let v16 = read_u16_le_adv(img.data, i * 2)
  49. 49● data8[i] = (v16 / 257).to_byte()
  50. 50 }
  51. 51 @types.Image::{
  52. 52 width: w,
  53. 53 height: h,
  54. 54 channels: ch,
  55. 55● data: Bytes::from_array(data8),
  56. 56 }
  57. 57}
  58. 58
  59. 59///|
  60. 60fn imgf_to_img8_adv(img : @types.ImageF) -> @types.Image {
  61. 61● let w = img.width
  62. 62 let h = img.height
  63. 63 let ch = img.channels
  64. 64● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  65. 65 for i in 0..<(w * h * ch) {
  66. 66● let vf = read_float_le_adv(img.data, i * 4)
  67. 67● let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255)
  68. 68● data8[i] = v8.to_byte()
  69. 69 }
  70. 70 @types.Image::{
  71. 71 width: w,
  72. 72 height: h,
  73. 73 channels: ch,
  74. 74● data: Bytes::from_array(data8),
  75. 75 }
  76. 76}
  77. 77
  78. 78///|
  79. 79fn img8_to_img16_adv(img : @types.Image) -> @types.Image16 {
  80. 80● let w = img.width
  81. 81 let h = img.height
  82. 82 let ch = img.channels
  83. 83● let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  84. 84 for i in 0..<(w * h * ch) {
  85. 85● let v8 = img.data[i].to_int()
  86. 86● write_u16_le_adv(out, i * 2, v8 * 257)
  87. 87 }
  88. 88 @types.Image16::{
  89. 89 width: w,
  90. 90 height: h,
  91. 91 channels: ch,
  92. 92● data: Bytes::from_array(out),
  93. 93 }
  94. 94}
  95. 95
  96. 96///|
  97. 97fn img8_to_imgf_adv(img : @types.Image) -> @types.ImageF {
  98. 98● let w = img.width
  99. 99 let h = img.height
  100. 100 let ch = img.channels
  101. 101● let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  102. 102 for i in 0..<(w * h * ch) {
  103. 103● let v8 = img.data[i].to_int()
  104. 104● write_f32_le_adv(out, i * 4, Float::from_int(v8) / 255.0F)
  105. 105 }
  106. 106 @types.ImageF::{
  107. 107 width: w,
  108. 108 height: h,
  109. 109 channels: ch,
  110. 110● data: Bytes::from_array(out),
  111. 111 }
  112. 112}
  113. 113
  114. 114// ========== Inpaint 16-bit ==========
  115. 115
  116. 116///|
  117. 117/// 16-bit 图像修复。
  118. 118/// @see inpaint 8-bit 版本,本函数为 Image16 变体
  119. 119pub fn inpaint_16(
  120. 120 img : @types.Image16,
  121. 121 mask : @types.Image16,
  122. 122 iterations~ : Int32 = 100,
  123. 123) -> @types.Image16 raise @types.LoadError {
  124. 124 // 输入验证
  125. 125● if img.width <= 0 || img.height <= 0 {
  126. 126● raise @types.LoadError::DecodeFailed(
  127. 127 "inpaint_16: 图像尺寸必须大于0",
  128. 128 )
  129. 129 }
  130. 130 if img.channels < 1 || img.channels > 4 {
  131. 131● raise @types.LoadError::DecodeFailed(
  132. 132 "inpaint_16: 通道数必须在1-4之间",
  133. 133 )
  134. 134 }
  135. 135 let expected_size = img.width * img.height * img.channels * 2
  136. 136● if img.data.length() != expected_size {
  137. 137● raise @types.LoadError::DecodeFailed(
  138. 138 "inpaint_16: 图像数据大小不匹配,期望 " +
  139. 139● expected_size.to_string() +
  140. 140 ",实际 " +
  141. 141● img.data.length().to_string(),
  142. 142 )
  143. 143 }
  144. 144 if mask.width != img.width || mask.height != img.height {
  145. 145● raise @types.LoadError::DecodeFailed(
  146. 146 "inpaint_16: 掩码图像尺寸必须与原图一致",
  147. 147 )
  148. 148 }
  149. 149 if iterations <= 0 {
  150. 150● raise @types.LoadError::DecodeFailed("inpaint_16: iterations必须大于0")
  151. 151 }
  152. 152
  153. 153● let img8 = img16_to_img8_adv(img)
  154. 154● let mask8 = img16_to_img8_adv(mask)
  155. 155● let out8 = inpaint(img8, mask8, iterations~)
  156. 156● img8_to_img16_adv(out8)
  157. 157}
  158. 158
  159. 159///|
  160. 160/// 16-bit 快速图像修复。
  161. 161/// @see inpaint_fast 8-bit 版本,本函数为 Image16 变体
  162. 162pub fn inpaint_fast_16(
  163. 163 img : @types.Image16,
  164. 164 mask : @types.Image16,
  165. 165 radius~ : Int32 = 5,
  166. 166) -> @types.Image16 raise @types.LoadError {
  167. 167 // 输入验证
  168. 168● if img.width <= 0 || img.height <= 0 {
  169. 169● raise @types.LoadError::DecodeFailed(
  170. 170 "inpaint_fast_16: 图像尺寸必须大于0",
  171. 171 )
  172. 172 }
  173. 173 if img.channels < 1 || img.channels > 4 {
  174. 174● raise @types.LoadError::DecodeFailed(
  175. 175 "inpaint_fast_16: 通道数必须在1-4之间",
  176. 176 )
  177. 177 }
  178. 178 let expected_size = img.width * img.height * img.channels * 2
  179. 179● if img.data.length() != expected_size {
  180. 180● raise @types.LoadError::DecodeFailed(
  181. 181 "inpaint_fast_16: 图像数据大小不匹配,期望 " +
  182. 182● expected_size.to_string() +
  183. 183 ",实际 " +
  184. 184● img.data.length().to_string(),
  185. 185 )
  186. 186 }
  187. 187 if mask.width != img.width || mask.height != img.height {
  188. 188○ raise @types.LoadError::DecodeFailed(
  189. 189 "inpaint_fast_16: 掩码图像尺寸必须与原图一致",
  190. 190 )
  191. 191 }
  192. 192 if radius <= 0 {
  193. 193● raise @types.LoadError::DecodeFailed("inpaint_fast_16: radius必须大于0")
  194. 194 }
  195. 195
  196. 196● let img8 = img16_to_img8_adv(img)
  197. 197● let mask8 = img16_to_img8_adv(mask)
  198. 198● let out8 = inpaint_fast(img8, mask8, radius~)
  199. 199● img8_to_img16_adv(out8)
  200. 200}
  201. 201
  202. 202// ========== Inpaint float ==========
  203. 203
  204. 204///|
  205. 205/// float 图像修复。
  206. 206/// @see inpaint 8-bit 版本,本函数为 ImageF 变体
  207. 207pub fn inpaint_f(
  208. 208 img : @types.ImageF,
  209. 209 mask : @types.ImageF,
  210. 210 iterations~ : Int32 = 100,
  211. 211) -> @types.ImageF raise @types.LoadError {
  212. 212 // 输入验证
  213. 213● if img.width <= 0 || img.height <= 0 {
  214. 214● raise @types.LoadError::DecodeFailed("inpaint_f: 图像尺寸必须大于0")
  215. 215 }
  216. 216 if img.channels < 1 || img.channels > 4 {
  217. 217● raise @types.LoadError::DecodeFailed(
  218. 218 "inpaint_f: 通道数必须在1-4之间",
  219. 219 )
  220. 220 }
  221. 221 let expected_size = img.width * img.height * img.channels * 4
  222. 222● if img.data.length() != expected_size {
  223. 223● raise @types.LoadError::DecodeFailed(
  224. 224 "inpaint_f: 图像数据大小不匹配,期望 " +
  225. 225● expected_size.to_string() +
  226. 226 ",实际 " +
  227. 227● img.data.length().to_string(),
  228. 228 )
  229. 229 }
  230. 230 if mask.width != img.width || mask.height != img.height {
  231. 231○ raise @types.LoadError::DecodeFailed(
  232. 232 "inpaint_f: 掩码图像尺寸必须与原图一致",
  233. 233 )
  234. 234 }
  235. 235 if iterations <= 0 {
  236. 236● raise @types.LoadError::DecodeFailed("inpaint_f: iterations必须大于0")
  237. 237 }
  238. 238
  239. 239● let img8 = imgf_to_img8_adv(img)
  240. 240● let mask8 = imgf_to_img8_adv(mask)
  241. 241● let out8 = inpaint(img8, mask8, iterations~)
  242. 242● img8_to_imgf_adv(out8)
  243. 243}
  244. 244
  245. 245///|
  246. 246/// float 快速图像修复。
  247. 247/// @see inpaint_fast 8-bit 版本,本函数为 ImageF 变体
  248. 248pub fn inpaint_fast_f(
  249. 249 img : @types.ImageF,
  250. 250 mask : @types.ImageF,
  251. 251 radius~ : Int32 = 5,
  252. 252) -> @types.ImageF raise @types.LoadError {
  253. 253 // 输入验证
  254. 254● if img.width <= 0 || img.height <= 0 {
  255. 255● raise @types.LoadError::DecodeFailed(
  256. 256 "inpaint_fast_f: 图像尺寸必须大于0",
  257. 257 )
  258. 258 }
  259. 259 if img.channels < 1 || img.channels > 4 {
  260. 260● raise @types.LoadError::DecodeFailed(
  261. 261 "inpaint_fast_f: 通道数必须在1-4之间",
  262. 262 )
  263. 263 }
  264. 264 let expected_size = img.width * img.height * img.channels * 4
  265. 265● if img.data.length() != expected_size {
  266. 266● raise @types.LoadError::DecodeFailed(
  267. 267 "inpaint_fast_f: 图像数据大小不匹配,期望 " +
  268. 268● expected_size.to_string() +
  269. 269 ",实际 " +
  270. 270● img.data.length().to_string(),
  271. 271 )
  272. 272 }
  273. 273 if mask.width != img.width || mask.height != img.height {
  274. 274○ raise @types.LoadError::DecodeFailed(
  275. 275 "inpaint_fast_f: 掩码图像尺寸必须与原图一致",
  276. 276 )
  277. 277 }
  278. 278 if radius <= 0 {
  279. 279● raise @types.LoadError::DecodeFailed("inpaint_fast_f: radius必须大于0")
  280. 280 }
  281. 281
  282. 282● let img8 = imgf_to_img8_adv(img)
  283. 283● let mask8 = imgf_to_img8_adv(mask)
  284. 284● let out8 = inpaint_fast(img8, mask8, radius~)
  285. 285● img8_to_imgf_adv(out8)
  286. 286}
  287. 287
  288. 288// ========== NLM 16-bit ==========
  289. 289
  290. 290///|
  291. 291/// 16-bit 非局部均值去噪。
  292. 292/// @see nlm_denoise 8-bit 版本,本函数为 Image16 变体
  293. 293pub fn nlm_denoise_16(
  294. 294 img : @types.Image16,
  295. 295 patch_size~ : Int32 = 3,
  296. 296 search_size~ : Int32 = 7,
  297. 297 h~ : Int32 = 10,
  298. 298) -> @types.Image16 raise @types.LoadError {
  299. 299 // 输入验证
  300. 300● if img.width <= 0 || img.height <= 0 {
  301. 301● raise @types.LoadError::DecodeFailed(
  302. 302 "nlm_denoise_16: 图像尺寸必须大于0",
  303. 303 )
  304. 304 }
  305. 305 if img.channels < 1 || img.channels > 4 {
  306. 306● raise @types.LoadError::DecodeFailed(
  307. 307 "nlm_denoise_16: 通道数必须在1-4之间",
  308. 308 )
  309. 309 }
  310. 310 let expected_size = img.width * img.height * img.channels * 2
  311. 311● if img.data.length() != expected_size {
  312. 312● raise @types.LoadError::DecodeFailed(
  313. 313 "nlm_denoise_16: 数据大小不匹配,期望 " +
  314. 314● expected_size.to_string() +
  315. 315 ",实际 " +
  316. 316● img.data.length().to_string(),
  317. 317 )
  318. 318 }
  319. 319 if patch_size < 1 || patch_size > 10 {
  320. 320● raise @types.LoadError::DecodeFailed(
  321. 321 "nlm_denoise_16: patch_size必须在1-10之间",
  322. 322 )
  323. 323 }
  324. 324 if search_size < 1 || search_size > 20 {
  325. 325● raise @types.LoadError::DecodeFailed(
  326. 326 "nlm_denoise_16: search_size必须在1-20之间",
  327. 327 )
  328. 328 }
  329. 329 if h < 1 {
  330. 330● raise @types.LoadError::DecodeFailed("nlm_denoise_16: h必须 >= 1")
  331. 331 }
  332. 332
  333. 333● let img8 = img16_to_img8_adv(img)
  334. 334● let out8 = nlm_denoise(img8, patch_size~, search_size~, h~)
  335. 335● img8_to_img16_adv(out8)
  336. 336}
  337. 337
  338. 338///|
  339. 339/// 16-bit 快速非局部均值去噪。
  340. 340/// @see nlm_denoise_fast 8-bit 版本,本函数为 Image16 变体
  341. 341pub fn nlm_denoise_fast_16(
  342. 342 img : @types.Image16,
  343. 343 patch_size~ : Int32 = 3,
  344. 344 search_size~ : Int32 = 7,
  345. 345 h~ : Int32 = 10,
  346. 346 step~ : Int32 = 2,
  347. 347) -> @types.Image16 raise @types.LoadError {
  348. 348 // 输入验证
  349. 349● if img.width <= 0 || img.height <= 0 {
  350. 350● raise @types.LoadError::DecodeFailed(
  351. 351 "nlm_denoise_fast_16: 图像尺寸必须大于0",
  352. 352 )
  353. 353 }
  354. 354 if img.channels < 1 || img.channels > 4 {
  355. 355● raise @types.LoadError::DecodeFailed(
  356. 356 "nlm_denoise_fast_16: 通道数必须在1-4之间",
  357. 357 )
  358. 358 }
  359. 359 let expected_size = img.width * img.height * img.channels * 2
  360. 360● if img.data.length() != expected_size {
  361. 361● raise @types.LoadError::DecodeFailed(
  362. 362 "nlm_denoise_fast_16: 数据大小不匹配,期望 " +
  363. 363● expected_size.to_string() +
  364. 364 ",实际 " +
  365. 365● img.data.length().to_string(),
  366. 366 )
  367. 367 }
  368. 368 if patch_size < 1 || patch_size > 10 {
  369. 369● raise @types.LoadError::DecodeFailed(
  370. 370 "nlm_denoise_fast_16: patch_size必须在1-10之间",
  371. 371 )
  372. 372 }
  373. 373 if search_size < 1 || search_size > 20 {
  374. 374● raise @types.LoadError::DecodeFailed(
  375. 375 "nlm_denoise_fast_16: search_size必须在1-20之间",
  376. 376 )
  377. 377 }
  378. 378 if h < 1 {
  379. 379● raise @types.LoadError::DecodeFailed("nlm_denoise_fast_16: h必须 >= 1")
  380. 380 }
  381. 381 if step < 1 || step > 5 {
  382. 382● raise @types.LoadError::DecodeFailed(
  383. 383 "nlm_denoise_fast_16: step必须在1-5之间",
  384. 384 )
  385. 385 }
  386. 386
  387. 387● let img8 = img16_to_img8_adv(img)
  388. 388● let out8 = nlm_denoise_fast(img8, patch_size~, search_size~, h~, step~)
  389. 389● img8_to_img16_adv(out8)
  390. 390}
  391. 391
  392. 392// ========== NLM float ==========
  393. 393
  394. 394///|
  395. 395/// float 非局部均值去噪。
  396. 396/// @see nlm_denoise 8-bit 版本,本函数为 ImageF 变体
  397. 397pub fn nlm_denoise_f(
  398. 398 img : @types.ImageF,
  399. 399 patch_size~ : Int32 = 3,
  400. 400 search_size~ : Int32 = 7,
  401. 401 h~ : Int32 = 10,
  402. 402) -> @types.ImageF raise @types.LoadError {
  403. 403 // 输入验证
  404. 404● if img.width <= 0 || img.height <= 0 {
  405. 405● raise @types.LoadError::DecodeFailed(
  406. 406 "nlm_denoise_f: 图像尺寸必须大于0",
  407. 407 )
  408. 408 }
  409. 409 if img.channels < 1 || img.channels > 4 {
  410. 410● raise @types.LoadError::DecodeFailed(
  411. 411 "nlm_denoise_f: 通道数必须在1-4之间",
  412. 412 )
  413. 413 }
  414. 414 let expected_size = img.width * img.height * img.channels * 4
  415. 415● if img.data.length() != expected_size {
  416. 416● raise @types.LoadError::DecodeFailed(
  417. 417 "nlm_denoise_f: 数据大小不匹配,期望 " +
  418. 418● expected_size.to_string() +
  419. 419 ",实际 " +
  420. 420● img.data.length().to_string(),
  421. 421 )
  422. 422 }
  423. 423 if patch_size < 1 || patch_size > 10 {
  424. 424● raise @types.LoadError::DecodeFailed(
  425. 425 "nlm_denoise_f: patch_size必须在1-10之间",
  426. 426 )
  427. 427 }
  428. 428 if search_size < 1 || search_size > 20 {
  429. 429● raise @types.LoadError::DecodeFailed(
  430. 430 "nlm_denoise_f: search_size必须在1-20之间",
  431. 431 )
  432. 432 }
  433. 433 if h < 1 {
  434. 434● raise @types.LoadError::DecodeFailed("nlm_denoise_f: h必须 >= 1")
  435. 435 }
  436. 436
  437. 437● let img8 = imgf_to_img8_adv(img)
  438. 438● let out8 = nlm_denoise(img8, patch_size~, search_size~, h~)
  439. 439● img8_to_imgf_adv(out8)
  440. 440}
  441. 441
  442. 442///|
  443. 443/// float 快速非局部均值去噪。
  444. 444/// @see nlm_denoise_fast 8-bit 版本,本函数为 ImageF 变体
  445. 445pub fn nlm_denoise_fast_f(
  446. 446 img : @types.ImageF,
  447. 447 patch_size~ : Int32 = 3,
  448. 448 search_size~ : Int32 = 7,
  449. 449 h~ : Int32 = 10,
  450. 450 step~ : Int32 = 2,
  451. 451) -> @types.ImageF raise @types.LoadError {
  452. 452 // 输入验证
  453. 453● if img.width <= 0 || img.height <= 0 {
  454. 454● raise @types.LoadError::DecodeFailed(
  455. 455 "nlm_denoise_fast_f: 图像尺寸必须大于0",
  456. 456 )
  457. 457 }
  458. 458 if img.channels < 1 || img.channels > 4 {
  459. 459● raise @types.LoadError::DecodeFailed(
  460. 460 "nlm_denoise_fast_f: 通道数必须在1-4之间",
  461. 461 )
  462. 462 }
  463. 463 let expected_size = img.width * img.height * img.channels * 4
  464. 464● if img.data.length() != expected_size {
  465. 465● raise @types.LoadError::DecodeFailed(
  466. 466 "nlm_denoise_fast_f: 数据大小不匹配,期望 " +
  467. 467● expected_size.to_string() +
  468. 468 ",实际 " +
  469. 469● img.data.length().to_string(),
  470. 470 )
  471. 471 }
  472. 472 if patch_size < 1 || patch_size > 10 {
  473. 473● raise @types.LoadError::DecodeFailed(
  474. 474 "nlm_denoise_fast_f: patch_size必须在1-10之间",
  475. 475 )
  476. 476 }
  477. 477 if search_size < 1 || search_size > 20 {
  478. 478● raise @types.LoadError::DecodeFailed(
  479. 479 "nlm_denoise_fast_f: search_size必须在1-20之间",
  480. 480 )
  481. 481 }
  482. 482 if h < 1 {
  483. 483● raise @types.LoadError::DecodeFailed("nlm_denoise_fast_f: h必须 >= 1")
  484. 484 }
  485. 485 if step < 1 || step > 5 {
  486. 486● raise @types.LoadError::DecodeFailed(
  487. 487 "nlm_denoise_fast_f: step必须在1-5之间",
  488. 488 )
  489. 489 }
  490. 490
  491. 491● let img8 = imgf_to_img8_adv(img)
  492. 492● let out8 = nlm_denoise_fast(img8, patch_size~, search_size~, h~, step~)
  493. 493● img8_to_imgf_adv(out8)
  494. 494}