Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blurhash to gallery #2796

Merged
merged 6 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ unused_results = "deny"

[workspace.lints.clippy]
type-complexity = "allow"
map-entry = "allow"
semicolon_if_nothing_returned = "deny"
trivially-copy-pass-by-ref = "deny"
default_trait_access = "deny"
Expand Down
12 changes: 12 additions & 0 deletions core/src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ where
T: Clone + Copy + PartialEq + Float,
{
raw: lilt::Animated<T, Instant>,
duration: Duration, // TODO: Expose duration getter in `lilt`
}

impl<T> Animation<T>
Expand All @@ -23,6 +24,7 @@ where
pub fn new(state: T) -> Self {
Self {
raw: lilt::Animated::new(state),
duration: Duration::from_millis(100),
}
}

Expand Down Expand Up @@ -58,6 +60,7 @@ where
/// Sets the duration of the [`Animation`] to the given value.
pub fn duration(mut self, duration: Duration) -> Self {
self.raw = self.raw.duration(duration.as_secs_f32() * 1_000.0);
self.duration = duration;
self
}

Expand Down Expand Up @@ -133,4 +136,13 @@ impl Animation<bool> {
{
self.raw.animate_bool(start, end, at)
}

/// Returns the remaining [`Duration`] of the [`Animation`].
pub fn remaining(&self, at: Instant) -> Duration {
Duration::from_secs_f32(self.interpolate(
self.duration.as_secs_f32(),
0.0,
at,
))
}
}
6 changes: 3 additions & 3 deletions core/src/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl From<f32> for Length {
}
}

impl From<u16> for Length {
fn from(units: u16) -> Self {
Length::Fixed(f32::from(units))
impl From<u32> for Length {
fn from(units: u32) -> Self {
Length::Fixed(units as f32)
}
}
6 changes: 3 additions & 3 deletions core/src/pixels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ impl From<f32> for Pixels {
}
}

impl From<u16> for Pixels {
fn from(amount: u16) -> Self {
Self(f32::from(amount))
impl From<u32> for Pixels {
fn from(amount: u32) -> Self {
Self(amount as f32)
}
}

Expand Down
2 changes: 2 additions & 0 deletions examples/gallery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ bytes.workspace = true
image.workspace = true
tokio.workspace = true

blurhash = "0.2.3"

[lints]
workspace = true
33 changes: 29 additions & 4 deletions examples/gallery/src/civitai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::sync::Arc;
pub struct Image {
pub id: Id,
url: String,
hash: String,
}

impl Image {
Expand Down Expand Up @@ -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 * 2) // High DPI
} else {
part
part.to_owned()
}
})
.collect::<Vec<_>>()
Expand Down Expand Up @@ -107,7 +125,7 @@ impl fmt::Debug for Rgba {
#[derive(Debug, Clone, Copy)]
pub enum Size {
Original,
Thumbnail,
Thumbnail { width: u32 },
}

#[derive(Debug, Clone)]
Expand All @@ -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 {
Expand All @@ -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))
}
}
Loading