diff --git a/src/decoder/image.rs b/src/decoder/image.rs index 172feb7..d5cf4fb 100644 --- a/src/decoder/image.rs +++ b/src/decoder/image.rs @@ -7,7 +7,7 @@ use super::tag_reader::TagReader; use super::{fp_predict_f32, fp_predict_f64, DecodingBuffer, Limits}; use super::{stream::SmartReader, ChunkType}; use crate::tags::{CompressionMethod, PhotometricInterpretation, Predictor, SampleFormat, Tag}; -use crate::{ColorType, TiffError, TiffFormatError, TiffResult, TiffUnsupportedError}; +use crate::{ColorType, TiffError, TiffFormatError, TiffResult, TiffUnsupportedError, UsageError}; use std::convert::{TryFrom, TryInto}; use std::io::{self, Cursor, Read, Seek}; use std::sync::Arc; @@ -419,7 +419,7 @@ impl Image { pub(crate) fn chunk_info(&self, chunk_index: usize) -> Option { match &self.chunk_type { ChunkType::Strip => { - let rows_per_strip = self.strip_decoder.as_ref()?.rows_per_strip as usize; + let rows_per_strip = self.strip_decoder.as_ref().unwrap().rows_per_strip as usize; let sized_width = self.width as usize; let sized_height = self.height as usize; @@ -440,7 +440,7 @@ impl Image { }) } ChunkType::Tile => { - let tile_attrs = self.tile_attributes.as_ref()?; + let tile_attrs = self.tile_attributes.as_ref().unwrap(); let (padding_right, padding_down) = tile_attrs.get_padding(chunk_index); let tile_width = tile_attrs.tile_width - padding_right; @@ -525,8 +525,8 @@ impl Image { let predictor = self.predictor; let samples = self.bits_per_sample.len(); - let chunk_info = self.chunk_info(chunk_index).ok_or(TiffError::FormatError( - TiffFormatError::InvalidChunkIndex(chunk_index), + let chunk_info = self.chunk_info(chunk_index).ok_or(TiffError::UsageError( + UsageError::InvalidChunkIndex(chunk_index), ))?; let jpeg_tables = self.jpeg_tables.clone(); diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 27e1465..b7a6b5d 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -1064,31 +1064,6 @@ impl Decoder { Ok(()) } - #[deprecated = "Use read_chunk_to_buffer instead"] - fn read_tile_to_buffer( - &mut self, - mut buffer: DecodingBuffer, - tile_index: usize, - output_width: usize, - ) -> TiffResult<()> { - self.check_chunk_type(ChunkType::Tile)?; - - let offset = self.image.chunk_file_range(tile_index)?.0; - self.goto_offset_u64(offset)?; - - let byte_order = self.reader.byte_order; - - self.image.expand_chunk( - &mut self.reader, - buffer.copy(), - output_width, - byte_order, - tile_index, - )?; - - Ok(()) - } - fn read_chunk_to_buffer( &mut self, mut buffer: DecodingBuffer, @@ -1168,74 +1143,19 @@ impl Decoder { /// information on the size of the strip read and so the caller must take care of the final /// strip in the image as the data read may be less than the expected strip size. This interface /// may change at a future date to reflect this. - /// - /// `read_strip_at` can be used to get both the strip data and the strip size. - /// - #[deprecated = "Use read_chunk instead"] + #[deprecated = "Use read_chunk_at instead"] pub fn read_strip(&mut self) -> TiffResult { - let result = self.read_strip_at(self.current_chunk)?; + let result = self.read_chunk_at(self.current_chunk)?; Ok(result.0) } - /// Read the specified strip (at index `strip_index`) and return the binary data as a Vector and - /// `ChunkInfo` describing the size of the strip. - /// - #[deprecated = "Use read_chunk instead"] - pub fn read_strip_at(&mut self, strip_index: usize) -> TiffResult<(DecodingResult, ChunkInfo)> { - self.check_chunk_type(ChunkType::Strip)?; - - let chunk_info = self - .image() - .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))?; - - Ok((result, chunk_info)) - } - /// Read a single tile from the image and return it as a Vector. This method does not return /// information on the size of the tile read and so the caller must take care of tiles on the /// rightmost column and the bottommost row in the image as the data read may be less than the /// expected tile size. This interface may change at a future date to reflect this. - /// - /// `read_tile_at` can be used to get both the tile data and the tile size. - #[deprecated = "Use read_chunk instead"] - pub fn read_tile(&mut self) -> TiffResult { - let result = self.read_tile_at(self.current_chunk)?; - - self.current_chunk += 1; - - Ok(result.0) - } - - /// Read the specified tile (at index `tile_index`) and return the binary data as a Vector and - /// `ChunkInfo` describing the size of the tile. #[deprecated = "Use read_chunk_at instead"] - pub fn read_tile_at(&mut self, tile_index: usize) -> TiffResult<(DecodingResult, ChunkInfo)> { - self.check_chunk_type(ChunkType::Tile)?; - - let chunk_info = self - .image() - .chunk_info(tile_index) - .ok_or(TiffFormatError::InvalidChunkIndex(tile_index))?; - - let mut result = self.result_buffer(chunk_info.data_width, chunk_info.data_height)?; - - self.read_tile_to_buffer(result.as_buffer(0), tile_index, chunk_info.data_width)?; - - Ok((result, chunk_info)) - } - - /// Read a single chunk from the image and return it as a Vector. This method does not return - /// information on the size of the chunk read and so the caller must take care of chunks on the - /// rightmost column and the bottommost row in the image as the data read may be less than the - /// expected chunk size. This interface may change at a future date to reflect this. - /// - /// `read_chunk_at` can be used to get both the chunk data and the tile size. - pub fn read_chunk(&mut self) -> TiffResult { + pub fn read_tile(&mut self) -> TiffResult { let result = self.read_chunk_at(self.current_chunk)?; self.current_chunk += 1; @@ -1249,7 +1169,7 @@ impl Decoder { let chunk_info = self .image() .chunk_info(chunk_index) - .ok_or(TiffFormatError::InvalidChunkIndex(chunk_index))?; + .ok_or(UsageError::InvalidChunkIndex(chunk_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 a31a04c..c03d371 100644 --- a/src/error.rs +++ b/src/error.rs @@ -56,7 +56,6 @@ pub enum TiffFormatError { actual_samples: usize, required_samples: usize, }, - InvalidChunkIndex(usize), InvalidTag, InvalidTagValueType(Tag), RequiredTagNotFound(Tag), @@ -101,7 +100,6 @@ 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) @@ -207,6 +205,7 @@ impl fmt::Display for TiffUnsupportedError { #[derive(Debug)] pub enum UsageError { InvalidChunkType(ChunkType, ChunkType), + InvalidChunkIndex(usize), } impl fmt::Display for UsageError { @@ -220,6 +219,7 @@ impl fmt::Display for UsageError { expected, actual ) } + InvalidChunkIndex(index) => write!(fmt, "Image chunk index ({}) requested.", index), } } } @@ -292,6 +292,12 @@ impl From for TiffError { } } +impl From for TiffError { + fn from(err: UsageError) -> TiffError { + TiffError::UsageError(err) + } +} + impl From for TiffError { fn from(_err: std::num::TryFromIntError) -> TiffError { TiffError::IntSizeError diff --git a/tests/decode_images.rs b/tests/decode_images.rs index b0f08d5..f96046e 100644 --- a/tests/decode_images.rs +++ b/tests/decode_images.rs @@ -275,8 +275,8 @@ fn test_tiled_incremental() { assert_eq!(tiles as usize, sums.len()); for tile in 0..tiles { - match decoder.read_chunk().unwrap() { - DecodingResult::U8(res) => { + match decoder.read_chunk_at(tile as usize).unwrap() { + (DecodingResult::U8(res), _) => { 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 af35d91..8db3bac 100644 --- a/tests/encode_images.rs +++ b/tests/encode_images.rs @@ -512,8 +512,8 @@ fn test_rows_per_strip() { for i in 0..50 { let img2 = [i; 2 * 100]; - match decoder.read_chunk().unwrap() { - DecodingResult::U8(data) => assert_eq!(&img2[..], &data[..]), + match decoder.read_chunk_at(i as usize).unwrap() { + (DecodingResult::U8(data), _) => assert_eq!(&img2[..], &data[..]), other => panic!("Incorrect strip type {:?}", other), } }