Skip to content

Issue with guessing format of WEBP images #660

@joek13

Description

@joek13

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions