Initialize Decoder in new instead of init
This commit is contained in:
1 parent
77f15b5c0a
commit
e9aa39d895
2 files changed
+62
-60
No files matched your search
+59
-60
@@ -525,12 +525,58 @@ fn rev_hpredict(
|
|||||||
impl<R: Read + Seek> Decoder<R> {
|
impl<R: Read + Seek> Decoder<R> {
|
||||||
/// Create a new decoder that decodes from the stream ```r```
|
/// Create a new decoder that decodes from the stream ```r```
|
||||||
pub fn new(r: R) -> TiffResult<Decoder<R>> {
|
pub fn new(r: R) -> TiffResult<Decoder<R>> {
|
||||||
Decoder {
|
let mut endianess = Vec::with_capacity(2);
|
||||||
reader: SmartReader::wrap(r, ByteOrder::LittleEndian),
|
r.take(2).read_to_end(&mut endianess)?;
|
||||||
byte_order: ByteOrder::LittleEndian,
|
let byte_order = match &*endianess {
|
||||||
bigtiff: false,
|
b"II" => {
|
||||||
|
ByteOrder::LittleEndian
|
||||||
|
}
|
||||||
|
b"MM" => {
|
||||||
|
ByteOrder::BigEndian
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::TiffSignatureNotFound,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut reader = SmartReader::wrap(r, byte_order);
|
||||||
|
|
||||||
|
let bigtiff = match reader.read_u16()? {
|
||||||
|
42 => false,
|
||||||
|
43 => {
|
||||||
|
// Read bytesize of offsets (in bigtiff it's alway 8 but provide a way to move to 16 some day)
|
||||||
|
if reader.read_u16()? != 8 {
|
||||||
|
return Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::TiffSignatureNotFound,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
// This constant should always be 0
|
||||||
|
if reader.read_u16()? != 0 {
|
||||||
|
return Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::TiffSignatureNotFound,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::TiffSignatureInvalid,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let next_ifd = if bigtiff {
|
||||||
|
Some(reader.read_u64()?)
|
||||||
|
} else {
|
||||||
|
Some(u64::from(reader.read_u32()?))
|
||||||
|
};
|
||||||
|
|
||||||
|
let decoder = Decoder {
|
||||||
|
reader,
|
||||||
|
byte_order,
|
||||||
|
bigtiff,
|
||||||
limits: Default::default(),
|
limits: Default::default(),
|
||||||
next_ifd: None,
|
next_ifd,
|
||||||
ifd: None,
|
ifd: None,
|
||||||
width: 0,
|
width: 0,
|
||||||
height: 0,
|
height: 0,
|
||||||
@@ -544,8 +590,9 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
tile_decoder: None,
|
tile_decoder: None,
|
||||||
tile_attributes: None,
|
tile_attributes: None,
|
||||||
seen_ifds: HashSet::new(),
|
seen_ifds: HashSet::new(),
|
||||||
}
|
};
|
||||||
.init()
|
decoder.next_image()?;
|
||||||
|
Ok(decoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_limits(mut self, limits: Limits) -> Decoder<R> {
|
pub fn with_limits(mut self, limits: Limits) -> Decoder<R> {
|
||||||
@@ -599,59 +646,11 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_header(&mut self) -> TiffResult<()> {
|
/// Reset the decoder.
|
||||||
let mut endianess = Vec::with_capacity(2);
|
#[deprecated = "Never should have been public. Only use Decoder::new()"]
|
||||||
self.reader.by_ref().take(2).read_to_end(&mut endianess)?;
|
pub fn init(self) -> TiffResult<Decoder<R>> {
|
||||||
match &*endianess {
|
let Self { reader, .. } = self;
|
||||||
b"II" => {
|
Self::new(reader.into_inner())
|
||||||
self.byte_order = ByteOrder::LittleEndian;
|
|
||||||
self.reader.byte_order = ByteOrder::LittleEndian;
|
|
||||||
}
|
|
||||||
b"MM" => {
|
|
||||||
self.byte_order = ByteOrder::BigEndian;
|
|
||||||
self.reader.byte_order = ByteOrder::BigEndian;
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return Err(TiffError::FormatError(
|
|
||||||
TiffFormatError::TiffSignatureNotFound,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match self.read_short()? {
|
|
||||||
42 => self.bigtiff = false,
|
|
||||||
43 => {
|
|
||||||
self.bigtiff = true;
|
|
||||||
// Read bytesize of offsets (in bigtiff it's alway 8 but provide a way to move to 16 some day)
|
|
||||||
if self.read_short()? != 8 {
|
|
||||||
return Err(TiffError::FormatError(
|
|
||||||
TiffFormatError::TiffSignatureNotFound,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
// This constant should always be 0
|
|
||||||
if self.read_short()? != 0 {
|
|
||||||
return Err(TiffError::FormatError(
|
|
||||||
TiffFormatError::TiffSignatureNotFound,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return Err(TiffError::FormatError(
|
|
||||||
TiffFormatError::TiffSignatureInvalid,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.next_ifd = match self.read_ifd_offset_nonrepeating()? {
|
|
||||||
0 => None,
|
|
||||||
n => Some(n),
|
|
||||||
};
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Initializes the decoder.
|
|
||||||
pub fn init(mut self) -> TiffResult<Decoder<R>> {
|
|
||||||
self.read_header()?;
|
|
||||||
self.next_image()?;
|
|
||||||
Ok(self)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads in the next image.
|
/// Reads in the next image.
|
||||||
|
|||||||
@@ -339,6 +339,9 @@ where
|
|||||||
pub fn wrap(reader: R, byte_order: ByteOrder) -> SmartReader<R> {
|
pub fn wrap(reader: R, byte_order: ByteOrder) -> SmartReader<R> {
|
||||||
SmartReader { reader, byte_order }
|
SmartReader { reader, byte_order }
|
||||||
}
|
}
|
||||||
|
pub fn into_inner(self) -> R {
|
||||||
|
self.reader
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R> EndianReader for SmartReader<R>
|
impl<R> EndianReader for SmartReader<R>
|
||||||
|
|||||||
Reference in new issue
Block a user