image.codec\codec\tga_extended.mbt

70.5% · 31 / 44 points · 118 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/// # TGA 扩展 TGA Extended
  3. 3///
  4. 4/// TGA 扩展功能 TGA extended features
  5. 5
  6. 6///|
  7. 7/// TGA 扩展信息(包含图像类型、位深、行序等)
  8. 8pub struct TgaInfoExtended {
  9. 9 width : Int32
  10. 10 height : Int32
  11. 11 image_type : Int32
  12. 12 image_type_name : String
  13. 13 bit_depth : Int32
  14. 14 num_channels : Int32
  15. 15 has_alpha : Bool
  16. 16 is_bottom_up : Bool
  17. 17 id_length : Int32
  18. 18 color_map_type : Int32
  19. 19 x_origin : Int32
  20. 20 y_origin : Int32
  21. 21}
  22. 22
  23. 23///|
  24. 24fn tga_image_type_name(t : Int32) -> String {
  25. 25● match t {
  26. 26○ 0 => "no image data"
  27. 27○ 1 => "uncompressed color-mapped"
  28. 28● 2 => "uncompressed true-color"
  29. 29○ 3 => "uncompressed grayscale"
  30. 30○ 9 => "RLE color-mapped"
  31. 31● 10 => "RLE true-color"
  32. 32○ 11 => "RLE grayscale"
  33. 33○ _ => "unknown"
  34. 34 }
  35. 35}
  36. 36
  37. 37///|
  38. 38/// 检测数据是否为 TGA 格式
  39. 39///
  40. 40/// TGA 无明确 magic,通过头部字段有效性判断
  41. 41pub fn is_tga_format(data : Bytes) -> Bool {
  42. 42● let len = data.length()
  43. 43 if len < 18 {
  44. 44● return false
  45. 45 }
  46. 46● let color_map_type = data[1].to_int()
  47. 47● let image_type = data[2].to_int()
  48. 48● let bpp = data[16].to_int()
  49. 49● let width = data[12].to_int() + data[13].to_int() * 256
  50. 50● let height = data[14].to_int() + data[15].to_int() * 256
  51. 51 if width == 0 || height == 0 {
  52. 52● return false
  53. 53 }
  54. 54 if color_map_type != 0 && color_map_type != 1 {
  55. 55● return false
  56. 56 }
  57. 57 // 图像类型:1 调色板 / 2 真彩 / 3 灰度 / 9·10·11 对应的 RLE 版本
  58. 58● let image_type_ok = match image_type {
  59. 59● 1 | 2 | 3 | 9 | 10 | 11 => true
  60. 60○ _ => false
  61. 61 }
  62. 62 if !image_type_ok {
  63. 63○ return false
  64. 64 }
  65. 65 // 调色板索引图(类型 1 / 9)必须声明 color map,否则是畸形头
  66. 66 if color_map_type == 0 && (image_type == 1 || image_type == 9) {
  67. 67○ return false
  68. 68 }
  69. 69 bpp == 8 || bpp == 15 || bpp == 16 || bpp == 24 || bpp == 32
  70. 70}
  71. 71
  72. 72///|
  73. 73/// 解析 TGA 扩展头部信息
  74. 74pub fn tga_info_extended(
  75. 75 data : Bytes,
  76. 76) -> TgaInfoExtended raise @types.LoadError {
  77. 77● let len = data.length()
  78. 78 if len < 18 {
  79. 79● raise @types.LoadError::DecodeFailed("TGA 数据过短")
  80. 80 }
  81. 81● let id_length = data[0].to_int()
  82. 82● let color_map_type = data[1].to_int()
  83. 83● let image_type = data[2].to_int()
  84. 84● let x_origin = read_u16_le(data, 8)
  85. 85● let y_origin = read_u16_le(data, 10)
  86. 86● let width = read_u16_le(data, 12)
  87. 87● let height = read_u16_le(data, 14)
  88. 88● let bpp = data[16].to_int()
  89. 89● let descriptor = data[17].to_int()
  90. 90 if color_map_type != 0 {
  91. 91○ raise @types.LoadError::DecodeFailed("TGA 颜色映射类型不支持")
  92. 92 }
  93. 93 if image_type != 2 && image_type != 10 && image_type != 3 && image_type != 11 {
  94. 94○ raise @types.LoadError::DecodeFailed("TGA image type 不支持")
  95. 95 }
  96. 96 if bpp != 8 && bpp != 24 && bpp != 32 {
  97. 97○ raise @types.LoadError::DecodeFailed("TGA 位深不支持")
  98. 98 }
  99. 99 if width == 0 || height == 0 {
  100. 100○ raise @types.LoadError::DecodeFailed("TGA 尺寸无效")
  101. 101 }
  102. 102 let is_bottom_up = (descriptor & 0x20) == 0
  103. 103 let num_channels = bpp / 8
  104. 104 TgaInfoExtended::{
  105. 105 width,
  106. 106 height,
  107. 107 image_type,
  108. 108● image_type_name: tga_image_type_name(image_type),
  109. 109 bit_depth: bpp,
  110. 110 num_channels,
  111. 111 has_alpha: bpp == 32,
  112. 112 is_bottom_up,
  113. 113 id_length,
  114. 114 color_map_type,
  115. 115 x_origin,
  116. 116 y_origin,
  117. 117 }
  118. 118}