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

Issue with guessing format of WEBP images #660

Closed
joek13 opened this issue Jul 1, 2017 · 3 comments · Fixed by #937
Closed

Issue with guessing format of WEBP images #660

joek13 opened this issue Jul 1, 2017 · 3 comments · Fixed by #937

Comments

@joek13
Copy link

joek13 commented Jul 1, 2017

Presently, using image::load_from_memory will make an attempt to guess the image's format using magic numbers at the beginning of the image's bytes. WebP's magic bytes are defined here:

static MAGIC_BYTES: [(&'static [u8], ImageFormat); 11] = [
    //omitted other definitions
    (b"WEBP", ImageFormat::WEBP),
    //omitted other definitions

];

This would cause any byte sequence starting with the bytes for "WEBP" to be recognized as WebP images; however, WebP images do not actually begin with the ASCII bytes "WEBP", but instead, their file header looks like this:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|      'R'      |      'I'      |      'F'      |      'F'      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                           File Size                           |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|      'W'      |      'E'      |      'B'      |      'P'      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

(taken from the WebP Container Specification)
Because of this, WEBP images' bytes are not properly recognized by load_from_memory.

@mraof
Copy link

mraof commented Aug 18, 2017

Could this be fixed, at least for now, by just changing it from "WEBP" to "RIFF"?

@joek13
Copy link
Author

joek13 commented Aug 20, 2017

@mraof
Perhaps, but the RIFF container is generic -- not specific to WebP. A file with a RIFF header could be any one of these formats.

@mraof
Copy link

mraof commented Aug 20, 2017

That's true, but right now this crate only supports WebP anyways, and the other image formats that use RIFF seem pretty low priority
Regardless, detecting that it's a RIFF container seems like the first step for detecting if it's any of these formats to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants