Switch from SmartReader to EndianReader (#233)

This commit is contained in:
Jonathan Behrens authored and GitHub committed 2025-06-08 18:45:10 -07:00
1 parent ee0dbf373b
commit f44da4a00d
5 files changed
+78 -118

No files matched your search

+16 -16
View File
@@ -12,7 +12,7 @@ use half::f16;
use self::ifd::Directory;
use self::image::Image;
use self::stream::{ByteOrder, EndianReader, SmartReader};
use self::stream::{ByteOrder, EndianReader};
pub mod ifd;
mod image;
@@ -260,7 +260,7 @@ pub struct Decoder<R>
where
R: Read + Seek,
{
reader: SmartReader<R>,
reader: EndianReader<R>,
bigtiff: bool,
limits: Limits,
next_ifd: Option<u64>,
@@ -462,7 +462,7 @@ impl<R: Read + Seek> Decoder<R> {
))
}
};
let mut reader = SmartReader::wrap(r, byte_order);
let mut reader = EndianReader::new(r, byte_order);
let bigtiff = match reader.read_u16()? {
42 => false,
@@ -640,7 +640,7 @@ impl<R: Read + Seek> Decoder<R> {
#[inline]
pub fn read_byte(&mut self) -> Result<u8, io::Error> {
let mut buf = [0; 1];
self.reader.read_exact(&mut buf)?;
self.reader.inner().read_exact(&mut buf)?;
Ok(buf[0])
}
@@ -694,7 +694,7 @@ impl<R: Read + Seek> Decoder<R> {
#[inline]
pub fn read_string(&mut self, length: usize) -> TiffResult<String> {
let mut out = vec![0; length];
self.reader.read_exact(&mut out)?;
self.reader.inner().read_exact(&mut out)?;
// Strings may be null-terminated, so we trim anything downstream of the null byte
if let Some(first) = out.iter().position(|&b| b == 0) {
out.truncate(first);
@@ -711,7 +711,7 @@ impl<R: Read + Seek> Decoder<R> {
));
}
let mut val = [0; 4];
self.reader.read_exact(&mut val)?;
self.reader.inner().read_exact(&mut val)?;
Ok(val)
}
@@ -719,7 +719,7 @@ impl<R: Read + Seek> Decoder<R> {
#[inline]
pub fn read_offset_u64(&mut self) -> Result<[u8; 8], io::Error> {
let mut val = [0; 8];
self.reader.read_exact(&mut val)?;
self.reader.inner().read_exact(&mut val)?;
Ok(val)
}
@@ -731,7 +731,7 @@ impl<R: Read + Seek> Decoder<R> {
#[inline]
pub fn goto_offset_u64(&mut self, offset: u64) -> io::Result<()> {
self.reader.seek(io::SeekFrom::Start(offset)).map(|_| ())
self.reader.goto_offset(offset)
}
/// Reads a IFD entry.
@@ -742,7 +742,7 @@ impl<R: Read + Seek> Decoder<R> {
// Count 4 bytes
// Value 4 bytes either a pointer the value itself
fn read_entry(
reader: &mut SmartReader<R>,
reader: &mut EndianReader<R>,
bigtiff: bool,
) -> TiffResult<Option<(Tag, ifd::Entry)>> {
let tag = Tag::from_u16_exhaustive(reader.read_u16()?);
@@ -759,13 +759,13 @@ impl<R: Read + Seek> Decoder<R> {
let mut offset = [0; 8];
let count = reader.read_u64()?;
reader.read_exact(&mut offset)?;
reader.inner().read_exact(&mut offset)?;
ifd::Entry::new_u64(type_, count, offset)
} else {
let mut offset = [0; 4];
let count = reader.read_u32()?;
reader.read_exact(&mut offset)?;
reader.inner().read_exact(&mut offset)?;
ifd::Entry::new(type_, count, offset)
};
Ok(Some((tag, entry)))
@@ -773,7 +773,7 @@ impl<R: Read + Seek> Decoder<R> {
/// Reads the IFD starting at the indicated location.
fn read_ifd(
reader: &mut SmartReader<R>,
reader: &mut EndianReader<R>,
bigtiff: bool,
ifd_location: u64,
) -> TiffResult<(Directory, Option<u64>)> {
@@ -984,7 +984,7 @@ impl<R: Read + Seek> Decoder<R> {
output_width: usize,
) -> TiffResult<()> {
let offset = self.image.chunk_file_range(chunk_index)?.0;
self.goto_offset_u64(offset)?;
self.reader.goto_offset(offset)?;
let byte_order = self.reader.byte_order;
@@ -994,7 +994,7 @@ impl<R: Read + Seek> Decoder<R> {
/ 8;
self.image.expand_chunk(
&mut self.reader,
self.reader.inner(),
buffer.as_bytes_mut(),
output_row_stride.try_into()?,
byte_order,
@@ -1129,7 +1129,7 @@ impl<R: Read + Seek> Decoder<R> {
// * pass requested band as parameter
// * collect bands to a RGB encoding result in case of RGB bands
for chunk in 0..image_chunks {
self.goto_offset_u64(self.image().chunk_offsets[chunk])?;
self.reader.goto_offset(self.image().chunk_offsets[chunk])?;
let x = chunk % chunks_across;
let y = chunk / chunks_across;
@@ -1137,7 +1137,7 @@ impl<R: Read + Seek> Decoder<R> {
y * output_row_stride * chunk_dimensions.1 as usize + x * chunk_row_bytes;
let byte_order = self.reader.byte_order;
self.image.expand_chunk(
&mut self.reader,
self.reader.inner(),
&mut result.as_buffer(0).as_bytes_mut()[buffer_offset..],
output_row_stride,
byte_order,