From 07f2a9fa381372a800f8a4e9526709fd632b4014 Mon Sep 17 00:00:00 2001 From: Breval Ferrari Date: Fri, 14 Mar 2025 23:54:22 -0400 Subject: [PATCH] dimensions operator impls, pad image if it's too small --- bingus/src/img/image.rs | 95 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/bingus/src/img/image.rs b/bingus/src/img/image.rs index 8d647b2..0f47b2d 100644 --- a/bingus/src/img/image.rs +++ b/bingus/src/img/image.rs @@ -1,6 +1,7 @@ use std::{ convert::Infallible, - ops::{Deref, DerefMut}, + iter::once, + ops::{Add, Deref, DerefMut, Div, Mul, Sub}, }; pub use image::*; @@ -25,15 +26,96 @@ where } } +#[cfg_attr(debug_assertions, derive(Debug))] pub struct Dimensions { pub width: u32, pub height: u32, } +impl Div for Dimensions { + type Output = Dimensions; + fn div(self, rhs: Dimensions) -> Self::Output { + Dimensions { + width: self.width / rhs.width, + height: self.height / rhs.height, + } + } +} + +impl Div for Dimensions { + type Output = Dimensions; + fn div(self, rhs: u32) -> Self::Output { + Dimensions { + width: self.width / rhs, + height: self.height / rhs, + } + } +} + +impl Mul for Dimensions { + type Output = Dimensions; + fn mul(self, rhs: Dimensions) -> Self::Output { + Dimensions { + width: self.width * rhs.width, + height: self.height * rhs.height, + } + } +} + +impl Mul for Dimensions { + type Output = Dimensions; + fn mul(self, rhs: u32) -> Self::Output { + Dimensions { + width: self.width * rhs, + height: self.height * rhs, + } + } +} + +impl Add for Dimensions { + type Output = Dimensions; + fn add(self, rhs: Dimensions) -> Self::Output { + Dimensions { + width: self.width + rhs.width, + height: self.height + rhs.height, + } + } +} + +impl Add for Dimensions { + type Output = Dimensions; + fn add(self, rhs: u32) -> Self::Output { + Dimensions { + width: self.width + rhs, + height: self.height + rhs, + } + } +} + +impl Sub for Dimensions { + type Output = Dimensions; + fn sub(self, rhs: Dimensions) -> Self::Output { + Dimensions { + width: self.width - rhs.width, + height: self.height - rhs.height, + } + } +} + +impl Sub for Dimensions { + type Output = Dimensions; + fn sub(self, rhs: u32) -> Self::Output { + Dimensions { + width: self.width - rhs, + height: self.height - rhs, + } + } +} + impl TryFromDataBytes for ImageBuffer> where Vec: Deref, - P::Subpixel: ToBytes + FromBytes, + P::Subpixel: ToBytes + FromBytes + Zero, ::Bytes: for<'a> TryFrom<&'a [u8]>, { type Error = Infallible; @@ -50,6 +132,7 @@ where format.width, format.height, match crop { + // TODO: separate outer crop from inner crop crate::Crop::End => bytes .chunks_exact(P::Subpixel::zero().to_ne_bytes().as_ref().len()) .map(|p| { @@ -60,6 +143,10 @@ where }, ) }) + .chain(once(P::Subpixel::zero()).cycle()) + .take( + format.width as usize * format.height as usize * P::CHANNEL_COUNT as usize, + ) .collect::>(), crate::Crop::Start => bytes .rchunks_exact(P::Subpixel::zero().to_ne_bytes().as_ref().len()) @@ -71,6 +158,10 @@ where }, ) }) + .chain(once(P::Subpixel::zero()).cycle()) + .take( + format.width as usize * format.height as usize * P::CHANNEL_COUNT as usize, + ) .collect::>(), }, )