Skip to content

Commit

Permalink
Merge pull request #1 from svartalf/master
Browse files Browse the repository at this point in the history
Add missing EXR and support HDR (RGBE) (svartalf#2)
  • Loading branch information
iwatakeshi committed May 7, 2019
2 parents 55d7d3d + 31a0e92 commit 9a751c3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -35,6 +35,8 @@ pub enum Type {
Ppm,
/// SGI image library files
Rgb,
/// RGBE (Radiance HDR) files
Rgbe,
/// FLIF (Free Lossless Image Format) files
Flif,
/// ICO files
Expand Down
9 changes: 9 additions & 0 deletions src/patterns.rs
Expand Up @@ -12,6 +12,7 @@ const RAST: &'static [u8] = b"\x59\xA6\x6A\x95";
const XBM: &'static [u8] = b"#define ";
const RIFF: &'static [u8] = b"RIFF";
const WEBP: &'static [u8] = b"WEBP";
const EXR: &'static [u8] = b"\x76\x2F\x31\x01";
const BMP: &'static [u8] = b"BM";
const BGP: &'static [u8] = b"BPG\xfb";
const RGB: &'static [u8] = b"\x01\xda";
Expand Down Expand Up @@ -39,6 +40,12 @@ fn is_ppm(ref bytes: [u8; 32]) -> bool {
&& b" \t\n\r".contains(&bytes[2]);
}

#[inline]
fn is_rgbe(ref bytes: [u8; 32]) -> bool {
return &bytes[..8] == b"\x23\x3f\x52\x47\x42\x45\x0a\x47"
|| &bytes[..8] == b"\x23\x3f\x52\x41\x44\x49\x41\x4e";
}

pub fn guess(ref bytes: [u8; 32]) -> Option<Type> {
match () {
_ if &bytes[..8] == PNG => Some(Type::Png),
Expand All @@ -48,6 +55,7 @@ pub fn guess(ref bytes: [u8; 32]) -> Option<Type> {
_ if &bytes[..4] == RAST => Some(Type::Rast),
_ if &bytes[..8] == XBM => Some(Type::Xbm),
_ if (&bytes[..4] == RIFF) && (&bytes[8..12] == WEBP) => Some(Type::Webp),
_ if (&bytes[..4] == EXR) => Some(Type::Exr),
_ if &bytes[..2] == BMP => Some(Type::Bmp),
_ if &bytes[..4] == BGP => Some(Type::Bgp),
_ if &bytes[..2] == RGB => Some(Type::Rgb),
Expand All @@ -56,6 +64,7 @@ pub fn guess(ref bytes: [u8; 32]) -> Option<Type> {
_ if is_pbm(*bytes) => Some(Type::Pbm),
_ if is_pgm(*bytes) => Some(Type::Pgm),
_ if is_ppm(*bytes) => Some(Type::Ppm),
_ if is_rgbe(*bytes) => Some(Type::Rgbe),
_ => None
}
}
Binary file added tests/images/example.exr
Binary file not shown.
Binary file added tests/images/example.hdr
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/mod.rs
Expand Up @@ -54,6 +54,11 @@ fn test_webp() {
assert_result!(Some(imghdr::Type::Webp), "./tests/images/example.webp");
}

#[test]
fn test_exr() {
assert_result!(Some(imghdr::Type::Exr), "./tests/images/example.exr");
}

#[test]
fn test_bmp() {
assert_result!(Some(imghdr::Type::Bmp), "./tests/images/example.bmp");
Expand Down Expand Up @@ -94,6 +99,11 @@ fn test_rgb() {
assert_result!(Some(imghdr::Type::Rgb), "./tests/images/example.rgb");
}

#[test]
fn test_rgbe() {
assert_result!(Some(imghdr::Type::Rgbe), "./tests/images/example.hdr");
}

#[test]
fn test_flif() {
assert_result!(Some(imghdr::Type::Flif), "./tests/images/example.flif");
Expand Down

0 comments on commit 9a751c3

Please sign in to comment.