Skip to content

Commit f44da4a

Browse files
authored
Switch from SmartReader to EndianReader (#233)
1 parent ee0dbf3 commit f44da4a

File tree

5 files changed

+78
-118
lines changed

5 files changed

+78
-118
lines changed

src/decoder/ifd.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::{self, Read, Seek};
55
use std::mem;
66
use std::str;
77

8-
use super::stream::{ByteOrder, EndianReader, SmartReader};
8+
use super::stream::{ByteOrder, EndianReader};
99
use crate::tags::{Tag, Type};
1010
use crate::{TiffError, TiffFormatError, TiffResult};
1111

@@ -364,22 +364,22 @@ impl Entry {
364364
}
365365

366366
/// Returns a mem_reader for the offset/value field
367-
fn r(&self, byte_order: ByteOrder) -> SmartReader<io::Cursor<Vec<u8>>> {
368-
SmartReader::wrap(io::Cursor::new(self.offset.to_vec()), byte_order)
367+
fn r(&self, byte_order: ByteOrder) -> EndianReader<io::Cursor<Vec<u8>>> {
368+
EndianReader::new(io::Cursor::new(self.offset.to_vec()), byte_order)
369369
}
370370

371-
pub fn val<R: Read + Seek>(
371+
pub(crate) fn val<R: Read + Seek>(
372372
&self,
373373
limits: &super::Limits,
374374
bigtiff: bool,
375-
reader: &mut SmartReader<R>,
375+
reader: &mut EndianReader<R>,
376376
) -> TiffResult<Value> {
377377
// Case 1: there are no values so we can return immediately.
378378
if self.count == 0 {
379379
return Ok(List(Vec::new()));
380380
}
381381

382-
let bo = reader.byte_order();
382+
let bo = reader.byte_order;
383383

384384
let tag_size = match self.type_ {
385385
Type::BYTE | Type::SBYTE | Type::ASCII | Type::UNDEFINED => 1,
@@ -482,7 +482,7 @@ impl Entry {
482482
Type::SBYTE => return offset_to_sbytes(self.count as usize, self),
483483
Type::ASCII => {
484484
let mut buf = vec![0; self.count as usize];
485-
self.r(bo).read_exact(&mut buf)?;
485+
buf.copy_from_slice(&self.offset[..self.count as usize]);
486486
if buf.is_ascii() && buf.ends_with(&[0]) {
487487
let v = str::from_utf8(&buf)?;
488488
let v = v.trim_matches(char::from(0));
@@ -564,7 +564,7 @@ impl Entry {
564564
// at a different endianess of file/computer.
565565
Type::BYTE => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
566566
let mut buf = [0; 1];
567-
reader.read_exact(&mut buf)?;
567+
reader.inner().read_exact(&mut buf)?;
568568
Ok(UnsignedBig(u64::from(buf[0])))
569569
}),
570570
Type::SBYTE => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
@@ -613,7 +613,7 @@ impl Entry {
613613
Type::UNDEFINED => {
614614
self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
615615
let mut buf = [0; 1];
616-
reader.read_exact(&mut buf)?;
616+
reader.inner().read_exact(&mut buf)?;
617617
Ok(Byte(buf[0]))
618618
})
619619
}
@@ -630,7 +630,7 @@ impl Entry {
630630
}
631631

632632
let mut out = vec![0; n];
633-
reader.read_exact(&mut out)?;
633+
reader.inner().read_exact(&mut out)?;
634634
// Strings may be null-terminated, so we trim anything downstream of the null byte
635635
if let Some(first) = out.iter().position(|&b| b == 0) {
636636
out.truncate(first);
@@ -647,12 +647,12 @@ impl Entry {
647647
bo: ByteOrder,
648648
bigtiff: bool,
649649
limits: &super::Limits,
650-
reader: &mut SmartReader<R>,
650+
reader: &mut EndianReader<R>,
651651
decode_fn: F,
652652
) -> TiffResult<Value>
653653
where
654654
R: Read + Seek,
655-
F: Fn(&mut SmartReader<R>) -> TiffResult<Value>,
655+
F: Fn(&mut EndianReader<R>) -> TiffResult<Value>,
656656
{
657657
let value_count = usize::try_from(value_count)?;
658658
if value_count > limits.decoding_buffer_size / mem::size_of::<Value>() {

src/decoder/image.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::ifd::{Directory, Value};
22
use super::stream::{ByteOrder, DeflateReader, LZWReader, PackBitsReader};
33
use super::tag_reader::TagReader;
44
use super::{predict_f16, predict_f32, predict_f64, Limits};
5-
use super::{stream::SmartReader, ChunkType};
5+
use super::{stream::EndianReader, ChunkType};
66
use crate::tags::{
77
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Predictor, SampleFormat, Tag,
88
};
@@ -80,7 +80,7 @@ pub(crate) struct Image {
8080

8181
impl Image {
8282
pub fn from_reader<R: Read + Seek>(
83-
reader: &mut SmartReader<R>,
83+
reader: &mut EndianReader<R>,
8484
ifd: Directory,
8585
limits: &Limits,
8686
bigtiff: bool,

src/decoder/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use half::f16;
1212

1313
use self::ifd::Directory;
1414
use self::image::Image;
15-
use self::stream::{ByteOrder, EndianReader, SmartReader};
15+
use self::stream::{ByteOrder, EndianReader};
1616

1717
pub mod ifd;
1818
mod image;
@@ -260,7 +260,7 @@ pub struct Decoder<R>
260260
where
261261
R: Read + Seek,
262262
{
263-
reader: SmartReader<R>,
263+
reader: EndianReader<R>,
264264
bigtiff: bool,
265265
limits: Limits,
266266
next_ifd: Option<u64>,
@@ -462,7 +462,7 @@ impl<R: Read + Seek> Decoder<R> {
462462
))
463463
}
464464
};
465-
let mut reader = SmartReader::wrap(r, byte_order);
465+
let mut reader = EndianReader::new(r, byte_order);
466466

467467
let bigtiff = match reader.read_u16()? {
468468
42 => false,
@@ -640,7 +640,7 @@ impl<R: Read + Seek> Decoder<R> {
640640
#[inline]
641641
pub fn read_byte(&mut self) -> Result<u8, io::Error> {
642642
let mut buf = [0; 1];
643-
self.reader.read_exact(&mut buf)?;
643+
self.reader.inner().read_exact(&mut buf)?;
644644
Ok(buf[0])
645645
}
646646

@@ -694,7 +694,7 @@ impl<R: Read + Seek> Decoder<R> {
694694
#[inline]
695695
pub fn read_string(&mut self, length: usize) -> TiffResult<String> {
696696
let mut out = vec![0; length];
697-
self.reader.read_exact(&mut out)?;
697+
self.reader.inner().read_exact(&mut out)?;
698698
// Strings may be null-terminated, so we trim anything downstream of the null byte
699699
if let Some(first) = out.iter().position(|&b| b == 0) {
700700
out.truncate(first);
@@ -711,15 +711,15 @@ impl<R: Read + Seek> Decoder<R> {
711711
));
712712
}
713713
let mut val = [0; 4];
714-
self.reader.read_exact(&mut val)?;
714+
self.reader.inner().read_exact(&mut val)?;
715715
Ok(val)
716716
}
717717

718718
/// Reads a TIFF IFA offset/value field
719719
#[inline]
720720
pub fn read_offset_u64(&mut self) -> Result<[u8; 8], io::Error> {
721721
let mut val = [0; 8];
722-
self.reader.read_exact(&mut val)?;
722+
self.reader.inner().read_exact(&mut val)?;
723723
Ok(val)
724724
}
725725

@@ -731,7 +731,7 @@ impl<R: Read + Seek> Decoder<R> {
731731

732732
#[inline]
733733
pub fn goto_offset_u64(&mut self, offset: u64) -> io::Result<()> {
734-
self.reader.seek(io::SeekFrom::Start(offset)).map(|_| ())
734+
self.reader.goto_offset(offset)
735735
}
736736

737737
/// Reads a IFD entry.
@@ -742,7 +742,7 @@ impl<R: Read + Seek> Decoder<R> {
742742
// Count 4 bytes
743743
// Value 4 bytes either a pointer the value itself
744744
fn read_entry(
745-
reader: &mut SmartReader<R>,
745+
reader: &mut EndianReader<R>,
746746
bigtiff: bool,
747747
) -> TiffResult<Option<(Tag, ifd::Entry)>> {
748748
let tag = Tag::from_u16_exhaustive(reader.read_u16()?);
@@ -759,21 +759,21 @@ impl<R: Read + Seek> Decoder<R> {
759759
let mut offset = [0; 8];
760760

761761
let count = reader.read_u64()?;
762-
reader.read_exact(&mut offset)?;
762+
reader.inner().read_exact(&mut offset)?;
763763
ifd::Entry::new_u64(type_, count, offset)
764764
} else {
765765
let mut offset = [0; 4];
766766

767767
let count = reader.read_u32()?;
768-
reader.read_exact(&mut offset)?;
768+
reader.inner().read_exact(&mut offset)?;
769769
ifd::Entry::new(type_, count, offset)
770770
};
771771
Ok(Some((tag, entry)))
772772
}
773773

774774
/// Reads the IFD starting at the indicated location.
775775
fn read_ifd(
776-
reader: &mut SmartReader<R>,
776+
reader: &mut EndianReader<R>,
777777
bigtiff: bool,
778778
ifd_location: u64,
779779
) -> TiffResult<(Directory, Option<u64>)> {
@@ -984,7 +984,7 @@ impl<R: Read + Seek> Decoder<R> {
984984
output_width: usize,
985985
) -> TiffResult<()> {
986986
let offset = self.image.chunk_file_range(chunk_index)?.0;
987-
self.goto_offset_u64(offset)?;
987+
self.reader.goto_offset(offset)?;
988988

989989
let byte_order = self.reader.byte_order;
990990

@@ -994,7 +994,7 @@ impl<R: Read + Seek> Decoder<R> {
994994
/ 8;
995995

996996
self.image.expand_chunk(
997-
&mut self.reader,
997+
self.reader.inner(),
998998
buffer.as_bytes_mut(),
999999
output_row_stride.try_into()?,
10001000
byte_order,
@@ -1129,15 +1129,15 @@ impl<R: Read + Seek> Decoder<R> {
11291129
// * pass requested band as parameter
11301130
// * collect bands to a RGB encoding result in case of RGB bands
11311131
for chunk in 0..image_chunks {
1132-
self.goto_offset_u64(self.image().chunk_offsets[chunk])?;
1132+
self.reader.goto_offset(self.image().chunk_offsets[chunk])?;
11331133

11341134
let x = chunk % chunks_across;
11351135
let y = chunk / chunks_across;
11361136
let buffer_offset =
11371137
y * output_row_stride * chunk_dimensions.1 as usize + x * chunk_row_bytes;
11381138
let byte_order = self.reader.byte_order;
11391139
self.image.expand_chunk(
1140-
&mut self.reader,
1140+
self.reader.inner(),
11411141
&mut result.as_buffer(0).as_bytes_mut()[buffer_offset..],
11421142
output_row_stride,
11431143
byte_order,

0 commit comments

Comments
 (0)