simplify lib towards simple wrapper
This commit is contained in:
parent
a0d82499c0
commit
c56b0b6227
3 changed files with 45 additions and 94 deletions
|
@ -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::<RgbImage>::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::<RgbImage>::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<u8> = (1u8..7).into_iter().collect();
|
||||
// assert_eq!(
|
||||
|
|
|
@ -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