From ff6e44072ee2cd8cba5eb87792131d889ae8a376 Mon Sep 17 00:00:00 2001 From: p6nj Date: Fri, 17 Jan 2025 01:45:25 -0500 Subject: [PATCH] rebranding, image databending --- bingus/Cargo.toml | 5 ++-- bingus/src/img.rs | 1 + bingus/src/img/image.rs | 66 +++++++++++++++++++++++++++++++++++++++++ bingus/src/lib.rs | 36 ++++++++++++++++++++-- bingus/src/rawdata.rs | 36 ---------------------- bingus/src/snd.rs | 0 bingus/src/txt.rs | 0 7 files changed, 104 insertions(+), 40 deletions(-) create mode 100644 bingus/src/img.rs create mode 100644 bingus/src/img/image.rs delete mode 100644 bingus/src/rawdata.rs create mode 100644 bingus/src/snd.rs create mode 100644 bingus/src/txt.rs diff --git a/bingus/Cargo.toml b/bingus/Cargo.toml index 5f28930..0383821 100644 --- a/bingus/Cargo.toml +++ b/bingus/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bingus" -version = "0.1.2" +version = "0.2.0" edition.workspace = true license.workspace = true description.workspace = true @@ -10,4 +10,5 @@ keywords.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -derive-new = { workspace = true } +image = { version = "0.25", features = ["rayon"] } +num = "0" diff --git a/bingus/src/img.rs b/bingus/src/img.rs new file mode 100644 index 0000000..14995d4 --- /dev/null +++ b/bingus/src/img.rs @@ -0,0 +1 @@ +pub mod image; diff --git a/bingus/src/img/image.rs b/bingus/src/img/image.rs new file mode 100644 index 0000000..a4a657f --- /dev/null +++ b/bingus/src/img/image.rs @@ -0,0 +1,66 @@ +use std::ops::{BitOr, Deref, Shl}; + +use image::{ImageBuffer, Pixel}; +use num::{traits::ToBytes, Zero}; + +use crate::{Bendable, IntoDataBytes, TryFromDataBytes}; + +impl IntoDataBytes for ImageBuffer> +where + Vec

: Deref, + P::Subpixel: ToBytes, +{ + fn into_bytes(self) -> crate::Bytes { + self.into_iter() + .flat_map(|subpixel| subpixel.to_be_bytes().as_ref().to_vec()) + .collect() + } +} + +pub struct Dimensions { + pub width: u32, + pub height: u32, +} + +impl TryFromDataBytes for ImageBuffer> +where + Vec

: Deref, + P::Subpixel: + ToBytes + Zero + Shl + BitOr, +{ + type Error = (); + type Format = Dimensions; + fn try_from_bytes(bytes: crate::Bytes, format: Self::Format) -> Result + where + Self: Sized, + { + ImageBuffer::from_raw( + format.width, + format.height, + bytes + .chunks_exact(P::Subpixel::zero().to_be_bytes().as_ref().len()) + .map(|p| { + p.iter() + .copied() + .flat_map(num::traits::cast::) + .reduce(|acc, b| (acc << 8u8) | b) + .unwrap() + }) + .collect::>(), + ) + .ok_or(()) + } +} + +impl Bendable for ImageBuffer> +where + Vec

: Deref, + P::Subpixel: + ToBytes + Zero + Shl + BitOr, +{ + type Unit = P; + fn apply Self::Unit>(mut self, f: F) -> Self { + self.pixels_mut().for_each(|p| *p = f(p)); + self + } +} diff --git a/bingus/src/lib.rs b/bingus/src/lib.rs index e295326..2fa2d6a 100644 --- a/bingus/src/lib.rs +++ b/bingus/src/lib.rs @@ -1,2 +1,34 @@ -#![forbid(unused_crate_dependencies)] -pub mod rawdata; +pub mod img; +pub mod snd; +pub mod txt; + +pub type Bytes = Vec; + +pub trait Bendable: TryFromDataBytes + IntoDataBytes { + type Unit; + fn bend_into( + self, + format: ::Format, + ) -> Result::Error> { + T::bend_from(self, format) + } + fn bend_from( + b: T, + format: ::Format, + ) -> Result::Error> { + Self::try_from_bytes(b.into_bytes(), format) + } + fn apply Self::Unit>(self, f: F) -> Self; +} + +pub trait IntoDataBytes: Sized { + fn into_bytes(self) -> Bytes; +} + +pub trait TryFromDataBytes { + type Error; + type Format; + fn try_from_bytes(bytes: Bytes, format: Self::Format) -> Result + where + Self: Sized; +} diff --git a/bingus/src/rawdata.rs b/bingus/src/rawdata.rs deleted file mode 100644 index d0ac87b..0000000 --- a/bingus/src/rawdata.rs +++ /dev/null @@ -1,36 +0,0 @@ -use std::ops::Deref; - -use derive_new::new; - -pub type Bytes = [u8]; - -#[derive(new)] -pub struct RawData>(D); - -impl From> for Vec -where - D: Deref, -{ - fn from(value: RawData) -> Self { - value.to_owned() - } -} - -impl Clone for RawData -where - D: Clone + Deref, -{ - fn clone(&self) -> Self { - Self(self.deref().clone()) - } -} - -impl Deref for RawData -where - D: Deref, -{ - type Target = D; - fn deref(&self) -> &Self::Target { - &self.0 - } -} diff --git a/bingus/src/snd.rs b/bingus/src/snd.rs new file mode 100644 index 0000000..e69de29 diff --git a/bingus/src/txt.rs b/bingus/src/txt.rs new file mode 100644 index 0000000..e69de29