use crate::codec::Dims;
use crate::error::{Error, Result};
use std::io::BufWriter;
use std::path::Path;
pub struct Image {
pub width: u32,
pub height: u32,
pub pixels: Vec<u8>, }
pub fn load(path: &Path) -> Result<Image> {
let img = image::open(path).map_err(|e| Error::ImageIo(e.to_string()))?;
let rgb = img.to_rgb8();
Ok(Image {
width: rgb.width(),
height: rgb.height(),
pixels: rgb.into_raw(),
})
}
pub fn save_png(path: &Path, dims: Dims, pixels: &[u8]) -> Result<()> {
let f = std::fs::File::create(path)?;
let bw = BufWriter::new(f);
let mut enc = png::Encoder::new(bw, dims.width, dims.height);
enc.set_color(png::ColorType::Rgb);
enc.set_depth(png::BitDepth::Eight);
let mut writer = enc
.write_header()
.map_err(|e| Error::ImageIo(format!("png header: {e}")))?;
writer
.write_image_data(pixels)
.map_err(|e| Error::ImageIo(format!("png data: {e}")))?;
Ok(())
}
pub fn encode_png(dims: Dims, pixels: &[u8]) -> Result<Vec<u8>> {
let mut buf = Vec::new();
{
let mut enc = png::Encoder::new(&mut buf, dims.width, dims.height);
enc.set_color(png::ColorType::Rgb);
enc.set_depth(png::BitDepth::Eight);
let mut writer = enc
.write_header()
.map_err(|e| Error::ImageIo(format!("png header: {e}")))?;
writer
.write_image_data(pixels)
.map_err(|e| Error::ImageIo(format!("png data: {e}")))?;
}
Ok(buf)
}