image.process\filter\convolve_bilateral_16f.mbt

89.3% · 117 / 131 points · 425 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/// # 卷积双边 16f convolve bilateral 16f
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 卷积 + 双边滤波 16-bit/float 变体
  7. 7
  8. 8// ========== 辅助函数 ==========
  9. 9
  10. 10///|
  11. 11fn read_u16_le_cb(data : Bytes, offset : Int32) -> Int32 {
  12. 12● data[offset].to_int() + data[offset + 1].to_int() * 256
  13. 13}
  14. 14
  15. 15///|
  16. 16fn write_u16_le_cb(buf : Array[Byte], offset : Int32, val : Int32) -> Unit {
  17. 17● buf[offset] = (val & 0xFF).to_byte()
  18. 18● buf[offset + 1] = ((val >> 8) & 0xFF).to_byte()
  19. 19}
  20. 20
  21. 21///|
  22. 22fn read_float_le_cb(data : Bytes, offset : Int32) -> Float {
  23. 23● let b0 = data[offset].to_int()
  24. 24● let b1 = data[offset + 1].to_int()
  25. 25● let b2 = data[offset + 2].to_int()
  26. 26● let b3 = data[offset + 3].to_int()
  27. 27 let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
  28. 28● Float::reinterpret_from_int(bits)
  29. 29}
  30. 30
  31. 31///|
  32. 32fn write_f32_le_cb(buf : Array[Byte], offset : Int32, val : Float) -> Unit {
  33. 33● let bits = val.reinterpret_as_int()
  34. 34● buf[offset] = (bits & 0xFF).to_byte()
  35. 35● buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  36. 36● buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  37. 37● buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
  38. 38}
  39. 39
  40. 40///|
  41. 41fn img16_to_img8_cb(img : @types.Image16) -> @types.Image {
  42. 42● let w = img.width
  43. 43 let h = img.height
  44. 44 let ch = img.channels
  45. 45● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  46. 46 for i in 0..<(w * h * ch) {
  47. 47● let v16 = read_u16_le_cb(img.data, i * 2)
  48. 48● data8[i] = (v16 / 257).to_byte()
  49. 49 }
  50. 50 @types.Image::{
  51. 51 width: w,
  52. 52 height: h,
  53. 53 channels: ch,
  54. 54● data: Bytes::from_array(data8),
  55. 55 }
  56. 56}
  57. 57
  58. 58///|
  59. 59fn imgf_to_img8_cb(img : @types.ImageF) -> @types.Image {
  60. 60● let w = img.width
  61. 61 let h = img.height
  62. 62 let ch = img.channels
  63. 63● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  64. 64 for i in 0..<(w * h * ch) {
  65. 65● let vf = read_float_le_cb(img.data, i * 4)
  66. 66● let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255)
  67. 67● data8[i] = v8.to_byte()
  68. 68 }
  69. 69 @types.Image::{
  70. 70 width: w,
  71. 71 height: h,
  72. 72 channels: ch,
  73. 73● data: Bytes::from_array(data8),
  74. 74 }
  75. 75}
  76. 76
  77. 77///|
  78. 78fn img8_to_img16_cb(img : @types.Image) -> @types.Image16 {
  79. 79● let w = img.width
  80. 80 let h = img.height
  81. 81 let ch = img.channels
  82. 82● let data16 : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  83. 83 for i in 0..<(w * h * ch) {
  84. 84● let v8 = img.data[i].to_int()
  85. 85● write_u16_le_cb(data16, i * 2, v8 * 257)
  86. 86 }
  87. 87 @types.Image16::{
  88. 88 width: w,
  89. 89 height: h,
  90. 90 channels: ch,
  91. 91● data: Bytes::from_array(data16),
  92. 92 }
  93. 93}
  94. 94
  95. 95///|
  96. 96fn img8_to_imgf_cb(img : @types.Image) -> @types.ImageF {
  97. 97● let w = img.width
  98. 98 let h = img.height
  99. 99 let ch = img.channels
  100. 100● let dataf : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  101. 101 for i in 0..<(w * h * ch) {
  102. 102● let v8 = img.data[i].to_int()
  103. 103● write_f32_le_cb(dataf, i * 4, Float::from_int(v8) / 255.0F)
  104. 104 }
  105. 105 @types.ImageF::{
  106. 106 width: w,
  107. 107 height: h,
  108. 108 channels: ch,
  109. 109● data: Bytes::from_array(dataf),
  110. 110 }
  111. 111}
  112. 112
  113. 113// ========== 卷积 16-bit ==========
  114. 114
  115. 115///|
  116. 116/// 16-bit 3x3 卷积。
  117. 117/// @see convolve 8-bit 版本,本函数为 Image16 变体
  118. 118pub fn convolve_16(
  119. 119 img : @types.Image16,
  120. 120 kernel : Array[Float],
  121. 121 bias : Float,
  122. 122 scale : Float,
  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 "convolve_16: 图像尺寸必须大于0",
  128. 128 )
  129. 129 }
  130. 130 if img.channels < 1 || img.channels > 4 {
  131. 131● raise @types.LoadError::DecodeFailed(
  132. 132 "convolve_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 "convolve_16: 数据大小不匹配,期望 " +
  139. 139● expected_size.to_string() +
  140. 140 ",实际 " +
  141. 141● img.data.length().to_string(),
  142. 142 )
  143. 143 }
  144. 144● if kernel.length() != 9 {
  145. 145● raise @types.LoadError::DecodeFailed(
  146. 146 "convolve_16: kernel必须为9个值(3x3)",
  147. 147 )
  148. 148 }
  149. 149 if scale == 0.0F {
  150. 150● raise @types.LoadError::DecodeFailed("convolve_16: scale不能为0")
  151. 151 }
  152. 152
  153. 153● let img8 = img16_to_img8_cb(img)
  154. 154● let result8 = convolve(img8, kernel, bias, scale)
  155. 155● img8_to_img16_cb(result8)
  156. 156}
  157. 157
  158. 158// ========== 卷积 float ==========
  159. 159
  160. 160///|
  161. 161/// float 3x3 卷积。
  162. 162/// @see convolve 8-bit 版本,本函数为 ImageF 变体
  163. 163pub fn convolve_f(
  164. 164 img : @types.ImageF,
  165. 165 kernel : Array[Float],
  166. 166 bias : Float,
  167. 167 scale : Float,
  168. 168) -> @types.ImageF raise @types.LoadError {
  169. 169 // 输入验证
  170. 170● if img.width <= 0 || img.height <= 0 {
  171. 171● raise @types.LoadError::DecodeFailed(
  172. 172 "convolve_f: 图像尺寸必须大于0",
  173. 173 )
  174. 174 }
  175. 175 if img.channels < 1 || img.channels > 4 {
  176. 176● raise @types.LoadError::DecodeFailed(
  177. 177 "convolve_f: 通道数必须在1-4之间",
  178. 178 )
  179. 179 }
  180. 180 let expected_size = img.width * img.height * img.channels * 4
  181. 181● if img.data.length() != expected_size {
  182. 182● raise @types.LoadError::DecodeFailed(
  183. 183 "convolve_f: 数据大小不匹配,期望 " +
  184. 184● expected_size.to_string() +
  185. 185 ",实际 " +
  186. 186● img.data.length().to_string(),
  187. 187 )
  188. 188 }
  189. 189● if kernel.length() != 9 {
  190. 190● raise @types.LoadError::DecodeFailed(
  191. 191 "convolve_f: kernel必须为9个值(3x3)",
  192. 192 )
  193. 193 }
  194. 194 if scale == 0.0F {
  195. 195○ raise @types.LoadError::DecodeFailed("convolve_f: scale不能为0")
  196. 196 }
  197. 197
  198. 198● let img8 = imgf_to_img8_cb(img)
  199. 199● let result8 = convolve(img8, kernel, bias, scale)
  200. 200● img8_to_imgf_cb(result8)
  201. 201}
  202. 202
  203. 203// ========== 双边滤波 16-bit ==========
  204. 204
  205. 205///|
  206. 206/// 16-bit 双边滤波。
  207. 207/// @see bilateral_filter 8-bit 版本,本函数为 Image16 变体
  208. 208pub fn bilateral_filter_16(
  209. 209 img : @types.Image16,
  210. 210 radius : Int32,
  211. 211 sigma_space : Float,
  212. 212 sigma_color : Float,
  213. 213) -> @types.Image16 raise @types.LoadError {
  214. 214 // 输入验证
  215. 215● if img.width <= 0 || img.height <= 0 {
  216. 216● raise @types.LoadError::DecodeFailed(
  217. 217 "bilateral_filter_16: 图像尺寸必须大于0",
  218. 218 )
  219. 219 }
  220. 220 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  221. 221○ raise @types.LoadError::DecodeFailed(
  222. 222 "bilateral_filter_16: 仅支持 channels=1/3/4",
  223. 223 )
  224. 224 }
  225. 225 let expected_size = img.width * img.height * img.channels * 2
  226. 226● if img.data.length() != expected_size {
  227. 227● raise @types.LoadError::DecodeFailed(
  228. 228 "bilateral_filter_16: 数据大小不匹配,期望 " +
  229. 229● expected_size.to_string() +
  230. 230 ",实际 " +
  231. 231● img.data.length().to_string(),
  232. 232 )
  233. 233 }
  234. 234 if radius < 1 {
  235. 235● raise @types.LoadError::DecodeFailed(
  236. 236 "bilateral_filter_16: radius必须 >= 1",
  237. 237 )
  238. 238 }
  239. 239 if sigma_space <= 0.0F {
  240. 240● raise @types.LoadError::DecodeFailed(
  241. 241 "bilateral_filter_16: sigma_space必须大于0",
  242. 242 )
  243. 243 }
  244. 244 if sigma_color <= 0.0F {
  245. 245○ raise @types.LoadError::DecodeFailed(
  246. 246 "bilateral_filter_16: sigma_color必须大于0",
  247. 247 )
  248. 248 }
  249. 249
  250. 250● let img8 = img16_to_img8_cb(img)
  251. 251● let result8 = bilateral_filter(img8, radius, sigma_space, sigma_color)
  252. 252● img8_to_img16_cb(result8)
  253. 253}
  254. 254
  255. 255// ========== 双边滤波 float ==========
  256. 256
  257. 257///|
  258. 258/// float 双边滤波。
  259. 259/// @see bilateral_filter 8-bit 版本,本函数为 ImageF 变体
  260. 260pub fn bilateral_filter_f(
  261. 261 img : @types.ImageF,
  262. 262 radius : Int32,
  263. 263 sigma_space : Float,
  264. 264 sigma_color : Float,
  265. 265) -> @types.ImageF raise @types.LoadError {
  266. 266 // 输入验证
  267. 267● if img.width <= 0 || img.height <= 0 {
  268. 268● raise @types.LoadError::DecodeFailed(
  269. 269 "bilateral_filter_f: 图像尺寸必须大于0",
  270. 270 )
  271. 271 }
  272. 272 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  273. 273○ raise @types.LoadError::DecodeFailed(
  274. 274 "bilateral_filter_f: 仅支持 channels=1/3/4",
  275. 275 )
  276. 276 }
  277. 277 let expected_size = img.width * img.height * img.channels * 4
  278. 278● if img.data.length() != expected_size {
  279. 279● raise @types.LoadError::DecodeFailed(
  280. 280 "bilateral_filter_f: 数据大小不匹配,期望 " +
  281. 281● expected_size.to_string() +
  282. 282 ",实际 " +
  283. 283● img.data.length().to_string(),
  284. 284 )
  285. 285 }
  286. 286 if radius < 1 {
  287. 287● raise @types.LoadError::DecodeFailed(
  288. 288 "bilateral_filter_f: radius必须 >= 1",
  289. 289 )
  290. 290 }
  291. 291 if sigma_space <= 0.0F {
  292. 292○ raise @types.LoadError::DecodeFailed(
  293. 293 "bilateral_filter_f: sigma_space必须大于0",
  294. 294 )
  295. 295 }
  296. 296 if sigma_color <= 0.0F {
  297. 297○ raise @types.LoadError::DecodeFailed(
  298. 298 "bilateral_filter_f: sigma_color必须大于0",
  299. 299 )
  300. 300 }
  301. 301
  302. 302● let img8 = imgf_to_img8_cb(img)
  303. 303● let result8 = bilateral_filter(img8, radius, sigma_space, sigma_color)
  304. 304● img8_to_imgf_cb(result8)
  305. 305}
  306. 306
  307. 307// ========== 快速双边滤波 16-bit ==========
  308. 308
  309. 309///|
  310. 310/// 16-bit 快速双边滤波。
  311. 311/// @see bilateral_filter_fast 8-bit 版本,本函数为 Image16 变体
  312. 312pub fn bilateral_filter_fast_16(
  313. 313 img : @types.Image16,
  314. 314 radius : Int32,
  315. 315 sigma_space : Float,
  316. 316 sigma_color : Float,
  317. 317 downsample : Int32,
  318. 318) -> @types.Image16 raise @types.LoadError {
  319. 319 // 输入验证
  320. 320● if img.width <= 0 || img.height <= 0 {
  321. 321● raise @types.LoadError::DecodeFailed(
  322. 322 "bilateral_filter_fast_16: 图像尺寸必须大于0",
  323. 323 )
  324. 324 }
  325. 325 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  326. 326○ raise @types.LoadError::DecodeFailed(
  327. 327 "bilateral_filter_fast_16: 仅支持 channels=1/3/4",
  328. 328 )
  329. 329 }
  330. 330 let expected_size = img.width * img.height * img.channels * 2
  331. 331● if img.data.length() != expected_size {
  332. 332● raise @types.LoadError::DecodeFailed(
  333. 333 "bilateral_filter_fast_16: 数据大小不匹配,期望 " +
  334. 334● expected_size.to_string() +
  335. 335 ",实际 " +
  336. 336● img.data.length().to_string(),
  337. 337 )
  338. 338 }
  339. 339 if radius < 1 {
  340. 340● raise @types.LoadError::DecodeFailed(
  341. 341 "bilateral_filter_fast_16: radius必须 >= 1",
  342. 342 )
  343. 343 }
  344. 344 if sigma_space <= 0.0F {
  345. 345○ raise @types.LoadError::DecodeFailed(
  346. 346 "bilateral_filter_fast_16: sigma_space必须大于0",
  347. 347 )
  348. 348 }
  349. 349 if sigma_color <= 0.0F {
  350. 350○ raise @types.LoadError::DecodeFailed(
  351. 351 "bilateral_filter_fast_16: sigma_color必须大于0",
  352. 352 )
  353. 353 }
  354. 354 if downsample < 1 {
  355. 355○ raise @types.LoadError::DecodeFailed(
  356. 356 "bilateral_filter_fast_16: downsample必须 >= 1",
  357. 357 )
  358. 358 }
  359. 359
  360. 360● let img8 = img16_to_img8_cb(img)
  361. 361● let result8 = bilateral_filter_fast(
  362. 362 img8, radius, sigma_space, sigma_color, downsample,
  363. 363 )
  364. 364● img8_to_img16_cb(result8)
  365. 365}
  366. 366
  367. 367// ========== 快速双边滤波 float ==========
  368. 368
  369. 369///|
  370. 370/// float 快速双边滤波。
  371. 371/// @see bilateral_filter_fast 8-bit 版本,本函数为 ImageF 变体
  372. 372pub fn bilateral_filter_fast_f(
  373. 373 img : @types.ImageF,
  374. 374 radius : Int32,
  375. 375 sigma_space : Float,
  376. 376 sigma_color : Float,
  377. 377 downsample : Int32,
  378. 378) -> @types.ImageF raise @types.LoadError {
  379. 379 // 输入验证
  380. 380● if img.width <= 0 || img.height <= 0 {
  381. 381● raise @types.LoadError::DecodeFailed(
  382. 382 "bilateral_filter_fast_f: 图像尺寸必须大于0",
  383. 383 )
  384. 384 }
  385. 385 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  386. 386○ raise @types.LoadError::DecodeFailed(
  387. 387 "bilateral_filter_fast_f: 仅支持 channels=1/3/4",
  388. 388 )
  389. 389 }
  390. 390 let expected_size = img.width * img.height * img.channels * 4
  391. 391● if img.data.length() != expected_size {
  392. 392● raise @types.LoadError::DecodeFailed(
  393. 393 "bilateral_filter_fast_f: 数据大小不匹配,期望 " +
  394. 394● expected_size.to_string() +
  395. 395 ",实际 " +
  396. 396● img.data.length().to_string(),
  397. 397 )
  398. 398 }
  399. 399 if radius < 1 {
  400. 400● raise @types.LoadError::DecodeFailed(
  401. 401 "bilateral_filter_fast_f: radius必须 >= 1",
  402. 402 )
  403. 403 }
  404. 404 if sigma_space <= 0.0F {
  405. 405○ raise @types.LoadError::DecodeFailed(
  406. 406 "bilateral_filter_fast_f: sigma_space必须大于0",
  407. 407 )
  408. 408 }
  409. 409 if sigma_color <= 0.0F {
  410. 410○ raise @types.LoadError::DecodeFailed(
  411. 411 "bilateral_filter_fast_f: sigma_color必须大于0",
  412. 412 )
  413. 413 }
  414. 414 if downsample < 1 {
  415. 415○ raise @types.LoadError::DecodeFailed(
  416. 416 "bilateral_filter_fast_f: downsample必须 >= 1",
  417. 417 )
  418. 418 }
  419. 419
  420. 420● let img8 = imgf_to_img8_cb(img)
  421. 421● let result8 = bilateral_filter_fast(
  422. 422 img8, radius, sigma_space, sigma_color, downsample,
  423. 423 )
  424. 424● img8_to_imgf_cb(result8)
  425. 425}