image.codec\codec\webp_extended.mbt
70.3% · 45 / 64 points · 166 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # WebP 扩展 WebP Extended - 3
/// - 4
/// WebP 扩展功能 WebP extended features - 5
- 6
///| - 7
/// WebP 扩展信息(包含格式类型、alpha、动画、元数据标志等) - 8
pub struct WebpInfoExtended { - 9
width : Int32 - 10
height : Int32 - 11
format : String - 12
has_alpha : Bool - 13
has_animation : Bool - 14
has_icc_profile : Bool - 15
has_exif : Bool - 16
has_xmp : Bool - 17
num_channels : Int32 - 18
} - 19
- 20
///| - 21
/// 检测数据是否为 WebP 格式 - 22
pub fn is_webp_format(data : Bytes) -> Bool { - 23●
let len = data.length() - 24
if len < 12 { - 25●
return false - 26
} - 27
// RIFF magic - 28●
if data[0].to_int() != 0x52 || - 29●
data[1].to_int() != 0x49 || - 30●
data[2].to_int() != 0x46 || - 31●
data[3].to_int() != 0x46 { - 32●
return false - 33
} - 34
// WEBP fourcc - 35●
data[8].to_int() == 0x57 && - 36●
data[9].to_int() == 0x45 && - 37●
data[10].to_int() == 0x42 && - 38●
data[11].to_int() == 0x50 - 39
} - 40
- 41
///| - 42
/// 解析 WebP 扩展头部信息 - 43
/// - 44
/// 遍历 RIFF chunk 结构,提取格式类型、尺寸、alpha、动画、ICC/EXIF/XMP 标志 - 45
pub fn webp_info_extended( - 46
data : Bytes, - 47
) -> WebpInfoExtended raise @types.LoadError { - 48●
if !is_webp_format(data) { - 49●
raise @types.LoadError::UnsupportedFormat("Not a WebP file") - 50
} - 51●
let len = data.length() - 52
let mut width = 0 - 53
let mut height = 0 - 54
let mut format = "unknown" - 55
let mut has_alpha = false - 56
let mut has_animation = false - 57
let mut has_icc_profile = false - 58
let mut has_exif = false - 59
let mut has_xmp = false - 60
// 遍历 RIFF chunks - 61
let mut pos = 12 - 62
while pos + 8 <= len { - 63●
let chunk_id = read_4cc(data, pos) - 64●
let chunk_size = data[pos + 4].to_int() | - 65●
(data[pos + 5].to_int() << 8) | - 66●
(data[pos + 6].to_int() << 16) | - 67●
(data[pos + 7].to_int() << 24) - 68
if chunk_size < 0 || pos + 8 + chunk_size > len { - 69○
break - 70
} - 71
let chunk_data_start = pos + 8 - 72●
match chunk_id { - 73●
"VP8X" => { - 74
format = "extended" - 75
if chunk_data_start + 10 <= len { - 76●
let flags = data[chunk_data_start].to_int() - 77
has_alpha = (flags & 0x10) != 0 - 78
has_animation = (flags & 0x20) != 0 - 79
has_icc_profile = (flags & 0x01) != 0 - 80
has_exif = (flags & 0x08) != 0 - 81
has_xmp = (flags & 0x04) != 0 - 82
// canvas width/height: 24-bit LE + 1 - 83●
width = data[chunk_data_start + 4].to_int() | - 84●
(data[chunk_data_start + 5].to_int() << 8) | - 85●
(data[chunk_data_start + 6].to_int() << 16) - 86
width += 1 - 87●
height = data[chunk_data_start + 7].to_int() | - 88●
(data[chunk_data_start + 8].to_int() << 8) | - 89●
(data[chunk_data_start + 9].to_int() << 16) - 90
height += 1 - 91
} - 92
} - 93○
"VP8 " => { - 94
if format != "extended" { - 95○
format = "lossy" - 96
} - 97
// VP8 keyframe: 3 bytes tag + 3 bytes start code + 14 bits width + 2 bits + 14 bits height - 98
if chunk_data_start + 10 <= len && width == 0 { - 99○
let tag = data[chunk_data_start].to_int() | - 100○
(data[chunk_data_start + 1].to_int() << 8) | - 101○
(data[chunk_data_start + 2].to_int() << 16) - 102
let is_keyframe = (tag & 1) == 0 - 103
if is_keyframe { - 104
// start code: 0x9d 0x01 0x2a - 105○
if chunk_data_start + 9 <= len { - 106○
let w = data[chunk_data_start + 6].to_int() | - 107○
(data[chunk_data_start + 7].to_int() << 8) - 108○
let h = data[chunk_data_start + 8].to_int() | - 109○
(data[chunk_data_start + 9].to_int() << 8) - 110
width = w & 0x3FFF - 111
height = h & 0x3FFF - 112
} - 113
} - 114
} - 115
} - 116●
"VP8L" => { - 117
if format != "extended" { - 118●
format = "lossless" - 119
} - 120
// VP8L: 0x2F signature + 14 bits width + 14 bits height + 1 bit alpha - 121
if chunk_data_start + 5 <= len && width == 0 { - 122●
if data[chunk_data_start].to_int() == 0x2F { - 123●
let b1 = data[chunk_data_start + 1].to_int() - 124●
let b2 = data[chunk_data_start + 2].to_int() - 125●
let b3 = data[chunk_data_start + 3].to_int() - 126●
let b4 = data[chunk_data_start + 4].to_int() - 127
// width: 14 bits LE from b1 (8 bits) + b2 (6 bits) - 128
width = (b1 | ((b2 & 0x3F) << 8)) + 1 - 129
// height: 14 bits from b2 (2 bits) + b3 (8 bits) + b4 (4 bits) - 130
height = (((b2 >> 6) & 0x03) | (b3 << 2) | ((b4 & 0x0F) << 10)) + 1 - 131
// alpha: 1 bit from b4 bit 4 - 132
if format != "extended" { - 133●
has_alpha = ((b4 >> 4) & 1) == 1 - 134
} - 135
} - 136
} - 137
} - 138○
"ANIM" => has_animation = true - 139○
"ICCP" => has_icc_profile = true - 140○
"EXIF" => has_exif = true - 141○
"XMP " => has_xmp = true - 142○
_ => () - 143
} - 144
pos += 8 + chunk_size - 145
if chunk_size % 2 == 1 { - 146●
pos += 1 // padding - 147
} - 148
} - 149
if width == 0 || height == 0 { - 150○
raise @types.LoadError::DecodeFailed( - 151
"WebP: could not determine image dimensions", - 152
) - 153
} - 154●
let num_channels = if has_alpha { 4 } else { 3 } - 155
WebpInfoExtended::{ - 156
width, - 157
height, - 158
format, - 159
has_alpha, - 160
has_animation, - 161
has_icc_profile, - 162
has_exif, - 163
has_xmp, - 164
num_channels, - 165
} - 166
}