image.codec\codec\webp_decode.mbt

94.1% · 32 / 34 points · 137 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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