diff --git a/src/lib.rs b/src/lib.rs index 2f32498..5b99ea0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ use image::GenericImageView; pub use neigbors::Neighbors; pub use pixel::Bit; -pub use view::{raw::BinaryRawView, BinaryView}; +pub use view::BinaryView; mod neigbors; mod pixel; @@ -41,7 +41,7 @@ impl BinaryImage { } } -impl GenericImageView for BinaryImage { +impl image::GenericImageView for BinaryImage { type Pixel = pixel::Bit; #[inline] unsafe fn unsafe_get_pixel(&self, x: u32, y: u32) -> Self::Pixel { @@ -84,7 +84,7 @@ impl image::GenericImage for BinaryImage { } } -impl> From<&I> for BinaryImage { +impl> From<&I> for BinaryImage { fn from(view: &I) -> Self { BinaryImage { height: view.height(), diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 55be7be..c71bcb0 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -11,35 +11,37 @@ static DATA: [u8; 16] = [ #[test] fn test_neigbors() { let binary_image = BinaryImage::from_raw(4, 4, &DATA); - let neigbors = Neighbors::get_neighbors(&binary_image, 1, 1); - assert_eq!(neigbors.bits(), 0b1010_0001); - - let neigbors = Neighbors::get_neighbors(&binary_image, 2, 2); - - assert_eq!(neigbors.bits(), 0b0101_1001); - - let neigbors = Neighbors::get_neighbors(&binary_image, 0, 0); - - assert_eq!(neigbors.bits(), 0b0000_1000); + assert_eq!( + Neighbors::get_neighbors(&binary_image, 1, 1).bits(), + 0b1010_0001 + ); + assert_eq!( + Neighbors::get_neighbors(&binary_image, 2, 2).bits(), + 0b0101_1001 + ); + assert_eq!( + Neighbors::get_neighbors(&binary_image, 0, 0).bits(), + 0b0000_1000 + ); } #[test] fn test_binary_image_creation() { - let binary_image = BinaryImage::from_raw(4, 4, &DATA); + let image = BinaryImage::from_raw(4, 4, &DATA); - assert_eq!(binary_image.width(), 4); - assert_eq!(binary_image.height(), 4); - assert!(*binary_image.get_pixel(0, 0)); - assert!(*binary_image.get_pixel(2, 1)); - assert!(*binary_image.get_pixel(1, 2)); - assert!(*binary_image.get_pixel(3, 3)); + assert_eq!(image.width(), 4); + assert_eq!(image.height(), 4); + assert!(*image.get_pixel(0, 0)); + assert!(*image.get_pixel(2, 1)); + assert!(*image.get_pixel(1, 2)); + assert!(*image.get_pixel(3, 3)); } #[test] -fn test_binary_view() { - let binary_image = BinaryImage::from_raw(4, 4, &DATA); - let view = BinaryView(&binary_image); +fn test_view() { + let image = BinaryImage::from_raw(4, 4, &DATA); + let view = BinaryView(&image); assert_eq!(view.width(), 4); assert_eq!(view.height(), 4); @@ -50,13 +52,15 @@ fn test_binary_view() { } #[test] -fn test_binary_raw_view() { - let raw_view = BinaryRawView::new(4, 4, &DATA); - - assert_eq!(raw_view.width(), 4); - assert_eq!(raw_view.height(), 4); - assert!(*raw_view.get_pixel(0, 0)); - assert!(*raw_view.get_pixel(2, 1)); - assert!(*raw_view.get_pixel(1, 2)); - assert!(*raw_view.get_pixel(3, 3)); +fn test_view_raw() { + let image: image::ImageBuffer, &[u8]> = + image::ImageBuffer::from_raw(4, 4, &DATA[..]).unwrap(); + let view = BinaryView(&image); + + assert_eq!(view.width(), 4); + assert_eq!(view.height(), 4); + assert!(*view.get_pixel(0, 0)); + assert!(*view.get_pixel(2, 1)); + assert!(*view.get_pixel(1, 2)); + assert!(*view.get_pixel(3, 3)); } diff --git a/src/view/mod.rs b/src/view.rs similarity index 94% rename from src/view/mod.rs rename to src/view.rs index bf23d7a..465b20d 100644 --- a/src/view/mod.rs +++ b/src/view.rs @@ -5,8 +5,6 @@ use num_traits::Zero; use crate::pixel::Bit; -pub mod raw; - #[derive(Debug, Clone, DerefMut, Deref, From, Constructor)] pub struct BinaryView<'a, I>(pub &'a I) where @@ -32,7 +30,7 @@ where alpha }, ); - Bit::from(if alpha_exist { alphas } else { channels }) + Bit(if alpha_exist { alphas } else { channels }) } #[inline] fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel { diff --git a/src/view/raw.rs b/src/view/raw.rs deleted file mode 100644 index 129f838..0000000 --- a/src/view/raw.rs +++ /dev/null @@ -1,68 +0,0 @@ -#![allow(clippy::module_name_repetitions)] -use image::GenericImageView; - -use crate::pixel::Bit; - -#[derive(Debug, Clone)] -pub struct BinaryRawView<'a, T> -where - T: num_traits::Zero, -{ - height: u32, - width: u32, - data: &'a [T], -} - -impl<'a, T> BinaryRawView<'a, T> -where - T: num_traits::Zero, -{ - #[must_use] - pub fn new(height: u32, width: u32, data: &'a [T]) -> Self { - debug_assert!( - data.len() >= (height * width) as usize, - "Data must not be smaller than image dimensions" - ); - Self { - height, - width, - data, - } - } -} - -impl<'a, T> GenericImageView for BinaryRawView<'a, T> -where - T: num_traits::Zero, -{ - type Pixel = Bit; - #[inline] - unsafe fn unsafe_get_pixel(&self, x: u32, y: u32) -> Self::Pixel { - let size = self.data.len() / (self.height * self.width) as usize; - let start = (y * self.width + x) as usize; - let end = start + size; - Bit::from( - self.data - .get_unchecked(start..end) - .iter() - .any(|channel| !channel.is_zero()), - ) - } - #[inline] - fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel { - debug_assert!(self.in_bounds(x, y)); - unsafe { self.unsafe_get_pixel(x, y) } - } - #[inline] - fn dimensions(&self) -> (u32, u32) { - (self.width(), self.height()) - } - #[inline] - fn height(&self) -> u32 { - self.height - } - #[inline] - fn width(&self) -> u32 { - self.width - } -}