Traits ImageView and ImageViewMut marked as unsafe.

This commit is contained in:
Kirill Kuzminykh
2024-05-05 18:58:25 +03:00
parent bd5a82ab4b
commit 895ffc3f0c
3 changed files with 22 additions and 50 deletions
+17 -28
View File
@@ -2,27 +2,22 @@ use crate::pixels::InnerPixel;
use crate::{ArrayChunks, ImageError, PixelTrait, PixelType};
/// A trait for getting access to image data.
pub trait ImageView {
///
/// # Safety
///
/// The length of the image rows returned by methods of this trait
/// must be equal or greater than the image width.
pub unsafe trait ImageView {
type Pixel: InnerPixel;
fn pixel_type(&self) -> PixelType {
Self::Pixel::pixel_type()
}
fn width(&self) -> u32;
fn height(&self) -> u32;
/// Returns iterator by slices with image rows.
///
/// Note: An implementation must guaranty that all rows returned by iterator
/// have the same size and this size isn't less than the image width.
fn iter_rows(&self, start_row: u32) -> impl Iterator<Item = &[Self::Pixel]>;
/// Returns iterator by arrays with two image rows.
///
/// Note: An implementation must guaranty that all rows returned by iterator
/// have the same size and this size isn't less than the image width.
fn iter_2_rows(
&self,
start_y: u32,
@@ -32,9 +27,6 @@ pub trait ImageView {
}
/// Returns iterator by arrays with four image rows.
///
/// Note: An implementation must guaranty that all rows returned by iterator
/// have the same size and this size isn't less than the image width.
fn iter_4_rows(
&self,
start_y: u32,
@@ -43,10 +35,8 @@ pub trait ImageView {
ArrayChunks::new(self.iter_rows(start_y).take(max_rows as usize))
}
/// Returns iterator by image rows selected from image with given step.
///
/// Note: An implementation must guaranty that all rows returned by iterator
/// have the same size and this size isn't less than the image width.
/// Returns iterator by slices with image rows selected from
/// the image with the given step.
fn iter_rows_with_step(
&self,
start_y: f64,
@@ -74,17 +64,16 @@ pub trait ImageView {
}
/// A trait for getting mutable access to image data.
pub trait ImageViewMut: ImageView {
///
/// # Safety
///
/// The length of the image rows returned by methods of this trait
/// must be equal or greater than the image width.
pub unsafe trait ImageViewMut: ImageView {
/// Returns iterator by mutable slices with image rows.
///
/// Note: An implementation must guaranty that all rows returned by iterator
/// have the same size and this size isn't less than the image width.
fn iter_rows_mut(&mut self, start_row: u32) -> impl Iterator<Item = &mut [Self::Pixel]>;
/// Returns iterator by arrays with four mutable image rows.
///
/// Note: An implementation must guaranty that all rows returned by iterator
/// have the same size and this size isn't less than the image width.
fn iter_4_rows_mut(&mut self) -> ArrayChunks<impl Iterator<Item = &mut [Self::Pixel]>, 4> {
ArrayChunks::new(self.iter_rows_mut(0))
}
@@ -92,7 +81,7 @@ pub trait ImageViewMut: ImageView {
/// Conversion into an [ImageView].
pub trait IntoImageView {
/// Returns pixels type of the image if this type is supported by the crate.
/// Returns pixel's type of the image.
fn pixel_type(&self) -> Option<PixelType>;
fn width(&self) -> u32;
@@ -107,8 +96,8 @@ pub trait IntoImageViewMut: IntoImageView {
fn image_view_mut<P: PixelTrait>(&mut self) -> Option<impl ImageViewMut<Pixel = P>>;
}
/// Returns supported by the crate pixels type of the image or `ImageError` if the image
/// has not supported pixels type.
/// Returns supported by the crate pixel's type of the image or `ImageError` if the image
/// has not supported pixel's type.
pub(crate) fn try_pixel_type(image: &impl IntoImageView) -> Result<PixelType, ImageError> {
image.pixel_type().ok_or(ImageError::UnsupportedPixelType)
}
+2 -2
View File
@@ -51,7 +51,7 @@ macro_rules! cropped_image_impl {
}
}
impl<V: $view_trait> ImageView for $wrapper_name<V> {
unsafe impl<V: $view_trait> ImageView for $wrapper_name<V> {
type Pixel = V::Pixel;
fn width(&self) -> u32 {
@@ -85,7 +85,7 @@ cropped_image_impl!(
"It is wrapper that provides [ImageViewMut] for part of wrapped image."
);
impl<V: ImageViewMut> ImageViewMut for CroppedImageMut<V> {
unsafe impl<V: ImageViewMut> ImageViewMut for CroppedImageMut<V> {
fn iter_rows_mut(&mut self, start_row: u32) -> impl Iterator<Item = &mut [Self::Pixel]> {
let left = self.left as usize;
let right = left + self.width as usize;
+3 -20
View File
@@ -58,7 +58,7 @@ impl<'a, P> TypedImage<'a, P> {
}
}
impl<'a, P: InnerPixel> ImageView for TypedImage<'a, P> {
unsafe impl<'a, P: InnerPixel> ImageView for TypedImage<'a, P> {
type Pixel = P;
fn width(&self) -> u32 {
@@ -144,23 +144,6 @@ impl<'a, P: InnerPixel> TypedImageMut<'a, P> {
})
}
// pub fn from_components(
// width: u32,
// height: u32,
// components: &'a mut [P::Component],
// ) -> Result<Self, ImageBufferError> {
// let components_count = width as usize * height as usize * P::count_of_components();
// if components.len() < components_count {
// return Err(ImageBufferError::InvalidBufferSize);
// }
// let pixels = align_buffer_to_mut(components)?;
// Ok(Self {
// width,
// height,
// pixels: PixelsContainer::Borrowed(pixels),
// })
// }
pub fn from_buffer(
width: u32,
height: u32,
@@ -175,7 +158,7 @@ impl<'a, P: InnerPixel> TypedImageMut<'a, P> {
}
}
impl<'a, P: InnerPixel> ImageView for TypedImageMut<'a, P> {
unsafe impl<'a, P: InnerPixel> ImageView for TypedImageMut<'a, P> {
type Pixel = P;
fn width(&self) -> u32 {
@@ -201,7 +184,7 @@ impl<'a, P: InnerPixel> ImageView for TypedImageMut<'a, P> {
}
}
impl<'a, P: InnerPixel> ImageViewMut for TypedImageMut<'a, P> {
unsafe impl<'a, P: InnerPixel> ImageViewMut for TypedImageMut<'a, P> {
fn iter_rows_mut(&mut self, start_row: u32) -> impl Iterator<Item = &mut [Self::Pixel]> {
let width = self.width as usize;
if width == 0 {