reorganize lib, use it in the funny zone

This commit is contained in:
Ponj 2024-07-14 07:57:40 +02:00
parent 825bcfa5a1
commit a0d82499c0
Signed by: p6nj
GPG key ID: CEAB625B75A836B2
6 changed files with 106 additions and 103 deletions

View file

@ -21,3 +21,4 @@ rfd = "0.14.1"
derive-new = "0.6.0"
infer = "0.16.0"
indicatif = { version = "0.17.8", features = ["rayon"] }
getset = "0.1.2"

View file

@ -1,11 +1,12 @@
use std::path::PathBuf;
use anyhow::{Context, Result};
use bingus::rawdata::rgbimage::RawImage;
use dasp_sample::{Sample, U24};
use derive_new::new;
use dirs::{download_dir, home_dir, picture_dir};
use fundsp::math::uparc;
use image::{io::Reader as ImageReader, ImageBuffer, ImageFormat, Rgb};
use image::{io::Reader as ImageReader, ImageFormat, RgbImage};
use indicatif::{ParallelProgressIterator, ProgressStyle};
use rayon::{iter::ParallelIterator, slice::ParallelSlice};
use rfd::FileDialog;
@ -66,17 +67,15 @@ impl Files {
fn main() -> Result<()> {
let dp = DataPath::new(Files::prompt()?);
let img = ImageReader::open(dp.files.input)?
.decode()
.context("can't use this picture")?
.to_rgb8();
let (width, height) = (img.width(), img.height());
let processed: ImageBuffer<Rgb<u8>, Vec<u8>> = ImageBuffer::from_raw(
width,
height,
img.into_raw()
.par_chunks_exact(3)
.progress_count((width * height).into())
let img = RawImage::from(
ImageReader::open(dp.files.input)?
.decode()
.context("can't use this picture")?,
);
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",
)?)
@ -94,9 +93,8 @@ fn main() -> Result<()> {
]
})
.collect(),
)
.unwrap();
processed.save(dp.files.output)?;
);
RgbImage::from(processed).save(dp.files.output)?;
// let bytes: Vec<u8> = (1u8..7).into_iter().collect();
// assert_eq!(

View file

@ -12,3 +12,4 @@ keywords.workspace = true
[dependencies]
image = { workspace = true }
derive-new = { workspace = true }
getset = { workspace = true }

View file

@ -1,88 +1 @@
use std::{borrow::Cow, ops::Deref};
use derive_new::new;
use image::{DynamicImage, RgbImage};
type Bytes<'a> = Cow<'a, [u8]>;
pub trait Metadata {}
pub struct RawData<'a, M: Metadata> {
pub(crate) data: Bytes<'a>,
pub(crate) metadata: M,
}
impl<'a, M> RawData<'a, M>
where
M: Metadata,
{
fn metadata(&self) -> &M {
&self.metadata
}
}
impl<'a, M> Deref for RawData<'a, M>
where
M: Metadata,
{
type Target = Bytes<'a>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<'a, M> From<RawData<'a, M>> for Vec<u8>
where
M: Metadata,
{
fn from(value: RawData<'a, M>) -> Self {
value.data.into_owned()
}
}
impl<'a, M> Clone for RawData<'a, M>
where
M: Metadata + Clone,
{
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
metadata: self.metadata().clone(),
}
}
}
#[derive(new)]
pub struct ImageMetadata {
width: u32,
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()
}
}
pub mod rawdata;

48
bingus/src/rawdata.rs Normal file
View file

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

View file

@ -0,0 +1,42 @@
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()
}
}