image.process\frequency\dct.mbt

90.7% · 78 / 86 points · 188 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/// # 离散余弦变换 dct
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// DCT (离散余弦变换) 公共 API:dct_2d + idct_2d
  7. 7// 2D DCT-II 和 IDCT-III,用于频域分析和图像压缩
  8. 8// 优化:预计算余弦表 + 可分离 1D DCT,O(N³) 而非 O(N⁴)
  9. 9
  10. 10///|
  11. 11/// 预计算 DCT 余弦表:cos_table[m][k] = cos((2*k+1)*m*π/(2*N))
  12. 12/// 注意索引顺序:第一维是频率 m,第二维是空间 k
  13. 13fn dct_cos_table(n : Int32) -> Array[Array[Float]] {
  14. 14● let pi = 3.14159265358979F
  15. 15● let nf = Float::from_int(n)
  16. 16● let table : Array[Array[Float]] = Array::make(n, [])
  17. 17 for m in 0..<n {
  18. 18● let row : Array[Float] = Array::make(n, 0.0F)
  19. 19● let mf = Float::from_int(m)
  20. 20 for k in 0..<n {
  21. 21● let kf = Float::from_int(2 * k + 1)
  22. 22● row[k] = @math.cosf(pi * kf * mf / (2.0F * nf))
  23. 23 }
  24. 24 table[m] = row
  25. 25 }
  26. 26 table
  27. 27}
  28. 28
  29. 29///|
  30. 30/// 预计算 DCT 归一化系数:C(0)=sqrt(1/N), C(k)=sqrt(2/N)
  31. 31fn dct_coeffs(n : Int32) -> Array[Float] {
  32. 32● let nf = Float::from_int(n)
  33. 33● let coeffs : Array[Float] = Array::make(n, 0.0F)
  34. 34 for i in 0..<n {
  35. 35● coeffs[i] = if i == 0 { (1.0F / nf).sqrt() } else { (2.0F / nf).sqrt() }
  36. 36 }
  37. 37 coeffs
  38. 38}
  39. 39
  40. 40///|
  41. 41/// 1D DCT-II 正变换:对长度 n 的行向量执行 DCT
  42. 42/// 输入 src[0..n),输出 dst[0..n)
  43. 43/// 余弦查找表与系数 cos_table[k][n] = cos((2*k+1)*n*π/(2*N)), coeffs[n] = C(n)
  44. 44fn dct_1d(
  45. 45 src : ArrayView[Float],
  46. 46 dst : Array[Float],
  47. 47 cos_table : Array[Array[Float]],
  48. 48 coeffs : ArrayView[Float],
  49. 49 n : Int32,
  50. 50) -> Unit {
  51. 51● for m in 0..<n {
  52. 52● let mut sum = 0.0F
  53. 53 let cos_m = cos_table[m]
  54. 54 for k in 0..<n {
  55. 55● sum += src[k] * cos_m[k]
  56. 56 }
  57. 57 dst[m] = coeffs[m] * sum
  58. 58 }
  59. 59}
  60. 60
  61. 61///|
  62. 62/// 1D IDCT-III 逆变换:对长度 n 的频域向量执行 IDCT
  63. 63/// 输入 src[0..n)(频域),输出 dst[0..n)(空间域)
  64. 64/// 公式:dst[k] = sum_m C(m) * src[m] * cos((2*k+1)*m*π/(2*N))
  65. 65fn idct_1d(
  66. 66 src : ArrayView[Float],
  67. 67 dst : Array[Float],
  68. 68 cos_table : Array[Array[Float]],
  69. 69 coeffs : ArrayView[Float],
  70. 70 n : Int32,
  71. 71) -> Unit {
  72. 72● for k in 0..<n {
  73. 73● let mut sum = 0.0F
  74. 74 for m in 0..<n {
  75. 75● sum += coeffs[m] * src[m] * cos_table[m][k]
  76. 76 }
  77. 77 dst[k] = sum
  78. 78 }
  79. 79}
  80. 80
  81. 81///|
  82. 82/// 2D DCT-II。将灰度图变换为 DCT 系数矩阵。
  83. 83/// 输入图像转为灰度,输出 N×M 的系数矩阵。
  84. 84/// 优化:可分离 1D DCT(先行变换再列变换),O(N³) 而非 O(N⁴)
  85. 85pub fn dct_2d(img : @types.Image) -> Array[Array[Double]] {
  86. 86● let ch = img.channels
  87. 87 let w = img.width
  88. 88 let h = img.height
  89. 89 let n = w * h
  90. 90 // 提取灰度像素数据
  91. 91● let src : Array[Float] = Array::make(n, 0.0F)
  92. 92 for i in 0..<n {
  93. 93● let v = if ch == 1 {
  94. 94● Float::from_int(img.data[i].to_int())
  95. 95 } else {
  96. 96○ let r = Float::from_int(img.data[i * ch].to_int())
  97. 97○ let g = Float::from_int(img.data[i * ch + 1].to_int())
  98. 98○ let b = Float::from_int(img.data[i * ch + 2].to_int())
  99. 99 r * 0.299F + g * 0.587F + b * 0.114F
  100. 100 }
  101. 101 src[i] = v
  102. 102 }
  103. 103 // 预计算余弦表和归一化系数
  104. 104● let cos_w = dct_cos_table(w)
  105. 105● let cos_h = dct_cos_table(h)
  106. 106● let coeffs_w = dct_coeffs(w)
  107. 107● let coeffs_h = dct_coeffs(h)
  108. 108 // 第一步:对每一行执行 1D DCT(宽度方向)
  109. 109 // row_dct[y][u] = C_w(u) * sum_x src[y][x] * cos((2x+1)uπ/(2W))
  110. 110● let row_dct : Array[Array[Float]] = Array::make(h, [])
  111. 111● let row_buf : Array[Float] = Array::make(w, 0.0F)
  112. 112 for y in 0..<h {
  113. 113● let row_src : Array[Float] = Array::make(w, 0.0F)
  114. 114 for x in 0..<w {
  115. 115● row_src[x] = src[y * w + x]
  116. 116 }
  117. 117● dct_1d(row_src[:], row_buf, cos_w, coeffs_w[:], w)
  118. 118● row_dct[y] = Array::make(w, 0.0F)
  119. 119 for u in 0..<w {
  120. 120● row_dct[y][u] = row_buf[u]
  121. 121 }
  122. 122 }
  123. 123 // 第二步:对每一列执行 1D DCT(高度方向)
  124. 124 // F(v,u) = C_h(v) * sum_y row_dct[y][u] * cos((2y+1)vπ/(2H))
  125. 125● let result : Array[Array[Double]] = Array::make(h, [])
  126. 126● let col_buf : Array[Float] = Array::make(h, 0.0F)
  127. 127 for u in 0..<w {
  128. 128● let col_src : Array[Float] = Array::make(h, 0.0F)
  129. 129 for y in 0..<h {
  130. 130● col_src[y] = row_dct[y][u]
  131. 131 }
  132. 132● dct_1d(col_src[:], col_buf, cos_h, coeffs_h[:], h)
  133. 133 for v in 0..<h {
  134. 134● if result[v].length() == 0 {
  135. 135● result[v] = Array::make(w, 0.0)
  136. 136 }
  137. 137● result[v][u] = col_buf[v].to_double()
  138. 138 }
  139. 139 }
  140. 140 result
  141. 141}
  142. 142
  143. 143///|
  144. 144/// 2D IDCT-III。将 DCT 系数矩阵逆变换为灰度图。
  145. 145/// 优化:可分离 1D IDCT(先列变换再行变换),O(N³) 而非 O(N⁴)
  146. 146pub fn idct_2d(coeffs : Array[Array[Double]]) -> @types.Image {
  147. 147● let h = coeffs.length()
  148. 148◐ let w = if h == 0 { 0 } else { coeffs[0].length() }
  149. 149 // 预计算余弦表和归一化系数
  150. 150● let cos_w = dct_cos_table(w)
  151. 151● let cos_h = dct_cos_table(h)
  152. 152● let coeffs_w = dct_coeffs(w)
  153. 153● let coeffs_h = dct_coeffs(h)
  154. 154 // 第一步:对每一列执行 1D IDCT(高度方向)
  155. 155 // col_idct[y][u] = sum_v C_h(v) * coeffs[v][u] * cos((2y+1)vπ/(2H))
  156. 156● let col_idct : Array[Array[Float]] = Array::make(h, [])
  157. 157● let col_buf : Array[Float] = Array::make(h, 0.0F)
  158. 158 for u in 0..<w {
  159. 159● let col_src : Array[Float] = Array::make(h, 0.0F)
  160. 160 for v in 0..<h {
  161. 161● col_src[v] = Float::from_double(coeffs[v][u])
  162. 162 }
  163. 163● idct_1d(col_src[:], col_buf, cos_h, coeffs_h[:], h)
  164. 164 for y in 0..<h {
  165. 165● if col_idct[y].length() == 0 {
  166. 166● col_idct[y] = Array::make(w, 0.0F)
  167. 167 }
  168. 168 col_idct[y][u] = col_buf[y]
  169. 169 }
  170. 170 }
  171. 171 // 第二步:对每一行执行 1D IDCT(宽度方向)
  172. 172 // f(x,y) = sum_u C_w(u) * col_idct[y][u] * cos((2x+1)uπ/(2W))
  173. 173● let out : Array[Byte] = Array::make(w * h, b'\x00')
  174. 174● let row_buf : Array[Float] = Array::make(w, 0.0F)
  175. 175 for y in 0..<h {
  176. 176● idct_1d(col_idct[y][:], row_buf, cos_w, coeffs_w[:], w)
  177. 177 for x in 0..<w {
  178. 178● let val = (row_buf[x] + 0.5F).to_int().clamp(min=0, max=255)
  179. 179● out[y * w + x] = val.to_byte()
  180. 180 }
  181. 181 }
  182. 182 @types.Image::{
  183. 183 width: w,
  184. 184 height: h,
  185. 185 channels: 1,
  186. 186● data: Bytes::from_array(out),
  187. 187 }
  188. 188}