Removed unused functions and make InvalidChunkIndex a UsageError

This commit is contained in:
Alan Race committed 2022-07-08 11:08:30 +02:00
1 parent 8c0c13f022
commit e1f4161e0b
5 files changed
+21 -95

No files matched your search

+5 -5
View File
@@ -7,7 +7,7 @@ use super::tag_reader::TagReader;
use super::{fp_predict_f32, fp_predict_f64, DecodingBuffer, Limits}; use super::{fp_predict_f32, fp_predict_f64, DecodingBuffer, Limits};
use super::{stream::SmartReader, ChunkType}; use super::{stream::SmartReader, ChunkType};
use crate::tags::{CompressionMethod, PhotometricInterpretation, Predictor, SampleFormat, Tag}; 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::convert::{TryFrom, TryInto};
use std::io::{self, Cursor, Read, Seek}; use std::io::{self, Cursor, Read, Seek};
use std::sync::Arc; use std::sync::Arc;
@@ -419,7 +419,7 @@ impl Image {
pub(crate) fn chunk_info(&self, chunk_index: usize) -> Option<ChunkInfo> { pub(crate) fn chunk_info(&self, chunk_index: usize) -> Option<ChunkInfo> {
match &self.chunk_type { match &self.chunk_type {
ChunkType::Strip => { 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_width = self.width as usize;
let sized_height = self.height as usize; let sized_height = self.height as usize;
@@ -440,7 +440,7 @@ impl Image {
}) })
} }
ChunkType::Tile => { 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 (padding_right, padding_down) = tile_attrs.get_padding(chunk_index);
let tile_width = tile_attrs.tile_width - padding_right; let tile_width = tile_attrs.tile_width - padding_right;
@@ -525,8 +525,8 @@ impl Image {
let predictor = self.predictor; let predictor = self.predictor;
let samples = self.bits_per_sample.len(); let samples = self.bits_per_sample.len();
let chunk_info = self.chunk_info(chunk_index).ok_or(TiffError::FormatError( let chunk_info = self.chunk_info(chunk_index).ok_or(TiffError::UsageError(
TiffFormatError::InvalidChunkIndex(chunk_index), UsageError::InvalidChunkIndex(chunk_index),
))?; ))?;
let jpeg_tables = self.jpeg_tables.clone(); let jpeg_tables = self.jpeg_tables.clone();
+4 -84
View File
@@ -1064,31 +1064,6 @@ impl<R: Read + Seek> Decoder<R> {
Ok(()) 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( fn read_chunk_to_buffer(
&mut self, &mut self,
mut buffer: DecodingBuffer, mut buffer: DecodingBuffer,
@@ -1168,74 +1143,19 @@ impl<R: Read + Seek> Decoder<R> {
/// information on the size of the strip read and so the caller must take care of the final /// 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 /// 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. /// may change at a future date to reflect this.
/// #[deprecated = "Use read_chunk_at instead"]
/// `read_strip_at` can be used to get both the strip data and the strip size.
///
#[deprecated = "Use read_chunk instead"]
pub fn read_strip(&mut self) -> TiffResult<DecodingResult> { pub fn read_strip(&mut self) -> TiffResult<DecodingResult> {
let result = self.read_strip_at(self.current_chunk)?; let result = self.read_chunk_at(self.current_chunk)?;
Ok(result.0) 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 /// 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 /// 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 /// 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. /// 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<DecodingResult> {
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"] #[deprecated = "Use read_chunk_at instead"]
pub fn read_tile_at(&mut self, tile_index: usize) -> TiffResult<(DecodingResult, ChunkInfo)> { pub fn read_tile(&mut self) -> TiffResult<DecodingResult> {
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<DecodingResult> {
let result = self.read_chunk_at(self.current_chunk)?; let result = self.read_chunk_at(self.current_chunk)?;
self.current_chunk += 1; self.current_chunk += 1;
@@ -1249,7 +1169,7 @@ impl<R: Read + Seek> Decoder<R> {
let chunk_info = self let chunk_info = self
.image() .image()
.chunk_info(chunk_index) .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)?; let mut result = self.result_buffer(chunk_info.data_width, chunk_info.data_height)?;
+8 -2
View File
@@ -56,7 +56,6 @@ pub enum TiffFormatError {
actual_samples: usize, actual_samples: usize,
required_samples: usize, required_samples: usize,
}, },
InvalidChunkIndex(usize),
InvalidTag, InvalidTag,
InvalidTagValueType(Tag), InvalidTagValueType(Tag),
RequiredTagNotFound(Tag), RequiredTagNotFound(Tag),
@@ -101,7 +100,6 @@ impl fmt::Display for TiffFormatError {
actual_samples, required_samples actual_samples, required_samples
) )
} }
InvalidChunkIndex(index) => write!(fmt, "Image chunk index ({}) requested.", index),
InvalidTag => write!(fmt, "Image contains invalid tag."), InvalidTag => write!(fmt, "Image contains invalid tag."),
InvalidTagValueType(ref tag) => { InvalidTagValueType(ref tag) => {
write!(fmt, "Tag `{:?}` did not have the expected value type.", tag) write!(fmt, "Tag `{:?}` did not have the expected value type.", tag)
@@ -207,6 +205,7 @@ impl fmt::Display for TiffUnsupportedError {
#[derive(Debug)] #[derive(Debug)]
pub enum UsageError { pub enum UsageError {
InvalidChunkType(ChunkType, ChunkType), InvalidChunkType(ChunkType, ChunkType),
InvalidChunkIndex(usize),
} }
impl fmt::Display for UsageError { impl fmt::Display for UsageError {
@@ -220,6 +219,7 @@ impl fmt::Display for UsageError {
expected, actual expected, actual
) )
} }
InvalidChunkIndex(index) => write!(fmt, "Image chunk index ({}) requested.", index),
} }
} }
} }
@@ -292,6 +292,12 @@ impl From<TiffUnsupportedError> for TiffError {
} }
} }
impl From<UsageError> for TiffError {
fn from(err: UsageError) -> TiffError {
TiffError::UsageError(err)
}
}
impl From<std::num::TryFromIntError> for TiffError { impl From<std::num::TryFromIntError> for TiffError {
fn from(_err: std::num::TryFromIntError) -> TiffError { fn from(_err: std::num::TryFromIntError) -> TiffError {
TiffError::IntSizeError TiffError::IntSizeError
+2 -2
View File
@@ -275,8 +275,8 @@ fn test_tiled_incremental() {
assert_eq!(tiles as usize, sums.len()); assert_eq!(tiles as usize, sums.len());
for tile in 0..tiles { for tile in 0..tiles {
match decoder.read_chunk().unwrap() { match decoder.read_chunk_at(tile as usize).unwrap() {
DecodingResult::U8(res) => { (DecodingResult::U8(res), _) => {
let sum: u64 = res.into_iter().map(<u64>::from).sum(); let sum: u64 = res.into_iter().map(<u64>::from).sum();
assert_eq!(sum, sums[tile as usize]); assert_eq!(sum, sums[tile as usize]);
} }
+2 -2
View File
@@ -512,8 +512,8 @@ fn test_rows_per_strip() {
for i in 0..50 { for i in 0..50 {
let img2 = [i; 2 * 100]; let img2 = [i; 2 * 100];
match decoder.read_chunk().unwrap() { match decoder.read_chunk_at(i as usize).unwrap() {
DecodingResult::U8(data) => assert_eq!(&img2[..], &data[..]), (DecodingResult::U8(data), _) => assert_eq!(&img2[..], &data[..]),
other => panic!("Incorrect strip type {:?}", other), other => panic!("Incorrect strip type {:?}", other),
} }
} }