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

Improve deflate support #132

Merged
merged 1 commit into from
May 14, 2021
Merged
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
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 8 additions & 16 deletions src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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),
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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(
Expand Down
39 changes: 1 addition & 38 deletions src/decoder/stream.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
20 changes: 4 additions & 16 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -62,6 +61,7 @@ pub enum TiffFormatError {
ByteExpected(Value),
UnsignedIntegerExpected(Value),
SignedIntegerExpected(Value),
#[allow(deprecated)]
InflateError(InflateError),
Format(String),
RequiredTagEmpty(Tag),
Expand Down Expand Up @@ -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.
///
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)]
Expand Down