Decode on the fly in gallery example

Use `release` mode. Image decoding is terribly slow
in `debug` mode!
This commit is contained in:
Héctor Ramón Jiménez 2025-10-24 20:06:26 +02:00
parent 1b51364154
commit 6fa54f7f6b
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 44 additions and 43 deletions

View file

@ -1,4 +1,3 @@
use bytes::Bytes;
use serde::Deserialize;
use sipper::{Straw, sipper};
use tokio::task;
@ -54,14 +53,14 @@ impl Image {
rgba: Rgba {
width,
height,
pixels: Bytes::from(pixels),
pixels: Bytes(pixels.into()),
},
})
})
.await?
}
pub fn download(self, size: Size) -> impl Straw<Rgba, Blurhash, Error> {
pub fn download(self, size: Size) -> impl Straw<Bytes, Blurhash, Error> {
sipper(async move |mut sender| {
let client = reqwest::Client::new();
@ -97,21 +96,7 @@ impl Image {
.bytes()
.await?;
let image = task::spawn_blocking(move || {
Ok::<_, Error>(
image::ImageReader::new(io::Cursor::new(bytes))
.with_guessed_format()?
.decode()?
.to_rgba8(),
)
})
.await??;
Ok(Rgba {
width: image.width(),
height: image.height(),
pixels: Bytes::from(image.into_raw()),
})
Ok(Bytes(bytes))
})
}
}
@ -142,6 +127,23 @@ impl fmt::Debug for Rgba {
}
}
#[derive(Clone)]
pub struct Bytes(bytes::Bytes);
impl From<Bytes> for bytes::Bytes {
fn from(value: Bytes) -> Self {
value.0
}
}
impl fmt::Debug for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Compressed")
.field("bytes", &self.0.len())
.finish()
}
}
#[derive(Debug, Clone, Copy)]
pub enum Size {
Original,