Struct pix_engine::image::Image
source · [−]pub struct Image { /* private fields */ }
Expand description
An Image
representing a buffer of pixel color values.
Implementations
sourceimpl Image
impl Image
sourcepub fn new(width: u32, height: u32) -> Self
pub fn new(width: u32, height: u32) -> Self
Constructs an empty RGBA Image
with given width
and height
.
sourcepub fn with_rgba(width: u32, height: u32) -> Self
pub fn with_rgba(width: u32, height: u32) -> Self
Constructs an empty RGBA Image
with given width
and height
.
Alias for Image::new.
sourcepub fn with_rgb(width: u32, height: u32) -> Self
pub fn with_rgb(width: u32, height: u32) -> Self
Constructs an empty RGB Image
with given width
and height
.
sourcepub fn from_bytes<B: AsRef<[u8]>>(
width: u32,
height: u32,
bytes: B,
format: PixelFormat
) -> PixResult<Self>
pub fn from_bytes<B: AsRef<[u8]>>(
width: u32,
height: u32,
bytes: B,
format: PixelFormat
) -> PixResult<Self>
Constructs an Image
from a u8 slice representing RGB/A values.
Errors
If the bytes length doesn’t match the image dimensions and PixelFormat
provided, then
an error is returned.
sourcepub fn from_pixels<P: AsRef<[Color]>>(
width: u32,
height: u32,
pixels: P,
format: PixelFormat
) -> PixResult<Self>
pub fn from_pixels<P: AsRef<[Color]>>(
width: u32,
height: u32,
pixels: P,
format: PixelFormat
) -> PixResult<Self>
Constructs an Image
from a Color slice representing RGBA values.
Errors
If the pixels length doesn’t match the image dimensions and PixelFormat
provided, then
an error is returned.
sourcepub fn from_vec(
width: u32,
height: u32,
data: Vec<u8>,
format: PixelFormat
) -> Self
pub fn from_vec(
width: u32,
height: u32,
data: Vec<u8>,
format: PixelFormat
) -> Self
Constructs an Image
from a Vec<u8>
representing RGB/A values.
sourcepub const fn dimensions(&self) -> (u32, u32)
pub const fn dimensions(&self) -> (u32, u32)
Returns the Image
dimensions as (width, height)
.
sourcepub const fn pitch(&self) -> usize
pub const fn pitch(&self) -> usize
Returns the pitch
of the image data which is the number of bytes in a row of pixel data,
including padding between lines.
sourcepub fn bounding_rect(&self) -> Rect<i32>
pub fn bounding_rect(&self) -> Rect<i32>
sourcepub fn bounding_rect_offset<P>(&self, offset: P) -> Rect<i32> where
P: Into<Point<i32>>,
pub fn bounding_rect_offset<P>(&self, offset: P) -> Rect<i32> where
P: Into<Point<i32>>,
Returns the Image
bounding Rect positioned at offset
.
sourcepub fn bytes(&self) -> Bytes<'_>ⓘNotable traits for Bytes<'_>impl Iterator for Bytes<'_> type Item = u8;
pub fn bytes(&self) -> Bytes<'_>ⓘNotable traits for Bytes<'_>impl Iterator for Bytes<'_> type Item = u8;
Returns the Image
pixel data as an iterator of u8.
sourcepub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
sourcepub fn as_mut_bytes(&mut self) -> &mut [u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn as_mut_bytes(&mut self) -> &mut [u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
sourcepub fn into_bytes(self) -> Vec<u8>
pub fn into_bytes(self) -> Vec<u8>
Returns the Image
pixel data as a Vec<u8>
.
This consumes the Image
, so we do not need to copy its contents.
sourcepub fn pixels(&self) -> Pixels<'_>ⓘNotable traits for Pixels<'_>impl Iterator for Pixels<'_> type Item = Color;
pub fn pixels(&self) -> Pixels<'_>ⓘNotable traits for Pixels<'_>impl Iterator for Pixels<'_> type Item = Color;
Returns the Image
pixel data as an iterator of Colors.
sourcepub fn into_pixels(self) -> Vec<Color>
pub fn into_pixels(self) -> Vec<Color>
Returns the Image
pixel data as a Vec<Color>
.
Panics
Panics if the image has an invalid sequence of bytes given it’s PixelFormat
.
sourcepub fn get_pixel(&self, x: u32, y: u32) -> Color
pub fn get_pixel(&self, x: u32, y: u32) -> Color
Returns the color value at the given (x, y)
position.
Panics
Panics if the image has an invalid sequence of bytes given it’s PixelFormat
, or the (x, y
) index is out of range.
sourcepub fn set_pixel<C: Into<Color>>(&mut self, x: u32, y: u32, color: C)
pub fn set_pixel<C: Into<Color>>(&mut self, x: u32, y: u32, color: C)
Sets the color value at the given (x, y)
position.
sourcepub fn update_bytes<B: AsRef<[u8]>>(&mut self, bytes: B)
pub fn update_bytes<B: AsRef<[u8]>>(&mut self, bytes: B)
sourcepub const fn format(&self) -> PixelFormat
pub const fn format(&self) -> PixelFormat
Returns the Image
pixel format.
sourcepub fn save<P>(&self, path: P) -> PixResult<()> where
P: AsRef<Path>,
pub fn save<P>(&self, path: P) -> PixResult<()> where
P: AsRef<Path>,
Save the Image
to a png file.
Errors
Returns an error for any of the following:
- An io::Error
occurs attempting to create the png file.
- A png::EncodingError
occurs attempting to write image bytes.
Example
fn on_key_pressed(&mut self, s: &mut PixState, event: KeyEvent) -> PixResult<bool> {
if let Key::S = event.key {
self.image.save("test_image.png")?;
}
Ok(false)
}
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Image
impl Send for Image
impl Sync for Image
impl Unpin for Image
impl UnwindSafe for Image
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more