-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::{io::Read, marker::PhantomData}; | ||
|
||
use webp::{AnimDecoder, DecodeAnimImage}; | ||
use zune_core::{bit_depth::BitDepth, bytestream::ZReaderTrait, colorspace::ColorSpace}; | ||
use zune_image::{errors::ImageErrors, frame::Frame, image::Image, traits::DecoderTrait}; | ||
|
||
pub struct WebPDecoder<R: Read> { | ||
inner: DecodeAnimImage, | ||
phantom: PhantomData<R>, | ||
} | ||
|
||
impl<R: Read> WebPDecoder<R> { | ||
pub fn try_new(mut source: R) -> Result<WebPDecoder<R>, ImageErrors> { | ||
let mut buf = Vec::new(); | ||
source.read_to_end(&mut buf)?; | ||
|
||
let decoder = AnimDecoder::new(&buf); | ||
let img = decoder | ||
.decode() | ||
.map_err(|e| ImageErrors::ImageDecodeErrors(e))?; | ||
|
||
Ok(WebPDecoder { | ||
inner: img, | ||
phantom: PhantomData, | ||
}) | ||
} | ||
} | ||
|
||
impl<R, T> DecoderTrait<T> for WebPDecoder<R> | ||
where | ||
R: Read, | ||
T: ZReaderTrait, | ||
{ | ||
fn decode(&mut self) -> Result<Image, ImageErrors> { | ||
let (width, height) = <WebPDecoder<R> as DecoderTrait<T>>::dimensions(self).unwrap(); | ||
let color = <WebPDecoder<R> as DecoderTrait<T>>::out_colorspace(self); | ||
|
||
let frames = self | ||
.inner | ||
.into_iter() | ||
.enumerate() | ||
.map(|(idx, frame)| { | ||
Frame::from_u8(frame.get_image(), color, idx, frame.get_time_ms() as usize) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
Ok(Image::new_frames( | ||
frames, | ||
BitDepth::Eight, | ||
width, | ||
height, | ||
color, | ||
)) | ||
} | ||
|
||
fn dimensions(&self) -> Option<(usize, usize)> { | ||
let frame = self.inner.get_frame(0).unwrap(); | ||
|
||
Some((frame.width() as usize, frame.height() as usize)) | ||
} | ||
|
||
fn out_colorspace(&self) -> ColorSpace { | ||
let frame = self.inner.get_frame(0).unwrap(); | ||
|
||
match frame.get_layout() { | ||
webp::PixelLayout::Rgb => ColorSpace::RGB, | ||
webp::PixelLayout::Rgba => ColorSpace::RGBA, | ||
} | ||
} | ||
|
||
fn name(&self) -> &'static str { | ||
"webp" | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::fs::read; | ||
|
||
use zune_core::bytestream::ZByteReader; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn decode() { | ||
let file_content = read("tests/files/webp/f1t.webp").unwrap(); | ||
|
||
let reader = ZByteReader::new(file_content); | ||
|
||
let mut decoder = WebPDecoder::try_new(reader).unwrap(); | ||
|
||
let img = | ||
<WebPDecoder<ZByteReader<Vec<u8>>> as DecoderTrait<Vec<u8>>>::decode(&mut decoder).unwrap(); | ||
|
||
assert_eq!(img.dimensions(), (48, 80)); | ||
assert_eq!(img.colorspace(), ColorSpace::RGBA); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod decoder; | ||
mod encoder; | ||
|
||
pub use decoder::*; | ||
pub use encoder::*; |