From 18410154289fa3262403bb2c9de3dd741fd7dda2 Mon Sep 17 00:00:00 2001 From: Soham Chowdhury Date: Sat, 29 Feb 2020 07:32:42 +0530 Subject: [PATCH 1/2] Add support for loading already-decoded image pixels --- native/src/widget/image.rs | 26 ++++++++++++++++++++++++++ wgpu/src/image/raster.rs | 15 +++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index 9b92c7f1..a1743744 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -125,6 +125,19 @@ impl Handle { Self::from_data(Data::Path(path.into())) } + /// Creates an image [`Handle`] containing the image pixels directly. + /// + /// This is useful if you have already decoded your image. + /// + /// [`Handle`]: struct.Handle.html + pub fn from_pixels(width: u32, height: u32, pixels: Vec) -> Handle { + Self::from_data(Data::Pixels { + width, + height, + pixels, + }) + } + /// Creates an image [`Handle`] containing the image data directly. /// /// This is useful if you already have your image loaded in-memory, maybe @@ -188,6 +201,16 @@ pub enum Data { /// In-memory data Bytes(Vec), + + /// Decoded image pixels in BGRA format. + Pixels { + /// The width of the image. + width: u32, + /// The height of the image. + height: u32, + /// The pixels. + pixels: Vec, + }, } impl std::fmt::Debug for Data { @@ -195,6 +218,9 @@ impl std::fmt::Debug for Data { match self { Data::Path(path) => write!(f, "Path({:?})", path), Data::Bytes(_) => write!(f, "Bytes(...)"), + Data::Pixels { width, height, .. } => { + write!(f, "Pixels({} * {})", width, height) + } } } } diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 3edec57e..4f69df8c 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -55,6 +55,21 @@ impl Cache { Memory::Invalid } } + image::Data::Pixels { + width, + height, + pixels, + } => { + if let Some(image) = ::image::ImageBuffer::from_vec( + *width, + *height, + pixels.to_vec(), + ) { + Memory::Host(image) + } else { + Memory::Invalid + } + } }; self.insert(handle, memory); From eb7e3250d3da495f46480360c99540a8f643d2e6 Mon Sep 17 00:00:00 2001 From: Soham Chowdhury Date: Sun, 1 Mar 2020 06:44:06 +0530 Subject: [PATCH 2/2] Note BGRA requirement in Handle::from_pixels docs --- native/src/widget/image.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index a1743744..fbe38bfc 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -125,7 +125,9 @@ impl Handle { Self::from_data(Data::Path(path.into())) } - /// Creates an image [`Handle`] containing the image pixels directly. + /// Creates an image [`Handle`] containing the image pixels directly. This + /// function expects the input data to be provided as a `Vec` of BGRA + /// pixels. /// /// This is useful if you have already decoded your image. ///