finally a perfect ToSample trait!

This commit is contained in:
Ponj 2024-07-07 08:19:23 +02:00
parent 41bbd3d371
commit 0194f60ca3
Signed by: p6nj
GPG key ID: CEAB625B75A836B2
6 changed files with 38 additions and 127 deletions

View file

@ -9,4 +9,8 @@ package.repository = "https://codeberg.org/p6nj/bent"
package.keywords = ["databending", "data-bending", "bending", "bend", "art"]
[workspace.dependencies]
fundsp = "0.18.2"
image = "0.24.9"
anyhow = "1.0.86"
dasp_sample = "0.11.0"
bingus = { version = "0.1.0", path = "bingus" }

View file

@ -11,7 +11,8 @@ keywords.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.86"
bmp = "0.5.0"
dasp_sample = "0.11.0"
fundsp = { version = "0.18.1", default-features = false, features = ["std"] }
image = { workspace = true }
fundsp = { workspace = true }
anyhow = { workspace = true }
dasp_sample = { workspace = true }
bingus = { workspace = true }

View file

@ -1,75 +1,7 @@
use std::fs::File;
use anyhow::Result;
use bmp::{open, Pixel};
use dasp_sample::{FromSample, Sample, ToSample, U24};
use bingus::BytesToSample;
fn main() -> Result<()> {
let mut bmp = open("bmp/bigsample.bmp")?;
for y in 0..bmp.get_height() {
for x in 0..bmp.get_width() {
if x > 100 && x < 300 {
bmp.set_pixel(
x,
y,
sample_to_rgb(rgb_to_sample::<f64>(bmp.get_pixel(x, y))),
);
}
}
}
bmp.to_writer(&mut File::create("bmp/out.bmp")?)?;
dbg!([1u8, 2, 3].to_sample::<f64>());
Ok(())
}
fn rgb_to_sample<T: Sample + FromSample<U24>>(pix: Pixel) -> T {
(((U24::from(pix.r)) << 16.into()) | (U24::from(pix.g) << 8.into()) | U24::from(pix.b))
.to_sample()
}
fn sample_to_rgb<T: Sample + ToSample<U24>>(sample: T) -> Pixel {
let rgb: U24 = sample.to_sample();
let rgo = ((rgb) >> 8.into()) << 8.into();
let roo = ((rgo) >> 16.into()) << 16.into();
Pixel::new(
((roo) >> 16.into()).inner() as u8,
((rgo - roo) >> 8.into()).inner() as u8,
(rgb - rgo).inner() as u8,
)
}
#[cfg(test)]
mod tests {
use bmp::Pixel;
use dasp_sample::{Sample, I24, I48, U24, U48};
use super::{rgb_to_sample, sample_to_rgb};
#[test]
fn sample_convert_consistency() {
let original = U24::from(42);
assert_eq!(original.clone(), original.to_sample::<f64>().to_sample())
}
#[test]
fn rgb2s2rgb_type_consistency() {
let pix = Pixel::new(45, 18, 143);
assert_eq!(
sample_to_rgb(rgb_to_sample::<f32>(pix)),
sample_to_rgb(rgb_to_sample::<f64>(pix))
);
assert_eq!(
sample_to_rgb(rgb_to_sample::<I24>(pix)),
sample_to_rgb(rgb_to_sample::<I48>(pix))
);
assert_eq!(
sample_to_rgb(rgb_to_sample::<U24>(pix)),
sample_to_rgb(rgb_to_sample::<U48>(pix))
);
}
#[test]
fn rgb2s2rgb() {
let pix = Pixel::new(45, 18, 143);
assert_eq!(pix, (sample_to_rgb(rgb_to_sample::<f64>(pix))));
}
}

View file

@ -10,4 +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 }
dasp_sample = { workspace = true }

View file

@ -1,61 +1,35 @@
use std::{
array, mem,
ops::{BitOr, Shl},
};
use dasp_sample::{FromSample, Sample, U24};
mod media;
use fundsp::Real;
pub trait AsMedia<M, T> {
fn as_media(self) -> T;
}
pub type Byte = u8;
pub trait AsSample<S>
pub trait BytesToSample<Ir, It, Ii>
where
S: Sample,
Self: IntoIterator<Item = It, IntoIter = Ii> + Sized,
Ii: Iterator<Item = It>,
Ir: FromSample<It>
+ Sample
+ Default
+ From<u8>
+ Shl<Ir, Output = Ir>
+ BitOr<Ir, Output = Ir>,
It: Sample,
{
fn as_sample(self) -> S;
}
impl<S> AsSample<S> for u8
where
S: Sample + FromSample<u8>,
{
fn as_sample(self) -> S {
self.to_sample()
}
}
impl<S> AsSample<S> for (u8, u8)
where
S: Sample + FromSample<u16>,
{
fn as_sample(self) -> S {
(((self.0 as u16) << 8) | self.1 as u16).to_sample()
}
}
impl<S> AsSample<S> for (u8, u8, u8)
where
S: Sample + FromSample<U24>,
{
fn as_sample(self) -> S {
(((U24::from(self.0)) << 16.into()) | (U24::from(self.1) << 8.into()) | U24::from(self.2))
fn to_sample<S>(self) -> S
where
S: Real + Sample + FromSample<Ir>,
{
self.into_iter()
.map(|it| Ir::from_sample(it))
.reduce(|acc, ir| (acc << Ir::from(mem::size_of::<It>() as u8 * 8u8)) | ir)
.unwrap()
.to_sample()
}
}
impl<S> AsSample<S> for (u8, u8, u8, u8)
where
S: Sample + FromSample<u32>,
{
fn as_sample(self) -> S {
(((self.0 as u32) << 24) | ((self.1 as u32) << 16) | ((self.2 as u32) << 8) | self.3 as u32)
.to_sample()
}
}
impl<T, S> AsMedia<media::Sample, S> for T
where
T: AsSample<S>,
S: Sample,
{
fn as_media(self) -> S {
self.as_sample()
}
}
impl BytesToSample<U24, Byte, array::IntoIter<Byte, 3>> for [Byte; 3] {}

View file

@ -1 +0,0 @@
pub struct Sample;