image.codec\codec\apng_extended.mbt
78.8% · 41 / 52 points · 137 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 Extended - 3
/// - 4
/// APNG 扩展功能 APNG extended features - 5
- 6
///| - 7
/// APNG 扩展信息(包含帧数、播放次数、动画标志等) - 8
pub struct ApngInfoExtended { - 9
width : Int32 - 10
height : Int32 - 11
bit_depth : Int32 - 12
color_type : Int32 - 13
color_type_name : String - 14
num_channels : Int32 - 15
has_alpha : Bool - 16
num_frames : Int32 - 17
num_plays : Int32 - 18
is_animated : Bool - 19
} - 20
- 21
///| - 22
fn apng_color_type_name(ct : Int32) -> String { - 23●
match ct { - 24○
0 => "Grayscale" - 25●
2 => "RGB" - 26○
3 => "Palette" - 27○
4 => "Grayscale+Alpha" - 28●
6 => "RGBA" - 29○
_ => "unknown" - 30
} - 31
} - 32
- 33
///| - 34
fn apng_channels_count(ct : Int32) -> Int32 { - 35●
match ct { - 36○
0 => 1 - 37●
2 => 3 - 38○
3 => 1 - 39○
4 => 2 - 40●
6 => 4 - 41○
_ => 0 - 42
} - 43
} - 44
- 45
///| - 46
/// 检测数据是否为 APNG 格式(PNG signature + 含 acTL chunk) - 47
pub fn is_apng_format(data : Bytes) -> Bool { - 48●
let len = data.length() - 49
if len < 8 { - 50●
return false - 51
} - 52●
if !is_png_signature(data, 0) { - 53●
return false - 54
} - 55
let mut pos = 8 - 56
while pos + 8 <= len { - 57●
let chunk_len = read_u32_be(data, pos) - 58●
let b0 = data[pos + 4].to_int() - 59●
let b1 = data[pos + 5].to_int() - 60●
let b2 = data[pos + 6].to_int() - 61●
let b3 = data[pos + 7].to_int() - 62
// 检查 "acTL" chunk - 63
if b0 == 0x61 && b1 == 0x63 && b2 == 0x54 && b3 == 0x4C { - 64●
return true - 65
} - 66
if chunk_len < 0 || chunk_len > len - pos - 8 { - 67●
break - 68
} - 69
pos += 8 + chunk_len + 4 - 70
} - 71
false - 72
} - 73
- 74
///| - 75
/// 解析 APNG 扩展头部信息 - 76
/// - 77
/// 遍历 PNG chunks,提取 IHDR(尺寸/位深/颜色类型) + acTL(帧数/播放次数) - 78
pub fn apng_info_extended( - 79
data : Bytes, - 80
) -> ApngInfoExtended raise @types.LoadError { - 81●
let len = data.length() - 82
if len < 8 { - 83●
raise @types.LoadError::DecodeFailed("APNG 数据过短") - 84
} - 85●
if !is_png_signature(data, 0) { - 86○
raise @types.LoadError::UnsupportedFormat("Not a PNG/APNG file") - 87
} - 88
let mut pos = 8 - 89
let mut width = 0 - 90
let mut height = 0 - 91
let mut bit_depth = 0 - 92
let mut color_type = 0 - 93
let mut num_frames = 0 - 94
let mut num_plays = 0 - 95
let mut found_ihdr = false - 96
while pos + 8 <= len { - 97●
let chunk_len = read_u32_be(data, pos) - 98●
let b0 = data[pos + 4].to_int() - 99●
let b1 = data[pos + 5].to_int() - 100●
let b2 = data[pos + 6].to_int() - 101●
let b3 = data[pos + 7].to_int() - 102
if pos + 8 + chunk_len + 4 > len { - 103○
break - 104
} - 105
// IHDR - 106
if b0 == 0x49 && b1 == 0x48 && b2 == 0x44 && b3 == 0x52 { - 107●
width = read_u32_be(data, pos + 8) - 108●
height = read_u32_be(data, pos + 12) - 109●
bit_depth = data[pos + 16].to_int() - 110●
color_type = data[pos + 17].to_int() - 111
found_ihdr = true - 112
} - 113
// acTL - 114
if b0 == 0x61 && b1 == 0x63 && b2 == 0x54 && b3 == 0x4C { - 115●
num_frames = read_u32_be(data, pos + 8) - 116●
num_plays = read_u32_be(data, pos + 12) - 117
} - 118
pos += 8 + chunk_len + 4 - 119
} - 120
if !found_ihdr || width == 0 || height == 0 { - 121○
raise @types.LoadError::DecodeFailed("APNG IHDR 不存在或尺寸无效") - 122
} - 123●
let num_channels = apng_channels_count(color_type) - 124
let has_alpha = color_type == 4 || color_type == 6 - 125
ApngInfoExtended::{ - 126
width, - 127
height, - 128
bit_depth, - 129
color_type, - 130●
color_type_name: apng_color_type_name(color_type), - 131
num_channels, - 132
has_alpha, - 133
num_frames, - 134
num_plays, - 135
is_animated: num_frames > 1, - 136
} - 137
}