From 0194f60ca3d2f8c329e35f1eed4db39202ed550c Mon Sep 17 00:00:00 2001 From: p6nj Date: Sun, 7 Jul 2024 08:19:23 +0200 Subject: [PATCH] finally a perfect ToSample trait! --- Cargo.toml | 4 ++ bent-funny-zone/Cargo.toml | 9 +++-- bent-funny-zone/src/main.rs | 72 +--------------------------------- bingus/Cargo.toml | 1 + bingus/src/lib.rs | 78 +++++++++++++------------------------ bingus/src/media.rs | 1 - 6 files changed, 38 insertions(+), 127 deletions(-) delete mode 100644 bingus/src/media.rs diff --git a/Cargo.toml b/Cargo.toml index 2fb99f0..bcf529e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/bent-funny-zone/Cargo.toml b/bent-funny-zone/Cargo.toml index aec3e0f..20f92ae 100644 --- a/bent-funny-zone/Cargo.toml +++ b/bent-funny-zone/Cargo.toml @@ -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 } diff --git a/bent-funny-zone/src/main.rs b/bent-funny-zone/src/main.rs index a4f4ea6..dfd2102 100644 --- a/bent-funny-zone/src/main.rs +++ b/bent-funny-zone/src/main.rs @@ -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::(bmp.get_pixel(x, y))), - ); - } - } - } - bmp.to_writer(&mut File::create("bmp/out.bmp")?)?; + dbg!([1u8, 2, 3].to_sample::()); Ok(()) } - -fn rgb_to_sample>(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>(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::().to_sample()) - } - - #[test] - fn rgb2s2rgb_type_consistency() { - let pix = Pixel::new(45, 18, 143); - assert_eq!( - sample_to_rgb(rgb_to_sample::(pix)), - sample_to_rgb(rgb_to_sample::(pix)) - ); - assert_eq!( - sample_to_rgb(rgb_to_sample::(pix)), - sample_to_rgb(rgb_to_sample::(pix)) - ); - assert_eq!( - sample_to_rgb(rgb_to_sample::(pix)), - sample_to_rgb(rgb_to_sample::(pix)) - ); - } - - #[test] - fn rgb2s2rgb() { - let pix = Pixel::new(45, 18, 143); - assert_eq!(pix, (sample_to_rgb(rgb_to_sample::(pix)))); - } -} diff --git a/bingus/Cargo.toml b/bingus/Cargo.toml index 8027b64..1548bfb 100644 --- a/bingus/Cargo.toml +++ b/bingus/Cargo.toml @@ -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 } diff --git a/bingus/src/lib.rs b/bingus/src/lib.rs index a717ba8..8c9509a 100644 --- a/bingus/src/lib.rs +++ b/bingus/src/lib.rs @@ -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 { - fn as_media(self) -> T; -} +pub type Byte = u8; -pub trait AsSample +pub trait BytesToSample where - S: Sample, + Self: IntoIterator + Sized, + Ii: Iterator, + Ir: FromSample + + Sample + + Default + + From + + Shl + + BitOr, + It: Sample, { - fn as_sample(self) -> S; -} - -impl AsSample for u8 -where - S: Sample + FromSample, -{ - fn as_sample(self) -> S { - self.to_sample() - } -} - -impl AsSample for (u8, u8) -where - S: Sample + FromSample, -{ - fn as_sample(self) -> S { - (((self.0 as u16) << 8) | self.1 as u16).to_sample() - } -} - -impl AsSample for (u8, u8, u8) -where - S: Sample + FromSample, -{ - fn as_sample(self) -> S { - (((U24::from(self.0)) << 16.into()) | (U24::from(self.1) << 8.into()) | U24::from(self.2)) + fn to_sample(self) -> S + where + S: Real + Sample + FromSample, + { + self.into_iter() + .map(|it| Ir::from_sample(it)) + .reduce(|acc, ir| (acc << Ir::from(mem::size_of::() as u8 * 8u8)) | ir) + .unwrap() .to_sample() } } -impl AsSample for (u8, u8, u8, u8) -where - S: Sample + FromSample, -{ - 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 AsMedia for T -where - T: AsSample, - S: Sample, -{ - fn as_media(self) -> S { - self.as_sample() - } -} +impl BytesToSample> for [Byte; 3] {} diff --git a/bingus/src/media.rs b/bingus/src/media.rs deleted file mode 100644 index 43abd1f..0000000 --- a/bingus/src/media.rs +++ /dev/null @@ -1 +0,0 @@ -pub struct Sample;