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
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§
source§impl<Container> Img<Container>
impl<Container> Img<Container>
sourcepub fn width(&self) -> usize
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()
sourcepub fn stride(&self) -> usize
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.
sourcepub fn buf(&self) -> &Container
pub fn buf(&self) -> &Container
Immutable reference to the pixel storage. Warning: exposes stride. Use pixels()
or rows()
instead.
See also into_contiguous_buf()
.
sourcepub fn buf_mut(&mut self) -> &mut Container
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§impl<'a, T> Img<&'a [T]>
impl<'a, T> Img<&'a [T]>
source§impl<'a, T: Clone> Img<&'a [T]>
impl<'a, T: Clone> Img<&'a [T]>
sourcepub fn to_contiguous_buf(&self) -> (Cow<'a, [T]>, usize, usize)
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]>
impl<'a, T> Img<&'a mut [T]>
sourcepub fn sub_image(
&'a mut self,
left: usize,
top: usize,
width: usize,
height: usize
) -> ImgRef<'a, T>
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§impl<'a, T: Copy> Img<&'a [T]>
impl<'a, T: Copy> Img<&'a [T]>
sourcepub fn pixels(&self) -> PixelsIter<'_, T> ⓘ
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]>
impl<'a, T> Img<&'a [T]>
sourcepub fn pixels_ref(&self) -> PixelsRefIter<'_, T> ⓘ
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]>
impl<'a, T: Copy> Img<&'a mut [T]>
sourcepub fn pixels(&self) -> PixelsIter<'_, T> ⓘ
pub fn pixels(&self) -> PixelsIter<'_, T> ⓘ
sourcepub fn pixels_mut(&mut self) -> PixelsIterMut<'_, T> ⓘ
pub fn pixels_mut(&mut self) -> PixelsIterMut<'_, T> ⓘ
source§impl<T: Copy> Img<Vec<T>>
impl<T: Copy> Img<Vec<T>>
sourcepub fn pixels(&self) -> PixelsIter<'_, T> ⓘ
pub fn pixels(&self) -> PixelsIter<'_, T> ⓘ
sourcepub fn pixels_mut(&mut self) -> PixelsIterMut<'_, T> ⓘ
pub fn pixels_mut(&mut self) -> PixelsIterMut<'_, T> ⓘ
source§impl<T> Img<Vec<T>>
impl<T> Img<Vec<T>>
sourcepub fn sub_image_mut(
&mut self,
left: usize,
top: usize,
width: usize,
height: usize
) -> ImgRefMut<'_, T>
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.
sourcepub fn sub_image(
&self,
left: usize,
top: usize,
width: usize,
height: usize
) -> ImgRef<'_, T>
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.
sourcepub fn as_ref(&self) -> ImgRef<'_, T>
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()
sourcepub fn as_mut(&mut self) -> ImgRefMut<'_, T>
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()
sourcepub fn rows(&self) -> RowsIter<'_, T> ⓘ
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()
)
sourcepub fn rows_mut(&mut self) -> RowsIterMut<'_, T> ⓘ
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>
impl<Container> Img<Container>
sourcepub fn new_stride(
buf: Container,
width: usize,
height: usize,
stride: usize
) -> Self
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.
sourcepub fn new(buf: Container, width: usize, height: usize) -> Self
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>>
impl<T: Copy> Img<Vec<T>>
sourcepub fn into_contiguous_buf(self) -> (Vec<T>, usize, usize)
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()
.
sourcepub fn as_contiguous_buf(&mut self) -> (&[T], usize, usize)
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>
impl<OldContainer> Img<OldContainer>
sourcepub fn map_buf<NewContainer, OldPixel, NewPixel, F>(
self,
callback: F
) -> Img<NewContainer>where
NewContainer: AsRef<[NewPixel]>,
OldContainer: AsRef<[OldPixel]>,
F: FnOnce(OldContainer) -> NewContainer,
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.
Trait Implementations§
source§impl<Pixel, Container> ImgExt<Pixel> for Img<Container>
impl<Pixel, Container> ImgExt<Pixel> for Img<Container>
source§fn rows_padded(&self) -> Chunks<'_, Pixel>
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
fn width_padded(&self) -> usize
source§fn height_padded(&self) -> usize
fn height_padded(&self) -> usize
source§impl<Container> IntoIterator for Img<Container>where
Container: IntoIterator,
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
fn into_iter(self) -> Container::IntoIter
Deprecated. Use .rows() or .pixels() iterators which are more predictable