Merge pull request #49 from Masterchef365/deflate
Old-style DEFLATE compression support
This commit is contained in:
5 files changed
+78
-2
No files matched your search
@@ -21,6 +21,7 @@ byteorder = "1.2"
|
|||||||
lzw = "0.10"
|
lzw = "0.10"
|
||||||
num-derive = "0.3"
|
num-derive = "0.3"
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
|
miniz_oxide = "0.3"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "3.0"
|
tempfile = "3.0"
|
||||||
+7
-1
@@ -7,7 +7,7 @@ use {ColorType, TiffError, TiffFormatError, TiffResult, TiffUnsupportedError};
|
|||||||
|
|
||||||
use self::ifd::Directory;
|
use self::ifd::Directory;
|
||||||
|
|
||||||
use self::stream::{ByteOrder, EndianReader, LZWReader, PackBitsReader, SmartReader};
|
use self::stream::{ByteOrder, EndianReader, LZWReader, DeflateReader, PackBitsReader, SmartReader};
|
||||||
|
|
||||||
pub mod ifd;
|
pub mod ifd;
|
||||||
mod stream;
|
mod stream;
|
||||||
@@ -100,6 +100,8 @@ pub enum CompressionMethod {
|
|||||||
Fax4 = 4,
|
Fax4 = 4,
|
||||||
LZW = 5,
|
LZW = 5,
|
||||||
JPEG = 6,
|
JPEG = 6,
|
||||||
|
Deflate = 8,
|
||||||
|
OldDeflate = 0x80B2,
|
||||||
PackBits = 0x8005,
|
PackBits = 0x8005,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -582,6 +584,10 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
)?;
|
)?;
|
||||||
(bytes, Box::new(reader))
|
(bytes, Box::new(reader))
|
||||||
}
|
}
|
||||||
|
CompressionMethod::OldDeflate => {
|
||||||
|
let (bytes, reader) = DeflateReader::new(&mut self.reader, max_uncompressed_length)?;
|
||||||
|
(bytes, Box::new(reader))
|
||||||
|
}
|
||||||
method => {
|
method => {
|
||||||
return Err(TiffError::UnsupportedError(
|
return Err(TiffError::UnsupportedError(
|
||||||
TiffUnsupportedError::UnsupportedCompressionMethod(method),
|
TiffUnsupportedError::UnsupportedCompressionMethod(method),
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
//! All IO functionality needed for TIFF decoding
|
//! All IO functionality needed for TIFF decoding
|
||||||
|
|
||||||
|
use crate::error::{TiffError, TiffResult};
|
||||||
use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
|
use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
|
||||||
use lzw;
|
use lzw;
|
||||||
|
use miniz_oxide::inflate;
|
||||||
use std::io::{self, Read, Seek};
|
use std::io::{self, Read, Seek};
|
||||||
|
|
||||||
/// Byte order of the TIFF file.
|
/// Byte order of the TIFF file.
|
||||||
@@ -67,6 +69,52 @@ pub trait EndianReader: Read {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reader that decompresses DEFLATE streams
|
||||||
|
pub struct DeflateReader {
|
||||||
|
buffer: io::Cursor<Vec<u8>>,
|
||||||
|
byte_order: ByteOrder,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeflateReader {
|
||||||
|
pub fn new<R: Read + Seek>(
|
||||||
|
reader: &mut SmartReader<R>,
|
||||||
|
max_uncompressed_length: usize,
|
||||||
|
) -> TiffResult<(usize, Self)> {
|
||||||
|
let byte_order = reader.byte_order;
|
||||||
|
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 {
|
||||||
|
byte_order,
|
||||||
|
buffer: io::Cursor::new(uncompressed),
|
||||||
|
},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Read for DeflateReader {
|
||||||
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
self.buffer.read(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EndianReader for DeflateReader {
|
||||||
|
fn byte_order(&self) -> ByteOrder {
|
||||||
|
self.byte_order
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Reader that decompresses LZW streams
|
/// Reader that decompresses LZW streams
|
||||||
pub struct LZWReader {
|
pub struct LZWReader {
|
||||||
buffer: io::Cursor<Vec<u8>>,
|
buffer: io::Cursor<Vec<u8>>,
|
||||||
|
|||||||
+21
-1
@@ -5,6 +5,7 @@ use std::string;
|
|||||||
|
|
||||||
use decoder::ifd::{Tag, Value};
|
use decoder::ifd::{Tag, Value};
|
||||||
use decoder::{CompressionMethod, PhotometricInterpretation, PlanarConfiguration};
|
use decoder::{CompressionMethod, PhotometricInterpretation, PlanarConfiguration};
|
||||||
|
use miniz_oxide::inflate::TINFLStatus;
|
||||||
use ColorType;
|
use ColorType;
|
||||||
|
|
||||||
/// Tiff error kinds.
|
/// Tiff error kinds.
|
||||||
@@ -34,6 +35,7 @@ pub enum TiffFormatError {
|
|||||||
UnknownPredictor(u32),
|
UnknownPredictor(u32),
|
||||||
UnsignedIntegerExpected(Value),
|
UnsignedIntegerExpected(Value),
|
||||||
SignedIntegerExpected(Value),
|
SignedIntegerExpected(Value),
|
||||||
|
InflateError(InflateError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for TiffFormatError {
|
impl fmt::Display for TiffFormatError {
|
||||||
@@ -55,10 +57,28 @@ impl fmt::Display for TiffFormatError {
|
|||||||
SignedIntegerExpected(ref val) => {
|
SignedIntegerExpected(ref val) => {
|
||||||
write!(fmt, "Expected signed integer, {:?} found.", val)
|
write!(fmt, "Expected signed integer, {:?} found.", val)
|
||||||
}
|
}
|
||||||
|
InflateError(_) => write!(fmt, "Failed to decode inflate data."),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct InflateError {
|
||||||
|
status: TINFLStatus,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InflateError {
|
||||||
|
pub 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)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum TiffUnsupportedError {
|
pub enum TiffUnsupportedError {
|
||||||
HorizontalPredictor(ColorType),
|
HorizontalPredictor(ColorType),
|
||||||
@@ -138,7 +158,7 @@ impl Error for TiffError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cause(&self) -> Option<&Error> {
|
fn cause(&self) -> Option<&dyn Error> {
|
||||||
match *self {
|
match *self {
|
||||||
TiffError::IoError(ref e) => Some(e),
|
TiffError::IoError(ref e) => Some(e),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
extern crate byteorder;
|
extern crate byteorder;
|
||||||
extern crate lzw;
|
extern crate lzw;
|
||||||
|
extern crate miniz_oxide;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate num_derive;
|
extern crate num_derive;
|
||||||
extern crate num_traits;
|
extern crate num_traits;
|
||||||
|
|||||||
Reference in new issue
Block a user