Read out short vecs and ascii from ifds

This commit is contained in:
ruffson committed 2020-11-16 18:52:04 +01:00
1 parent 83004c8344
commit 3953bd31e1
2 files changed
+49 -2

No files matched your search

+40 -2
View File
@@ -10,14 +10,15 @@ use crate::tags::{Tag, Type};
use crate::{TiffError, TiffFormatError, TiffResult, TiffUnsupportedError}; use crate::{TiffError, TiffFormatError, TiffResult, TiffUnsupportedError};
use self::Value::{ use self::Value::{
Ascii, Byte, Double, Float, List, Rational, RationalBig, SRational, SRationalBig, Signed, Ascii, Byte, Double, Float, List, Rational, RationalBig, SRational, SRationalBig, Short,
SignedBig, Unsigned, UnsignedBig, Signed, SignedBig, Unsigned, UnsignedBig,
}; };
#[allow(unused_qualifications)] #[allow(unused_qualifications)]
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum Value { pub enum Value {
Byte(u8), Byte(u8),
Short(u16),
Signed(i32), Signed(i32),
SignedBig(i64), SignedBig(i64),
Unsigned(u32), Unsigned(u32),
@@ -42,6 +43,17 @@ impl Value {
} }
} }
pub fn into_u16(self) -> TiffResult<u16> {
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<u32> { pub fn into_u32(self) -> TiffResult<u32> {
match self { match self {
Unsigned(val) => Ok(val), Unsigned(val) => Ok(val),
@@ -100,6 +112,15 @@ impl Value {
} }
} }
pub fn into_string(self) -> TiffResult<String> {
match self {
Ascii(val) => Ok(val),
val => Err(TiffError::FormatError(
TiffFormatError::SignedIntegerExpected(val),
)),
}
}
pub fn into_u32_vec(self) -> TiffResult<Vec<u32>> { pub fn into_u32_vec(self) -> TiffResult<Vec<u32>> {
match self { match self {
List(vec) => { List(vec) => {
@@ -139,6 +160,22 @@ impl Value {
} }
} }
pub fn into_u16_vec(self) -> TiffResult<Vec<u16>> {
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<Vec<i32>> { pub fn into_i32_vec(self) -> TiffResult<Vec<i32>> {
match self { match self {
List(vec) => { List(vec) => {
@@ -400,6 +437,7 @@ impl Entry {
Signed(i32::from(r.read_i16()?)), 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| { (Type::SHORT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
Ok(UnsignedBig(u64::from(decoder.read_short()?))) Ok(UnsignedBig(u64::from(decoder.read_short()?)))
}), }),
+9
View File
@@ -728,6 +728,10 @@ impl<R: Read + Seek> Decoder<R> {
pub fn get_tag_u32_vec(&mut self, tag: Tag) -> TiffResult<Vec<u32>> { pub fn get_tag_u32_vec(&mut self, tag: Tag) -> TiffResult<Vec<u32>> {
self.get_tag(tag)?.into_u32_vec() self.get_tag(tag)?.into_u32_vec()
} }
pub fn get_tag_u16_vec(&mut self, tag: Tag) -> TiffResult<Vec<u16>> {
self.get_tag(tag)?.into_u16_vec()
}
pub fn get_tag_u64_vec(&mut self, tag: Tag) -> TiffResult<Vec<u64>> { pub fn get_tag_u64_vec(&mut self, tag: Tag) -> TiffResult<Vec<u64>> {
self.get_tag(tag)?.into_u64_vec() self.get_tag(tag)?.into_u64_vec()
} }
@@ -747,6 +751,11 @@ impl<R: Read + Seek> Decoder<R> {
self.get_tag(tag)?.into_u8_vec() 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<String> {
self.get_tag(tag)?.into_string()
}
/// Decompresses the strip into the supplied buffer. /// Decompresses the strip into the supplied buffer.
/// Returns the number of bytes read. /// Returns the number of bytes read.
fn expand_strip<'a>( fn expand_strip<'a>(