Changed to use u32 for chunk_index

This commit is contained in:
Alan Race committed 2022-07-11 08:56:35 +02:00
1 parent e1f4161e0b
commit 5cadbffebf
5 files changed
+67 -55

No files matched your search

+44 -39
View File
@@ -67,29 +67,29 @@ impl TileAttributes {
pub struct ChunkInfo { pub struct ChunkInfo {
/// Width of the chunk as specified (includes potential padding). This has the same /// Width of the chunk as specified (includes potential padding). This has the same
/// value for all chunks in the image. /// value for all chunks in the image.
pub chunk_width: usize, pub chunk_width: u32,
/// Height/Length of the chunk as specified (includes potential padding). This has /// Height/Length of the chunk as specified (includes potential padding). This has
/// the same value for all chunks in the image. /// the same value for all chunks in the image.
pub chunk_height: usize, pub chunk_height: u32,
/// Width of the data (excluding potential padding). This can take different values /// Width of the data (excluding potential padding). This can take different values
/// on the right column of the image due to padding. /// on the right column of the image due to padding.
pub data_width: usize, pub data_width: u32,
/// Height/Length of the data (excluding potential padding). This can take different values /// Height/Length of the data (excluding potential padding). This can take different values
/// on the bottom row of the image due to padding. /// on the bottom row of the image due to padding.
pub data_height: usize, pub data_height: u32,
} }
impl ChunkInfo { impl ChunkInfo {
#[inline] #[inline]
/// Returns the amount of padding (pixles) on the right-side of the chunk. /// Returns the amount of padding (pixles) on the right-side of the chunk.
pub fn padding_right(&self) -> usize { pub fn padding_right(&self) -> u32 {
self.chunk_width - self.data_width self.chunk_width - self.data_width
} }
#[inline] #[inline]
/// Returns the amount of padding (pixles) on the bottom-side of the chunk. /// Returns the amount of padding (pixles) on the bottom-side of the chunk.
pub fn padding_down(&self) -> usize { pub fn padding_down(&self) -> u32 {
self.chunk_height - self.data_height self.chunk_height - self.data_height
} }
} }
@@ -390,12 +390,18 @@ impl Image {
}) })
} }
pub(crate) fn chunk_file_range(&self, chunk: usize) -> TiffResult<(u64, u64)> { pub(crate) fn chunk_file_range(&self, chunk: u32) -> TiffResult<(u64, u64)> {
let file_offset = self.chunk_offsets.get(chunk).ok_or(TiffError::FormatError( let file_offset = self
.chunk_offsets
.get(chunk as usize)
.ok_or(TiffError::FormatError(
TiffFormatError::InconsistentSizesEncountered, TiffFormatError::InconsistentSizesEncountered,
))?; ))?;
let compressed_bytes = self.chunk_bytes.get(chunk).ok_or(TiffError::FormatError( let compressed_bytes =
self.chunk_bytes
.get(chunk as usize)
.ok_or(TiffError::FormatError(
TiffFormatError::InconsistentSizesEncountered, TiffFormatError::InconsistentSizesEncountered,
))?; ))?;
@@ -416,42 +422,39 @@ impl Image {
} }
} }
pub(crate) fn chunk_info(&self, chunk_index: usize) -> Option<ChunkInfo> { pub(crate) fn chunk_info(&self, chunk_index: u32) -> Option<ChunkInfo> {
match &self.chunk_type { match &self.chunk_type {
ChunkType::Strip => { ChunkType::Strip => {
let rows_per_strip = self.strip_decoder.as_ref().unwrap().rows_per_strip as usize; let rows_per_strip = self.strip_decoder.as_ref().unwrap().rows_per_strip;
let sized_width = self.width as usize;
let sized_height = self.height as usize;
let strip_height_without_padding = chunk_index let strip_height_without_padding = chunk_index
.checked_mul(rows_per_strip) .checked_mul(rows_per_strip)
.and_then(|x| sized_height.checked_sub(x))?; .and_then(|x| self.height.checked_sub(x))?;
// Ignore potential vertical padding on the bottommost strip // Ignore potential vertical padding on the bottommost strip
let strip_height = rows_per_strip.min(strip_height_without_padding); let strip_height = rows_per_strip.min(strip_height_without_padding);
Some(ChunkInfo { Some(ChunkInfo {
chunk_width: sized_width, chunk_width: self.width,
chunk_height: rows_per_strip, chunk_height: rows_per_strip,
data_width: sized_width, data_width: self.width,
data_height: strip_height, data_height: strip_height,
}) })
} }
ChunkType::Tile => { ChunkType::Tile => {
let tile_attrs = self.tile_attributes.as_ref().unwrap(); 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 as usize);
let tile_width = tile_attrs.tile_width - padding_right; let tile_width = tile_attrs.tile_width - padding_right;
let tile_length = tile_attrs.tile_length - padding_down; let tile_length = tile_attrs.tile_length - padding_down;
Some(ChunkInfo { Some(ChunkInfo {
chunk_width: tile_attrs.tile_width, chunk_width: tile_attrs.tile_width as u32,
chunk_height: tile_attrs.tile_length, chunk_height: tile_attrs.tile_length as u32,
data_width: tile_width, data_width: tile_width as u32,
data_height: tile_length, data_height: tile_length as u32,
}) })
} }
} }
@@ -463,7 +466,7 @@ impl Image {
mut buffer: DecodingBuffer, mut buffer: DecodingBuffer,
output_width: usize, output_width: usize,
byte_order: ByteOrder, byte_order: ByteOrder,
chunk_index: usize, chunk_index: u32,
) -> TiffResult<()> { ) -> TiffResult<()> {
// Validate that the provided buffer is of the expected type. // Validate that the provided buffer is of the expected type.
let color_type = self.colortype()?; let color_type = self.colortype()?;
@@ -512,9 +515,9 @@ impl Image {
_ => {} _ => {}
} }
let compressed_bytes = self let compressed_bytes =
.chunk_bytes self.chunk_bytes
.get(chunk_index) .get(chunk_index as usize)
.ok_or(TiffError::FormatError( .ok_or(TiffError::FormatError(
TiffFormatError::InconsistentSizesEncountered, TiffFormatError::InconsistentSizesEncountered,
))?; ))?;
@@ -533,14 +536,15 @@ impl Image {
let mut reader = let mut reader =
Self::create_reader(reader, compression_method, *compressed_bytes, jpeg_tables)?; Self::create_reader(reader, compression_method, *compressed_bytes, jpeg_tables)?;
if output_width == chunk_info.data_width && chunk_info.padding_right() == 0 { if output_width == chunk_info.data_width as usize && chunk_info.padding_right() == 0 {
let total_samples = chunk_info.data_width * chunk_info.data_height * samples; let total_samples =
chunk_info.data_width as usize * chunk_info.data_height as usize * samples;
let tile = &mut buffer.as_bytes_mut()[..total_samples * byte_len]; let tile = &mut buffer.as_bytes_mut()[..total_samples * byte_len];
reader.read_exact(tile)?; reader.read_exact(tile)?;
for row in 0..chunk_info.data_height { for row in 0..chunk_info.data_height as usize {
let row_start = row * output_width * samples; let row_start = row as usize * output_width as usize * samples;
let row_end = (row + 1) * output_width * samples; let row_end = (row + 1) * output_width as usize * samples;
let row = buffer.subrange(row_start..row_end); let row = buffer.subrange(row_start..row_end);
super::fix_endianness_and_predict(row, samples, byte_order, predictor); super::fix_endianness_and_predict(row, samples, byte_order, predictor);
} }
@@ -550,10 +554,10 @@ impl Image {
} else if chunk_info.padding_right() > 0 && self.predictor == Predictor::FloatingPoint { } else if chunk_info.padding_right() > 0 && self.predictor == Predictor::FloatingPoint {
// The floating point predictor shuffles the padding bytes into the encoded output, so // The floating point predictor shuffles the padding bytes into the encoded output, so
// this case is handled specially when needed. // this case is handled specially when needed.
let mut encoded = vec![0u8; chunk_info.chunk_width * samples * byte_len]; let mut encoded = vec![0u8; chunk_info.chunk_width as usize * samples * byte_len];
for row in 0..chunk_info.data_height { for row in 0..chunk_info.data_height as usize {
let row_start = row * output_width * samples; let row_start = row * output_width as usize * samples;
let row_end = row_start + chunk_info.data_width * samples; let row_end = row_start + chunk_info.data_width as usize * samples;
reader.read_exact(&mut encoded)?; reader.read_exact(&mut encoded)?;
match buffer.subrange(row_start..row_end) { match buffer.subrange(row_start..row_end) {
@@ -566,16 +570,17 @@ impl Image {
} }
} }
} else { } else {
for row in 0..chunk_info.data_height { for row in 0..chunk_info.data_height as usize {
let row_start = row * output_width * samples; let row_start = row * output_width as usize * samples;
let row_end = row_start + chunk_info.data_width * samples; let row_end = row_start + chunk_info.data_width as usize * samples;
let row = &mut buffer.as_bytes_mut()[(row_start * byte_len)..(row_end * byte_len)]; let row = &mut buffer.as_bytes_mut()[(row_start * byte_len)..(row_end * byte_len)];
reader.read_exact(row)?; reader.read_exact(row)?;
// Skip horizontal padding // Skip horizontal padding
if chunk_info.padding_right() > 0 { if chunk_info.padding_right() > 0 {
let len = u64::try_from(chunk_info.padding_right() * samples * byte_len)?; let len =
u64::try_from(chunk_info.padding_right() as usize * samples * byte_len)?;
io::copy(&mut reader.by_ref().take(len), &mut io::sink())?; io::copy(&mut reader.by_ref().take(len), &mut io::sink())?;
} }
+20 -13
View File
@@ -301,7 +301,7 @@ where
reader: SmartReader<R>, reader: SmartReader<R>,
bigtiff: bool, bigtiff: bool,
limits: Limits, limits: Limits,
current_chunk: usize, current_chunk: u32,
next_ifd: Option<u64>, next_ifd: Option<u64>,
ifd_offsets: Vec<u64>, ifd_offsets: Vec<u64>,
seen_ifds: HashSet<u64>, seen_ifds: HashSet<u64>,
@@ -1064,13 +1064,13 @@ impl<R: Read + Seek> Decoder<R> {
Ok(()) Ok(())
} }
fn read_chunk_to_buffer( pub fn read_chunk_to_buffer(
&mut self, &mut self,
mut buffer: DecodingBuffer, mut buffer: DecodingBuffer,
tile_index: usize, chunk_index: u32,
output_width: usize, output_width: usize,
) -> TiffResult<()> { ) -> TiffResult<()> {
let offset = self.image.chunk_file_range(tile_index)?.0; let offset = self.image.chunk_file_range(chunk_index)?.0;
self.goto_offset_u64(offset)?; self.goto_offset_u64(offset)?;
let byte_order = self.reader.byte_order; let byte_order = self.reader.byte_order;
@@ -1080,7 +1080,7 @@ impl<R: Read + Seek> Decoder<R> {
buffer.copy(), buffer.copy(),
output_width, output_width,
byte_order, byte_order,
tile_index, chunk_index,
)?; )?;
Ok(()) Ok(())
@@ -1143,9 +1143,9 @@ 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"] #[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_chunk_at(self.current_chunk)?; let result = self.read_chunk(self.current_chunk)?;
Ok(result.0) Ok(result.0)
} }
@@ -1154,9 +1154,9 @@ impl<R: Read + Seek> Decoder<R> {
/// 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.
#[deprecated = "Use read_chunk_at instead"] #[deprecated = "Use read_chunk instead"]
pub fn read_tile(&mut self) -> TiffResult<DecodingResult> { pub fn read_tile(&mut self) -> TiffResult<DecodingResult> {
let result = self.read_chunk_at(self.current_chunk)?; let result = self.read_chunk(self.current_chunk)?;
self.current_chunk += 1; self.current_chunk += 1;
@@ -1165,15 +1165,22 @@ impl<R: Read + Seek> Decoder<R> {
/// Read the specified chunk (at index `chunk_index`) and return the binary data as a Vector and /// Read the specified chunk (at index `chunk_index`) and return the binary data as a Vector and
/// `ChunkInfo` describing the size of the chunk. /// `ChunkInfo` describing the size of the chunk.
pub fn read_chunk_at(&mut self, chunk_index: usize) -> TiffResult<(DecodingResult, ChunkInfo)> { pub fn read_chunk(&mut self, chunk_index: u32) -> TiffResult<(DecodingResult, ChunkInfo)> {
let chunk_info = self let chunk_info = self
.image() .image()
.chunk_info(chunk_index) .chunk_info(chunk_index)
.ok_or(UsageError::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 as usize,
chunk_info.data_height as usize,
)?;
self.read_chunk_to_buffer(result.as_buffer(0), chunk_index, chunk_info.data_width)?; self.read_chunk_to_buffer(
result.as_buffer(0),
chunk_index,
chunk_info.data_width as usize,
)?;
Ok((result, chunk_info)) Ok((result, chunk_info))
} }
@@ -1220,7 +1227,7 @@ impl<R: Read + Seek> Decoder<R> {
result.as_buffer(buffer_offset).copy(), result.as_buffer(buffer_offset).copy(),
width, width,
byte_order, byte_order,
chunk, chunk as u32,
)?; )?;
} }
+1 -1
View File
@@ -205,7 +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), InvalidChunkIndex(u32),
} }
impl fmt::Display for UsageError { impl fmt::Display for UsageError {
+1 -1
View File
@@ -275,7 +275,7 @@ 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_at(tile as usize).unwrap() { match decoder.read_chunk(tile).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]);
+1 -1
View File
@@ -512,7 +512,7 @@ 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_at(i as usize).unwrap() { match decoder.read_chunk(i as u32).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),
} }