image.codec\codec\png_encode.mbt
99.3% · 133 / 134 points · 198 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 Encode - 3
/// - 4
/// PNG 图像格式编码 - 5
/// PNG 图像格式编码 PNG image format encoding - 6
// 纯 MoonBit PNG 编码(全目标可用:native/wasm/js) - 7
// 使用 filter type 0 (None) + zlib stored blocks(无压缩但有效) - 8
- 9
///| - 10
/// PNG 行过滤(RFC 2083 §9):0 None / 1 Sub / 2 Up / 3 Average / 4 Paeth。 - 11
fn png_filter_row( - 12
raw : Array[Byte], - 13
prev : Array[Byte], - 14
bpp : Int32, - 15
ft : Int32, - 16
) -> Array[Byte] { - 17●
let n = raw.length() - 18●
let out : Array[Byte] = Array::make(n, b'\x00') - 19
for i in 0..<n { - 20●
let cur = raw[i].to_int() - 21●
let left = if i >= bpp { raw[i - bpp].to_int() } else { 0 } - 22●
let up = prev[i].to_int() - 23●
let ul = if i >= bpp { prev[i - bpp].to_int() } else { 0 } - 24●
let pred = match ft { - 25●
1 => left - 26●
2 => up - 27●
3 => (left + up) / 2 - 28●
4 => { - 29
// Paeth predictor - 30
let p = left + up - ul - 31●
let pa = if p > left { p - left } else { left - p } - 32●
let pb = if p > up { p - up } else { up - p } - 33●
let pc = if p > ul { p - ul } else { ul - p } - 34
if pa <= pb && pa <= pc { - 35●
left - 36●
} else if pb <= pc { - 37●
up - 38
} else { - 39●
ul - 40
} - 41
} - 42●
_ => 0 - 43
} - 44●
out[i] = ((cur - pred) & 0xFF).to_byte() - 45
} - 46
out - 47
} - 48
- 49
///| - 50
/// 过滤结果的启发式代价:按「有符号字节」取绝对值求和(PNG 规范推荐)。 - 51
fn png_filter_score(row : Array[Byte]) -> Int32 { - 52●
let mut s = 0 - 53●
for i in 0..<row.length() { - 54●
let v = row[i].to_int() - 55●
s += if v < 128 { v } else { 256 - v } - 56
} - 57
s - 58
} - 59
- 60
///| - 61
/// 写入 PNG chunk - 62
fn write_chunk(out : Array[Byte], chunk_type : String, data : Bytes) -> Unit { - 63●
let len = data.length() - 64
// length (big-endian) - 65●
out.push(((len >> 24) & 0xFF).to_byte()) - 66●
out.push(((len >> 16) & 0xFF).to_byte()) - 67●
out.push(((len >> 8) & 0xFF).to_byte()) - 68●
out.push((len & 0xFF).to_byte()) - 69
// type - 70●
let type_bytes = @encoding/utf8.encode(chunk_type) - 71
for i in 0..<4 { - 72●
out.push(type_bytes[i]) - 73
} - 74
// data - 75
for i in 0..<len { - 76●
out.push(data[i]) - 77
} - 78
// CRC (over type + data) - 79
let crc_data : Array[Byte] = [] - 80
for i in 0..<4 { - 81●
crc_data.push(type_bytes[i]) - 82
} - 83
for i in 0..<len { - 84●
crc_data.push(data[i]) - 85
} - 86●
let crc = @util.crc32(Bytes::from_array(crc_data)) - 87●
out.push(((crc >> 24) & 0xFF).to_byte()) - 88●
out.push(((crc >> 16) & 0xFF).to_byte()) - 89●
out.push(((crc >> 8) & 0xFF).to_byte()) - 90●
out.push((crc & 0xFF).to_byte()) - 91
} - 92
- 93
///| - 94
/// PNG 编码(纯 MoonBit,全目标可用)。 - 95
/// 输入 Image(RGB),返回 PNG 格式字节流。 - 96
/// 使用 filter type 0 (None) + zlib stored blocks(无压缩)。 - 97
pub fn encode_png_pure(img : @types.Image) -> Bytes { - 98●
let width = img.width - 99
let height = img.height - 100
let ch = img.channels - 101
// 确定输出通道数 - 102
let out_ch = if ch == 4 { - 103●
4 - 104●
} else if ch == 2 { - 105●
2 - 106●
} else if ch == 1 { - 107●
1 - 108
} else { - 109●
3 - 110
} - 111
let color_type = if out_ch == 1 { - 112●
0 - 113●
} else if out_ch == 2 { - 114●
4 - 115●
} else if out_ch == 4 { - 116●
6 - 117
} else { - 118●
2 - 119
} - 120
let out : Array[Byte] = [] - 121
// PNG signature - 122●
out.push(b'\x89') - 123●
out.push(b'\x50') - 124●
out.push(b'\x4E') - 125●
out.push(b'\x47') - 126●
out.push(b'\x0D') - 127●
out.push(b'\x0A') - 128●
out.push(b'\x1A') - 129●
out.push(b'\x0A') - 130
// IHDR chunk - 131
let ihdr : Array[Byte] = [] - 132
// width (big-endian) - 133●
ihdr.push(((width >> 24) & 0xFF).to_byte()) - 134●
ihdr.push(((width >> 16) & 0xFF).to_byte()) - 135●
ihdr.push(((width >> 8) & 0xFF).to_byte()) - 136●
ihdr.push((width & 0xFF).to_byte()) - 137
// height (big-endian) - 138●
ihdr.push(((height >> 24) & 0xFF).to_byte()) - 139●
ihdr.push(((height >> 16) & 0xFF).to_byte()) - 140●
ihdr.push(((height >> 8) & 0xFF).to_byte()) - 141●
ihdr.push((height & 0xFF).to_byte()) - 142●
ihdr.push(b'\x08') // bit depth = 8 - 143●
ihdr.push(color_type.to_byte()) - 144●
ihdr.push(b'\x00') // compression = 0 - 145●
ihdr.push(b'\x00') // filter = 0 - 146●
ihdr.push(b'\x00') // interlace = 0 - 147●
write_chunk(out, "IHDR", Bytes::from_array(ihdr)) - 148
// 构建过滤后的图像数据:逐行在 5 种 filter 里选绝对值和最小的那个 - 149
let bpp = out_ch - 150
let row_len = width * out_ch - 151
let filtered : Array[Byte] = [] - 152●
let prev_row : Array[Byte] = Array::make(row_len, b'\x00') - 153
for y in 0..<height { - 154●
let raw : Array[Byte] = Array::make(row_len, b'\x00') - 155
for x in 0..<width { - 156●
let src_off = (y * width + x) * ch - 157
if ch == out_ch { - 158●
for c in 0..<ch { - 159●
raw[x * out_ch + c] = img.data[src_off + c] - 160
} - 161
} else { - 162●
for c in 0..<out_ch { - 163●
raw[x * out_ch + c] = if c < ch { - 164●
img.data[src_off + c] - 165
} else { - 166○
b'\xFF' - 167
} - 168
} - 169
} - 170
} - 171
let mut best_type = 0 - 172
let mut best_score = 0x7FFFFFFF - 173
let mut best_row : Array[Byte] = raw - 174
for ft in 0..<5 { - 175●
let cand = png_filter_row(raw, prev_row, bpp, ft) - 176●
let score = png_filter_score(cand) - 177
if score < best_score { - 178●
best_score = score - 179
best_type = ft - 180
best_row = cand - 181
} - 182
} - 183●
filtered.push(best_type.to_byte()) - 184
for i in 0..<row_len { - 185●
filtered.push(best_row[i]) - 186
} - 187
for i in 0..<row_len { - 188●
prev_row[i] = raw[i] - 189
} - 190
} - 191
// zlib deflate(fixed Huffman + LZ77,RFC 1950/1951 合法流) - 192●
let compressed = zlib_compress(Bytes::from_array(filtered)) - 193
// IDAT chunk - 194●
write_chunk(out, "IDAT", compressed) - 195
// IEND chunk - 196●
write_chunk(out, "IEND", Bytes::from_array([])) - 197●
Bytes::from_array(out) - 198
}