Fix error handling
This commit is contained in:
1 parent
72474b569e
commit
5fd0534ea2
2 files changed
+31
-17
No files matched your search
+27
-17
@@ -9,7 +9,7 @@ use self::ifd::Directory;
|
|||||||
use tags::{CompressionMethod, PhotometricInterpretation, Predictor, SampleFormat, Tag, Type};
|
use tags::{CompressionMethod, PhotometricInterpretation, Predictor, SampleFormat, Tag, Type};
|
||||||
|
|
||||||
use self::stream::{
|
use self::stream::{
|
||||||
ByteOrder, DeflateReader, EndianReader, LZWReader, PackBitsReader, SmartReader, JpegReader
|
ByteOrder, DeflateReader, EndianReader, JpegReader, LZWReader, PackBitsReader, SmartReader,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod ifd;
|
pub mod ifd;
|
||||||
@@ -956,18 +956,21 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_jpeg(&mut self, read_strip_wise: bool) -> TiffResult<DecodingResult> {
|
pub fn read_jpeg(&mut self) -> TiffResult<DecodingResult> {
|
||||||
|
|
||||||
let offsets: Vec<u32>;
|
let offsets: Vec<u32>;
|
||||||
let bytes: Vec<u32>;
|
let bytes: Vec<u32>;
|
||||||
let jpeg_tables: Vec<u8> = self.get_tag_u8_vec(Tag::JPEGTables)?;
|
let jpeg_tables: Vec<u8> = self.get_tag_u8_vec(Tag::JPEGTables)?;
|
||||||
|
|
||||||
if read_strip_wise {
|
match self.find_tag(Tag::TileOffsets) {
|
||||||
offsets = self.get_tag_u32_vec(Tag::StripOffsets)?;
|
Ok(None) => {
|
||||||
bytes = self.get_tag_u32_vec(Tag::StripByteCounts)?;
|
offsets = self.get_tag_u32_vec(Tag::StripOffsets)?;
|
||||||
} else {
|
bytes = self.get_tag_u32_vec(Tag::StripByteCounts)?;
|
||||||
offsets = self.get_tag_u32_vec(Tag::TileOffsets)?;
|
}
|
||||||
bytes = self.get_tag_u32_vec(Tag::TileByteCounts)?;
|
Ok(_) => {
|
||||||
|
offsets = self.get_tag_u32_vec(Tag::TileOffsets)?;
|
||||||
|
bytes = self.get_tag_u32_vec(Tag::TileByteCounts)?;
|
||||||
|
}
|
||||||
|
Err(e) => return Err(e),
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut res_img = Vec::with_capacity(offsets[0] as usize);
|
let mut res_img = Vec::with_capacity(offsets[0] as usize);
|
||||||
@@ -978,10 +981,21 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
let mut decoder = jpeg::Decoder::new(jpeg_reader);
|
let mut decoder = jpeg::Decoder::new(jpeg_reader);
|
||||||
|
|
||||||
match decoder.decode() {
|
match decoder.decode() {
|
||||||
Err(e) => {
|
|
||||||
panic!("Error when reading jpeg tiles: {}", e);
|
|
||||||
}
|
|
||||||
Ok(mut val) => res_img.append(&mut val),
|
Ok(mut val) => res_img.append(&mut val),
|
||||||
|
Err(e) => {
|
||||||
|
return match e {
|
||||||
|
jpeg::Error::Io(io_err) => Err(TiffError::IoError(io_err)),
|
||||||
|
jpeg::Error::Format(fmt_err) => {
|
||||||
|
Err(TiffError::FormatError(TiffFormatError::Format(fmt_err)))
|
||||||
|
}
|
||||||
|
jpeg::Error::Unsupported(_) => Err(TiffError::UnsupportedError(
|
||||||
|
TiffUnsupportedError::UnknownInterpretation,
|
||||||
|
)),
|
||||||
|
jpeg::Error::Internal(_) => Err(TiffError::UnsupportedError(
|
||||||
|
TiffUnsupportedError::UnknownInterpretation,
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1110,12 +1124,8 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
|
|
||||||
/// Decodes the entire image and return it as a Vector
|
/// Decodes the entire image and return it as a Vector
|
||||||
pub fn read_image(&mut self) -> TiffResult<DecodingResult> {
|
pub fn read_image(&mut self) -> TiffResult<DecodingResult> {
|
||||||
|
|
||||||
if self.compression_method == CompressionMethod::ModernJPEG {
|
if self.compression_method == CompressionMethod::ModernJPEG {
|
||||||
match self.find_tag(Tag::TileOffsets) {
|
return self.read_jpeg();
|
||||||
Ok(None) | Err(_) => return self.read_jpeg(true),
|
|
||||||
Ok(_) => return self.read_jpeg(false),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.initialize_strip_decoder()?;
|
self.initialize_strip_decoder()?;
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ pub enum TiffFormatError {
|
|||||||
UnsignedIntegerExpected(Value),
|
UnsignedIntegerExpected(Value),
|
||||||
SignedIntegerExpected(Value),
|
SignedIntegerExpected(Value),
|
||||||
InflateError(InflateError),
|
InflateError(InflateError),
|
||||||
|
Format(String),
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
/// Do not match against this variant. It may get removed.
|
/// Do not match against this variant. It may get removed.
|
||||||
__NonExhaustive,
|
__NonExhaustive,
|
||||||
@@ -80,6 +81,9 @@ impl fmt::Display for TiffFormatError {
|
|||||||
write!(fmt, "Expected signed integer, {:?} found.", val)
|
write!(fmt, "Expected signed integer, {:?} found.", val)
|
||||||
}
|
}
|
||||||
InflateError(_) => write!(fmt, "Failed to decode inflate data."),
|
InflateError(_) => write!(fmt, "Failed to decode inflate data."),
|
||||||
|
Format(ref val) => {
|
||||||
|
write!(fmt, "Invalid format: {:?}.", val)
|
||||||
|
}
|
||||||
__NonExhaustive => unreachable!(),
|
__NonExhaustive => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user