use crate::image_data::ImageData;
use crate::{Error, Result};
#[derive(Default)]
pub struct Image {
image: ImageData,
}
impl Image {
pub(crate) fn as_mut_image(&mut self) -> &mut ImageData {
&mut self.image
}
pub fn from_gray(data: &[u8], width: u32, height: u32) -> Result<Self> {
if (data.len() as u64) != (width as u64) * (height as u64) {
return Err(Error::Invalid);
}
let mut image = Self::default();
image.image.width = width;
image.image.height = height;
image.image.data.extend_from_slice(data);
Ok(image)
}
pub fn width(&self) -> u32 {
self.image.width
}
pub fn height(&self) -> u32 {
self.image.height
}
pub fn data(&self) -> &[u8] {
&self.image.data
}
pub fn crop(&self, x: u32, y: u32, width: u32, height: u32) -> Option<Self> {
self.image
.crop(x, y, width, height)
.map(|image| Image { image })
}
pub fn upscale(&self, scale: u32) -> Option<Self> {
self.image.upscale(scale).map(|image| Image { image })
}
}