image.codec\codec\psd_decode.mbt
93.5% · 29 / 31 points · 120 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 Decode - 3
/// - 4
/// Adobe PSD 格式解码 Adobe PSD format decoding - 5
// 纯 MoonBit PSD 解码器 - 6
// 支持 8-bit RGB(channelCount=3)/ RGBA(channelCount=4)、无压缩(compression=0) - 7
// 大端序字节流,按通道排列像素数据交错为 RGBRGBRGB - 8
// 规范: Adobe Photoshop PSD File Format + stb_image.h:6126-6280 stbi__psd_load - 9
- 10
///| - 11
/// 解码 PSD 字节流为 Image,支持 8-bit RGB(channelCount=3)/ RGBA(channelCount=4)、无压缩(compression=0),像素数据按通道排列(RRRGGGBBB),交错为 Image.data 格式(RGBRGBRGB) Decode PSD byte stream to Image, Supports 8-bit RGB (channelCount=3) / RGBA (channelCount=4), uncompressed (compression=0), Pixel data stored per-channel (RRRGGGBBB), interleaved to Image.data format (RGBRGBRGB) - 12
- 13
///| - 14
/// 参数 Parameters: - 15
/// - `data` - PSD 字节流 PSD byte stream - 16
/// - 17
/// 返回 Return: - 18
/// - 解码后的 8-bit Image Decoded 8-bit Image - 19
/// - 20
/// 错误 Errors: - 21
/// - `LoadError::DecodeFailed` - 数据过短、8BPS 签名无效、不支持的颜色模式/压缩 Data too short, invalid 8BPS signature, unsupported color mode/compression - 22
pub fn decode_psd_pure(data : Bytes) -> @types.Image raise @types.LoadError { - 23●
let len = data.length() - 24
// Header 26 字节 - 25
if len < 26 { - 26●
raise @types.LoadError::DecodeFailed( - 27
"PSD 数据过短:无法读取完整 header", - 28
) - 29
} - 30
// signature "8BPS" (0x38 0x42 0x50 0x53) - 31●
if data[0].to_int() != 0x38 || - 32●
data[1].to_int() != 0x42 || - 33●
data[2].to_int() != 0x50 || - 34●
data[3].to_int() != 0x53 { - 35●
raise @types.LoadError::DecodeFailed("PSD signature 错误,非 \"8BPS\"") - 36
} - 37
// version (2 字节 BE),必须为 1 - 38●
let version = read_u16_be(data, 4) - 39
if version != 1 { - 40●
raise @types.LoadError::DecodeFailed("PSD version 错误,仅支持 1") - 41
} - 42
// reserved(6 字节) 跳过 - 43
// channelCount (2 字节 BE) - 44●
let channel_count = read_u16_be(data, 12) - 45
if channel_count != 3 && channel_count != 4 { - 46●
raise @types.LoadError::DecodeFailed( - 47
"不支持的 PSD channelCount,仅支持 3(RGB)和 4(RGBA)", - 48
) - 49
} - 50
// h (4 字节 BE) - 51●
let h = read_u32_be(data, 14) - 52
// w (4 字节 BE) - 53●
let w = read_u32_be(data, 18) - 54
// bitdepth (2 字节 BE),仅支持 8 - 55●
let bitdepth = read_u16_be(data, 22) - 56
if bitdepth != 8 { - 57●
raise @types.LoadError::DecodeFailed( - 58
"不支持的 PSD bitdepth,仅支持 8", - 59
) - 60
} - 61
// colorMode (2 字节 BE),仅支持 3(RGB) - 62●
let color_mode = read_u16_be(data, 24) - 63
if color_mode != 3 { - 64●
raise @types.LoadError::DecodeFailed( - 65
"不支持的 PSD colorMode,仅支持 3(RGB)", - 66
) - 67
} - 68
// 尺寸校验 - 69
if w == 0 || h == 0 { - 70●
raise @types.LoadError::DecodeFailed("PSD 尺寸无效") - 71
} - 72
// 跳过 3 个 length 前缀段(color mode data / image resources / layer and mask data) - 73
let mut pos = 26 - 74
for _ in 0..<3 { - 75●
if pos + 4 > len { - 76○
raise @types.LoadError::DecodeFailed( - 77
"PSD 数据过短:无法读取 4 字节 BE", - 78
) - 79
} - 80●
let seg_len = read_u32_be(data, pos) - 81
pos += 4 - 82
if seg_len > len - pos { - 83●
raise @types.LoadError::DecodeFailed("PSD 段数据越界") - 84
} - 85
pos += seg_len - 86
} - 87
// Image data: compression (2 字节 BE) - 88
if pos + 2 > len { - 89○
raise @types.LoadError::DecodeFailed( - 90
"PSD 数据过短:无法读取 2 字节 BE", - 91
) - 92
} - 93●
let compression = read_u16_be(data, pos) - 94
pos += 2 - 95
if compression != 0 { - 96●
raise @types.LoadError::DecodeFailed( - 97
"不支持的 PSD compression,仅支持 0(无压缩)", - 98
) - 99
} - 100
// 像素数据:按通道排列,每通道 w*h 字节 - 101
let pixel_count = w * h - 102
let pixel_bytes = channel_count * pixel_count - 103
if pos + pixel_bytes > len { - 104●
raise @types.LoadError::DecodeFailed("PSD 像素数据不足") - 105
} - 106
// 交错:data[i * channel_count + c] = raw[c * w * h + i] - 107●
let interleaved : Array[Byte] = Array::make(pixel_bytes, b'\x00') - 108
for c in 0..<channel_count { - 109●
let channel_base = c * pixel_count - 110
for i in 0..<pixel_count { - 111●
interleaved[i * channel_count + c] = data[pos + channel_base + i] - 112
} - 113
} - 114
@types.Image::{ - 115
width: w, - 116
height: h, - 117
channels: channel_count, - 118●
data: Bytes::from_array(interleaved), - 119
} - 120
}