Skip to content

Commit

Permalink
replace libwebp with image-webp for decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed May 23, 2024
1 parent a833910 commit 938c573
Showing 1 changed file with 7 additions and 30 deletions.
37 changes: 7 additions & 30 deletions src/image_io/load.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{io::Cursor, time::Duration};
use std::io::Cursor;

use fontdb::Database;
use image::{
codecs::{gif::GifDecoder, openexr::OpenExrDecoder, png::PngDecoder},
codecs::{gif::GifDecoder, openexr::OpenExrDecoder, png::PngDecoder, webp::WebPDecoder},
io::Reader as ImageReader,
AnimationDecoder, DynamicImage, Frame, ImageBuffer, ImageFormat, Rgb, Rgba,
};
Expand Down Expand Up @@ -39,36 +39,13 @@ pub fn load_raster(bytes: &[u8]) -> Option<Vec<Image>> {
None
}
ImageFormat::WebP => {
if let Ok(decoder) = webp_animation::Decoder::new(bytes) {
let mut time = 0;
let frames: Vec<Image> = decoder
.into_iter()
.filter_map(|frame| {
let timestamp = frame.timestamp();
let difference = timestamp - time;

let (width, height) = frame.dimensions();
let data = frame.data().to_vec();

ImageBuffer::from_raw(width, height, data).map(|image| {
time = timestamp;
let delay = Duration::from_millis(difference as u64);
Image::with_delay(DynamicImage::ImageRgba8(image), delay)
})
})
.collect();

if !frames.is_empty() {
return Some(frames);
if let Ok(decoder) = WebPDecoder::new(Cursor::new(bytes)) {
if decoder.has_animation() {
return Some(decode_images(decoder.into_frames()));
} else if let Ok(image) = DynamicImage::from_decoder(decoder) {
return Some(vec![Image::new(image)]);
}
}
if let Ok((width, height, buf)) = libwebp::WebPDecodeRGBA(bytes) {
return Some(vec![Image::from(ImageBuffer::from_raw(
width,
height,
buf.to_vec(),
)?)]);
}
None
}
ImageFormat::Png => {
Expand Down

0 comments on commit 938c573

Please sign in to comment.