image.codec\codec\icns_extended.mbt

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