simplify lib towards simple wrapper
This commit is contained in:
parent
a0d82499c0
commit
c56b0b6227
3 changed files with 45 additions and 94 deletions
|
@ -1,48 +1,36 @@
|
|||
use std::{borrow::Cow, ops::Deref};
|
||||
use std::ops::Deref;
|
||||
|
||||
use derive_new::new;
|
||||
use getset::Getters;
|
||||
|
||||
pub mod rgbimage;
|
||||
pub type Bytes = [u8];
|
||||
|
||||
type Bytes<'a> = Cow<'a, [u8]>;
|
||||
#[derive(new)]
|
||||
pub struct RawData<D: Deref<Target = Bytes>>(D);
|
||||
|
||||
pub trait Metadata {}
|
||||
|
||||
#[derive(new, Getters)]
|
||||
pub struct RawData<'a, M: Metadata> {
|
||||
#[getset(get = "pub")]
|
||||
pub(crate) metadata: M,
|
||||
pub(crate) data: Bytes<'a>,
|
||||
}
|
||||
|
||||
impl<'a, M> From<RawData<'a, M>> for Vec<u8>
|
||||
impl<D> From<RawData<D>> for Vec<u8>
|
||||
where
|
||||
M: Metadata,
|
||||
D: Deref<Target = Bytes>,
|
||||
{
|
||||
fn from(value: RawData<'a, M>) -> Self {
|
||||
value.data.into_owned()
|
||||
fn from(value: RawData<D>) -> Self {
|
||||
value.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, M> Clone for RawData<'a, M>
|
||||
impl<D> Clone for RawData<D>
|
||||
where
|
||||
M: Metadata + Clone,
|
||||
D: Clone + Deref<Target = Bytes>,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
data: self.data.clone(),
|
||||
metadata: self.metadata().clone(),
|
||||
}
|
||||
Self(self.deref().clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, M> Deref for RawData<'a, M>
|
||||
impl<D> Deref for RawData<D>
|
||||
where
|
||||
M: Metadata,
|
||||
D: Deref<Target = Bytes>,
|
||||
{
|
||||
type Target = Bytes<'a>;
|
||||
type Target = D;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.data
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,42 +0,0 @@
|
|||
use derive_new::new;
|
||||
use getset::Getters;
|
||||
use image::{DynamicImage, RgbImage};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(new, Getters, Clone, Copy)]
|
||||
pub struct ImageMetadata {
|
||||
#[getset(get = "pub")]
|
||||
width: u32,
|
||||
#[getset(get = "pub")]
|
||||
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()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue