From ab236376a3f8f1ac3cdd8aeb0ffeee45e3de37e3 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Thu, 6 Feb 2025 13:54:45 -0800 Subject: Add blurhash to gallery --- examples/gallery/src/civitai.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'examples/gallery/src/civitai.rs') 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 { + 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 { 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::>() @@ -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), JoinFailed(Arc), ImageDecodingFailed(Arc), + BlurhashDecodingFailed(Arc), } impl From for Error { @@ -142,3 +161,9 @@ impl From for Error { Self::ImageDecodingFailed(Arc::new(error)) } } + +impl From for Error { + fn from(error: blurhash::Error) -> Self { + Self::BlurhashDecodingFailed(Arc::new(error)) + } +} -- cgit From f3ae4266e9727940ba0d0e8469362923590916f4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 9 Feb 2025 06:38:48 +0100 Subject: Implement `From` instead of `u16` for `Length` and `Pixels` --- examples/gallery/src/civitai.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/gallery/src/civitai.rs') diff --git a/examples/gallery/src/civitai.rs b/examples/gallery/src/civitai.rs index c394ef1d..457091e9 100644 --- a/examples/gallery/src/civitai.rs +++ b/examples/gallery/src/civitai.rs @@ -125,7 +125,7 @@ impl fmt::Debug for Rgba { #[derive(Debug, Clone, Copy)] pub enum Size { Original, - Thumbnail { width: u16 }, + Thumbnail { width: u32 }, } #[derive(Debug, Clone)] -- cgit From 17395e83201d1e5e00be46c1fb1d6fa18f3d87f1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 9 Feb 2025 06:40:48 +0100 Subject: Use `to_owned` instead of `to_string` in `gallery` example --- examples/gallery/src/civitai.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/gallery/src/civitai.rs') diff --git a/examples/gallery/src/civitai.rs b/examples/gallery/src/civitai.rs index 457091e9..8e57db3d 100644 --- a/examples/gallery/src/civitai.rs +++ b/examples/gallery/src/civitai.rs @@ -71,7 +71,7 @@ impl Image { if part.starts_with("width=") { format!("width={width}") } else { - part.to_string() + part.to_owned() } }) .collect::>() -- cgit From ec4d007a4cafa005a61aa24a47960e5d639b5587 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 9 Feb 2025 08:02:05 +0100 Subject: Simplify `gallery` example a bit --- examples/gallery/src/civitai.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/gallery/src/civitai.rs') diff --git a/examples/gallery/src/civitai.rs b/examples/gallery/src/civitai.rs index 8e57db3d..18d2a040 100644 --- a/examples/gallery/src/civitai.rs +++ b/examples/gallery/src/civitai.rs @@ -69,7 +69,7 @@ impl Image { .split("/") .map(|part| { if part.starts_with("width=") { - format!("width={width}") + format!("width={}", width * 2) // High DPI } else { part.to_owned() } -- cgit