image.codec\codec\webp_vp8l_encoder_transforms.mbt

98.1% · 210 / 214 points · 368 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 编码变换:palette / predictor / subtract-green 与对外单入口
  3. 3
  4. 4///|
  5. 5fn vp8l_write_header(
  6. 6 bw : Vp8lBits,
  7. 7 w : Int32,
  8. 8 h : Int32,
  9. 9 alpha_solid : Bool,
  10. 10) -> Unit {
  11. 11● bw.write(0x2F, 8)
  12. 12● bw.write(w - 1, 14)
  13. 13● bw.write(h - 1, 14)
  14. 14◐ bw.write(if alpha_solid { 0 } else { 1 }, 1)
  15. 15● bw.write(0, 3)
  16. 16}
  17. 17
  18. 18///|
  19. 19fn vp8l_alpha_solid(pixels : Array[Int32]) -> Bool {
  20. 20● for i in 0..<pixels.length() {
  21. 21● if vp8l_ch(pixels[i], 24) != 255 {
  22. 22○ return false
  23. 23 }
  24. 24 }
  25. 25 true
  26. 26}
  27. 27
  28. 28// ==================== 变换:subtract-green ====================
  29. 29
  30. 30///|
  31. 31fn vp8l_apply_subtract_green(pixels : Array[Int32]) -> Array[Int32] {
  32. 32● let out : Array[Int32] = Array::make(pixels.length(), 0)
  33. 33● for i in 0..<pixels.length() {
  34. 34● let argb = pixels[i]
  35. 35● let g = vp8l_ch(argb, 8)
  36. 36● let r = (vp8l_ch(argb, 16) - g) & 0xFF
  37. 37● let b = (vp8l_ch(argb, 0) - g) & 0xFF
  38. 38● out[i] = (vp8l_ch(argb, 24) << 24) | (r << 16) | (g << 8) | b
  39. 39 }
  40. 40 out
  41. 41}
  42. 42
  43. 43// ==================== 变换:predictor ====================
  44. 44
  45. 45///|
  46. 46fn vp8l_predict_value(
  47. 47 mode : Int32,
  48. 48 l : Int32,
  49. 49 t : Int32,
  50. 50 tl : Int32,
  51. 51 tr : Int32,
  52. 52) -> Int32 {
  53. 53● if mode == 0 {
  54. 54● 0xFF000000
  55. 55● } else if mode == 1 {
  56. 56● l
  57. 57● } else if mode == 2 {
  58. 58● t
  59. 59● } else if mode == 3 {
  60. 60● tr
  61. 61● } else if mode == 4 {
  62. 62● tl
  63. 63● } else if mode == 5 {
  64. 64● vp8l_avg2(vp8l_avg2(l, tr), t)
  65. 65● } else if mode == 6 {
  66. 66● vp8l_avg2(l, tl)
  67. 67● } else if mode == 7 {
  68. 68● vp8l_avg2(l, t)
  69. 69● } else if mode == 8 {
  70. 70● vp8l_avg2(tl, t)
  71. 71● } else if mode == 9 {
  72. 72● vp8l_avg2(t, tr)
  73. 73● } else if mode == 10 {
  74. 74● vp8l_avg2(vp8l_avg2(l, tl), vp8l_avg2(t, tr))
  75. 75● } else if mode == 11 {
  76. 76● vp8l_select(l, t, tl)
  77. 77● } else if mode == 12 {
  78. 78● vp8l_clamp_add_full(l, t, tl)
  79. 79 } else {
  80. 80● vp8l_clamp_add_half(vp8l_avg2(l, t), tl)
  81. 81 }
  82. 82}
  83. 83
  84. 84///|
  85. 85/// 按 RFC 边界规则取预测值(编码侧邻居用原图,解码侧邻居是已重建像素,两者等价)
  86. 86fn vp8l_predict_pixel(
  87. 87 pixels : Array[Int32],
  88. 88 w : Int32,
  89. 89 x : Int32,
  90. 90 y : Int32,
  91. 91 mode : Int32,
  92. 92) -> Int32 {
  93. 93● let i = y * w + x
  94. 94 if y == 0 {
  95. 95● if x == 0 {
  96. 96● 0xFF000000
  97. 97 } else {
  98. 98● pixels[i - 1]
  99. 99 }
  100. 100● } else if x == 0 {
  101. 101● pixels[i - w]
  102. 102 } else {
  103. 103● let l = pixels[i - 1]
  104. 104 let t = pixels[i - w]
  105. 105 let tl = pixels[i - w - 1]
  106. 106● let tr = if x + 1 < w { pixels[i - w + 1] } else { pixels[y * w] }
  107. 107● vp8l_predict_value(mode, l, t, tl, tr)
  108. 108 }
  109. 109}
  110. 110
  111. 111///|
  112. 112/// 残差代价:四通道环差绝对值之和。四个通道手工展开 —— 这里在内层 14×像素 的循环里
  113. 113/// 每像素调用一次,用 `for shift in [...]` 会在每次调用分配一个数组。
  114. 114fn vp8l_residual_cost(orig : Int32, pred : Int32) -> Int32 {
  115. 115● let mut c = 0
  116. 116● let d0 = (vp8l_ch(orig, 24) - vp8l_ch(pred, 24)) & 0xFF
  117. 117◐ c += if d0 < 128 { d0 } else { 256 - d0 }
  118. 118● let d1 = (vp8l_ch(orig, 16) - vp8l_ch(pred, 16)) & 0xFF
  119. 119● c += if d1 < 128 { d1 } else { 256 - d1 }
  120. 120● let d2 = (vp8l_ch(orig, 8) - vp8l_ch(pred, 8)) & 0xFF
  121. 121● c += if d2 < 128 { d2 } else { 256 - d2 }
  122. 122● let d3 = (vp8l_ch(orig, 0) - vp8l_ch(pred, 0)) & 0xFF
  123. 123● c += if d3 < 128 { d3 } else { 256 - d3 }
  124. 124 c
  125. 125}
  126. 126
  127. 127///|
  128. 128/// 前向 predictor:逐 16² 块选模式,返回 (残差像素, 模式图)
  129. 129fn vp8l_forward_predictor(
  130. 130 pixels : Array[Int32],
  131. 131 w : Int32,
  132. 132 h : Int32,
  133. 133 block_bits : Int32,
  134. 134) -> (Array[Int32], Array[Int32], Int32) {
  135. 135● let bs = 1 << block_bits
  136. 136 let bx = (w + bs - 1) / bs
  137. 137 let by = (h + bs - 1) / bs
  138. 138● let modes : Array[Int32] = Array::make(bx * by, 0)
  139. 139 for bloc_y in 0..<by {
  140. 140● for bloc_x in 0..<bx {
  141. 141● let x0 = bloc_x * bs
  142. 142 let y0 = bloc_y * bs
  143. 143● let x1 = if x0 + bs > w { w } else { x0 + bs }
  144. 144● let y1 = if y0 + bs > h { h } else { y0 + bs }
  145. 145 let mut best_mode = 1
  146. 146 let mut best_cost = -1
  147. 147 for mode in 0..<14 {
  148. 148● let mut cost = 0
  149. 149 for yy in y0..<y1 {
  150. 150● for xx in x0..<x1 {
  151. 151● let i = yy * w + xx
  152. 152● let pred = vp8l_predict_pixel(pixels, w, xx, yy, mode)
  153. 153● cost += vp8l_residual_cost(pixels[i], pred)
  154. 154 }
  155. 155 }
  156. 156 if best_cost < 0 || cost < best_cost {
  157. 157● best_cost = cost
  158. 158 best_mode = mode
  159. 159 }
  160. 160 }
  161. 161 modes[bloc_y * bx + bloc_x] = best_mode
  162. 162 }
  163. 163 }
  164. 164● let resid : Array[Int32] = Array::make(w * h, 0)
  165. 165 for y in 0..<h {
  166. 166● for x in 0..<w {
  167. 167● let i = y * w + x
  168. 168 let mode = if y < by * bs && x < bx * bs {
  169. 169● modes[y / bs * bx + x / bs]
  170. 170 } else {
  171. 171○ 1
  172. 172 }
  173. 173● let pred = vp8l_predict_pixel(pixels, w, x, y, mode)
  174. 174● let a = (vp8l_ch(pixels[i], 24) - vp8l_ch(pred, 24)) & 0xFF
  175. 175● let r = (vp8l_ch(pixels[i], 16) - vp8l_ch(pred, 16)) & 0xFF
  176. 176● let g = (vp8l_ch(pixels[i], 8) - vp8l_ch(pred, 8)) & 0xFF
  177. 177● let b = (vp8l_ch(pixels[i], 0) - vp8l_ch(pred, 0)) & 0xFF
  178. 178 resid[i] = (a << 24) | (r << 16) | (g << 8) | b
  179. 179 }
  180. 180 }
  181. 181 (resid, modes, bx)
  182. 182}
  183. 183
  184. 184// ==================== 变换:palette ====================
  185. 185
  186. 186///|
  187. 187priv struct Vp8lPalette {
  188. 188 colors : Array[Int32]
  189. 189 packed : Array[Int32]
  190. 190 packed_width : Int32
  191. 191}
  192. 192
  193. 193///|
  194. 194fn vp8l_try_palette(pixels : Array[Int32], w : Int32) -> Vp8lPalette? {
  195. 195● let table : Array[Int32] = Array::make(1024, -1)
  196. 196 let colors : Array[Int32] = []
  197. 197● let indices : Array[Int32] = Array::make(pixels.length(), 0)
  198. 198● for i in 0..<pixels.length() {
  199. 199● let argb = pixels[i]
  200. 200 let mut slot = (argb * 0x9E3779B1) & 0x3FF
  201. 201 let mut found = -1
  202. 202 while table[slot] >= 0 {
  203. 203● if colors[table[slot]] == argb {
  204. 204● found = table[slot]
  205. 205 break
  206. 206 }
  207. 207 slot = (slot + 1) & 0x3FF
  208. 208 }
  209. 209 if found < 0 {
  210. 210● if colors.length() == 256 {
  211. 211● return None
  212. 212 }
  213. 213● colors.push(argb)
  214. 214● found = colors.length() - 1
  215. 215 table[slot] = found
  216. 216 }
  217. 217 indices[i] = found
  218. 218 }
  219. 219● let n = colors.length()
  220. 220 let mut width_bits = if n <= 2 {
  221. 221● 3
  222. 222● } else if n <= 4 {
  223. 223● 2
  224. 224● } else if n <= 16 {
  225. 225● 1
  226. 226 } else {
  227. 227● 0
  228. 228 }
  229. 229 // 宽度不能整除时退回不打包,避免解码侧展开后宽度越界
  230. 230 if width_bits > 0 && w % (1 << width_bits) != 0 {
  231. 231● width_bits = 0
  232. 232 }
  233. 233 let bundle = 1 << width_bits
  234. 234 let bits_per = 8 >> width_bits
  235. 235 let packed_width = (w + bundle - 1) / bundle
  236. 236● let packed : Array[Int32] = Array::make(
  237. 237● packed_width * (pixels.length() / w),
  238. 238 0,
  239. 239 )
  240. 240● for y in 0..<(pixels.length() / w) {
  241. 241● for sx in 0..<packed_width {
  242. 242● let mut v = 0
  243. 243 for j in 0..<bundle {
  244. 244● let x = sx * bundle + j
  245. 245 if x < w {
  246. 246● let idx = indices[y * w + x]
  247. 247 v = v | (idx << (j * bits_per))
  248. 248 }
  249. 249 }
  250. 250 packed[y * packed_width + sx] = (255 << 24) | (v << 8)
  251. 251 }
  252. 252 }
  253. 253 Some(Vp8lPalette::{ colors, packed, packed_width, })
  254. 254}
  255. 255
  256. 256///|
  257. 257/// 调色板子图:1×N,delta 编码
  258. 258fn vp8l_palette_delta(colors : Array[Int32]) -> Array[Int32] {
  259. 259● let out : Array[Int32] = Array::make(colors.length(), 0)
  260. 260 out[0] = 0
  261. 261 let mut prev = 0
  262. 262● for k in 0..<colors.length() {
  263. 263● let c = colors[k]
  264. 264● let a = (vp8l_ch(c, 24) - vp8l_ch(prev, 24)) & 0xFF
  265. 265● let r = (vp8l_ch(c, 16) - vp8l_ch(prev, 16)) & 0xFF
  266. 266● let g = (vp8l_ch(c, 8) - vp8l_ch(prev, 8)) & 0xFF
  267. 267● let b = (vp8l_ch(c, 0) - vp8l_ch(prev, 0)) & 0xFF
  268. 268 out[k] = (a << 24) | (r << 16) | (g << 8) | b
  269. 269 prev = c
  270. 270 }
  271. 271 out
  272. 272}
  273. 273
  274. 274// ==================== 对外单入口 ====================
  275. 275
  276. 276///|
  277. 277fn vp8l_encode_palette_variant(
  278. 278 w : Int32,
  279. 279 h : Int32,
  280. 280 pal : Vp8lPalette,
  281. 281 alpha_solid : Bool,
  282. 282) -> Bytes {
  283. 283● let bw = Vp8lBits::new()
  284. 284● vp8l_write_header(bw, w, h, alpha_solid)
  285. 285● bw.write(1, 1)
  286. 286● bw.write(3, 2)
  287. 287● bw.write(pal.colors.length() - 1, 8)
  288. 288● vp8l_write_image_stream(
  289. 289 bw,
  290. 290● vp8l_palette_delta(pal.colors),
  291. 291● pal.colors.length(),
  292. 292 false,
  293. 293 )
  294. 294● bw.write(0, 1)
  295. 295● vp8l_write_image_stream(bw, pal.packed, pal.packed_width, true)
  296. 296● bw.finish()
  297. 297}
  298. 298
  299. 299///|
  300. 300/// 无损 WebP 编码单入口:自动在 palette / subtract-green / predictor 之间选体积最小者。
  301. 301///
  302. 302/// 体积选择靠"实际编码一遍比长度",不做估计 —— 编码成本换确定性。
  303. 303pub fn encode_webp_lossless(img : @types.Image) -> Bytes raise @types.LoadError {
  304. 304● let (pixels, w, h) = vp8l_pack_argb(img)
  305. 305● let alpha_solid = vp8l_alpha_solid(pixels)
  306. 306 // 候选 1:palette(颜色数 ≤256 且平均每色 ≥4 像素时值得试)
  307. 307● let mut best : Bytes = Bytes::from_array([])
  308. 308● match vp8l_try_palette(pixels, w) {
  309. 309● Some(pal) => {
  310. 310● let n = pal.colors.length()
  311. 311● if n * 4 <= pixels.length() {
  312. 312● best = vp8l_encode_palette_variant(w, h, pal, alpha_solid)
  313. 313 }
  314. 314 }
  315. 315● None => ()
  316. 316 }
  317. 317 // 候选 2:原样
  318. 318 let plain = {
  319. 319● let bw = Vp8lBits::new()
  320. 320● vp8l_write_header(bw, w, h, alpha_solid)
  321. 321● bw.write(0, 1)
  322. 322● vp8l_write_image_stream(bw, pixels, w, true)
  323. 323● bw.finish()
  324. 324 }
  325. 325● if best.length() == 0 || plain.length() < best.length() {
  326. 326● best = plain
  327. 327 }
  328. 328 // 候选 3:subtract-green
  329. 329● let sg_pixels = vp8l_apply_subtract_green(pixels)
  330. 330 let sg = {
  331. 331● let bw = Vp8lBits::new()
  332. 332● vp8l_write_header(bw, w, h, alpha_solid)
  333. 333● bw.write(1, 1)
  334. 334● bw.write(2, 2)
  335. 335● bw.write(0, 1)
  336. 336● vp8l_write_image_stream(bw, sg_pixels, w, true)
  337. 337● bw.finish()
  338. 338 }
  339. 339● if sg.length() < best.length() {
  340. 340● best = sg
  341. 341 }
  342. 342 // 候选 4:predictor(+subtract-green)
  343. 343● let (resid, modes, mode_w) = vp8l_forward_predictor(pixels, w, h, 4)
  344. 344● let mode_pixels : Array[Int32] = Array::make(modes.length(), 0)
  345. 345● for i in 0..<modes.length() {
  346. 346● mode_pixels[i] = (255 << 24) | (modes[i] << 8)
  347. 347 }
  348. 348● let pred_resid = vp8l_apply_subtract_green(resid)
  349. 349 let pred = {
  350. 350● let bw = Vp8lBits::new()
  351. 351● vp8l_write_header(bw, w, h, alpha_solid)
  352. 352 // 变换列表按前向顺序书写:predictor forward → subtract-green forward;
  353. 353 // 解码端从末尾倒序做逆变换,因此实际是 SG 逆变换 → predictor 逆变换。
  354. 354● bw.write(1, 1)
  355. 355● bw.write(0, 2)
  356. 356● bw.write(4 - 2, 3)
  357. 357● vp8l_write_image_stream(bw, mode_pixels, mode_w, false)
  358. 358● bw.write(1, 1)
  359. 359● bw.write(2, 2)
  360. 360● bw.write(0, 1)
  361. 361● vp8l_write_image_stream(bw, pred_resid, w, true)
  362. 362● bw.finish()
  363. 363 }
  364. 364● if pred.length() < best.length() {
  365. 365● best = pred
  366. 366 }
  367. 367● vp8l_wrap_riff(best)
  368. 368}