diff --git a/src/decoder/image.rs b/src/decoder/image.rs index c22f963..c678da8 100644 --- a/src/decoder/image.rs +++ b/src/decoder/image.rs @@ -4,8 +4,8 @@ use super::stream::{ PackBitsReader, }; use super::tag_reader::TagReader; +use super::{fp_predict_f32, fp_predict_f64, DecodingBuffer, Limits}; use super::{stream::SmartReader, ChunkType}; -use super::{DecodingBuffer, Limits}; use crate::tags::{CompressionMethod, PhotometricInterpretation, Predictor, SampleFormat, Tag}; use crate::{ColorType, TiffError, TiffFormatError, TiffResult, TiffUnsupportedError}; use std::convert::{TryFrom, TryInto}; @@ -406,6 +406,11 @@ impl Image { TiffUnsupportedError::HorizontalPredictor(color_type), )) } + Predictor::FloatingPoint => { + return Err(TiffError::UnsupportedError( + TiffUnsupportedError::FloatingPointPredictor(color_type), + )); + } Predictor::__NonExhaustive => unreachable!(), }, (type_, _) => { @@ -423,6 +428,13 @@ impl Image { TiffUnsupportedError::HorizontalPredictor(color_type), )); } + (Predictor::FloatingPoint, DecodingBuffer::F32(_)) + | (Predictor::FloatingPoint, DecodingBuffer::F64(_)) => {} + (Predictor::FloatingPoint, _) => { + return Err(TiffError::UnsupportedError( + TiffUnsupportedError::FloatingPointPredictor(color_type), + )); + } _ => {} } @@ -471,6 +483,24 @@ impl Image { if photometric_interpretation == PhotometricInterpretation::WhiteIsZero { super::invert_colors(&mut buffer.subrange(0..total_samples), color_type); } + } else if padding.0 > 0 && self.predictor == Predictor::FloatingPoint { + // The floating point predictor shuffles the padding bytes into the encoded output, so + // this case is handled specially when needed. + for row in 0..(chunk_dimensions.1 - padding.1) { + let row_start = row * output_width * samples; + let row_end = row_start + (chunk_dimensions.0 - padding.0) * samples; + + let mut encoded = vec![0u8; chunk_dimensions.0 * samples * byte_len]; + reader.read_exact(&mut encoded)?; + match buffer.subrange(row_start..row_end) { + DecodingBuffer::F32(buf) => fp_predict_f32(&mut encoded, buf, samples), + DecodingBuffer::F64(buf) => fp_predict_f64(&mut encoded, buf, samples), + _ => unreachable!(), + } + if photometric_interpretation == PhotometricInterpretation::WhiteIsZero { + super::invert_colors(&mut buffer.subrange(row_start..row_end), color_type); + } + } } else { for row in 0..(chunk_dimensions.1 - padding.1) { let row_start = row * output_width * samples; diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 844cdaf..22face5 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -365,6 +365,36 @@ fn rev_hpredict_nsamp(image: &mut [T], samples: usize) { } } +pub fn fp_predict_f32(input: &mut [u8], output: &mut [f32], samples: usize) { + rev_hpredict_nsamp(input, samples); + for i in 0..output.len() { + // TODO: use f32::from_be_bytes() when we can (version 1.40) + output[i] = f32::from_bits(u32::from_be_bytes([ + input[input.len() / 4 * 0 + i], + input[input.len() / 4 * 1 + i], + input[input.len() / 4 * 2 + i], + input[input.len() / 4 * 3 + i], + ])); + } +} + +pub fn fp_predict_f64(input: &mut [u8], output: &mut [f64], samples: usize) { + rev_hpredict_nsamp(input, samples); + for i in 0..output.len() { + // TODO: use f64::from_be_bytes() when we can (version 1.40) + output[i] = f64::from_bits(u64::from_be_bytes([ + input[input.len() / 8 * 0 + i], + input[input.len() / 8 * 1 + i], + input[input.len() / 8 * 2 + i], + input[input.len() / 8 * 3 + i], + input[input.len() / 8 * 4 + i], + input[input.len() / 8 * 5 + i], + input[input.len() / 8 * 6 + i], + input[input.len() / 8 * 7 + i], + ])); + } +} + fn fix_endianness_and_predict( mut image: DecodingBuffer, samples: usize, @@ -389,6 +419,14 @@ fn fix_endianness_and_predict( DecodingBuffer::F32(_) | DecodingBuffer::F64(_) => unreachable!(), } } + Predictor::FloatingPoint => { + let mut buffer_copy = image.as_bytes_mut().to_vec(); + match image { + DecodingBuffer::F32(buf) => fp_predict_f32(&mut buffer_copy, buf, samples), + DecodingBuffer::F64(buf) => fp_predict_f64(&mut buffer_copy, buf, samples), + _ => unreachable!(), + } + } Predictor::__NonExhaustive => unreachable!(), } } diff --git a/src/error.rs b/src/error.rs index 45ea5b0..0f91ad8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -134,6 +134,7 @@ impl fmt::Display for TiffFormatError { /// stability guarantees. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum TiffUnsupportedError { + FloatingPointPredictor(ColorType), HorizontalPredictor(ColorType), InterpretationWithBits(PhotometricInterpretation, Vec), UnknownInterpretation, @@ -154,6 +155,11 @@ impl fmt::Display for TiffUnsupportedError { fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { use self::TiffUnsupportedError::*; match *self { + FloatingPointPredictor(color_type) => write!( + fmt, + "Floating point predictor for {:?} is unsupported.", + color_type + ), HorizontalPredictor(color_type) => write!( fmt, "Horizontal predictor for {:?} is unsupported.", diff --git a/src/tags.rs b/src/tags.rs index f0a2c07..04cb5ec 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -214,6 +214,7 @@ tags! { pub enum Predictor(u16) { None = 1, Horizontal = 2, + FloatingPoint = 3, } } diff --git a/tests/decode_images.rs b/tests/decode_images.rs index 5d947dc..df43524 100644 --- a/tests/decode_images.rs +++ b/tests/decode_images.rs @@ -494,3 +494,13 @@ fn timeout() { fn test_no_rows_per_strip() { test_image_sum_u8("no_rows_per_strip.tiff", ColorType::RGB(8), 99448840); } + +#[test] +fn test_predictor_3_rgb_f32() { + test_image_sum_f32("predictor-3-rgb-f32.tif", ColorType::RGB(32), 54004.33); +} + +#[test] +fn test_predictor_3_gray_f32() { + test_image_sum_f32("predictor-3-gray-f32.tif", ColorType::Gray(32), 20008.275); +} diff --git a/tests/images/predictor-3-gray-f32.tif b/tests/images/predictor-3-gray-f32.tif new file mode 100644 index 0000000..9fbda84 Binary files /dev/null and b/tests/images/predictor-3-gray-f32.tif differ diff --git a/tests/images/predictor-3-rgb-f32.tif b/tests/images/predictor-3-rgb-f32.tif new file mode 100644 index 0000000..d338f59 Binary files /dev/null and b/tests/images/predictor-3-rgb-f32.tif differ