diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index 2fbaee1..ced9a81 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -10,8 +10,8 @@ use tags::{Tag, Type}; use {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, Signed, + SignedBig, Unsigned, UnsignedBig, }; #[allow(unused_qualifications)] @@ -38,9 +38,7 @@ impl Value { pub fn into_u8(self) -> TiffResult { match self { Byte(val) => Ok(val), - val => Err(TiffError::FormatError( - TiffFormatError::ByteExpected(val), - )), + val => Err(TiffError::FormatError(TiffFormatError::ByteExpected(val))), } } @@ -452,7 +450,7 @@ impl Entry { let string = decoder.read_string(n)?; Ok(Ascii(string)) } - }, + } (Type::UNDEFINED, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { Ok(Byte(u8::from(decoder.read_byte()?))) }), diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index cda4d76..b552cd6 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -933,7 +933,12 @@ impl Decoder { pub fn read_jpeg(&mut self) -> TiffResult { let offsets = self.get_tag_u32_vec(Tag::StripOffsets)?; let bytes = self.get_tag_u32_vec(Tag::StripByteCounts)?; - let jpeg_tables: Vec = self.get_tag_u8_vec(Tag::JPEGTables)?; + + let jpeg_tables: Option> = match self.find_tag(Tag::JPEGTables) { + Ok(None) => None, + Ok(_) => Some(self.get_tag_u8_vec(Tag::JPEGTables)?), + Err(e) => return Err(e), + }; if offsets.len() == 0 { return Err(TiffError::FormatError(TiffFormatError::RequiredTagEmpty( diff --git a/src/decoder/stream.rs b/src/decoder/stream.rs index 6b77c3d..c538b94 100644 --- a/src/decoder/stream.rs +++ b/src/decoder/stream.rs @@ -329,7 +329,7 @@ pub(crate) struct JpegReader { impl JpegReader { /// Constructs new JpegReader wrapping a SmartReader. /// Because JPEG compression in TIFF allows to save quantization and/or huffman tables in one - /// central loacation, the constructor accepts this data as `jpeg_tables` here containing either + /// central location, the constructor accepts this data as `jpeg_tables` here containing either /// or both. /// These `jpeg_tables` are simply prepended to the remaining jpeg image data. /// Because these `jpeg_tables` start with a `SOI` (HEX: `0xFFD8`) or __start of image__ marker @@ -342,7 +342,7 @@ impl JpegReader { pub fn new( reader: &mut SmartReader, length: u32, - jpeg_tables: &Vec, + jpeg_tables: &Option>, ) -> io::Result where R: Read + Seek, @@ -353,15 +353,25 @@ impl JpegReader { let mut segment = vec![0; length as usize]; reader.read_exact(&mut segment[..])?; - let mut jpeg_data = jpeg_tables.clone(); - jpeg_data.truncate(jpeg_data.len() - 2); + match jpeg_tables { + Some(tables) => { + let mut jpeg_data = tables.clone(); + jpeg_data.truncate(jpeg_data.len() - 2); + jpeg_data.extend_from_slice(&mut segment[2..]); - jpeg_data.extend_from_slice(&mut segment[2..]); + Ok(JpegReader { + buffer: io::Cursor::new(jpeg_data), + byte_order: order, + }) + }, + None => { + Ok(JpegReader { + buffer: io::Cursor::new(segment), + byte_order: order, + }) + } + } - Ok(JpegReader { - buffer: io::Cursor::new(jpeg_data), - byte_order: order, - }) } } diff --git a/src/error.rs b/src/error.rs index 15e0e7e..51aef38 100644 --- a/src/error.rs +++ b/src/error.rs @@ -72,9 +72,7 @@ impl fmt::Display for TiffFormatError { UnknownPredictor(ref predictor) => { write!(fmt, "Unknown predictor “{}” encountered", predictor) } - ByteExpected(ref val) => { - write!(fmt, "Expected byte, {:?} found.", val) - } + ByteExpected(ref val) => write!(fmt, "Expected byte, {:?} found.", val), UnsignedIntegerExpected(ref val) => { write!(fmt, "Expected unsigned integer, {:?} found.", val) } @@ -82,12 +80,8 @@ impl fmt::Display for TiffFormatError { write!(fmt, "Expected signed integer, {:?} found.", val) } InflateError(_) => write!(fmt, "Failed to decode inflate data."), - Format(ref val) => { - write!(fmt, "Invalid format: {:?}.", val) - } - RequiredTagEmpty(ref val) => { - write!(fmt, "Required tag {:?} was empty.", val) - } + Format(ref val) => write!(fmt, "Invalid format: {:?}.", val), + RequiredTagEmpty(ref val) => write!(fmt, "Required tag {:?} was empty.", val), __NonExhaustive => unreachable!(), } }