image.codec\codec\heif_extended.mbt
82% · 41 / 50 points · 161 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # HEIF 扩展 HEIF Extended - 3
/// - 4
/// HEIF 扩展功能 HEIF extended features - 5
- 6
///| - 7
/// HEIF 扩展信息(包含色彩空间、旋转、HEVC 编码配置等) - 8
pub struct HeifInfoExtended { - 9
width : Int32 - 10
height : Int32 - 11
bit_depth : Int32 - 12
num_channels : Int32 - 13
has_alpha : Bool - 14
rotation : Int32 - 15
mirror : Int32 - 16
color_type : String - 17
color_primaries : Int32 - 18
transfer_characteristics : Int32 - 19
matrix_coefficients : Int32 - 20
full_range : Bool - 21
hevc_profile : Int32 - 22
hevc_level : Int32 - 23
hevc_tier : Int32 - 24
hevc_chroma_format : Int32 - 25
hevc_bit_depth_luma : Int32 - 26
hevc_bit_depth_chroma : Int32 - 27
} - 28
- 29
///| - 30
/// 解析 HEIF 扩展头部信息 - 31
/// - 32
/// 递归遍历 ISOBMFF box 结构,提取色彩空间、旋转、镜像、HEVC 编码配置等 - 33
pub fn heif_info_extended( - 34
data : Bytes, - 35
) -> HeifInfoExtended raise @types.LoadError { - 36●
if !is_heif_format(data) { - 37●
raise @types.LoadError::UnsupportedFormat("Not a HEIF file") - 38
} - 39●
let len = data.length() - 40
let mut width = 0 - 41
let mut height = 0 - 42
let mut bit_depth = 8 - 43
let mut has_alpha = false - 44
let mut rotation = 0 - 45
let mut mirror = 0 - 46
let mut color_type = "unknown" - 47
let mut color_primaries = 0 - 48
let mut transfer_characteristics = 0 - 49
let mut matrix_coefficients = 0 - 50
let mut full_range = false - 51
let mut hevc_profile = 0 - 52
let mut hevc_level = 0 - 53
let mut hevc_tier = 0 - 54
let mut hevc_chroma_format = 0 - 55
let mut hevc_bit_depth_luma = 8 - 56
let mut hevc_bit_depth_chroma = 8 - 57●
heif_scan_boxes(data, 0, len, fn(box_off, _box_size, box_type) { - 58●
if box_type == "ispe" && box_off + 20 <= len { - 59●
width = read_u32_be(data, box_off + 12) - 60●
height = read_u32_be(data, box_off + 16) - 61
} - 62
if box_type == "auxC" { - 63○
has_alpha = true - 64
} - 65
if box_type == "pixi" && box_off + 13 <= len { - 66●
let num_ch = data[box_off + 12].to_int() - 67
if num_ch > 0 && box_off + 13 + num_ch <= len { - 68●
bit_depth = data[box_off + 13].to_int() - 69
} - 70
} - 71
if box_type == "irot" && box_off + 9 <= len { - 72●
let angle_bits = data[box_off + 8].to_int() & 0x03 - 73
rotation = angle_bits * 90 - 74
} - 75
if box_type == "imir" && box_off + 9 <= len { - 76○
mirror = data[box_off + 8].to_int() & 0x01 - 77
} - 78
if box_type == "colr" && box_off + 16 <= len { - 79●
let ctype = read_4cc(data, box_off + 8) - 80
if ctype == "nclx" && box_off + 19 <= len { - 81●
color_type = "nclx" - 82●
color_primaries = read_u16_be(data, box_off + 12) - 83●
transfer_characteristics = read_u16_be(data, box_off + 14) - 84●
matrix_coefficients = read_u16_be(data, box_off + 16) - 85●
full_range = (data[box_off + 18].to_int() & 0x80) != 0 - 86○
} else if ctype == "rICC" { - 87○
color_type = "rICC" - 88○
} else if ctype == "prof" { - 89○
color_type = "prof" - 90
} - 91
} - 92
if box_type == "hvcC" && box_off + 22 <= len { - 93●
let b1 = data[box_off + 9].to_int() - 94
hevc_tier = (b1 >> 6) & 0x01 - 95
hevc_profile = b1 & 0x1F - 96●
hevc_level = data[box_off + 13].to_int() - 97●
let chroma_byte = data[box_off + 21].to_int() - 98
hevc_chroma_format = chroma_byte & 0x03 - 99
if box_off + 23 <= len { - 100●
hevc_bit_depth_luma = (data[box_off + 22].to_int() & 0x07) + 8 - 101
} - 102
if box_off + 24 <= len { - 103●
hevc_bit_depth_chroma = (data[box_off + 23].to_int() & 0x07) + 8 - 104
} - 105
} - 106
}) - 107
if width == 0 || height == 0 { - 108●
raise @types.LoadError::DecodeFailed( - 109
"HEIF: could not determine image dimensions", - 110
) - 111
} - 112◐
let num_channels = if has_alpha { 4 } else { 3 } - 113
HeifInfoExtended::{ - 114
width, - 115
height, - 116
bit_depth, - 117
num_channels, - 118
has_alpha, - 119
rotation, - 120
mirror, - 121
color_type, - 122
color_primaries, - 123
transfer_characteristics, - 124
matrix_coefficients, - 125
full_range, - 126
hevc_profile, - 127
hevc_level, - 128
hevc_tier, - 129
hevc_chroma_format, - 130
hevc_bit_depth_luma, - 131
hevc_bit_depth_chroma, - 132
} - 133
} - 134
- 135
///| - 136
fn heif_scan_boxes( - 137
data : Bytes, - 138
start : Int32, - 139
end : Int32, - 140
callback : (Int32, Int32, String) -> Unit, - 141
) -> Unit { - 142●
let mut offset = start - 143
while offset + 8 <= end { - 144●
let box_size = read_u32_be(data, offset) - 145
if box_size < 8 || box_size > end - offset { - 146○
break - 147
} - 148●
let btype = box_type_str(data, offset) - 149●
callback(offset, box_size, btype) - 150
if btype == "meta" || - 151
btype == "iprp" || - 152
btype == "ipco" || - 153
btype == "moov" || - 154
btype == "trak" || - 155
btype == "mdia" { - 156●
let child_start = if btype == "meta" { offset + 12 } else { offset + 8 } - 157●
heif_scan_boxes(data, child_start, offset + box_size, callback) - 158
} - 159
offset += box_size - 160
} - 161
}