use mln_style::bitmap::{PremultipliedImage, UnassociatedImage};
use mln_style::error::Error;
use mln_style::size::Size;
use crate::image::image::expand_to_rgba;
use crate::image::premultiply::premultiply;
pub fn decode_png(data: &[u8]) -> Result<PremultipliedImage, Error> {
const SIGNATURE: [u8; 8] = [0x89, b'P', b'N', b'G', b'\r', b'\n', 0x1a, b'\n'];
if !data.starts_with(&SIGNATURE) {
return Err(Error::invalid_argument("invalid PNG: no PNG signature"));
}
let invalid = |e: png::DecodingError| Error::invalid_argument(format!("invalid PNG: {e}"));
let mut decoder = png::Decoder::new(std::io::Cursor::new(data));
decoder.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16);
let mut reader = decoder.read_info().map_err(invalid)?;
let (width, height) = {
let info = reader.info();
(info.width, info.height)
};
let bytes = (width as usize)
.checked_mul(height as usize)
.and_then(|n| n.checked_mul(4))
.ok_or_else(|| Error::invalid_argument("invalid PNG: image too large"))?;
let mut data = vec![0u8; bytes];
let output = reader.next_frame(&mut data).map_err(invalid)?;
debug_assert_eq!(output.bit_depth, png::BitDepth::Eight);
debug_assert_eq!(output.line_size, width as usize * output.color_type.samples());
match output.color_type {
png::ColorType::Grayscale => expand_to_rgba(&mut data, |[g]| [g, g, g, 0xff]),
png::ColorType::GrayscaleAlpha => expand_to_rgba(&mut data, |[g, a]| [g, g, g, a]),
png::ColorType::Rgb => expand_to_rgba(&mut data, |[r, g, b]| [r, g, b, 0xff]),
png::ColorType::Rgba => {}
png::ColorType::Indexed => {
return Err(Error::invalid_argument("invalid PNG: unexpanded palette"));
}
}
Ok(premultiply(UnassociatedImage::from_vec(Size::new(width, height), data)))
}
#[cfg(test)]
mod tests {
use super::*;
fn encode(color: png::ColorType, depth: png::BitDepth, pixels: &[u8]) -> Vec<u8> {
let mut data = Vec::new();
let mut encoder = png::Encoder::new(&mut data, 2, 1);
encoder.set_color(color);
encoder.set_depth(depth);
if color == png::ColorType::Indexed {
encoder.set_palette(&[9, 8, 7, 6, 5, 4][..]);
encoder.set_trns(&[255, 0][..]);
}
encoder.write_header().unwrap().write_image_data(pixels).unwrap();
data
}
#[test]
fn every_color_type_decodes_to_premultiplied_rgba() {
use png::BitDepth::{Eight, Sixteen};
use png::ColorType::{Grayscale, GrayscaleAlpha, Indexed, Rgb, Rgba};
let cases: [(_, _, &[u8], [u8; 8]); 6] = [
(Grayscale, Eight, &[10, 200], [10, 10, 10, 255, 200, 200, 200, 255]),
(GrayscaleAlpha, Eight, &[10, 255, 200, 128], [10, 10, 10, 255, 100, 100, 100, 128]),
(Rgb, Eight, &[1, 2, 3, 4, 5, 6], [1, 2, 3, 255, 4, 5, 6, 255]),
(Rgb, Sixteen, &[1, 9, 2, 9, 3, 9, 4, 9, 5, 9, 6, 9], [1, 2, 3, 255, 4, 5, 6, 255]),
(Rgba, Eight, &[1, 2, 3, 255, 200, 0, 0, 128], [1, 2, 3, 255, 100, 0, 0, 128]),
(Indexed, Eight, &[0, 1], [9, 8, 7, 255, 0, 0, 0, 0]),
];
for (color, depth, pixels, expected) in cases {
let image = decode_png(&encode(color, depth, pixels)).unwrap();
assert_eq!(image.size, Size::new(2, 1), "{color:?} {depth:?}");
assert_eq!(image.data, expected, "{color:?} {depth:?}");
}
}
}