image.codec\codec\webp_vp8l_encoder_core.mbt
97.1% · 101 / 104 points · 201 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 编码核心:可复用的图像流写入 + 变换选择 - 3
/// - 4
/// 分层: - 5
/// - `vp8l_pack_argb`:Image → RFC 顺序 ARGB 像素数组(a<<24 | r<<16 | g<<8 | b) - 6
/// - `vp8l_tokenize`:贪心 LZ77 切 token - 7
/// - `vp8l_write_image_stream`:写一条图像流(cache-info + [meta] + 5 棵树 + token) - 8
/// - `encode_webp_lossless`:对外单入口,自动选 palette / predictor / subtract-green - 9
- 10
///| - 11
priv struct Vp8lTokenStream { - 12
kinds : Array[Int32] - 13
v1 : Array[Int32] - 14
v2 : Array[Int32] - 15
} - 16
- 17
///| - 18
/// 打包为 RFC 顺序 ARGB(alpha 31..24、red 23..16、green 15..8、blue 7..0) - 19
fn vp8l_pack_argb( - 20
img : @types.Image, - 21
) -> (Array[Int32], Int32, Int32) raise @types.LoadError { - 22●
let w = img.width.to_int() - 23●
let h = img.height.to_int() - 24●
let ch = img.channels.to_int() - 25
if w <= 0 || h <= 0 { - 26○
raise @types.LoadError::DecodeFailed("WebP 编码:图像尺寸无效") - 27
} - 28
if w > 16384 || h > 16384 { - 29○
raise @types.LoadError::DecodeFailed( - 30
"WebP 编码:尺寸超出 VP8L 上限 16384", - 31
) - 32
} - 33
if ch != 3 && ch != 4 { - 34○
raise @types.LoadError::DecodeFailed("WebP 编码:仅支持 3/4 通道") - 35
} - 36
let n = w * h - 37●
let out : Array[Int32] = Array::make(n, 0) - 38
for i in 0..<n { - 39●
let base = i * ch - 40●
let r = img.data[base].to_int() - 41●
let g = img.data[base + 1].to_int() - 42●
let b = img.data[base + 2].to_int() - 43●
let a = if ch == 4 { img.data[base + 3].to_int() } else { 255 } - 44
out[i] = (a << 24) | (r << 16) | (g << 8) | b - 45
} - 46
(out, w, h) - 47
} - 48
- 49
///| - 50
fn vp8l_tokenize(pixels : Array[Int32]) -> Vp8lTokenStream { - 51●
let n = pixels.length() - 52
let kinds : Array[Int32] = [] - 53
let v1 : Array[Int32] = [] - 54
let v2 : Array[Int32] = [] - 55●
let head : Array[Int32] = Array::make(4096, -1) - 56●
let chain : Array[Int32] = Array::make(n, -1) - 57
let mut i = 0 - 58
while i < n { - 59●
let mut best_len = 0 - 60
let mut best_dist = 0 - 61
if i + 3 < n { - 62●
let hv = vp8l_hash_pixel(pixels[i]) - 63
let mut j = head[hv] - 64
let mut steps = 0 - 65
while j >= 0 && steps < 16 { - 66●
if pixels[j] == pixels[i] { - 67●
let mut l = 1 - 68
while l < 4096 && - 69
i + l < n && - 70
j + l < n && - 71
pixels[j + l] == pixels[i + l] { - 72●
l += 1 - 73
} - 74
if l >= 3 && l > best_len && i - j <= 1048000 { - 75●
best_len = l - 76
best_dist = i - j - 77
} - 78
} - 79
j = chain[j] - 80
steps += 1 - 81
} - 82
} - 83
if best_len >= 3 { - 84●
kinds.push(1) - 85●
v1.push(best_len) - 86●
v2.push(best_dist) - 87
for k in 0..<best_len { - 88●
if i + k + 3 < n { - 89●
let hv = vp8l_hash_pixel(pixels[i + k]) - 90
chain[i + k] = head[hv] - 91
head[hv] = i + k - 92
} - 93
} - 94
i += best_len - 95
} else { - 96●
kinds.push(0) - 97●
v1.push(pixels[i]) - 98●
v2.push(0) - 99
if i + 3 < n { - 100●
let hv = vp8l_hash_pixel(pixels[i]) - 101
chain[i] = head[hv] - 102
head[hv] = i - 103
} - 104
i += 1 - 105
} - 106
} - 107
Vp8lTokenStream::{ kinds, v1, v2, } - 108
} - 109
- 110
///| - 111
fn vp8l_ch(argb : Int32, shift : Int32) -> Int32 { - 112●
(argb >> shift) & 0xFF - 113
} - 114
- 115
///| - 116
/// 写一条图像流。`level0 = true` 时写 meta 位(主图),子分辨率图像不写。 - 117
fn vp8l_write_image_stream( - 118
bw : Vp8lBits, - 119
pixels : Array[Int32], - 120
width : Int32, - 121
level0 : Bool, - 122
) -> Unit { - 123●
let tok = vp8l_tokenize(pixels) - 124●
bw.write(0, 1) // 无色彩缓存 - 125
if level0 { - 126●
bw.write(0, 1) // 单组前缀码 - 127
} - 128●
let cg : Array[Int32] = Array::make(280, 0) - 129●
let cr : Array[Int32] = Array::make(256, 0) - 130●
let cb : Array[Int32] = Array::make(256, 0) - 131●
let ca : Array[Int32] = Array::make(256, 0) - 132●
let cd : Array[Int32] = Array::make(40, 0) - 133●
for t in 0..<tok.kinds.length() { - 134●
if tok.kinds[t] == 0 { - 135●
let argb = tok.v1[t] - 136●
cg[vp8l_ch(argb, 8)] += 1 - 137●
cr[vp8l_ch(argb, 16)] += 1 - 138●
cb[vp8l_ch(argb, 0)] += 1 - 139●
ca[vp8l_ch(argb, 24)] += 1 - 140
} else { - 141●
let (prefix, _, _) = vp8l_encode_length(tok.v1[t]) - 142
cg[256 + prefix] += 1 - 143●
let dcode = vp8l_distance_code(tok.v2[t], width) - 144●
let (dsym, _, _) = vp8l_encode_distance(dcode) - 145
cd[dsym] += 1 - 146
} - 147
} - 148●
let glens = vp8l_build_lengths(cg, 280, 15) - 149●
let rlens = vp8l_build_lengths(cr, 256, 15) - 150●
let blens = vp8l_build_lengths(cb, 256, 15) - 151●
let alens = vp8l_build_lengths(ca, 256, 15) - 152●
let dlens = vp8l_build_lengths(cd, 40, 15) - 153●
let gcodes = vp8l_canonical_codes(glens, 280) - 154●
let rcodes = vp8l_canonical_codes(rlens, 256) - 155●
let bcodes = vp8l_canonical_codes(blens, 256) - 156●
let acodes = vp8l_canonical_codes(alens, 256) - 157●
let dcodes = vp8l_canonical_codes(dlens, 40) - 158●
let g_single = vp8l_is_single(glens, 280) - 159●
let r_single = vp8l_is_single(rlens, 256) - 160●
let b_single = vp8l_is_single(blens, 256) - 161●
let a_single = vp8l_is_single(alens, 256) - 162●
let d_single = vp8l_is_single(dlens, 40) - 163●
vp8l_write_prefix_table(bw, glens, 280) - 164●
vp8l_write_prefix_table(bw, rlens, 256) - 165●
vp8l_write_prefix_table(bw, blens, 256) - 166●
vp8l_write_prefix_table(bw, alens, 256) - 167●
vp8l_write_prefix_table(bw, dlens, 40) - 168●
for t in 0..<tok.kinds.length() { - 169●
if tok.kinds[t] == 0 { - 170●
let argb = tok.v1[t] - 171
if !g_single { - 172●
let g = vp8l_ch(argb, 8) - 173●
vp8l_write_code(bw, gcodes[g], glens[g]) - 174
} - 175
if !r_single { - 176●
let r = vp8l_ch(argb, 16) - 177●
vp8l_write_code(bw, rcodes[r], rlens[r]) - 178
} - 179
if !b_single { - 180●
let b = vp8l_ch(argb, 0) - 181●
vp8l_write_code(bw, bcodes[b], blens[b]) - 182
} - 183
if !a_single { - 184●
let a = vp8l_ch(argb, 24) - 185●
vp8l_write_code(bw, acodes[a], alens[a]) - 186
} - 187
} else { - 188●
let (prefix, extra, ebits) = vp8l_encode_length(tok.v1[t]) - 189
if !g_single { - 190●
vp8l_write_code(bw, gcodes[256 + prefix], glens[256 + prefix]) - 191
} - 192●
bw.write(extra, ebits) - 193●
let dcode = vp8l_distance_code(tok.v2[t], width) - 194●
let (dsym, dextra, debits) = vp8l_encode_distance(dcode) - 195
if !d_single { - 196●
vp8l_write_code(bw, dcodes[dsym], dlens[dsym]) - 197
} - 198●
bw.write(dextra, debits) - 199
} - 200
} - 201
}