image.process\filter\filter_16f.mbt

96.9% · 347 / 358 points · 933 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 filter 16f
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 图像滤波 16-bit/float 变体:box_blur / gaussian_blur / median_blur
  7. 7// 支持 Image16 / ImageF 两种类型
  8. 8
  9. 9///|
  10. 10fn read_u16_le_f(data : Bytes, offset : Int32) -> Int32 {
  11. 11● data[offset].to_int() + data[offset + 1].to_int() * 256
  12. 12}
  13. 13
  14. 14///|
  15. 15fn write_u16_le_f(buf : Array[Byte], offset : Int32, val : Int32) -> Unit {
  16. 16● buf[offset] = (val & 0xFF).to_byte()
  17. 17● buf[offset + 1] = ((val >> 8) & 0xFF).to_byte()
  18. 18}
  19. 19
  20. 20///|
  21. 21fn read_f32_le_f(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_le_f(buf : Array[Byte], offset : Int32, val : Float) -> Unit {
  32. 32● let bits = val.reinterpret_as_int()
  33. 33● buf[offset] = (bits & 0xFF).to_byte()
  34. 34● buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  35. 35● buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  36. 36● buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
  37. 37}
  38. 38
  39. 39///|
  40. 40/// 16-bit 盒模糊。radius 为模糊半径,窗口大小 = 2*radius+1。
  41. 41/// 使用可分离滑动窗口,O(n) 复杂度。仅模糊颜色通道,alpha 不变。
  42. 42/// @see box_blur 8-bit 版本,本函数为 Image16 变体
  43. 43pub fn box_blur_16(
  44. 44 img : @types.Image16,
  45. 45 radius : Int32,
  46. 46) -> @types.Image16 raise @types.LoadError {
  47. 47 // 输入验证
  48. 48● if img.width <= 0 || img.height <= 0 {
  49. 49● raise @types.LoadError::DecodeFailed(
  50. 50 "box_blur_16: 图像尺寸必须大于0",
  51. 51 )
  52. 52 }
  53. 53 if img.channels < 1 || img.channels > 4 {
  54. 54● raise @types.LoadError::DecodeFailed(
  55. 55 "box_blur_16: 通道数必须在1-4之间",
  56. 56 )
  57. 57 }
  58. 58 let expected_size = img.width * img.height * img.channels * 2
  59. 59● if img.data.length() != expected_size {
  60. 60● raise @types.LoadError::DecodeFailed(
  61. 61 "box_blur_16: 数据大小不匹配,期望 " +
  62. 62● expected_size.to_string() +
  63. 63 ",实际 " +
  64. 64● img.data.length().to_string(),
  65. 65 )
  66. 66 }
  67. 67 if radius < 0 {
  68. 68● raise @types.LoadError::DecodeFailed("box_blur_16: radius不能为负")
  69. 69 }
  70. 70 if radius <= 0 {
  71. 71● return img
  72. 72 }
  73. 73 let w = img.width
  74. 74 let h = img.height
  75. 75 let ch = img.channels
  76. 76● let color_ch = if ch >= 3 { 3 } else { ch }
  77. 77 let n = w * h
  78. 78 let win_size = 2 * radius + 1
  79. 79 let bpp = ch * 2
  80. 80 // 水平模糊:img -> temp
  81. 81● let temp : Array[Int32] = Array::make(n * color_ch, 0)
  82. 82 for y in 0..<h {
  83. 83● for c in 0..<color_ch {
  84. 84● let mut sum = 0
  85. 85 for dx in 0..<win_size {
  86. 86● let x = @types.clamp_coord(dx - radius, w)
  87. 87● sum += read_u16_le_f(img.data, (y * w + x) * bpp + c * 2)
  88. 88 }
  89. 89 for x in 0..<w {
  90. 90● temp[(y * w + x) * color_ch + c] = sum / win_size
  91. 91● let x_out = @types.clamp_coord(x - radius, w)
  92. 92● let x_in = @types.clamp_coord(x + radius + 1, w)
  93. 93 sum = sum -
  94. 94● read_u16_le_f(img.data, (y * w + x_out) * bpp + c * 2) +
  95. 95● read_u16_le_f(img.data, (y * w + x_in) * bpp + c * 2)
  96. 96 }
  97. 97 }
  98. 98 }
  99. 99 // 垂直模糊:temp -> out
  100. 100● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  101. 101 for x in 0..<w {
  102. 102● for c in 0..<color_ch {
  103. 103● let mut sum = 0
  104. 104 for dy in 0..<win_size {
  105. 105● let y = @types.clamp_coord(dy - radius, h)
  106. 106 sum += temp[(y * w + x) * color_ch + c]
  107. 107 }
  108. 108 for y in 0..<h {
  109. 109● write_u16_le_f(out, (y * w + x) * bpp + c * 2, sum / win_size)
  110. 110● let y_out = @types.clamp_coord(y - radius, h)
  111. 111● let y_in = @types.clamp_coord(y + radius + 1, h)
  112. 112 sum = sum -
  113. 113 temp[(y_out * w + x) * color_ch + c] +
  114. 114 temp[(y_in * w + x) * color_ch + c]
  115. 115 }
  116. 116 }
  117. 117 }
  118. 118 // 复制 alpha
  119. 119 if ch == 4 {
  120. 120● for i in 0..<n {
  121. 121● let alpha_off = i * bpp + 6
  122. 122 out[alpha_off] = img.data[alpha_off]
  123. 123 out[alpha_off + 1] = img.data[alpha_off + 1]
  124. 124 }
  125. 125 }
  126. 126 @types.Image16::{
  127. 127 width: w,
  128. 128 height: h,
  129. 129 channels: ch,
  130. 130● data: Bytes::from_array(out),
  131. 131 }
  132. 132}
  133. 133
  134. 134///|
  135. 135/// float 盒模糊。radius 为模糊半径,窗口大小 = 2*radius+1。
  136. 136/// 使用可分离滑动窗口,O(n) 复杂度。仅模糊颜色通道,alpha 不变。
  137. 137/// @see box_blur 8-bit 版本,本函数为 ImageF 变体
  138. 138pub fn box_blur_f(
  139. 139 img : @types.ImageF,
  140. 140 radius : Int32,
  141. 141) -> @types.ImageF raise @types.LoadError {
  142. 142 // 输入验证
  143. 143● if img.width <= 0 || img.height <= 0 {
  144. 144● raise @types.LoadError::DecodeFailed(
  145. 145 "box_blur_f: 图像尺寸必须大于0",
  146. 146 )
  147. 147 }
  148. 148 if img.channels < 1 || img.channels > 4 {
  149. 149● raise @types.LoadError::DecodeFailed(
  150. 150 "box_blur_f: 通道数必须在1-4之间",
  151. 151 )
  152. 152 }
  153. 153 let expected_size = img.width * img.height * img.channels * 4
  154. 154● if img.data.length() != expected_size {
  155. 155● raise @types.LoadError::DecodeFailed(
  156. 156 "box_blur_f: 数据大小不匹配,期望 " +
  157. 157● expected_size.to_string() +
  158. 158 ",实际 " +
  159. 159● img.data.length().to_string(),
  160. 160 )
  161. 161 }
  162. 162 if radius < 0 {
  163. 163● raise @types.LoadError::DecodeFailed("box_blur_f: radius不能为负")
  164. 164 }
  165. 165 if radius <= 0 {
  166. 166● return img
  167. 167 }
  168. 168 let w = img.width
  169. 169 let h = img.height
  170. 170 let ch = img.channels
  171. 171● let color_ch = if ch >= 3 { 3 } else { ch }
  172. 172 let n = w * h
  173. 173 let win_size = 2 * radius + 1
  174. 174 let bpp = ch * 4
  175. 175● let win_size_f = Float::from_int(win_size)
  176. 176 // 水平模糊:img -> temp
  177. 177● let temp : Array[Float] = Array::make(n * color_ch, 0.0F)
  178. 178 for y in 0..<h {
  179. 179● for c in 0..<color_ch {
  180. 180● let mut sum = 0.0F
  181. 181 for dx in 0..<win_size {
  182. 182● let x = @types.clamp_coord(dx - radius, w)
  183. 183● sum += read_f32_le_f(img.data, (y * w + x) * bpp + c * 4)
  184. 184 }
  185. 185 for x in 0..<w {
  186. 186● temp[(y * w + x) * color_ch + c] = sum / win_size_f
  187. 187● let x_out = @types.clamp_coord(x - radius, w)
  188. 188● let x_in = @types.clamp_coord(x + radius + 1, w)
  189. 189 sum = sum -
  190. 190● read_f32_le_f(img.data, (y * w + x_out) * bpp + c * 4) +
  191. 191● read_f32_le_f(img.data, (y * w + x_in) * bpp + c * 4)
  192. 192 }
  193. 193 }
  194. 194 }
  195. 195 // 垂直模糊:temp -> out
  196. 196● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  197. 197 for x in 0..<w {
  198. 198● for c in 0..<color_ch {
  199. 199● let mut sum = 0.0F
  200. 200 for dy in 0..<win_size {
  201. 201● let y = @types.clamp_coord(dy - radius, h)
  202. 202 sum += temp[(y * w + x) * color_ch + c]
  203. 203 }
  204. 204 for y in 0..<h {
  205. 205● write_f32_le_f(out, (y * w + x) * bpp + c * 4, sum / win_size_f)
  206. 206● let y_out = @types.clamp_coord(y - radius, h)
  207. 207● let y_in = @types.clamp_coord(y + radius + 1, h)
  208. 208 sum = sum -
  209. 209 temp[(y_out * w + x) * color_ch + c] +
  210. 210 temp[(y_in * w + x) * color_ch + c]
  211. 211 }
  212. 212 }
  213. 213 }
  214. 214 // 复制 alpha
  215. 215 if ch == 4 {
  216. 216● for i in 0..<n {
  217. 217● let alpha_off = i * bpp + 12
  218. 218 for b in 0..<4 {
  219. 219● out[alpha_off + b] = img.data[alpha_off + b]
  220. 220 }
  221. 221 }
  222. 222 }
  223. 223 @types.ImageF::{
  224. 224 width: w,
  225. 225 height: h,
  226. 226 channels: ch,
  227. 227● data: Bytes::from_array(out),
  228. 228 }
  229. 229}
  230. 230
  231. 231///|
  232. 232/// 16-bit 高斯模糊。radius 为核半径,sigma 为标准差。
  233. 233/// 使用可分离高斯核,O(n*radius) 复杂度。仅模糊颜色通道,alpha 不变。
  234. 234/// @see gaussian_blur 8-bit 版本,本函数为 Image16 变体
  235. 235pub fn gaussian_blur_16(
  236. 236 img : @types.Image16,
  237. 237 radius : Int32,
  238. 238 sigma : Float,
  239. 239) -> @types.Image16 raise @types.LoadError {
  240. 240 // 输入验证
  241. 241● if img.width <= 0 || img.height <= 0 {
  242. 242● raise @types.LoadError::DecodeFailed(
  243. 243 "gaussian_blur_16: 图像尺寸必须大于0",
  244. 244 )
  245. 245 }
  246. 246 if img.channels < 1 || img.channels > 4 {
  247. 247● raise @types.LoadError::DecodeFailed(
  248. 248 "gaussian_blur_16: 通道数必须在1-4之间",
  249. 249 )
  250. 250 }
  251. 251 let expected_size = img.width * img.height * img.channels * 2
  252. 252● if img.data.length() != expected_size {
  253. 253● raise @types.LoadError::DecodeFailed(
  254. 254 "gaussian_blur_16: 数据大小不匹配,期望 " +
  255. 255● expected_size.to_string() +
  256. 256 ",实际 " +
  257. 257● img.data.length().to_string(),
  258. 258 )
  259. 259 }
  260. 260 if radius < 0 {
  261. 261● raise @types.LoadError::DecodeFailed("gaussian_blur_16: radius不能为负")
  262. 262 }
  263. 263 if sigma <= 0.0F {
  264. 264● raise @types.LoadError::DecodeFailed("gaussian_blur_16: sigma必须大于0")
  265. 265 }
  266. 266 if radius <= 0 {
  267. 267● return img
  268. 268 }
  269. 269 let w = img.width
  270. 270 let h = img.height
  271. 271 let ch = img.channels
  272. 272● let color_ch = if ch >= 3 { 3 } else { ch }
  273. 273 let n = w * h
  274. 274 let win_size = 2 * radius + 1
  275. 275 let bpp = ch * 2
  276. 276 // 生成 1D 高斯核
  277. 277● let kernel : Array[Float] = Array::make(win_size, 0.0F)
  278. 278 let two_sigma_sq = 2.0F * sigma * sigma
  279. 279 let mut ksum = 0.0F
  280. 280 for i in 0..<win_size {
  281. 281● let x = Float::from_int(i - radius)
  282. 282● let val = @math.expf(-(x * x) / two_sigma_sq)
  283. 283 kernel[i] = val
  284. 284 ksum += val
  285. 285 }
  286. 286 for i in 0..<win_size {
  287. 287● kernel[i] = kernel[i] / ksum
  288. 288 }
  289. 289 // 预计算像素 Float 数组
  290. 290● let src : Array[Float] = Array::make(n * color_ch, 0.0F)
  291. 291 for i in 0..<n {
  292. 292● for c in 0..<color_ch {
  293. 293● src[i * color_ch + c] = Float::from_int(
  294. 294● read_u16_le_f(img.data, i * bpp + c * 2),
  295. 295 )
  296. 296 }
  297. 297 }
  298. 298 // 水平模糊:src -> temp (Float)
  299. 299● let temp : Array[Float] = Array::make(n * color_ch, 0.0F)
  300. 300◐ let x_inner_start = if radius >= w { w } else { radius }
  301. 301◐ let x_inner_end = if radius >= w { w } else { w - radius }
  302. 302 for y in 0..<h {
  303. 303● let row_base = y * w
  304. 304 for c in 0..<color_ch {
  305. 305● for x in x_inner_start..<x_inner_end {
  306. 306● let mut acc = 0.0F
  307. 307 let base = (row_base + x - radius) * color_ch + c
  308. 308 for k in 0..<win_size {
  309. 309● acc += kernel[k] * src[base + k * color_ch]
  310. 310 }
  311. 311 temp[(row_base + x) * color_ch + c] = acc
  312. 312 }
  313. 313 for x in 0..<x_inner_start {
  314. 314● let mut acc = 0.0F
  315. 315 for k in 0..<win_size {
  316. 316● let sx = @types.clamp_coord(x + k - radius, w)
  317. 317 acc += kernel[k] * src[(row_base + sx) * color_ch + c]
  318. 318 }
  319. 319 temp[(row_base + x) * color_ch + c] = acc
  320. 320 }
  321. 321 for x in x_inner_end..<w {
  322. 322● let mut acc = 0.0F
  323. 323 for k in 0..<win_size {
  324. 324● let sx = @types.clamp_coord(x + k - radius, w)
  325. 325 acc += kernel[k] * src[(row_base + sx) * color_ch + c]
  326. 326 }
  327. 327 temp[(row_base + x) * color_ch + c] = acc
  328. 328 }
  329. 329 }
  330. 330 }
  331. 331 // 垂直模糊:temp -> out
  332. 332● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  333. 333◐ let y_inner_start = if radius >= h { h } else { radius }
  334. 334◐ let y_inner_end = if radius >= h { h } else { h - radius }
  335. 335 for x in 0..<w {
  336. 336● for c in 0..<color_ch {
  337. 337● for y in y_inner_start..<y_inner_end {
  338. 338● let mut acc = 0.0F
  339. 339 let base = ((y - radius) * w + x) * color_ch + c
  340. 340 for k in 0..<win_size {
  341. 341● acc += kernel[k] * temp[base + k * w * color_ch]
  342. 342 }
  343. 343● let v = (acc + 0.5F).to_int().clamp(min=0, max=65535)
  344. 344● write_u16_le_f(out, (y * w + x) * bpp + c * 2, v)
  345. 345 }
  346. 346 for y in 0..<y_inner_start {
  347. 347● let mut acc = 0.0F
  348. 348 for k in 0..<win_size {
  349. 349● let sy = @types.clamp_coord(y + k - radius, h)
  350. 350 acc += kernel[k] * temp[(sy * w + x) * color_ch + c]
  351. 351 }
  352. 352● let v = (acc + 0.5F).to_int().clamp(min=0, max=65535)
  353. 353● write_u16_le_f(out, (y * w + x) * bpp + c * 2, v)
  354. 354 }
  355. 355 for y in y_inner_end..<h {
  356. 356● let mut acc = 0.0F
  357. 357 for k in 0..<win_size {
  358. 358● let sy = @types.clamp_coord(y + k - radius, h)
  359. 359 acc += kernel[k] * temp[(sy * w + x) * color_ch + c]
  360. 360 }
  361. 361● let v = (acc + 0.5F).to_int().clamp(min=0, max=65535)
  362. 362● write_u16_le_f(out, (y * w + x) * bpp + c * 2, v)
  363. 363 }
  364. 364 }
  365. 365 }
  366. 366 // 复制 alpha
  367. 367 if ch == 4 {
  368. 368● for i in 0..<n {
  369. 369● let alpha_off = i * bpp + 6
  370. 370 out[alpha_off] = img.data[alpha_off]
  371. 371 out[alpha_off + 1] = img.data[alpha_off + 1]
  372. 372 }
  373. 373 }
  374. 374 @types.Image16::{
  375. 375 width: w,
  376. 376 height: h,
  377. 377 channels: ch,
  378. 378● data: Bytes::from_array(out),
  379. 379 }
  380. 380}
  381. 381
  382. 382///|
  383. 383/// float 高斯模糊。radius 为核半径,sigma 为标准差。
  384. 384/// 使用可分离高斯核,O(n*radius) 复杂度。仅模糊颜色通道,alpha 不变。
  385. 385/// @see gaussian_blur 8-bit 版本,本函数为 ImageF 变体
  386. 386pub fn gaussian_blur_f(
  387. 387 img : @types.ImageF,
  388. 388 radius : Int32,
  389. 389 sigma : Float,
  390. 390) -> @types.ImageF raise @types.LoadError {
  391. 391 // 输入验证
  392. 392● if img.width <= 0 || img.height <= 0 {
  393. 393● raise @types.LoadError::DecodeFailed(
  394. 394 "gaussian_blur_f: 图像尺寸必须大于0",
  395. 395 )
  396. 396 }
  397. 397 if img.channels < 1 || img.channels > 4 {
  398. 398● raise @types.LoadError::DecodeFailed(
  399. 399 "gaussian_blur_f: 通道数必须在1-4之间",
  400. 400 )
  401. 401 }
  402. 402 let expected_size = img.width * img.height * img.channels * 4
  403. 403● if img.data.length() != expected_size {
  404. 404● raise @types.LoadError::DecodeFailed(
  405. 405 "gaussian_blur_f: 数据大小不匹配,期望 " +
  406. 406● expected_size.to_string() +
  407. 407 ",实际 " +
  408. 408● img.data.length().to_string(),
  409. 409 )
  410. 410 }
  411. 411 if radius < 0 {
  412. 412○ raise @types.LoadError::DecodeFailed("gaussian_blur_f: radius不能为负")
  413. 413 }
  414. 414 if sigma <= 0.0F {
  415. 415● raise @types.LoadError::DecodeFailed("gaussian_blur_f: sigma必须大于0")
  416. 416 }
  417. 417 if radius <= 0 {
  418. 418● return img
  419. 419 }
  420. 420 let w = img.width
  421. 421 let h = img.height
  422. 422 let ch = img.channels
  423. 423● let color_ch = if ch >= 3 { 3 } else { ch }
  424. 424 let n = w * h
  425. 425 let win_size = 2 * radius + 1
  426. 426 let bpp = ch * 4
  427. 427 // 生成 1D 高斯核
  428. 428● let kernel : Array[Float] = Array::make(win_size, 0.0F)
  429. 429 let two_sigma_sq = 2.0F * sigma * sigma
  430. 430 let mut ksum = 0.0F
  431. 431 for i in 0..<win_size {
  432. 432● let x = Float::from_int(i - radius)
  433. 433● let val = @math.expf(-(x * x) / two_sigma_sq)
  434. 434 kernel[i] = val
  435. 435 ksum += val
  436. 436 }
  437. 437 for i in 0..<win_size {
  438. 438● kernel[i] = kernel[i] / ksum
  439. 439 }
  440. 440 // 预计算像素 Float 数组
  441. 441● let src : Array[Float] = Array::make(n * color_ch, 0.0F)
  442. 442 for i in 0..<n {
  443. 443● for c in 0..<color_ch {
  444. 444● src[i * color_ch + c] = read_f32_le_f(img.data, i * bpp + c * 4)
  445. 445 }
  446. 446 }
  447. 447 // 水平模糊:src -> temp (Float)
  448. 448● let temp : Array[Float] = Array::make(n * color_ch, 0.0F)
  449. 449◐ let x_inner_start = if radius >= w { w } else { radius }
  450. 450◐ let x_inner_end = if radius >= w { w } else { w - radius }
  451. 451 for y in 0..<h {
  452. 452● let row_base = y * w
  453. 453 for c in 0..<color_ch {
  454. 454● for x in x_inner_start..<x_inner_end {
  455. 455● let mut acc = 0.0F
  456. 456 let base = (row_base + x - radius) * color_ch + c
  457. 457 for k in 0..<win_size {
  458. 458● acc += kernel[k] * src[base + k * color_ch]
  459. 459 }
  460. 460 temp[(row_base + x) * color_ch + c] = acc
  461. 461 }
  462. 462 for x in 0..<x_inner_start {
  463. 463● let mut acc = 0.0F
  464. 464 for k in 0..<win_size {
  465. 465● let sx = @types.clamp_coord(x + k - radius, w)
  466. 466 acc += kernel[k] * src[(row_base + sx) * color_ch + c]
  467. 467 }
  468. 468 temp[(row_base + x) * color_ch + c] = acc
  469. 469 }
  470. 470 for x in x_inner_end..<w {
  471. 471● let mut acc = 0.0F
  472. 472 for k in 0..<win_size {
  473. 473● let sx = @types.clamp_coord(x + k - radius, w)
  474. 474 acc += kernel[k] * src[(row_base + sx) * color_ch + c]
  475. 475 }
  476. 476 temp[(row_base + x) * color_ch + c] = acc
  477. 477 }
  478. 478 }
  479. 479 }
  480. 480 // 垂直模糊:temp -> out
  481. 481● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  482. 482◐ let y_inner_start = if radius >= h { h } else { radius }
  483. 483◐ let y_inner_end = if radius >= h { h } else { h - radius }
  484. 484 for x in 0..<w {
  485. 485● for c in 0..<color_ch {
  486. 486● for y in y_inner_start..<y_inner_end {
  487. 487● let mut acc = 0.0F
  488. 488 let base = ((y - radius) * w + x) * color_ch + c
  489. 489 for k in 0..<win_size {
  490. 490● acc += kernel[k] * temp[base + k * w * color_ch]
  491. 491 }
  492. 492● write_f32_le_f(out, (y * w + x) * bpp + c * 4, acc)
  493. 493 }
  494. 494 for y in 0..<y_inner_start {
  495. 495● let mut acc = 0.0F
  496. 496 for k in 0..<win_size {
  497. 497● let sy = @types.clamp_coord(y + k - radius, h)
  498. 498 acc += kernel[k] * temp[(sy * w + x) * color_ch + c]
  499. 499 }
  500. 500● write_f32_le_f(out, (y * w + x) * bpp + c * 4, acc)
  501. 501 }
  502. 502 for y in y_inner_end..<h {
  503. 503● let mut acc = 0.0F
  504. 504 for k in 0..<win_size {
  505. 505● let sy = @types.clamp_coord(y + k - radius, h)
  506. 506 acc += kernel[k] * temp[(sy * w + x) * color_ch + c]
  507. 507 }
  508. 508● write_f32_le_f(out, (y * w + x) * bpp + c * 4, acc)
  509. 509 }
  510. 510 }
  511. 511 }
  512. 512 // 复制 alpha
  513. 513 if ch == 4 {
  514. 514● for i in 0..<n {
  515. 515● let alpha_off = i * bpp + 12
  516. 516 for b in 0..<4 {
  517. 517● out[alpha_off + b] = img.data[alpha_off + b]
  518. 518 }
  519. 519 }
  520. 520 }
  521. 521 @types.ImageF::{
  522. 522 width: w,
  523. 523 height: h,
  524. 524 channels: ch,
  525. 525● data: Bytes::from_array(out),
  526. 526 }
  527. 527}
  528. 528
  529. 529///|
  530. 530/// 16-bit 中值滤波。ksize 为窗口大小(奇数)。有效去除椒盐噪声。
  531. 531/// 仅滤波颜色通道,alpha 不变。
  532. 532/// @see median_blur 8-bit 版本,本函数为 Image16 变体
  533. 533pub fn median_blur_16(
  534. 534 img : @types.Image16,
  535. 535 ksize : Int32,
  536. 536) -> @types.Image16 raise @types.LoadError {
  537. 537 // 输入验证
  538. 538● if img.width <= 0 || img.height <= 0 {
  539. 539● raise @types.LoadError::DecodeFailed(
  540. 540 "median_blur_16: 图像尺寸必须大于0",
  541. 541 )
  542. 542 }
  543. 543 if img.channels < 1 || img.channels > 4 {
  544. 544● raise @types.LoadError::DecodeFailed(
  545. 545 "median_blur_16: 通道数必须在1-4之间",
  546. 546 )
  547. 547 }
  548. 548 let expected_size = img.width * img.height * img.channels * 2
  549. 549● if img.data.length() != expected_size {
  550. 550● raise @types.LoadError::DecodeFailed(
  551. 551 "median_blur_16: 数据大小不匹配,期望 " +
  552. 552● expected_size.to_string() +
  553. 553 ",实际 " +
  554. 554● img.data.length().to_string(),
  555. 555 )
  556. 556 }
  557. 557 if ksize <= 0 {
  558. 558● raise @types.LoadError::DecodeFailed("median_blur_16: ksize必须大于0")
  559. 559 }
  560. 560 if ksize % 2 == 0 {
  561. 561● raise @types.LoadError::DecodeFailed("median_blur_16: ksize必须为奇数")
  562. 562 }
  563. 563 if ksize <= 1 {
  564. 564● return img
  565. 565 }
  566. 566 let radius = ksize / 2
  567. 567 let w = img.width
  568. 568 let h = img.height
  569. 569 let ch = img.channels
  570. 570● let color_ch = if ch >= 3 { 3 } else { ch }
  571. 571 let n = w * h
  572. 572 let bpp = ch * 2
  573. 573● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  574. 574● let buf : Array[Int32] = Array::make(ksize * ksize, 0)
  575. 575 for y in 0..<h {
  576. 576● for x in 0..<w {
  577. 577● for c in 0..<color_ch {
  578. 578● let mut idx = 0
  579. 579 for dy in -radius..<=radius {
  580. 580● for dx in -radius..<=radius {
  581. 581● let sx = @types.clamp_coord(x + dx, w)
  582. 582● let sy = @types.clamp_coord(y + dy, h)
  583. 583● buf[idx] = read_u16_le_f(img.data, (sy * w + sx) * bpp + c * 2)
  584. 584 idx += 1
  585. 585 }
  586. 586 }
  587. 587 let mid = ksize * ksize / 2
  588. 588 let mut lo = 0
  589. 589 let mut hi = ksize * ksize - 1
  590. 590 while lo < hi {
  591. 591● let pivot = buf[(lo + hi) / 2]
  592. 592 let mut i = lo
  593. 593 let mut j = hi
  594. 594 while i <= j {
  595. 595● while buf[i] < pivot {
  596. 596● i += 1
  597. 597 }
  598. 598 while buf[j] > pivot {
  599. 599● j -= 1
  600. 600 }
  601. 601 if i <= j {
  602. 602● let tmp = buf[i]
  603. 603 buf[i] = buf[j]
  604. 604 buf[j] = tmp
  605. 605 i += 1
  606. 606 j -= 1
  607. 607 }
  608. 608 }
  609. 609 if mid <= j {
  610. 610● hi = j
  611. 611● } else if mid >= i {
  612. 612● lo = i
  613. 613 } else {
  614. 614● lo = mid
  615. 615 hi = mid
  616. 616 }
  617. 617 }
  618. 618● write_u16_le_f(out, (y * w + x) * bpp + c * 2, buf[mid])
  619. 619 }
  620. 620 }
  621. 621 }
  622. 622 // 复制 alpha
  623. 623 if ch == 4 {
  624. 624● for i in 0..<n {
  625. 625● let alpha_off = i * bpp + 6
  626. 626 out[alpha_off] = img.data[alpha_off]
  627. 627 out[alpha_off + 1] = img.data[alpha_off + 1]
  628. 628 }
  629. 629 }
  630. 630 @types.Image16::{
  631. 631 width: w,
  632. 632 height: h,
  633. 633 channels: ch,
  634. 634● data: Bytes::from_array(out),
  635. 635 }
  636. 636}
  637. 637
  638. 638///|
  639. 639/// float 中值滤波。ksize 为窗口大小(奇数)。有效去除椒盐噪声。
  640. 640/// 仅滤波颜色通道,alpha 不变。
  641. 641/// @see median_blur 8-bit 版本,本函数为 ImageF 变体
  642. 642pub fn median_blur_f(
  643. 643 img : @types.ImageF,
  644. 644 ksize : Int32,
  645. 645) -> @types.ImageF raise @types.LoadError {
  646. 646 // 输入验证
  647. 647● if img.width <= 0 || img.height <= 0 {
  648. 648● raise @types.LoadError::DecodeFailed(
  649. 649 "median_blur_f: 图像尺寸必须大于0",
  650. 650 )
  651. 651 }
  652. 652 if img.channels < 1 || img.channels > 4 {
  653. 653● raise @types.LoadError::DecodeFailed(
  654. 654 "median_blur_f: 通道数必须在1-4之间",
  655. 655 )
  656. 656 }
  657. 657 let expected_size = img.width * img.height * img.channels * 4
  658. 658● if img.data.length() != expected_size {
  659. 659● raise @types.LoadError::DecodeFailed(
  660. 660 "median_blur_f: 数据大小不匹配,期望 " +
  661. 661● expected_size.to_string() +
  662. 662 ",实际 " +
  663. 663● img.data.length().to_string(),
  664. 664 )
  665. 665 }
  666. 666 if ksize <= 0 {
  667. 667● raise @types.LoadError::DecodeFailed("median_blur_f: ksize必须大于0")
  668. 668 }
  669. 669 if ksize % 2 == 0 {
  670. 670● raise @types.LoadError::DecodeFailed("median_blur_f: ksize必须为奇数")
  671. 671 }
  672. 672 if ksize <= 1 {
  673. 673● return img
  674. 674 }
  675. 675 let radius = ksize / 2
  676. 676 let w = img.width
  677. 677 let h = img.height
  678. 678 let ch = img.channels
  679. 679● let color_ch = if ch >= 3 { 3 } else { ch }
  680. 680 let n = w * h
  681. 681 let bpp = ch * 4
  682. 682● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  683. 683● let buf : Array[Float] = Array::make(ksize * ksize, 0.0F)
  684. 684 for y in 0..<h {
  685. 685● for x in 0..<w {
  686. 686● for c in 0..<color_ch {
  687. 687● let mut idx = 0
  688. 688 for dy in -radius..<=radius {
  689. 689● for dx in -radius..<=radius {
  690. 690● let sx = @types.clamp_coord(x + dx, w)
  691. 691● let sy = @types.clamp_coord(y + dy, h)
  692. 692● buf[idx] = read_f32_le_f(img.data, (sy * w + sx) * bpp + c * 4)
  693. 693 idx += 1
  694. 694 }
  695. 695 }
  696. 696 // 完整排序找中值(Float 需要完整排序)
  697. 697 let total = ksize * ksize
  698. 698 for i in 0..<total {
  699. 699● for j in 0..<(total - i - 1) {
  700. 700● if buf[j] > buf[j + 1] {
  701. 701● let tmp = buf[j]
  702. 702 buf[j] = buf[j + 1]
  703. 703 buf[j + 1] = tmp
  704. 704 }
  705. 705 }
  706. 706 }
  707. 707● write_f32_le_f(out, (y * w + x) * bpp + c * 4, buf[total / 2])
  708. 708 }
  709. 709 }
  710. 710 }
  711. 711 // 复制 alpha
  712. 712 if ch == 4 {
  713. 713● for i in 0..<n {
  714. 714● let alpha_off = i * bpp + 12
  715. 715 for b in 0..<4 {
  716. 716● out[alpha_off + b] = img.data[alpha_off + b]
  717. 717 }
  718. 718 }
  719. 719 }
  720. 720 @types.ImageF::{
  721. 721 width: w,
  722. 722 height: h,
  723. 723 channels: ch,
  724. 724● data: Bytes::from_array(out),
  725. 725 }
  726. 726}
  727. 727
  728. 728///|
  729. 729/// 16-bit 锐化。amount 控制锐化强度(0.0=原图,1.0=强锐化)。
  730. 730/// @see sharpen 8-bit 版本,本函数为 Image16 变体
  731. 731pub fn sharpen_16(
  732. 732 img : @types.Image16,
  733. 733 amount : Float,
  734. 734) -> @types.Image16 raise @types.LoadError {
  735. 735 // 输入验证
  736. 736● if img.width <= 0 || img.height <= 0 {
  737. 737● raise @types.LoadError::DecodeFailed(
  738. 738 "sharpen_16: 图像尺寸必须大于0",
  739. 739 )
  740. 740 }
  741. 741 if img.channels < 1 || img.channels > 4 {
  742. 742● raise @types.LoadError::DecodeFailed(
  743. 743 "sharpen_16: 通道数必须在1-4之间",
  744. 744 )
  745. 745 }
  746. 746 let expected_size = img.width * img.height * img.channels * 2
  747. 747● if img.data.length() != expected_size {
  748. 748● raise @types.LoadError::DecodeFailed(
  749. 749 "sharpen_16: 数据大小不匹配,期望 " +
  750. 750● expected_size.to_string() +
  751. 751 ",实际 " +
  752. 752● img.data.length().to_string(),
  753. 753 )
  754. 754 }
  755. 755 if amount < 0.0F {
  756. 756○ raise @types.LoadError::DecodeFailed("sharpen_16: amount不能为负")
  757. 757 }
  758. 758
  759. 759 let w = img.width
  760. 760 let h = img.height
  761. 761 let ch = img.channels
  762. 762● let color_ch = if ch >= 3 { 3 } else { ch }
  763. 763 let n = w * h
  764. 764 let bpp = ch * 2
  765. 765 let center = 1.0F + 4.0F * amount
  766. 766● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  767. 767● let px : Array[Int32] = Array::make(n * ch, 0)
  768. 768 for i in 0..<n {
  769. 769● for c in 0..<ch {
  770. 770● px[i * ch + c] = read_u16_le_f(img.data, i * bpp + c * 2)
  771. 771 }
  772. 772 }
  773. 773 for y in 1..<(h - 1) {
  774. 774● for x in 1..<(w - 1) {
  775. 775● let idx = y * w + x
  776. 776 for c in 0..<color_ch {
  777. 777● let cx = px[idx * ch + c]
  778. 778 let left = px[(idx - 1) * ch + c]
  779. 779 let right = px[(idx + 1) * ch + c]
  780. 780 let top = px[(idx - w) * ch + c]
  781. 781 let bottom = px[(idx + w) * ch + c]
  782. 782● let val = Float::from_int(cx) * center -
  783. 783● amount * Float::from_int(left + right + top + bottom)
  784. 784● let new_val = (val + 0.5F).to_int()
  785. 785 let clamped = if new_val < 0 {
  786. 786● 0
  787. 787● } else if new_val > 65535 {
  788. 788● 65535
  789. 789 } else {
  790. 790● new_val
  791. 791 }
  792. 792● write_u16_le_f(out, idx * bpp + c * 2, clamped)
  793. 793 }
  794. 794 }
  795. 795 }
  796. 796 for y in 0..<h {
  797. 797● for x in 0..<w {
  798. 798● if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 {
  799. 799● continue
  800. 800 }
  801. 801 let idx = y * w + x
  802. 802● let x_l = @types.clamp_coord(x - 1, w)
  803. 803● let x_r = @types.clamp_coord(x + 1, w)
  804. 804● let y_t = @types.clamp_coord(y - 1, h)
  805. 805● let y_b = @types.clamp_coord(y + 1, h)
  806. 806 for c in 0..<color_ch {
  807. 807● let cx = px[idx * ch + c]
  808. 808 let left = px[(y * w + x_l) * ch + c]
  809. 809 let right = px[(y * w + x_r) * ch + c]
  810. 810 let top = px[(y_t * w + x) * ch + c]
  811. 811 let bottom = px[(y_b * w + x) * ch + c]
  812. 812● let val = Float::from_int(cx) * center -
  813. 813● amount * Float::from_int(left + right + top + bottom)
  814. 814● let new_val = (val + 0.5F).to_int()
  815. 815 let clamped = if new_val < 0 {
  816. 816● 0
  817. 817● } else if new_val > 65535 {
  818. 818● 65535
  819. 819 } else {
  820. 820● new_val
  821. 821 }
  822. 822● write_u16_le_f(out, idx * bpp + c * 2, clamped)
  823. 823 }
  824. 824 }
  825. 825 }
  826. 826 if ch == 4 {
  827. 827● for i in 0..<n {
  828. 828● let alpha_off = i * bpp + 6
  829. 829 out[alpha_off] = img.data[alpha_off]
  830. 830 out[alpha_off + 1] = img.data[alpha_off + 1]
  831. 831 }
  832. 832 }
  833. 833 @types.Image16::{
  834. 834 width: w,
  835. 835 height: h,
  836. 836 channels: ch,
  837. 837● data: Bytes::from_array(out),
  838. 838 }
  839. 839}
  840. 840
  841. 841///|
  842. 842/// float 锐化。amount 控制锐化强度(0.0=原图,1.0=强锐化)。
  843. 843/// @see sharpen 8-bit 版本,本函数为 ImageF 变体
  844. 844pub fn sharpen_f(
  845. 845 img : @types.ImageF,
  846. 846 amount : Float,
  847. 847) -> @types.ImageF raise @types.LoadError {
  848. 848 // 输入验证
  849. 849● if img.width <= 0 || img.height <= 0 {
  850. 850● raise @types.LoadError::DecodeFailed("sharpen_f: 图像尺寸必须大于0")
  851. 851 }
  852. 852 if img.channels < 1 || img.channels > 4 {
  853. 853● raise @types.LoadError::DecodeFailed(
  854. 854 "sharpen_f: 通道数必须在1-4之间",
  855. 855 )
  856. 856 }
  857. 857 let expected_size = img.width * img.height * img.channels * 4
  858. 858● if img.data.length() != expected_size {
  859. 859● raise @types.LoadError::DecodeFailed(
  860. 860 "sharpen_f: 数据大小不匹配,期望 " +
  861. 861● expected_size.to_string() +
  862. 862 ",实际 " +
  863. 863● img.data.length().to_string(),
  864. 864 )
  865. 865 }
  866. 866 if amount < 0.0F {
  867. 867○ raise @types.LoadError::DecodeFailed("sharpen_f: amount不能为负")
  868. 868 }
  869. 869
  870. 870 let w = img.width
  871. 871 let h = img.height
  872. 872 let ch = img.channels
  873. 873● let color_ch = if ch >= 3 { 3 } else { ch }
  874. 874 let n = w * h
  875. 875 let bpp = ch * 4
  876. 876 let center = 1.0F + 4.0F * amount
  877. 877● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  878. 878● let px : Array[Float] = Array::make(n * ch, 0.0F)
  879. 879 for i in 0..<n {
  880. 880● for c in 0..<ch {
  881. 881● px[i * ch + c] = read_f32_le_f(img.data, i * bpp + c * 4)
  882. 882 }
  883. 883 }
  884. 884 for y in 1..<(h - 1) {
  885. 885● for x in 1..<(w - 1) {
  886. 886● let idx = y * w + x
  887. 887 for c in 0..<color_ch {
  888. 888● let cx = px[idx * ch + c]
  889. 889 let left = px[(idx - 1) * ch + c]
  890. 890 let right = px[(idx + 1) * ch + c]
  891. 891 let top = px[(idx - w) * ch + c]
  892. 892 let bottom = px[(idx + w) * ch + c]
  893. 893 let val = cx * center - amount * (left + right + top + bottom)
  894. 894● write_f32_le_f(out, idx * bpp + c * 4, val)
  895. 895 }
  896. 896 }
  897. 897 }
  898. 898 for y in 0..<h {
  899. 899● for x in 0..<w {
  900. 900● if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 {
  901. 901● continue
  902. 902 }
  903. 903 let idx = y * w + x
  904. 904● let x_l = @types.clamp_coord(x - 1, w)
  905. 905● let x_r = @types.clamp_coord(x + 1, w)
  906. 906● let y_t = @types.clamp_coord(y - 1, h)
  907. 907● let y_b = @types.clamp_coord(y + 1, h)
  908. 908 for c in 0..<color_ch {
  909. 909● let cx = px[idx * ch + c]
  910. 910 let left = px[(y * w + x_l) * ch + c]
  911. 911 let right = px[(y * w + x_r) * ch + c]
  912. 912 let top = px[(y_t * w + x) * ch + c]
  913. 913 let bottom = px[(y_b * w + x) * ch + c]
  914. 914 let val = cx * center - amount * (left + right + top + bottom)
  915. 915● write_f32_le_f(out, idx * bpp + c * 4, val)
  916. 916 }
  917. 917 }
  918. 918 }
  919. 919 if ch == 4 {
  920. 920● for i in 0..<n {
  921. 921● let alpha_off = i * bpp + 12
  922. 922 for b in 0..<4 {
  923. 923● out[alpha_off + b] = img.data[alpha_off + b]
  924. 924 }
  925. 925 }
  926. 926 }
  927. 927 @types.ImageF::{
  928. 928 width: w,
  929. 929 height: h,
  930. 930 channels: ch,
  931. 931● data: Bytes::from_array(out),
  932. 932 }
  933. 933}