image.process\frequency\frequency_16f.mbt

98.8% · 159 / 161 points · 450 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 变体 Frequency domain transforms 16-bit/float variants
  3. 3///
  4. 4/// 16位 FFT/DCT/Haar 小波 fft_2d_16 / dct_2d_16 / haar_wavelet_16
  5. 5/// 16位/浮点 FFT/DCT/Haar 小波 16-bit/float FFT/DCT/Haar wavelet
  6. 6// 频域变换 16-bit/float 变体
  7. 7// FFT / DCT / Haar 支持 Image16 / ImageF
  8. 8
  9. 9// ========== 辅助函数 ==========
  10. 10
  11. 11///|
  12. 12fn read_u16_le_freq(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_freq(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_freq(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_freq(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// ========== FFT 16-bit ==========
  42. 42
  43. 43///|
  44. 44/// 16-bit 2D FFT。输入归一化到 0-1 后变换。
  45. 45/// @see fft_2d 8-bit 版本,本函数为 Image16 变体
  46. 46pub fn fft_2d_16(
  47. 47 img : @types.Image16,
  48. 48) -> Array[FFTResult] raise @types.LoadError {
  49. 49● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  50. 50● raise @types.LoadError::DecodeFailed("fft_2d_16 仅支持 channels=1/3/4")
  51. 51 }
  52. 52 let ch = img.channels
  53. 53◐ let color_ch = if ch >= 3 { 3 } else { 1 }
  54. 54 let w = img.width
  55. 55 let h = img.height
  56. 56 let bpp = ch * 2
  57. 57 // 转换为 8-bit 灰度图(归一化)
  58. 58● let data8 : Array[Byte] = Array::make(w * h * color_ch, b'\x00')
  59. 59 for i in 0..<(w * h) {
  60. 60● for c in 0..<color_ch {
  61. 61● let v16 = read_u16_le_freq(img.data, i * bpp + c * 2)
  62. 62● data8[i * color_ch + c] = (v16 / 257).to_byte()
  63. 63 }
  64. 64 }
  65. 65 let img8 = @types.Image::{
  66. 66 width: w,
  67. 67 height: h,
  68. 68 channels: color_ch,
  69. 69● data: Bytes::from_array(data8),
  70. 70 }
  71. 71● fft_2d(img8)
  72. 72}
  73. 73
  74. 74///|
  75. 75/// 16-bit 逆 FFT。输出 Image16(0-65535)。
  76. 76/// @see ifft_2d 8-bit 版本,本函数为 Image16 变体
  77. 77pub fn ifft_2d_16(fft : FFTResult) -> @types.Image16 {
  78. 78● let img8 = ifft_2d(fft)
  79. 79 let w = img8.width
  80. 80 let h = img8.height
  81. 81 let ch = img8.channels
  82. 82● let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  83. 83 for i in 0..<(w * h * ch) {
  84. 84● let v8 = img8.data[i].to_int()
  85. 85● write_u16_le_freq(out, 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(out),
  92. 92 }
  93. 93}
  94. 94
  95. 95///|
  96. 96/// 16-bit FFT 幅度谱。输出 Image16。
  97. 97/// @see fft_magnitude 8-bit 版本,本函数为 Image16 变体
  98. 98pub fn fft_magnitude_16(fft : FFTResult, log_scale : Bool) -> @types.Image16 {
  99. 99● let img8 = fft_magnitude(fft, log_scale)
  100. 100 let w = img8.width
  101. 101 let h = img8.height
  102. 102 let ch = img8.channels
  103. 103● let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  104. 104 for i in 0..<(w * h * ch) {
  105. 105● let v8 = img8.data[i].to_int()
  106. 106● write_u16_le_freq(out, i * 2, v8 * 257)
  107. 107 }
  108. 108 @types.Image16::{
  109. 109 width: w,
  110. 110 height: h,
  111. 111 channels: ch,
  112. 112● data: Bytes::from_array(out),
  113. 113 }
  114. 114}
  115. 115
  116. 116// ========== FFT float ==========
  117. 117
  118. 118///|
  119. 119/// float 2D FFT。输入直接使用 float 值。
  120. 120/// @see fft_2d 8-bit 版本,本函数为 ImageF 变体
  121. 121pub fn fft_2d_f(img : @types.ImageF) -> Array[FFTResult] raise @types.LoadError {
  122. 122● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  123. 123● raise @types.LoadError::DecodeFailed("fft_2d_f 仅支持 channels=1/3/4")
  124. 124 }
  125. 125 let ch = img.channels
  126. 126◐ let color_ch = if ch >= 3 { 3 } else { 1 }
  127. 127 let w = img.width
  128. 128 let h = img.height
  129. 129 let bpp = ch * 4
  130. 130 // 转换为 8-bit(归一化到 0-255)
  131. 131● let data8 : Array[Byte] = Array::make(w * h * color_ch, b'\x00')
  132. 132 for i in 0..<(w * h) {
  133. 133● for c in 0..<color_ch {
  134. 134● let vf = read_float_le_freq(img.data, i * bpp + c * 4)
  135. 135● let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255)
  136. 136● data8[i * color_ch + c] = v8.to_byte()
  137. 137 }
  138. 138 }
  139. 139 let img8 = @types.Image::{
  140. 140 width: w,
  141. 141 height: h,
  142. 142 channels: color_ch,
  143. 143● data: Bytes::from_array(data8),
  144. 144 }
  145. 145● fft_2d(img8)
  146. 146}
  147. 147
  148. 148///|
  149. 149/// float 逆 FFT。输出 ImageF(0-1)。
  150. 150/// @see ifft_2d 8-bit 版本,本函数为 ImageF 变体
  151. 151pub fn ifft_2d_f(fft : FFTResult) -> @types.ImageF {
  152. 152● let img8 = ifft_2d(fft)
  153. 153 let w = img8.width
  154. 154 let h = img8.height
  155. 155 let ch = img8.channels
  156. 156● let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  157. 157 for i in 0..<(w * h * ch) {
  158. 158● let v8 = img8.data[i].to_int()
  159. 159● write_f32_le_freq(out, i * 4, Float::from_int(v8) / 255.0F)
  160. 160 }
  161. 161 @types.ImageF::{
  162. 162 width: w,
  163. 163 height: h,
  164. 164 channels: ch,
  165. 165● data: Bytes::from_array(out),
  166. 166 }
  167. 167}
  168. 168
  169. 169///|
  170. 170/// float FFT 幅度谱。输出 ImageF。
  171. 171/// @see fft_magnitude 8-bit 版本,本函数为 ImageF 变体
  172. 172pub fn fft_magnitude_f(fft : FFTResult, log_scale : Bool) -> @types.ImageF {
  173. 173● let img8 = fft_magnitude(fft, log_scale)
  174. 174 let w = img8.width
  175. 175 let h = img8.height
  176. 176 let ch = img8.channels
  177. 177● let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  178. 178 for i in 0..<(w * h * ch) {
  179. 179● let v8 = img8.data[i].to_int()
  180. 180● write_f32_le_freq(out, i * 4, Float::from_int(v8) / 255.0F)
  181. 181 }
  182. 182 @types.ImageF::{
  183. 183 width: w,
  184. 184 height: h,
  185. 185 channels: ch,
  186. 186● data: Bytes::from_array(out),
  187. 187 }
  188. 188}
  189. 189
  190. 190// ========== DCT 16-bit ==========
  191. 191
  192. 192///|
  193. 193/// 16-bit 2D DCT。输入归一化后变换。
  194. 194/// @see dct_2d 8-bit 版本,本函数为 Image16 变体
  195. 195pub fn dct_2d_16(img : @types.Image16) -> Array[Array[Double]] {
  196. 196● let w = img.width
  197. 197 let h = img.height
  198. 198 let ch = img.channels
  199. 199 // 转换为 8-bit
  200. 200● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  201. 201 for i in 0..<(w * h * ch) {
  202. 202● let v16 = read_u16_le_freq(img.data, i * 2)
  203. 203● data8[i] = (v16 / 257).to_byte()
  204. 204 }
  205. 205 let img8 = @types.Image::{
  206. 206 width: w,
  207. 207 height: h,
  208. 208 channels: ch,
  209. 209● data: Bytes::from_array(data8),
  210. 210 }
  211. 211● dct_2d(img8)
  212. 212}
  213. 213
  214. 214///|
  215. 215/// 16-bit 逆 DCT。输出 Image16。
  216. 216/// @see idct_2d 8-bit 版本,本函数为 Image16 变体
  217. 217pub fn idct_2d_16(coeffs : Array[Array[Double]]) -> @types.Image16 {
  218. 218● let img8 = idct_2d(coeffs)
  219. 219 let w = img8.width
  220. 220 let h = img8.height
  221. 221 let ch = img8.channels
  222. 222● let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  223. 223 for i in 0..<(w * h * ch) {
  224. 224● let v8 = img8.data[i].to_int()
  225. 225● write_u16_le_freq(out, i * 2, v8 * 257)
  226. 226 }
  227. 227 @types.Image16::{
  228. 228 width: w,
  229. 229 height: h,
  230. 230 channels: ch,
  231. 231● data: Bytes::from_array(out),
  232. 232 }
  233. 233}
  234. 234
  235. 235// ========== DCT float ==========
  236. 236
  237. 237///|
  238. 238/// float 2D DCT。
  239. 239/// @see dct_2d 8-bit 版本,本函数为 ImageF 变体
  240. 240pub fn dct_2d_f(img : @types.ImageF) -> Array[Array[Double]] {
  241. 241● let w = img.width
  242. 242 let h = img.height
  243. 243 let ch = img.channels
  244. 244● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  245. 245 for i in 0..<(w * h * ch) {
  246. 246● let vf = read_float_le_freq(img.data, i * 4)
  247. 247● let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255)
  248. 248● data8[i] = v8.to_byte()
  249. 249 }
  250. 250 let img8 = @types.Image::{
  251. 251 width: w,
  252. 252 height: h,
  253. 253 channels: ch,
  254. 254● data: Bytes::from_array(data8),
  255. 255 }
  256. 256● dct_2d(img8)
  257. 257}
  258. 258
  259. 259///|
  260. 260/// float 逆 DCT。输出 ImageF。
  261. 261/// @see idct_2d 8-bit 版本,本函数为 ImageF 变体
  262. 262pub fn idct_2d_f(coeffs : Array[Array[Double]]) -> @types.ImageF {
  263. 263● let img8 = idct_2d(coeffs)
  264. 264 let w = img8.width
  265. 265 let h = img8.height
  266. 266 let ch = img8.channels
  267. 267● let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  268. 268 for i in 0..<(w * h * ch) {
  269. 269● let v8 = img8.data[i].to_int()
  270. 270● write_f32_le_freq(out, i * 4, Float::from_int(v8) / 255.0F)
  271. 271 }
  272. 272 @types.ImageF::{
  273. 273 width: w,
  274. 274 height: h,
  275. 275 channels: ch,
  276. 276● data: Bytes::from_array(out),
  277. 277 }
  278. 278}
  279. 279
  280. 280// ========== Haar 16-bit ==========
  281. 281
  282. 282///|
  283. 283/// 16-bit 2D Haar 小波变换。
  284. 284/// @see haar_transform_2d 8-bit 版本,本函数为 Image16 变体
  285. 285pub fn haar_transform_2d_16(
  286. 286 img : @types.Image16,
  287. 287 levels~ : Int32 = 1,
  288. 288) -> HaarWaveletResult raise @types.LoadError {
  289. 289● let w = img.width
  290. 290 let h = img.height
  291. 291 let ch = img.channels
  292. 292● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  293. 293 for i in 0..<(w * h * ch) {
  294. 294● let v16 = read_u16_le_freq(img.data, i * 2)
  295. 295● data8[i] = (v16 / 257).to_byte()
  296. 296 }
  297. 297 let img8 = @types.Image::{
  298. 298 width: w,
  299. 299 height: h,
  300. 300 channels: ch,
  301. 301● data: Bytes::from_array(data8),
  302. 302 }
  303. 303● haar_transform_2d(img8, levels~)
  304. 304}
  305. 305
  306. 306///|
  307. 307/// 16-bit 逆 Haar 小波变换。输出 Image16。
  308. 308/// @see haar_inverse_transform_2d 8-bit 版本,本函数为 Image16 变体
  309. 309pub fn haar_inverse_transform_2d_16(
  310. 310 result : HaarWaveletResult,
  311. 311) -> @types.Image16 {
  312. 312● let img8 = haar_inverse_transform_2d(result)
  313. 313 let w = img8.width
  314. 314 let h = img8.height
  315. 315 let ch = img8.channels
  316. 316● let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  317. 317 for i in 0..<(w * h * ch) {
  318. 318● let v8 = img8.data[i].to_int()
  319. 319● write_u16_le_freq(out, i * 2, v8 * 257)
  320. 320 }
  321. 321 @types.Image16::{
  322. 322 width: w,
  323. 323 height: h,
  324. 324 channels: ch,
  325. 325● data: Bytes::from_array(out),
  326. 326 }
  327. 327}
  328. 328
  329. 329///|
  330. 330/// 16-bit Haar 去噪。
  331. 331/// @see haar_denoise 8-bit 版本,本函数为 Image16 变体
  332. 332pub fn haar_denoise_16(
  333. 333 img : @types.Image16,
  334. 334 threshold~ : Float = 10.0F,
  335. 335 soft~ : Bool = true,
  336. 336 levels~ : Int32 = 3,
  337. 337) -> @types.Image16 raise @types.LoadError {
  338. 338● let w = img.width
  339. 339 let h = img.height
  340. 340 let ch = img.channels
  341. 341● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  342. 342 for i in 0..<(w * h * ch) {
  343. 343● let v16 = read_u16_le_freq(img.data, i * 2)
  344. 344● data8[i] = (v16 / 257).to_byte()
  345. 345 }
  346. 346 let img8 = @types.Image::{
  347. 347 width: w,
  348. 348 height: h,
  349. 349 channels: ch,
  350. 350● data: Bytes::from_array(data8),
  351. 351 }
  352. 352● let out8 = haar_denoise(img8, threshold~, soft~, levels~)
  353. 353● let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  354. 354 for i in 0..<(w * h * ch) {
  355. 355● let v8 = out8.data[i].to_int()
  356. 356● write_u16_le_freq(out, i * 2, v8 * 257)
  357. 357 }
  358. 358 @types.Image16::{
  359. 359 width: w,
  360. 360 height: h,
  361. 361 channels: ch,
  362. 362● data: Bytes::from_array(out),
  363. 363 }
  364. 364}
  365. 365
  366. 366// ========== Haar float ==========
  367. 367
  368. 368///|
  369. 369/// float 2D Haar 小波变换。
  370. 370/// @see haar_transform_2d 8-bit 版本,本函数为 ImageF 变体
  371. 371pub fn haar_transform_2d_f(
  372. 372 img : @types.ImageF,
  373. 373 levels~ : Int32 = 1,
  374. 374) -> HaarWaveletResult raise @types.LoadError {
  375. 375● let w = img.width
  376. 376 let h = img.height
  377. 377 let ch = img.channels
  378. 378● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  379. 379 for i in 0..<(w * h * ch) {
  380. 380● let vf = read_float_le_freq(img.data, i * 4)
  381. 381● let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255)
  382. 382● data8[i] = v8.to_byte()
  383. 383 }
  384. 384 let img8 = @types.Image::{
  385. 385 width: w,
  386. 386 height: h,
  387. 387 channels: ch,
  388. 388● data: Bytes::from_array(data8),
  389. 389 }
  390. 390● haar_transform_2d(img8, levels~)
  391. 391}
  392. 392
  393. 393///|
  394. 394/// float 逆 Haar 小波变换。输出 ImageF。
  395. 395/// @see haar_inverse_transform_2d 8-bit 版本,本函数为 ImageF 变体
  396. 396pub fn haar_inverse_transform_2d_f(result : HaarWaveletResult) -> @types.ImageF {
  397. 397● let img8 = haar_inverse_transform_2d(result)
  398. 398 let w = img8.width
  399. 399 let h = img8.height
  400. 400 let ch = img8.channels
  401. 401● let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  402. 402 for i in 0..<(w * h * ch) {
  403. 403● let v8 = img8.data[i].to_int()
  404. 404● write_f32_le_freq(out, i * 4, Float::from_int(v8) / 255.0F)
  405. 405 }
  406. 406 @types.ImageF::{
  407. 407 width: w,
  408. 408 height: h,
  409. 409 channels: ch,
  410. 410● data: Bytes::from_array(out),
  411. 411 }
  412. 412}
  413. 413
  414. 414///|
  415. 415/// float Haar 去噪。
  416. 416/// @see haar_denoise 8-bit 版本,本函数为 ImageF 变体
  417. 417pub fn haar_denoise_f(
  418. 418 img : @types.ImageF,
  419. 419 threshold~ : Float = 10.0F,
  420. 420 soft~ : Bool = true,
  421. 421 levels~ : Int32 = 3,
  422. 422) -> @types.ImageF raise @types.LoadError {
  423. 423● let w = img.width
  424. 424 let h = img.height
  425. 425 let ch = img.channels
  426. 426● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  427. 427 for i in 0..<(w * h * ch) {
  428. 428● let vf = read_float_le_freq(img.data, i * 4)
  429. 429● let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255)
  430. 430● data8[i] = v8.to_byte()
  431. 431 }
  432. 432 let img8 = @types.Image::{
  433. 433 width: w,
  434. 434 height: h,
  435. 435 channels: ch,
  436. 436● data: Bytes::from_array(data8),
  437. 437 }
  438. 438● let out8 = haar_denoise(img8, threshold~, soft~, levels~)
  439. 439● let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  440. 440 for i in 0..<(w * h * ch) {
  441. 441● let v8 = out8.data[i].to_int()
  442. 442● write_f32_le_freq(out, i * 4, Float::from_int(v8) / 255.0F)
  443. 443 }
  444. 444 @types.ImageF::{
  445. 445 width: w,
  446. 446 height: h,
  447. 447 channels: ch,
  448. 448● data: Bytes::from_array(out),
  449. 449 }
  450. 450}