diff --git a/bent-funny-zone/src/main.rs b/bent-funny-zone/src/main.rs index 8681dac..563303f 100644 --- a/bent-funny-zone/src/main.rs +++ b/bent-funny-zone/src/main.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use anyhow::{Context, Result}; -use bingus::rawdata::rgbimage::RawImage; +use bingus::rawdata::RawData; use dasp_sample::{Sample, U24}; use derive_new::new; use dirs::{download_dir, home_dir, picture_dir}; @@ -67,34 +67,39 @@ impl Files { fn main() -> Result<()> { let dp = DataPath::new(Files::prompt()?); - let img = RawImage::from( + let img = RawData::::new( ImageReader::open(dp.files.input)? .decode() - .context("can't use this picture")?, + .context("can't use this picture")? + .into_rgb8(), ); - let processed = RawImage::new( - *img.metadata(), - img.par_chunks_exact(3) - .progress_count((img.metadata().width() * img.metadata().height()).into()) - .with_style(ProgressStyle::with_template( - "[{eta}] {bar:40.green/red} {pos}/{len} pixels", - )?) - .map(|px: &[u8]| (px[2] as i32) | ((px[1] as i32) << 8) | ((px[0] as i32) << 16)) - .map(U24::new_unchecked) - .map(|x| uparc(x.to_sample())) - .flat_map(|sample: f32| { - let rgb: U24 = sample.to_sample(); - let rgo = ((rgb) >> 8.into()) << 8.into(); - let roo = ((rgo) >> 16.into()) << 16.into(); - [ - ((roo) >> 16.into()).inner() as u8, - ((rgo - roo) >> 8.into()).inner() as u8, - (rgb - rgo).inner() as u8, - ] - }) - .collect(), + let processed = RawData::::new( + RgbImage::from_raw( + img.width(), + img.height(), + img.par_chunks_exact(3) + .progress_count((img.width() * img.height()).into()) + .with_style(ProgressStyle::with_template( + "[{eta}] {bar:40.green/red} {pos}/{len} pixels", + )?) + .map(|px: &[u8]| (px[2] as i32) | ((px[1] as i32) << 8) | ((px[0] as i32) << 16)) + .map(U24::new_unchecked) + .map(|x| uparc(x.to_sample())) + .flat_map(|sample: f32| { + let rgb: U24 = sample.to_sample(); + let rgo = ((rgb) >> 8.into()) << 8.into(); + let roo = ((rgo) >> 16.into()) << 16.into(); + [ + ((roo) >> 16.into()).inner() as u8, + ((rgo - roo) >> 8.into()).inner() as u8, + (rgb - rgo).inner() as u8, + ] + }) + .collect(), + ) + .unwrap(), ); - RgbImage::from(processed).save(dp.files.output)?; + processed.save(dp.files.output)?; // let bytes: Vec = (1u8..7).into_iter().collect(); // assert_eq!( diff --git a/bingus/src/rawdata.rs b/bingus/src/rawdata.rs index 98498d5..d0ac87b 100644 --- a/bingus/src/rawdata.rs +++ b/bingus/src/rawdata.rs @@ -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); -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> for Vec +impl From> for Vec where - M: Metadata, + D: Deref, { - fn from(value: RawData<'a, M>) -> Self { - value.data.into_owned() + fn from(value: RawData) -> Self { + value.to_owned() } } -impl<'a, M> Clone for RawData<'a, M> +impl Clone for RawData where - M: Metadata + Clone, + D: Clone + Deref, { 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 Deref for RawData where - M: Metadata, + D: Deref, { - type Target = Bytes<'a>; + type Target = D; fn deref(&self) -> &Self::Target { - &self.data + &self.0 } } diff --git a/bingus/src/rawdata/rgbimage.rs b/bingus/src/rawdata/rgbimage.rs deleted file mode 100644 index f5daf9c..0000000 --- a/bingus/src/rawdata/rgbimage.rs +++ /dev/null @@ -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 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() - } -}