image.codec\codec\tga_decode.mbt

94.1% · 64 / 68 points · 189 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 Decode
  3. 3///
  4. 4/// TGA 图像格式解码 TGA image format decoding
  5. 5// 纯 MoonBit TGA 解码器
  6. 6// 支持 image type 2(未压缩 RGB)和 type 10(RLE RGB)
  7. 7// 24-bit(BGR→RGB)和 32-bit(BGRA→RGBA)
  8. 8// 规范: https://en.wikipedia.org/wiki/Truevision_TGA
  9. 9
  10. 10///|
  11. 11/// 解码 TGA 字节流为 Image(24-bit -> channels=3, 32-bit -> channels=4),支持 image type 2(未压缩 RGB)和 type 10(RLE RGB) Decode TGA byte stream to Image (24-bit -> channels=3, 32-bit -> channels=4), Supports image type 2 (uncompressed RGB) and type 10 (RLE RGB)
  12. 12
  13. 13///|
  14. 14/// 参数 Parameters:
  15. 15/// - `data` - TGA 字节流 TGA byte stream
  16. 16///
  17. 17/// 返回 Return:
  18. 18/// - 解码后的 8-bit Image Decoded 8-bit Image
  19. 19///
  20. 20/// 错误 Errors:
  21. 21/// - `LoadError::DecodeFailed` - 数据过短、不支持的类型/位深、尺寸无效、像素越界 Data too short, unsupported type/bpp, invalid dimensions, pixel out of bounds
  22. 22///
  23. 23/// ```mbt check
  24. 24/// test "doc_decode_tga_pure" {
  25. 25/// try {
  26. 26/// let data = Bytes::from_array([b'\xff', b'\x00', b'\x00'])
  27. 27/// let img = @types.Image::{ width: 1, height: 1, channels: 3, data, }
  28. 28/// let tga_bytes = @codec.encode_tga_pure(img)
  29. 29/// let decoded = @codec.decode_tga_pure(tga_bytes)
  30. 30/// assert_eq(decoded.width, 1)
  31. 31/// assert_eq(decoded.height, 1)
  32. 32/// assert_eq(decoded.channels, 3)
  33. 33/// } catch {
  34. 34/// _ => assert_true(false)
  35. 35/// }
  36. 36/// }
  37. 37/// ```
  38. 38pub fn decode_tga_pure(data : Bytes) -> @types.Image raise @types.LoadError {
  39. 39● let len = data.length()
  40. 40 // TGA header 18 字节
  41. 41 if len < 18 {
  42. 42● raise @types.LoadError::DecodeFailed("TGA 数据过短")
  43. 43 }
  44. 44● let id_length = data[0].to_int()
  45. 45● let color_map_type = data[1].to_int()
  46. 46● let image_type = data[2].to_int()
  47. 47● let width = read_u16_le(data, 12)
  48. 48● let height = read_u16_le(data, 14)
  49. 49● let bpp = data[16].to_int()
  50. 50● let descriptor = data[17].to_int()
  51. 51 // 仅支持无颜色映射
  52. 52 if color_map_type != 0 {
  53. 53● raise @types.LoadError::DecodeFailed(
  54. 54 "不支持的 TGA 颜色映射类型,仅支持无颜色映射",
  55. 55 )
  56. 56 }
  57. 57 // 仅支持 image type 2(未压缩 RGB)和 type 10(RLE RGB)
  58. 58 if image_type != 2 && image_type != 10 {
  59. 59● raise @types.LoadError::DecodeFailed(
  60. 60 "不支持的 TGA image type,仅支持 type 2 和 type 10",
  61. 61 )
  62. 62 }
  63. 63 // 仅支持 24-bit 和 32-bit
  64. 64 if bpp != 24 && bpp != 32 {
  65. 65● raise @types.LoadError::DecodeFailed(
  66. 66 "不支持的 TGA 位深,仅支持 24-bit 和 32-bit",
  67. 67 )
  68. 68 }
  69. 69 // 尺寸校验
  70. 70 if width == 0 || height == 0 {
  71. 71○ raise @types.LoadError::DecodeFailed("TGA 尺寸无效")
  72. 72 }
  73. 73 let bytes_per_pixel = bpp / 8
  74. 74 let out_channels = bytes_per_pixel
  75. 75● @types.check_dims(width, height, out_channels)
  76. 76 // 行序:描述符 bit5(0x20)= top-down,bit4(0x10)= right-to-left(水平镜像)
  77. 77 // 注意:旧实现误把 bit4 当成 top-down,与规范相反——真实 top-down 文件会被上下翻转。
  78. 78 let top_down = (descriptor & 0x20) != 0
  79. 79 let right_to_left = (descriptor & 0x10) != 0
  80. 80 // 像素数据起始位置
  81. 81 let pixel_data_start = 18 + id_length
  82. 82 // 解码像素(BGR(A) → RGB(A))到 raw_pixels(按文件行序)
  83. 83 let pixel_count = width * height
  84. 84● let raw_pixels : Array[Byte] = Array::make(
  85. 85 pixel_count * out_channels,
  86. 86 b'\x00',
  87. 87 )
  88. 88 let pos = Ref(pixel_data_start)
  89. 89 let pixel_idx = Ref(0)
  90. 90 if image_type == 2 {
  91. 91 // 未压缩
  92. 92● while pixel_idx.val < pixel_count {
  93. 93● let p = pos.val
  94. 94 if p + bytes_per_pixel > len {
  95. 95○ raise @types.LoadError::DecodeFailed("TGA 像素数据越界")
  96. 96 }
  97. 97 // BGR(A) → RGB(A)
  98. 98● let b = data[p].to_int()
  99. 99● let g = data[p + 1].to_int()
  100. 100● let r = data[p + 2].to_int()
  101. 101 let out_idx = pixel_idx.val * out_channels
  102. 102● raw_pixels[out_idx] = r.to_byte()
  103. 103● raw_pixels[out_idx + 1] = g.to_byte()
  104. 104● raw_pixels[out_idx + 2] = b.to_byte()
  105. 105 if out_channels == 4 {
  106. 106● let a = data[p + 3].to_int()
  107. 107● raw_pixels[out_idx + 3] = a.to_byte()
  108. 108 }
  109. 109 pos.val += bytes_per_pixel
  110. 110 pixel_idx.val += 1
  111. 111 }
  112. 112 } else {
  113. 113 // RLE (image_type == 10)
  114. 114● while pixel_idx.val < pixel_count && pos.val < len {
  115. 115● let header = data[pos.val].to_int()
  116. 116 pos.val += 1
  117. 117 if (header & 0x80) != 0 {
  118. 118 // RLE packet: run = (header & 0x7F) + 1, 读 1 像素重复 run 次
  119. 119● let run = (header & 0x7F) + 1
  120. 120 let p = pos.val
  121. 121 if p + bytes_per_pixel > len {
  122. 122○ raise @types.LoadError::DecodeFailed("TGA RLE 像素数据越界")
  123. 123 }
  124. 124● let b = data[p].to_int()
  125. 125● let g = data[p + 1].to_int()
  126. 126● let r = data[p + 2].to_int()
  127. 127● let a = if out_channels == 4 { data[p + 3].to_int() } else { 0 }
  128. 128 pos.val += bytes_per_pixel
  129. 129 for i in 0..<run {
  130. 130● if pixel_idx.val + i < pixel_count {
  131. 131● let out_idx = (pixel_idx.val + i) * out_channels
  132. 132● raw_pixels[out_idx] = r.to_byte()
  133. 133● raw_pixels[out_idx + 1] = g.to_byte()
  134. 134● raw_pixels[out_idx + 2] = b.to_byte()
  135. 135 if out_channels == 4 {
  136. 136● raw_pixels[out_idx + 3] = a.to_byte()
  137. 137 }
  138. 138 }
  139. 139 }
  140. 140 pixel_idx.val += run
  141. 141 } else {
  142. 142 // raw packet: count = (header & 0x7F) + 1, 读 count 像素
  143. 143● let count = (header & 0x7F) + 1
  144. 144 for i in 0..<count {
  145. 145● let p = pos.val + i * bytes_per_pixel
  146. 146 if p + bytes_per_pixel > len {
  147. 147○ raise @types.LoadError::DecodeFailed(
  148. 148 "TGA RLE raw 像素数据越界",
  149. 149 )
  150. 150 }
  151. 151 if pixel_idx.val + i < pixel_count {
  152. 152● let b = data[p].to_int()
  153. 153● let g = data[p + 1].to_int()
  154. 154● let r = data[p + 2].to_int()
  155. 155 let out_idx = (pixel_idx.val + i) * out_channels
  156. 156● raw_pixels[out_idx] = r.to_byte()
  157. 157● raw_pixels[out_idx + 1] = g.to_byte()
  158. 158● raw_pixels[out_idx + 2] = b.to_byte()
  159. 159 if out_channels == 4 {
  160. 160● let a = data[p + 3].to_int()
  161. 161● raw_pixels[out_idx + 3] = a.to_byte()
  162. 162 }
  163. 163 }
  164. 164 }
  165. 165 pos.val += count * bytes_per_pixel
  166. 166 pixel_idx.val += count
  167. 167 }
  168. 168 }
  169. 169 }
  170. 170 // 行序处理:按输出行序写入 pixels(bottom-up 文件整体上下翻转;bit4 置位时每行再水平镜像)
  171. 171● let pixels : Array[Byte] = Array::make(pixel_count * out_channels, b'\x00')
  172. 172 for file_row in 0..<height {
  173. 173● let out_row = if top_down { file_row } else { height - 1 - file_row }
  174. 174 for x in 0..<width {
  175. 175● let src_x = if right_to_left { width - 1 - x } else { x }
  176. 176 let src_idx = (file_row * width + src_x) * out_channels
  177. 177 let dst_idx = (out_row * width + x) * out_channels
  178. 178 for c in 0..<out_channels {
  179. 179● pixels[dst_idx + c] = raw_pixels[src_idx + c]
  180. 180 }
  181. 181 }
  182. 182 }
  183. 183 @types.Image::{
  184. 184 width,
  185. 185 height,
  186. 186 channels: out_channels,
  187. 187● data: Bytes::from_array(pixels),
  188. 188 }
  189. 189}