simplify lib towards simple wrapper

This commit is contained in:
Breval Ferrari 2024-07-14 17:35:44 +02:00
parent a0d82499c0
commit c56b0b6227
No known key found for this signature in database
GPG key ID: CEAB625B75A836B2
3 changed files with 45 additions and 94 deletions

View file

@ -1,7 +1,7 @@
use std::path::PathBuf; use std::path::PathBuf;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use bingus::rawdata::rgbimage::RawImage; use bingus::rawdata::RawData;
use dasp_sample::{Sample, U24}; use dasp_sample::{Sample, U24};
use derive_new::new; use derive_new::new;
use dirs::{download_dir, home_dir, picture_dir}; use dirs::{download_dir, home_dir, picture_dir};
@ -67,15 +67,18 @@ impl Files {
fn main() -> Result<()> { fn main() -> Result<()> {
let dp = DataPath::new(Files::prompt()?); let dp = DataPath::new(Files::prompt()?);
let img = RawImage::from( let img = RawData::<RgbImage>::new(
ImageReader::open(dp.files.input)? ImageReader::open(dp.files.input)?
.decode() .decode()
.context("can't use this picture")?, .context("can't use this picture")?
.into_rgb8(),
); );
let processed = RawImage::new( let processed = RawData::<RgbImage>::new(
*img.metadata(), RgbImage::from_raw(
img.width(),
img.height(),
img.par_chunks_exact(3) img.par_chunks_exact(3)
.progress_count((img.metadata().width() * img.metadata().height()).into()) .progress_count((img.width() * img.height()).into())
.with_style(ProgressStyle::with_template( .with_style(ProgressStyle::with_template(
"[{eta}] {bar:40.green/red} {pos}/{len} pixels", "[{eta}] {bar:40.green/red} {pos}/{len} pixels",
)?) )?)
@ -93,8 +96,10 @@ fn main() -> Result<()> {
] ]
}) })
.collect(), .collect(),
)
.unwrap(),
); );
RgbImage::from(processed).save(dp.files.output)?; processed.save(dp.files.output)?;
// let bytes: Vec<u8> = (1u8..7).into_iter().collect(); // let bytes: Vec<u8> = (1u8..7).into_iter().collect();
// assert_eq!( // assert_eq!(

View file

@ -1,48 +1,36 @@
use std::{borrow::Cow, ops::Deref}; use std::ops::Deref;
use derive_new::new; 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 {} impl<D> From<RawData<D>> for Vec<u8>
#[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>
where where
M: Metadata, D: Deref<Target = Bytes>,
{ {
fn from(value: RawData<'a, M>) -> Self { fn from(value: RawData<D>) -> Self {
value.data.into_owned() value.to_owned()
} }
} }
impl<'a, M> Clone for RawData<'a, M> impl<D> Clone for RawData<D>
where where
M: Metadata + Clone, D: Clone + Deref<Target = Bytes>,
{ {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self(self.deref().clone())
data: self.data.clone(),
metadata: self.metadata().clone(),
}
} }
} }
impl<'a, M> Deref for RawData<'a, M> impl<D> Deref for RawData<D>
where where
M: Metadata, D: Deref<Target = Bytes>,
{ {
type Target = Bytes<'a>; type Target = D;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.data &self.0
} }
} }

View file

@ -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()
}
}