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