pub struct Image { /* private fields */ }
Expand description
Loading, manipulating and saving images.
Implementations§
source§impl Image
impl Image
sourcepub fn new(width: u32, height: u32) -> Self
pub fn new(width: u32, height: u32) -> Self
Create an image
This image is filled with black pixels.
Arguments
- width - Width of the image
- height - Height of the image
sourcepub fn from_stream<T: Read + Seek>(stream: &mut T) -> Option<Self>
pub fn from_stream<T: Read + Seek>(stream: &mut T) -> Option<Self>
Create an image from a custom stream.
The supported image formats are bmp, png, tga, jpg, gif, psd, hdr and pic. Some format options are not supported, like progressive jpeg. If this function fails, the image is left unchanged.
Arguments
- stream - Your struct, implementing Read and Seek
Returns None
if loading fails
sourcepub fn from_memory(mem: &[u8]) -> Option<Self>
pub fn from_memory(mem: &[u8]) -> Option<Self>
Create an image from a file in memory
The supported image formats are bmp, png, tga, jpg, gif, psd, hdr and pic. Some format options are not supported, like progressive jpeg. If this function fails, the image is left unchanged.
Arguments
- mem - Pointer to the file data in memory
Returns None
if loading fails.
sourcepub fn from_color(width: u32, height: u32, color: Color) -> Option<Self>
pub fn from_color(width: u32, height: u32, color: Color) -> Option<Self>
Create an image and fill it with a unique color
Arguments
- width - Width of the image
- height - Height of the image
- color - Fill color
Returns None
if creation fails.
sourcepub fn from_file(filename: &str) -> Option<Self>
pub fn from_file(filename: &str) -> Option<Self>
Create an image from a file on disk
The supported image formats are bmp, png, tga, jpg, gif, psd, hdr and pic. Some format options are not supported, like progressive jpeg. If this function fails, the image is left unchanged.
Arguments
- filename - Path of the image file to load
Returns None
if loading fails
sourcepub unsafe fn create_from_pixels(
width: u32,
height: u32,
pixels: &[u8]
) -> Option<Self>
pub unsafe fn create_from_pixels(
width: u32,
height: u32,
pixels: &[u8]
) -> Option<Self>
Create an image from an vector of pixels
Arguments
- width - Width of the image
- height - Height of the image
- pixels - Vector of pixels to copy to the image
Returns None
if creation fails.
Safety
The pixel vector is assumed to contain 32-bits RGBA pixels, and have the given width and height. If not, this is an undefined behaviour.
sourcepub fn save_to_file(&self, filename: &str) -> bool
pub fn save_to_file(&self, filename: &str) -> bool
Save an image to a file on disk
The format of the image is automatically deduced from the extension. The supported image formats are bmp, png, tga and jpg. The destination file is overwritten if it already exists. This function fails if the image is empty.
Arguments
- filename - Path of the file to save
Return true if saving was successful
sourcepub fn create_mask_from_color(&self, color: Color, alpha: u8)
pub fn create_mask_from_color(&self, color: Color, alpha: u8)
Create a transparency mask from a specified color-key
This function sets the alpha value of every pixel matching the given color to alpha (0 by default), so that they become transparent.
Arguments
- color - Color to make transparent
- alpha - Alpha value to assign to transparent pixels
sourcepub unsafe fn pixel_at(&self, x: u32, y: u32) -> Color
pub unsafe fn pixel_at(&self, x: u32, y: u32) -> Color
Get the color of a pixel in an image
Arguments
- x - X coordinate of pixel to get
- y - Y coordinate of pixel to get
Return the Color of the pixel at coordinates (x, y)
Safety
This function doesn’t check the validity of the pixel coordinates, using out-of-range values will result in an undefined behaviour.
sourcepub fn pixel_data(&self) -> &[u8] ⓘ
pub fn pixel_data(&self) -> &[u8] ⓘ
Return the memory buffer of this image.
sourcepub fn flip_horizontally(&mut self)
pub fn flip_horizontally(&mut self)
Flip an image horizontally (left <-> right)
sourcepub fn flip_vertically(&mut self)
pub fn flip_vertically(&mut self)
Flip an image vertically (top <-> bottom)
sourcepub fn copy_image(
&mut self,
source: &Image,
dest_x: u32,
dest_y: u32,
source_rect: IntRect,
apply_alpha: bool
)
pub fn copy_image(
&mut self,
source: &Image,
dest_x: u32,
dest_y: u32,
source_rect: IntRect,
apply_alpha: bool
)
Copy pixels from an image onto another
This function does a slow pixel copy and should not be
used intensively. It can be used to prepare a complex
static image from several others, but if you need this
kind of feature in real-time you’d better use RenderTexture
.
If sourceRect is empty, the whole image is copied. If applyAlpha is set to true, the transparency of source pixels is applied. If it is false, the pixels are copied unchanged with their alpha value.
Arguments
- source - Source image to copy
- destX - X coordinate of the destination position
- destY - Y coordinate of the destination position
- sourceRect - Sub-rectangle of the source image to copy
- applyAlpha - Should the copy take in account the source transparency?