image.codec\codec\ico_extended.mbt

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