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

Support decoding WEBPs in extended format #1168

Merged
merged 1 commit into from
Mar 26, 2020
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
48 changes: 31 additions & 17 deletions src/webp/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,39 @@ impl<R: Read> WebPDecoder<R> {
Ok(size)
}

fn read_vp8_header(&mut self) -> ImageResult<()> {
let mut vp8 = Vec::with_capacity(4);
self.r.by_ref().take(4).read_to_end(&mut vp8)?;

if &*vp8 != b"VP8 " {
return Err(ImageError::Decoding(DecodingError::with_message(
ImageFormat::WebP.into(),
"Invalid VP8 signature.".to_string(),
)));
fn read_vp8_header(&mut self) -> ImageResult<u32> {
loop {
let mut chunk = Vec::with_capacity(4);
self.r.by_ref().take(4).read_to_end(&mut chunk)?;

match &*chunk {
b"VP8 " => {
let len = self.r.read_u32::<LittleEndian>()?;
return Ok(len);
}
b"ALPH" | b"VP8L" | b"ANIM" | b"ANMF" => {
// Alpha, Lossless and Animation isn't supported
return Err(ImageError::Decoding(DecodingError::with_message(
ImageFormat::WebP.into(),
"Unsupported WEBP feature.".to_string(),
)));
}
_ => {
let mut len = self.r.read_u32::<LittleEndian>()?;
if len % 2 != 0 {
// RIFF chunks containing an uneven number of bytes append
// an extra 0x00 at the end of the chunk
len += 1;
}
io::copy(&mut self.r.by_ref().take(len as u64), &mut io::sink())?;
}
}
}

let _len = self.r.read_u32::<LittleEndian>()?;

Ok(())
}

fn read_frame(&mut self) -> ImageResult<()> {
fn read_frame(&mut self, len: u32) -> ImageResult<()> {
let mut framedata = Vec::new();
self.r.read_to_end(&mut framedata)?;
self.r.by_ref().take(len as u64).read_to_end(&mut framedata)?;
let m = io::Cursor::new(framedata);

let mut v = Vp8Decoder::new(m);
Expand All @@ -92,8 +106,8 @@ impl<R: Read> WebPDecoder<R> {
fn read_metadata(&mut self) -> ImageResult<()> {
if !self.have_frame {
self.read_riff_header()?;
self.read_vp8_header()?;
self.read_frame()?;
let len = self.read_vp8_header()?;
self.read_frame(len)?;

self.have_frame = true;
}
Expand Down
Binary file added tests/images/webp/images/vp8x-gray.webp
Binary file not shown.
Binary file added tests/images/webp/images/vp8x-rgb.webp
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.