image.codec\codec\webp_decode.mbt
94.1% · 32 / 34 points · 137 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 Lossless 解码器 WebP Lossless Decoder - 3
/// - 4
/// 纯 MoonBit WebP lossless (VP8L) 解码器,支持 RGBA 解码,不支持 lossy (VP8)。 Pure MoonBit WebP lossless (VP8L) decoder, supports RGBA decoding, does not support lossy (VP8). - 5
/// - 6
/// 核心函数 Core functions: - 7
/// - `decode_webp_pure` - 解码 WebP 字节流为 Image Decode WebP byte stream to Image - 8
- 9
// 纯 MoonBit WebP lossless 解码器 (VP8L) - 10
// 规范: https://developers.google.com/speed/webp/docs/compression - 11
// 支持 lossless RGBA 解码,不支持 lossy (VP8) - 12
- 13
// ==================== BitReader ==================== - 14
- 15
///| - 16
priv struct BitReader { - 17
data : Bytes - 18
mut pos : Int32 - 19
mut bit_buf : Int32 - 20
mut bit_count : Int32 - 21
} - 22
- 23
///| - 24
fn BitReader::new(data : Bytes, start : Int32) -> BitReader { - 25●
BitReader::{ data, pos: start, bit_buf: 0, bit_count: 0, } - 26
} - 27
- 28
///| - 29
fn BitReader::read_bits(self : BitReader, n : Int32) -> Int32 { - 30●
let mut result = 0 - 31
for i in 0..<n { - 32●
if self.bit_count == 0 { - 33●
if self.pos >= self.data.length() { - 34●
self.bit_buf = 0 - 35
self.bit_count = 8 - 36
} else { - 37●
self.bit_buf = self.data[self.pos].to_int() - 38
self.pos += 1 - 39
self.bit_count = 8 - 40
} - 41
} - 42
let bit = self.bit_buf & 1 - 43
self.bit_buf = self.bit_buf >> 1 - 44
self.bit_count -= 1 - 45
result = result | (bit << i) - 46
} - 47
result - 48
} - 49
- 50
///| - 51
fn BitReader::read_bit(self : BitReader) -> Int32 { - 52●
self.read_bits(1) - 53
} - 54
- 55
// ==================== Huffman Decoding ==================== - 56
- 57
///| - 58
priv struct HuffmanCode { - 59
mut value : Int32 - 60
mut left : Int32 - 61
mut right : Int32 - 62
} - 63
- 64
///| - 65
priv struct HuffmanTree { - 66
codes : Array[HuffmanCode] - 67
} - 68
- 69
///| - 70
fn HuffmanTree::new() -> HuffmanTree { - 71●
HuffmanTree::{ codes: [], } - 72
} - 73
- 74
///| - 75
fn HuffmanTree::add_code( - 76
self : HuffmanTree, - 77
code : Int32, - 78
code_len : Int32, - 79
symbol : Int32, - 80
) -> Unit { - 81
// Build a binary tree by walking the code bits MSB-first - 82●
let mut node = 0 - 83●
if self.codes.length() == 0 { - 84●
self.codes.push(HuffmanCode::{ value: -1, left: -1, right: -1, }) - 85
} - 86
for i in 0..<code_len { - 87●
let bit = (code >> (code_len - 1 - i)) & 1 - 88
let child = if bit == 0 { - 89●
self.codes[node].left - 90
} else { - 91●
self.codes[node].right - 92
} - 93
if child == -1 { - 94●
let new_idx = self.codes.length() - 95●
self.codes.push(HuffmanCode::{ value: -1, left: -1, right: -1, }) - 96
if bit == 0 { - 97●
self.codes[node].left = new_idx - 98
} else { - 99●
self.codes[node].right = new_idx - 100
} - 101
node = new_idx - 102
} else { - 103●
node = child - 104
} - 105
} - 106
self.codes[node].value = symbol - 107
} - 108
- 109
///| - 110
fn HuffmanTree::decode(self : HuffmanTree, br : BitReader) -> Int32 { - 111●
let mut node = 0 - 112●
if self.codes.length() == 0 { - 113○
return 0 - 114
} - 115
// 单符号码表:不消耗任何比特(RFC 9649 §3.2.2;此前按"读 1 位走左子树"会让后续流错位) - 116
if self.codes[0].value != -1 { - 117●
return self.codes[0].value - 118
} - 119
while true { - 120●
let bit = br.read_bit() - 121
let child = if bit == 0 { - 122●
self.codes[node].left - 123
} else { - 124●
self.codes[node].right - 125
} - 126
if child == -1 { - 127○
return self.codes[node].value - 128
} - 129
node = child - 130
if self.codes[node].value != -1 { - 131●
return self.codes[node].value - 132
} - 133
} - 134
0 - 135
} - 136
- 137
///|