image.codec\codec\jpeg_extended.mbt
65.9% · 27 / 41 points · 134 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 扩展 JPEG Extended - 3
/// - 4
/// JPEG 扩展功能 JPEG extended features - 5
- 6
///| - 7
/// JPEG 扩展信息(包含精度、分量数、渐进标志等) - 8
pub struct JpegInfoExtended { - 9
width : Int32 - 10
height : Int32 - 11
precision : Int32 - 12
num_components : Int32 - 13
is_grayscale : Bool - 14
is_progressive : Bool - 15
sof_marker : Int32 - 16
sof_name : String - 17
num_channels : Int32 - 18
has_alpha : Bool - 19
} - 20
- 21
///| - 22
/// 检测数据是否为 JPEG 格式 - 23
pub fn is_jpeg_format(data : Bytes) -> Bool { - 24●
let len = data.length() - 25
if len < 4 { - 26●
return false - 27
} - 28●
data[0].to_int() == 0xFF && - 29●
data[1].to_int() == 0xD8 && - 30●
data[2].to_int() == 0xFF - 31
} - 32
- 33
///| - 34
fn jpeg_sof_name(marker : Int32) -> String { - 35●
match marker { - 36●
0xC0 => "Baseline DCT" - 37○
0xC1 => "Extended sequential DCT" - 38●
0xC2 => "Progressive DCT" - 39○
0xC3 => "Lossless sequential" - 40○
0xC5 => "Differential sequential DCT" - 41○
0xC6 => "Differential progressive DCT" - 42○
0xC7 => "Differential lossless" - 43○
0xC9 => "Extended sequential arithmetic" - 44○
0xCA => "Progressive arithmetic" - 45○
0xCB => "Lossless arithmetic" - 46○
_ => "unknown" - 47
} - 48
} - 49
- 50
///| - 51
fn jpeg_is_sof_marker(marker : Int32) -> Bool { - 52●
marker == 0xC0 || - 53
marker == 0xC1 || - 54
marker == 0xC2 || - 55
marker == 0xC3 || - 56
marker == 0xC5 || - 57
marker == 0xC6 || - 58
marker == 0xC7 || - 59
marker == 0xC9 || - 60
marker == 0xCA || - 61
marker == 0xCB - 62
} - 63
- 64
///| - 65
/// 解析 JPEG 扩展头部信息 - 66
/// - 67
/// 遍历 JPEG markers,查找 SOF marker 提取尺寸、精度、分量数、渐进标志 - 68
pub fn jpeg_info_extended( - 69
data : Bytes, - 70
) -> JpegInfoExtended raise @types.LoadError { - 71●
if !is_jpeg_format(data) { - 72●
raise @types.LoadError::UnsupportedFormat("Not a JPEG file") - 73
} - 74●
let len = data.length() - 75
let mut pos = 2 - 76
let mut width = 0 - 77
let mut height = 0 - 78
let mut precision = 8 - 79
let mut num_components = 3 - 80
let mut sof_marker = 0xC0 - 81
let mut found_sof = false - 82
while pos + 2 <= len { - 83●
if data[pos].to_int() != 0xFF { - 84○
pos += 1 - 85
continue - 86
} - 87●
let marker = data[pos + 1].to_int() - 88
if marker == 0x00 || marker == 0xFF { - 89○
pos += 1 - 90
continue - 91
} - 92
if marker == 0xD8 || marker == 0xD9 { - 93○
pos += 2 - 94
continue - 95
} - 96
if marker >= 0xD0 && marker <= 0xD7 { - 97○
pos += 2 - 98
continue - 99
} - 100
if pos + 4 > len { - 101○
break - 102
} - 103●
let seg_len = read_u16_be(data, pos + 2) - 104●
if jpeg_is_sof_marker(marker) && pos + 9 <= len { - 105●
precision = data[pos + 4].to_int() - 106●
height = read_u16_be(data, pos + 5) - 107●
width = read_u16_be(data, pos + 7) - 108●
num_components = data[pos + 9].to_int() - 109
sof_marker = marker - 110
found_sof = true - 111
break - 112
} - 113
pos += 2 + seg_len - 114
} - 115
if !found_sof || width == 0 || height == 0 { - 116●
raise @types.LoadError::DecodeFailed( - 117
"JPEG: could not determine image dimensions", - 118
) - 119
} - 120
let is_progressive = sof_marker == 0xC2 - 121
let is_grayscale = num_components == 1 - 122
JpegInfoExtended::{ - 123
width, - 124
height, - 125
precision, - 126
num_components, - 127
is_grayscale, - 128
is_progressive, - 129
sof_marker, - 130●
sof_name: jpeg_sof_name(sof_marker), - 131
num_channels: num_components, - 132
has_alpha: false, - 133
} - 134
}