Improve deflate support
- Switch to streaming decompression via `flate2`. Aside from performance improvements and lower RAM consumption, this fixes a bug where `max_uncompressed_length` was precalculated for a single tile but then used as a hard limit on the whole data, failing to decompress any tiled images. - Deprecate `InflateError` - this was used for wrapping `miniz_oxide` raw status, but `flate2` already reports them as `io::Error` instead. This makes errors from both Lzw and Deflate modes more consistent - they're all now just `io::Error`. - Add support for new `Deflate` tag in addition to `OldDeflate` - format-wise it's same Zlib, and on practice decodes successfully with the same underlying code. - Slightly simplify partial reads from the `Reader` by using the `std::io::copy` helper instead of doing it manually. Fixes #131.
This commit is contained in:
1 parent
a874b9c3a5
commit
a6fb855121
5 files changed
+18
-78
No files matched your search
+1
-4
@@ -20,10 +20,7 @@ edition = "2018"
|
||||
[dependencies]
|
||||
weezl = "0.1.0"
|
||||
jpeg = { package = "jpeg-decoder", version = "0.1.17", default-features = false }
|
||||
|
||||
[dependencies.miniz_oxide]
|
||||
version = "0.4.1"
|
||||
features = ["no_extern_crate_alloc"]
|
||||
flate2 = "1.0.20"
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.3.1"
|
||||
|
||||
+8
-16
@@ -1084,7 +1084,7 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
// Construct necessary reader to perform decompression.
|
||||
self.goto_offset_u64(offset)?;
|
||||
let byte_order = self.reader.byte_order;
|
||||
let mut reader: Box<dyn Read> = match self.compression_method {
|
||||
let reader: Box<dyn Read> = match self.compression_method {
|
||||
CompressionMethod::None => Box::new(&mut self.reader),
|
||||
CompressionMethod::LZW => Box::new(
|
||||
LZWReader::new(
|
||||
@@ -1097,8 +1097,8 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
CompressionMethod::PackBits => {
|
||||
Box::new(PackBitsReader::new(&mut self.reader, usize::try_from(length)?)?.1)
|
||||
}
|
||||
CompressionMethod::OldDeflate => {
|
||||
Box::new(DeflateReader::new(&mut self.reader, buffer.len() * buffer.byte_len())?.1)
|
||||
CompressionMethod::Deflate | CompressionMethod::OldDeflate => {
|
||||
Box::new(DeflateReader::new(&mut self.reader))
|
||||
}
|
||||
method => {
|
||||
return Err(TiffError::UnsupportedError(
|
||||
@@ -1109,7 +1109,7 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
|
||||
// Read into output buffer.
|
||||
{
|
||||
let buffer = match &mut buffer {
|
||||
let mut buffer = match &mut buffer {
|
||||
DecodingBuffer::U8(buf) => &mut *buf,
|
||||
DecodingBuffer::I8(buf) => bytecast::i8_as_ne_mut_bytes(buf),
|
||||
DecodingBuffer::U16(buf) => bytecast::u16_as_ne_mut_bytes(buf),
|
||||
@@ -1122,16 +1122,8 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
DecodingBuffer::F64(buf) => bytecast::f64_as_ne_mut_bytes(buf),
|
||||
};
|
||||
|
||||
let mut bytes_written = 0;
|
||||
while bytes_written < buffer.len() {
|
||||
match reader.read(&mut buffer[bytes_written..]) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => bytes_written += n,
|
||||
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
|
||||
Err(e) => Err(e)?,
|
||||
}
|
||||
}
|
||||
for b in &mut buffer[bytes_written..] {
|
||||
std::io::copy(&mut reader.take(buffer.len() as u64), &mut buffer)?;
|
||||
for b in buffer {
|
||||
*b = 0;
|
||||
}
|
||||
}
|
||||
@@ -1383,8 +1375,8 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
CompressionMethod::PackBits => {
|
||||
Box::new(PackBitsReader::new(reader, usize::try_from(compressed_length)?)?.1)
|
||||
}
|
||||
CompressionMethod::OldDeflate => {
|
||||
Box::new(DeflateReader::new(reader, samples * byte_len)?.1)
|
||||
CompressionMethod::Deflate | CompressionMethod::OldDeflate => {
|
||||
Box::new(DeflateReader::new(reader))
|
||||
}
|
||||
method => {
|
||||
return Err(TiffError::UnsupportedError(
|
||||
|
||||
+1
-38
@@ -1,7 +1,5 @@
|
||||
//! All IO functionality needed for TIFF decoding
|
||||
|
||||
use crate::error::{TiffError, TiffResult};
|
||||
use miniz_oxide::inflate;
|
||||
use std::io::{self, Read, Seek};
|
||||
|
||||
/// Byte order of the TIFF file.
|
||||
@@ -126,42 +124,7 @@ pub trait EndianReader: Read {
|
||||
/// ## Deflate Reader
|
||||
///
|
||||
|
||||
/// Reader that decompresses DEFLATE streams
|
||||
pub struct DeflateReader {
|
||||
buffer: io::Cursor<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl DeflateReader {
|
||||
pub fn new<R: Read + Seek>(
|
||||
reader: &mut SmartReader<R>,
|
||||
max_uncompressed_length: usize,
|
||||
) -> TiffResult<(usize, Self)> {
|
||||
let mut compressed = Vec::new();
|
||||
reader.read_to_end(&mut compressed)?;
|
||||
|
||||
// TODO: Implement streaming compression, and remove this (temporary) and somewhat
|
||||
// misleading workaround.
|
||||
if compressed.len() > max_uncompressed_length {
|
||||
return Err(TiffError::LimitsExceeded);
|
||||
}
|
||||
|
||||
let uncompressed =
|
||||
inflate::decompress_to_vec_zlib(&compressed).map_err(TiffError::from_inflate_status)?;
|
||||
|
||||
Ok((
|
||||
uncompressed.len(),
|
||||
Self {
|
||||
buffer: io::Cursor::new(uncompressed),
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for DeflateReader {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
self.buffer.read(buf)
|
||||
}
|
||||
}
|
||||
pub type DeflateReader<'r, R> = flate2::read::ZlibDecoder<&'r mut SmartReader<R>>;
|
||||
|
||||
///
|
||||
/// ## LZW Reader
|
||||
|
||||
+4
-16
@@ -9,7 +9,6 @@ use crate::tags::{
|
||||
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, SampleFormat, Tag,
|
||||
};
|
||||
use crate::ColorType;
|
||||
use miniz_oxide::inflate::TINFLStatus;
|
||||
|
||||
/// Tiff error kinds.
|
||||
#[derive(Debug)]
|
||||
@@ -62,6 +61,7 @@ pub enum TiffFormatError {
|
||||
ByteExpected(Value),
|
||||
UnsignedIntegerExpected(Value),
|
||||
SignedIntegerExpected(Value),
|
||||
#[allow(deprecated)]
|
||||
InflateError(InflateError),
|
||||
Format(String),
|
||||
RequiredTagEmpty(Tag),
|
||||
@@ -123,21 +123,9 @@ impl fmt::Display for TiffFormatError {
|
||||
|
||||
/// Decompression failed due to faulty compressed data.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct InflateError {
|
||||
status: TINFLStatus,
|
||||
}
|
||||
|
||||
impl InflateError {
|
||||
pub(crate) fn new(status: TINFLStatus) -> Self {
|
||||
Self { status }
|
||||
}
|
||||
}
|
||||
|
||||
impl TiffError {
|
||||
pub(crate) fn from_inflate_status(status: TINFLStatus) -> Self {
|
||||
TiffError::FormatError(TiffFormatError::InflateError(InflateError::new(status)))
|
||||
}
|
||||
}
|
||||
#[allow(deprecated)]
|
||||
#[deprecated = "This type is not used anymore: deflate errors are now propagated as regular `io::Error`s"]
|
||||
pub struct InflateError(());
|
||||
|
||||
/// The Decoder does not support features required by the image.
|
||||
///
|
||||
|
||||
+4
-4
@@ -7,7 +7,6 @@
|
||||
//! * <https://www.adobe.io/open/standards/TIFF.html> - The TIFF specification
|
||||
|
||||
extern crate jpeg;
|
||||
extern crate miniz_oxide;
|
||||
extern crate weezl;
|
||||
|
||||
mod bytecast;
|
||||
@@ -16,9 +15,10 @@ pub mod encoder;
|
||||
mod error;
|
||||
pub mod tags;
|
||||
|
||||
pub use self::error::{
|
||||
InflateError, TiffError, TiffFormatError, TiffResult, TiffUnsupportedError, UsageError,
|
||||
};
|
||||
pub use self::error::{TiffError, TiffFormatError, TiffResult, TiffUnsupportedError, UsageError};
|
||||
|
||||
#[allow(deprecated)]
|
||||
pub use self::error::InflateError;
|
||||
|
||||
/// An enumeration over supported color types and their bit depths
|
||||
#[derive(Copy, PartialEq, Eq, Debug, Clone, Hash)]
|
||||
|
||||
Reference in new issue
Block a user