image.codec\codec\apng_codec.mbt
92.8% · 192 / 207 points · 379 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # APNG 编解码 APNG Codec - 3
/// - 4
/// APNG 动画 PNG 编解码实现 - 5
/// APNG 动画编解码 Animated PNG encoding/decoding - 6
// 纯 MoonBit APNG 编解码(全目标可用:native/wasm/js) - 7
// APNG 规范: https://wiki.mozilla.org/APNG_Specification - 8
// APNG 是 PNG 的扩展,新增 acTL/fcTL/fdAT chunk - 9
// 仅支持全尺寸帧(x_offset=0, y_offset=0, frame_w=width, frame_h=height) - 10
- 11
///| - 12
/// 从 4 字节构造 chunk type 字符串 - 13
fn apng_chunk_type(data : Bytes, offset : Int32) -> String { - 14●
let buf : Array[Byte] = [ - 15
data[offset], - 16
data[offset + 1], - 17
data[offset + 2], - 18
data[offset + 3], - 19
] - 20●
@encoding/utf8.decode(Bytes::from_array(buf)) catch { - 21○
_ => "????" - 22
} - 23
} - 24
- 25
///| - 26
/// PNG chunk 信息 - 27
priv struct PngChunk { - 28
chunk_type : String - 29
data_offset : Int32 - 30
data_size : Int32 - 31
} - 32
- 33
///| - 34
/// 解析所有 PNG chunk - 35
fn apng_parse_chunks(data : Bytes) -> Array[PngChunk] raise @types.LoadError { - 36●
let len = data.length() - 37
if len < 8 { - 38●
raise @types.LoadError::DecodeFailed("PNG/APNG 数据过短") - 39
} - 40
// 验证 PNG signature - 41●
if !is_png_signature(data, 0) { - 42●
raise @types.LoadError::DecodeFailed("PNG signature 错误") - 43
} - 44
let chunks : Array[PngChunk] = [] - 45
let mut pos = 8 - 46
while pos + 8 <= len { - 47●
let chunk_len = read_u32_be(data, pos) - 48●
let chunk_type = apng_chunk_type(data, pos + 4) - 49
pos += 8 - 50
if chunk_len < 0 || chunk_len > len - pos - 4 { - 51●
raise @types.LoadError::DecodeFailed("PNG chunk 数据越界") - 52
} - 53●
chunks.push(PngChunk::{ - 54
chunk_type, - 55
data_offset: pos, - 56
data_size: chunk_len, - 57
}) - 58
pos += chunk_len + 4 // 跳过数据 + CRC - 59
} - 60
chunks - 61
} - 62
- 63
///| - 64
/// 从 chunk 数据中提取子字节流 - 65
fn apng_extract_chunk_data(data : Bytes, chunk : PngChunk) -> Bytes { - 66●
let out : Array[Byte] = [] - 67
for i in 0..<chunk.data_size { - 68●
out.push(data[chunk.data_offset + i]) - 69
} - 70●
Bytes::from_array(out) - 71
} - 72
- 73
///| - 74
/// 构建独立 PNG 文件(signature + IHDR + IDAT + IEND) - 75
fn build_standalone_png(ihdr_data : Bytes, idat_data : Bytes) -> Bytes { - 76●
let out : Array[Byte] = [] - 77
// PNG signature - 78●
out.push(b'\x89') - 79●
out.push(b'\x50') - 80●
out.push(b'\x4E') - 81●
out.push(b'\x47') - 82●
out.push(b'\x0D') - 83●
out.push(b'\x0A') - 84●
out.push(b'\x1A') - 85●
out.push(b'\x0A') - 86
// IHDR - 87●
write_u32_be(out, ihdr_data.length()) - 88●
out.push(b'\x49') - 89●
out.push(b'\x48') - 90●
out.push(b'\x44') - 91●
out.push(b'\x52') - 92●
for i in 0..<ihdr_data.length() { - 93●
out.push(ihdr_data[i]) - 94
} - 95
// IHDR CRC (simplified: compute over type+data) - 96
let crc_data : Array[Byte] = [b'\x49', b'\x48', b'\x44', b'\x52'] - 97●
for i in 0..<ihdr_data.length() { - 98●
crc_data.push(ihdr_data[i]) - 99
} - 100●
let ihdr_crc = @util.crc32(Bytes::from_array(crc_data)) - 101●
write_u32_be(out, ihdr_crc) - 102
// IDAT - 103●
write_u32_be(out, idat_data.length()) - 104●
out.push(b'\x49') - 105●
out.push(b'\x44') - 106●
out.push(b'\x41') - 107●
out.push(b'\x54') - 108●
for i in 0..<idat_data.length() { - 109●
out.push(idat_data[i]) - 110
} - 111
let idat_crc_data : Array[Byte] = [b'\x49', b'\x44', b'\x41', b'\x54'] - 112●
for i in 0..<idat_data.length() { - 113●
idat_crc_data.push(idat_data[i]) - 114
} - 115●
let idat_crc = @util.crc32(Bytes::from_array(idat_crc_data)) - 116●
write_u32_be(out, idat_crc) - 117
// IEND - 118●
write_u32_be(out, 0) - 119●
out.push(b'\x49') - 120●
out.push(b'\x45') - 121●
out.push(b'\x4E') - 122●
out.push(b'\x44') - 123●
let iend_crc = @util.crc32(b"\x49\x45\x4E\x44") - 124●
write_u32_be(out, iend_crc) - 125●
Bytes::from_array(out) - 126
} - 127
- 128
///| - 129
/// 解码 APNG 字节流为 PngAnimation - 130
/// 如果不是 APNG(无 acTL chunk),则当作单帧 PNG 返回 - 131
pub fn decode_apng_pure( - 132
data : Bytes, - 133
) -> @types.PngAnimation raise @types.LoadError { - 134●
let chunks = apng_parse_chunks(data) - 135
// 查找 IHDR - 136
let mut ihdr_data : Bytes = b"" - 137
let mut found_ihdr = false - 138●
for i in 0..<chunks.length() { - 139●
if chunks[i].chunk_type == "IHDR" { - 140●
ihdr_data = apng_extract_chunk_data(data, chunks[i]) - 141
found_ihdr = true - 142
break - 143
} - 144
} - 145
if !found_ihdr { - 146●
raise @types.LoadError::DecodeFailed("PNG/APNG 缺少 IHDR") - 147
} - 148
// 查找 acTL - 149
let mut has_actl = false - 150●
for i in 0..<chunks.length() { - 151●
if chunks[i].chunk_type == "acTL" { - 152●
has_actl = true - 153
break - 154
} - 155
} - 156
if !has_actl { - 157
// 普通 PNG,当作单帧动画 - 158●
let img = decode_png_pure(data) - 159
return @types.PngAnimation::{ frames: [img], delays: [0], } - 160
} - 161
// APNG: 收集所有帧 - 162
let frames : Array[@types.Image] = [] - 163
let delays : Array[Int32] = [] - 164
// 收集 IDAT 数据(第一帧可能用 IDAT) - 165
let mut idat_data : Bytes = b"" - 166
let mut found_idat = false - 167●
for i in 0..<chunks.length() { - 168●
if chunks[i].chunk_type == "IDAT" { - 169●
let chunk_data = apng_extract_chunk_data(data, chunks[i]) - 170
if !found_idat { - 171●
idat_data = chunk_data - 172
found_idat = true - 173
} else { - 174
// 合并多个 IDAT - 175○
let merged : Array[Byte] = [] - 176○
for j in 0..<idat_data.length() { - 177○
merged.push(idat_data[j]) - 178
} - 179○
for j in 0..<chunk_data.length() { - 180○
merged.push(chunk_data[j]) - 181
} - 182○
idat_data = Bytes::from_array(merged) - 183
} - 184
} - 185
} - 186
// 遍历 chunk,处理 fcTL + IDAT/fdAT - 187
let mut i = 0 - 188
let mut first_frame_done = false - 189●
while i < chunks.length() { - 190●
if chunks[i].chunk_type == "fcTL" { - 191
// 解析 fcTL - 192●
let fctl_data = apng_extract_chunk_data(data, chunks[i]) - 193
// delay_num (2 bytes at offset 20), delay_den (2 bytes at offset 22) - 194●
let delay_num = read_u16_be(fctl_data, 20) - 195●
let delay_den = read_u16_be(fctl_data, 22) - 196
let delay_ms = if delay_den == 0 { - 197○
0 - 198
} else { - 199●
delay_num * 1000 / delay_den - 200
} - 201
// 查找对应的帧数据(IDAT 或 fdAT) - 202
let mut frame_idat : Bytes = b"" - 203
let mut found_frame_data = false - 204
let mut j = i + 1 - 205●
while j < chunks.length() { - 206●
if chunks[j].chunk_type == "IDAT" && !first_frame_done { - 207●
frame_idat = idat_data - 208
found_frame_data = true - 209
first_frame_done = true - 210
break - 211●
} else if chunks[j].chunk_type == "fdAT" { - 212
// fdAT: 前 4 字节是 seq_num,后面是 IDAT 数据 - 213●
let fdat_data = apng_extract_chunk_data(data, chunks[j]) - 214
let fdat_idat : Array[Byte] = [] - 215●
for k in 4..<fdat_data.length() { - 216●
fdat_idat.push(fdat_data[k]) - 217
} - 218●
frame_idat = Bytes::from_array(fdat_idat) - 219
found_frame_data = true - 220
break - 221○
} else if chunks[j].chunk_type == "fcTL" { - 222
// 遇到下一个 fcTL,停止 - 223○
break - 224
} - 225
j += 1 - 226
} - 227
if found_frame_data { - 228●
let png = build_standalone_png(ihdr_data, frame_idat) - 229●
let img = decode_png_pure(png) - 230●
frames.push(img) - 231●
delays.push(delay_ms) - 232
} - 233
} - 234
i += 1 - 235
} - 236●
if frames.length() == 0 { - 237○
raise @types.LoadError::DecodeFailed("APNG 无有效帧") - 238
} - 239
@types.PngAnimation::{ frames, delays, } - 240
} - 241
- 242
///| - 243
/// 从 PNG 字节流中提取 IDAT 数据 - 244
fn extract_idat_from_png(png_data : Bytes) -> Bytes raise @types.LoadError { - 245●
let chunks = apng_parse_chunks(png_data) - 246
let idat : Array[Byte] = [] - 247●
for i in 0..<chunks.length() { - 248●
if chunks[i].chunk_type == "IDAT" { - 249●
for j in 0..<chunks[i].data_size { - 250●
idat.push(png_data[chunks[i].data_offset + j]) - 251
} - 252
} - 253
} - 254●
Bytes::from_array(idat) - 255
} - 256
- 257
///| - 258
/// 从 PNG 字节流中提取 IHDR 数据 - 259
fn extract_ihdr_from_png(png_data : Bytes) -> Bytes raise @types.LoadError { - 260●
let chunks = apng_parse_chunks(png_data) - 261●
for i in 0..<chunks.length() { - 262●
if chunks[i].chunk_type == "IHDR" { - 263●
return apng_extract_chunk_data(png_data, chunks[i]) - 264
} - 265
} - 266
raise @types.LoadError::DecodeFailed("PNG 缺少 IHDR") - 267
} - 268
- 269
///| - 270
/// 写入 PNG chunk(type + data + CRC) - 271
fn apng_write_chunk( - 272
out : Array[Byte], - 273
chunk_type : String, - 274
chunk_data : Bytes, - 275
) -> Unit { - 276●
write_u32_be(out, chunk_data.length()) - 277●
let type_bytes = @encoding/utf8.encode(chunk_type) - 278
for i in 0..<4 { - 279●
out.push(type_bytes[i]) - 280
} - 281●
for i in 0..<chunk_data.length() { - 282●
out.push(chunk_data[i]) - 283
} - 284
// CRC - 285
let crc_data : Array[Byte] = [] - 286
for i in 0..<4 { - 287●
crc_data.push(type_bytes[i]) - 288
} - 289●
for i in 0..<chunk_data.length() { - 290●
crc_data.push(chunk_data[i]) - 291
} - 292●
let crc = @util.crc32(Bytes::from_array(crc_data)) - 293●
write_u32_be(out, crc) - 294
} - 295
- 296
///| - 297
/// 将 PngAnimation 编码为 APNG 字节流 - 298
/// 所有帧必须尺寸相同 - 299
pub fn encode_apng_pure( - 300
anim : @types.PngAnimation, - 301
) -> Bytes raise @types.LoadError { - 302●
let num_frames = anim.frames.length() - 303
if num_frames == 0 { - 304●
raise @types.LoadError::EncodeFailed("APNG 至少需要一帧") - 305
} - 306
let first = anim.frames[0] - 307
let width = first.width - 308
let height = first.height - 309
let out : Array[Byte] = [] - 310
// PNG signature - 311●
out.push(b'\x89') - 312●
out.push(b'\x50') - 313●
out.push(b'\x4E') - 314●
out.push(b'\x47') - 315●
out.push(b'\x0D') - 316●
out.push(b'\x0A') - 317●
out.push(b'\x1A') - 318●
out.push(b'\x0A') - 319
// 编码第一帧为 PNG,提取 IHDR 和 IDAT - 320●
let first_png = encode_png_pure(first) - 321●
let ihdr_data = extract_ihdr_from_png(first_png) - 322
// IHDR chunk - 323●
apng_write_chunk(out, "IHDR", ihdr_data) - 324
// acTL chunk: num_frames + num_plays(0=无限) - 325
let actl : Array[Byte] = [] - 326●
write_u32_be(actl, num_frames) - 327●
write_u32_be(actl, 0) - 328●
apng_write_chunk(out, "acTL", Bytes::from_array(actl)) - 329
// 序列号计数器 - 330
let mut seq = 0 - 331
// 第一帧 fcTL - 332
let fctl1 : Array[Byte] = [] - 333●
write_u32_be(fctl1, seq) // sequence_number - 334
seq += 1 - 335●
write_u32_be(fctl1, width) // width - 336●
write_u32_be(fctl1, height) // height - 337●
write_u32_be(fctl1, 0) // x_offset - 338●
write_u32_be(fctl1, 0) // y_offset - 339◐
let delay1 = if anim.delays.length() > 0 { anim.delays[0] } else { 0 } - 340●
write_u16_be(fctl1, delay1) // delay_num (ms) - 341●
write_u16_be(fctl1, 1000) // delay_den - 342●
fctl1.push(b'\x01') // dispose_op = APNG_DISPOSE_OP_BACKGROUND - 343●
fctl1.push(b'\x00') // blend_op = APNG_BLEND_OP_SOURCE - 344●
apng_write_chunk(out, "fcTL", Bytes::from_array(fctl1)) - 345
// 第一帧 IDAT - 346●
let first_idat = extract_idat_from_png(first_png) - 347●
apng_write_chunk(out, "IDAT", first_idat) - 348
// 后续帧 - 349
for i in 1..<num_frames { - 350●
let frame = anim.frames[i] - 351●
let frame_png = encode_png_pure(frame) - 352●
let frame_idat = extract_idat_from_png(frame_png) - 353
// fcTL - 354
let fctl : Array[Byte] = [] - 355●
write_u32_be(fctl, seq) // sequence_number - 356
seq += 1 - 357●
write_u32_be(fctl, frame.width) - 358●
write_u32_be(fctl, frame.height) - 359●
write_u32_be(fctl, 0) // x_offset - 360●
write_u32_be(fctl, 0) // y_offset - 361◐
let delay = if i < anim.delays.length() { anim.delays[i] } else { 0 } - 362●
write_u16_be(fctl, delay) // delay_num (ms) - 363●
write_u16_be(fctl, 1000) // delay_den - 364●
fctl.push(b'\x01') // dispose_op - 365●
fctl.push(b'\x00') // blend_op - 366●
apng_write_chunk(out, "fcTL", Bytes::from_array(fctl)) - 367
// fdAT: seq_num + IDAT data - 368
let fdat : Array[Byte] = [] - 369●
write_u32_be(fdat, seq) // sequence_number - 370
seq += 1 - 371●
for j in 0..<frame_idat.length() { - 372●
fdat.push(frame_idat[j]) - 373
} - 374●
apng_write_chunk(out, "fdAT", Bytes::from_array(fdat)) - 375
} - 376
// IEND - 377●
apng_write_chunk(out, "IEND", Bytes::from_array([])) - 378●
Bytes::from_array(out) - 379
}