finally a perfect ToSample trait!
This commit is contained in:
parent
41bbd3d371
commit
0194f60ca3
6 changed files with 38 additions and 127 deletions
|
@ -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 }
|
||||
|
|
|
@ -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))));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue