From 6ce12fc0c60adbd169676ee985e8529831633a76 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Mon, 3 Oct 2022 15:53:39 -0700 Subject: Use `Cow<'static, [u8]>` in image/svg, add constructors taking &[u8]` This should resolve https://github.com/iced-rs/iced/issues/580 by providing a way to use an image included with `include_bytes!` without needing to copy it to a `Vec` to create an image handle. It would be nice if these methods could also be `const`, but that isn't possible due to the hashing being done. This is technically a breaking change since `Handle::data()` is public. But if that is used, it's most likely in used somewhere that only relies on the type derefing to `&[u8]`. --- native/src/image.rs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'native/src/image.rs') diff --git a/native/src/image.rs b/native/src/image.rs index 516eb2db..41e41aa4 100644 --- a/native/src/image.rs +++ b/native/src/image.rs @@ -1,6 +1,7 @@ //! Load and draw raster graphics. use crate::{Hasher, Rectangle}; +use std::borrow::Cow; use std::hash::{Hash, Hasher as _}; use std::path::PathBuf; use std::sync::Arc; @@ -29,7 +30,22 @@ impl Handle { Self::from_data(Data::Pixels { width, height, - pixels, + pixels: Cow::Owned(pixels), + }) + } + + /// Like [`Handle::from_pixels`], but from static pixel data. + /// + /// Useful for images included in binary, for instance with [`include_bytes!`]. + pub fn from_static_pixels( + width: u32, + height: u32, + pixels: &'static [u8], + ) -> Handle { + Self::from_data(Data::Pixels { + width, + height, + pixels: Cow::Borrowed(pixels), }) } @@ -40,7 +56,14 @@ impl Handle { /// This is useful if you already have your image loaded in-memory, maybe /// because you downloaded or generated it procedurally. pub fn from_memory(bytes: Vec) -> Handle { - Self::from_data(Data::Bytes(bytes)) + Self::from_data(Data::Bytes(Cow::Owned(bytes))) + } + + /// Like [`Handle::from_memory`], but from static image data. + /// + /// Useful for images included in binary, for instance with [`include_bytes!`]. + pub fn from_static_memory(bytes: &'static [u8]) -> Handle { + Self::from_data(Data::Bytes(Cow::Borrowed(bytes))) } fn from_data(data: Data) -> Handle { @@ -86,7 +109,7 @@ pub enum Data { Path(PathBuf), /// In-memory data - Bytes(Vec), + Bytes(Cow<'static, [u8]>), /// Decoded image pixels in BGRA format. Pixels { @@ -95,7 +118,7 @@ pub enum Data { /// The height of the image. height: u32, /// The pixels. - pixels: Vec, + pixels: Cow<'static, [u8]>, }, } -- cgit From bc5986c7c69efd206b900e8d923d3df3a225f6cc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 5 Nov 2022 01:53:24 +0100 Subject: Unify methods by leveraging `Into` in `image` and `svg` --- native/src/image.rs | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) (limited to 'native/src/image.rs') diff --git a/native/src/image.rs b/native/src/image.rs index 41e41aa4..b849ef84 100644 --- a/native/src/image.rs +++ b/native/src/image.rs @@ -26,26 +26,15 @@ impl Handle { /// pixels. /// /// This is useful if you have already decoded your image. - pub fn from_pixels(width: u32, height: u32, pixels: Vec) -> Handle { - Self::from_data(Data::Pixels { - width, - height, - pixels: Cow::Owned(pixels), - }) - } - - /// Like [`Handle::from_pixels`], but from static pixel data. - /// - /// Useful for images included in binary, for instance with [`include_bytes!`]. - pub fn from_static_pixels( + pub fn from_pixels( width: u32, height: u32, - pixels: &'static [u8], + pixels: impl Into>, ) -> Handle { Self::from_data(Data::Pixels { width, height, - pixels: Cow::Borrowed(pixels), + pixels: pixels.into(), }) } @@ -55,15 +44,8 @@ impl Handle { /// /// This is useful if you already have your image loaded in-memory, maybe /// because you downloaded or generated it procedurally. - pub fn from_memory(bytes: Vec) -> Handle { - Self::from_data(Data::Bytes(Cow::Owned(bytes))) - } - - /// Like [`Handle::from_memory`], but from static image data. - /// - /// Useful for images included in binary, for instance with [`include_bytes!`]. - pub fn from_static_memory(bytes: &'static [u8]) -> Handle { - Self::from_data(Data::Bytes(Cow::Borrowed(bytes))) + pub fn from_memory(bytes: impl Into>) -> Handle { + Self::from_data(Data::Bytes(bytes.into())) } fn from_data(data: Data) -> Handle { -- cgit From 8ce8d374b1e8d1d394a42a5ee2bca8af790f0b71 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 5 Nov 2022 03:13:04 +0100 Subject: Refactor some `image` traits a bit - Use `Size` were applicable. - Rename `TextureStore` to `image::Storage`. - Rename `TextureStoreEntry` to `image::storage::Entry`. - Wire up `viewport_dimensions` to `iced_glow` for `Svg`. --- native/src/image.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'native/src/image.rs') diff --git a/native/src/image.rs b/native/src/image.rs index b849ef84..b313d96d 100644 --- a/native/src/image.rs +++ b/native/src/image.rs @@ -1,5 +1,5 @@ //! Load and draw raster graphics. -use crate::{Hasher, Rectangle}; +use crate::{Hasher, Rectangle, Size}; use std::borrow::Cow; use std::hash::{Hash, Hasher as _}; @@ -126,7 +126,7 @@ pub trait Renderer: crate::Renderer { type Handle: Clone + Hash; /// Returns the dimensions of an image for the given [`Handle`]. - fn dimensions(&self, handle: &Self::Handle) -> (u32, u32); + fn dimensions(&self, handle: &Self::Handle) -> Size; /// Draws an image with the given [`Handle`] and inside the provided /// `bounds`. -- cgit From 438f97a6d00ad0312e7c84b4c1529968bdfba849 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 5 Nov 2022 03:18:13 +0100 Subject: Use RGBA texture for `image` and `svg` pipelines --- native/src/image.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'native/src/image.rs') diff --git a/native/src/image.rs b/native/src/image.rs index b313d96d..06fd7ae6 100644 --- a/native/src/image.rs +++ b/native/src/image.rs @@ -22,7 +22,7 @@ impl Handle { } /// Creates an image [`Handle`] containing the image pixels directly. This - /// function expects the input data to be provided as a `Vec` of BGRA + /// function expects the input data to be provided as a `Vec` of RGBA /// pixels. /// /// This is useful if you have already decoded your image. @@ -31,7 +31,7 @@ impl Handle { height: u32, pixels: impl Into>, ) -> Handle { - Self::from_data(Data::Pixels { + Self::from_data(Data::Rgba { width, height, pixels: pixels.into(), @@ -93,8 +93,8 @@ pub enum Data { /// In-memory data Bytes(Cow<'static, [u8]>), - /// Decoded image pixels in BGRA format. - Pixels { + /// Decoded image pixels in RGBA format. + Rgba { /// The width of the image. width: u32, /// The height of the image. @@ -109,7 +109,7 @@ impl std::fmt::Debug for Data { match self { Data::Path(path) => write!(f, "Path({:?})", path), Data::Bytes(_) => write!(f, "Bytes(...)"), - Data::Pixels { width, height, .. } => { + Data::Rgba { width, height, .. } => { write!(f, "Pixels({} * {})", width, height) } } -- cgit