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