From e9aa39d8956e255391ac9fba731e8b1825bff8c6 Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Thu, 6 Jan 2022 17:43:23 -0500 Subject: [PATCH] Initialize Decoder in new instead of init --- src/decoder/mod.rs | 119 +++++++++++++++++++++--------------------- src/decoder/stream.rs | 3 ++ 2 files changed, 62 insertions(+), 60 deletions(-) diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index bd48457..b1c1346 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -525,12 +525,58 @@ fn rev_hpredict( impl Decoder { /// Create a new decoder that decodes from the stream ```r``` pub fn new(r: R) -> TiffResult> { - Decoder { - reader: SmartReader::wrap(r, ByteOrder::LittleEndian), - byte_order: ByteOrder::LittleEndian, - bigtiff: false, + let mut endianess = Vec::with_capacity(2); + r.take(2).read_to_end(&mut endianess)?; + let byte_order = match &*endianess { + b"II" => { + ByteOrder::LittleEndian + } + b"MM" => { + ByteOrder::BigEndian + } + _ => { + return Err(TiffError::FormatError( + TiffFormatError::TiffSignatureNotFound, + )) + } + }; + let mut reader = SmartReader::wrap(r, byte_order); + + let bigtiff = match reader.read_u16()? { + 42 => false, + 43 => { + // Read bytesize of offsets (in bigtiff it's alway 8 but provide a way to move to 16 some day) + if reader.read_u16()? != 8 { + return Err(TiffError::FormatError( + TiffFormatError::TiffSignatureNotFound, + )); + } + // This constant should always be 0 + if reader.read_u16()? != 0 { + return Err(TiffError::FormatError( + TiffFormatError::TiffSignatureNotFound, + )); + } + true + } + _ => { + return Err(TiffError::FormatError( + TiffFormatError::TiffSignatureInvalid, + )) + } + }; + let next_ifd = if bigtiff { + Some(reader.read_u64()?) + } else { + Some(u64::from(reader.read_u32()?)) + }; + + let decoder = Decoder { + reader, + byte_order, + bigtiff, limits: Default::default(), - next_ifd: None, + next_ifd, ifd: None, width: 0, height: 0, @@ -544,8 +590,9 @@ impl Decoder { tile_decoder: None, tile_attributes: None, seen_ifds: HashSet::new(), - } - .init() + }; + decoder.next_image()?; + Ok(decoder) } pub fn with_limits(mut self, limits: Limits) -> Decoder { @@ -599,59 +646,11 @@ impl Decoder { } } - fn read_header(&mut self) -> TiffResult<()> { - let mut endianess = Vec::with_capacity(2); - self.reader.by_ref().take(2).read_to_end(&mut endianess)?; - match &*endianess { - b"II" => { - self.byte_order = ByteOrder::LittleEndian; - self.reader.byte_order = ByteOrder::LittleEndian; - } - b"MM" => { - self.byte_order = ByteOrder::BigEndian; - self.reader.byte_order = ByteOrder::BigEndian; - } - _ => { - return Err(TiffError::FormatError( - TiffFormatError::TiffSignatureNotFound, - )) - } - } - match self.read_short()? { - 42 => self.bigtiff = false, - 43 => { - self.bigtiff = true; - // Read bytesize of offsets (in bigtiff it's alway 8 but provide a way to move to 16 some day) - if self.read_short()? != 8 { - return Err(TiffError::FormatError( - TiffFormatError::TiffSignatureNotFound, - )); - } - // This constant should always be 0 - if self.read_short()? != 0 { - return Err(TiffError::FormatError( - TiffFormatError::TiffSignatureNotFound, - )); - } - } - _ => { - return Err(TiffError::FormatError( - TiffFormatError::TiffSignatureInvalid, - )) - } - } - self.next_ifd = match self.read_ifd_offset_nonrepeating()? { - 0 => None, - n => Some(n), - }; - Ok(()) - } - - /// Initializes the decoder. - pub fn init(mut self) -> TiffResult> { - self.read_header()?; - self.next_image()?; - Ok(self) + /// Reset the decoder. + #[deprecated = "Never should have been public. Only use Decoder::new()"] + pub fn init(self) -> TiffResult> { + let Self { reader, .. } = self; + Self::new(reader.into_inner()) } /// Reads in the next image. diff --git a/src/decoder/stream.rs b/src/decoder/stream.rs index c1c990d..e64fef9 100644 --- a/src/decoder/stream.rs +++ b/src/decoder/stream.rs @@ -339,6 +339,9 @@ where pub fn wrap(reader: R, byte_order: ByteOrder) -> SmartReader { SmartReader { reader, byte_order } } + pub fn into_inner(self) -> R { + self.reader + } } impl EndianReader for SmartReader