diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index e8f8f16..acbe484 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -10,13 +10,14 @@ use tags::{Tag, Type}; use {TiffError, TiffFormatError, TiffResult, TiffUnsupportedError}; use self::Value::{ - Ascii, Double, Float, List, Rational, RationalBig, SRational, SRationalBig, Signed, SignedBig, + Ascii, Byte, Double, Float, List, Rational, RationalBig, SRational, SRationalBig, Signed, SignedBig, Unsigned, UnsignedBig, }; #[allow(unused_qualifications)] #[derive(Debug, Clone, PartialEq)] pub enum Value { + Byte(u8), Signed(i32), SignedBig(i64), Unsigned(u32), @@ -34,6 +35,16 @@ pub enum Value { } impl Value { + + pub fn into_u8(self) -> TiffResult { + match self { + Byte(val) => Ok(val), + val => Err(TiffError::FormatError( + TiffFormatError::UnsignedIntegerExpected(val), + )), + } + } + pub fn into_u32(self) -> TiffResult { match self { Unsigned(val) => Ok(val), @@ -114,6 +125,23 @@ impl Value { } } + pub fn into_u8_vec(self) -> TiffResult> { + match self { + List(vec) => { + let mut new_vec = Vec::with_capacity(vec.len()); + for v in vec { + new_vec.push(v.into_u8()?) + } + Ok(new_vec) + } + Byte(val) => Ok(vec![val]), + + val => Err(TiffError::FormatError( + TiffFormatError::UnsignedIntegerExpected(val), + )), + } + } + pub fn into_i32_vec(self) -> TiffResult> { match self { List(vec) => { diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 6d6281f..40dd25c 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -746,6 +746,11 @@ impl Decoder { self.get_tag(tag)?.into_f64_vec() } + /// Tries to retrieve a tag and convert it to a 8bit vector. + pub fn get_tag_u8_vec(&mut self, tag: Tag) -> TiffResult> { + self.get_tag(tag)?.into_u8_vec() + } + /// Decompresses the strip into the supplied buffer. /// Returns the number of bytes read. fn expand_strip<'a>(