diff --git a/src/value.rs b/src/value.rs index 12fab3b..e5e55a9 100644 --- a/src/value.rs +++ b/src/value.rs @@ -25,6 +25,7 @@ // use std::fmt; +use std::mem; use endian::Endian; @@ -55,6 +56,9 @@ pub enum Value<'a> { /// Vector of signed rationals. /// A signed rational number is a pair of 32-bit signed integers. SRational(Vec), + /// Vector of 32-bit (single precision) floating-point numbers. + /// Unused in the Exif specification. + Float(Vec), /// The type is unknown to this implementation. /// The associated values are the type and the count, and the /// offset of the "Value Offset" element. @@ -95,6 +99,7 @@ pub fn get_type_info<'a, E>(typecode: u16) 8 => (2, parse_sshort::), 9 => (4, parse_slong::), 10 => (8, parse_srational::), + 11 => (4, parse_float::), _ => (0, parse_unknown), } } @@ -189,6 +194,16 @@ fn parse_srational<'a, E>(data: &'a [u8], offset: usize, count: usize) Value::SRational(val) } +// TIFF and Rust use IEEE 754 format, so no conversion is required. +fn parse_float<'a, E>(data: &'a [u8], offset: usize, count: usize) + -> Value<'a> where E: Endian { + let mut val = Vec::with_capacity(count); + for i in 0..count { + val.push(unsafe { mem::transmute(E::loadu32(data, offset + i * 4)) }); + } + Value::Float(val) +} + // This is a dummy function and will never be called. #[allow(unused_variables)] fn parse_unknown<'a>(data: &'a [u8], offset: usize, count: usize) @@ -383,4 +398,21 @@ mod tests { } } } + + #[test] + fn float() { + let sets: &[(&[u8], Vec)] = &[ + (b"x", vec![]), + (b"x\x7f\x7f\xff\xff\x80\x80\x00\x00\x40\x00\x00\x00", + vec![::std::f32::MAX, -::std::f32::MIN_POSITIVE, 2.0]), + ]; + let (unitlen, parser) = get_type_info::(11); + for &(data, ref ans) in sets { + assert!((data.len() - 1) % unitlen == 0); + match parser(data, 1, (data.len() - 1) / unitlen) { + Value::Float(v) => assert_eq!(v, *ans), + v => panic!("wrong variant {:?}", v), + } + } + } }