image.codec\lib\animation.mbt

100% · 14 / 14 points · 44 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/// # 动画入口 Animation entry
  3. 3///
  4. 4/// 统一 GIF / APNG 的帧与延迟读取,并提供 APNG 编码包装。
  5. 5
  6. 6///|
  7. 7/// 解码动画(GIF / APNG),返回帧数组与每帧延迟(毫秒)。
  8. 8/// 非动画格式报 `DecodeFailed`。
  9. 9pub fn decode_animation(
  10. 10 data : Bytes,
  11. 11) -> (Array[@types.Image], Array[Int32]) raise @types.LoadError {
  12. 12● match detect_format(data) {
  13. 13● @types.ImageFormat::Apng => {
  14. 14● let anim = @codec.decode_apng_pure(data)
  15. 15 (anim.frames, anim.delays)
  16. 16 }
  17. 17● @types.ImageFormat::Gif => {
  18. 18● let anim = @codec.decode_gif_animation_pure(data)
  19. 19 (anim.frames, anim.delays)
  20. 20 }
  21. 21 _ =>
  22. 22● raise @types.LoadError::DecodeFailed(
  23. 23 "decode_animation 仅支持 GIF / APNG",
  24. 24 )
  25. 25 }
  26. 26}
  27. 27
  28. 28///|
  29. 29/// 将帧序列 + 延迟(毫秒)编码为 APNG。
  30. 30/// 帧数必须 ≥1,`delays` 长度必须与 `frames` 一致,所有帧尺寸必须相同。
  31. 31pub fn encode_apng(
  32. 32 frames : Array[@types.Image],
  33. 33 delays : Array[Int32],
  34. 34) -> Bytes raise @types.LoadError {
  35. 35● if frames.length() == 0 {
  36. 36● raise @types.LoadError::EncodeFailed("APNG 至少需要一帧")
  37. 37 }
  38. 38● if delays.length() != frames.length() {
  39. 39● raise @types.LoadError::EncodeFailed(
  40. 40 "APNG frames 与 delays 长度不一致",
  41. 41 )
  42. 42 }
  43. 43● @codec.encode_apng_pure(@types.PngAnimation::{ frames, delays, })
  44. 44}