Skip to content

Commit

Permalink
feat: Allow opening of webp animations
Browse files Browse the repository at this point in the history
  • Loading branch information
woelper committed Feb 11, 2024
1 parent c9dead5 commit 8cfb3a4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
24 changes: 24 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 @@ -75,6 +75,7 @@ bitflags = "2.4.1"
flate2 = "1.0.28"
ruzstd = "0.5.0"
basis-universal = "0.3.1"
webp-animation = { version = "0.9.0", features = ["static"] }

[features]
heif = ["libheif-rs"]
Expand Down
37 changes: 34 additions & 3 deletions src/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ use std::fs::File;
use std::io::BufReader;
use std::path::Path;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::time::SystemTime;
use tiff::decoder::Limits;
use usvg::{TreeParsing, TreeTextToPath};
use webp_animation::prelude::*;
use zune_png::zune_core::options::DecoderOptions;
use zune_png::zune_core::result::DecodingResult;
use zune_png::PngDecoder;
Expand Down Expand Up @@ -429,9 +431,38 @@ pub fn open_image(img_location: &Path) -> Result<Receiver<Frame>> {
return Ok(receiver);
}
"webp" => {
let contents = std::fs::read(img_location)?;
let buf = decode_webp(&contents).context("Can't decode webp")?;
_ = sender.send(Frame::new_still(buf));
// let contents = std::fs::read(img_location)?;
// let buf = decode_webp(&contents).context("Can't decode webp")?;
// _ = sender.send(Frame::new_still(buf));
// return Ok(receiver);
let stamp = SystemTime::now();

let buffer = std::fs::read(img_location)?;
info!("Loaded after {}s", stamp.elapsed().unwrap().as_secs_f32());

let decoder = Decoder::new(&buffer)?;
info!("Dec 2 after {}s", stamp.elapsed().unwrap().as_secs_f32());

let mut last_timestamp = 0;

let mut last_buffer : Option<Vec<u8>>;

for frame in decoder.into_iter() {
let buf = image::ImageBuffer::from_raw(
frame.dimensions().0,
frame.dimensions().1,
frame.data().to_vec(),
)
.context("Can't create imagebuffer from webp")?;
let t = frame.timestamp();
let delay = t - last_timestamp;
debug!("time {t} {delay}");
last_timestamp = t;
_ = sender.send(Frame::new(buf, delay as u16, FrameSource::Animation));
}

// TODO: Use thread for animation and return receiver immediately

return Ok(receiver);
}
"png" => {
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ pub const FONT: &[u8; 309828] = include_bytes!("../res/fonts/Inter-Regular.ttf")
#[notan_main]
fn main() -> Result<(), String> {
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "warning");
std::env::set_var("RUST_LOG", "info");
let _ = env_logger::try_init();

}
// on debug builds, override log level
#[cfg(debug_assertions)]
Expand Down
3 changes: 3 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ pub fn send_image_threaded(
}
// a "normal image (no animation)"
if f.source == FrameSource::Still {

info!("Received image in {:?}", timer.elapsed());

let largest_side = f.buffer.dimensions().0.max(f.buffer.dimensions().1);

// Check if texture is too large to fit on the texture
Expand Down

0 comments on commit 8cfb3a4

Please sign in to comment.