Added ability to select which tile to load (based on index)

Also reduced repeated code when calculating padding etc.
This commit is contained in:
Alan Race committed 2022-04-27 16:46:48 +02:00
1 parent 2cf33d634d
commit 8fcf49deac
1 file changed
+96 -48
+96 -48
View File
@@ -243,6 +243,28 @@ pub enum ChunkType {
Tile, Tile,
} }
pub struct ChunkInfo {
/// Width of the chunk as specified (includes potential padding)
pub chunk_width: usize,
pub chunk_height: usize,
/// Width of the data (excluding potential padding)
pub data_width: usize,
pub data_height: usize,
}
impl ChunkInfo {
#[inline]
pub fn padding_right(&self) -> usize {
self.chunk_width - self.data_width
}
#[inline]
pub fn padding_down(&self) -> usize {
self.chunk_height - self.data_height
}
}
/// Decoding limits /// Decoding limits
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Limits { pub struct Limits {
@@ -1066,33 +1088,72 @@ impl<R: Read + Seek> Decoder<R> {
Ok(()) Ok(())
} }
pub fn chunk_info(&self, chunk_index: usize) -> Option<ChunkInfo> {
match &self.image().chunk_type {
ChunkType::Strip => {
let rows_per_strip =
usize::try_from(self.image().strip_decoder.as_ref()?.rows_per_strip).unwrap();
let sized_width = usize::try_from(self.image().width).unwrap();
let sized_height = usize::try_from(self.image().height).unwrap();
let strip_height_without_padding = chunk_index
.checked_mul(rows_per_strip)
.and_then(|x| sized_height.checked_sub(x))?;
// Ignore potential vertical padding on the bottommost strip
let strip_height = rows_per_strip.min(strip_height_without_padding);
Some(ChunkInfo {
chunk_width: sized_width,
chunk_height: sized_height,
data_width: sized_width,
data_height: strip_height,
})
}
ChunkType::Tile => {
let tile_attrs = self.image().tile_attributes.as_ref()?;
let (padding_right, padding_down) = tile_attrs.get_padding(chunk_index);
let tile_width = tile_attrs.tile_width - padding_right;
let tile_length = tile_attrs.tile_length - padding_down;
Some(ChunkInfo {
chunk_width: tile_attrs.tile_width,
chunk_height: tile_attrs.tile_length,
data_width: tile_width,
data_height: tile_length,
})
}
}
}
/// Decompresses the tile into the supplied buffer. /// Decompresses the tile into the supplied buffer.
fn expand_tile( fn expand_tile(
&mut self, &mut self,
mut buffer: DecodingBuffer, mut buffer: DecodingBuffer,
offset: u64, offset: u64,
compressed_length: u64, compressed_length: u64,
tile: usize, chunk_info: &ChunkInfo,
output_width: usize, output_width: usize,
) -> TiffResult<()> { ) -> TiffResult<()> {
let color_type = self.colortype()?; let color_type = self.colortype()?;
let byte_len = buffer.byte_len(); let byte_len = buffer.byte_len();
let byte_order = self.reader.byte_order; let byte_order = self.reader.byte_order;
let tile_attrs = self.image().tile_attributes.as_ref().unwrap();
let (padding_right, padding_down) = tile_attrs.get_padding(tile);
let tile_length = tile_attrs.tile_length;
let row_samples = tile_attrs.row_samples();
let padding_right_samples = padding_right * self.image().bits_per_sample.len();
self.goto_offset_u64(offset)?; self.goto_offset_u64(offset)?;
let line_samples = output_width * self.image().bits_per_sample.len(); let line_samples = output_width * self.image().bits_per_sample.len();
let photometric_interpretation = self.image().photometric_interpretation; let photometric_interpretation = self.image().photometric_interpretation;
let padding_right_samples = chunk_info.padding_right() * self.image().bits_per_sample.len();
let row_samples = chunk_info.data_width * self.image().bits_per_sample.len();
let mut reader = Self::create_reader(self, compressed_length)?; let mut reader = Self::create_reader(self, compressed_length)?;
for row in 0..(tile_length - padding_down) { for row_index in 0..chunk_info.data_height {
let buf = match &mut buffer { let buf = match &mut buffer {
DecodingBuffer::U8(buf) => &mut *buf, DecodingBuffer::U8(buf) => &mut *buf,
DecodingBuffer::I8(buf) => bytecast::i8_as_ne_mut_bytes(buf), DecodingBuffer::I8(buf) => bytecast::i8_as_ne_mut_bytes(buf),
@@ -1106,14 +1167,14 @@ impl<R: Read + Seek> Decoder<R> {
DecodingBuffer::F64(buf) => bytecast::f64_as_ne_mut_bytes(buf), DecodingBuffer::F64(buf) => bytecast::f64_as_ne_mut_bytes(buf),
}; };
let row_start = row * line_samples; let row_start = row_index * line_samples;
let row_end = row_start + row_samples - padding_right_samples; let row_end = row_start + row_samples;
let row = &mut buf[(row_start * byte_len)..(row_end * byte_len)]; let row = &mut buf[(row_start * byte_len)..(row_end * byte_len)];
reader.read_exact(row)?; reader.read_exact(row)?;
// Skip horizontal padding // Skip horizontal padding
if padding_right > 0 { if padding_right_samples > 0 {
let len = u64::try_from(padding_right_samples * byte_len)?; let len = u64::try_from(padding_right_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())?;
} }
@@ -1273,7 +1334,7 @@ impl<R: Read + Seek> Decoder<R> {
jpeg::Error::Internal(_) => Err(TiffError::UnsupportedError( jpeg::Error::Internal(_) => Err(TiffError::UnsupportedError(
TiffUnsupportedError::UnknownInterpretation, TiffUnsupportedError::UnknownInterpretation,
)), )),
} };
} }
} }
} }
@@ -1298,22 +1359,12 @@ impl<R: Read + Seek> Decoder<R> {
.ok_or(TiffError::FormatError( .ok_or(TiffError::FormatError(
TiffFormatError::InconsistentSizesEncountered, TiffFormatError::InconsistentSizesEncountered,
))?; ))?;
let rows_per_strip =
usize::try_from(self.image().strip_decoder.as_ref().unwrap().rows_per_strip)?;
let sized_width = usize::try_from(self.image().width)?; let chunk_info = self.chunk_info(index).ok_or(TiffError::IntSizeError)?;
let sized_height = usize::try_from(self.image().height)?;
let strip_height_without_padding = index let buffer_size = chunk_info
.checked_mul(rows_per_strip) .data_width
.and_then(|x| sized_height.checked_sub(x)) .checked_mul(chunk_info.data_height)
.ok_or(TiffError::IntSizeError)?;
// Ignore potential vertical padding on the bottommost strip
let strip_height = rows_per_strip.min(strip_height_without_padding);
let buffer_size = sized_width
.checked_mul(strip_height)
.and_then(|x| x.checked_mul(self.image().bits_per_sample.len())) .and_then(|x| x.checked_mul(self.image().bits_per_sample.len()))
.ok_or(TiffError::LimitsExceeded)?; .ok_or(TiffError::LimitsExceeded)?;
@@ -1331,7 +1382,7 @@ impl<R: Read + Seek> Decoder<R> {
Predictor::Horizontal => { Predictor::Horizontal => {
rev_hpredict( rev_hpredict(
buffer.copy(), buffer.copy(),
(self.image().width, u32::try_from(strip_height)?), (self.image().width, u32::try_from(chunk_info.data_height)?),
usize::try_from(self.image().width)?, usize::try_from(self.image().width)?,
self.colortype()?, self.colortype()?,
)?; )?;
@@ -1364,17 +1415,13 @@ impl<R: Read + Seek> Decoder<R> {
TiffFormatError::InconsistentSizesEncountered, TiffFormatError::InconsistentSizesEncountered,
))?; ))?;
let tile_attrs = self.image().tile_attributes.as_ref().unwrap(); let chunk_info = self.chunk_info(tile).unwrap();
let tile_width = tile_attrs.tile_width;
let tile_length = tile_attrs.tile_length;
let (padding_right, padding_down) = tile_attrs.get_padding(tile);
self.expand_tile( self.expand_tile(
result.copy(), result.copy(),
file_offset, file_offset,
compressed_bytes, compressed_bytes,
tile, &chunk_info,
output_width, output_width,
)?; )?;
@@ -1384,8 +1431,8 @@ impl<R: Read + Seek> Decoder<R> {
rev_hpredict( rev_hpredict(
result.copy(), result.copy(),
( (
u32::try_from(tile_width - padding_right)?, u32::try_from(chunk_info.data_width)?,
u32::try_from(tile_length - padding_down)?, u32::try_from(chunk_info.data_height)?,
), ),
output_width, output_width,
self.colortype()?, self.colortype()?,
@@ -1393,6 +1440,7 @@ impl<R: Read + Seek> Decoder<R> {
} }
Predictor::__NonExhaustive => unreachable!(), Predictor::__NonExhaustive => unreachable!(),
} }
Ok(()) Ok(())
} }
@@ -1470,22 +1518,22 @@ impl<R: Read + Seek> Decoder<R> {
/// Read a single tile from the image and return it as a Vector /// Read a single tile from the image and return it as a Vector
pub fn read_tile(&mut self) -> TiffResult<DecodingResult> { pub fn read_tile(&mut self) -> TiffResult<DecodingResult> {
self.check_chunk_type(ChunkType::Tile)?; let result = self.read_tile_at(self.current_chunk);
let tile = self.current_chunk;
let tile_attrs = self.image().tile_attributes.as_ref().unwrap();
let (padding_right, padding_down) = tile_attrs.get_padding(tile);
let tile_width = tile_attrs.tile_width - padding_right;
let tile_length = tile_attrs.tile_length - padding_down;
let mut result = self.result_buffer(tile_width, tile_length)?;
self.read_tile_to_buffer(&mut result.as_buffer(0), tile, tile_width)?;
self.current_chunk += 1; self.current_chunk += 1;
result
}
pub fn read_tile_at(&mut self, tile_index: usize) -> TiffResult<DecodingResult> {
self.check_chunk_type(ChunkType::Tile)?;
let chunk_info = self.chunk_info(tile_index).unwrap();
let mut result = self.result_buffer(chunk_info.data_width, chunk_info.data_height)?;
self.read_tile_to_buffer(&mut result.as_buffer(0), tile_index, chunk_info.data_width)?;
Ok(result) Ok(result)
} }