image.codec\codec\ico_extended.mbt
86.2% · 25 / 29 points · 94 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # ICO 扩展 ICO Extended - 3
/// - 4
/// ICO 扩展功能 ICO extended features - 5
- 6
///| - 7
/// ICO 扩展信息(包含图像数量、各子图像尺寸/位深等) - 8
pub struct IcoInfoExtended { - 9
icon_type : Int32 - 10
icon_type_name : String - 11
num_images : Int32 - 12
widths : Array[Int32] - 13
heights : Array[Int32] - 14
bit_depths : Array[Int32] - 15
width : Int32 - 16
height : Int32 - 17
num_channels : Int32 - 18
has_alpha : Bool - 19
} - 20
- 21
///| - 22
/// 检测数据是否为 ICO 格式 - 23
/// - 24
/// 与 CUR 同口径:除 reserved/type 外还校验条目数 ≥ 1 且目录表装得下, - 25
/// 避免其他格式(如 TGA 头)撞上前 4 字节。 - 26
pub fn is_ico_format(data : Bytes) -> Bool { - 27●
let len = data.length() - 28
if len < 6 { - 29●
return false - 30
} - 31●
let reserved = read_u16_le(data, 0) - 32●
let icon_type = read_u16_le(data, 2) - 33●
let count = read_u16_le(data, 4) - 34
reserved == 0 && icon_type == 1 && count >= 1 && len >= 6 + count * 16 - 35
} - 36
- 37
///| - 38
/// 解析 ICO 扩展头部信息 - 39
pub fn ico_info_extended( - 40
data : Bytes, - 41
) -> IcoInfoExtended raise @types.LoadError { - 42●
let len = data.length() - 43
if len < 6 { - 44●
raise @types.LoadError::DecodeFailed("ICO 数据过短") - 45
} - 46●
let icon_type = read_u16_le(data, 2) - 47●
let count = read_u16_le(data, 4) - 48
if icon_type != 1 { - 49○
raise @types.LoadError::UnsupportedFormat("Not an ICO file") - 50
} - 51
if count == 0 { - 52○
raise @types.LoadError::DecodeFailed("ICO 无图像条目") - 53
} - 54
if len < 6 + count * 16 { - 55○
raise @types.LoadError::DecodeFailed("ICO 目录数据不完整") - 56
} - 57
let widths : Array[Int32] = [] - 58
let heights : Array[Int32] = [] - 59
let bit_depths : Array[Int32] = [] - 60
let mut best_w = 0 - 61
let mut best_h = 0 - 62
let mut best_area = 0 - 63
for i in 0..<count { - 64●
let base = 6 + i * 16 - 65●
let w = data[base].to_int() - 66●
let h = data[base + 1].to_int() - 67●
let planes = read_u16_le(data, base + 4) - 68●
let bit_count = read_u16_le(data, base + 6) - 69●
let real_w = if w == 0 { 256 } else { w } - 70●
let real_h = if h == 0 { 256 } else { h } - 71◐
let real_bd = if planes == 0 { bit_count } else { planes * bit_count } - 72●
widths.push(real_w) - 73●
heights.push(real_h) - 74●
bit_depths.push(real_bd) - 75
let area = real_w * real_h - 76
if area > best_area { - 77●
best_area = area - 78
best_w = real_w - 79
best_h = real_h - 80
} - 81
} - 82
IcoInfoExtended::{ - 83
icon_type, - 84
icon_type_name: "ICO", - 85
num_images: count, - 86
widths, - 87
heights, - 88
bit_depths, - 89
width: best_w, - 90
height: best_h, - 91
num_channels: 4, - 92
has_alpha: true, - 93
} - 94
}