image.codec\codec\psd_extended.mbt
65.2% · 30 / 46 points · 116 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # PSD 扩展 PSD Extended - 3
/// - 4
/// PSD 扩展功能 PSD extended features - 5
- 6
///| - 7
/// PSD 扩展信息(包含通道数、位深、颜色模式、压缩方式等) - 8
pub struct PsdInfoExtended { - 9
width : Int32 - 10
height : Int32 - 11
bit_depth : Int32 - 12
channel_count : Int32 - 13
num_channels : Int32 - 14
has_alpha : Bool - 15
color_mode : Int32 - 16
color_mode_name : String - 17
compression : Int32 - 18
compression_name : String - 19
version : Int32 - 20
} - 21
- 22
///| - 23
/// 检测数据是否为 PSD 格式 - 24
pub fn is_psd_format(data : Bytes) -> Bool { - 25●
let len = data.length() - 26
if len < 26 { - 27●
return false - 28
} - 29
// signature "8BPS" (0x38 0x42 0x50 0x53) - 30●
data[0].to_int() == 0x38 && - 31●
data[1].to_int() == 0x42 && - 32●
data[2].to_int() == 0x50 && - 33●
data[3].to_int() == 0x53 - 34
} - 35
- 36
///| - 37
fn psd_color_mode_name(m : Int32) -> String { - 38●
match m { - 39○
0 => "Bitmap" - 40○
1 => "Grayscale" - 41○
2 => "Indexed" - 42●
3 => "RGB" - 43○
4 => "CMYK" - 44○
7 => "Multichannel" - 45○
8 => "Duotone" - 46○
9 => "Lab" - 47○
_ => "unknown" - 48
} - 49
} - 50
- 51
///| - 52
fn psd_compression_name(c : Int32) -> String { - 53●
match c { - 54●
0 => "Raw" - 55○
1 => "RLE" - 56○
2 => "ZIP without prediction" - 57○
3 => "ZIP with prediction" - 58○
_ => "unknown" - 59
} - 60
} - 61
- 62
///| - 63
/// 解析 PSD 扩展头部信息 - 64
/// - 65
/// 解析 PSD 文件头,提取尺寸、通道数、位深、颜色模式、压缩方式等 - 66
pub fn psd_info_extended( - 67
data : Bytes, - 68
) -> PsdInfoExtended raise @types.LoadError { - 69●
if !is_psd_format(data) { - 70●
raise @types.LoadError::UnsupportedFormat("Not a PSD file") - 71
} - 72●
let len = data.length() - 73●
let version = read_u16_be(data, 4) - 74●
let channel_count = read_u16_be(data, 12) - 75●
let height = read_u32_be(data, 14) - 76●
let width = read_u32_be(data, 18) - 77●
let bit_depth = read_u16_be(data, 22) - 78●
let color_mode = read_u16_be(data, 24) - 79
if width == 0 || height == 0 { - 80○
raise @types.LoadError::DecodeFailed("PSD 尺寸无效") - 81
} - 82
// 跳过 3 个 length 前缀段(color mode data / image resources / layer and mask data) - 83
let mut pos = 26 - 84
let mut compression = 0 - 85
for seg_idx in 0..<3 { - 86●
if pos + 4 > len { - 87○
break - 88
} - 89●
let seg_len = read_u32_be(data, pos) - 90
pos += 4 - 91
if seg_len > len - pos { - 92○
break - 93
} - 94
pos += seg_len - 95
let _ = seg_idx - 96
} - 97
// Image data: compression (2 bytes BE) - 98
if pos + 2 <= len { - 99●
compression = read_u16_be(data, pos) - 100
} - 101
let has_alpha = channel_count == 4 || channel_count == 5 - 102◐
let num_channels = if channel_count > 4 { 4 } else { channel_count } - 103
PsdInfoExtended::{ - 104
width, - 105
height, - 106
bit_depth, - 107
channel_count, - 108
num_channels, - 109
has_alpha, - 110
color_mode, - 111●
color_mode_name: psd_color_mode_name(color_mode), - 112
compression, - 113●
compression_name: psd_compression_name(compression), - 114
version, - 115
} - 116
}