Skip to main content

Image

Struct Image 

Source
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

Source

pub unsafe fn unwrap(self) -> Image

Take the raw ffi type. Must manually free memory by calling the proper unload function

§Safety

The caller is responsible for freeing the returned value by calling the appropriate raylib unload function. Failure to do so will leak resources.

Source§

impl Image

Source

pub fn to_raw(self) -> Image

returns the unwrapped raylib-sys object

Source

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

Source

pub fn width(&self) -> i32

Image base width

Source

pub fn height(&self) -> i32

Image base height

Source

pub fn mipmaps(&self) -> i32

Mipmap levels, 1 by default

Source

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.

Source

pub fn blur_gaussian(&mut self, blur_size: i32)

Apply Gaussian blur using a box blur approximation

Source

pub fn rotate(&mut self, degrees: i32)

Rotate image by input angle in degrees (-359 to 359)

Source

pub fn get_color(&self, x: i32, y: i32) -> Color

Get image pixel color at (x, y) position

Source

pub fn draw_circle_lines( &mut self, center_x: i32, center_y: i32, radius: i32, color: Color, )

Draw circle outline within an image

Source

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)

Source

pub fn format(&self) -> PixelFormat

Data format (PixelFormat type)

Source

pub fn from_image(&self, rec: impl Into<Rectangle>) -> Image

Create an image from another image piece

Source

pub fn from_channel(&self, selected_channel: i32) -> Image

Create an image from a selected channel of another image (GRAYSCALE)

Source

pub fn export_image(&self, filename: &str)

Exports image as a PNG file.

Source

pub fn export_image_as_code(&self, filename: &str)

Exports image as a PNG file.

Source

pub fn get_pixel_data_size(&self) -> usize

Get pixel data size in bytes (image or texture)

Source

pub fn get_image_data(&self) -> ImageColors

Gets pixel data from image as a Vec of Color structs.

Source

pub fn get_image_data_u8(&self, flip: bool) -> Vec<u8>

Gets pixel data from image as a Vec of Color structs.

Source

pub fn extract_palette(&self, max_palette_size: u32) -> ImagePalette

Extract color palette from image to maximum size

Source

pub fn to_pot(&mut self, fill_color: impl Into<Color>)

Converts image to POT (power-of-two).

Source

pub fn set_format(&mut self, new_format: PixelFormat)

Converts image data to desired pixel format.

Source

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)

Source

pub fn alpha_clear(&mut self, color: impl Into<Color>, threshold: f32)

Clears alpha channel on image to desired color.

Source

pub fn alpha_crop(&mut self, threshold: f32)

Crops image depending on alpha value.

Source

pub fn alpha_premultiply(&mut self)

Premultiplies alpha channel on image.

Source

pub fn crop(&mut self, crop: impl Into<Rectangle>)

Crops image to a defined rectangle.

Source

pub fn resize(&mut self, new_width: i32, new_height: i32)

Resizes image (bilinear filtering).

Source

pub fn resize_nn(&mut self, new_width: i32, new_height: i32)

Resizes image (nearest-neighbor scaling).

Source

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.

Source

pub fn gen_mipmaps(&mut self)

Generates all mipmap levels for a provided image.

Source

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).

Source

pub fn get_image_alpha_border(&self, threshold: f32) -> Rectangle

Get image alpha border rectangle

Source

pub fn clear_background(&mut self, color: impl Into<Color>)

Clear image background with given color

Source

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.

Source

pub fn draw_pixel(&mut self, pos_x: i32, pos_y: i32, color: impl Into<Color>)

Draw pixel within an image

Source

pub fn draw_pixel_v( &mut self, position: impl Into<Vector2>, color: impl Into<Color>, )

Draw pixel within an image (Vector version)

Source

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

Source

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)

Source

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)

Source

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

Source

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

Source

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

Source

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)

Source

pub fn draw_triangle_strip( &mut self, points: &mut [Vector2], color: impl Into<Color>, )

Draw a triangle strip defined by points within an image

Source

pub fn draw_circle( &mut self, center_x: i32, center_y: i32, radius: i32, color: impl Into<Color>, )

Draw circle within an image

Source

pub fn draw_circle_v( &mut self, center: impl Into<Vector2>, radius: i32, color: impl Into<Color>, )

Draw circle within an image (Vector version)

Source

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.

Source

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)

Source

pub fn draw_rectangle_rec( &mut self, rectangle: impl Into<Rectangle>, color: impl Into<Color>, )

Draw rectangle within an image (Rectangle version)

Source

pub fn draw_rectangle_lines( &mut self, rec: Rectangle, thickness: i32, color: impl Into<Color>, )

Draws a rectangle within an image.

Source

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).

Source

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).

Source

pub fn flip_vertical(&mut self)

Flips image vertically.

Source

pub fn flip_horizontal(&mut self)

Flips image horizontally.

Source

pub fn rotate_cw(&mut self)

Rotates image clockwise by 90 degrees (PI/2 radians).

Source

pub fn rotate_ccw(&mut self)

Rotates image counterclockwise by 90 degrees (PI/2 radians).

Source

pub fn color_tint(&mut self, color: impl Into<Color>)

Tints colors in image using specified color.

Source

pub fn color_invert(&mut self)

Inverts the colors in image.

Source

pub fn color_grayscale(&mut self)

Converts `image color to grayscale.

Source

pub fn color_contrast(&mut self, contrast: f32)

Adjusts the contrast of image.

Source

pub fn color_brightness(&mut self, brightness: i32)

Adjusts the brightness of image.

Source

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.

Source

pub fn export_image_to_memory( &self, file_type: &str, ) -> Result<DataBuf<[u8]>, InvalidImageError>

Export image to memory buffer.

Source

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

Source

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.

Source

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.

Source

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

Source

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

Source

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.

Source

pub fn image_text(text: &str, font_size: i32, color: impl Into<Color>) -> Image

Creates an image from text (custom font).

Source

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).

Source

pub fn is_image_valid(&self) -> bool

Check if an image is valid (data and parameters)

Trait Implementations§

Source§

impl AsRawMut<Image> for Image

Source§

unsafe fn as_raw_mut(&mut self) -> &mut Image

Mutable access to the wrapped raw FFI value. Read more
Source§

impl AsRef<Image> for Image

Source§

fn as_ref(&self) -> &Image

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Image

Source§

fn clone(&self) -> Image

Create an image duplicate (useful for transformations)

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Image

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for Image

Source§

type Target = Image

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Drop for Image

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl !Send for Image

§

impl !Sync for Image

§

impl Freeze for Image

§

impl RefUnwindSafe for Image

§

impl Unpin for Image

§

impl UnsafeUnpin for Image

§

impl UnwindSafe for Image

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.