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