From b680db9114779f234e25b74223af5e90c74b4f1b Mon Sep 17 00:00:00 2001 From: Alula Date: Fri, 28 Aug 2020 05:48:41 +0200 Subject: [PATCH] texture_set: add memory image loading function --- src/texture_set.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/texture_set.rs b/src/texture_set.rs index 226d744..44062fc 100644 --- a/src/texture_set.rs +++ b/src/texture_set.rs @@ -12,6 +12,7 @@ use log::info; use crate::common; use crate::engine_constants::EngineConstants; use crate::str; +use image::RgbaImage; pub struct SizedBatch { pub batch: SpriteBatch, @@ -95,6 +96,14 @@ impl TextureSet { } } + fn make_transparent(rgba: &mut RgbaImage) { + for (r, g, b, a) in rgba.iter_mut().tuples() { + if *r == 0 && *g == 0 && *b == 0 { + *a = 0; + } + } + } + fn load_image(&self, ctx: &mut Context, path: &str) -> GameResult { let img = { let mut buf = [0u8; 8]; @@ -104,15 +113,23 @@ impl TextureSet { let image = image::load(BufReader::new(reader), image::guess_format(&buf)?)?; let mut rgba = image.to_rgba(); - if image.color().channel_count() != 4 { - for (r, g, b, a) in rgba.iter_mut().tuples() { - if *r == 0 && *g == 0 && *b == 0 { - *a = 0; - } - } + TextureSet::make_transparent(&mut rgba); } + rgba + }; + let (width, height) = img.dimensions(); + Image::from_rgba8(ctx, width as u16, height as u16, img.as_ref()) + } + + fn load_image_from_buf(&self, ctx: &mut Context, buf: &[u8]) -> GameResult { + let img = { + let image = image::load_from_memory(buf)?; + let mut rgba = image.to_rgba(); + if image.color().channel_count() != 4 { + TextureSet::make_transparent(&mut rgba); + } rgba }; let (width, height) = img.dimensions();