image.codec\codec\png_extended.mbt
85.1% · 40 / 47 points · 112 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # PNG 扩展 PNG Extended - 3
/// - 4
/// PNG 扩展功能 PNG extended features - 5
- 6
///| - 7
/// PNG 扩展信息(包含位深、颜色类型、隔行扫描等) - 8
pub struct PngInfoExtended { - 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
is_palette : Bool - 17
compression_method : Int32 - 18
filter_method : Int32 - 19
interlace_method : Int32 - 20
} - 21
- 22
///| - 23
/// 检测数据是否为 PNG 格式 - 24
pub fn is_png_format(data : Bytes) -> Bool { - 25●
let len = data.length() - 26
if len < 8 { - 27●
return false - 28
} - 29●
data[0].to_int() == 0x89 && - 30●
data[1].to_int() == 0x50 && - 31●
data[2].to_int() == 0x4E && - 32●
data[3].to_int() == 0x47 && - 33●
data[4].to_int() == 0x0D && - 34●
data[5].to_int() == 0x0A && - 35●
data[6].to_int() == 0x1A && - 36●
data[7].to_int() == 0x0A - 37
} - 38
- 39
///| - 40
fn png_color_type_name(ct : Int32) -> String { - 41●
match ct { - 42●
0 => "Grayscale" - 43●
2 => "RGB" - 44●
3 => "Palette" - 45○
4 => "Grayscale+Alpha" - 46●
6 => "RGBA" - 47○
_ => "unknown" - 48
} - 49
} - 50
- 51
///| - 52
fn png_channels_count(ct : Int32) -> Int32 { - 53●
match ct { - 54●
0 => 1 - 55●
2 => 3 - 56●
3 => 1 - 57○
4 => 2 - 58●
6 => 4 - 59○
_ => 0 - 60
} - 61
} - 62
- 63
///| - 64
/// 解析 PNG 扩展头部信息 - 65
/// - 66
/// 解析 IHDR chunk,提取尺寸、位深、颜色类型、压缩/滤波/隔行方式 - 67
pub fn png_info_extended( - 68
data : Bytes, - 69
) -> PngInfoExtended raise @types.LoadError { - 70●
if !is_png_format(data) { - 71●
raise @types.LoadError::UnsupportedFormat("Not a PNG file") - 72
} - 73●
let len = data.length() - 74
// IHDR chunk starts at offset 8 - 75
// length(4) + "IHDR"(4) + width(4) + height(4) + bit_depth(1) + color_type(1) + compression(1) + filter(1) + interlace(1) - 76
if len < 29 { - 77○
raise @types.LoadError::DecodeFailed("PNG 数据过短,无法读取 IHDR") - 78
} - 79
// 验证 IHDR chunk type - 80●
if data[12].to_int() != 0x49 || - 81●
data[13].to_int() != 0x48 || - 82●
data[14].to_int() != 0x44 || - 83●
data[15].to_int() != 0x52 { - 84○
raise @types.LoadError::DecodeFailed("PNG IHDR chunk 不存在") - 85
} - 86●
let width = read_u32_be(data, 16) - 87●
let height = read_u32_be(data, 20) - 88●
let bit_depth = data[24].to_int() - 89●
let color_type = data[25].to_int() - 90●
let compression_method = data[26].to_int() - 91●
let filter_method = data[27].to_int() - 92●
let interlace_method = data[28].to_int() - 93
if width == 0 || height == 0 { - 94○
raise @types.LoadError::DecodeFailed("PNG 尺寸无效") - 95
} - 96●
let num_channels = png_channels_count(color_type) - 97
let has_alpha = color_type == 4 || color_type == 6 - 98
let is_palette = color_type == 3 - 99
PngInfoExtended::{ - 100
width, - 101
height, - 102
bit_depth, - 103
color_type, - 104●
color_type_name: png_color_type_name(color_type), - 105
num_channels, - 106
has_alpha, - 107
is_palette, - 108
compression_method, - 109
filter_method, - 110
interlace_method, - 111
} - 112
}