image.codec\codec\cur_extended.mbt

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