///|
/// # PNM 编码 PNM Encode
///
/// PNM 图像格式编码
/// PNM 图像格式编码 PNM image format encoding
// 纯 MoonBit PNM 格式编码器:PPM (P6) + PGM (P5)
// 移植自 src/format/pnm_encode.mbt,仅替换类型引用:
// @core.Image -> @types.Image
///|
/// 编码为 PPM (P6 binary RGB) 格式。
/// 灰度输入自动转为 RGB,RGBA 输入丢弃 alpha。
pub fn encode_ppm_pure(img : @types.Image) -> Bytes {
let w = img.width
let h = img.height
let ch = img.channels
let n = w * h
// 构建 RGB 像素数据
let pixel_data : Array[Byte] = Array::make(n * 3, b'\x00')
for i in 0..= 2 { img.data[i * ch + 1] } else { r }
let b = if ch >= 3 { img.data[i * ch + 2] } else { r }
pixel_data[i * 3] = r
pixel_data[i * 3 + 1] = g
pixel_data[i * 3 + 2] = b
}
// 头部: "P6\n{w} {h}\n255\n"
let header = "P6\n" + w.to_string() + " " + h.to_string() + "\n255\n"
let header_bytes = @encoding/utf8.encode(header)
let result : Array[Byte] = []
for i in 0.. Bytes {
let w = img.width
let h = img.height
let ch = img.channels
let n = w * h
// 构建灰度像素数据
let pixel_data : Array[Byte] = Array::make(n, b'\x00')
for i in 0.. Bytes {
if img.channels <= 1 {
encode_pgm_pure(img)
} else {
encode_ppm_pure(img)
}
}