Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from SmartReader to EndianReader #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/decoder/ifd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::{self, Read, Seek};
use std::mem;
use std::str;

use super::stream::{ByteOrder, EndianReader, SmartReader};
use super::stream::{ByteOrder, EndianReader};
use crate::tags::{Tag, Type};
use crate::{TiffError, TiffFormatError, TiffResult};

Expand Down Expand Up @@ -332,22 +332,22 @@ impl Entry {
}

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

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

let bo = reader.byte_order();
let bo = reader.byte_order;

let tag_size = match self.type_ {
Type::BYTE | Type::SBYTE | Type::ASCII | Type::UNDEFINED => 1,
Expand Down Expand Up @@ -450,7 +450,7 @@ impl Entry {
Type::SBYTE => return offset_to_sbytes(self.count as usize, self),
Type::ASCII => {
let mut buf = vec![0; self.count as usize];
self.r(bo).read_exact(&mut buf)?;
buf.copy_from_slice(&self.offset[..self.count as usize]);
if buf.is_ascii() && buf.ends_with(&[0]) {
let v = str::from_utf8(&buf)?;
let v = v.trim_matches(char::from(0));
Expand Down Expand Up @@ -532,7 +532,7 @@ impl Entry {
// at a different endianess of file/computer.
Type::BYTE => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
let mut buf = [0; 1];
reader.read_exact(&mut buf)?;
reader.inner().read_exact(&mut buf)?;
Ok(UnsignedBig(u64::from(buf[0])))
}),
Type::SBYTE => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
Expand Down Expand Up @@ -581,7 +581,7 @@ impl Entry {
Type::UNDEFINED => {
self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
let mut buf = [0; 1];
reader.read_exact(&mut buf)?;
reader.inner().read_exact(&mut buf)?;
Ok(Byte(buf[0]))
})
}
Expand All @@ -598,7 +598,7 @@ impl Entry {
}

let mut out = vec![0; n];
reader.read_exact(&mut out)?;
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);
Expand All @@ -615,12 +615,12 @@ impl Entry {
bo: ByteOrder,
bigtiff: bool,
limits: &super::Limits,
reader: &mut SmartReader<R>,
reader: &mut EndianReader<R>,
decode_fn: F,
) -> TiffResult<Value>
where
R: Read + Seek,
F: Fn(&mut SmartReader<R>) -> TiffResult<Value>,
F: Fn(&mut EndianReader<R>) -> TiffResult<Value>,
{
let value_count = usize::try_from(value_count)?;
if value_count > limits.decoding_buffer_size / mem::size_of::<Value>() {
Expand Down
4 changes: 2 additions & 2 deletions src/decoder/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::ifd::{Directory, Value};
use super::stream::{ByteOrder, DeflateReader, LZWReader, PackBitsReader};
use super::tag_reader::TagReader;
use super::{fp_predict_f32, fp_predict_f64, DecodingBuffer, Limits};
use super::{stream::SmartReader, ChunkType};
use super::{stream::EndianReader, ChunkType};
use crate::tags::{
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Predictor, SampleFormat, Tag,
};
Expand Down Expand Up @@ -81,7 +81,7 @@ pub(crate) struct Image {

impl Image {
pub fn from_reader<R: Read + Seek>(
reader: &mut SmartReader<R>,
reader: &mut EndianReader<R>,
ifd: Directory,
limits: &Limits,
bigtiff: bool,
Expand Down
32 changes: 16 additions & 16 deletions src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::tags::{
Tag, Type,
};

use self::stream::{ByteOrder, EndianReader, SmartReader};
use self::stream::{ByteOrder, EndianReader};

pub mod ifd;
mod image;
Expand Down Expand Up @@ -298,7 +298,7 @@ pub struct Decoder<R>
where
R: Read + Seek,
{
reader: SmartReader<R>,
reader: EndianReader<R>,
bigtiff: bool,
limits: Limits,
next_ifd: Option<u64>,
Expand Down Expand Up @@ -525,7 +525,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,
Expand Down Expand Up @@ -703,7 +703,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])
}

Expand Down Expand Up @@ -757,7 +757,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);
Expand All @@ -774,15 +774,15 @@ 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)
}

/// Reads a TIFF IFA offset/value field
#[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)
}

Expand All @@ -794,7 +794,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.
Expand All @@ -805,7 +805,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()?);
Expand All @@ -822,21 +822,21 @@ 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)))
}

/// 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>)> {
Expand Down Expand Up @@ -1038,12 +1038,12 @@ 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;

self.image.expand_chunk(
&mut self.reader,
self.reader.inner(),
buffer.copy(),
output_width,
byte_order,
Expand Down Expand Up @@ -1155,14 +1155,14 @@ 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;
let buffer_offset = y * strip_samples + x * chunk_dimensions.0 as usize * samples;
let byte_order = self.reader.byte_order;
self.image.expand_chunk(
&mut self.reader,
self.reader.inner(),
result.as_buffer(buffer_offset).copy(),
width as usize,
byte_order,
Expand Down
Loading
Loading