feat: Add decoding value and get tags as f32 or f64
This commit is contained in:
1 parent
54c3ebf358
commit
3b0da4d1ae
3 files changed
+83
-3
No files matched your search
+60
-2
@@ -9,14 +9,16 @@ use super::stream::{ByteOrder, EndianReader, SmartReader};
|
||||
use tags::{Tag, Type};
|
||||
use {TiffError, TiffFormatError, TiffResult, TiffUnsupportedError};
|
||||
|
||||
use self::Value::{Ascii, List, Rational, Unsigned, Signed, SRational};
|
||||
use self::Value::{Ascii, List, Rational, Unsigned, Signed, SRational, Float, Double};
|
||||
|
||||
|
||||
#[allow(unused_qualifications)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Value {
|
||||
Signed(i32),
|
||||
Unsigned(u32),
|
||||
Float(f32),
|
||||
Double(f64),
|
||||
List(Vec<Value>),
|
||||
Rational(u32, u32),
|
||||
SRational(i32, i32),
|
||||
@@ -44,6 +46,24 @@ impl Value {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_f32(self) -> TiffResult<f32> {
|
||||
match self {
|
||||
Float(val) => Ok(val),
|
||||
val => Err(TiffError::FormatError(
|
||||
TiffFormatError::SignedIntegerExpected(val),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_f64(self) -> TiffResult<f64> {
|
||||
match self {
|
||||
Double(val) => Ok(val),
|
||||
val => Err(TiffError::FormatError(
|
||||
TiffFormatError::SignedIntegerExpected(val),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_u32_vec(self) -> TiffResult<Vec<u32>> {
|
||||
match self {
|
||||
List(vec) => {
|
||||
@@ -84,6 +104,38 @@ impl Value {
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_f32_vec(self) -> TiffResult<Vec<f32>> {
|
||||
match self {
|
||||
List(vec) => {
|
||||
let mut new_vec = Vec::with_capacity(vec.len());
|
||||
for v in vec {
|
||||
new_vec.push(v.into_f32()?)
|
||||
}
|
||||
Ok(new_vec)
|
||||
}
|
||||
Float(val) => Ok(vec![val]),
|
||||
val => Err(TiffError::FormatError(
|
||||
TiffFormatError::UnsignedIntegerExpected(val),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_f64_vec(self) -> TiffResult<Vec<f64>> {
|
||||
match self {
|
||||
List(vec) => {
|
||||
let mut new_vec = Vec::with_capacity(vec.len());
|
||||
for v in vec {
|
||||
new_vec.push(v.into_f64()?)
|
||||
}
|
||||
Ok(new_vec)
|
||||
}
|
||||
Double(val) => Ok(vec![val]),
|
||||
val => Err(TiffError::FormatError(
|
||||
TiffFormatError::UnsignedIntegerExpected(val),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -169,6 +221,12 @@ impl Entry {
|
||||
(Type::SLONG, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
||||
Ok(Signed(decoder.read_slong()?))
|
||||
}),
|
||||
(Type::FLOAT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
||||
Ok(Float(decoder.read_float()?))
|
||||
}),
|
||||
(Type::DOUBLE, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
||||
Ok(Double(decoder.read_double()?))
|
||||
}),
|
||||
(Type::RATIONAL, 1) => {
|
||||
decoder.goto_offset(self.r(bo).read_u32()?)?;
|
||||
let numerator = decoder.read_long()?;
|
||||
|
||||
@@ -483,6 +483,18 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
self.reader.read_i32()
|
||||
}
|
||||
|
||||
/// Reads a TIFF float value
|
||||
#[inline]
|
||||
pub fn read_float(&mut self) -> Result<f32, io::Error> {
|
||||
self.reader.read_f32()
|
||||
}
|
||||
|
||||
/// Reads a TIFF double value
|
||||
#[inline]
|
||||
pub fn read_double(&mut self) -> Result<f64, io::Error> {
|
||||
self.reader.read_f64()
|
||||
}
|
||||
|
||||
/// Reads a string
|
||||
#[inline]
|
||||
pub fn read_string(&mut self, length: usize) -> TiffResult<String> {
|
||||
@@ -625,6 +637,16 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
self.get_tag(tag)?.into_u32_vec()
|
||||
}
|
||||
|
||||
/// Tries to retrieve a tag and convert it to the desired type.
|
||||
pub fn get_tag_f32_vec(&mut self, tag: Tag) -> TiffResult<Vec<f32>> {
|
||||
self.get_tag(tag)?.into_f32_vec()
|
||||
}
|
||||
|
||||
/// Tries to retrieve a tag and convert it to the desired type.
|
||||
pub fn get_tag_f64_vec(&mut self, tag: Tag) -> TiffResult<Vec<f64>> {
|
||||
self.get_tag(tag)?.into_f64_vec()
|
||||
}
|
||||
|
||||
/// Decompresses the strip into the supplied buffer.
|
||||
/// Returns the number of bytes read.
|
||||
fn expand_strip<'a>(
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ pub enum TiffError {
|
||||
///
|
||||
/// The list of variants may grow to incorporate errors of future features. Matching against this
|
||||
/// exhaustively is not covered by interface stability guarantees.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum TiffFormatError {
|
||||
TiffSignatureNotFound,
|
||||
TiffSignatureInvalid,
|
||||
|
||||
Reference in new issue
Block a user