From 3953bd31e14185cd5d7726eda2556623ee73cf0e Mon Sep 17 00:00:00 2001 From: ruffson Date: Sat, 17 Oct 2020 11:27:41 +0200 Subject: [PATCH] Read out short vecs and ascii from ifds --- src/decoder/ifd.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- src/decoder/mod.rs | 9 +++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index 2100833..edfa054 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -10,14 +10,15 @@ use crate::tags::{Tag, Type}; use crate::{TiffError, TiffFormatError, TiffResult, TiffUnsupportedError}; use self::Value::{ - Ascii, Byte, Double, Float, List, Rational, RationalBig, SRational, SRationalBig, Signed, - SignedBig, Unsigned, UnsignedBig, + Ascii, Byte, Double, Float, List, Rational, RationalBig, SRational, SRationalBig, Short, + Signed, SignedBig, Unsigned, UnsignedBig, }; #[allow(unused_qualifications)] #[derive(Debug, Clone, PartialEq)] pub enum Value { Byte(u8), + Short(u16), Signed(i32), SignedBig(i64), Unsigned(u32), @@ -42,6 +43,17 @@ impl Value { } } + pub fn into_u16(self) -> TiffResult { + match self { + Short(val) => Ok(val), + Unsigned(val) => Ok(u16::try_from(val)?), + UnsignedBig(val) => Ok(u16::try_from(val)?), + val => Err(TiffError::FormatError( + TiffFormatError::UnsignedIntegerExpected(val), + )), + } + } + pub fn into_u32(self) -> TiffResult { match self { Unsigned(val) => Ok(val), @@ -100,6 +112,15 @@ impl Value { } } + pub fn into_string(self) -> TiffResult { + match self { + Ascii(val) => Ok(val), + val => Err(TiffError::FormatError( + TiffFormatError::SignedIntegerExpected(val), + )), + } + } + pub fn into_u32_vec(self) -> TiffResult> { match self { List(vec) => { @@ -139,6 +160,22 @@ impl Value { } } + pub fn into_u16_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_u16()?) + } + Ok(new_vec) + } + Short(val) => Ok(vec![val]), + val => Err(TiffError::FormatError( + TiffFormatError::UnsignedIntegerExpected(val), + )), + } + } + pub fn into_i32_vec(self) -> TiffResult> { match self { List(vec) => { @@ -400,6 +437,7 @@ impl Entry { Signed(i32::from(r.read_i16()?)), ])) } + (Type::SHORT, 16) => Ok(Short(u16::from(decoder.read_short()?))), (Type::SHORT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { Ok(UnsignedBig(u64::from(decoder.read_short()?))) }), diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 4105b89..5b99382 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -728,6 +728,10 @@ impl Decoder { pub fn get_tag_u32_vec(&mut self, tag: Tag) -> TiffResult> { self.get_tag(tag)?.into_u32_vec() } + + pub fn get_tag_u16_vec(&mut self, tag: Tag) -> TiffResult> { + self.get_tag(tag)?.into_u16_vec() + } pub fn get_tag_u64_vec(&mut self, tag: Tag) -> TiffResult> { self.get_tag(tag)?.into_u64_vec() } @@ -747,6 +751,11 @@ impl Decoder { self.get_tag(tag)?.into_u8_vec() } + /// Tries to retrieve a tag and convert it to a ascii vector. + pub fn get_tag_ascii_string(&mut self, tag: Tag) -> TiffResult { + self.get_tag(tag)?.into_string() + } + /// Decompresses the strip into the supplied buffer. /// Returns the number of bytes read. fn expand_strip<'a>(