Struct Img

struct Img<Container> { ... }

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

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

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

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

Height of the image in pixels.

Implementations

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

fn to_contiguous_buf(self: &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()

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

fn sub_image(self: &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.)

fn rows(self: &Self) -> RowsIter<'slice, T>

Iterate over whole rows of pixels as slices

Panics

If stride is 0

See also pixels()

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

fn pixels_ref(self: &Self) -> PixelsRefIter<'slice, 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

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

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

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

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

Slices this image reference to produce another reference to a subregion of it.

Note that mutable borrows are exclusive, so it's not possible to have more than one mutable subimage at a time.

Panics

If the coordinates are out of bounds

fn into_sub_image_mut(self: Self, left: usize, top: usize, width: usize, height: usize) -> Self

Transforms this image reference to refer to a subregion.

This is identical in behavior to [ImgRefMut::sub_image_mut()], except that it returns an ImgRefMut with the same lifetime, rather than a reborrow with a shorter lifetime.

Panics

If the coordinates are out of bounds

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

Make mutable reference immutable

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

fn pixels(self: &Self) -> PixelsIter<'slice, 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

impl<Container> Img<Container>

const fn width(self: &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()

const fn height(self: &Self) -> usize

Height of the image in pixels.

const fn stride(self: &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.

const fn buf(self: &Self) -> &Container

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

See also into_contiguous_buf().

fn buf_mut(self: &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().

fn into_buf(self: Self) -> Container

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

impl<Container> Img<Container>

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.

Panics

If stride is 0.

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.

impl<OldContainer> Img<OldContainer>

fn map_buf<NewContainer, OldPixel, NewPixel, F>(self: 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.

fn new_buf<NewContainer, OldPixel, NewPixel>(self: &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.

impl<T> Img<&mut [T]>

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

Iterate over whole rows as slices

Panics

if stride is 0

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

Iterate over whole rows as slices

Panics

if stride is 0

impl<T> Img<&mut [T]>

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

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

Panics

if width is 0

impl<T> Img<Vec<T>>

fn sub_image_mut(self: &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.

Panics

If the coordinates are out of bounds

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

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

fn as_ref(self: &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()

fn as_mut(self: &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()

fn rows(self: &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())

fn rows_mut(self: &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())

impl<T> Img<Vec<T>>

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

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

Panics

if width is 0

impl<T> Img<T>

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

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

See also to_contiguous_buf().0.into_owned()

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

fn into_owned(self: Self) -> ImgVec<T>

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

See also to_contiguous_buf().0.into_owned()

impl<T: Copy> Img<&mut [T]>

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

Panics

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

if width is 0

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

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

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

Panics

if width is 0

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

fn into_contiguous_buf(self: 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().

fn as_contiguous_buf(self: &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().

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

fn index(self: &Self, index: (u32, u32)) -> &<Self as >::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.

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

fn index(self: &Self, row: usize) -> &<Self as >::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.

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

fn index(self: &Self, index: (usize, usize)) -> &<Self as >::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.

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

fn index(self: &Self, index: (u32, u32)) -> &<Self as >::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.

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

fn index(self: &Self, index: (usize, usize)) -> &<Self as >::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.

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

fn index(self: &Self, row: usize) -> &<Self as >::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.

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

fn index(self: &Self, index: (u32, u32)) -> &<Self as >::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.

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

fn index(self: &Self, index: (usize, usize)) -> &<Self as >::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.

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

fn index(self: &Self, row: usize) -> &<Self as >::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.

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

fn index_mut(self: &mut Self, index: (u32, u32)) -> &mut <Self as >::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.

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

fn index_mut(self: &mut Self, index: (usize, usize)) -> &mut <Self as >::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.

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

fn index_mut(self: &mut Self, row: usize) -> &mut <Self as >::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.

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

fn index_mut(self: &mut Self, row: usize) -> &mut <Self as >::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.

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

fn index_mut(self: &mut Self, index: (u32, u32)) -> &mut <Self as >::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.

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

fn index_mut(self: &mut Self, index: (usize, usize)) -> &mut <Self as >::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.

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

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

impl<Container> Freeze for Img<Container>

impl<Container> IntoIterator for Img<Container>

fn into_iter(self: Self) -> <Container as >::IntoIter

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

impl<Container> RefUnwindSafe for Img<Container>

impl<Container> Send for Img<Container>

impl<Container> Sync for Img<Container>

impl<Container> Unpin for Img<Container>

impl<Container> UnsafeUnpin for Img<Container>

impl<Container> UnwindSafe for Img<Container>

impl<Container: $crate::clone::Clone> Clone for Img<Container>

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

impl<Container: $crate::fmt::Debug> Debug for Img<Container>

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

impl<Container: $crate::marker::Copy> Copy for Img<Container>

impl<Pixel, Container> ImgExt for Img<Container>

fn width_padded(self: &Self) -> usize
fn height_padded(self: &Self) -> usize
fn rows_padded(self: &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.

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

impl<Pixel, Container> ImgExtMut for Img<Container>

fn rows_padded_mut(self: &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

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

impl<T> Any for Img<Container>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Img<Container>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Img<Container>

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

impl<T> CloneToUninit for Img<Container>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Img<Container>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Img<Container>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for Img<Container>

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Img<Container>

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

impl<T, U> TryInto for Img<Container>

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

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

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

impl<T: Clone> From for Img<Vec<T>>

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