Make image::Id actually opaque
This commit is contained in:
parent
b52c7bb610
commit
58ea914ad2
3 changed files with 40 additions and 24 deletions
|
|
@ -10,13 +10,26 @@ use std::path::{Path, PathBuf};
|
||||||
/// A handle of some image data.
|
/// A handle of some image data.
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub enum Handle {
|
pub enum Handle {
|
||||||
/// File data
|
/// A file handle. The image data will be read
|
||||||
|
/// from the file path.
|
||||||
|
///
|
||||||
|
/// Use [`from_path`] to create this variant.
|
||||||
|
///
|
||||||
|
/// [`from_path`]: Self::from_path
|
||||||
Path(Id, PathBuf),
|
Path(Id, PathBuf),
|
||||||
|
|
||||||
/// In-memory data
|
/// A handle pointing to some encoded image bytes in-memory.
|
||||||
|
///
|
||||||
|
/// Use [`from_bytes`] to create this variant.
|
||||||
|
///
|
||||||
|
/// [`from_bytes`]: Self::from_bytes
|
||||||
Bytes(Id, Bytes),
|
Bytes(Id, Bytes),
|
||||||
|
|
||||||
/// Decoded image pixels in RGBA format.
|
/// A handle pointing to decoded image pixels in RGBA format.
|
||||||
|
///
|
||||||
|
/// Use [`from_rgba`] to create this variant.
|
||||||
|
///
|
||||||
|
/// [`from_rgba`]: Self::from_bytes
|
||||||
Rgba {
|
Rgba {
|
||||||
/// The id of this handle.
|
/// The id of this handle.
|
||||||
id: Id,
|
id: Id,
|
||||||
|
|
@ -39,12 +52,24 @@ impl Handle {
|
||||||
Self::Path(Id::path(&path), path)
|
Self::Path(Id::path(&path), path)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an image [`Handle`] containing the image pixels directly. This
|
/// Creates an image [`Handle`] containing the encoded image data directly.
|
||||||
/// function expects the input data to be provided as a `Vec<u8>` of RGBA
|
///
|
||||||
/// pixels.
|
/// Makes an educated guess about the image format by examining the given data.
|
||||||
|
///
|
||||||
|
/// This is useful if you already have your image loaded in-memory, maybe
|
||||||
|
/// because you downloaded or generated it procedurally.
|
||||||
|
pub fn from_bytes(bytes: impl Into<Bytes>) -> Handle {
|
||||||
|
Self::Bytes(Id::unique(), bytes.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates an image [`Handle`] containing the decoded image pixels directly.
|
||||||
|
///
|
||||||
|
/// This function expects the pixel data to be provided as a collection of [`Bytes`]
|
||||||
|
/// of RGBA pixels. Therefore, the length of the pixel data should always be
|
||||||
|
/// `width * height * 4`.
|
||||||
///
|
///
|
||||||
/// This is useful if you have already decoded your image.
|
/// This is useful if you have already decoded your image.
|
||||||
pub fn from_pixels(
|
pub fn from_rgba(
|
||||||
width: u32,
|
width: u32,
|
||||||
height: u32,
|
height: u32,
|
||||||
pixels: impl Into<Bytes>,
|
pixels: impl Into<Bytes>,
|
||||||
|
|
@ -57,16 +82,6 @@ impl Handle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an image [`Handle`] containing the image data directly.
|
|
||||||
///
|
|
||||||
/// Makes an educated guess about the image format by examining the given data.
|
|
||||||
///
|
|
||||||
/// 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: impl Into<Bytes>) -> Handle {
|
|
||||||
Self::Bytes(Id::unique(), bytes.into())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the unique identifier of the [`Handle`].
|
/// Returns the unique identifier of the [`Handle`].
|
||||||
pub fn id(&self) -> Id {
|
pub fn id(&self) -> Id {
|
||||||
match self {
|
match self {
|
||||||
|
|
@ -100,10 +115,11 @@ impl std::fmt::Debug for Handle {
|
||||||
|
|
||||||
/// The unique identifier of some [`Handle`] data.
|
/// The unique identifier of some [`Handle`] data.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub enum Id {
|
pub struct Id(_Id);
|
||||||
/// A unique identifier.
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
|
enum _Id {
|
||||||
Unique(u64),
|
Unique(u64),
|
||||||
/// A hash identifier.
|
|
||||||
Hash(u64),
|
Hash(u64),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -113,7 +129,7 @@ impl Id {
|
||||||
|
|
||||||
static NEXT_ID: AtomicU64 = AtomicU64::new(0);
|
static NEXT_ID: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
Self::Unique(NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed))
|
Self(_Id::Unique(NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path(path: impl AsRef<Path>) -> Self {
|
fn path(path: impl AsRef<Path>) -> Self {
|
||||||
|
|
@ -124,7 +140,7 @@ impl Id {
|
||||||
hasher.finish()
|
hasher.finish()
|
||||||
};
|
};
|
||||||
|
|
||||||
Self::Hash(hash)
|
Self(_Id::Hash(hash))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -188,7 +188,7 @@ impl Pokemon {
|
||||||
{
|
{
|
||||||
let bytes = reqwest::get(&url).await?.bytes().await?;
|
let bytes = reqwest::get(&url).await?.bytes().await?;
|
||||||
|
|
||||||
Ok(image::Handle::from_memory(bytes))
|
Ok(image::Handle::from_bytes(bytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
|
|
||||||
|
|
@ -109,7 +109,7 @@ impl Example {
|
||||||
fn view(&self) -> Element<'_, Message> {
|
fn view(&self) -> Element<'_, Message> {
|
||||||
let image: Element<Message> = if let Some(screenshot) = &self.screenshot
|
let image: Element<Message> = if let Some(screenshot) = &self.screenshot
|
||||||
{
|
{
|
||||||
image(image::Handle::from_pixels(
|
image(image::Handle::from_rgba(
|
||||||
screenshot.size.width,
|
screenshot.size.width,
|
||||||
screenshot.size.height,
|
screenshot.size.height,
|
||||||
screenshot.clone(),
|
screenshot.clone(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue