image.codec\codec\webp_vp8l_encoder_transforms.mbt
98.1% · 210 / 214 points · 368 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # VP8L 编码变换:palette / predictor / subtract-green 与对外单入口 - 3
- 4
///| - 5
fn vp8l_write_header( - 6
bw : Vp8lBits, - 7
w : Int32, - 8
h : Int32, - 9
alpha_solid : Bool, - 10
) -> Unit { - 11●
bw.write(0x2F, 8) - 12●
bw.write(w - 1, 14) - 13●
bw.write(h - 1, 14) - 14◐
bw.write(if alpha_solid { 0 } else { 1 }, 1) - 15●
bw.write(0, 3) - 16
} - 17
- 18
///| - 19
fn vp8l_alpha_solid(pixels : Array[Int32]) -> Bool { - 20●
for i in 0..<pixels.length() { - 21●
if vp8l_ch(pixels[i], 24) != 255 { - 22○
return false - 23
} - 24
} - 25
true - 26
} - 27
- 28
// ==================== 变换:subtract-green ==================== - 29
- 30
///| - 31
fn vp8l_apply_subtract_green(pixels : Array[Int32]) -> Array[Int32] { - 32●
let out : Array[Int32] = Array::make(pixels.length(), 0) - 33●
for i in 0..<pixels.length() { - 34●
let argb = pixels[i] - 35●
let g = vp8l_ch(argb, 8) - 36●
let r = (vp8l_ch(argb, 16) - g) & 0xFF - 37●
let b = (vp8l_ch(argb, 0) - g) & 0xFF - 38●
out[i] = (vp8l_ch(argb, 24) << 24) | (r << 16) | (g << 8) | b - 39
} - 40
out - 41
} - 42
- 43
// ==================== 变换:predictor ==================== - 44
- 45
///| - 46
fn vp8l_predict_value( - 47
mode : Int32, - 48
l : Int32, - 49
t : Int32, - 50
tl : Int32, - 51
tr : Int32, - 52
) -> Int32 { - 53●
if mode == 0 { - 54●
0xFF000000 - 55●
} else if mode == 1 { - 56●
l - 57●
} else if mode == 2 { - 58●
t - 59●
} else if mode == 3 { - 60●
tr - 61●
} else if mode == 4 { - 62●
tl - 63●
} else if mode == 5 { - 64●
vp8l_avg2(vp8l_avg2(l, tr), t) - 65●
} else if mode == 6 { - 66●
vp8l_avg2(l, tl) - 67●
} else if mode == 7 { - 68●
vp8l_avg2(l, t) - 69●
} else if mode == 8 { - 70●
vp8l_avg2(tl, t) - 71●
} else if mode == 9 { - 72●
vp8l_avg2(t, tr) - 73●
} else if mode == 10 { - 74●
vp8l_avg2(vp8l_avg2(l, tl), vp8l_avg2(t, tr)) - 75●
} else if mode == 11 { - 76●
vp8l_select(l, t, tl) - 77●
} else if mode == 12 { - 78●
vp8l_clamp_add_full(l, t, tl) - 79
} else { - 80●
vp8l_clamp_add_half(vp8l_avg2(l, t), tl) - 81
} - 82
} - 83
- 84
///| - 85
/// 按 RFC 边界规则取预测值(编码侧邻居用原图,解码侧邻居是已重建像素,两者等价) - 86
fn vp8l_predict_pixel( - 87
pixels : Array[Int32], - 88
w : Int32, - 89
x : Int32, - 90
y : Int32, - 91
mode : Int32, - 92
) -> Int32 { - 93●
let i = y * w + x - 94
if y == 0 { - 95●
if x == 0 { - 96●
0xFF000000 - 97
} else { - 98●
pixels[i - 1] - 99
} - 100●
} else if x == 0 { - 101●
pixels[i - w] - 102
} else { - 103●
let l = pixels[i - 1] - 104
let t = pixels[i - w] - 105
let tl = pixels[i - w - 1] - 106●
let tr = if x + 1 < w { pixels[i - w + 1] } else { pixels[y * w] } - 107●
vp8l_predict_value(mode, l, t, tl, tr) - 108
} - 109
} - 110
- 111
///| - 112
/// 残差代价:四通道环差绝对值之和。四个通道手工展开 —— 这里在内层 14×像素 的循环里 - 113
/// 每像素调用一次,用 `for shift in [...]` 会在每次调用分配一个数组。 - 114
fn vp8l_residual_cost(orig : Int32, pred : Int32) -> Int32 { - 115●
let mut c = 0 - 116●
let d0 = (vp8l_ch(orig, 24) - vp8l_ch(pred, 24)) & 0xFF - 117◐
c += if d0 < 128 { d0 } else { 256 - d0 } - 118●
let d1 = (vp8l_ch(orig, 16) - vp8l_ch(pred, 16)) & 0xFF - 119●
c += if d1 < 128 { d1 } else { 256 - d1 } - 120●
let d2 = (vp8l_ch(orig, 8) - vp8l_ch(pred, 8)) & 0xFF - 121●
c += if d2 < 128 { d2 } else { 256 - d2 } - 122●
let d3 = (vp8l_ch(orig, 0) - vp8l_ch(pred, 0)) & 0xFF - 123●
c += if d3 < 128 { d3 } else { 256 - d3 } - 124
c - 125
} - 126
- 127
///| - 128
/// 前向 predictor:逐 16² 块选模式,返回 (残差像素, 模式图) - 129
fn vp8l_forward_predictor( - 130
pixels : Array[Int32], - 131
w : Int32, - 132
h : Int32, - 133
block_bits : Int32, - 134
) -> (Array[Int32], Array[Int32], Int32) { - 135●
let bs = 1 << block_bits - 136
let bx = (w + bs - 1) / bs - 137
let by = (h + bs - 1) / bs - 138●
let modes : Array[Int32] = Array::make(bx * by, 0) - 139
for bloc_y in 0..<by { - 140●
for bloc_x in 0..<bx { - 141●
let x0 = bloc_x * bs - 142
let y0 = bloc_y * bs - 143●
let x1 = if x0 + bs > w { w } else { x0 + bs } - 144●
let y1 = if y0 + bs > h { h } else { y0 + bs } - 145
let mut best_mode = 1 - 146
let mut best_cost = -1 - 147
for mode in 0..<14 { - 148●
let mut cost = 0 - 149
for yy in y0..<y1 { - 150●
for xx in x0..<x1 { - 151●
let i = yy * w + xx - 152●
let pred = vp8l_predict_pixel(pixels, w, xx, yy, mode) - 153●
cost += vp8l_residual_cost(pixels[i], pred) - 154
} - 155
} - 156
if best_cost < 0 || cost < best_cost { - 157●
best_cost = cost - 158
best_mode = mode - 159
} - 160
} - 161
modes[bloc_y * bx + bloc_x] = best_mode - 162
} - 163
} - 164●
let resid : Array[Int32] = Array::make(w * h, 0) - 165
for y in 0..<h { - 166●
for x in 0..<w { - 167●
let i = y * w + x - 168
let mode = if y < by * bs && x < bx * bs { - 169●
modes[y / bs * bx + x / bs] - 170
} else { - 171○
1 - 172
} - 173●
let pred = vp8l_predict_pixel(pixels, w, x, y, mode) - 174●
let a = (vp8l_ch(pixels[i], 24) - vp8l_ch(pred, 24)) & 0xFF - 175●
let r = (vp8l_ch(pixels[i], 16) - vp8l_ch(pred, 16)) & 0xFF - 176●
let g = (vp8l_ch(pixels[i], 8) - vp8l_ch(pred, 8)) & 0xFF - 177●
let b = (vp8l_ch(pixels[i], 0) - vp8l_ch(pred, 0)) & 0xFF - 178
resid[i] = (a << 24) | (r << 16) | (g << 8) | b - 179
} - 180
} - 181
(resid, modes, bx) - 182
} - 183
- 184
// ==================== 变换:palette ==================== - 185
- 186
///| - 187
priv struct Vp8lPalette { - 188
colors : Array[Int32] - 189
packed : Array[Int32] - 190
packed_width : Int32 - 191
} - 192
- 193
///| - 194
fn vp8l_try_palette(pixels : Array[Int32], w : Int32) -> Vp8lPalette? { - 195●
let table : Array[Int32] = Array::make(1024, -1) - 196
let colors : Array[Int32] = [] - 197●
let indices : Array[Int32] = Array::make(pixels.length(), 0) - 198●
for i in 0..<pixels.length() { - 199●
let argb = pixels[i] - 200
let mut slot = (argb * 0x9E3779B1) & 0x3FF - 201
let mut found = -1 - 202
while table[slot] >= 0 { - 203●
if colors[table[slot]] == argb { - 204●
found = table[slot] - 205
break - 206
} - 207
slot = (slot + 1) & 0x3FF - 208
} - 209
if found < 0 { - 210●
if colors.length() == 256 { - 211●
return None - 212
} - 213●
colors.push(argb) - 214●
found = colors.length() - 1 - 215
table[slot] = found - 216
} - 217
indices[i] = found - 218
} - 219●
let n = colors.length() - 220
let mut width_bits = if n <= 2 { - 221●
3 - 222●
} else if n <= 4 { - 223●
2 - 224●
} else if n <= 16 { - 225●
1 - 226
} else { - 227●
0 - 228
} - 229
// 宽度不能整除时退回不打包,避免解码侧展开后宽度越界 - 230
if width_bits > 0 && w % (1 << width_bits) != 0 { - 231●
width_bits = 0 - 232
} - 233
let bundle = 1 << width_bits - 234
let bits_per = 8 >> width_bits - 235
let packed_width = (w + bundle - 1) / bundle - 236●
let packed : Array[Int32] = Array::make( - 237●
packed_width * (pixels.length() / w), - 238
0, - 239
) - 240●
for y in 0..<(pixels.length() / w) { - 241●
for sx in 0..<packed_width { - 242●
let mut v = 0 - 243
for j in 0..<bundle { - 244●
let x = sx * bundle + j - 245
if x < w { - 246●
let idx = indices[y * w + x] - 247
v = v | (idx << (j * bits_per)) - 248
} - 249
} - 250
packed[y * packed_width + sx] = (255 << 24) | (v << 8) - 251
} - 252
} - 253
Some(Vp8lPalette::{ colors, packed, packed_width, }) - 254
} - 255
- 256
///| - 257
/// 调色板子图:1×N,delta 编码 - 258
fn vp8l_palette_delta(colors : Array[Int32]) -> Array[Int32] { - 259●
let out : Array[Int32] = Array::make(colors.length(), 0) - 260
out[0] = 0 - 261
let mut prev = 0 - 262●
for k in 0..<colors.length() { - 263●
let c = colors[k] - 264●
let a = (vp8l_ch(c, 24) - vp8l_ch(prev, 24)) & 0xFF - 265●
let r = (vp8l_ch(c, 16) - vp8l_ch(prev, 16)) & 0xFF - 266●
let g = (vp8l_ch(c, 8) - vp8l_ch(prev, 8)) & 0xFF - 267●
let b = (vp8l_ch(c, 0) - vp8l_ch(prev, 0)) & 0xFF - 268
out[k] = (a << 24) | (r << 16) | (g << 8) | b - 269
prev = c - 270
} - 271
out - 272
} - 273
- 274
// ==================== 对外单入口 ==================== - 275
- 276
///| - 277
fn vp8l_encode_palette_variant( - 278
w : Int32, - 279
h : Int32, - 280
pal : Vp8lPalette, - 281
alpha_solid : Bool, - 282
) -> Bytes { - 283●
let bw = Vp8lBits::new() - 284●
vp8l_write_header(bw, w, h, alpha_solid) - 285●
bw.write(1, 1) - 286●
bw.write(3, 2) - 287●
bw.write(pal.colors.length() - 1, 8) - 288●
vp8l_write_image_stream( - 289
bw, - 290●
vp8l_palette_delta(pal.colors), - 291●
pal.colors.length(), - 292
false, - 293
) - 294●
bw.write(0, 1) - 295●
vp8l_write_image_stream(bw, pal.packed, pal.packed_width, true) - 296●
bw.finish() - 297
} - 298
- 299
///| - 300
/// 无损 WebP 编码单入口:自动在 palette / subtract-green / predictor 之间选体积最小者。 - 301
/// - 302
/// 体积选择靠"实际编码一遍比长度",不做估计 —— 编码成本换确定性。 - 303
pub fn encode_webp_lossless(img : @types.Image) -> Bytes raise @types.LoadError { - 304●
let (pixels, w, h) = vp8l_pack_argb(img) - 305●
let alpha_solid = vp8l_alpha_solid(pixels) - 306
// 候选 1:palette(颜色数 ≤256 且平均每色 ≥4 像素时值得试) - 307●
let mut best : Bytes = Bytes::from_array([]) - 308●
match vp8l_try_palette(pixels, w) { - 309●
Some(pal) => { - 310●
let n = pal.colors.length() - 311●
if n * 4 <= pixels.length() { - 312●
best = vp8l_encode_palette_variant(w, h, pal, alpha_solid) - 313
} - 314
} - 315●
None => () - 316
} - 317
// 候选 2:原样 - 318
let plain = { - 319●
let bw = Vp8lBits::new() - 320●
vp8l_write_header(bw, w, h, alpha_solid) - 321●
bw.write(0, 1) - 322●
vp8l_write_image_stream(bw, pixels, w, true) - 323●
bw.finish() - 324
} - 325●
if best.length() == 0 || plain.length() < best.length() { - 326●
best = plain - 327
} - 328
// 候选 3:subtract-green - 329●
let sg_pixels = vp8l_apply_subtract_green(pixels) - 330
let sg = { - 331●
let bw = Vp8lBits::new() - 332●
vp8l_write_header(bw, w, h, alpha_solid) - 333●
bw.write(1, 1) - 334●
bw.write(2, 2) - 335●
bw.write(0, 1) - 336●
vp8l_write_image_stream(bw, sg_pixels, w, true) - 337●
bw.finish() - 338
} - 339●
if sg.length() < best.length() { - 340●
best = sg - 341
} - 342
// 候选 4:predictor(+subtract-green) - 343●
let (resid, modes, mode_w) = vp8l_forward_predictor(pixels, w, h, 4) - 344●
let mode_pixels : Array[Int32] = Array::make(modes.length(), 0) - 345●
for i in 0..<modes.length() { - 346●
mode_pixels[i] = (255 << 24) | (modes[i] << 8) - 347
} - 348●
let pred_resid = vp8l_apply_subtract_green(resid) - 349
let pred = { - 350●
let bw = Vp8lBits::new() - 351●
vp8l_write_header(bw, w, h, alpha_solid) - 352
// 变换列表按前向顺序书写:predictor forward → subtract-green forward; - 353
// 解码端从末尾倒序做逆变换,因此实际是 SG 逆变换 → predictor 逆变换。 - 354●
bw.write(1, 1) - 355●
bw.write(0, 2) - 356●
bw.write(4 - 2, 3) - 357●
vp8l_write_image_stream(bw, mode_pixels, mode_w, false) - 358●
bw.write(1, 1) - 359●
bw.write(2, 2) - 360●
bw.write(0, 1) - 361●
vp8l_write_image_stream(bw, pred_resid, w, true) - 362●
bw.finish() - 363
} - 364●
if pred.length() < best.length() { - 365●
best = pred - 366
} - 367●
vp8l_wrap_riff(best) - 368
}