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

add AVIF support #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pub enum Type {
Flif,
/// ICO files
Ico,
/// AVIF files
Avif,
}

/// Try to determine image format from a bytes slice.
Expand Down
2 changes: 2 additions & 0 deletions src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const BGP: &'static [u8] = b"BPG\xfb";
const RGB: &'static [u8] = b"\x01\xda";
const FLIF: &'static [u8] = b"FLIF";
const ICO: &'static [u8] = b"\x00\x00\x01\x00";
const AVIF: &'static [u8] = b"avif";

#[inline]
fn is_pbm(bytes: &[u8]) -> bool {
Expand Down Expand Up @@ -86,6 +87,7 @@ pub fn guess(bytes: &[u8]) -> Option<Type> {
_ if &bytes[..2] == RGB => Some(Type::Rgb),
_ if &bytes[..4] == FLIF => Some(Type::Flif),
_ if &bytes[..4] == ICO => Some(Type::Ico),
_ if (&bytes[8..12] == AVIF) => Some(Type::Avif),
_ if is_pbm(bytes) => Some(Type::Pbm),
_ if is_pgm(bytes) => Some(Type::Pgm),
_ if is_ppm(bytes) => Some(Type::Ppm),
Expand Down
Binary file added tests/images/example.avif
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ fn test_ico() {
assert_result!(Some(imghdr::Type::Ico), "./tests/images/example.ico");
}

#[test]
fn test_avif() {
assert_result!(Some(imghdr::Type::Avif), "./tests/images/example.avif");
}

#[test]
fn test_not_a_image() {
assert_result!(None::<imghdr::Type>, "./tests/images/not-a-image.txt");
Expand Down