use crate::coord::Coord;
use crate::drawable::Dimensions;
use crate::drawable::Drawable;
use crate::pixelcolor::PixelColor;
use crate::transform::Transform;
use crate::unsignedcoord::{ToSigned, UnsignedCoord};
use core::marker::PhantomData;
pub trait ImageType {}
#[derive(Debug)]
pub struct Image<'a, C, T>
where
C: PixelColor,
T: ImageType,
{
pub(crate) width: u32,
pub(crate) height: u32,
pub(crate) imagedata: &'a [u8],
pub offset: Coord,
pixel_type: PhantomData<C>,
image_type: PhantomData<T>,
}
impl<'a, C, T> Image<'a, C, T>
where
C: PixelColor,
T: ImageType,
{
pub fn new(imagedata: &'a [u8], width: u32, height: u32) -> Self {
Self {
width,
height,
imagedata,
offset: Coord::new(0, 0),
pixel_type: PhantomData,
image_type: PhantomData,
}
}
}
impl<'a, C, T> Dimensions for Image<'a, C, T>
where
C: PixelColor,
T: ImageType,
{
fn top_left(&self) -> Coord {
self.offset
}
fn bottom_right(&self) -> Coord {
self.top_left() + self.size().to_signed()
}
fn size(&self) -> UnsignedCoord {
let height = self.height;
let width = self.width;
UnsignedCoord::new(width, height)
}
}
impl<'a, C, T> Drawable for Image<'a, C, T>
where
C: PixelColor,
T: ImageType,
{
}
impl<'a, C, T> Transform for Image<'a, C, T>
where
C: PixelColor,
T: ImageType,
{
fn translate(&self, by: Coord) -> Self {
Self {
offset: self.offset + by,
..*self.clone()
}
}
fn translate_mut(&mut self, by: Coord) -> &mut Self {
self.offset += by;
self
}
}
#[derive(Debug)]
pub struct ImageIterator<'a, C: 'a, T>
where
C: PixelColor,
T: ImageType,
{
pub(crate) x: u32,
pub(crate) y: u32,
pub(crate) im: &'a Image<'a, C, T>,
}
impl<'a, C, T> ImageIterator<'a, C, T>
where
C: PixelColor,
T: ImageType,
{
pub fn new(image: &'a Image<'a, C, T>) -> Self {
ImageIterator {
im: image,
x: 0,
y: 0,
}
}
}
pub trait ImageFile<'a>: Dimensions + Sized {
fn new(filedata: &'a [u8]) -> Result<Self, ()>;
fn width(&self) -> u32;
fn height(&self) -> u32;
}