image.codec\codec\hdr_extended.mbt
94.1% · 48 / 51 points · 119 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # HDR 扩展 HDR Extended - 3
/// - 4
/// HDR 扩展功能 HDR extended features - 5
- 6
///| - 7
/// HDR 扩展信息(包含尺寸、通道数等) - 8
pub struct HdrInfoExtended { - 9
width : Int32 - 10
height : Int32 - 11
num_channels : Int32 - 12
has_alpha : Bool - 13
format : String - 14
} - 15
- 16
///| - 17
/// 检测数据是否为 HDR (Radiance RGBE) 格式 - 18
pub fn is_hdr_format(data : Bytes) -> Bool { - 19●
let len = data.length() - 20
if len < 2 { - 21●
return false - 22
} - 23●
data[0].to_int() == 0x23 && data[1].to_int() == 0x3F - 24
} - 25
- 26
///| - 27
/// 解析 HDR 扩展头部信息 - 28
pub fn hdr_info_extended( - 29
data : Bytes, - 30
) -> HdrInfoExtended raise @types.LoadError { - 31●
if !is_hdr_format(data) { - 32●
raise @types.LoadError::UnsupportedFormat("Not an HDR file") - 33
} - 34●
let len = data.length() - 35
let mut pos = 2 - 36
// 跳过 magic 行 - 37●
while pos < len && data[pos].to_int() != 0x0A { - 38●
pos += 1 - 39
} - 40
pos += 1 - 41
// 跳过 header 行,查找尺寸行 - 42
let mut format = "RGBE" - 43
while pos < len { - 44●
let b = data[pos].to_int() - 45
if b == 0x2D || b == 0x2B { - 46●
break - 47
} - 48
// 检查 FORMAT= 行 - 49
if pos + 7 <= len && - 50●
data[pos].to_int() == 0x46 && - 51●
data[pos + 1].to_int() == 0x4F && - 52●
data[pos + 2].to_int() == 0x52 && - 53●
data[pos + 3].to_int() == 0x4D && - 54●
data[pos + 4].to_int() == 0x41 && - 55●
data[pos + 5].to_int() == 0x54 && - 56●
data[pos + 6].to_int() == 0x3D { - 57
// FORMAT= 后面的值 - 58●
let fmt_start = pos + 7 - 59
let mut fmt_end = fmt_start - 60●
while fmt_end < len && data[fmt_end].to_int() != 0x0A { - 61●
fmt_end += 1 - 62
} - 63
let fmt_bytes : Array[Byte] = [] - 64
for i in fmt_start..<fmt_end { - 65●
fmt_bytes.push(data[i]) - 66
} - 67●
format = @encoding/utf8.decode(Bytes::from_array(fmt_bytes)) catch { - 68○
_ => "RGBE" - 69
} - 70
} - 71
// 跳过当前行 - 72●
while pos < len && data[pos].to_int() != 0x0A { - 73●
pos += 1 - 74
} - 75
pos += 1 - 76
} - 77
// 解析尺寸行: "-Y HEIGHT +X WIDTH\n" - 78
let mut width = 0 - 79
let mut height = 0 - 80
let vals : Array[Int32] = [] - 81●
while pos < len && data[pos].to_int() != 0x0A { - 82●
let b = data[pos].to_int() - 83
if b == 0x2D || b == 0x2B { - 84●
pos += 1 - 85
// 跳过字母 (Y/X) - 86
while pos < len && - 87●
data[pos].to_int() != 0x2D && - 88●
data[pos].to_int() != 0x2B && - 89●
data[pos].to_int() != 0x0A { - 90●
let c = data[pos].to_int() - 91
if c >= 0x30 && c <= 0x39 { - 92●
let mut num = 0 - 93
while pos < len { - 94●
let d = data[pos].to_int() - 95
if d >= 0x30 && d <= 0x39 { - 96●
num = num * 10 + (d - 0x30) - 97
pos += 1 - 98
} else { - 99●
break - 100
} - 101
} - 102●
vals.push(num) - 103
} else { - 104●
pos += 1 - 105
} - 106
} - 107
} else { - 108○
pos += 1 - 109
} - 110
} - 111●
if vals.length() >= 2 { - 112●
height = vals[0] - 113
width = vals[1] - 114
} - 115
if width == 0 || height == 0 { - 116○
raise @types.LoadError::DecodeFailed("HDR 尺寸无效") - 117
} - 118
HdrInfoExtended::{ width, height, num_channels: 3, has_alpha: false, format, } - 119
}