finally a perfect ToSample trait!
This commit is contained in:
parent
41bbd3d371
commit
0194f60ca3
6 changed files with 38 additions and 127 deletions
|
@ -9,4 +9,8 @@ package.repository = "https://codeberg.org/p6nj/bent"
|
||||||
package.keywords = ["databending", "data-bending", "bending", "bend", "art"]
|
package.keywords = ["databending", "data-bending", "bending", "bend", "art"]
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
|
fundsp = "0.18.2"
|
||||||
|
image = "0.24.9"
|
||||||
|
anyhow = "1.0.86"
|
||||||
dasp_sample = "0.11.0"
|
dasp_sample = "0.11.0"
|
||||||
|
bingus = { version = "0.1.0", path = "bingus" }
|
||||||
|
|
|
@ -11,7 +11,8 @@ keywords.workspace = true
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.86"
|
image = { workspace = true }
|
||||||
bmp = "0.5.0"
|
fundsp = { workspace = true }
|
||||||
dasp_sample = "0.11.0"
|
anyhow = { workspace = true }
|
||||||
fundsp = { version = "0.18.1", default-features = false, features = ["std"] }
|
dasp_sample = { workspace = true }
|
||||||
|
bingus = { workspace = true }
|
||||||
|
|
|
@ -1,75 +1,7 @@
|
||||||
use std::fs::File;
|
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use bmp::{open, Pixel};
|
use bingus::BytesToSample;
|
||||||
use dasp_sample::{FromSample, Sample, ToSample, U24};
|
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut bmp = open("bmp/bigsample.bmp")?;
|
dbg!([1u8, 2, 3].to_sample::<f64>());
|
||||||
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")?)?;
|
|
||||||
Ok(())
|
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))));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -10,4 +10,5 @@ keywords.workspace = true
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
fundsp = { workspace = true }
|
||||||
dasp_sample = { workspace = true }
|
dasp_sample = { workspace = true }
|
||||||
|
|
|
@ -1,61 +1,35 @@
|
||||||
|
use std::{
|
||||||
|
array, mem,
|
||||||
|
ops::{BitOr, Shl},
|
||||||
|
};
|
||||||
|
|
||||||
use dasp_sample::{FromSample, Sample, U24};
|
use dasp_sample::{FromSample, Sample, U24};
|
||||||
mod media;
|
use fundsp::Real;
|
||||||
|
|
||||||
pub trait AsMedia<M, T> {
|
pub type Byte = u8;
|
||||||
fn as_media(self) -> T;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait AsSample<S>
|
pub trait BytesToSample<Ir, It, Ii>
|
||||||
where
|
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;
|
fn to_sample<S>(self) -> S
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> AsSample<S> for u8
|
|
||||||
where
|
where
|
||||||
S: Sample + FromSample<u8>,
|
S: Real + Sample + FromSample<Ir>,
|
||||||
{
|
{
|
||||||
fn as_sample(self) -> S {
|
self.into_iter()
|
||||||
self.to_sample()
|
.map(|it| Ir::from_sample(it))
|
||||||
}
|
.reduce(|acc, ir| (acc << Ir::from(mem::size_of::<It>() as u8 * 8u8)) | ir)
|
||||||
}
|
.unwrap()
|
||||||
|
|
||||||
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))
|
|
||||||
.to_sample()
|
.to_sample()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> AsSample<S> for (u8, u8, u8, u8)
|
impl BytesToSample<U24, Byte, array::IntoIter<Byte, 3>> for [Byte; 3] {}
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
pub struct Sample;
|
|
Loading…
Reference in a new issue