image.codec\codec\cur_extended.mbt
82.1% · 23 / 28 points · 95 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # CUR 扩展 CUR Extended - 3
/// - 4
/// CUR 扩展功能 CUR extended features - 5
- 6
///| - 7
/// CUR 扩展信息(包含图像数量、热点坐标等) - 8
pub struct CurInfoExtended { - 9
icon_type : Int32 - 10
num_images : Int32 - 11
widths : Array[Int32] - 12
heights : Array[Int32] - 13
hotspot_xs : Array[Int32] - 14
hotspot_ys : Array[Int32] - 15
width : Int32 - 16
height : Int32 - 17
num_channels : Int32 - 18
has_alpha : Bool - 19
} - 20
- 21
///| - 22
/// 检测数据是否为 CUR 格式 - 23
/// - 24
/// 仅看前 4 字节会把 24/32-bit TGA 误判为 CUR(TGA 头前 4 字节恰好是 - 25
/// `00 00 02 00`)。这里同时校验图像条目数 ≥ 1 且目录表装得下。 - 26
pub fn is_cur_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 == 2 && count >= 1 && len >= 6 + count * 16 - 35
} - 36
- 37
///| - 38
/// 解析 CUR 扩展头部信息 - 39
pub fn cur_info_extended( - 40
data : Bytes, - 41
) -> CurInfoExtended raise @types.LoadError { - 42●
let len = data.length() - 43
if len < 6 { - 44●
raise @types.LoadError::DecodeFailed("CUR 数据过短") - 45
} - 46●
let icon_type = read_u16_le(data, 2) - 47●
let count = read_u16_le(data, 4) - 48
if icon_type != 2 { - 49○
raise @types.LoadError::UnsupportedFormat("Not a CUR file") - 50
} - 51
if count == 0 { - 52○
raise @types.LoadError::DecodeFailed("CUR 无图像条目") - 53
} - 54
if len < 6 + count * 16 { - 55○
raise @types.LoadError::DecodeFailed("CUR 目录数据不完整") - 56
} - 57
let widths : Array[Int32] = [] - 58
let heights : Array[Int32] = [] - 59
let hotspot_xs : Array[Int32] = [] - 60
let hotspot_ys : Array[Int32] = [] - 61
let mut best_w = 0 - 62
let mut best_h = 0 - 63
let mut best_area = 0 - 64
for i in 0..<count { - 65●
let base = 6 + i * 16 - 66●
let w = data[base].to_int() - 67●
let h = data[base + 1].to_int() - 68●
let hotspot_x = read_u16_le(data, base + 4) - 69●
let hotspot_y = read_u16_le(data, base + 6) - 70◐
let real_w = if w == 0 { 256 } else { w } - 71◐
let real_h = if h == 0 { 256 } else { h } - 72●
widths.push(real_w) - 73●
heights.push(real_h) - 74●
hotspot_xs.push(hotspot_x) - 75●
hotspot_ys.push(hotspot_y) - 76
let area = real_w * real_h - 77
if area > best_area { - 78●
best_area = area - 79
best_w = real_w - 80
best_h = real_h - 81
} - 82
} - 83
CurInfoExtended::{ - 84
icon_type, - 85
num_images: count, - 86
widths, - 87
heights, - 88
hotspot_xs, - 89
hotspot_ys, - 90
width: best_w, - 91
height: best_h, - 92
num_channels: 4, - 93
has_alpha: true, - 94
} - 95
}