From 23751bd8c5729d7af551959ea05d8b799559371b Mon Sep 17 00:00:00 2001 From: Etienne Prothon Date: Thu, 23 Jul 2020 01:33:41 +0200 Subject: [PATCH 1/4] feat: Support decoding big tiff --- src/decoder/ifd.rs | 267 ++++++++++++++++++++++++++--------------- src/decoder/mod.rs | 119 ++++++++++++------ tests/encode_images.rs | 40 +++--- 3 files changed, 270 insertions(+), 156 deletions(-) diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index 58965d1..d5b9e0e 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -15,18 +15,18 @@ use self::Value::{Ascii, List, Rational, Unsigned, Signed, SRational}; #[allow(unused_qualifications)] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum Value { - Signed(i32), - Unsigned(u32), + Signed(i64), + Unsigned(u64), List(Vec), - Rational(u32, u32), - SRational(i32, i32), + Rational(u64, u64), + SRational(i64, i64), Ascii(String), #[doc(hidden)] // Do not match against this. __NonExhaustive, } impl Value { - pub fn into_u32(self) -> TiffResult { + pub fn into_u64(self) -> TiffResult { match self { Unsigned(val) => Ok(val), val => Err(TiffError::FormatError( @@ -35,7 +35,7 @@ impl Value { } } - pub fn into_i32(self) -> TiffResult { + pub fn into_i64(self) -> TiffResult { match self { Signed(val) => Ok(val), val => Err(TiffError::FormatError( @@ -44,25 +44,25 @@ impl Value { } } - pub fn into_u32_vec(self) -> TiffResult> { + pub fn into_u64_vec(self) -> TiffResult> { match self { List(vec) => { let mut new_vec = Vec::with_capacity(vec.len()); for v in vec { - new_vec.push(v.into_u32()?) + new_vec.push(v.into_u64()?) } Ok(new_vec) } Unsigned(val) => Ok(vec![val]), Rational(numerator, denominator) => Ok(vec![numerator, denominator]), - Ascii(val) => Ok(val.chars().map(u32::from).collect()), + Ascii(val) => Ok(val.chars().map(u32::from).map(u64::from).collect()), val => Err(TiffError::FormatError( TiffFormatError::UnsignedIntegerExpected(val), )), } } - pub fn into_i32_vec(self) -> TiffResult> { + pub fn into_i64_vec(self) -> TiffResult> { match self { List(vec) => { let mut new_vec = Vec::with_capacity(vec.len()); @@ -72,7 +72,7 @@ impl Value { new_vec.push(numerator); new_vec.push(denominator); } - _ => new_vec.push(v.into_i32()?) + _ => new_vec.push(v.into_i64()?), } } Ok(new_vec) @@ -85,12 +85,12 @@ impl Value { } } } - + #[derive(Clone)] pub struct Entry { type_: Type, - count: u32, - offset: [u8; 4], + count: u64, + offset: [u8; 8], } impl ::std::fmt::Debug for Entry { @@ -103,7 +103,7 @@ impl ::std::fmt::Debug for Entry { } impl Entry { - pub fn new(type_: Type, count: u32, offset: [u8; 4]) -> Entry { + pub fn new(type_: Type, count: u64, offset: [u8; 8]) -> Entry { Entry { type_, count, @@ -122,94 +122,156 @@ impl Entry { decoder: &mut super::Decoder, ) -> TiffResult { let bo = decoder.byte_order(); - match (self.type_, self.count) { - // TODO check if this could give wrong results - // at a different endianess of file/computer. - (Type::BYTE, 1) => Ok(Unsigned(u32::from(self.offset[0]))), - (Type::BYTE, 2) => offset_to_bytes(2, self), - (Type::BYTE, 3) => offset_to_bytes(3, self), - (Type::BYTE, 4) => offset_to_bytes(4, self), - (Type::BYTE, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Unsigned(u32::from(decoder.read_byte()?))) - }), - (Type::SBYTE, 1) => Ok(Signed(i32::from(self.offset[0] as i8))), - (Type::SBYTE, 2) => offset_to_sbytes(2, self), - (Type::SBYTE, 3) => offset_to_sbytes(3, self), - (Type::SBYTE, 4) => offset_to_sbytes(4, self), - (Type::SBYTE, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Signed(i32::from(decoder.read_byte()? as i8))) - }), - (Type::SHORT, 1) => Ok(Unsigned(u32::from(self.r(bo).read_u16()?))), - (Type::SSHORT, 1) => Ok(Signed(i32::from(self.r(bo).read_i16()?))), - (Type::SHORT, 2) => { - let mut r = self.r(bo); - Ok(List(vec![ - Unsigned(u32::from(r.read_u16()?)), - Unsigned(u32::from(r.read_u16()?)), - ])) - } - (Type::SSHORT, 2) => { - let mut r = self.r(bo); - Ok(List(vec![ - Signed(i32::from(r.read_i16()?)), - Signed(i32::from(r.read_i16()?)), - ])) - } - (Type::SHORT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Unsigned(u32::from(decoder.read_short()?))) - }), - (Type::SSHORT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Signed(i32::from(decoder.read_sshort()?))) - }), - (Type::LONG, 1) => Ok(Unsigned(self.r(bo).read_u32()?)), - (Type::SLONG, 1) => Ok(Signed(self.r(bo).read_i32()?)), - (Type::LONG, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Unsigned(decoder.read_long()?)) - }), - (Type::SLONG, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Signed(decoder.read_slong()?)) - }), - (Type::RATIONAL, 1) => { - decoder.goto_offset(self.r(bo).read_u32()?)?; - let numerator = decoder.read_long()?; - let denominator = decoder.read_long()?; - Ok(Rational(numerator, denominator)) - } - (Type::SRATIONAL, 1) => { - decoder.goto_offset(self.r(bo).read_u32()?)?; - let numerator = decoder.read_slong()?; - let denominator = decoder.read_slong()?; - Ok(SRational(numerator, denominator)) - } - (Type::RATIONAL, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Rational( - decoder.read_long()?, - decoder.read_long()?, - )) - }), - (Type::SRATIONAL, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(SRational( - decoder.read_slong()?, - decoder.read_slong()?, - )) - }), - (Type::ASCII, n) => { - let n = usize::try_from(n)?; - if n > limits.decoding_buffer_size { - return Err(TiffError::LimitsExceeded); + let bigtiff_value = if decoder.bigtiff && 4 < self.count && self.count <= 8 { + match (self.type_, self.count) { + (Type::BYTE, n) => Some(offset_to_bytes(n as usize, self)?), + (Type::SBYTE, n) => Some(offset_to_sbytes(n as usize, self)?), + (Type::SHORT, 3) => { + let mut r = self.r(bo); + Some(List(vec![ + Unsigned(u64::from(r.read_u16()?)), + Unsigned(u64::from(r.read_u16()?)), + Unsigned(u64::from(r.read_u16()?)), + ])) } - decoder.goto_offset(self.r(bo).read_u32()?)?; - let string = decoder.read_string(n)?; - Ok(Ascii(string)) + (Type::SSHORT, 3) => { + let mut r = self.r(bo); + Some(List(vec![ + Signed(i64::from(r.read_i16()?)), + Signed(i64::from(r.read_i16()?)), + Signed(i64::from(r.read_i16()?)), + ])) + } + (Type::SHORT, 4) => { + let mut r = self.r(bo); + Some(List(vec![ + Unsigned(u64::from(r.read_u16()?)), + Unsigned(u64::from(r.read_u16()?)), + Unsigned(u64::from(r.read_u16()?)), + Unsigned(u64::from(r.read_u16()?)), + ])) + } + (Type::SSHORT, 4) => { + let mut r = self.r(bo); + Some(List(vec![ + Signed(i64::from(r.read_i16()?)), + Signed(i64::from(r.read_i16()?)), + Signed(i64::from(r.read_i16()?)), + Signed(i64::from(r.read_i16()?)), + ])) + } + (Type::LONG, 2) => { + let mut r = self.r(bo); + Some(List(vec![ + Unsigned(r.read_u32()?.into()), + Unsigned(r.read_u32()?.into()), + ])) + } + (Type::SLONG, 2) => { + let mut r = self.r(bo); + Some(List(vec![ + Signed(r.read_i32()?.into()), + Signed(r.read_i32()?.into()), + ])) + } + (Type::RATIONAL, 1) => { + let mut r = self.r(bo); + let numerator = r.read_u32()?.into(); + let denominator = r.read_u32()?.into(); + Some(Rational(numerator, denominator)) + } + (Type::SRATIONAL, 1) => { + let mut r = self.r(bo); + let numerator = r.read_i32()?.into(); + let denominator = r.read_i32()?.into(); + Some(SRational(numerator, denominator)) + } + _ => None + } + } else { + None + }; + + if let Some(v) = bigtiff_value { + Ok(v) + } else { + match (self.type_, self.count) { + // TODO check if this could give wrong results + // at a different endianess of file/computer. + (Type::BYTE, 1) => Ok(Unsigned(u64::from(self.offset[0]))), + (Type::BYTE, 2) => offset_to_bytes(2, self), + (Type::BYTE, 3) => offset_to_bytes(3, self), + (Type::BYTE, 4) => offset_to_bytes(4, self), + (Type::BYTE, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { + Ok(Unsigned(u64::from(decoder.read_byte()?))) + }), + (Type::SBYTE, 1) => Ok(Signed(i64::from(self.offset[0] as i8))), + (Type::SBYTE, 2) => offset_to_sbytes(2, self), + (Type::SBYTE, 3) => offset_to_sbytes(3, self), + (Type::SBYTE, 4) => offset_to_sbytes(4, self), + (Type::SBYTE, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { + Ok(Signed(i64::from(decoder.read_byte()? as i8))) + }), + (Type::SHORT, 1) => Ok(Unsigned(u64::from(self.r(bo).read_u16()?))), + (Type::SSHORT, 1) => Ok(Signed(i64::from(self.r(bo).read_i16()?))), + (Type::SHORT, 2) => { + let mut r = self.r(bo); + Ok(List(vec![ + Unsigned(u64::from(r.read_u16()?)), + Unsigned(u64::from(r.read_u16()?)), + ])) + } + (Type::SSHORT, 2) => { + let mut r = self.r(bo); + Ok(List(vec![ + Signed(i64::from(r.read_i16()?)), + Signed(i64::from(r.read_i16()?)), + ])) + } + (Type::SHORT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { + Ok(Unsigned(u64::from(decoder.read_short()?))) + }), + (Type::SSHORT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { + Ok(Signed(i64::from(decoder.read_sshort()?))) + }), + (Type::LONG, 1) => Ok(Unsigned(self.r(bo).read_u32()?.into())), + (Type::SLONG, 1) => Ok(Signed(self.r(bo).read_i32()?.into())), + (Type::LONG, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { + Ok(Unsigned(decoder.read_long()?.into())) + }), + (Type::SLONG, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { + Ok(Signed(decoder.read_slong()?.into())) + }), + (Type::RATIONAL, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { + Ok(Rational(decoder.read_long()?.into(), decoder.read_long()?.into())) + }), + (Type::SRATIONAL, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { + Ok(SRational(decoder.read_slong()?.into(), decoder.read_slong()?.into())) + }), + (Type::ASCII, n) => { + println!("ASCIII COUNT {}", n); + let n = usize::try_from(n)?; + if n > limits.decoding_buffer_size { + return Err(TiffError::LimitsExceeded); + } + let offset = if decoder.bigtiff { + self.r(bo).read_u64()? + } else { + self.r(bo).read_u32()?.into() + }; + decoder.goto_offset(offset)?; + let string = decoder.read_string(n)?; + Ok(Ascii(string)) + } + _ => Err(TiffError::UnsupportedError( + TiffUnsupportedError::UnsupportedDataType, + )), } - _ => Err(TiffError::UnsupportedError( - TiffUnsupportedError::UnsupportedDataType, - )), } } #[inline] - fn decode_offset(&self, value_count: u32, bo: ByteOrder, limits: &super::Limits, decoder: &mut super::Decoder, decode_fn: F) -> TiffResult + fn decode_offset(&self, value_count: u64, bo: ByteOrder, limits: &super::Limits, decoder: &mut super::Decoder, decode_fn: F) -> TiffResult where R: Read + Seek, F: Fn(&mut super::Decoder) -> TiffResult, @@ -220,7 +282,12 @@ impl Entry { } let mut v = Vec::with_capacity(value_count); - decoder.goto_offset(self.r(bo).read_u32()?)?; + let offset = if decoder.bigtiff { + self.r(bo).read_u64()? + } else { + self.r(bo).read_u32()?.into() + }; + decoder.goto_offset(offset)?; for _ in 0..value_count { v.push(decode_fn(decoder)?) } @@ -234,7 +301,7 @@ fn offset_to_bytes(n: usize, entry: &Entry) -> TiffResult { Ok(List( entry.offset[0..n] .iter() - .map(|&e| Unsigned(u32::from(e))) + .map(|&e| Unsigned(u64::from(e))) .collect() )) } @@ -245,7 +312,7 @@ fn offset_to_sbytes(n: usize, entry: &Entry) -> TiffResult { Ok(List( entry.offset[0..n] .iter() - .map(|&e| Signed(i32::from(e as i8))) + .map(|&e| Signed(i64::from(e as i8))) .collect() )) } diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index a2cd6a0..c34d228 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -1,4 +1,4 @@ -use std::convert::TryFrom; +use std::convert::{TryInto, TryFrom}; use std::collections::HashMap; use std::io::{self, Read, Seek}; use std::cmp; @@ -116,8 +116,8 @@ impl<'a> DecodingBuffer<'a> { #[derive(Debug)] struct StripDecodeState { strip_index: usize, - strip_offsets: Vec, - strip_bytes: Vec, + strip_offsets: Vec, + strip_bytes: Vec, } /// Decoding limits @@ -157,8 +157,9 @@ where { reader: SmartReader, byte_order: ByteOrder, + bigtiff: bool, limits: Limits, - next_ifd: Option, + next_ifd: Option, ifd: Option, width: u32, height: u32, @@ -248,6 +249,7 @@ impl Decoder { Decoder { reader: SmartReader::wrap(r, ByteOrder::LittleEndian), byte_order: ByteOrder::LittleEndian, + bigtiff: false, limits: Default::default(), next_ifd: None, ifd: None, @@ -330,12 +332,28 @@ impl Decoder { )) } } - if self.read_short()? != 42 { - return Err(TiffError::FormatError( + match self.read_short()? { + 42 => self.bigtiff = false, + 43 => { + self.bigtiff = true; + // Read bytesize of offsets (in bigtiff it's alway 8 but provide a way to move to 16 some day) + if self.read_short()? != 8 { + return Err(TiffError::FormatError( + TiffFormatError::TiffSignatureNotFound, + )) + } + // This constant should always be 0 + if self.read_short()? != 0 { + return Err(TiffError::FormatError( + TiffFormatError::TiffSignatureNotFound, + )) + } + }, + _ => return Err(TiffError::FormatError( TiffFormatError::TiffSignatureInvalid, - )); + )) } - self.next_ifd = match self.read_long()? { + self.next_ifd = match self.read_ifd_offset()? { 0 => None, n => Some(n), }; @@ -354,8 +372,8 @@ impl Decoder { /// To determine whether there are more images call `TIFFDecoder::more_images` instead. pub fn next_image(&mut self) -> TiffResult<()> { self.ifd = Some(self.read_ifd()?); - self.width = self.get_tag_u32(Tag::ImageWidth)?; - self.height = self.get_tag_u32(Tag::ImageLength)?; + self.width = u32::try_from(self.get_tag_u64(Tag::ImageWidth)?)?; + self.height = u32::try_from(self.get_tag_u64(Tag::ImageLength)?)?; self.strip_decoder = None; self.photometric_interpretation = self @@ -389,6 +407,15 @@ impl Decoder { self.byte_order } + #[inline] + pub fn read_ifd_offset(&mut self) -> Result { + if self.bigtiff { + self.read_long8() + } else { + self.read_long().map(u64::from) + } + } + /// Reads a TIFF byte value #[inline] pub fn read_byte(&mut self) -> Result { @@ -421,6 +448,12 @@ impl Decoder { self.reader.read_i32() } + #[inline] + pub fn read_long8(&mut self) -> Result { + self.reader.read_u64() + } + + /// Reads a string #[inline] pub fn read_string(&mut self, length: usize) -> TiffResult { @@ -433,17 +466,21 @@ impl Decoder { /// Reads a TIFF IFA offset/value field #[inline] - pub fn read_offset(&mut self) -> Result<[u8; 4], io::Error> { - let mut val = [0; 4]; + pub fn read_offset(&mut self) -> Result<[u8; 8], io::Error> { + let bytes_to_read = if self.bigtiff { 8 } else { 4 }; + let mut val = vec![0; bytes_to_read]; self.reader.read_exact(&mut val)?; - Ok(val) + if ! self.bigtiff { + val.append(&mut vec![0; 4]) + } + Ok(val[..].try_into().unwrap()) } /// Moves the cursor to the specified offset #[inline] - pub fn goto_offset(&mut self, offset: u32) -> io::Result<()> { + pub fn goto_offset(&mut self, offset: u64) -> io::Result<()> { self.reader - .seek(io::SeekFrom::Start(u64::from(offset))) + .seek(io::SeekFrom::Start(offset)) .map(|_| ()) } @@ -465,12 +502,17 @@ impl Decoder { return Ok(None); } }; + let count= if self.bigtiff { + self.read_long8()? + } else { + self.read_long()?.into() + }; Ok(Some(( tag, ifd::Entry::new( type_, - self.read_long()?, // count - self.read_offset()?, // offset + count, + self.read_offset()? ), ))) } @@ -493,7 +535,7 @@ impl Decoder { }; dir.insert(tag, entry); } - self.next_ifd = match self.read_long()? { + self.next_ifd = match self.read_ifd_offset()? { 0 => None, n => Some(n), }; @@ -503,6 +545,7 @@ impl Decoder { /// Tries to retrieve a tag. /// Return `Ok(None)` if the tag is not present. pub fn find_tag(&mut self, tag: Tag) -> TiffResult> { + println!("IFD {:?}", self.ifd.as_ref().unwrap().get(&tag)); let entry = match self.ifd.as_ref().unwrap().get(&tag) { None => return Ok(None), Some(entry) => entry.clone(), @@ -514,9 +557,9 @@ impl Decoder { } /// Tries to retrieve a tag and convert it to the desired unsigned type. - pub fn find_tag_unsigned>(&mut self, tag: Tag) -> TiffResult> { + pub fn find_tag_unsigned>(&mut self, tag: Tag) -> TiffResult> { self.find_tag(tag)? - .map(|v| v.into_u32()).transpose()? + .map(|v| v.into_u64()).transpose()? .map(|value| T::try_from(value) .map_err(|_| TiffFormatError::InvalidTagValueType(tag).into())) .transpose() @@ -524,9 +567,9 @@ impl Decoder { /// Tries to retrieve a vector of all a tag's values and convert them to /// the desired unsigned type. - pub fn find_tag_unsigned_vec>(&mut self, tag: Tag) -> TiffResult>> { + pub fn find_tag_unsigned_vec>(&mut self, tag: Tag) -> TiffResult>> { self.find_tag(tag)? - .map(|v| v.into_u32_vec()).transpose()? + .map(|v| v.into_u64_vec()).transpose()? .map(|v| v.into_iter() .map(|u| T::try_from(u).map_err(|_| TiffFormatError::InvalidTagValueType(tag).into())) .collect()) @@ -535,7 +578,7 @@ impl Decoder { /// Tries to retrieve a tag and convert it to the desired unsigned type. /// Returns an error if the tag is not present. - pub fn get_tag_unsigned>(&mut self, tag: Tag) -> TiffResult { + pub fn get_tag_unsigned>(&mut self, tag: Tag) -> TiffResult { self.find_tag_unsigned(tag)? .ok_or_else(|| TiffFormatError::RequiredTagNotFound(tag).into()) } @@ -552,13 +595,13 @@ impl Decoder { } /// Tries to retrieve a tag and convert it to the desired type. - pub fn get_tag_u32(&mut self, tag: Tag) -> TiffResult { - self.get_tag(tag)?.into_u32() + pub fn get_tag_u64(&mut self, tag: Tag) -> TiffResult { + self.get_tag(tag)?.into_u64() } /// Tries to retrieve a tag and convert it to the desired type. - pub fn get_tag_u32_vec(&mut self, tag: Tag) -> TiffResult> { - self.get_tag(tag)?.into_u32_vec() + pub fn get_tag_u64_vec(&mut self, tag: Tag) -> TiffResult> { + self.get_tag(tag)?.into_u64_vec() } /// Decompresses the strip into the supplied buffer. @@ -566,13 +609,13 @@ impl Decoder { fn expand_strip<'a>( &mut self, buffer: DecodingBuffer<'a>, - offset: u32, - length: u32, + offset: u64, + length: u64, max_uncompressed_length: usize, ) -> TiffResult { let color_type = self.colortype()?; self.goto_offset(offset)?; - let (bytes, mut reader): (usize, Box) = match self.compression_method { + let (bytes, mut reader): (usize, Box) = match self.compression_method { CompressionMethod::None => { let order = self.reader.byte_order; ( @@ -686,7 +729,8 @@ impl Decoder { /// Number of strips in image pub fn strip_count(&mut self) -> TiffResult { let rows_per_strip = self - .get_tag_u32(Tag::RowsPerStrip) + .get_tag_u64(Tag::RowsPerStrip) + .map(u32::try_from)? .unwrap_or(self.height); if rows_per_strip == 0 { @@ -698,8 +742,8 @@ impl Decoder { fn initialize_strip_decoder(&mut self) -> TiffResult<()> { if self.strip_decoder.is_none() { - let strip_offsets = self.get_tag_u32_vec(Tag::StripOffsets)?; - let strip_bytes = self.get_tag_u32_vec(Tag::StripByteCounts)?; + let strip_offsets = self.get_tag_u64_vec(Tag::StripOffsets)?; + let strip_bytes = self.get_tag_u64_vec(Tag::StripByteCounts)?; self.strip_decoder = Some(StripDecodeState { strip_index: 0, @@ -734,7 +778,8 @@ impl Decoder { ))?; let rows_per_strip = usize::try_from(self - .get_tag_u32(Tag::RowsPerStrip) + .get_tag_u64(Tag::RowsPerStrip) + .map(u32::try_from)? .unwrap_or(self.height))?; let strip_height = cmp::min( @@ -806,7 +851,8 @@ impl Decoder { let index = self.strip_decoder.as_ref().unwrap().strip_index; let rows_per_strip = usize::try_from(self - .get_tag_u32(Tag::RowsPerStrip) + .get_tag_u64(Tag::RowsPerStrip) + .map(u32::try_from)? .unwrap_or(self.height))?; let strip_height = cmp::min( @@ -825,7 +871,8 @@ impl Decoder { pub fn read_image(&mut self) -> TiffResult { self.initialize_strip_decoder()?; let rows_per_strip = usize::try_from(self - .get_tag_u32(Tag::RowsPerStrip) + .get_tag_u64(Tag::RowsPerStrip) + .map(u32::try_from)? .unwrap_or(self.height))?; let samples_per_strip = diff --git a/tests/encode_images.rs b/tests/encode_images.rs index 9d1ecfd..c2bd88d 100644 --- a/tests/encode_images.rs +++ b/tests/encode_images.rs @@ -165,11 +165,11 @@ fn test_multiple_byte() { { let mut decoder = Decoder::new(&mut data).unwrap(); - assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_u32_vec().unwrap(), [1]); - assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_u32_vec().unwrap(), [1, 2]); - assert_eq!(decoder.get_tag(Tag::Unknown(65002)).unwrap().into_u32_vec().unwrap(), [1, 2, 3]); - assert_eq!(decoder.get_tag(Tag::Unknown(65003)).unwrap().into_u32_vec().unwrap(), [1, 2, 3, 4]); - assert_eq!(decoder.get_tag(Tag::Unknown(65004)).unwrap().into_u32_vec().unwrap(), [1, 2, 3, 4, 5]); + assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_u64_vec().unwrap(), [1]); + assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_u64_vec().unwrap(), [1, 2]); + assert_eq!(decoder.get_tag(Tag::Unknown(65002)).unwrap().into_u64_vec().unwrap(), [1, 2, 3]); + assert_eq!(decoder.get_tag(Tag::Unknown(65003)).unwrap().into_u64_vec().unwrap(), [1, 2, 3, 4]); + assert_eq!(decoder.get_tag(Tag::Unknown(65004)).unwrap().into_u64_vec().unwrap(), [1, 2, 3, 4, 5]); } } @@ -209,24 +209,24 @@ fn test_signed() { { let mut decoder = Decoder::new(&mut data).unwrap(); - assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_i32().unwrap(), -1, ); - assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_i32_vec().unwrap(), [-1]); - assert_eq!(decoder.get_tag(Tag::Unknown(65002)).unwrap().into_i32_vec().unwrap(), [-1, 2]); - assert_eq!(decoder.get_tag(Tag::Unknown(65003)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3]); - assert_eq!(decoder.get_tag(Tag::Unknown(65004)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3, 4]); - assert_eq!(decoder.get_tag(Tag::Unknown(65005)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3, 4, -5], ); + assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_i64().unwrap(), -1, ); + assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_i64_vec().unwrap(), [-1]); + assert_eq!(decoder.get_tag(Tag::Unknown(65002)).unwrap().into_i64_vec().unwrap(), [-1, 2]); + assert_eq!(decoder.get_tag(Tag::Unknown(65003)).unwrap().into_i64_vec().unwrap(), [-1, 2, -3]); + assert_eq!(decoder.get_tag(Tag::Unknown(65004)).unwrap().into_i64_vec().unwrap(), [-1, 2, -3, 4]); + assert_eq!(decoder.get_tag(Tag::Unknown(65005)).unwrap().into_i64_vec().unwrap(), [-1, 2, -3, 4, -5], ); - assert_eq!(decoder.get_tag(Tag::Unknown(65010)).unwrap().into_i32().unwrap(), -1); - assert_eq!(decoder.get_tag(Tag::Unknown(65011)).unwrap().into_i32_vec().unwrap(), [-1]); - assert_eq!(decoder.get_tag(Tag::Unknown(65012)).unwrap().into_i32_vec().unwrap(), [-1, 2]); - assert_eq!(decoder.get_tag(Tag::Unknown(65013)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3]); + assert_eq!(decoder.get_tag(Tag::Unknown(65010)).unwrap().into_i64().unwrap(), -1); + assert_eq!(decoder.get_tag(Tag::Unknown(65011)).unwrap().into_i64_vec().unwrap(), [-1]); + assert_eq!(decoder.get_tag(Tag::Unknown(65012)).unwrap().into_i64_vec().unwrap(), [-1, 2]); + assert_eq!(decoder.get_tag(Tag::Unknown(65013)).unwrap().into_i64_vec().unwrap(), [-1, 2, -3]); - assert_eq!(decoder.get_tag(Tag::Unknown(65020)).unwrap().into_i32().unwrap(), -1); - assert_eq!(decoder.get_tag(Tag::Unknown(65021)).unwrap().into_i32_vec().unwrap(), [-1]); - assert_eq!(decoder.get_tag(Tag::Unknown(65022)).unwrap().into_i32_vec().unwrap(), [-1, 2]); + assert_eq!(decoder.get_tag(Tag::Unknown(65020)).unwrap().into_i64().unwrap(), -1); + assert_eq!(decoder.get_tag(Tag::Unknown(65021)).unwrap().into_i64_vec().unwrap(), [-1]); + assert_eq!(decoder.get_tag(Tag::Unknown(65022)).unwrap().into_i64_vec().unwrap(), [-1, 2]); - assert_eq!(decoder.get_tag(Tag::Unknown(65030)).unwrap().into_i32_vec().unwrap(), [-1, 100]); - assert_eq!(decoder.get_tag(Tag::Unknown(65031)).unwrap().into_i32_vec().unwrap(), [-1_i32, 100, 2, 100]); + assert_eq!(decoder.get_tag(Tag::Unknown(65030)).unwrap().into_i64_vec().unwrap(), [-1, 100]); + assert_eq!(decoder.get_tag(Tag::Unknown(65031)).unwrap().into_i64_vec().unwrap(), [-1_i64, 100, 2, 100]); } } From 797e3dca07b47aefa55f6a3dcda063d15e8fa2e6 Mon Sep 17 00:00:00 2001 From: Etienne Prothon Date: Fri, 24 Jul 2020 00:47:14 +0200 Subject: [PATCH 2/4] fix: Big tiff decoding and add tests --- src/decoder/ifd.rs | 13 +++++-- src/decoder/mod.rs | 4 +- tests/decode_bigtiff_images.rs | 46 +++++++++++++++++++++++ tests/images/bigtiff/BigTIFF.tif | Bin 0 -> 12480 bytes tests/images/bigtiff/BigTIFFLong.tif | Bin 0 -> 12480 bytes tests/images/bigtiff/BigTIFFMotorola.tif | Bin 0 -> 12480 bytes 6 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 tests/decode_bigtiff_images.rs create mode 100644 tests/images/bigtiff/BigTIFF.tif create mode 100644 tests/images/bigtiff/BigTIFFLong.tif create mode 100644 tests/images/bigtiff/BigTIFFMotorola.tif diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index d5b9e0e..64faf99 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -122,10 +122,16 @@ impl Entry { decoder: &mut super::Decoder, ) -> TiffResult { let bo = decoder.byte_order(); - let bigtiff_value = if decoder.bigtiff && 4 < self.count && self.count <= 8 { + let bigtiff_value = if decoder.bigtiff { match (self.type_, self.count) { - (Type::BYTE, n) => Some(offset_to_bytes(n as usize, self)?), - (Type::SBYTE, n) => Some(offset_to_sbytes(n as usize, self)?), + (Type::BYTE, 5) => Some(offset_to_bytes(5, self)?), + (Type::BYTE, 6) => Some(offset_to_bytes(6, self)?), + (Type::BYTE, 7) => Some(offset_to_bytes(7, self)?), + (Type::BYTE, 8) => Some(offset_to_bytes(8, self)?), + (Type::SBYTE, 5) => Some(offset_to_sbytes(5, self)?), + (Type::SBYTE, 6) => Some(offset_to_sbytes(6, self)?), + (Type::SBYTE, 7) => Some(offset_to_sbytes(7, self)?), + (Type::SBYTE, 8) => Some(offset_to_sbytes(8, self)?), (Type::SHORT, 3) => { let mut r = self.r(bo); Some(List(vec![ @@ -249,7 +255,6 @@ impl Entry { Ok(SRational(decoder.read_slong()?.into(), decoder.read_slong()?.into())) }), (Type::ASCII, n) => { - println!("ASCIII COUNT {}", n); let n = usize::try_from(n)?; if n > limits.decoding_buffer_size { return Err(TiffError::LimitsExceeded); diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index c34d228..6160af4 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -528,7 +528,8 @@ impl Decoder { } Some(offset) => self.goto_offset(offset)?, } - for _ in 0..self.read_short()? { + let num_tags = if self.bigtiff { self.read_long8()? } else { self.read_short()?.into() }; + for _ in 0..num_tags { let (tag, entry) = match self.read_entry()? { Some(val) => val, None => continue, // Unknown data type in tag, skip @@ -545,7 +546,6 @@ impl Decoder { /// Tries to retrieve a tag. /// Return `Ok(None)` if the tag is not present. pub fn find_tag(&mut self, tag: Tag) -> TiffResult> { - println!("IFD {:?}", self.ifd.as_ref().unwrap().get(&tag)); let entry = match self.ifd.as_ref().unwrap().get(&tag) { None => return Ok(None), Some(entry) => entry.clone(), diff --git a/tests/decode_bigtiff_images.rs b/tests/decode_bigtiff_images.rs new file mode 100644 index 0000000..9113f42 --- /dev/null +++ b/tests/decode_bigtiff_images.rs @@ -0,0 +1,46 @@ +extern crate tiff; + +use tiff::decoder::Decoder; +use tiff::tags::Tag; +use tiff::ColorType; + +use std::fs::File; +use std::path::PathBuf; + +const TEST_IMAGE_DIR: &str = "./tests/images/bigtiff"; + +#[test] +fn test_big_tiff() { + let filenames = ["BigTIFF.tif", "BigTIFFMotorola.tif", "BigTIFFLong.tif"]; + for filename in filenames.iter() { + let path = PathBuf::from(TEST_IMAGE_DIR).join(filename); + let img_file = File::open(path).expect("Cannot find test image!"); + let mut decoder = Decoder::new(img_file).expect("Cannot create decoder"); + assert_eq!( + decoder.dimensions().expect("Cannot get dimensions"), + (64, 64) + ); + assert_eq!( + decoder.colortype().expect("Cannot get colortype"), + ColorType::RGB(8) + ); + assert_eq!( + decoder + .get_tag_u64(Tag::StripOffsets) + .expect("Cannot get StripOffsets"), + 16 + ); + assert_eq!( + decoder + .get_tag_u64(Tag::RowsPerStrip) + .expect("Cannot get RowsPerStrip"), + 64 + ); + assert_eq!( + decoder + .get_tag_u64(Tag::StripByteCounts) + .expect("Cannot get StripByteCounts"), + 12288 + ) + } +} diff --git a/tests/images/bigtiff/BigTIFF.tif b/tests/images/bigtiff/BigTIFF.tif new file mode 100644 index 0000000000000000000000000000000000000000..8a0e5a2459cdfd006e51efc1ffdee2eba52d6784 GIT binary patch literal 12480 zcmeI3OKux65JX2d5P0bKYc&?hjT2Gd&~pjFXzT;|f2_)It+P+aMbV8qu<$&B(V= zXzO%me84XxvcN+@JnLA^ge*Ev0HpM5e=dZ0MGm*4{Q3+z*TPU!NE$SqwAW*@i3$Lu zc>DGk@z6h)oSl@cgR1CYxD|oL5^hD%Y=y-x>8FDr2hr5YB-oNJRI1u>BaCIAFhOD& zY_c#2n{6jCmeV*WNGyX*76xIn?Igx>8V3c5Ww6P@AZ)gs#8^(_pdhgfHdz>i&9;*m z%V``GB$mM@3xlxPb`oPbje~;3GT3Bc5O!^Q?-Sg&ekMAC*W`m-P7B6=KK}<#;Q9Rh zC$Rj+o-FjI??)#wmeckL{SM1ulZF2D{pcjda@szj-(eYSve2KtADzTlPTME+J1m1u z7W&ioqYVbXL@Hk#<-bkoi=zjBq@098c}NHn$DOD%VD9<&b?EVIjO!~q!L?U z3g^hNzLpdujLr$w!PZ`Fn)r6zgYUs~XwTX9sZZPHOAfb2R9XSZIn}||UOWB)bM3%E zxe0d-zYRfK2Q?$V1ITH0I=1$TGjJb1r$nFF8oV^-OMk?Nw>Xmb&Bx29Xrd zox`m%+n$wQ(zq;}X!sBe(d>LA=+(}O&^jmT9>HvTMsaIjJ1au#oTz&Qv+Wtht$ppR z2(5FX?h(wkXB4;gwX-6$&WXB5Fx#F{+}hX9iqJYI>K?&tdq#0zt^21hefK z#f`q{w*YxvUXM$WALZXPm&SwnBHnAsdy`)hNd4`H$$KfaKaKY;`B!=UGTv+XABf+^ T`zSvZ_+z|7ey5OXkFx#)S4oj9 literal 0 HcmV?d00001 diff --git a/tests/images/bigtiff/BigTIFFLong.tif b/tests/images/bigtiff/BigTIFFLong.tif new file mode 100644 index 0000000000000000000000000000000000000000..1a321ef1b92739fcfeb5c385146606c04e9cda19 GIT binary patch literal 12480 zcmeI3O>P@8428#WQDm7e+8&~t9wFP@$~Si?J@JW>En1Pnodsj&i9e5@1_t7~@W;o$ zxBqgHbq#(Umm~D`!W@32qkJNVQ0vv;On+con6Nje5)B%~=L-y-hJ z?O?j?UCwb0AX8pH$XU(cviJhO494OSxD~-*D+DYh6&?Z+dqtT<5M-H+K-);_YR9E* zw@@HKRzfp^dS+VzFz|HO(wC2x%=C=VGm&K8jw}2KCJQYPZI{Gl*(`M{(ukG~Hlw)B zLfc4p#s~aDA`3he#Iq5rS&&7?34m07>Cc4}uc+a6R9~MV=UNzQ3MqqTB<=N>G*JP7 zlyBc2BObV_E$Bi(Sxw4KCQF5{peu?#lZ7=&GI?|p*%*3U#o@S1#(%Voj%&*%T(2|S;_ z{{)ub*prR^^!@B4#&X#{q3^H^HreP;-_K5BESK#Q`VPxrla2oL{p=*ha@jti@30Iu z+2~K-&o&tR5~+M~l>auRFODAkk#Y(O;f-^OXXmLSj4PYb9V)h6@QY`u{1y~ExZ3zg zk1J>S?J1~5V2uwo#jiF3Q|r|^MMNV(QR><^27M@UYdT|YEQf`@jodp$nUnh4MJfp^ zOyL|wY+PFk5=Q5O8o}0HZJEUFx(DBb>Cm3D^{G$W)=LeyMpRk>$Tc;Bt-W^q1Lo?$ zLD_`6hTo>3Z3I1|cn6Tn>U3=F6=&=s2zFMwEzG&)0l(Cs4A)z78&|JNOSa^a7Z^lR zKz9*tmDze$ektR!Y@!i|V2Z&mjsm^fSrJ<2MBO8pt!EUs`r26$TIWRFBbcpc6u0`? zSrJ<2MBO8pt!EUs`r26$TIWRFBbcpc6u0`?SrJ<2MBO8pt!EUs`r26$TIWRFBbcpc z6gPa+ZvpZ;osUzI@8#b#rzVEri^R^Q?p1zCApO^$rtYQm{yP1j-<#B57AoruUHFDWu+`tWQ_DkuCrL literal 0 HcmV?d00001 diff --git a/tests/images/bigtiff/BigTIFFMotorola.tif b/tests/images/bigtiff/BigTIFFMotorola.tif new file mode 100644 index 0000000000000000000000000000000000000000..4699ecd7bc8ba60d79b8c9449b59affe83e94c1f GIT binary patch literal 12480 zcmeI3O;Q^%5QS%;D#a2ONe+<>N8ke7$u}o0J@r#dw%S6XcUGvGr`Lan~ zwoZ4(JN!Z-3p^CWvyRnF$fDx}KuW*#=R$~A@gJ|kx5^PBqDpl>c5yrAlm>{tX zHdz>i&9;*m%V``GB$mM@3xlxPb`oPbje~;3GT3Bc5H{ORVl1a|P>@&#n=A~%X4^@O z9bp8uUE@Ob|I z6Igy>PZs*q_oI^-%W3Qcgl4yy~3%+}NHn$DOD%VD9fom;0Ub5eb~NF}zy z6wZ-jeJv?S7@ZTUgRQ;VH1X}a1>b||(4MpHQ=hiYmmF@5sI&r*bE<=_y>|Qq=GuXS zaueovLcwO6GjTk4V*7(`M) zcMiA8YgepjSI9LhGEUdjzxX8O5!A?W_o`bE57M%(iC~xAwKO zBDBtlx<@eEo>AP|*UpO2Iw$HL!EAd*acf^YD?;m>sCxvn?HR?beeJ9Wt#hL85zMw{ z6nE42 Date: Fri, 24 Jul 2020 01:22:33 +0200 Subject: [PATCH 3/4] feat: better compatibility with old api --- src/decoder/ifd.rs | 87 ++++++++++++++++++++++++++++++++++++------ src/decoder/mod.rs | 74 +++++++++++++++++++++-------------- tests/encode_images.rs | 40 +++++++++---------- 3 files changed, 141 insertions(+), 60 deletions(-) diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index 64faf99..b12d428 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -1,7 +1,7 @@ //! Function for reading TIFF tags use std::collections::HashMap; -use std::convert::TryFrom; +use std::convert::{TryInto, TryFrom}; use std::io::{self, Read, Seek}; use std::mem; @@ -26,6 +26,24 @@ pub enum Value { } impl Value { + pub fn into_u32(self) -> TiffResult { + match self { + Unsigned(val) => Ok(u32::try_from(val)?), + val => Err(TiffError::FormatError( + TiffFormatError::UnsignedIntegerExpected(val), + )), + } + } + + pub fn into_i32(self) -> TiffResult { + match self { + Signed(val) => Ok(i32::try_from(val)?), + val => Err(TiffError::FormatError( + TiffFormatError::SignedIntegerExpected(val), + )), + } + } + pub fn into_u64(self) -> TiffResult { match self { Unsigned(val) => Ok(val), @@ -44,6 +62,47 @@ impl Value { } } + pub fn into_u32_vec(self) -> TiffResult> { + match self { + List(vec) => { + let mut new_vec = Vec::with_capacity(vec.len()); + for v in vec { + new_vec.push(v.into_u32()?) + } + Ok(new_vec) + } + Unsigned(val) => Ok(vec![u32::try_from(val)?]), + Rational(numerator, denominator) => Ok(vec![u32::try_from(numerator)?, u32::try_from(denominator)?]), + Ascii(val) => Ok(val.chars().map(u32::from).collect()), + val => Err(TiffError::FormatError( + TiffFormatError::UnsignedIntegerExpected(val), + )), + } + } + + pub fn into_i32_vec(self) -> TiffResult> { + match self { + List(vec) => { + let mut new_vec = Vec::with_capacity(vec.len()); + for v in vec { + match v { + SRational(numerator, denominator) => { + new_vec.push(i32::try_from(numerator)?); + new_vec.push(i32::try_from(denominator)?); + } + _ => new_vec.push(v.into_i32()?), + } + } + Ok(new_vec) + } + Signed(val) => Ok(vec![i32::try_from(val)?]), + SRational(numerator, denominator) => Ok(vec![i32::try_from(numerator)?, i32::try_from(denominator)?]), + val => Err(TiffError::FormatError( + TiffFormatError::SignedIntegerExpected(val), + )), + } + } + pub fn into_u64_vec(self) -> TiffResult> { match self { List(vec) => { @@ -103,7 +162,13 @@ impl ::std::fmt::Debug for Entry { } impl Entry { - pub fn new(type_: Type, count: u64, offset: [u8; 8]) -> Entry { + pub fn new(type_: Type, count: u32, offset: [u8; 4]) -> Entry { + let mut offset = offset.to_vec(); + offset.append(&mut vec![0; 4]); + Entry::new_u64(type_, count.into(), offset[..].try_into().unwrap()) + } + + pub fn new_u64(type_: Type, count: u64, offset: [u8; 8]) -> Entry { Entry { type_, count, @@ -259,12 +324,11 @@ impl Entry { if n > limits.decoding_buffer_size { return Err(TiffError::LimitsExceeded); } - let offset = if decoder.bigtiff { - self.r(bo).read_u64()? + if decoder.bigtiff { + decoder.goto_offset_u64(self.r(bo).read_u64()?)? } else { - self.r(bo).read_u32()?.into() - }; - decoder.goto_offset(offset)?; + decoder.goto_offset(self.r(bo).read_u32()?)? + } let string = decoder.read_string(n)?; Ok(Ascii(string)) } @@ -287,12 +351,11 @@ impl Entry { } let mut v = Vec::with_capacity(value_count); - let offset = if decoder.bigtiff { - self.r(bo).read_u64()? + if decoder.bigtiff { + decoder.goto_offset_u64(self.r(bo).read_u64()?)? } else { - self.r(bo).read_u32()?.into() - }; - decoder.goto_offset(offset)?; + decoder.goto_offset(self.r(bo).read_u32()?)? + } for _ in 0..value_count { v.push(decode_fn(decoder)?) } diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 6160af4..d179cc5 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -372,8 +372,8 @@ impl Decoder { /// To determine whether there are more images call `TIFFDecoder::more_images` instead. pub fn next_image(&mut self) -> TiffResult<()> { self.ifd = Some(self.read_ifd()?); - self.width = u32::try_from(self.get_tag_u64(Tag::ImageWidth)?)?; - self.height = u32::try_from(self.get_tag_u64(Tag::ImageLength)?)?; + self.width = self.get_tag_u32(Tag::ImageWidth)?; + self.height = self.get_tag_u32(Tag::ImageLength)?; self.strip_decoder = None; self.photometric_interpretation = self @@ -466,19 +466,31 @@ impl Decoder { /// Reads a TIFF IFA offset/value field #[inline] - pub fn read_offset(&mut self) -> Result<[u8; 8], io::Error> { - let bytes_to_read = if self.bigtiff { 8 } else { 4 }; - let mut val = vec![0; bytes_to_read]; - self.reader.read_exact(&mut val)?; - if ! self.bigtiff { - val.append(&mut vec![0; 4]) + pub fn read_offset(&mut self) -> TiffResult<[u8; 4]> { + if self.bigtiff { + return Err(TiffError::FormatError(TiffFormatError::InconsistentSizesEncountered)) } - Ok(val[..].try_into().unwrap()) + let mut val = [0; 4]; + self.reader.read_exact(&mut val)?; + Ok(val) + } + + /// Reads a TIFF IFA offset/value field + #[inline] + pub fn read_offset_u64(&mut self) -> Result<[u8; 8], io::Error> { + let mut val = [0; 8]; + self.reader.read_exact(&mut val)?; + Ok(val) } /// Moves the cursor to the specified offset #[inline] - pub fn goto_offset(&mut self, offset: u64) -> io::Result<()> { + pub fn goto_offset(&mut self, offset: u32) -> io::Result<()> { + self.goto_offset_u64(offset.into()) + } + + #[inline] + pub fn goto_offset_u64(&mut self, offset: u64) -> io::Result<()> { self.reader .seek(io::SeekFrom::Start(offset)) .map(|_| ()) @@ -502,18 +514,22 @@ impl Decoder { return Ok(None); } }; - let count= if self.bigtiff { - self.read_long8()? + let entry = if self.bigtiff { + ifd::Entry::new_u64( + type_, + self.read_long8()?, + self.read_offset_u64()? + ) } else { - self.read_long()?.into() + ifd::Entry::new( + type_, + self.read_long()?, + self.read_offset()? + ) }; Ok(Some(( tag, - ifd::Entry::new( - type_, - count, - self.read_offset()? - ), + entry, ))) } @@ -526,7 +542,7 @@ impl Decoder { TiffFormatError::ImageFileDirectoryNotFound, )) } - Some(offset) => self.goto_offset(offset)?, + Some(offset) => self.goto_offset_u64(offset)?, } let num_tags = if self.bigtiff { self.read_long8()? } else { self.read_short()?.into() }; for _ in 0..num_tags { @@ -595,11 +611,17 @@ impl Decoder { } /// Tries to retrieve a tag and convert it to the desired type. + pub fn get_tag_u32(&mut self, tag: Tag) -> TiffResult { + self.get_tag(tag)?.into_u32() + } pub fn get_tag_u64(&mut self, tag: Tag) -> TiffResult { self.get_tag(tag)?.into_u64() } /// Tries to retrieve a tag and convert it to the desired type. + pub fn get_tag_u32_vec(&mut self, tag: Tag) -> TiffResult> { + self.get_tag(tag)?.into_u32_vec() + } pub fn get_tag_u64_vec(&mut self, tag: Tag) -> TiffResult> { self.get_tag(tag)?.into_u64_vec() } @@ -614,7 +636,7 @@ impl Decoder { max_uncompressed_length: usize, ) -> TiffResult { let color_type = self.colortype()?; - self.goto_offset(offset)?; + self.goto_offset_u64(offset)?; let (bytes, mut reader): (usize, Box) = match self.compression_method { CompressionMethod::None => { let order = self.reader.byte_order; @@ -729,8 +751,7 @@ impl Decoder { /// Number of strips in image pub fn strip_count(&mut self) -> TiffResult { let rows_per_strip = self - .get_tag_u64(Tag::RowsPerStrip) - .map(u32::try_from)? + .get_tag_u32(Tag::RowsPerStrip) .unwrap_or(self.height); if rows_per_strip == 0 { @@ -778,8 +799,7 @@ impl Decoder { ))?; let rows_per_strip = usize::try_from(self - .get_tag_u64(Tag::RowsPerStrip) - .map(u32::try_from)? + .get_tag_u32(Tag::RowsPerStrip) .unwrap_or(self.height))?; let strip_height = cmp::min( @@ -851,8 +871,7 @@ impl Decoder { let index = self.strip_decoder.as_ref().unwrap().strip_index; let rows_per_strip = usize::try_from(self - .get_tag_u64(Tag::RowsPerStrip) - .map(u32::try_from)? + .get_tag_u32(Tag::RowsPerStrip) .unwrap_or(self.height))?; let strip_height = cmp::min( @@ -871,8 +890,7 @@ impl Decoder { pub fn read_image(&mut self) -> TiffResult { self.initialize_strip_decoder()?; let rows_per_strip = usize::try_from(self - .get_tag_u64(Tag::RowsPerStrip) - .map(u32::try_from)? + .get_tag_u32(Tag::RowsPerStrip) .unwrap_or(self.height))?; let samples_per_strip = diff --git a/tests/encode_images.rs b/tests/encode_images.rs index c2bd88d..9d1ecfd 100644 --- a/tests/encode_images.rs +++ b/tests/encode_images.rs @@ -165,11 +165,11 @@ fn test_multiple_byte() { { let mut decoder = Decoder::new(&mut data).unwrap(); - assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_u64_vec().unwrap(), [1]); - assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_u64_vec().unwrap(), [1, 2]); - assert_eq!(decoder.get_tag(Tag::Unknown(65002)).unwrap().into_u64_vec().unwrap(), [1, 2, 3]); - assert_eq!(decoder.get_tag(Tag::Unknown(65003)).unwrap().into_u64_vec().unwrap(), [1, 2, 3, 4]); - assert_eq!(decoder.get_tag(Tag::Unknown(65004)).unwrap().into_u64_vec().unwrap(), [1, 2, 3, 4, 5]); + assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_u32_vec().unwrap(), [1]); + assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_u32_vec().unwrap(), [1, 2]); + assert_eq!(decoder.get_tag(Tag::Unknown(65002)).unwrap().into_u32_vec().unwrap(), [1, 2, 3]); + assert_eq!(decoder.get_tag(Tag::Unknown(65003)).unwrap().into_u32_vec().unwrap(), [1, 2, 3, 4]); + assert_eq!(decoder.get_tag(Tag::Unknown(65004)).unwrap().into_u32_vec().unwrap(), [1, 2, 3, 4, 5]); } } @@ -209,24 +209,24 @@ fn test_signed() { { let mut decoder = Decoder::new(&mut data).unwrap(); - assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_i64().unwrap(), -1, ); - assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_i64_vec().unwrap(), [-1]); - assert_eq!(decoder.get_tag(Tag::Unknown(65002)).unwrap().into_i64_vec().unwrap(), [-1, 2]); - assert_eq!(decoder.get_tag(Tag::Unknown(65003)).unwrap().into_i64_vec().unwrap(), [-1, 2, -3]); - assert_eq!(decoder.get_tag(Tag::Unknown(65004)).unwrap().into_i64_vec().unwrap(), [-1, 2, -3, 4]); - assert_eq!(decoder.get_tag(Tag::Unknown(65005)).unwrap().into_i64_vec().unwrap(), [-1, 2, -3, 4, -5], ); + assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_i32().unwrap(), -1, ); + assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_i32_vec().unwrap(), [-1]); + assert_eq!(decoder.get_tag(Tag::Unknown(65002)).unwrap().into_i32_vec().unwrap(), [-1, 2]); + assert_eq!(decoder.get_tag(Tag::Unknown(65003)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3]); + assert_eq!(decoder.get_tag(Tag::Unknown(65004)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3, 4]); + assert_eq!(decoder.get_tag(Tag::Unknown(65005)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3, 4, -5], ); - assert_eq!(decoder.get_tag(Tag::Unknown(65010)).unwrap().into_i64().unwrap(), -1); - assert_eq!(decoder.get_tag(Tag::Unknown(65011)).unwrap().into_i64_vec().unwrap(), [-1]); - assert_eq!(decoder.get_tag(Tag::Unknown(65012)).unwrap().into_i64_vec().unwrap(), [-1, 2]); - assert_eq!(decoder.get_tag(Tag::Unknown(65013)).unwrap().into_i64_vec().unwrap(), [-1, 2, -3]); + assert_eq!(decoder.get_tag(Tag::Unknown(65010)).unwrap().into_i32().unwrap(), -1); + assert_eq!(decoder.get_tag(Tag::Unknown(65011)).unwrap().into_i32_vec().unwrap(), [-1]); + assert_eq!(decoder.get_tag(Tag::Unknown(65012)).unwrap().into_i32_vec().unwrap(), [-1, 2]); + assert_eq!(decoder.get_tag(Tag::Unknown(65013)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3]); - assert_eq!(decoder.get_tag(Tag::Unknown(65020)).unwrap().into_i64().unwrap(), -1); - assert_eq!(decoder.get_tag(Tag::Unknown(65021)).unwrap().into_i64_vec().unwrap(), [-1]); - assert_eq!(decoder.get_tag(Tag::Unknown(65022)).unwrap().into_i64_vec().unwrap(), [-1, 2]); + assert_eq!(decoder.get_tag(Tag::Unknown(65020)).unwrap().into_i32().unwrap(), -1); + assert_eq!(decoder.get_tag(Tag::Unknown(65021)).unwrap().into_i32_vec().unwrap(), [-1]); + assert_eq!(decoder.get_tag(Tag::Unknown(65022)).unwrap().into_i32_vec().unwrap(), [-1, 2]); - assert_eq!(decoder.get_tag(Tag::Unknown(65030)).unwrap().into_i64_vec().unwrap(), [-1, 100]); - assert_eq!(decoder.get_tag(Tag::Unknown(65031)).unwrap().into_i64_vec().unwrap(), [-1_i64, 100, 2, 100]); + assert_eq!(decoder.get_tag(Tag::Unknown(65030)).unwrap().into_i32_vec().unwrap(), [-1, 100]); + assert_eq!(decoder.get_tag(Tag::Unknown(65031)).unwrap().into_i32_vec().unwrap(), [-1_i32, 100, 2, 100]); } } From ba3f6e604ae036dde108842eeaa4406d22f93853 Mon Sep 17 00:00:00 2001 From: Etienne Prothon Date: Mon, 27 Jul 2020 10:52:43 +0200 Subject: [PATCH 4/4] feat: Add BigTiff enum values --- src/decoder/ifd.rs | 142 ++++++++++++++++++++++++++------------------- src/decoder/mod.rs | 2 +- 2 files changed, 84 insertions(+), 60 deletions(-) diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index b12d428..a77dcdc 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -9,17 +9,21 @@ use super::stream::{ByteOrder, EndianReader, SmartReader}; use tags::{Tag, Type}; use {TiffError, TiffFormatError, TiffResult, TiffUnsupportedError}; -use self::Value::{Ascii, List, Rational, Unsigned, Signed, SRational}; +use self::Value::{Ascii, List, Rational, Unsigned, Signed, SRational, RationalBig, UnsignedBig, SignedBig, SRationalBig}; #[allow(unused_qualifications)] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum Value { - Signed(i64), - Unsigned(u64), + Signed(i32), + SignedBig(i64), + Unsigned(u32), + UnsignedBig(u64), List(Vec), - Rational(u64, u64), - SRational(i64, i64), + Rational(u32, u32), + RationalBig(u64, u64), + SRational(i32, i32), + SRationalBig(i64, i64), Ascii(String), #[doc(hidden)] // Do not match against this. __NonExhaustive, @@ -28,7 +32,8 @@ pub enum Value { impl Value { pub fn into_u32(self) -> TiffResult { match self { - Unsigned(val) => Ok(u32::try_from(val)?), + Unsigned(val) => Ok(val), + UnsignedBig(val) => Ok(u32::try_from(val)?), val => Err(TiffError::FormatError( TiffFormatError::UnsignedIntegerExpected(val), )), @@ -37,7 +42,8 @@ impl Value { pub fn into_i32(self) -> TiffResult { match self { - Signed(val) => Ok(i32::try_from(val)?), + Signed(val) => Ok(val), + SignedBig(val) => Ok(i32::try_from(val)?), val => Err(TiffError::FormatError( TiffFormatError::SignedIntegerExpected(val), )), @@ -46,7 +52,8 @@ impl Value { pub fn into_u64(self) -> TiffResult { match self { - Unsigned(val) => Ok(val), + Unsigned(val) => Ok(val.into()), + UnsignedBig(val) => Ok(val), val => Err(TiffError::FormatError( TiffFormatError::UnsignedIntegerExpected(val), )), @@ -55,7 +62,8 @@ impl Value { pub fn into_i64(self) -> TiffResult { match self { - Signed(val) => Ok(val), + Signed(val) => Ok(val.into()), + SignedBig(val) => Ok(val), val => Err(TiffError::FormatError( TiffFormatError::SignedIntegerExpected(val), )), @@ -71,8 +79,10 @@ impl Value { } Ok(new_vec) } - Unsigned(val) => Ok(vec![u32::try_from(val)?]), - Rational(numerator, denominator) => Ok(vec![u32::try_from(numerator)?, u32::try_from(denominator)?]), + Unsigned(val) => Ok(vec![val]), + UnsignedBig(val) => Ok(vec![u32::try_from(val)?]), + Rational(numerator, denominator) => Ok(vec![numerator, denominator]), + RationalBig(numerator, denominator) => Ok(vec![u32::try_from(numerator)?, u32::try_from(denominator)?]), Ascii(val) => Ok(val.chars().map(u32::from).collect()), val => Err(TiffError::FormatError( TiffFormatError::UnsignedIntegerExpected(val), @@ -87,6 +97,10 @@ impl Value { for v in vec { match v { SRational(numerator, denominator) => { + new_vec.push(numerator); + new_vec.push(denominator); + } + SRationalBig(numerator, denominator) => { new_vec.push(i32::try_from(numerator)?); new_vec.push(i32::try_from(denominator)?); } @@ -95,8 +109,10 @@ impl Value { } Ok(new_vec) } - Signed(val) => Ok(vec![i32::try_from(val)?]), - SRational(numerator, denominator) => Ok(vec![i32::try_from(numerator)?, i32::try_from(denominator)?]), + Signed(val) => Ok(vec![val]), + SignedBig(val) => Ok(vec![i32::try_from(val)?]), + SRational(numerator, denominator) => Ok(vec![numerator, denominator]), + SRationalBig(numerator, denominator) => Ok(vec![i32::try_from(numerator)?, i32::try_from(denominator)?]), val => Err(TiffError::FormatError( TiffFormatError::SignedIntegerExpected(val), )), @@ -112,8 +128,10 @@ impl Value { } Ok(new_vec) } - Unsigned(val) => Ok(vec![val]), - Rational(numerator, denominator) => Ok(vec![numerator, denominator]), + Unsigned(val) => Ok(vec![val.into()]), + UnsignedBig(val) => Ok(vec![val]), + Rational(numerator, denominator) => Ok(vec![numerator.into(), denominator.into()]), + RationalBig(numerator, denominator) => Ok(vec![numerator, denominator]), Ascii(val) => Ok(val.chars().map(u32::from).map(u64::from).collect()), val => Err(TiffError::FormatError( TiffFormatError::UnsignedIntegerExpected(val), @@ -128,6 +146,10 @@ impl Value { for v in vec { match v { SRational(numerator, denominator) => { + new_vec.push(numerator.into()); + new_vec.push(denominator.into()); + } + SRationalBig(numerator, denominator) => { new_vec.push(numerator); new_vec.push(denominator); } @@ -136,8 +158,10 @@ impl Value { } Ok(new_vec) } - Signed(val) => Ok(vec![val]), - SRational(numerator, denominator) => Ok(vec![numerator, denominator]), + Signed(val) => Ok(vec![val.into()]), + SignedBig(val) => Ok(vec![val]), + SRational(numerator, denominator) => Ok(vec![numerator.into(), denominator.into()]), + SRationalBig(numerator, denominator) => Ok(vec![numerator, denominator]), val => Err(TiffError::FormatError( TiffFormatError::SignedIntegerExpected(val), )), @@ -200,61 +224,61 @@ impl Entry { (Type::SHORT, 3) => { let mut r = self.r(bo); Some(List(vec![ - Unsigned(u64::from(r.read_u16()?)), - Unsigned(u64::from(r.read_u16()?)), - Unsigned(u64::from(r.read_u16()?)), + Unsigned(u32::from(r.read_u16()?)), + Unsigned(u32::from(r.read_u16()?)), + Unsigned(u32::from(r.read_u16()?)), ])) } (Type::SSHORT, 3) => { let mut r = self.r(bo); Some(List(vec![ - Signed(i64::from(r.read_i16()?)), - Signed(i64::from(r.read_i16()?)), - Signed(i64::from(r.read_i16()?)), + Signed(i32::from(r.read_i16()?)), + Signed(i32::from(r.read_i16()?)), + Signed(i32::from(r.read_i16()?)), ])) } (Type::SHORT, 4) => { let mut r = self.r(bo); Some(List(vec![ - Unsigned(u64::from(r.read_u16()?)), - Unsigned(u64::from(r.read_u16()?)), - Unsigned(u64::from(r.read_u16()?)), - Unsigned(u64::from(r.read_u16()?)), + Unsigned(u32::from(r.read_u16()?)), + Unsigned(u32::from(r.read_u16()?)), + Unsigned(u32::from(r.read_u16()?)), + Unsigned(u32::from(r.read_u16()?)), ])) } (Type::SSHORT, 4) => { let mut r = self.r(bo); Some(List(vec![ - Signed(i64::from(r.read_i16()?)), - Signed(i64::from(r.read_i16()?)), - Signed(i64::from(r.read_i16()?)), - Signed(i64::from(r.read_i16()?)), + Signed(i32::from(r.read_i16()?)), + Signed(i32::from(r.read_i16()?)), + Signed(i32::from(r.read_i16()?)), + Signed(i32::from(r.read_i16()?)), ])) } (Type::LONG, 2) => { let mut r = self.r(bo); Some(List(vec![ - Unsigned(r.read_u32()?.into()), - Unsigned(r.read_u32()?.into()), + Unsigned(r.read_u32()?), + Unsigned(r.read_u32()?), ])) } (Type::SLONG, 2) => { let mut r = self.r(bo); Some(List(vec![ - Signed(r.read_i32()?.into()), - Signed(r.read_i32()?.into()), + Signed(r.read_i32()?), + Signed(r.read_i32()?), ])) } (Type::RATIONAL, 1) => { let mut r = self.r(bo); - let numerator = r.read_u32()?.into(); - let denominator = r.read_u32()?.into(); + let numerator = r.read_u32()?; + let denominator = r.read_u32()?; Some(Rational(numerator, denominator)) } (Type::SRATIONAL, 1) => { let mut r = self.r(bo); - let numerator = r.read_i32()?.into(); - let denominator = r.read_i32()?.into(); + let numerator = r.read_i32()?; + let denominator = r.read_i32()?; Some(SRational(numerator, denominator)) } _ => None @@ -269,55 +293,55 @@ impl Entry { match (self.type_, self.count) { // TODO check if this could give wrong results // at a different endianess of file/computer. - (Type::BYTE, 1) => Ok(Unsigned(u64::from(self.offset[0]))), + (Type::BYTE, 1) => Ok(Unsigned(u32::from(self.offset[0]))), (Type::BYTE, 2) => offset_to_bytes(2, self), (Type::BYTE, 3) => offset_to_bytes(3, self), (Type::BYTE, 4) => offset_to_bytes(4, self), (Type::BYTE, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Unsigned(u64::from(decoder.read_byte()?))) + Ok(UnsignedBig(u64::from(decoder.read_byte()?))) }), - (Type::SBYTE, 1) => Ok(Signed(i64::from(self.offset[0] as i8))), + (Type::SBYTE, 1) => Ok(Signed(i32::from(self.offset[0] as i8))), (Type::SBYTE, 2) => offset_to_sbytes(2, self), (Type::SBYTE, 3) => offset_to_sbytes(3, self), (Type::SBYTE, 4) => offset_to_sbytes(4, self), (Type::SBYTE, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Signed(i64::from(decoder.read_byte()? as i8))) + Ok(SignedBig(i64::from(decoder.read_byte()? as i8))) }), - (Type::SHORT, 1) => Ok(Unsigned(u64::from(self.r(bo).read_u16()?))), - (Type::SSHORT, 1) => Ok(Signed(i64::from(self.r(bo).read_i16()?))), + (Type::SHORT, 1) => Ok(Unsigned(u32::from(self.r(bo).read_u16()?))), + (Type::SSHORT, 1) => Ok(Signed(i32::from(self.r(bo).read_i16()?))), (Type::SHORT, 2) => { let mut r = self.r(bo); Ok(List(vec![ - Unsigned(u64::from(r.read_u16()?)), - Unsigned(u64::from(r.read_u16()?)), + Unsigned(u32::from(r.read_u16()?)), + Unsigned(u32::from(r.read_u16()?)), ])) } (Type::SSHORT, 2) => { let mut r = self.r(bo); Ok(List(vec![ - Signed(i64::from(r.read_i16()?)), - Signed(i64::from(r.read_i16()?)), + Signed(i32::from(r.read_i16()?)), + Signed(i32::from(r.read_i16()?)), ])) } (Type::SHORT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Unsigned(u64::from(decoder.read_short()?))) + Ok(UnsignedBig(u64::from(decoder.read_short()?))) }), (Type::SSHORT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Signed(i64::from(decoder.read_sshort()?))) + Ok(SignedBig(i64::from(decoder.read_sshort()?))) }), - (Type::LONG, 1) => Ok(Unsigned(self.r(bo).read_u32()?.into())), - (Type::SLONG, 1) => Ok(Signed(self.r(bo).read_i32()?.into())), + (Type::LONG, 1) => Ok(Unsigned(self.r(bo).read_u32()?)), + (Type::SLONG, 1) => Ok(Signed(self.r(bo).read_i32()?)), (Type::LONG, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Unsigned(decoder.read_long()?.into())) + Ok(Unsigned(decoder.read_long()?)) }), (Type::SLONG, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Signed(decoder.read_slong()?.into())) + Ok(Signed(decoder.read_slong()?)) }), (Type::RATIONAL, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(Rational(decoder.read_long()?.into(), decoder.read_long()?.into())) + Ok(Rational(decoder.read_long()?, decoder.read_long()?)) }), (Type::SRATIONAL, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { - Ok(SRational(decoder.read_slong()?.into(), decoder.read_slong()?.into())) + Ok(SRational(decoder.read_slong()?, decoder.read_slong()?)) }), (Type::ASCII, n) => { let n = usize::try_from(n)?; @@ -369,7 +393,7 @@ fn offset_to_bytes(n: usize, entry: &Entry) -> TiffResult { Ok(List( entry.offset[0..n] .iter() - .map(|&e| Unsigned(u64::from(e))) + .map(|&e| Unsigned(u32::from(e))) .collect() )) } @@ -380,7 +404,7 @@ fn offset_to_sbytes(n: usize, entry: &Entry) -> TiffResult { Ok(List( entry.offset[0..n] .iter() - .map(|&e| Signed(i64::from(e as i8))) + .map(|&e| Signed(i32::from(e as i8))) .collect() )) } diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index d179cc5..c9e2c2a 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -1,4 +1,4 @@ -use std::convert::{TryInto, TryFrom}; +use std::convert::TryFrom; use std::collections::HashMap; use std::io::{self, Read, Seek}; use std::cmp;