diff --git a/bingus/Cargo.toml b/bingus/Cargo.toml index 8ee8c05..00acf16 100644 --- a/bingus/Cargo.toml +++ b/bingus/Cargo.toml @@ -10,5 +10,5 @@ keywords.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -fundsp = { workspace = true } -num-traits = { workspace = true } +image = { workspace = true } +derive-new = { workspace = true } diff --git a/bingus/src/lib.rs b/bingus/src/lib.rs index ce40a72..2a54c04 100644 --- a/bingus/src/lib.rs +++ b/bingus/src/lib.rs @@ -1,5 +1,8 @@ use std::{borrow::Cow, ops::Deref}; +use derive_new::new; +use image::{DynamicImage, RgbImage}; + type Bytes<'a> = Cow<'a, [u8]>; pub trait Metadata {} @@ -48,3 +51,38 @@ where } } } + +#[derive(new)] +pub struct ImageMetadata { + width: u32, + height: u32, +} +impl Metadata for ImageMetadata {} +pub type RawImage<'a> = RawData<'a, ImageMetadata>; + +impl From for RawImage<'_> { + fn from(value: DynamicImage) -> Self { + value.to_rgb8().into() + } +} + +impl From 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> for RgbImage { + fn from(value: RawImage<'a>) -> Self { + RgbImage::from_raw( + value.metadata.width, + value.metadata.height, + value.data.to_vec(), + ) + .unwrap() + } +}