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 8 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"
fax = { git = "https://github.com/stephenjudkins/fax.git", rev = "7dde1564500cfc046f0489a87761a5d9111fc205" }
bitvec = "1.0.1"

[dev-dependencies]
criterion = "0.3.1"
Expand Down
70 changes: 69 additions & 1 deletion src/decoder/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ use crate::tags::{
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Predictor, SampleFormat, Tag,
};
use crate::{ColorType, TiffError, TiffFormatError, TiffResult, TiffUnsupportedError, UsageError};
use std::io::{self, Cursor, Read, Seek};
use bitvec;
use fax;
use fax::decoder::Group4Decoder;
use std::borrow::Borrow;
use std::collections::VecDeque;
use std::io::{self, Cursor, Read, Seek, Write};
use std::sync::Arc;

#[derive(Debug)]
Expand Down Expand Up @@ -368,6 +373,7 @@ impl Image {
}

fn create_reader<'r, R: 'r + Read>(
dimensions: (u32, u32),
reader: R,
photometric_interpretation: PhotometricInterpretation,
compression_method: CompressionMethod,
Expand Down Expand Up @@ -447,6 +453,67 @@ impl Image {

Box::new(Cursor::new(data))
}
CompressionMethod::Fax4 => {
let width = u16::try_from(dimensions.0)?;
let height = u16::try_from(dimensions.1)?;

struct Group4Reader<R2> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see this moved into decoder/stream.rs alongside the other decompressors

decoder: fax::decoder::Group4Decoder<std::io::Bytes<R2>>,
bits: bitvec::vec::BitVec<u8, bitvec::prelude::Msb0>,
byte_buf: VecDeque<u8>,
y: u16,
height: u16,
width: u16,
}

impl<R2: Read> Read for Group4Reader<R2> {
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 => {
// eprintln!("DONE!");
self.byte_buf.extend(self.bits.as_raw_slice())
}
fax::decoder::DecodeStatus::Incomplete => {
self.y += 1;
// eprintln!("{:?}", self.y);
for c in
fax::decoder::pels(self.decoder.transition(), self.width)
{
self.bits.push(match c {
fax::Color::Black => true,
fax::Color::White => false,
});
if self.bits.len() == 8 {
self.byte_buf.extend(self.bits.as_raw_slice());
self.bits.clear()
}
}
}
}
}
// eprintln!("{:?}: {:?} / {:?}", self.y, self.byte_buf.len(), self.bits.len());

self.byte_buf.read(buf)
}
}

Box::new(Group4Reader {
decoder: fax::decoder::Group4Decoder::new(
reader.take(compressed_length).bytes(),
width,
)?,
bits: bitvec::vec::BitVec::new(),
byte_buf: VecDeque::new(),
y: 0,
width: width,
height: height,
})
}
method => {
return Err(TiffError::UnsupportedError(
TiffUnsupportedError::UnsupportedCompressionMethod(method),
Expand Down Expand Up @@ -634,6 +701,7 @@ impl Image {
let padding_right = chunk_dims.0 - data_dims.0;

let mut reader = Self::create_reader(
chunk_dims,
reader,
photometric_interpretation,
compression_method,
Expand Down
5 changes: 4 additions & 1 deletion src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,10 @@ impl<R: Read + Seek> Decoder<R> {
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),
n if n < 8 => {
DecodingResult::new_u8(buffer_size / 8 * usize::from(n), &self.limits)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm slightly suspicious that this will round incorrectly if the buffer_size isn't a multiple of 8

}
n if n == 8 => DecodingResult::new_u8(buffer_size, &self.limits),
n if n <= 16 => DecodingResult::new_u16(buffer_size, &self.limits),
n if n <= 32 => DecodingResult::new_u32(buffer_size, &self.limits),
n if n <= 64 => DecodingResult::new_u64(buffer_size, &self.limits),
Expand Down
5 changes: 5 additions & 0 deletions tests/decode_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,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), 62384985);
}
Binary file added tests/images/fax4.tiff
Binary file not shown.