pub struct Image(/* private fields */);Expand description
CPU-side pixel buffer.
An Image holds raw pixel data in system RAM. It is the starting point for most
texture-loading workflows: generate or load pixels on the CPU, manipulate them
(crop, resize, draw into, colour-fill, etc.), then upload to the GPU as a
Texture2D via RaylibHandle::load_texture_from_image.
Image is automatically freed via UnloadImage when it goes out of scope.
§Examples
Generate a solid-colour image and read a pixel back (no window required):
use raylib::prelude::*;
let img = Image::gen_image_color(64, 64, Color::RED);
assert_eq!(img.width(), 64);
assert_eq!(img.height(), 64);
let pixel = img.get_color(0, 0);
assert_eq!(pixel.r, 255);
assert_eq!(pixel.g, 0);
assert_eq!(pixel.b, 0);Implementations§
Source§impl Image
impl Image
Sourcepub unsafe fn from_raw(raw: Image) -> Self
pub unsafe fn from_raw(raw: Image) -> Self
converts raylib-sys object to a “safe” version. Make sure to call this function from the thread the resource was created.
§Safety
The caller must ensure raw is a valid, fully initialized raylib object
obtained from a raylib load function. Ownership is transferred to the
returned wrapper, which will call the appropriate unload function on drop.
Source§impl Image
impl Image
Sourcepub unsafe fn data(&self) -> *mut c_void
pub unsafe fn data(&self) -> *mut c_void
Image raw data
§Safety
The returned pointer is only valid for the lifetime of self and the image’s backing
buffer. The caller must not use the pointer after the image is dropped or reallocated.
Sourcepub fn blur_gaussian(&mut self, blur_size: i32)
pub fn blur_gaussian(&mut self, blur_size: i32)
Apply Gaussian blur using a box blur approximation
Sourcepub fn draw_circle_lines(
&mut self,
center_x: i32,
center_y: i32,
radius: i32,
color: Color,
)
pub fn draw_circle_lines( &mut self, center_x: i32, center_y: i32, radius: i32, color: Color, )
Draw circle outline within an image
Sourcepub fn draw_circle_lines_v(
&mut self,
center: impl Into<Vector2>,
center_y: i32,
color: Color,
)
pub fn draw_circle_lines_v( &mut self, center: impl Into<Vector2>, center_y: i32, color: Color, )
Draw circle outline within an image (Vector version)
Sourcepub fn format(&self) -> PixelFormat
pub fn format(&self) -> PixelFormat
Data format (PixelFormat type)
Sourcepub fn from_image(&self, rec: impl Into<Rectangle>) -> Image
pub fn from_image(&self, rec: impl Into<Rectangle>) -> Image
Create an image from another image piece
Sourcepub fn from_channel(&self, selected_channel: i32) -> Image
pub fn from_channel(&self, selected_channel: i32) -> Image
Create an image from a selected channel of another image (GRAYSCALE)
Sourcepub fn export_image(&self, filename: &str)
pub fn export_image(&self, filename: &str)
Exports image as a PNG file.
Sourcepub fn export_image_as_code(&self, filename: &str)
pub fn export_image_as_code(&self, filename: &str)
Exports image as a PNG file.
Sourcepub fn get_pixel_data_size(&self) -> usize
pub fn get_pixel_data_size(&self) -> usize
Get pixel data size in bytes (image or texture)
Sourcepub fn get_image_data(&self) -> ImageColors
pub fn get_image_data(&self) -> ImageColors
Gets pixel data from image as a Vec of Color structs.
Sourcepub fn get_image_data_u8(&self, flip: bool) -> Vec<u8> ⓘ
pub fn get_image_data_u8(&self, flip: bool) -> Vec<u8> ⓘ
Gets pixel data from image as a Vec of Color structs.
Sourcepub fn extract_palette(&self, max_palette_size: u32) -> ImagePalette
pub fn extract_palette(&self, max_palette_size: u32) -> ImagePalette
Extract color palette from image to maximum size
Sourcepub fn set_format(&mut self, new_format: PixelFormat)
pub fn set_format(&mut self, new_format: PixelFormat)
Converts image data to desired pixel format.
Sourcepub fn alpha_mask(&mut self, alpha_mask: &Image)
pub fn alpha_mask(&mut self, alpha_mask: &Image)
Applies alpha mask to image.
Alpha mask must be same size as the image. If alpha mask is not greyscale
Ensure the colors are white (255, 255, 255, 255) or black (0, 0, 0, 0)
Sourcepub fn alpha_clear(&mut self, color: impl Into<Color>, threshold: f32)
pub fn alpha_clear(&mut self, color: impl Into<Color>, threshold: f32)
Clears alpha channel on image to desired color.
Sourcepub fn alpha_crop(&mut self, threshold: f32)
pub fn alpha_crop(&mut self, threshold: f32)
Crops image depending on alpha value.
Sourcepub fn alpha_premultiply(&mut self)
pub fn alpha_premultiply(&mut self)
Premultiplies alpha channel on image.
Sourcepub fn resize_nn(&mut self, new_width: i32, new_height: i32)
pub fn resize_nn(&mut self, new_width: i32, new_height: i32)
Resizes image (nearest-neighbor scaling).
Sourcepub fn resize_canvas(
&mut self,
new_width: i32,
new_height: i32,
offset_x: i32,
offset_y: i32,
color: impl Into<Color>,
)
pub fn resize_canvas( &mut self, new_width: i32, new_height: i32, offset_x: i32, offset_y: i32, color: impl Into<Color>, )
Resizes image canvas and fills with color.
Sourcepub fn gen_mipmaps(&mut self)
pub fn gen_mipmaps(&mut self)
Generates all mipmap levels for a provided image.
Sourcepub fn dither(&mut self, r_bpp: i32, g_bpp: i32, b_bpp: i32, a_bpp: i32)
pub fn dither(&mut self, r_bpp: i32, g_bpp: i32, b_bpp: i32, a_bpp: i32)
Dithers image data to 16bpp or lower (Floyd-Steinberg dithering).
Sourcepub fn get_image_alpha_border(&self, threshold: f32) -> Rectangle
pub fn get_image_alpha_border(&self, threshold: f32) -> Rectangle
Get image alpha border rectangle
Sourcepub fn clear_background(&mut self, color: impl Into<Color>)
pub fn clear_background(&mut self, color: impl Into<Color>)
Clear image background with given color
Sourcepub fn draw(
&mut self,
src: &Image,
src_rec: Rectangle,
dst_rec: Rectangle,
tint: impl Into<Color>,
)
pub fn draw( &mut self, src: &Image, src_rec: Rectangle, dst_rec: Rectangle, tint: impl Into<Color>, )
Draws a source image within a destination image.
Sourcepub fn draw_pixel(&mut self, pos_x: i32, pos_y: i32, color: impl Into<Color>)
pub fn draw_pixel(&mut self, pos_x: i32, pos_y: i32, color: impl Into<Color>)
Draw pixel within an image
Sourcepub fn draw_pixel_v(
&mut self,
position: impl Into<Vector2>,
color: impl Into<Color>,
)
pub fn draw_pixel_v( &mut self, position: impl Into<Vector2>, color: impl Into<Color>, )
Draw pixel within an image (Vector version)
Sourcepub fn draw_line(
&mut self,
start_pos_x: i32,
start_pos_y: i32,
end_pos_x: i32,
end_pos_y: i32,
color: impl Into<Color>,
)
pub fn draw_line( &mut self, start_pos_x: i32, start_pos_y: i32, end_pos_x: i32, end_pos_y: i32, color: impl Into<Color>, )
Draw line within an image
Sourcepub fn draw_line_ex(
&mut self,
start_pos: impl Into<Vector2>,
end_pos: impl Into<Vector2>,
thick: i32,
color: impl Into<Color>,
)
pub fn draw_line_ex( &mut self, start_pos: impl Into<Vector2>, end_pos: impl Into<Vector2>, thick: i32, color: impl Into<Color>, )
Draw a line (using triangles/quads)
Sourcepub fn draw_line_v(
&mut self,
start: impl Into<Vector2>,
end: impl Into<Vector2>,
color: impl Into<Color>,
)
pub fn draw_line_v( &mut self, start: impl Into<Vector2>, end: impl Into<Vector2>, color: impl Into<Color>, )
Draw line within an image (Vector version)
Sourcepub fn draw_triangle(
&mut self,
v1: impl Into<Vector2>,
v2: impl Into<Vector2>,
v3: impl Into<Vector2>,
color: impl Into<Color>,
)
pub fn draw_triangle( &mut self, v1: impl Into<Vector2>, v2: impl Into<Vector2>, v3: impl Into<Vector2>, color: impl Into<Color>, )
Draw triangle within an image
Sourcepub fn draw_triangle_ex(
&mut self,
v1: impl Into<Vector2>,
v2: impl Into<Vector2>,
v3: impl Into<Vector2>,
c1: impl Into<Color>,
c2: impl Into<Color>,
c3: impl Into<Color>,
)
pub fn draw_triangle_ex( &mut self, v1: impl Into<Vector2>, v2: impl Into<Vector2>, v3: impl Into<Vector2>, c1: impl Into<Color>, c2: impl Into<Color>, c3: impl Into<Color>, )
Draw triangle with interpolated colors within an image
Sourcepub fn draw_triangle_lines(
&mut self,
v1: impl Into<Vector2>,
v2: impl Into<Vector2>,
v3: impl Into<Vector2>,
color: impl Into<Color>,
)
pub fn draw_triangle_lines( &mut self, v1: impl Into<Vector2>, v2: impl Into<Vector2>, v3: impl Into<Vector2>, color: impl Into<Color>, )
Draw triangle outline within an image
Sourcepub fn draw_triangle_fan(
&mut self,
points: &mut [Vector2],
color: impl Into<Color>,
)
pub fn draw_triangle_fan( &mut self, points: &mut [Vector2], color: impl Into<Color>, )
Draw a triangle fan defined by points within an image (first vertex is the center)
Sourcepub fn draw_triangle_strip(
&mut self,
points: &mut [Vector2],
color: impl Into<Color>,
)
pub fn draw_triangle_strip( &mut self, points: &mut [Vector2], color: impl Into<Color>, )
Draw a triangle strip defined by points within an image
Sourcepub fn draw_circle(
&mut self,
center_x: i32,
center_y: i32,
radius: i32,
color: impl Into<Color>,
)
pub fn draw_circle( &mut self, center_x: i32, center_y: i32, radius: i32, color: impl Into<Color>, )
Draw circle within an image
Sourcepub fn draw_circle_v(
&mut self,
center: impl Into<Vector2>,
radius: i32,
color: impl Into<Color>,
)
pub fn draw_circle_v( &mut self, center: impl Into<Vector2>, radius: i32, color: impl Into<Color>, )
Draw circle within an image (Vector version)
Sourcepub fn draw_rectangle(
&mut self,
pos_x: i32,
pos_y: i32,
width: i32,
height: i32,
color: impl Into<Color>,
)
pub fn draw_rectangle( &mut self, pos_x: i32, pos_y: i32, width: i32, height: i32, color: impl Into<Color>, )
Draws a rectangle within an image.
Sourcepub fn draw_rectangle_v(
&mut self,
position: impl Into<Vector2>,
size: impl Into<Vector2>,
color: impl Into<Color>,
)
pub fn draw_rectangle_v( &mut self, position: impl Into<Vector2>, size: impl Into<Vector2>, color: impl Into<Color>, )
Draw rectangle within an image (Vector version)
Sourcepub fn draw_rectangle_rec(
&mut self,
rectangle: impl Into<Rectangle>,
color: impl Into<Color>,
)
pub fn draw_rectangle_rec( &mut self, rectangle: impl Into<Rectangle>, color: impl Into<Color>, )
Draw rectangle within an image (Rectangle version)
Sourcepub fn draw_rectangle_lines(
&mut self,
rec: Rectangle,
thickness: i32,
color: impl Into<Color>,
)
pub fn draw_rectangle_lines( &mut self, rec: Rectangle, thickness: i32, color: impl Into<Color>, )
Draws a rectangle within an image.
Sourcepub fn draw_text(
&mut self,
text: &str,
pos_x: i32,
pos_y: i32,
font_size: i32,
color: impl Into<Color>,
)
pub fn draw_text( &mut self, text: &str, pos_x: i32, pos_y: i32, font_size: i32, color: impl Into<Color>, )
Draws text (default font) within an image (destination).
Sourcepub fn draw_text_ex(
&mut self,
font: impl AsRef<Font>,
text: &str,
position: impl Into<Vector2>,
font_size: f32,
spacing: f32,
color: impl Into<Color>,
)
pub fn draw_text_ex( &mut self, font: impl AsRef<Font>, text: &str, position: impl Into<Vector2>, font_size: f32, spacing: f32, color: impl Into<Color>, )
Draws text (default font) within an image (destination).
Sourcepub fn flip_vertical(&mut self)
pub fn flip_vertical(&mut self)
Flips image vertically.
Sourcepub fn flip_horizontal(&mut self)
pub fn flip_horizontal(&mut self)
Flips image horizontally.
Sourcepub fn rotate_ccw(&mut self)
pub fn rotate_ccw(&mut self)
Rotates image counterclockwise by 90 degrees (PI/2 radians).
Sourcepub fn color_tint(&mut self, color: impl Into<Color>)
pub fn color_tint(&mut self, color: impl Into<Color>)
Tints colors in image using specified color.
Sourcepub fn color_invert(&mut self)
pub fn color_invert(&mut self)
Inverts the colors in image.
Sourcepub fn color_grayscale(&mut self)
pub fn color_grayscale(&mut self)
Converts `image color to grayscale.
Sourcepub fn color_contrast(&mut self, contrast: f32)
pub fn color_contrast(&mut self, contrast: f32)
Adjusts the contrast of image.
Sourcepub fn color_brightness(&mut self, brightness: i32)
pub fn color_brightness(&mut self, brightness: i32)
Adjusts the brightness of image.
Sourcepub fn color_replace(
&mut self,
color: impl Into<Color>,
replace: impl Into<Color>,
)
pub fn color_replace( &mut self, color: impl Into<Color>, replace: impl Into<Color>, )
Searches image for all occurrences of color and replaces them with replace color.
Sourcepub fn export_image_to_memory(
&self,
file_type: &str,
) -> Result<DataBuf<[u8]>, InvalidImageError>
pub fn export_image_to_memory( &self, file_type: &str, ) -> Result<DataBuf<[u8]>, InvalidImageError>
Export image to memory buffer.
Sourcepub fn kernel_convolution(
&mut self,
kernel: &[f32],
) -> Result<(), InvalidImageError>
pub fn kernel_convolution( &mut self, kernel: &[f32], ) -> Result<(), InvalidImageError>
Apply custom square convolution kernel to image NOTE: The convolution kernel matrix is expected to be square
Sourcepub fn load_image(filename: &str) -> Result<Image, InvalidImageError>
pub fn load_image(filename: &str) -> Result<Image, InvalidImageError>
Loads an image from a file path into CPU memory.
Supported formats depend on which SUPPORT_FILEFORMAT_* features were compiled in.
With the full feature, PNG, BMP, TGA, JPG, GIF, QOI, PSD, DDS, HDR, PIC, PNM,
KTX, ASTC, PKM, and PVR are all available.
To display the image, upload it to the GPU with RaylibHandle::load_texture_from_image.
Sourcepub fn load_image_from_mem(
filetype: &str,
bytes: &[u8],
) -> Result<Image, InvalidImageError>
pub fn load_image_from_mem( filetype: &str, bytes: &[u8], ) -> Result<Image, InvalidImageError>
Loads image from a given memory buffer The input data is expected to be in a supported file format such as png. Which formats are supported depend on the build flags used for the raylib (C) library.
Sourcepub fn load_image_anim(filename: &str, frame_num: &mut i32) -> Self
pub fn load_image_anim(filename: &str, frame_num: &mut i32) -> Self
Load image sequence from file, with the number of frames loaded saved to frame_num. Image.data buffer includes all frames. All frames returned are in RGBA format. Frames delay data is discarded
Sourcepub fn load_image_anim_from_memory(
filetype: &str,
data: &[u8],
frame_num: &mut i32,
) -> Self
pub fn load_image_anim_from_memory( filetype: &str, data: &[u8], frame_num: &mut i32, ) -> Self
Load image from memory buffer, with the number of frames loaded saved to frame_num. fileType refers to extension: i.e. “.png”. File extension must be provided in lower-case
Sourcepub fn load_image_raw(
filename: &str,
width: i32,
height: i32,
format: i32,
header_size: i32,
) -> Result<Image, InvalidImageError>
pub fn load_image_raw( filename: &str, width: i32, height: i32, format: i32, header_size: i32, ) -> Result<Image, InvalidImageError>
Loads image from RAW file data.
Sourcepub fn image_text(text: &str, font_size: i32, color: impl Into<Color>) -> Image
pub fn image_text(text: &str, font_size: i32, color: impl Into<Color>) -> Image
Creates an image from text (custom font).
Sourcepub fn image_text_ex(
font: impl AsRef<Font>,
text: &str,
font_size: f32,
spacing: f32,
tint: impl Into<Color>,
) -> Image
pub fn image_text_ex( font: impl AsRef<Font>, text: &str, font_size: f32, spacing: f32, tint: impl Into<Color>, ) -> Image
Creates an image from text (custom font).
Sourcepub fn is_image_valid(&self) -> bool
pub fn is_image_valid(&self) -> bool
Check if an image is valid (data and parameters)