image.codec\codec\jpeg_upsample.mbt

100% · 65 / 65 points · 253 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/// # JPEG 色度上采样(libjpeg fancy upsampling 移植)
  2. 2///
  3. 3/// 4:2:0 / 4:2:2 的色度分辨率是亮度的一半,解码时要放大回亮度网格。以前用最近邻
  4. 4/// (一个色度值铺满 2×2 像素),放大处会出现方块状边缘;libjpeg 默认用"三角滤波"
  5. 5/// (`h2v2_fancy_upsample` / `h2v1_fancy_upsample`)——按 3/4·近邻 + 1/4·远邻 加权,
  6. 6/// 相当于权重 9/16、3/16、3/16、1/16 的双线性,边缘平滑很多。
  7. 7///
  8. 8/// 本文件按 libjpeg `jdsample.c` 的公式逐位复刻,只依赖整数运算:
  9. 9/// - `h2v1`:水平 2:1,偶数列 `(3·this + last + 2) >> 2`、奇数列 `(3·this + next + 1) >> 2`;
  10. 10/// - `h2v2`:水平 + 垂直各 2:1,先算列和 `colsum = 3·近行 + 远行`,
  11. 11/// 偶数列 `(3·colsum + lastsum + 8) >> 4`、奇数列 `(3·colsum + nextsum + 7) >> 4`。
  12. 12///
  13. 13/// 边界(首/末行、首/末列)用复制钳制,与 libjpeg 在输入数组两侧填 context 行的效果一致。
  14. 14
  15. 15///|
  16. 16/// 把 `v` 钳到 `[lo, hi]`
  17. 17fn jpeg_clamp_i32(v : Int32, lo : Int32, hi : Int32) -> Int32 {
  18. 18● if v < lo {
  19. 19● lo
  20. 20● } else if v > hi {
  21. 21● hi
  22. 22 } else {
  23. 23● v
  24. 24 }
  25. 25}
  26. 26
  27. 27///|
  28. 28/// 一列上的"行加权和":`3·近行 + 远行`(libjpeg 的 `colsum`)
  29. 29fn jpeg_colsum(
  30. 30 plane : Array[Int32],
  31. 31 stride : Int32,
  32. 32 near_row : Int32,
  33. 33 far_row : Int32,
  34. 34 c : Int32,
  35. 35) -> Int32 {
  36. 36● plane[near_row * stride + c] * 3 + plane[far_row * stride + c]
  37. 37}
  38. 38
  39. 39///|
  40. 40/// 水平 2:1 三角滤波上采样(libjpeg `h2v1_fancy_upsample`)
  41. 41///
  42. 42/// `stride` 是原平面的行跨距(可能大于有效宽 `dw`,块对齐留下的余量)。
  43. 43/// 返回 `(放大后的平面, 宽, 高)`;宽 = `dw * 2`,高不变。
  44. 44fn jpeg_fancy_h2v1(
  45. 45 plane : Array[Int32],
  46. 46 stride : Int32,
  47. 47 dw : Int32,
  48. 48 dh : Int32,
  49. 49) -> (Array[Int32], Int32, Int32) {
  50. 50● let uw = dw * 2
  51. 51● let out : Array[Int32] = Array::make(uw * dh, 0)
  52. 52 for y in 0..<dh {
  53. 53● for x in 0..<uw {
  54. 54● let cx = x >> 1
  55. 55 let near = plane[y * stride + cx]
  56. 56 let neighbor = if (x & 1) == 0 {
  57. 57● plane[y * stride + jpeg_clamp_i32(cx - 1, 0, dw - 1)]
  58. 58 } else {
  59. 59● plane[y * stride + jpeg_clamp_i32(cx + 1, 0, dw - 1)]
  60. 60 }
  61. 61 out[y * uw + x] = if (x & 1) == 0 {
  62. 62● (near * 3 + neighbor + 2) >> 2
  63. 63 } else {
  64. 64● (near * 3 + neighbor + 1) >> 2
  65. 65 }
  66. 66 }
  67. 67 }
  68. 68 (out, uw, dh)
  69. 69}
  70. 70
  71. 71///|
  72. 72/// 水平 + 垂直各 2:1 三角滤波上采样(libjpeg `h2v2_fancy_upsample`)
  73. 73///
  74. 74/// 返回 `(放大后的平面, 宽, 高)`;宽高各 ×2。权重合计 9/16、3/16、3/16、1/16(见 libjpeg 注释)。
  75. 75fn jpeg_fancy_h2v2(
  76. 76 plane : Array[Int32],
  77. 77 stride : Int32,
  78. 78 dw : Int32,
  79. 79 dh : Int32,
  80. 80) -> (Array[Int32], Int32, Int32) {
  81. 81● let uw = dw * 2
  82. 82 let uh = dh * 2
  83. 83● let out : Array[Int32] = Array::make(uw * uh, 0)
  84. 84 for y in 0..<uh {
  85. 85● let cy = y >> 1
  86. 86 // 偶数行离上一行近,奇数行离下一行近(越界复制边缘行)
  87. 87 let far = if (y & 1) == 0 {
  88. 88● jpeg_clamp_i32(cy - 1, 0, dh - 1)
  89. 89 } else {
  90. 90● jpeg_clamp_i32(cy + 1, 0, dh - 1)
  91. 91 }
  92. 92 for x in 0..<uw {
  93. 93● let cx = x >> 1
  94. 94● let this_sum = jpeg_colsum(plane, stride, cy, far, cx)
  95. 95 let neighbor_sum = if (x & 1) == 0 {
  96. 96● jpeg_colsum(plane, stride, cy, far, jpeg_clamp_i32(cx - 1, 0, dw - 1))
  97. 97 } else {
  98. 98● jpeg_colsum(plane, stride, cy, far, jpeg_clamp_i32(cx + 1, 0, dw - 1))
  99. 99 }
  100. 100 out[y * uw + x] = if (x & 1) == 0 {
  101. 101● (this_sum * 3 + neighbor_sum + 8) >> 4
  102. 102 } else {
  103. 103● (this_sum * 3 + neighbor_sum + 7) >> 4
  104. 104 }
  105. 105 }
  106. 106 }
  107. 107 (out, uw, uh)
  108. 108}
  109. 109
  110. 110///|
  111. 111/// 按采样因子选择上采样方式:2×2 → `h2v2` 三角滤波,2×1 → `h2v1` 三角滤波,
  112. 112/// 其他(整分辨或罕见比例)保持原平面并按最近邻取样。
  113. 113///
  114. 114/// 第四项 `true` 表示返回平面已在亮度网格上,取样直接用 `(ox, oy)`。
  115. 115fn jpeg_upsample_plane(
  116. 116 plane : Array[Int32],
  117. 117 stride : Int32,
  118. 118 dw : Int32,
  119. 119 dh : Int32,
  120. 120 scale_h : Int32,
  121. 121 scale_v : Int32,
  122. 122) -> (Array[Int32], Int32, Int32, Bool) {
  123. 123● if scale_h == 2 && scale_v == 2 {
  124. 124● let (up, uw, uh) = jpeg_fancy_h2v2(plane, stride, dw, dh)
  125. 125 (up, uw, uh, true)
  126. 126● } else if scale_h == 2 && scale_v == 1 {
  127. 127● let (up, uw, uh) = jpeg_fancy_h2v1(plane, stride, dw, dh)
  128. 128 (up, uw, uh, true)
  129. 129 } else {
  130. 130 // 保持原平面:返回的"宽"必须是真实行跨距(块对齐留下 padding 列),
  131. 131 // 取样按 neighest 索引 stride,边界钳制交给调用方按有效宽处理
  132. 132● (plane, stride, dh, false)
  133. 133 }
  134. 134}
  135. 135
  136. 136///|
  137. 137/// 从(可能已上采样的)分量平面取样一个像素
  138. 138fn jpeg_plane_pixel(
  139. 139 planes : Array[Array[Int32]],
  140. 140 plane_w : Array[Int32],
  141. 141 plane_h : Array[Int32],
  142. 142 on_luma_grid : Array[Bool],
  143. 143 ci : Int32,
  144. 144 ox : Int32,
  145. 145 oy : Int32,
  146. 146 hi : Int32,
  147. 147 vi : Int32,
  148. 148 max_h : Int32,
  149. 149 max_v : Int32,
  150. 150) -> Int32 {
  151. 151● let pw = plane_w[ci]
  152. 152 let ph = plane_h[ci]
  153. 153 if on_luma_grid[ci] {
  154. 154● planes[ci][jpeg_clamp_i32(oy, 0, ph - 1) * pw +
  155. 155● jpeg_clamp_i32(ox, 0, pw - 1)]
  156. 156 } else {
  157. 157● let sx = jpeg_clamp_i32(ox * hi / max_h, 0, pw - 1)
  158. 158● let sy = jpeg_clamp_i32(oy * vi / max_v, 0, ph - 1)
  159. 159 planes[ci][sy * pw + sx]
  160. 160 }
  161. 161}
  162. 162
  163. 163///|
  164. 164/// 分量平面 → RGB(灰度复制三通道)。色度走三角滤波上采样,其余按采样因子最近邻取样。
  165. 165///
  166. 166/// 这是 baseline(`jpeg_decode_scan`)与 progressive(`jpeg_progressive_output`)共用的收尾步骤:
  167. 167/// 两者解码阶段都产出"每分量按块网格铺好的整幅平面",差别只在系数怎么攒。
  168. 168fn jpeg_planes_to_rgb(
  169. 169 header : JpegHeader,
  170. 170 planes : Array[Array[Int32]],
  171. 171 plane_w : Array[Int32],
  172. 172 max_h : Int32,
  173. 173 max_v : Int32,
  174. 174) -> @types.Image {
  175. 175● let width = header.width
  176. 176 let height = header.height
  177. 177 let nc = header.num_components
  178. 178● let out : Array[Byte] = Array::make(width * height * 3, b'\x00')
  179. 179 // 每分量:上采样后的平面 + 宽高 + 是否已落在亮度网格
  180. 180 let s_planes : Array[Array[Int32]] = []
  181. 181● let s_w : Array[Int32] = Array::make(4, 0)
  182. 182● let s_h : Array[Int32] = Array::make(4, 0)
  183. 183● let on_grid : Array[Bool] = Array::make(4, false)
  184. 184 for ci in 0..<nc {
  185. 185● let hi = header.component_h[ci]
  186. 186 let vi = header.component_v[ci]
  187. 187 let dw = (width * hi + max_h - 1) / max_h
  188. 188 let dh = (height * vi + max_v - 1) / max_v
  189. 189● let (p, pw, ph, up) = jpeg_upsample_plane(
  190. 190 planes[ci],
  191. 191 plane_w[ci],
  192. 192 dw,
  193. 193 dh,
  194. 194 max_h / hi,
  195. 195 max_v / vi,
  196. 196 )
  197. 197● s_planes.push(p)
  198. 198 s_w[ci] = pw
  199. 199 s_h[ci] = ph
  200. 200 on_grid[ci] = up
  201. 201 }
  202. 202 let h0 = header.component_h[0]
  203. 203 let v0 = header.component_v[0]
  204. 204 for oy in 0..<height {
  205. 205● for ox in 0..<width {
  206. 206● let oidx = (oy * width + ox) * 3
  207. 207 if nc == 1 {
  208. 208● let v = jpeg_plane_pixel(
  209. 209 s_planes, s_w, s_h, on_grid, 0, ox, oy, h0, v0, max_h, max_v,
  210. 210 )
  211. 211● let vb = v.to_byte()
  212. 212 out[oidx] = vb
  213. 213 out[oidx + 1] = vb
  214. 214 out[oidx + 2] = vb
  215. 215 } else {
  216. 216● let yv = jpeg_plane_pixel(
  217. 217 s_planes, s_w, s_h, on_grid, 0, ox, oy, h0, v0, max_h, max_v,
  218. 218 )
  219. 219● let cb = jpeg_plane_pixel(
  220. 220 s_planes,
  221. 221 s_w,
  222. 222 s_h,
  223. 223 on_grid,
  224. 224 1,
  225. 225 ox,
  226. 226 oy,
  227. 227 header.component_h[1],
  228. 228 header.component_v[1],
  229. 229 max_h,
  230. 230 max_v,
  231. 231 )
  232. 232● let cr = jpeg_plane_pixel(
  233. 233 s_planes,
  234. 234 s_w,
  235. 235 s_h,
  236. 236 on_grid,
  237. 237 2,
  238. 238 ox,
  239. 239 oy,
  240. 240 header.component_h[2],
  241. 241 header.component_v[2],
  242. 242 max_h,
  243. 243 max_v,
  244. 244 )
  245. 245● let (r, g, b) = ycbcr_to_rgb(yv, cb, cr)
  246. 246● out[oidx] = r.to_byte()
  247. 247● out[oidx + 1] = g.to_byte()
  248. 248● out[oidx + 2] = b.to_byte()
  249. 249 }
  250. 250 }
  251. 251 }
  252. 252● @types.Image::{ width, height, channels: 3, data: Bytes::from_array(out), }
  253. 253}