Skip to content

Commit

Permalink
Merge pull request raylib-rs#16 from JulianGmp/refactor_load_image_fr…
Browse files Browse the repository at this point in the history
…om_mem

simplify load_image_from_mem function
  • Loading branch information
bitten2up authored Jan 31, 2024
2 parents ac3b15e + 36f36c8 commit f2b8129
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions raylib/src/core/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::core::color::Color;
use crate::core::math::{Rectangle, Vector4};
use crate::core::{RaylibHandle, RaylibThread};
use crate::ffi;
use std::convert::TryInto;
use std::ffi::CString;
use std::mem::ManuallyDrop;

Expand Down Expand Up @@ -693,12 +694,18 @@ impl Image {
Ok(Image(i))
}

/// Loads image from a given memory buffer as a vector of arrays
/// ensure filetype is extension, for example, ".png"
pub fn load_image_from_mem(filetype: &str, bytes: &Vec<u8>, size: i32) -> Result<Image, String> {
/// 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.
pub fn load_image_from_mem(filetype: &str, bytes: &[u8]) -> Result<Image, String> {
let c_filetype = CString::new(filetype).unwrap();
let c_bytes = bytes.as_ptr();
let i = unsafe { ffi::LoadImageFromMemory(c_filetype.as_ptr(), c_bytes, size) };
let i = unsafe {
ffi::LoadImageFromMemory(
c_filetype.as_ptr(),
bytes.as_ptr(),
bytes.len().try_into().unwrap(),
)
};
if i.data.is_null() {
return Err(format!(
"Image data is null. Check provided buffer data"
Expand Down

0 comments on commit f2b8129

Please sign in to comment.