summaryrefslogtreecommitdiffstats
path: root/examples/gallery/src/civitai.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/gallery/src/civitai.rs')
-rw-r--r--examples/gallery/src/civitai.rs33
1 files changed, 29 insertions, 4 deletions
diff --git a/examples/gallery/src/civitai.rs b/examples/gallery/src/civitai.rs
index 986b6bf2..c394ef1d 100644
--- a/examples/gallery/src/civitai.rs
+++ b/examples/gallery/src/civitai.rs
@@ -10,6 +10,7 @@ use std::sync::Arc;
pub struct Image {
pub id: Id,
url: String,
+ hash: String,
}
impl Image {
@@ -40,20 +41,37 @@ impl Image {
Ok(response.items)
}
+ pub async fn blurhash(
+ self,
+ width: u32,
+ height: u32,
+ ) -> Result<Rgba, Error> {
+ task::spawn_blocking(move || {
+ let pixels = blurhash::decode(&self.hash, width, height, 1.0)?;
+
+ Ok::<_, Error>(Rgba {
+ width,
+ height,
+ pixels: Bytes::from(pixels),
+ })
+ })
+ .await?
+ }
+
pub async fn download(self, size: Size) -> Result<Rgba, Error> {
let client = reqwest::Client::new();
let bytes = client
.get(match size {
Size::Original => self.url,
- Size::Thumbnail => self
+ Size::Thumbnail { width } => self
.url
.split("/")
.map(|part| {
if part.starts_with("width=") {
- "width=640"
+ format!("width={width}")
} else {
- part
+ part.to_string()
}
})
.collect::<Vec<_>>()
@@ -107,7 +125,7 @@ impl fmt::Debug for Rgba {
#[derive(Debug, Clone, Copy)]
pub enum Size {
Original,
- Thumbnail,
+ Thumbnail { width: u16 },
}
#[derive(Debug, Clone)]
@@ -117,6 +135,7 @@ pub enum Error {
IOFailed(Arc<io::Error>),
JoinFailed(Arc<task::JoinError>),
ImageDecodingFailed(Arc<image::ImageError>),
+ BlurhashDecodingFailed(Arc<blurhash::Error>),
}
impl From<reqwest::Error> for Error {
@@ -142,3 +161,9 @@ impl From<image::ImageError> for Error {
Self::ImageDecodingFailed(Arc::new(error))
}
}
+
+impl From<blurhash::Error> for Error {
+ fn from(error: blurhash::Error) -> Self {
+ Self::BlurhashDecodingFailed(Arc::new(error))
+ }
+}