diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index c80197f..aeb3fa7 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -241,6 +241,7 @@ pub enum ChunkType { Tile, } +#[derive(Debug)] #[non_exhaustive] pub struct ChunkInfo { /// Width of the chunk as specified (includes potential padding) @@ -1085,6 +1086,9 @@ impl Decoder { pub fn read_strip_to_buffer(&mut self, mut buffer: DecodingBuffer) -> TiffResult<()> { self.check_chunk_type(ChunkType::Strip)?; + let chunk_info = self + .chunk_info(index) + .ok_or(TiffFormatError::InvalidChunkIndex(index))?; let offset = self.image.chunk_file_range(self.current_chunk)?.0; self.goto_offset_u64(offset)?; let chunk_info = self.chunk_info(index).ok_or(TiffError::IntSizeError)?; @@ -1107,30 +1111,25 @@ impl Decoder { fn read_tile_to_buffer( &mut self, result: &mut DecodingBuffer, - tile: usize, + tile_index: usize, output_width: usize, ) -> TiffResult<()> { let file_offset = *self .image() .chunk_offsets - .get(tile) - .ok_or(TiffError::FormatError( - TiffFormatError::InconsistentSizesEncountered, - ))?; + .get(tile_index) + .ok_or(TiffFormatError::InvalidChunkIndex(tile_index))?; - let compressed_bytes = - *self - .image() - .chunk_bytes - .get(tile) - .ok_or(TiffError::FormatError( - TiffFormatError::InconsistentSizesEncountered, - ))?; + let compressed_bytes = *self + .image() + .chunk_bytes + .get(tile_index) + .ok_or(TiffFormatError::InvalidChunkIndex(tile_index))?; // Return the same error as above if the chunk doesn't exist - let chunk_info = self.chunk_info(tile).ok_or(TiffError::FormatError( - TiffFormatError::InconsistentSizesEncountered, - ))?; + let chunk_info = self + .chunk_info(tile_index) + .ok_or(TiffFormatError::InvalidChunkIndex(tile_index))?; self.expand_tile( result.copy(), @@ -1214,10 +1213,15 @@ impl Decoder { /// Read a single strip from the image and return it as a Vector pub fn read_strip(&mut self) -> TiffResult<(DecodingResult, ChunkInfo)> { - self.check_chunk_type(ChunkType::Strip)?; - let index = self.current_chunk; + self.read_strip_at(self.current_chunk) + } - let chunk_info = self.chunk_info(index).unwrap(); + pub fn read_strip_at(&mut self, strip_index: usize) -> TiffResult<(DecodingResult, ChunkInfo)> { + self.check_chunk_type(ChunkType::Strip)?; + + let chunk_info = self + .chunk_info(strip_index) + .ok_or(TiffFormatError::InvalidChunkIndex(strip_index))?; let mut result = self.result_buffer(chunk_info.data_width, chunk_info.data_height)?; self.read_strip_to_buffer(result.as_buffer(0))?; @@ -1237,7 +1241,9 @@ impl Decoder { pub fn read_tile_at(&mut self, tile_index: usize) -> TiffResult<(DecodingResult, ChunkInfo)> { self.check_chunk_type(ChunkType::Tile)?; - let chunk_info = self.chunk_info(tile_index).unwrap(); + let chunk_info = self + .chunk_info(tile_index) + .ok_or(TiffFormatError::InvalidChunkIndex(tile_index))?; let mut result = self.result_buffer(chunk_info.data_width, chunk_info.data_height)?; diff --git a/src/error.rs b/src/error.rs index 45ea5b0..c94c2f9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -56,6 +56,7 @@ pub enum TiffFormatError { actual_samples: usize, required_samples: usize, }, + InvalidChunkIndex(usize), InvalidTag, InvalidTagValueType(Tag), RequiredTagNotFound(Tag), @@ -100,6 +101,7 @@ impl fmt::Display for TiffFormatError { actual_samples, required_samples ) } + InvalidChunkIndex(index) => write!(fmt, "Image chunk index ({}) requested.", index), InvalidTag => write!(fmt, "Image contains invalid tag."), InvalidTagValueType(ref tag) => { write!(fmt, "Tag `{:?}` did not have the expected value type.", tag) diff --git a/tests/decode_images.rs b/tests/decode_images.rs index 59ee303..56907aa 100644 --- a/tests/decode_images.rs +++ b/tests/decode_images.rs @@ -276,7 +276,7 @@ fn test_tiled_incremental() { for tile in 0..tiles { match decoder.read_tile().unwrap() { - (DecodingResult::U8(res), chunk_info) => { + (DecodingResult::U8(res), _chunk_info) => { let sum: u64 = res.into_iter().map(::from).sum(); assert_eq!(sum, sums[tile as usize]); } diff --git a/tests/encode_images.rs b/tests/encode_images.rs index a8e1ca3..6d34ee6 100644 --- a/tests/encode_images.rs +++ b/tests/encode_images.rs @@ -513,7 +513,7 @@ fn test_rows_per_strip() { for i in 0..50 { let img2 = [i; 2 * 100]; match decoder.read_strip().unwrap() { - DecodingResult::U8(data) => assert_eq!(&img2[..], &data[..]), + (DecodingResult::U8(data), _chunk_info) => assert_eq!(&img2[..], &data[..]), other => panic!("Incorrect strip type {:?}", other), } }