use image;
#[derive(Clone)]
pub struct Image {
data: Box<image::RgbaImage>,
pub width: f32,
pub height: f32,
}
impl Image {
pub fn new(path: &str) -> Result<Image, String> {
let data = Self::data(path)?;
let width = data.width() as f32;
let height = data.height() as f32;
Ok(Image{
data: Box::new(data),
width, height
})
}
pub fn from(image: image::RgbaImage) -> Self {
let width = image.width() as f32;
let height = image.height() as f32;
Image {
data: Box::new(image),
width, height,
}
}
pub fn to_rgba_image(&self) -> image::RgbaImage {
let container = self.data.as_raw();
let mut buffer = image::ImageBuffer::new(self.data.width(), self.data.height());
buffer.clone_from_slice(container);
buffer
}
pub fn data(path: &str) -> Result<image::RgbaImage, String> {
if let Ok(data) = image::open(path) {
let flipped = data.flipv();
return Ok(flipped.to_rgba8())
}
Err(format!("could not open image '{}'", path))
}
pub fn crop(&self, x: u32, y: u32, width: u32, height: u32) -> Image {
let mut data = self.to_rgba_image();
let img = image::imageops::crop(&mut data, x, y, width, height);
Image{
data: Box::new(img.to_image()),
width: width as f32, height: height as f32,
}
}
}