diff --git a/Cargo.toml b/Cargo.toml index 3afb300..bdba5b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ byteorder = "1.2" lzw = "0.10" num-derive = "0.3" num-traits = "0.2" -miniz_oxide = "0.3" # TODO: Put behind a feature? +miniz_oxide = "0.3" [dev-dependencies] tempfile = "3.0" diff --git a/src/decoder/stream.rs b/src/decoder/stream.rs index c5c0a44..58a6350 100644 --- a/src/decoder/stream.rs +++ b/src/decoder/stream.rs @@ -1,9 +1,10 @@ //! All IO functionality needed for TIFF decoding +use crate::error::TiffResult; use byteorder::{BigEndian, LittleEndian, ReadBytesExt}; use lzw; -use std::io::{self, Read, Seek}; use miniz_oxide::inflate; +use std::io::{self, Read, Seek}; /// Byte order of the TIFF file. #[derive(Clone, Copy, Debug)] @@ -75,15 +76,12 @@ pub struct DeflateReader { } impl DeflateReader { - pub fn new( - reader: &mut SmartReader, - ) -> io::Result<(usize, Self)> { + pub fn new(reader: &mut SmartReader) -> TiffResult<(usize, Self)> { let byte_order = reader.byte_order; let mut compressed = Vec::new(); reader.read_to_end(&mut compressed)?; - let uncompressed = inflate::decompress_to_vec_zlib(&compressed).map_err(|error| { - std::io::Error::new(std::io::ErrorKind::InvalidData, format!("DEFLATE error: {:?}", error)) - })?; + let uncompressed = inflate::decompress_to_vec_zlib(&compressed)?; + Ok(( uncompressed.len(), Self { diff --git a/src/error.rs b/src/error.rs index 69402b0..cbe7e3f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,6 +5,7 @@ use std::string; use decoder::ifd::{Tag, Value}; use decoder::{CompressionMethod, PhotometricInterpretation, PlanarConfiguration}; +use miniz_oxide::inflate::TINFLStatus; use ColorType; /// Tiff error kinds. @@ -34,6 +35,7 @@ pub enum TiffFormatError { UnknownPredictor(u32), UnsignedIntegerExpected(Value), SignedIntegerExpected(Value), + InflateError(InflateError), } impl fmt::Display for TiffFormatError { @@ -55,10 +57,28 @@ impl fmt::Display for TiffFormatError { SignedIntegerExpected(ref val) => { write!(fmt, "Expected signed integer, {:?} found.", val) } + InflateError(_) => write!(fmt, "Failed to decode inflate data."), } } } +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct InflateError { + status: TINFLStatus, +} + +impl From for TiffFormatError { + fn from(status: TINFLStatus) -> Self { + TiffFormatError::InflateError(InflateError { status }) + } +} + +impl From for TiffError { + fn from(status: TINFLStatus) -> Self { + TiffError::FormatError(status.into()) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum TiffUnsupportedError { HorizontalPredictor(ColorType), @@ -138,7 +158,7 @@ impl Error for TiffError { } } - fn cause(&self) -> Option<& dyn Error> { + fn cause(&self) -> Option<&dyn Error> { match *self { TiffError::IoError(ref e) => Some(e), _ => None,