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: ContainerStorage for the pixels. Usually
Vec<Pixel>or&[Pixel]. SeeImgVecandImgRef.Note that future version will make this field private. Use
.rows()and.pixels()iterators where possible, orbuf()/buf_mut()/into_buf().stride: usizeNumber of pixels to skip in the container to advance to the next row.
Note: pixels between
widthandstridemay not be usable, and may not even exist in the last row.width: u32Width of the image in pixels.
Note that this isn't same as the width of the row in the
buf, seestrideheight: u32Height 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*heightelements 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) -> SelfMake a reference for a part of the image, without copying any pixels.
Panics
It will panic if
sub_imageis 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*heightpixels in theImg, by reference, ignoring padding areaIf 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) -> SelfTransforms this image reference to refer to a subregion.
This is identical in behavior to [
ImgRefMut::sub_image_mut()], except that it returns anImgRefMutwith 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*heightpixels in theImg, ignoring padding areaIf you want to iterate in parallel, parallelize
rows()instead.Panics
if width is 0
impl<Container> Img<Container>
const fn width(self: &Self) -> usizeWidth 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) -> usizeHeight of the image in pixels.
const fn stride(self: &Self) -> usizeNumber 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) -> &ContainerImmutable reference to the pixel storage. Warning: exposes stride. Use
pixels()orrows()instead.See also
into_contiguous_buf().fn buf_mut(self: &mut Self) -> &mut ContainerMutable reference to the pixel storage. Warning: exposes stride. Use
pixels_mut()orrows_mut()instead.See also
into_contiguous_buf().fn into_buf(self: Self) -> ContainerGet 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) -> SelfSame as
new(), except each row is locatedstridenumber of pixels after the previous one.Stride can be equal to
widthor 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
Containeris usually aVecor a slice.Panics
If stride is 0.
fn new(buf: Container, width: usize, height: usize) -> SelfCreate new image with
Container(which can beVec,&[]or something else) with givenwidthandheightin pixels.Assumes the pixels in container are contiguous, layed out row by row with
widthpixels per row and at leastheightrows.If the container is larger than
width×heightpixels, 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) -> NewContainerA 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()andsub_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()androws_mut()fn rows(self: &Self) -> RowsIter<'_, T>Iterate over rows of the image as slices
Each slice is guaranteed to be exactly
widthpixels 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
widthpixels 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*heightelements 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*heightelements 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 >::OutputRead a pixel at
(x,y)location (e.g. px =img[(x,y)])Coordinates may be outside
width/heightif the buffer has enough padding. The x coordinate can't exceedstride.
impl<'a, Pixel: Copy> Index for Img<&'a [Pixel]>
fn index(self: &Self, row: usize) -> &<Self as >::OutputTake 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 >::OutputRead a pixel at
(x,y)location (e.g. px =img[(x,y)])Coordinates may be outside
width/heightif the buffer has enough padding. The x coordinate can't exceedstride.
impl<'a, Pixel: Copy> Index for Img<&'a mut [Pixel]>
fn index(self: &Self, index: (u32, u32)) -> &<Self as >::OutputRead a pixel at
(x,y)location (e.g. px =img[(x,y)])Coordinates may be outside
width/heightif the buffer has enough padding. The x coordinate can't exceedstride.
impl<'a, Pixel: Copy> Index for Img<&'a mut [Pixel]>
fn index(self: &Self, index: (usize, usize)) -> &<Self as >::OutputRead a pixel at
(x,y)location (e.g. px =img[(x,y)])Coordinates may be outside
width/heightif the buffer has enough padding. The x coordinate can't exceedstride.
impl<'a, Pixel: Copy> Index for Img<&'a mut [Pixel]>
fn index(self: &Self, row: usize) -> &<Self as >::OutputTake 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 >::OutputRead a pixel at
(x,y)location (e.g. px =img[(x,y)])Coordinates may be outside
width/heightif the buffer has enough padding. The x coordinate can't exceedstride.
impl<'a, Pixel: Copy> Index for Img<Vec<Pixel>>
fn index(self: &Self, index: (usize, usize)) -> &<Self as >::OutputRead a pixel at
(x,y)location (e.g. px =img[(x,y)])Coordinates may be outside
width/heightif the buffer has enough padding. The x coordinate can't exceedstride.
impl<'a, Pixel: Copy> Index for Img<Vec<Pixel>>
fn index(self: &Self, row: usize) -> &<Self as >::OutputTake 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 >::OutputWrite a pixel at
(x,y)location (e.g.img[(x,y)] = px)Coordinates may be outside
width/heightif the buffer has enough padding. The x coordinate can't exceedstride.
impl<'a, Pixel: Copy> IndexMut for Img<&'a mut [Pixel]>
fn index_mut(self: &mut Self, index: (usize, usize)) -> &mut <Self as >::OutputWrite a pixel at
(x,y)location (e.g.img[(x,y)] = px)Coordinates may be outside
width/heightif the buffer has enough padding. The x coordinate can't exceedstride.
impl<'a, Pixel: Copy> IndexMut for Img<&'a mut [Pixel]>
fn index_mut(self: &mut Self, row: usize) -> &mut <Self as >::OutputTake 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 >::OutputTake 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 >::OutputWrite a pixel at
(x,y)location (e.g.img[(x,y)] = px)Coordinates may be outside
width/heightif the buffer has enough padding. The x coordinate can't exceedstride.
impl<'a, Pixel: Copy> IndexMut for Img<Vec<Pixel>>
fn index_mut(self: &mut Self, index: (usize, usize)) -> &mut <Self as >::OutputWrite a pixel at
(x,y)location (e.g.img[(x,y)] = px)Coordinates may be outside
width/heightif the buffer has enough padding. The x coordinate can't exceedstride.
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 >::IntoIterDeprecated. 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) -> usizefn height_padded(self: &Self) -> usizefn rows_padded(self: &Self) -> Chunks<'_, Pixel>Iterate over the entire buffer as rows, including all padding
Rows will have up to
stridewidth, 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
stridewidth, 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) -> TReturns the argument unchanged.
impl<T> ToOwned for Img<Container>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for Img<Container>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses 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