image.codec\codec\heif_decode.mbt
82.1% · 32 / 39 points · 143 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 Decode - 3
/// - 4
/// HEIF 图像格式解码 HEIF image format decoding - 5
- 6
///| - 7
/// HEIF (High Efficiency Image File Format) 解码器 - 8
/// - 9
/// HEIF 基于 HEVC (H.265) 编码,使用 ISOBMFF 容器格式。 - 10
/// 本模块提供格式检测和基本头部解析,完整解码需 HEVC 解码器支持。 - 11
/// - 12
/// Magic bytes: 0x00 0x00 0x00 0x?? 0x66 0x74 0x79 0x70 0x68 0x65 0x69 0x63(ftyp box,brand 为 "heic"、"heix"、"hevc" 或 "mif1") - 13
- 14
///| - 15
/// HEIF 图像信息 - 16
pub struct HeifInfo { - 17
width : Int32 - 18
height : Int32 - 19
bit_depth : Int32 - 20
num_channels : Int32 - 21
has_alpha : Bool - 22
} - 23
- 24
///| - 25
/// 检测数据是否为 HEIF 格式 - 26
/// - 27
/// HEIF 文件以 ftyp box 开头,brand 为 "heic", "heix", "hevc", "hevm", "heim", "heis", "mif1" 等 - 28
pub fn is_heif_format(data : Bytes) -> Bool { - 29●
let len = data.length() - 30
if len < 12 { - 31●
return false - 32
} - 33
// 检查 ftyp box: bytes 4-7 = "ftyp" - 34
if data[4] != b'f' || data[5] != b't' || data[6] != b'y' || data[7] != b'p' { - 35●
return false - 36
} - 37
let b8 = data[8] - 38
let b9 = data[9] - 39
let b10 = data[10] - 40
let b11 = data[11] - 41
// "heic" = 0x68 0x65 0x69 0x63 - 42
if b8 == b'h' && b9 == b'e' && b10 == b'i' && b11 == b'c' { - 43●
return true - 44
} - 45
// "heix" = 0x68 0x65 0x69 0x78 - 46
if b8 == b'h' && b9 == b'e' && b10 == b'i' && b11 == b'x' { - 47●
return true - 48
} - 49
// "hevc" = 0x68 0x65 0x76 0x63 - 50
if b8 == b'h' && b9 == b'e' && b10 == b'v' && b11 == b'c' { - 51●
return true - 52
} - 53
// "mif1" = 0x6D 0x69 0x66 0x31 - 54
if b8 == b'm' && b9 == b'i' && b10 == b'f' && b11 == b'1' { - 55●
return true - 56
} - 57
false - 58
} - 59
- 60
///| - 61
/// 解析 HEIF 头部信息 - 62
/// - 63
/// 解析 ISOBMFF box 结构,提取图像尺寸和位深信息。 - 64
pub fn heif_info(data : Bytes) -> HeifInfo raise @types.LoadError { - 65●
if !is_heif_format(data) { - 66●
raise @types.LoadError::UnsupportedFormat("Not a HEIF file") - 67
} - 68●
let len = data.length() - 69
let mut offset = 0 - 70
let mut width = 0 - 71
let mut height = 0 - 72
let mut bit_depth = 8 - 73
let mut has_alpha = false - 74
// 遍历 ISOBMFF boxes - 75
while offset + 8 <= len { - 76●
let box_size = (data[offset].to_int() << 24) | - 77●
(data[offset + 1].to_int() << 16) | - 78●
(data[offset + 2].to_int() << 8) | - 79●
data[offset + 3].to_int() - 80
if box_size < 8 || box_size > len - offset { - 81●
break - 82
} - 83
let box_type_0 = data[offset + 4] - 84
let box_type_1 = data[offset + 5] - 85
let box_type_2 = data[offset + 6] - 86
let box_type_3 = data[offset + 7] - 87
// 检查 ispe box (ImageSpatialExtentsProperty) - 88
if box_type_0 == b'i' && - 89
box_type_1 == b's' && - 90
box_type_2 == b'p' && - 91
box_type_3 == b'e' { - 92●
if offset + 20 <= len { - 93●
width = (data[offset + 12].to_int() << 24) | - 94●
(data[offset + 13].to_int() << 16) | - 95●
(data[offset + 14].to_int() << 8) | - 96●
data[offset + 15].to_int() - 97●
height = (data[offset + 16].to_int() << 24) | - 98●
(data[offset + 17].to_int() << 16) | - 99●
(data[offset + 18].to_int() << 8) | - 100●
data[offset + 19].to_int() - 101
} - 102
} - 103
// 检查 auxC box for alpha - 104
if box_type_0 == b'a' && - 105
box_type_1 == b'u' && - 106
box_type_2 == b'x' && - 107
box_type_3 == b'C' { - 108○
has_alpha = true - 109
} - 110
// 检查 pixi box for bit depth - 111
if box_type_0 == b'p' && - 112
box_type_1 == b'i' && - 113
box_type_2 == b'x' && - 114
box_type_3 == b'i' { - 115○
if offset + 13 <= len { - 116○
let num_ch = data[offset + 12].to_int() - 117
if num_ch > 0 && offset + 13 + num_ch <= len { - 118○
bit_depth = data[offset + 13].to_int() - 119
} - 120
} - 121
} - 122
offset += box_size - 123
} - 124
if width == 0 || height == 0 { - 125●
raise @types.LoadError::DecodeFailed( - 126
"HEIF: could not determine image dimensions", - 127
) - 128
} - 129◐
let num_channels = if has_alpha { 4 } else { 3 } - 130
HeifInfo::{ width, height, bit_depth, num_channels, has_alpha, } - 131
} - 132
- 133
///| - 134
/// 解码 HEIF 图像 - 135
/// - 136
/// 注意:完整 HEIF 解码需要 HEVC 解码器,当前返回 UnsupportedFormat。 - 137
/// 使用 heif_info 获取元数据。 - 138
pub fn heif_decode(data : Bytes) -> @types.Image raise @types.LoadError { - 139●
let info = heif_info(data) - 140
raise @types.LoadError::UnsupportedFormat( - 141
"HEIF decode not yet implemented (HEVC decoder required). Image is \{info.width}x\{info.height} \{info.bit_depth}-bit", - 142
) - 143
}