image.codec\codec\icns_codec.mbt
81.9% · 77 / 94 points · 211 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 Codec - 3
/// - 4
/// macOS ICNS 图标格式编解码 macOS ICNS icon format encoding/decoding - 5
// 纯 MoonBit ICNS 编解码(全目标可用:native/wasm/js) - 6
// ICNS 规范: https://en.wikipedia.org/wiki/Apple_Icon_Image_format - 7
// 文件结构: "icns" + file_size(BE32) + chunks - 8
// 每个 chunk: type_code(4B) + length(BE32, 含8B头) + data - 9
// PNG 类型: ic07(128), ic08(256), ic09(512), ic10(1024), - 10
// ic11(16), ic12(32), ic13(64), ic14(256) - 11
// ARGB 类型: ic04(16), ic05(32), icp6(64), icp7(128), icp8(256), icp9(512) - 12
- 13
///| - 14
/// ICNS chunk 信息 - 15
priv struct IcnsChunk { - 16
data_offset : Int32 - 17
data_size : Int32 - 18
icon_size : Int32 // 像素边长,0 表示非图标数据 - 19
is_png : Bool - 20
} - 21
- 22
///| - 23
/// 从 4 字节构造 type code 字符串 - 24
fn icns_type_to_string(data : Bytes, offset : Int32) -> String { - 25●
let buf : Array[Byte] = [ - 26
data[offset], - 27
data[offset + 1], - 28
data[offset + 2], - 29
data[offset + 3], - 30
] - 31●
@encoding/utf8.decode(Bytes::from_array(buf)) catch { - 32○
_ => "????" - 33
} - 34
} - 35
- 36
///| - 37
/// 根据 type code 获取图标像素边长和是否 PNG - 38
fn icns_icon_info(type_code : String) -> (Int32, Bool) { - 39●
match type_code { - 40●
"ic07" => (128, true) // 128x128 PNG - 41●
"ic08" => (256, true) // 256x256 PNG - 42○
"ic09" => (512, true) // 512x512 PNG - 43○
"ic10" => (1024, true) // 1024x1024 PNG - 44●
"ic11" => (16, true) // 16x16 PNG - 45●
"ic12" => (32, true) // 32x32 PNG - 46○
"ic13" => (64, true) // 64x64 PNG - 47○
"ic14" => (256, true) // 256x256 PNG - 48●
"ic04" => (16, false) // 16x16 ARGB - 49○
"ic05" => (32, false) // 32x32 ARGB - 50○
"icp4" => (16, false) // 16x16 ARGB - 51○
"icp5" => (32, false) // 32x32 ARGB - 52○
"icp6" => (64, false) // 64x64 ARGB - 53○
"icp7" => (128, false) // 128x128 ARGB - 54○
"icp8" => (256, false) // 256x256 ARGB - 55○
"icp9" => (512, false) // 512x512 ARGB - 56○
_ => (0, false) - 57
} - 58
} - 59
- 60
///| - 61
/// 根据像素边长选择最佳 type code(PNG 类型) - 62
fn icns_size_to_type(size : Int32) -> String { - 63●
match size { - 64●
16 => "ic11" - 65●
32 => "ic12" - 66●
64 => "ic13" - 67●
128 => "ic07" - 68●
256 => "ic08" - 69○
512 => "ic09" - 70○
1024 => "ic10" - 71●
_ => "ic08" // 默认用 256x256 - 72
} - 73
} - 74
- 75
///| - 76
/// 解析 ICNS 文件,返回所有 chunk - 77
fn parse_icns(data : Bytes) -> Array[IcnsChunk] raise @types.LoadError { - 78●
let len = data.length() - 79
if len < 8 { - 80●
raise @types.LoadError::DecodeFailed("ICNS 数据过短") - 81
} - 82
// 检查 magic "icns" - 83●
if data[0].to_int() != 0x69 || - 84●
data[1].to_int() != 0x63 || - 85●
data[2].to_int() != 0x6E || - 86●
data[3].to_int() != 0x73 { - 87●
raise @types.LoadError::DecodeFailed("ICNS magic 不匹配") - 88
} - 89●
let file_size = read_u32_be(data, 4) - 90
let chunks : Array[IcnsChunk] = [] - 91
let mut pos = 8 - 92●
let end = if file_size < len { file_size } else { len } - 93
while pos + 8 <= end { - 94●
let type_code = icns_type_to_string(data, pos) - 95●
let chunk_len = read_u32_be(data, pos + 4) - 96
if chunk_len < 8 || chunk_len > end - pos { - 97●
break - 98
} - 99
let data_offset = pos + 8 - 100
let data_size = chunk_len - 8 - 101●
let (icon_size, is_png) = icns_icon_info(type_code) - 102●
chunks.push(IcnsChunk::{ data_offset, data_size, icon_size, is_png, }) - 103
pos += chunk_len - 104
} - 105●
if chunks.length() == 0 { - 106●
raise @types.LoadError::DecodeFailed("ICNS 无图标 chunk") - 107
} - 108
chunks - 109
} - 110
- 111
///| - 112
/// 解码 ARGB 原始数据为 Image(BGRA → RGBA) - 113
fn decode_icns_argb( - 114
data : Bytes, - 115
offset : Int32, - 116
size : Int32, - 117
) -> @types.Image raise @types.LoadError { - 118●
let len = data.length() - 119
let pixel_count = size * size - 120
if offset + pixel_count * 4 > len { - 121○
raise @types.LoadError::DecodeFailed("ICNS ARGB 数据不完整") - 122
} - 123●
let pixels : Array[Byte] = Array::make(pixel_count * 4, b'\x00') - 124
for i in 0..<pixel_count { - 125●
let src = offset + i * 4 - 126●
let a = data[src].to_int() - 127●
let r = data[src + 1].to_int() - 128●
let g = data[src + 2].to_int() - 129●
let b = data[src + 3].to_int() - 130
let dst = i * 4 - 131●
pixels[dst] = r.to_byte() - 132●
pixels[dst + 1] = g.to_byte() - 133●
pixels[dst + 2] = b.to_byte() - 134●
pixels[dst + 3] = a.to_byte() - 135
} - 136
@types.Image::{ - 137
width: size, - 138
height: size, - 139
channels: 4, - 140●
data: Bytes::from_array(pixels), - 141
} - 142
} - 143
- 144
///| - 145
/// 解码 ICNS 字节流为 Image(选择面积最大的图标),支持 PNG 子图像(ic07-ic14)和 ARGB 原始数据(ic04/ic05/icp4-9) Decode ICNS byte stream to Image (selects largest icon by area), Supports PNG sub-images (ic07-ic14) and ARGB raw data (ic04/ic05/icp4-9) - 146
- 147
///| - 148
/// 参数 Parameters: - 149
/// - `data` - ICNS 字节流 ICNS byte stream - 150
/// - 151
/// 返回 Return: - 152
/// - 解码后的 8-bit Image Decoded 8-bit Image - 153
/// - 154
/// 错误 Errors: - 155
/// - `LoadError::DecodeFailed` - 数据过短、icns 签名无效、无可用图标 Data too short, invalid icns signature, no usable icon - 156
pub fn decode_icns_pure(data : Bytes) -> @types.Image raise @types.LoadError { - 157●
let chunks = parse_icns(data) - 158
// 选择面积最大的图标 chunk - 159
let mut best_idx = 0 - 160
let mut best_area = 0 - 161●
for i in 0..<chunks.length() { - 162●
let area = chunks[i].icon_size * chunks[i].icon_size - 163
if area > best_area { - 164●
best_area = area - 165
best_idx = i - 166
} - 167
} - 168
let chunk = chunks[best_idx] - 169
if chunk.icon_size == 0 { - 170○
raise @types.LoadError::DecodeFailed("ICNS 无有效图标 chunk") - 171
} - 172
if chunk.is_png { - 173
// PNG 子图像 - 174●
let sub : Array[Byte] = [] - 175
for i in 0..<chunk.data_size { - 176●
sub.push(data[chunk.data_offset + i]) - 177
} - 178●
decode_png_pure(Bytes::from_array(sub)) - 179
} else { - 180
// ARGB 原始数据 - 181●
decode_icns_argb(data, chunk.data_offset, chunk.icon_size) - 182
} - 183
} - 184
- 185
///| - 186
/// 将单个 Image 编码为 ICNS 字节流(内嵌 PNG) - 187
pub fn encode_icns_pure(img : @types.Image) -> Bytes { - 188●
let png_data = encode_png_pure(img) - 189●
let type_code = icns_size_to_type(img.width) - 190●
let type_bytes = @encoding/utf8.encode(type_code) - 191●
let chunk_data_size = png_data.length() - 192
let chunk_total_size = 8 + chunk_data_size - 193
let file_size = 8 + chunk_total_size - 194
let out : Array[Byte] = [] - 195
// ICNS 文件头 - 196●
out.push(b'\x69') // 'i' - 197●
out.push(b'\x63') // 'c' - 198●
out.push(b'\x6E') // 'n' - 199●
out.push(b'\x73') // 's' - 200●
write_u32_be(out, file_size) - 201
// chunk 头 - 202
for i in 0..<4 { - 203●
out.push(type_bytes[i]) - 204
} - 205●
write_u32_be(out, chunk_total_size) - 206
// chunk 数据(PNG) - 207
for i in 0..<chunk_data_size { - 208●
out.push(png_data[i]) - 209
} - 210●
Bytes::from_array(out) - 211
}