image.codec\codec\qoi_extended.mbt
88.9% · 16 / 18 points · 62 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # QOI 扩展 QOI Extended - 3
/// - 4
/// QOI 扩展功能 QOI extended features - 5
- 6
///| - 7
/// QOI 扩展信息(包含通道数、色彩空间等) - 8
pub struct QoiInfoExtended { - 9
width : Int32 - 10
height : Int32 - 11
channels : Int32 - 12
colorspace : Int32 - 13
colorspace_name : String - 14
num_channels : Int32 - 15
has_alpha : Bool - 16
} - 17
- 18
///| - 19
/// 检测数据是否为 QOI 格式 - 20
pub fn is_qoi_format(data : Bytes) -> Bool { - 21●
let len = data.length() - 22
if len < 14 { - 23●
return false - 24
} - 25●
data[0].to_int() == 0x71 && - 26●
data[1].to_int() == 0x6F && - 27●
data[2].to_int() == 0x69 && - 28●
data[3].to_int() == 0x66 - 29
} - 30
- 31
///| - 32
/// 解析 QOI 扩展头部信息 - 33
pub fn qoi_info_extended( - 34
data : Bytes, - 35
) -> QoiInfoExtended raise @types.LoadError { - 36●
if !is_qoi_format(data) { - 37●
raise @types.LoadError::UnsupportedFormat("Not a QOI file") - 38
} - 39●
let width = read_u32_be(data, 4) - 40●
let height = read_u32_be(data, 8) - 41●
let channels = data[12].to_int() - 42●
let colorspace = data[13].to_int() - 43
if width == 0 || height == 0 { - 44○
raise @types.LoadError::DecodeFailed("QOI 尺寸无效") - 45
} - 46
if channels != 3 && channels != 4 { - 47○
raise @types.LoadError::DecodeFailed("QOI 通道数无效") - 48
} - 49
QoiInfoExtended::{ - 50
width, - 51
height, - 52
channels, - 53
colorspace, - 54
colorspace_name: if colorspace == 0 { - 55●
"sRGB with linear alpha" - 56
} else { - 57●
"all linear" - 58
}, - 59
num_channels: channels, - 60
has_alpha: channels == 4, - 61
} - 62
}