image.codec\codec\icns_extended.mbt
91.7% · 22 / 24 points · 79 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # ICNS 扩展 ICNS Extended - 3
/// - 4
/// ICNS 扩展功能 ICNS extended features - 5
- 6
///| - 7
/// ICNS 扩展信息(包含图标数量、各图标类型/尺寸等) - 8
pub struct IcnsInfoExtended { - 9
file_size : Int32 - 10
num_icons : Int32 - 11
icon_types : Array[String] - 12
icon_sizes : Array[Int32] - 13
is_png_list : Array[Bool] - 14
width : Int32 - 15
height : Int32 - 16
num_channels : Int32 - 17
has_alpha : Bool - 18
} - 19
- 20
///| - 21
/// 检测数据是否为 ICNS 格式 - 22
pub fn is_icns_format(data : Bytes) -> Bool { - 23●
let len = data.length() - 24
if len < 8 { - 25●
return false - 26
} - 27●
data[0].to_int() == 0x69 && - 28●
data[1].to_int() == 0x63 && - 29●
data[2].to_int() == 0x6E && - 30●
data[3].to_int() == 0x73 - 31
} - 32
- 33
///| - 34
/// 解析 ICNS 扩展头部信息 - 35
pub fn icns_info_extended( - 36
data : Bytes, - 37
) -> IcnsInfoExtended raise @types.LoadError { - 38●
if !is_icns_format(data) { - 39●
raise @types.LoadError::UnsupportedFormat("Not an ICNS file") - 40
} - 41●
let len = data.length() - 42●
let file_size = read_u32_be(data, 4) - 43
let icon_types : Array[String] = [] - 44
let icon_sizes : Array[Int32] = [] - 45
let is_png_list : Array[Bool] = [] - 46
let mut pos = 8 - 47
let mut max_size = 0 - 48
while pos + 8 <= len { - 49●
let type_code = icns_type_to_string(data, pos) - 50●
let chunk_len = read_u32_be(data, pos + 4) - 51
if chunk_len < 8 || chunk_len > len - pos { - 52○
break - 53
} - 54●
let (icon_size, is_png) = icns_icon_info(type_code) - 55
if icon_size > 0 { - 56●
icon_types.push(type_code) - 57●
icon_sizes.push(icon_size) - 58●
is_png_list.push(is_png) - 59
if icon_size > max_size { - 60●
max_size = icon_size - 61
} - 62
} - 63
pos += chunk_len - 64
} - 65
if max_size == 0 { - 66○
raise @types.LoadError::DecodeFailed("ICNS 无有效图标") - 67
} - 68
IcnsInfoExtended::{ - 69
file_size, - 70●
num_icons: icon_types.length(), - 71
icon_types, - 72
icon_sizes, - 73
is_png_list, - 74
width: max_size, - 75
height: max_size, - 76
num_channels: 4, - 77
has_alpha: true, - 78
} - 79
}