use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pixmap {
width: u32,
height: u32,
pixels: Arc<[u8]>,
}
impl Pixmap {
#[must_use]
pub fn from_rgba8(width: u32, height: u32, pixels: impl Into<Arc<[u8]>>) -> Option<Self> {
let pixels = pixels.into();
let expect = (width as usize)
.checked_mul(height as usize)?
.checked_mul(4)?;
if pixels.len() != expect {
return None;
}
Some(Self {
width,
height,
pixels,
})
}
#[must_use]
pub fn width(&self) -> u32 {
self.width
}
#[must_use]
pub fn height(&self) -> u32 {
self.height
}
#[must_use]
pub fn pixels(&self) -> &[u8] {
&self.pixels
}
#[must_use]
pub fn to_png(&self) -> Vec<u8> {
let mut out = Vec::with_capacity(self.pixels.len() + 64);
{
let mut enc = png::Encoder::new(&mut out, self.width, self.height);
enc.set_color(png::ColorType::Rgba);
enc.set_depth(png::BitDepth::Eight);
let mut writer = enc
.write_header()
.expect("[lievisual] PNG encoding: write header failed");
writer
.write_image_data(&self.pixels)
.expect("[lievisual] PNG encoding: write image data failed");
}
out
}
#[must_use]
pub fn decode_png(bytes: &[u8]) -> Option<Self> {
let mut decoder = png::Decoder::new(std::io::Cursor::new(bytes));
decoder.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16);
let mut reader = decoder.read_info().ok()?;
let mut buf = vec![0u8; reader.output_buffer_size()?];
let info = reader.next_frame(&mut buf).ok()?;
buf.truncate(info.buffer_size());
let (color, depth) = reader.output_color_type();
let (w, h) = (info.width, info.height);
let rgba = normalize_to_rgba8(buf, color, depth, w, h)?;
Self::from_rgba8(w, h, rgba)
}
}
fn normalize_to_rgba8(
buf: Vec<u8>,
color: png::ColorType,
depth: png::BitDepth,
width: u32,
height: u32,
) -> Option<Vec<u8>> {
let n = (width as usize) * (height as usize);
let bpp = match color {
png::ColorType::Grayscale => 1usize,
png::ColorType::Rgb => 3,
png::ColorType::Indexed => 1,
png::ColorType::GrayscaleAlpha => 2,
png::ColorType::Rgba => 4,
};
let chan = if depth == png::BitDepth::Eight { 1 } else { 2 };
if buf.len() < n * bpp * chan {
return None;
}
let mut out = Vec::with_capacity(n * 4);
for i in 0..n {
let base = i * bpp * chan;
let byte = |idx: usize| buf[base + idx * chan];
let (r, g, b, a) = match color {
png::ColorType::Grayscale => {
let v = byte(0);
(v, v, v, 255)
}
png::ColorType::GrayscaleAlpha => {
let v = byte(0);
(v, v, v, byte(1))
}
png::ColorType::Rgb => (byte(0), byte(1), byte(2), 255),
png::ColorType::Rgba => (byte(0), byte(1), byte(2), byte(3)),
png::ColorType::Indexed => {
let v = byte(0);
(v, v, v, 255)
}
};
out.extend_from_slice(&[r, g, b, a]);
}
Some(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_rgba8_validates_length() {
assert!(Pixmap::from_rgba8(2, 2, vec![0u8; 16]).is_some());
assert!(Pixmap::from_rgba8(2, 2, vec![0u8; 15]).is_none());
assert!(Pixmap::from_rgba8(0, 0, vec![] as Vec<u8>).is_some());
}
#[test]
fn png_roundtrip() {
let pixels: Vec<u8> = (0..4u32 * 3 * 3).map(|i| (i % 251) as u8).collect();
let pm = Pixmap::from_rgba8(3, 3, pixels).unwrap();
let png = pm.to_png();
let back = Pixmap::decode_png(&png).unwrap();
assert_eq!(back.width(), 3);
assert_eq!(back.height(), 3);
assert_eq!(back.pixels(), pm.pixels());
}
#[test]
fn decode_png_rejects_garbage() {
assert!(Pixmap::decode_png(b"not a png").is_none());
}
#[test]
fn decode_png_expands_palette_and_trns() {
let mut out = Vec::new();
{
let mut enc = png::Encoder::new(&mut out, 2, 2);
enc.set_color(png::ColorType::Indexed);
enc.set_depth(png::BitDepth::Eight);
enc.set_palette(vec![0xff, 0x00, 0x00, 0x00, 0xff, 0x00]);
enc.set_trns(vec![0xff, 0x80]);
let mut writer = enc.write_header().expect("编码头失败");
writer
.write_image_data(&[0u8, 1, 1, 0])
.expect("写入像素失败");
}
let pm = Pixmap::decode_png(&out).expect("palette PNG 应能解码");
let px = pm.pixels();
assert_eq!(&px[0..4], &[0xff, 0x00, 0x00, 0xff], "索引0 应为不透明红");
assert_eq!(&px[4..8], &[0x00, 0xff, 0x00, 0x80], "索引1 应为半透明绿");
assert_eq!(&px[8..12], &[0x00, 0xff, 0x00, 0x80]);
assert_eq!(&px[12..16], &[0xff, 0x00, 0x00, 0xff]);
}
}