image.codec\codec\png_decode.mbt
87.6% · 113 / 129 points · 271 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # PNG 解码 PNG Decode - 3
/// - 4
/// PNG 图像格式解码 PNG image format decoding - 5
// 纯 MoonBit PNG 解码(全目标可用:native/wasm/js) - 6
// 依赖 @pure/zlib 进行 IDAT 解压缩 - 7
// 支持 color type 0/2/3/4/6, bit depth 8/16 - 8
- 9
///| - 10
/// PNG 颜色类型对应的通道数 - 11
fn png_channels(color_type : Int32) -> Int32? { - 12●
match color_type { - 13●
0 => Some(1) // gray - 14●
2 => Some(3) // rgb - 15●
3 => Some(1) // palette - 16●
4 => Some(2) // gray + alpha - 17●
6 => Some(4) // rgba - 18●
_ => None - 19
} - 20
} - 21
- 22
///| - 23
/// Paeth 预测函数 - 24
fn paeth_predict(a : Int32, b : Int32, c : Int32) -> Int32 { - 25●
let p = a + b - c - 26●
let pa = (p - a).abs() - 27●
let pb = (p - b).abs() - 28●
let pc = (p - c).abs() - 29
if pa <= pb && pa <= pc { - 30●
a - 31●
} else if pb <= pc { - 32●
b - 33
} else { - 34●
c - 35
} - 36
} - 37
- 38
///| - 39
/// PNG 解码(纯 MoonBit,全目标可用)。支持 8-bit gray/RGB/RGBA/gray+alpha/palette。不支持 interlace。req_channels: None=原色(保留 alpha), 1=灰度, 2=灰度+alpha, 3=RGB, 4=RGBA PNG decoding (pure MoonBit, all targets). Supports 8-bit gray/RGB/RGBA/gray+alpha/palette. Does not support interlace. req_channels: None=original (preserve alpha), 1=gray, 2=gray+alpha, 3=RGB, 4=RGBA - 40
- 41
///| - 42
/// ```mbt check - 43
/// test "doc_decode_png_pure" { - 44
/// try { - 45
/// let img = @codec.decode_png_pure(@types.DOC_TEST_PNG_1X1_RGBA) - 46
/// assert_eq(img.width, 1) - 47
/// assert_eq(img.height, 1) - 48
/// assert_eq(img.channels, 4) - 49
/// } catch { - 50
/// _ => assert_true(false) - 51
/// } - 52
/// } - 53
/// ``` - 54
pub fn decode_png_pure( - 55
data : Bytes, - 56
req_channels~ : Int32? = None, - 57
deadline~ : @types.Deadline? = None, - 58
) -> @types.Image raise @types.LoadError { - 59●
let len = data.length() - 60
// 验证 PNG signature - 61●
if len < 8 || !is_png_signature(data, 0) { - 62●
raise @types.LoadError::DecodeFailed("PNG signature 错误") - 63
} - 64
let mut pos = 8 - 65
// 解析 chunks - 66
let mut width = 0 - 67
let mut height = 0 - 68
let mut bit_depth = 0 - 69
let mut color_type = 0 - 70
let mut interlace = 0 - 71
let palette : Array[(Int32, Int32, Int32)] = [] - 72
let idat_data : Array[Byte] = [] - 73
let mut found_ihdr = false - 74
let mut found_iend = false - 75
while pos < len && !found_iend { - 76●
if pos + 8 > len { - 77●
raise @types.LoadError::DecodeFailed("PNG chunk 头部过短") - 78
} - 79●
let chunk_len = read_u32_be(data, pos) - 80
if chunk_len < 0 { - 81●
raise @types.LoadError::DecodeFailed("PNG chunk 长度无效") - 82
} - 83●
let chunk_key = read_u32_be(data, pos + 4) - 84
pos += 8 - 85
if chunk_len > len - pos { - 86●
raise @types.LoadError::DecodeFailed("PNG chunk 数据越界") - 87
} - 88●
match chunk_key { - 89●
0x49484452 => { - 90
// IHDR - 91
if chunk_len < 13 { - 92○
raise @types.LoadError::DecodeFailed("PNG IHDR 过短") - 93
} - 94●
width = read_u32_be(data, pos) - 95●
height = read_u32_be(data, pos + 4) - 96●
bit_depth = data[pos + 8].to_int() - 97●
color_type = data[pos + 9].to_int() - 98●
interlace = data[pos + 12].to_int() - 99
found_ihdr = true - 100
} - 101○
0x504C5445 => { - 102
// PLTE - 103
let num_colors = chunk_len / 3 - 104
for i in 0..<num_colors { - 105○
palette.push( - 106
( - 107○
data[pos + i * 3].to_int(), - 108○
data[pos + i * 3 + 1].to_int(), - 109○
data[pos + i * 3 + 2].to_int(), - 110
), - 111
) - 112
} - 113
} - 114
0x49444154 => - 115
// IDAT - 116●
for i in 0..<chunk_len { - 117●
idat_data.push(data[pos + i]) - 118
} - 119
0x49454E44 => - 120
// IEND - 121●
found_iend = true - 122●
_ => () - 123
} - 124
pos += chunk_len + 4 // skip data + CRC - 125
} - 126
if !found_ihdr { - 127●
raise @types.LoadError::DecodeFailed("PNG 无 IHDR chunk") - 128
} - 129
if width <= 0 || height <= 0 { - 130●
raise @types.LoadError::DecodeFailed("PNG 尺寸无效") - 131
} - 132
if interlace != 0 { - 133●
raise @types.LoadError::DecodeFailed("PNG interlace 不支持") - 134
} - 135
if bit_depth != 8 { - 136●
raise @types.LoadError::DecodeFailed("PNG 仅支持 8-bit bit depth") - 137
} - 138●
let ch = match png_channels(color_type) { - 139●
Some(c) => c - 140●
None => raise @types.LoadError::DecodeFailed("PNG 无效 color type") - 141
} - 142●
@types.check_dims(width, height, ch) - 143
// zlib inflate IDAT 数据 - 144●
let raw = @util.zlib_inflate(Bytes::from_array(idat_data), deadline~) - 145
// unfilter scanlines - 146
let bpp = ch // bytes per pixel (8-bit) - 147
let stride = width * bpp - 148●
let unfiltered : Array[Byte] = Array::make(height * stride, b'\x00') - 149
let mut raw_pos = 0 - 150
for y in 0..<height { - 151●
@types.deadline_guard(deadline, "PNG 解码") - 152●
if raw_pos >= raw.length() { - 153○
raise @types.LoadError::DecodeFailed("PNG 解压数据过短") - 154
} - 155●
let filter_type = raw[raw_pos].to_int() - 156
raw_pos += 1 - 157
for x in 0..<stride { - 158●
if raw_pos >= raw.length() { - 159○
raise @types.LoadError::DecodeFailed("PNG 解压数据过短") - 160
} - 161●
let raw_byte = raw[raw_pos].to_int() - 162
raw_pos += 1 - 163
let a = if x >= bpp { - 164●
unfiltered[y * stride + x - bpp].to_int() - 165
} else { - 166●
0 - 167
} - 168●
let b = if y > 0 { unfiltered[(y - 1) * stride + x].to_int() } else { 0 } - 169
let c = if x >= bpp && y > 0 { - 170●
unfiltered[(y - 1) * stride + x - bpp].to_int() - 171
} else { - 172●
0 - 173
} - 174●
let val = match filter_type { - 175●
0 => raw_byte // None - 176●
1 => (raw_byte + a) & 0xFF // Sub - 177●
2 => (raw_byte + b) & 0xFF // Up - 178●
3 => (raw_byte + (a + b) / 2) & 0xFF // Average - 179●
4 => (raw_byte + paeth_predict(a, b, c)) & 0xFF // Paeth - 180○
_ => raise @types.LoadError::DecodeFailed("PNG 无效 filter type") - 181
} - 182●
unfiltered[y * stride + x] = val.to_byte() - 183
} - 184
} - 185
// 转换为 Image(保留 alpha,根据 req_channels 决定输出通道数) - 186
let pixel_count = width * height - 187●
let out_ch = match req_channels { - 188●
Some(n) => n - 189●
None => ch // 默认保留原色(含 alpha) - 190
} - 191○
if color_type == 3 && palette.length() == 0 { - 192○
raise @types.LoadError::DecodeFailed("PNG palette 缺失") - 193
} - 194●
let out_data : Array[Byte] = Array::make(pixel_count * out_ch, b'\x00') - 195
for i in 0..<pixel_count { - 196●
let (r, g, b, a) = match color_type { - 197○
3 => { - 198
// palette - 199○
let idx = unfiltered[i].to_int() - 200○
if idx >= palette.length() { - 201○
raise @types.LoadError::DecodeFailed("PNG palette 索引越界") - 202
} - 203
let (r, g, b) = palette[idx] - 204
(r, g, b, 255) - 205
} - 206●
0 => { - 207
// gray - 208●
let v = unfiltered[i].to_int() - 209
(v, v, v, 255) - 210
} - 211
2 => - 212
// RGB - 213●
( - 214●
unfiltered[i * 3].to_int(), - 215●
unfiltered[i * 3 + 1].to_int(), - 216●
unfiltered[i * 3 + 2].to_int(), - 217
255, - 218
) - 219●
4 => { - 220
// gray + alpha - 221●
let v = unfiltered[i * 2].to_int() - 222●
let a = unfiltered[i * 2 + 1].to_int() - 223
(v, v, v, a) - 224
} - 225
_ => - 226
// RGBA - 227●
( - 228●
unfiltered[i * 4].to_int(), - 229●
unfiltered[i * 4 + 1].to_int(), - 230●
unfiltered[i * 4 + 2].to_int(), - 231●
unfiltered[i * 4 + 3].to_int(), - 232
) - 233
} - 234●
let (or, og, ob, oa) = match out_ch { - 235●
1 => { - 236
let gray = (r * 299 + g * 587 + b * 114) / 1000 - 237
(gray, gray, gray, 255) - 238
} - 239●
2 => { - 240
let gray = (r * 299 + g * 587 + b * 114) / 1000 - 241
(gray, gray, gray, a) - 242
} - 243●
3 => (r, g, b, 255) - 244●
_ => (r, g, b, a) - 245
} - 246●
match out_ch { - 247●
1 => out_data[i] = or.to_byte() - 248●
2 => { - 249●
out_data[i * 2] = or.to_byte() - 250●
out_data[i * 2 + 1] = oa.to_byte() - 251
} - 252●
3 => { - 253●
out_data[i * 3] = or.to_byte() - 254●
out_data[i * 3 + 1] = og.to_byte() - 255●
out_data[i * 3 + 2] = ob.to_byte() - 256
} - 257●
_ => { - 258●
out_data[i * 4] = or.to_byte() - 259●
out_data[i * 4 + 1] = og.to_byte() - 260●
out_data[i * 4 + 2] = ob.to_byte() - 261●
out_data[i * 4 + 3] = oa.to_byte() - 262
} - 263
} - 264
} - 265
@types.Image::{ - 266
width, - 267
height, - 268
channels: out_ch, - 269●
data: Bytes::from_array(out_data), - 270
} - 271
}