image.codec\codec\icns_codec.mbt

81.9% · 77 / 94 points · 211 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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