Struct imgref::Img

source ·
pub struct Img<Container> {
    pub buf: Container,
    pub stride: usize,
    pub width: u32,
    pub height: u32,
}
Expand description

Basic struct used for both owned (alias ImgVec) and borrowed (alias ImgRef) image fragments.

Note: the fields are pub only because of borrow checker limitations. Please consider them as read-only.

Fields§

§buf: Container
👎Deprecated: Don’t access struct fields directly. Use buf(), buf_mut() or into_buf()

Storage for the pixels. Usually Vec<Pixel> or &[Pixel]. See ImgVec and ImgRef.

Note that future version will make this field private. Use .rows() and .pixels() iterators where possible, or buf()/buf_mut()/into_buf().

§stride: usize
👎Deprecated: Don’t access struct fields directly. Use stride()

Number of pixels to skip in the container to advance to the next row.

Note: pixels between width and stride may not be usable, and may not even exist in the last row.

§width: u32
👎Deprecated: Don’t access struct fields directly. Use width()

Width of the image in pixels.

Note that this isn’t same as the width of the row in the buf, see stride

§height: u32
👎Deprecated: Don’t access struct fields directly. Use height()

Height of the image in pixels.

Implementations§

source§

impl<Container> Img<Container>

source

pub fn width(&self) -> usize

Width of the image in pixels.

Note that this isn’t same as the width of the row in image data, see stride()

source

pub fn height(&self) -> usize

Height of the image in pixels.

source

pub fn stride(&self) -> usize

Number of pixels to skip in the container to advance to the next row.

Note the last row may have fewer pixels than the stride. Some APIs use number of bytes for a stride. You may need to multiply this one by number of pixels.

source

pub fn buf(&self) -> &Container

Immutable reference to the pixel storage. Warning: exposes stride. Use pixels() or rows() instead.

See also into_contiguous_buf().

source

pub fn buf_mut(&mut self) -> &mut Container

Mutable reference to the pixel storage. Warning: exposes stride. Use pixels_mut() or rows_mut() instead.

See also into_contiguous_buf().

source

pub fn into_buf(self) -> Container

Get the pixel storage by consuming the image. Be careful about stride — see into_contiguous_buf() for a safe version.

source§

impl<'a, T> Img<&'a [T]>

source

pub fn sub_image( &self, left: usize, top: usize, width: usize, height: usize ) -> Self

Make a reference for a part of the image, without copying any pixels.

§Panics

It will panic if sub_image is outside of the image area (left + width must be <= container width, etc.)

source

pub fn rows(&self) -> RowsIter<'_, T>

Iterate over whole rows of pixels as slices

§Panics

If stride is 0

See also pixels()

source§

impl<'a, T: Clone> Img<&'a [T]>

source

