add AVIF support

This commit is contained in:
Julien Nicoulaud
2021-09-25 10:39:19 +02:00
committed by Mehdi Benadel
parent 2f0b96d682
commit c303b00158
4 changed files with 9 additions and 0 deletions
+2
View File
@@ -93,6 +93,8 @@ pub enum Type {
Flif,
/// ICO files
Ico,
/// AVIF files
Avif,
}
/// Try to determine image format from a bytes slice.
+2
View File
@@ -23,6 +23,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 {
@@ -89,6 +90,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),
Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

+5
View File
@@ -112,6 +112,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");