image.codec\codec\webp_vp8l_encoder_core.mbt

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