image.codec\codec\webp_vp8l_encode.mbt
97.1% · 99 / 102 points · 213 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # WebP VP8L 无损编码器(M1:literal-only) - 3
/// - 4
/// 规范:RFC 9649 §3(WebP Lossless Bitstream) - 5
/// 里程碑:M1 = 合法容器 + 5 棵前缀码 + literal 像素,产物可被标准解码器逐字节还原。 - 6
/// 尚未实现(见 docs/webp-encoder.md):LZ77(M2)、subtract-green/predictor/色彩缓存(M3)。 - 7
/// - 8
/// 设计取舍: - 9
/// - 位序 LSB-first,与 `webp_decode.mbt` 的 `BitReader` 一致; - 10
/// - Huffman 码按 canonical 规则计算后**按位反转**写入(解码器 MSB-first 累积), - 11
/// 因此 8 位符号码等于 `vp8l_reverse8(value)`; - 12
/// - 绿树字母表 280(256 literal + 24 长度码),必须写满,否则解码器会越界读下一棵树; - 13
/// - simple 码表里单符号 >1 时必须置 `is_first_8bits=1` 用 8 位写,否则值被截成 1 位。 - 14
- 15
///| - 16
/// VP8L 位写入器(LSB-first) - 17
priv struct Vp8lBits { - 18
bytes : Array[Byte] - 19
mut cur : Int32 - 20
mut nbits : Int32 - 21
} - 22
- 23
///| - 24
fn Vp8lBits::new() -> Vp8lBits { - 25●
{ bytes: [], cur: 0, nbits: 0, } - 26
} - 27
- 28
///| - 29
/// 写入 `n` 位(低位先出) - 30
fn Vp8lBits::write(self : Vp8lBits, value : Int32, n : Int32) -> Unit { - 31●
for i in 0..<n { - 32●
let bit = (value >> i) & 1 - 33
self.cur = self.cur | (bit << self.nbits) - 34
self.nbits = self.nbits + 1 - 35
if self.nbits == 8 { - 36●
self.bytes.push(self.cur.to_byte()) - 37
self.cur = 0 - 38
self.nbits = 0 - 39
} - 40
} - 41
} - 42
- 43
///| - 44
/// 收尾:补齐最后一个字节并返回 - 45
fn Vp8lBits::finish(self : Vp8lBits) -> Bytes { - 46●
if self.nbits > 0 { - 47●
self.bytes.push(self.cur.to_byte()) - 48
self.cur = 0 - 49
self.nbits = 0 - 50
} - 51●
Bytes::from_array(self.bytes) - 52
} - 53
- 54
///| - 55
/// 8 位反转(canonical 码按 MSB-first 由解码器累积,写入前需反转) - 56
fn vp8l_reverse8(v : Int32) -> Int32 { - 57●
let mut r = 0 - 58
for i in 0..<8 { - 59●
r = r | (((v >> i) & 1) << (7 - i)) - 60
} - 61
r - 62
} - 63
- 64
///| - 65
/// 码长码的写入顺序(RFC 9649 §3.2.1) - 66
let vp8l_code_length_order : Array[Int32] = [ - 67
17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 68
] - 69
- 70
///| - 71
/// 写一棵"前 256 个符号长 8、其余长 0"的 normal 码表。 - 72
/// - 73
/// 码长码只用 `{8, 18}` 两个符号:`8` 表示长度 8,`18` 表示 11–138 个长度为 0 的符号。 - 74
/// 两符号各 1 位,Kraft 和 = 1,解码器可直接建表。 - 75
/// `alphabet_size` 为绿树时传 280,红/蓝树传 256。 - 76
fn vp8l_write_flat8_normal_code(w : Vp8lBits, alphabet_size : Int32) -> Unit { - 77●
w.write(0, 1) - 78
// num_code_lengths = 4 + ReadBits(4);写 8 得到 12,覆盖 order[0..11](含 16 与 8) - 79●
w.write(8, 4) - 80
for i in 0..<12 { - 81●
let sym = vp8l_code_length_order[i] - 82●
w.write(if sym == 8 || sym == 18 { 1 } else { 0 }, 3) - 83
} - 84
// max_symbol 标志 0 = 读满整个字母表 - 85●
w.write(0, 1) - 86
// 256 个符号长度 8:码长码符号 8 的码是 0,写 256 个 0 位 - 87
for _ in 0..<256 { - 88●
w.write(0, 1) - 89
} - 90
// 余下符号长度 0:符号 18 的码是 1,附加 7 位写 "个数 - 11" - 91
let mut zeros = alphabet_size - 256 - 92
while zeros > 0 { - 93◐
let take = if zeros > 138 { 138 } else { zeros } - 94●
w.write(1, 1) - 95●
w.write(take - 11, 7) - 96
zeros = zeros - take - 97
} - 98
} - 99
- 100
///| - 101
/// 写一棵单符号 simple 码表(0 位/符号)。 - 102
/// 符号 >1 时必须用 8 位写,否则会被截断成 1 位值。 - 103
fn vp8l_write_simple_single(w : Vp8lBits, symbol : Int32) -> Unit { - 104●
w.write(1, 1) - 105●
w.write(0, 1) - 106
if symbol > 1 { - 107●
w.write(1, 1) - 108●
w.write(symbol, 8) - 109
} else { - 110●
w.write(0, 1) - 111●
w.write(symbol, 1) - 112
} - 113
} - 114
- 115
///| - 116
/// 把 VP8L 载荷包进 RIFF/WEBP 容器 - 117
fn vp8l_wrap_riff(payload : Bytes) -> Bytes { - 118●
let out : Array[Byte] = [] - 119●
out.push(0x52) - 120●
out.push(0x49) - 121●
out.push(0x46) - 122●
out.push(0x46) - 123●
let pad = payload.length() % 2 - 124●
write_u32_le(out, 4 + 8 + payload.length() + pad) - 125●
out.push(0x57) - 126●
out.push(0x45) - 127●
out.push(0x42) - 128●
out.push(0x50) - 129●
out.push(0x56) - 130●
out.push(0x50) - 131●
out.push(0x38) - 132●
out.push(0x4C) - 133●
write_u32_le(out, payload.length()) - 134
for b in payload { - 135●
out.push(b) - 136
} - 137
if pad != 0 { - 138●
out.push(0) - 139
} - 140●
Bytes::from_array(out) - 141
} - 142
- 143
///| - 144
/// M1 编码器:literal-only 的合法 VP8L 产物。 - 145
/// - 146
/// 参数:`img` 支持 channels = 3(RGB)或 4(RGBA);尺寸上限 16384。 - 147
/// 返回:完整的 RIFF/WEBP 字节流(单 VP8L chunk)。 - 148
/// - 149
/// 注意:本实现体积约 3 字节/像素(劣于 PNG),LZ77 与变换完成后才会优于 PNG。 - 150
/// 当前仅供内部验证与 M2/M3 基线,不作为对外 API 承诺。 - 151
/// - 152
/// ```moonbit nocheck - 153
/// let bytes = @codec.encode_webp_vp8l_literal(img) - 154
/// ``` - 155
pub fn encode_webp_vp8l_literal( - 156
img : @types.Image, - 157
) -> Bytes raise @types.LoadError { - 158●
let w = img.width.to_int() - 159●
let h = img.height.to_int() - 160
if w <= 0 || h <= 0 { - 161○
raise @types.LoadError::DecodeFailed("WebP 编码:图像尺寸无效") - 162
} - 163
if w > 16384 || h > 16384 { - 164●
raise @types.LoadError::DecodeFailed( - 165
"WebP 编码:尺寸超出 VP8L 上限 16384", - 166
) - 167
} - 168●
let ch = img.channels.to_int() - 169
if ch != 3 && ch != 4 { - 170○
raise @types.LoadError::DecodeFailed("WebP 编码:仅支持 3/4 通道") - 171
} - 172●
let bw = Vp8lBits::new() - 173●
bw.write(0x2F, 8) - 174●
bw.write(w - 1, 14) - 175●
bw.write(h - 1, 14) - 176●
bw.write(1, 1) // alpha_is_used - 177●
bw.write(0, 3) // version - 178●
bw.write(0, 1) // 无 transform - 179●
bw.write(0, 1) // 无色彩缓存 - 180●
bw.write(0, 1) // 单 meta 前缀码组 - 181
// alpha 是否全不透明:全不透明时用单符号码表,省掉每像素 8 位 - 182
let mut all_solid_alpha = true - 183
if ch == 4 { - 184●
for i in 0..<(w * h) { - 185●
if img.data[i * 4 + 3].to_int() != 255 { - 186●
all_solid_alpha = false - 187
break - 188
} - 189
} - 190
} - 191●
vp8l_write_flat8_normal_code(bw, 280) - 192●
vp8l_write_flat8_normal_code(bw, 256) - 193●
vp8l_write_flat8_normal_code(bw, 256) - 194
if all_solid_alpha { - 195●
vp8l_write_simple_single(bw, 255) - 196
} else { - 197●
vp8l_write_flat8_normal_code(bw, 256) - 198
} - 199●
vp8l_write_simple_single(bw, 0) // 距离码(literal-only 不使用) - 200
for i in 0..<(w * h) { - 201●
let base = i * ch - 202●
let r = img.data[base].to_int() - 203●
let g = img.data[base + 1].to_int() - 204●
let b = img.data[base + 2].to_int() - 205●
bw.write(vp8l_reverse8(g), 8) - 206●
bw.write(vp8l_reverse8(r), 8) - 207●
bw.write(vp8l_reverse8(b), 8) - 208
if !all_solid_alpha { - 209●
bw.write(vp8l_reverse8(img.data[base + 3].to_int()), 8) - 210
} - 211
} - 212●
vp8l_wrap_riff(bw.finish()) - 213
}