reorganize lib, use it in the funny zone

This commit is contained in:
Breval Ferrari 2024-07-14 07:57:40 +02:00
parent 825bcfa5a1
commit a0d82499c0
No known key found for this signature in database
GPG key ID: CEAB625B75A836B2
6 changed files with 106 additions and 103 deletions

View file

@ -1,88 +1 @@
use std::{borrow::Cow, ops::Deref};
use derive_new::new;
use image::{DynamicImage, RgbImage};
type Bytes<'a> = Cow<'a, [u8]>;
pub trait Metadata {}
pub struct RawData<'a, M: Metadata> {
pub(crate) data: Bytes<'a>,
pub(crate) metadata: M,
}
impl<'a, M> RawData<'a, M>
where
M: Metadata,
{
fn metadata(&self) -> &M {
&self.metadata
}
}
impl<'a, M> Deref for RawData<'a, M>
where
M: Metadata,
{
type Target = Bytes<'a>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<'a, M> From<RawData<'a, M>> for Vec<u8>
where
M: Metadata,
{
fn from(value: RawData<'a, M>) -> Self {
value.data.into_owned()
}
}
impl<'a, M> Clone for RawData<'a, M>
where
M: Metadata + Clone,
{
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
metadata: self.metadata().clone(),
}
}
}
#[derive(new)]
pub struct ImageMetadata {
width: u32,
height: u32,
}
impl Metadata for ImageMetadata {}
pub type RawImage<'a> = RawData<'a, ImageMetadata>;
impl From<DynamicImage> for RawImage<'_> {
fn from(value: DynamicImage) -> Self {
value.to_rgb8().into()
}
}
impl From<RgbImage> for RawImage<'_> {
fn from(value: RgbImage) -> Self {
let metadata = ImageMetadata::new(value.width(), value.height());
Self {
data: Cow::Owned(value.into_raw()),
metadata,
}
}
}
impl<'a> From<RawImage<'a>> for RgbImage {
fn from(value: RawImage<'a>) -> Self {
RgbImage::from_raw(
value.metadata.width,
value.metadata.height,
value.data.to_vec(),
)
.unwrap()
}
}
pub mod rawdata;