image.codec\codec\png_decode_16.mbt
99% · 103 / 104 points · 217 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 16-bit 解码 PNG 16-bit Decode - 3
/// - 4
/// PNG 16-bit 高精度解码 - 5
/// PNG 16-bit 高精度解码 PNG 16-bit high-precision decoding - 6
// 纯 MoonBit PNG 16-bit 解码(全目标可用:native/wasm/js) - 7
// 支持 8-bit PNG 上采样为 16-bit,以及原生 16-bit PNG - 8
// 输出 Image16(little-endian uint16 per sample) - 9
- 10
///| - 11
/// PNG 16-bit 解码(纯 MoonBit,全目标可用)。 - 12
/// 支持 bit depth 8(上采样为 16-bit)和 16(原生)。 - 13
/// color type 0/2/3/4/6,不支持 interlace。 - 14
/// req_channels: 0/None=原色, 1=灰度, 2=灰度+alpha, 3=RGB, 4=RGBA - 15
pub fn decode_png_16_pure( - 16
data : Bytes, - 17
req_channels~ : Int32? = None, - 18
) -> @types.Image16 raise @types.LoadError { - 19●
match req_channels { - 20
Some(n) => - 21●
if n < 1 || n > 4 { - 22●
raise @types.LoadError::DecodeFailed("req_channels 必须为 1-4") - 23
} - 24●
None => () - 25
} - 26●
let len = data.length() - 27●
if len < 8 || !is_png_signature(data, 0) { - 28●
raise @types.LoadError::DecodeFailed("PNG signature 错误") - 29
} - 30
let mut pos = 8 - 31
let mut width = 0 - 32
let mut height = 0 - 33
let mut bit_depth = 0 - 34
let mut color_type = 0 - 35
let mut interlace = 0 - 36
let palette : Array[(Int32, Int32, Int32)] = [] - 37
let idat_data : Array[Byte] = [] - 38
let mut found_ihdr = false - 39
let mut found_iend = false - 40
while pos < len && !found_iend { - 41●
if pos + 8 > len { - 42●
raise @types.LoadError::DecodeFailed("PNG chunk 头部过短") - 43
} - 44●
let chunk_len = read_u32_be(data, pos) - 45●
let chunk_key = read_u32_be(data, pos + 4) - 46
pos += 8 - 47
// 溢出安全:以 `len - pos` 为界,避免 pos+chunk_len 在 Int32 上回绕 - 48
if chunk_len < 0 || chunk_len > len - pos { - 49●
raise @types.LoadError::DecodeFailed("PNG chunk 数据越界") - 50
} - 51●
match chunk_key { - 52●
0x49484452 => { - 53
if chunk_len < 13 { - 54●
raise @types.LoadError::DecodeFailed("PNG IHDR 过短") - 55
} - 56●
width = read_u32_be(data, pos) - 57●
height = read_u32_be(data, pos + 4) - 58●
bit_depth = data[pos + 8].to_int() - 59●
color_type = data[pos + 9].to_int() - 60●
interlace = data[pos + 12].to_int() - 61
found_ihdr = true - 62
} - 63●
0x504C5445 => { - 64
let num_colors = chunk_len / 3 - 65
for i in 0..<num_colors { - 66●
palette.push( - 67
( - 68●
data[pos + i * 3].to_int(), - 69●
data[pos + i * 3 + 1].to_int(), - 70●
data[pos + i * 3 + 2].to_int(), - 71
), - 72
) - 73
} - 74
} - 75
0x49444154 => - 76●
for i in 0..<chunk_len { - 77●
idat_data.push(data[pos + i]) - 78
} - 79●
0x49454E44 => found_iend = true - 80●
_ => () - 81
} - 82
pos += chunk_len + 4 - 83
} - 84
if !found_ihdr { - 85●
raise @types.LoadError::DecodeFailed("PNG 无 IHDR chunk") - 86
} - 87
if width <= 0 || height <= 0 { - 88●
raise @types.LoadError::DecodeFailed("PNG 尺寸无效") - 89
} - 90
if interlace != 0 { - 91●
raise @types.LoadError::DecodeFailed("PNG interlace 不支持") - 92
} - 93
if bit_depth != 8 && bit_depth != 16 { - 94●
raise @types.LoadError::DecodeFailed( - 95
"PNG 16-bit 解码仅支持 bit depth 8 或 16", - 96
) - 97
} - 98●
let ch = match png_channels(color_type) { - 99●
Some(c) => c - 100●
None => raise @types.LoadError::DecodeFailed("PNG 无效 color type") - 101
} - 102●
let raw = @util.zlib_inflate(Bytes::from_array(idat_data)) - 103
// bytes per pixel for filtering - 104
let bpp = ch * (bit_depth / 8) - 105
let stride = width * bpp - 106●
let unfiltered : Array[Byte] = Array::make(height * stride, b'\x00') - 107
let mut raw_pos = 0 - 108
for y in 0..<height { - 109●
if raw_pos >= raw.length() { - 110○
raise @types.LoadError::DecodeFailed("PNG 解压数据过短") - 111
} - 112●
let filter_type = raw[raw_pos].to_int() - 113
raw_pos += 1 - 114
for x in 0..<stride { - 115●
if raw_pos >= raw.length() { - 116●
raise @types.LoadError::DecodeFailed("PNG 解压数据过短") - 117
} - 118●
let raw_byte = raw[raw_pos].to_int() - 119
raw_pos += 1 - 120
let a = if x >= bpp { - 121●
unfiltered[y * stride + x - bpp].to_int() - 122
} else { - 123●
0 - 124
} - 125●
let b = if y > 0 { unfiltered[(y - 1) * stride + x].to_int() } else { 0 } - 126
let c = if x >= bpp && y > 0 { - 127●
unfiltered[(y - 1) * stride + x - bpp].to_int() - 128
} else { - 129●
0 - 130
} - 131●
let val = match filter_type { - 132●
0 => raw_byte - 133●
1 => (raw_byte + a) & 0xFF - 134●
2 => (raw_byte + b) & 0xFF - 135●
3 => (raw_byte + (a + b) / 2) & 0xFF - 136●
4 => (raw_byte + paeth_predict(a, b, c)) & 0xFF - 137●
_ => raise @types.LoadError::DecodeFailed("PNG 无效 filter type") - 138
} - 139●
unfiltered[y * stride + x] = val.to_byte() - 140
} - 141
} - 142
// 转换为 16-bit samples (little-endian) - 143
let pixel_count = width * height - 144●
let out_ch = match req_channels { - 145●
Some(n) => n - 146●
None => ch - 147
} - 148
// 先提取原生 16-bit samples - 149●
let samples16 : Array[Int32] = Array::make(pixel_count * ch, 0) - 150
if bit_depth == 16 { - 151
// big-endian → native int - 152●
for i in 0..<(pixel_count * ch) { - 153●
let hi = unfiltered[i * 2].to_int() - 154●
let lo = unfiltered[i * 2 + 1].to_int() - 155
samples16[i] = hi * 256 + lo - 156
} - 157
} else { - 158
// 8-bit → upscale: v * 257 - 159●
for i in 0..<(pixel_count * ch) { - 160●
samples16[i] = unfiltered[i].to_int() * 257 - 161
} - 162
} - 163
// 通道转换 - 164●
let out_data : Array[Byte] = Array::make(pixel_count * out_ch * 2, b'\x00') - 165
for i in 0..<pixel_count { - 166●
let (r16, g16, b16, a16) = if ch == 1 { - 167●
let v = samples16[i] - 168
(v, v, v, 65535) - 169●
} else if ch == 2 { - 170●
let v = samples16[i * 2] - 171
let a = samples16[i * 2 + 1] - 172
(v, v, v, a) - 173●
} else if ch == 3 { - 174●
(samples16[i * 3], samples16[i * 3 + 1], samples16[i * 3 + 2], 65535) - 175
} else { - 176●
( - 177
samples16[i * 4], - 178
samples16[i * 4 + 1], - 179
samples16[i * 4 + 2], - 180
samples16[i * 4 + 3], - 181
) - 182
} - 183●
let (or16, og16, ob16, oa16) = match out_ch { - 184●
1 => { - 185
let gray = (r16 * 299 + g16 * 587 + b16 * 114) / 1000 - 186
(gray, gray, gray, 65535) - 187
} - 188●
2 => { - 189
let gray = (r16 * 299 + g16 * 587 + b16 * 114) / 1000 - 190
(gray, gray, gray, a16) - 191
} - 192●
3 => (r16, g16, b16, 65535) - 193●
_ => (r16, g16, b16, a16) - 194
} - 195
// little-endian uint16 - 196●
out_data[i * out_ch * 2] = (or16 & 0xFF).to_byte() - 197●
out_data[i * out_ch * 2 + 1] = ((or16 >> 8) & 0xFF).to_byte() - 198
if out_ch >= 2 { - 199●
out_data[i * out_ch * 2 + 2] = (og16 & 0xFF).to_byte() - 200●
out_data[i * out_ch * 2 + 3] = ((og16 >> 8) & 0xFF).to_byte() - 201
} - 202
if out_ch >= 3 { - 203●
out_data[i * out_ch * 2 + 4] = (ob16 & 0xFF).to_byte() - 204●
out_data[i * out_ch * 2 + 5] = ((ob16 >> 8) & 0xFF).to_byte() - 205
} - 206
if out_ch >= 4 { - 207●
out_data[i * out_ch * 2 + 6] = (oa16 & 0xFF).to_byte() - 208●
out_data[i * out_ch * 2 + 7] = ((oa16 >> 8) & 0xFF).to_byte() - 209
} - 210
} - 211
@types.Image16::{ - 212
width, - 213
height, - 214
channels: out_ch, - 215●
data: Bytes::from_array(out_data), - 216
} - 217
}