image.codec\codec\jpegxl_extended.mbt
71.9% · 41 / 57 points · 160 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # JPEG-XL 扩展 JPEG-XL Extended - 3
/// - 4
/// JPEG-XL 扩展功能 JPEG-XL extended features - 5
- 6
///| - 7
/// JPEG-XL 扩展信息 - 8
pub struct JpegXlInfoExtended { - 9
width : Int32 - 10
height : Int32 - 11
bit_depth : Int32 - 12
num_channels : Int32 - 13
has_alpha : Bool - 14
is_lossless : Bool - 15
is_codestream : Bool - 16
orientation : Int32 - 17
} - 18
- 19
///| - 20
/// 位流读取器 - 21
priv struct JxlBitReader { - 22
data : Bytes - 23
mut byte_pos : Int32 - 24
mut bit_pos : Int32 - 25
} - 26
- 27
///| - 28
fn JxlBitReader::new(data : Bytes, start : Int32) -> JxlBitReader { - 29●
JxlBitReader::{ data, byte_pos: start, bit_pos: 0, } - 30
} - 31
- 32
///| - 33
fn JxlBitReader::read_bit(self : JxlBitReader) -> Int32 { - 34●
if self.byte_pos >= self.data.length() { - 35○
return 0 - 36
} - 37●
let byte = self.data[self.byte_pos].to_int() - 38
let bit = (byte >> (7 - self.bit_pos)) & 1 - 39
self.bit_pos += 1 - 40
if self.bit_pos == 8 { - 41●
self.bit_pos = 0 - 42
self.byte_pos += 1 - 43
} - 44
bit - 45
} - 46
- 47
///| - 48
fn JxlBitReader::read_bits(self : JxlBitReader, n : Int32) -> Int32 { - 49●
let mut result = 0 - 50
for _ in 0..<n { - 51●
result = (result << 1) | self.read_bit() - 52
} - 53
result - 54
} - 55
- 56
///| - 57
fn JxlBitReader::read_u32( - 58
self : JxlBitReader, - 59
d0 : Int32, - 60
d1 : Int32, - 61
d2 : Int32, - 62
d3 : Int32, - 63
) -> Int32 { - 64●
let selector = self.read_bits(2) - 65●
match selector { - 66●
0 => d0 - 67○
1 => d1 + self.read_bits(8) - 68○
2 => d2 + self.read_bits(16) - 69○
_ => d3 + self.read_bits(24) - 70
} - 71
} - 72
- 73
///| - 74
/// 从宽高比计算宽度 - 75
fn jxl_width_from_ratio( - 76
height : Int32, - 77
ratio : Int32, - 78
reader : JxlBitReader, - 79
) -> Int32 { - 80●
match ratio { - 81●
0 => height - 82●
1 => height * 4 / 3 - 83●
2 => height * 3 / 2 - 84○
3 => height * 16 / 9 - 85○
4 => height * 5 / 4 - 86○
5 => height * 2 - 87○
6 => if height >= 2 { height / 2 } else { 1 } - 88○
_ => reader.read_u32(1, 257, 65793, 0) + 1 - 89
} - 90
} - 91
- 92
///| - 93
/// 解析 JPEG-XL 裸码流 SizeHeader - 94
fn jxl_parse_size_header(data : Bytes, start : Int32) -> (Int32, Int32) { - 95●
let reader = JxlBitReader::new(data, start) - 96●
let small = reader.read_bit() - 97
let height = if small == 1 { - 98●
reader.read_bits(5) + 1 - 99
} else { - 100●
reader.read_u32(1, 257, 65793, 0) + 1 - 101
} - 102●
let ratio = reader.read_bits(3) - 103●
let width = jxl_width_from_ratio(height, ratio, reader) - 104
(width, height) - 105
} - 106
- 107
///| - 108
/// 在 ISOBMFF 容器中查找 codestream box 的偏移 - 109
fn jxl_find_codestream(data : Bytes) -> Int32 { - 110●
let len = data.length() - 111
let mut offset = 0 - 112
while offset + 8 <= len { - 113●
let box_size = read_u32_be(data, offset) - 114
if box_size < 8 || box_size > len - offset { - 115○
break - 116
} - 117●
let btype = box_type_str(data, offset) - 118
// jxlc = codestream box, jxlp = partial codestream - 119
if btype == "jxlc" || btype == "jxlp" { - 120●
return offset + 8 - 121
} - 122
offset += box_size - 123
} - 124
-1 - 125
} - 126
- 127
///| - 128
/// 解析 JPEG-XL 扩展头部信息 - 129
/// - 130
/// 解析 SizeHeader 提取图像尺寸,支持裸码流和 ISOBMFF 容器两种格式 - 131
pub fn jpegxl_info_extended( - 132
data : Bytes, - 133
) -> JpegXlInfoExtended raise @types.LoadError { - 134●
if !is_jpegxl_format(data) { - 135●
raise @types.LoadError::UnsupportedFormat("Not a JPEG-XL file") - 136
} - 137●
let is_cs = is_jpegxl_codestream(data) - 138
let codestream_start = if is_cs { - 139●
2 - 140
} else { - 141●
let cs = jxl_find_codestream(data) - 142
if cs < 0 { - 143●
raise @types.LoadError::DecodeFailed( - 144
"JPEG-XL: codestream box not found in container", - 145
) - 146
} - 147
cs - 148
} - 149●
let (width, height) = jxl_parse_size_header(data, codestream_start) - 150
JpegXlInfoExtended::{ - 151
width, - 152
height, - 153
bit_depth: 8, - 154
num_channels: 3, - 155
has_alpha: false, - 156
is_lossless: false, - 157
is_codestream: is_cs, - 158
orientation: 1, - 159
} - 160
}