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