Newer
Older
/// Embed an image in the binary and load it at runtime.
#[macro_export]
macro_rules! img {
($name: literal) => {{
crate::image::load_image_from_memory(include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/image/",
$name
)))
.expect($name)
}};
}
/// Embed an image in the binary and load it into the [`Context`] at runtime.
#[macro_export]
macro_rules! ctx_img {
($ctx: expr, $name: literal) => {{
$ctx.load_texture($name, img!($name))
}};
}
pub fn load_image_from_memory(
image_data: &[u8],
) -> Result<eframe::egui::ColorImage, image::ImageError> {
let image = image::load_from_memory(image_data)?;
let size = [image.width() as _, image.height() as _];
let image_buffer = image.to_rgba8();
let pixels = image_buffer.as_flat_samples();
Ok(eframe::egui::ColorImage::from_rgba_unmultiplied(
size,
pixels.as_slice(),
))
}