pub fn to_contiguous_buf(&self) -> (Cow<'a, [T]>, usize, usize)

Returns a reference to the buffer, width, height. Guarantees that the buffer is contiguous, i.e. it’s width*height elements long, and [x + y*width] addresses each pixel.

It will create a copy if the buffer isn’t contiguous (width != stride). For a more efficient version, see into_contiguous_buf()

source§

impl<'a, T> Img<&'a mut [T]>

source

pub fn sub_image( &'a mut self, left: usize, top: usize, width: usize, height: usize ) -> ImgRef<'a, T>

Turn this into immutable reference, and slice a subregion of it

source

pub fn sub_image_mut( &mut self, left: usize, top: usize, width: usize, height: usize ) -> ImgRefMut<'_, T>

Trim this image without copying. Note that mutable borrows are exclusive, so it’s not possible to have more than one mutable subimage at a time.

source

pub fn as_ref(&self) -> ImgRef<'_, T>

Make mutable reference immutable

source§

impl<'a, T: Copy> Img<&'a [T]>

source

pub fn pixels(&self) -> PixelsIter<'_, T>

Iterate width*height pixels in the Img, ignoring padding area

If you want to iterate in parallel, parallelize rows() instead.

§Panics

if width is 0

source§

impl<'a, T> Img<&'a [T]>

source

pub fn pixels_ref(&self) -> PixelsRefIter<'_, T>

Iterate width*height pixels in the Img, by reference, ignoring padding area

If you want to iterate in parallel, parallelize rows() instead.

§Panics

if width is 0

source§

impl<'a, T: Copy> Img<&'a mut [T]>

source

pub fn pixels(&self) -> PixelsIter<'_, T>

§Panics

If you want to iterate in parallel, parallelize rows() instead.

if width is 0

source

pub fn pixels_mut(&mut self) -> PixelsIterMut<'_, T>

If you want to iterate in parallel, parallelize rows() instead.

§Panics

if width is 0

source§

impl<T: Copy> Img<Vec<T>>

source

pub fn pixels(&self) -> PixelsIter<'_, T>

If you want to iterate in parallel, parallelize rows() instead.

§Panics

if width is 0

source

pub fn pixels_mut(&mut self) -> PixelsIterMut<'_, T>

If you want to iterate in parallel, parallelize rows() instead.

§Panics

if width is 0

source§

impl<'a, T> Img<&'a mut [T]>

source

pub fn rows(&self) -> RowsIter<'_, T>

Iterate over whole rows as slices

§Panics

if stride is 0

source

pub fn rows_mut(&mut self) -> RowsIterMut<'_, T>

Iterate over whole rows as slices

§Panics

if stride is 0

source§

impl<T> Img<Vec<T>>

source

pub fn sub_image_mut( &mut self, left: usize, top: usize, width: usize, height: usize ) -> ImgRefMut<'_, T>

Create a mutable view into a region within the image. See sub_image() for read-only views.

source

pub fn sub_image( &self, left: usize, top: usize, width: usize, height: usize ) -> ImgRef<'_, T>

Make a reference for a part of the image, without copying any pixels.

source

pub fn as_ref(&self) -> ImgRef<'_, T>

Make a reference to this image to pass it to functions without giving up ownership

The reference should be passed by value (ImgRef, not &ImgRef).

If you need a mutable reference, see as_mut() and sub_image_mut()

source

pub fn as_mut(&mut self) -> ImgRefMut<'_, T>

Make a mutable reference to the entire image

The reference should be passed by value (ImgRefMut, not &mut ImgRefMut).

See also sub_image_mut() and rows_mut()

source

pub fn rows(&self) -> RowsIter<'_, T>

Iterate over rows of the image as slices

Each slice is guaranteed to be exactly width pixels wide.

This iterator is a good candidate for parallelization (e.g. rayon’s par_bridge())

source

pub fn rows_mut(&mut self) -> RowsIterMut<'_, T>

Iterate over rows of the image as mutable slices

Each slice is guaranteed to be exactly width pixels wide.

This iterator is a good candidate for parallelization (e.g. rayon’s par_bridge())

source§

impl<Container> Img<Container>

source

pub fn new_stride( buf: Container, width: usize, height: usize, stride: usize ) -> Self

Same as new(), except each row is located stride number of pixels after the previous one.

Stride can be equal to width or larger. If it’s larger, then pixels between end of previous row and start of the next are considered a padding, and may be ignored.

The Container is usually a Vec or a slice.

source

pub fn new(buf: Container, width: usize, height: usize) -> Self

Create new image with Container (which can be Vec, &[] or something else) with given width and height in pixels.

Assumes the pixels in container are contiguous, layed out row by row with width pixels per row and at least height rows.

If the container is larger than width×height pixels, the extra rows are a considered a padding and may be ignored.

source§

impl<T: Copy> Img<Vec<T>>

source

pub fn into_contiguous_buf(self) -> (Vec<T>, usize, usize)

Returns the buffer, width, height. Guarantees that the buffer is contiguous, i.e. it’s width*height elements long, and [x + y*width] addresses each pixel.

Efficiently performs operation in-place. For other containers use pixels().collect().

source

pub fn as_contiguous_buf(&mut self) -> (&[T], usize, usize)

Returns a reference to the buffer, width, height. Guarantees that the buffer is contiguous, i.e. it’s width*height elements long, and [x + y*width] addresses each pixel.

Efficiently performs operation in-place. For other containers use pixels().collect().

source§

impl<OldContainer> Img<OldContainer>

source

pub fn map_buf<NewContainer, OldPixel, NewPixel, F>( self, callback: F ) -> Img<NewContainer>
where NewContainer: AsRef<[NewPixel]>, OldContainer: AsRef<[OldPixel]>, F: FnOnce(OldContainer) -> NewContainer,

A convenience method for creating an image of the same size and stride, but with a new buffer.

source

pub fn new_buf<NewContainer, OldPixel, NewPixel>( &self, new_buf: NewContainer ) -> Img<NewContainer>
where NewContainer: AsRef<[NewPixel]>, OldContainer: AsRef<[OldPixel]>,

A convenience method for creating an image of the same size and stride, but with a new buffer.

source§

impl<T: Clone> Img<Cow<'_, [T]>>

source

pub fn into_owned(self) -> ImgVec<T>

Convert underlying buffer to owned (e.g. slice to vec)

See also to_contiguous_buf().0.into_owned()

source§

impl<T> Img<T>
where T: ToOwned,

source

pub fn to_owned(&self) -> Img<T::Owned>

Convert underlying buffer to owned (e.g. slice to vec)

See also to_contiguous_buf().0.into_owned()

Trait Implementations§

source§

impl<Container: Clone> Clone for Img<Container>

source§

fn clone(&self) -> Img<Container>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Container: Debug> Debug for Img<Container>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, T: Clone> From<Img<&'a [T]>> for Img<Cow<'a, [T]>>

source§

fn from(img: ImgRef<'a, T>) -> Self

Converts to this type from the input type.
source§

impl<T: Clone> From<Img<Cow<'_, [T]>>> for Img<Vec<T>>

source§

fn from(img: Img<Cow<'_, [T]>>) -> Self

Converts to this type from the input type.
source§

impl<T: Clone> From<Img<Vec<T>>> for Img<Cow<'static, [T]>>

source§

fn from(img: ImgVec<T>) -> Self

Converts to this type from the input type.
source§

impl<Pixel, Container> ImgExt<Pixel> for Img<Container>
where Container: AsRef<[Pixel]>,

source§

fn rows_padded(&self) -> Chunks<'_, Pixel>

Iterate over the entire buffer as rows, including all padding

Rows will have up to stride width, but the last row may be shorter.

source§

fn width_padded(&self) -> usize

Maximum possible width of the data, including the stride. Read more
source§

fn height_padded(&self) -> usize

Height in number of full strides. If the underlying buffer is not an even multiple of strides, the last row is ignored. Read more
source§

fn as_ref(&self) -> ImgRef<'_, Pixel>

Borrow the container
source§

impl<Pixel, Container> ImgExtMut<Pixel> for Img<Container>
where Container: AsMut<[Pixel]>,

source§

fn rows_padded_mut(&mut self) -> ChunksMut<'_, Pixel>

Iterate over the entire buffer as rows, including all padding

Rows will have up to stride width, but the last row may be shorter.

§Panics

If stride is 0

source§

fn as_mut(&mut self) -> ImgRefMut<'_, Pixel>

Borrow the container mutably
source§

impl<'a, Pixel: Copy> Index<(u32, u32)> for Img<&'a [Pixel]>

source§

fn index(&self, index: (u32, u32)) -> &Self::Output

Read a pixel at (x,y) location (e.g. px = img[(x,y)])

Coordinates may be outside width/height if the buffer has enough padding. The x coordinate can’t exceed stride.

§

type Output = Pixel

The returned type after indexing.
source§

impl<'a, Pixel: Copy> Index<(u32, u32)> for Img<&'a mut [Pixel]>

source§

fn index(&self, index: (u32, u32)) -> &Self::Output

Read a pixel at (x,y) location (e.g. px = img[(x,y)])

Coordinates may be outside width/height if the buffer has enough padding. The x coordinate can’t exceed stride.

§

type Output = Pixel

The returned type after indexing.
source§

impl<'a, Pixel: Copy> Index<(u32, u32)> for Img<Vec<Pixel>>

source§

fn index(&self, index: (u32, u32)) -> &Self::Output

Read a pixel at (x,y) location (e.g. px = img[(x,y)])

Coordinates may be outside width/height if the buffer has enough padding. The x coordinate can’t exceed stride.

§

type Output = Pixel

The returned type after indexing.
source§

impl<'a, Pixel: Copy> Index<(usize, usize)> for Img<&'a [Pixel]>

source§

fn index(&self, index: (usize, usize)) -> &Self::Output

Read a pixel at (x,y) location (e.g. px = img[(x,y)])

Coordinates may be outside width/height if the buffer has enough padding. The x coordinate can’t exceed stride.

§

type Output = Pixel

The returned type after indexing.
source§

impl<'a, Pixel: Copy> Index<(usize, usize)> for Img<&'a mut [Pixel]>

source§

fn index(&self, index: (usize, usize)) -> &Self::Output

Read a pixel at (x,y) location (e.g. px = img[(x,y)])

Coordinates may be outside width/height if the buffer has enough padding. The x coordinate can’t exceed stride.

§

type Output = Pixel

The returned type after indexing.
source§

impl<'a, Pixel: Copy> Index<(usize, usize)> for Img<Vec<Pixel>>

source§

fn index(&self, index: (usize, usize)) -> &Self::Output

Read a pixel at (x,y) location (e.g. px = img[(x,y)])

Coordinates may be outside width/height if the buffer has enough padding. The x coordinate can’t exceed stride.

§

type Output = Pixel

The returned type after indexing.
source§

impl<'a, Pixel: Copy> Index<usize> for Img<&'a [Pixel]>

source§

fn index(&self, row: usize) -> &Self::Output

Take n-th row as a slice. Same as .rows().nth(n).unwrap()

Slice length is guaranteed to equal image width. Row must be within image height.

§

type Output = [Pixel]

The returned type after indexing.
source§

impl<'a, Pixel: Copy> Index<usize> for Img<&'a mut [Pixel]>

source§

fn index(&self, row: usize) -> &Self::Output

Take n-th row as a slice. Same as .rows().nth(n).unwrap()

Slice length is guaranteed to equal image width. Row must be within image height.

§

type Output = [Pixel]

The returned type after indexing.
source§

impl<'a, Pixel: Copy> Index<usize> for Img<Vec<Pixel>>

source§

fn index(&self, row: usize) -> &Self::Output

Take n-th row as a slice. Same as .rows().nth(n).unwrap()

Slice length is guaranteed to equal image width. Row must be within image height.

§

type Output = [Pixel]

The returned type after indexing.
source§

impl<'a, Pixel: Copy> IndexMut<(u32, u32)> for Img<&'a mut [Pixel]>

source§

fn index_mut(&mut self, index: (u32, u32)) -> &mut Self::Output

Write a pixel at (x,y) location (e.g. img[(x,y)] = px)

Coordinates may be outside width/height if the buffer has enough padding. The x coordinate can’t exceed stride.

source§

impl<'a, Pixel: Copy> IndexMut<(u32, u32)> for Img<Vec<Pixel>>

source§

fn index_mut(&mut self, index: (u32, u32)) -> &mut Self::Output

Write a pixel at (x,y) location (e.g. img[(x,y)] = px)

Coordinates may be outside width/height if the buffer has enough padding. The x coordinate can’t exceed stride.

source§

impl<'a, Pixel: Copy> IndexMut<(usize, usize)> for Img<&'a mut [Pixel]>

source§

fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output

Write a pixel at (x,y) location (e.g. img[(x,y)] = px)

Coordinates may be outside width/height if the buffer has enough padding. The x coordinate can’t exceed stride.

source§

impl<'a, Pixel: Copy> IndexMut<(usize, usize)> for Img<Vec<Pixel>>

source§

fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output

Write a pixel at (x,y) location (e.g. img[(x,y)] = px)

Coordinates may be outside width/height if the buffer has enough padding. The x coordinate can’t exceed stride.

source§

impl<'a, Pixel: Copy> IndexMut<usize> for Img<&'a mut [Pixel]>

source§

fn index_mut(&mut self, row: usize) -> &mut Self::Output

Take n-th row as a mutable slice. Same as .rows().nth(n).unwrap()

Slice length is guaranteed to equal image width. Row must be within image height.

source§

impl<'a, Pixel: Copy> IndexMut<usize> for Img<Vec<Pixel>>

source§

fn index_mut(&mut self, row: usize) -> &mut Self::Output

Take n-th row as a mutable slice. Same as .rows().nth(n).unwrap()

Slice length is guaranteed to equal image width. Row must be within image height.

source§

impl<Container> IntoIterator for Img<Container>
where Container: IntoIterator,

Deprecated. Use .rows() or .pixels() iterators which are more predictable

source§

fn into_iter(self) -> Container::IntoIter

Deprecated. Use .rows() or .pixels() iterators which are more predictable

§

type Item = <Container as IntoIterator>::Item

The type of the elements being iterated over.
§

type IntoIter = <Container as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
source§

impl<'a, 'b, T, U> PartialEq<Img<&'b [U]>> for ImgRef<'a, T>
where T: PartialEq<U>,

source§

fn eq(&self, other: &ImgRef<'b, U>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, 'b, T, U> PartialEq<Img<&'b [U]>> for ImgRefMut<'a, T>
where T: PartialEq<U>,

source§

fn eq(&self, other: &ImgRef<'b, U>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, T, U> PartialEq<Img<&'a [U]>> for ImgVec<T>
where T: PartialEq<U>,

source§

fn eq(&self, other: &ImgRef<'a, U>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, 'b, T, U> PartialEq<Img<&'b mut [U]>> for ImgRef<'a, T>
where T: PartialEq<U>,

source§

fn eq(&self, other: &ImgRefMut<'b, U>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, 'b, T, U> PartialEq<Img<&'b mut [U]>> for ImgRefMut<'a, T>
where T: PartialEq<U>,

source§

fn eq(&self, other: &ImgRefMut<'b, U>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, T, U> PartialEq<Img<Vec<U>>> for ImgRef<'a, T>
where T: PartialEq<U>,

source§

fn eq(&self, other: &ImgVec<U>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T, U> PartialEq<Img<Vec<U>>> for ImgVec<T>
where T: PartialEq<U>,

source§

fn eq(&self, other: &ImgVec<U>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<Container: Copy> Copy for Img<Container>

Auto Trait Implementations§

§

impl<Container> Freeze for Img<Container>
where Container: Freeze,

§

impl<Container> RefUnwindSafe for Img<Container>
where Container: RefUnwindSafe,

§

impl<Container> Send for Img<Container>
where Container: Send,

§

impl<Container> Sync for Img<Container>
where Container: Sync,

§

impl<Container> Unpin for Img<Container>
where Container: Unpin,

§

impl<Container> UnwindSafe for Img<Container>
where Container: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.