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

CCITT group 4 (Fax4) decoding support #229

Open
wants to merge 13 commits 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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ exclude = ["tests/images/*", "tests/fuzz_images/*"]
weezl = "0.1.0"
jpeg = { package = "jpeg-decoder", version = "0.3.0", default-features = false }
flate2 = "1.0.20"
bitvec = "1.0.1"
fax = "0.2.4"

[dev-dependencies]
criterion = "0.3.1"
Expand Down
23 changes: 21 additions & 2 deletions src/decoder/image.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::ifd::{Directory, Value};
use super::stream::{ByteOrder, DeflateReader, LZWReader, PackBitsReader};
use super::stream::{ByteOrder, DeflateReader, Group4Reader, LZWReader, PackBitsReader};
use super::tag_reader::TagReader;
use super::{predict_f32, predict_f64, Limits};
use super::{stream::SmartReader, ChunkType};
Expand Down Expand Up @@ -76,6 +76,7 @@ pub(crate) struct Image {
pub tile_attributes: Option<TileAttributes>,
pub chunk_offsets: Vec<u64>,
pub chunk_bytes: Vec<u64>,
pub expand_samples_to_bytes: bool,
}

impl Image {
Expand All @@ -84,6 +85,7 @@ impl Image {
ifd: Directory,
limits: &Limits,
bigtiff: bool,
expand_samples_to_bytes: bool,
) -> TiffResult<Image> {
let mut tag_reader = TagReader {
reader,
Expand Down Expand Up @@ -308,6 +310,7 @@ impl Image {
tile_attributes,
chunk_offsets,
chunk_bytes,
expand_samples_to_bytes,
})
}

Expand Down Expand Up @@ -368,11 +371,13 @@ impl Image {
}

fn create_reader<'r, R: 'r + Read>(
dimensions: (u32, u32),
reader: R,
photometric_interpretation: PhotometricInterpretation,
compression_method: CompressionMethod,
compressed_length: u64,
jpeg_tables: Option<&[u8]>,
expand_samples_to_bytes: bool,
) -> TiffResult<Box<dyn Read + 'r>> {
Ok(match compression_method {
CompressionMethod::None => Box::new(reader),
Expand Down Expand Up @@ -447,6 +452,12 @@ impl Image {

Box::new(Cursor::new(data))
}
CompressionMethod::Fax4 => Box::new(Group4Reader::new(
dimensions,
reader,
compressed_length,
expand_samples_to_bytes,
)?),
method => {
return Err(TiffError::UnsupportedError(
TiffUnsupportedError::UnsupportedCompressionMethod(method),
Expand Down Expand Up @@ -529,7 +540,13 @@ impl Image {
// Ignore potential vertical padding on the bottommost strip
let strip_height = dims.1.min(strip_height_without_padding);

Ok((dims.0, strip_height))
let strip_width = if !self.expand_samples_to_bytes && self.bits_per_sample < 8 {
(dims.0 * self.bits_per_sample as u32).div_ceil(8)
} else {
dims.0
};

Ok((strip_width, strip_height))
}
ChunkType::Tile => {
let tile_attrs = self.tile_attributes.as_ref().unwrap();
Expand Down Expand Up @@ -638,11 +655,13 @@ impl Image {
assert!(buf.len() >= output_row_stride * (data_dims.1 as usize - 1) + data_row_bytes);

let mut reader = Self::create_reader(
chunk_dims,
reader,
photometric_interpretation,
compression_method,
*compressed_bytes,
self.jpeg_tables.as_deref().map(|a| &**a),
self.expand_samples_to_bytes,
)?;

if output_row_stride == chunk_row_bytes as usize {
Expand Down
36 changes: 32 additions & 4 deletions src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ where
ifd_offsets: Vec<u64>,
seen_ifds: HashSet<u64>,
image: Image,
options: DecoderOptions,
}

fn rev_hpredict_nsamp(buf: &mut [u8], bit_depth: u8, samples: usize) {
Expand Down Expand Up @@ -420,9 +421,21 @@ fn fix_endianness(buf: &mut [u8], byte_order: ByteOrder, bit_depth: u8) {
};
}

#[derive(Debug)]
pub struct DecoderOptions {
pub expand_samples_to_bytes: bool,
}

impl<R: Read + Seek> Decoder<R> {
pub fn new(r: R) -> TiffResult<Decoder<R>> {
let default_options = DecoderOptions {
expand_samples_to_bytes: false,
};
Self::new_with_options(r, default_options)
}

/// Create a new decoder that decodes from the stream ```r```
pub fn new(mut r: R) -> TiffResult<Decoder<R>> {
pub fn new_with_options(mut r: R, options: DecoderOptions) -> TiffResult<Decoder<R>> {
let mut endianess = Vec::with_capacity(2);
(&mut r).take(2).read_to_end(&mut endianess)?;
let byte_order = match &*endianess {
Expand Down Expand Up @@ -493,7 +506,9 @@ impl<R: Read + Seek> Decoder<R> {
tile_attributes: None,
chunk_offsets: Vec::new(),
chunk_bytes: Vec::new(),
expand_samples_to_bytes: options.expand_samples_to_bytes,
},
options: options,
};
decoder.next_image()?;
Ok(decoder)
Expand Down Expand Up @@ -545,7 +560,13 @@ impl<R: Read + Seek> Decoder<R> {
if let Some(ifd_offset) = self.ifd_offsets.get(ifd_index) {
let (ifd, _next_ifd) = Self::read_ifd(&mut self.reader, self.bigtiff, *ifd_offset)?;

self.image = Image::from_reader(&mut self.reader, ifd, &self.limits, self.bigtiff)?;
self.image = Image::from_reader(
&mut self.reader,
ifd,
&self.limits,
self.bigtiff,
self.options.expand_samples_to_bytes,
)?;

Ok(())
} else {
Expand Down Expand Up @@ -585,7 +606,13 @@ impl<R: Read + Seek> Decoder<R> {
pub fn next_image(&mut self) -> TiffResult<()> {
let (ifd, _next_ifd) = self.next_ifd()?;

self.image = Image::from_reader(&mut self.reader, ifd, &self.limits, self.bigtiff)?;
self.image = Image::from_reader(
&mut self.reader,
ifd,
&self.limits,
self.bigtiff,
self.options.expand_samples_to_bytes,
)?;
Ok(())
}

Expand Down Expand Up @@ -971,7 +998,7 @@ impl<R: Read + Seek> Decoder<R> {
fn result_buffer(&self, width: usize, height: usize) -> TiffResult<DecodingResult> {
let bits_per_sample = self.image().bits_per_sample;

let row_samples = if bits_per_sample >= 8 {
let row_samples = if bits_per_sample >= 8 || self.options.expand_samples_to_bytes {
width
} else {
((((width as u64) * bits_per_sample as u64) + 7) / 8)
Expand All @@ -985,6 +1012,7 @@ impl<R: Read + Seek> Decoder<R> {
.ok_or(TiffError::LimitsExceeded)?;

let max_sample_bits = self.image().bits_per_sample;

match self.image().sample_format {
SampleFormat::Uint => match max_sample_bits {
n if n <= 8 => DecodingResult::new_u8(buffer_size, &self.limits),
Expand Down
70 changes: 70 additions & 0 deletions src/decoder/stream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! All IO functionality needed for TIFF decoding

use crate::TiffResult;
use bitvec::order::Msb0;
use bitvec::vec::BitVec;
use fax::decoder::Group4Decoder;
use std::collections::VecDeque;
use std::io::{self, BufRead, BufReader, Read, Seek, Take};

/// Byte order of the TIFF file.
Expand Down Expand Up @@ -252,6 +257,71 @@ impl<R: Read> Read for PackBitsReader<R> {
}
}

pub struct Group4Reader<R: Read> {
decoder: Group4Decoder<std::io::Bytes<std::io::Take<R>>>,
bits: BitVec<u8, Msb0>,
byte_buf: VecDeque<u8>,
height: u16,
width: u16,
y: u16,
expand_samples_to_bytes: bool,
}

impl<R: Read> Group4Reader<R> {
pub fn new(
dimensions: (u32, u32),
reader: R,
compressed_length: u64,
expand_samples_to_bytes: bool,
) -> TiffResult<Self> {
let width = u16::try_from(dimensions.0)?;
let height = u16::try_from(dimensions.1)?;

Ok(Self {
decoder: Group4Decoder::new(reader.take(compressed_length).bytes(), width)?,
bits: BitVec::new(),
byte_buf: VecDeque::new(),
width: width,
height: height,
y: 0,
expand_samples_to_bytes: expand_samples_to_bytes,
})
}
}

impl<R: Read> Read for Group4Reader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if self.byte_buf.is_empty() && self.y < self.height {
let next = self
.decoder
.advance()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;

match next {
fax::decoder::DecodeStatus::End => (),
fax::decoder::DecodeStatus::Incomplete => {
self.y += 1;
let transitions = fax::decoder::pels(self.decoder.transition(), self.width);
if self.expand_samples_to_bytes {
self.byte_buf.extend(transitions.map(|c| match c {
fax::Color::Black => 0xFF,
fax::Color::White => 0x00,
}))
} else {
self.bits.extend(transitions.map(|c| match c {
fax::Color::Black => true,
fax::Color::White => false,
}));
self.byte_buf.extend(self.bits.as_raw_slice());
self.bits.clear();
}
}
}
}
self.byte_buf.read(buf)
}
}

///
/// ## SmartReader Reader
///
Expand Down
5 changes: 5 additions & 0 deletions tests/decode_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,8 @@ fn test_predictor_3_rgb_f32() {
fn test_predictor_3_gray_f32() {
test_image_sum_f32("predictor-3-gray-f32.tif", ColorType::Gray(32), 20008.275);
}

#[test]
fn test_fax4() {
test_image_sum_u8("fax4.tiff", ColorType::Gray(1), 7802706);
}
Binary file added tests/images/fax4.tiff
Binary file not shown.
Loading