feat: better compatibility with old api
This commit is contained in:
1 parent
797e3dca07
commit
dacc2864e8
3 files changed
+141
-60
No files matched your search
+75
-12
@@ -1,7 +1,7 @@
|
|||||||
//! Function for reading TIFF tags
|
//! Function for reading TIFF tags
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::convert::TryFrom;
|
use std::convert::{TryInto, TryFrom};
|
||||||
use std::io::{self, Read, Seek};
|
use std::io::{self, Read, Seek};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
@@ -26,6 +26,24 @@ pub enum Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
|
pub fn into_u32(self) -> TiffResult<u32> {
|
||||||
|
match self {
|
||||||
|
Unsigned(val) => Ok(u32::try_from(val)?),
|
||||||
|
val => Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::UnsignedIntegerExpected(val),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_i32(self) -> TiffResult<i32> {
|
||||||
|
match self {
|
||||||
|
Signed(val) => Ok(i32::try_from(val)?),
|
||||||
|
val => Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::SignedIntegerExpected(val),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn into_u64(self) -> TiffResult<u64> {
|
pub fn into_u64(self) -> TiffResult<u64> {
|
||||||
match self {
|
match self {
|
||||||
Unsigned(val) => Ok(val),
|
Unsigned(val) => Ok(val),
|
||||||
@@ -44,6 +62,47 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn into_u32_vec(self) -> TiffResult<Vec<u32>> {
|
||||||
|
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<Vec<i32>> {
|
||||||
|
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<Vec<u64>> {
|
pub fn into_u64_vec(self) -> TiffResult<Vec<u64>> {
|
||||||
match self {
|
match self {
|
||||||
List(vec) => {
|
List(vec) => {
|
||||||
@@ -103,7 +162,13 @@ impl ::std::fmt::Debug for Entry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
Entry {
|
||||||
type_,
|
type_,
|
||||||
count,
|
count,
|
||||||
@@ -259,12 +324,11 @@ impl Entry {
|
|||||||
if n > limits.decoding_buffer_size {
|
if n > limits.decoding_buffer_size {
|
||||||
return Err(TiffError::LimitsExceeded);
|
return Err(TiffError::LimitsExceeded);
|
||||||
}
|
}
|
||||||
let offset = if decoder.bigtiff {
|
if decoder.bigtiff {
|
||||||
self.r(bo).read_u64()?
|
decoder.goto_offset_u64(self.r(bo).read_u64()?)?
|
||||||
} else {
|
} else {
|
||||||
self.r(bo).read_u32()?.into()
|
decoder.goto_offset(self.r(bo).read_u32()?)?
|
||||||
};
|
}
|
||||||
decoder.goto_offset(offset)?;
|
|
||||||
let string = decoder.read_string(n)?;
|
let string = decoder.read_string(n)?;
|
||||||
Ok(Ascii(string))
|
Ok(Ascii(string))
|
||||||
}
|
}
|
||||||
@@ -287,12 +351,11 @@ impl Entry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut v = Vec::with_capacity(value_count);
|
let mut v = Vec::with_capacity(value_count);
|
||||||
let offset = if decoder.bigtiff {
|
if decoder.bigtiff {
|
||||||
self.r(bo).read_u64()?
|
decoder.goto_offset_u64(self.r(bo).read_u64()?)?
|
||||||
} else {
|
} else {
|
||||||
self.r(bo).read_u32()?.into()
|
decoder.goto_offset(self.r(bo).read_u32()?)?
|
||||||
};
|
}
|
||||||
decoder.goto_offset(offset)?;
|
|
||||||
for _ in 0..value_count {
|
for _ in 0..value_count {
|
||||||
v.push(decode_fn(decoder)?)
|
v.push(decode_fn(decoder)?)
|
||||||
}
|
}
|
||||||
|
|||||||
+46
-28
@@ -372,8 +372,8 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
/// To determine whether there are more images call `TIFFDecoder::more_images` instead.
|
/// To determine whether there are more images call `TIFFDecoder::more_images` instead.
|
||||||
pub fn next_image(&mut self) -> TiffResult<()> {
|
pub fn next_image(&mut self) -> TiffResult<()> {
|
||||||
self.ifd = Some(self.read_ifd()?);
|
self.ifd = Some(self.read_ifd()?);
|
||||||
self.width = u32::try_from(self.get_tag_u64(Tag::ImageWidth)?)?;
|
self.width = self.get_tag_u32(Tag::ImageWidth)?;
|
||||||
self.height = u32::try_from(self.get_tag_u64(Tag::ImageLength)?)?;
|
self.height = self.get_tag_u32(Tag::ImageLength)?;
|
||||||
self.strip_decoder = None;
|
self.strip_decoder = None;
|
||||||
|
|
||||||
self.photometric_interpretation = self
|
self.photometric_interpretation = self
|
||||||
@@ -466,19 +466,31 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
|
|
||||||
/// Reads a TIFF IFA offset/value field
|
/// Reads a TIFF IFA offset/value field
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn read_offset(&mut self) -> Result<[u8; 8], io::Error> {
|
pub fn read_offset(&mut self) -> TiffResult<[u8; 4]> {
|
||||||
let bytes_to_read = if self.bigtiff { 8 } else { 4 };
|
if self.bigtiff {
|
||||||
let mut val = vec![0; bytes_to_read];
|
return Err(TiffError::FormatError(TiffFormatError::InconsistentSizesEncountered))
|
||||||
self.reader.read_exact(&mut val)?;
|
|
||||||
if ! self.bigtiff {
|
|
||||||
val.append(&mut vec![0; 4])
|
|
||||||
}
|
}
|
||||||
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
|
/// Moves the cursor to the specified offset
|
||||||
#[inline]
|
#[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
|
self.reader
|
||||||
.seek(io::SeekFrom::Start(offset))
|
.seek(io::SeekFrom::Start(offset))
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
@@ -502,18 +514,22 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let count= if self.bigtiff {
|
let entry = if self.bigtiff {
|
||||||
self.read_long8()?
|
ifd::Entry::new_u64(
|
||||||
|
type_,
|
||||||
|
self.read_long8()?,
|
||||||
|
self.read_offset_u64()?
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
self.read_long()?.into()
|
ifd::Entry::new(
|
||||||
|
type_,
|
||||||
|
self.read_long()?,
|
||||||
|
self.read_offset()?
|
||||||
|
)
|
||||||
};
|
};
|
||||||
Ok(Some((
|
Ok(Some((
|
||||||
tag,
|
tag,
|
||||||
ifd::Entry::new(
|
entry,
|
||||||
type_,
|
|
||||||
count,
|
|
||||||
self.read_offset()?
|
|
||||||
),
|
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -526,7 +542,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
TiffFormatError::ImageFileDirectoryNotFound,
|
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() };
|
let num_tags = if self.bigtiff { self.read_long8()? } else { self.read_short()?.into() };
|
||||||
for _ in 0..num_tags {
|
for _ in 0..num_tags {
|
||||||
@@ -595,11 +611,17 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Tries to retrieve a tag and convert it to the desired type.
|
/// Tries to retrieve a tag and convert it to the desired type.
|
||||||
|
pub fn get_tag_u32(&mut self, tag: Tag) -> TiffResult<u32> {
|
||||||
|
self.get_tag(tag)?.into_u32()
|
||||||
|
}
|
||||||
pub fn get_tag_u64(&mut self, tag: Tag) -> TiffResult<u64> {
|
pub fn get_tag_u64(&mut self, tag: Tag) -> TiffResult<u64> {
|
||||||
self.get_tag(tag)?.into_u64()
|
self.get_tag(tag)?.into_u64()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tries to retrieve a tag and convert it to the desired type.
|
/// Tries to retrieve a tag and convert it to the desired type.
|
||||||
|
pub fn get_tag_u32_vec(&mut self, tag: Tag) -> TiffResult<Vec<u32>> {
|
||||||
|
self.get_tag(tag)?.into_u32_vec()
|
||||||
|
}
|
||||||
pub fn get_tag_u64_vec(&mut self, tag: Tag) -> TiffResult<Vec<u64>> {
|
pub fn get_tag_u64_vec(&mut self, tag: Tag) -> TiffResult<Vec<u64>> {
|
||||||
self.get_tag(tag)?.into_u64_vec()
|
self.get_tag(tag)?.into_u64_vec()
|
||||||
}
|
}
|
||||||
@@ -614,7 +636,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
max_uncompressed_length: usize,
|
max_uncompressed_length: usize,
|
||||||
) -> TiffResult<usize> {
|
) -> TiffResult<usize> {
|
||||||
let color_type = self.colortype()?;
|
let color_type = self.colortype()?;
|
||||||
self.goto_offset(offset)?;
|
self.goto_offset_u64(offset)?;
|
||||||
let (bytes, mut reader): (usize, Box<dyn EndianReader>) = match self.compression_method {
|
let (bytes, mut reader): (usize, Box<dyn EndianReader>) = match self.compression_method {
|
||||||
CompressionMethod::None => {
|
CompressionMethod::None => {
|
||||||
let order = self.reader.byte_order;
|
let order = self.reader.byte_order;
|
||||||
@@ -729,8 +751,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
/// Number of strips in image
|
/// Number of strips in image
|
||||||
pub fn strip_count(&mut self) -> TiffResult<u32> {
|
pub fn strip_count(&mut self) -> TiffResult<u32> {
|
||||||
let rows_per_strip = self
|
let rows_per_strip = self
|
||||||
.get_tag_u64(Tag::RowsPerStrip)
|
.get_tag_u32(Tag::RowsPerStrip)
|
||||||
.map(u32::try_from)?
|
|
||||||
.unwrap_or(self.height);
|
.unwrap_or(self.height);
|
||||||
|
|
||||||
if rows_per_strip == 0 {
|
if rows_per_strip == 0 {
|
||||||
@@ -778,8 +799,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
))?;
|
))?;
|
||||||
|
|
||||||
let rows_per_strip = usize::try_from(self
|
let rows_per_strip = usize::try_from(self
|
||||||
.get_tag_u64(Tag::RowsPerStrip)
|
.get_tag_u32(Tag::RowsPerStrip)
|
||||||
.map(u32::try_from)?
|
|
||||||
.unwrap_or(self.height))?;
|
.unwrap_or(self.height))?;
|
||||||
|
|
||||||
let strip_height = cmp::min(
|
let strip_height = cmp::min(
|
||||||
@@ -851,8 +871,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
let index = self.strip_decoder.as_ref().unwrap().strip_index;
|
let index = self.strip_decoder.as_ref().unwrap().strip_index;
|
||||||
|
|
||||||
let rows_per_strip = usize::try_from(self
|
let rows_per_strip = usize::try_from(self
|
||||||
.get_tag_u64(Tag::RowsPerStrip)
|
.get_tag_u32(Tag::RowsPerStrip)
|
||||||
.map(u32::try_from)?
|
|
||||||
.unwrap_or(self.height))?;
|
.unwrap_or(self.height))?;
|
||||||
|
|
||||||
let strip_height = cmp::min(
|
let strip_height = cmp::min(
|
||||||
@@ -871,8 +890,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
pub fn read_image(&mut self) -> TiffResult<DecodingResult> {
|
pub fn read_image(&mut self) -> TiffResult<DecodingResult> {
|
||||||
self.initialize_strip_decoder()?;
|
self.initialize_strip_decoder()?;
|
||||||
let rows_per_strip = usize::try_from(self
|
let rows_per_strip = usize::try_from(self
|
||||||
.get_tag_u64(Tag::RowsPerStrip)
|
.get_tag_u32(Tag::RowsPerStrip)
|
||||||
.map(u32::try_from)?
|
|
||||||
.unwrap_or(self.height))?;
|
.unwrap_or(self.height))?;
|
||||||
|
|
||||||
let samples_per_strip =
|
let samples_per_strip =
|
||||||
|
|||||||
+20
-20
@@ -165,11 +165,11 @@ fn test_multiple_byte() {
|
|||||||
{
|
{
|
||||||
let mut decoder = Decoder::new(&mut data).unwrap();
|
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(65000)).unwrap().into_u32_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(65001)).unwrap().into_u32_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(65002)).unwrap().into_u32_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(65003)).unwrap().into_u32_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(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();
|
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(65000)).unwrap().into_i32().unwrap(), -1, );
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_i64_vec().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_i64_vec().unwrap(), [-1, 2]);
|
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_i64_vec().unwrap(), [-1, 2, -3]);
|
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_i64_vec().unwrap(), [-1, 2, -3, 4]);
|
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_i64_vec().unwrap(), [-1, 2, -3, 4, -5], );
|
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(65010)).unwrap().into_i32().unwrap(), -1);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65011)).unwrap().into_i64_vec().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_i64_vec().unwrap(), [-1, 2]);
|
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_i64_vec().unwrap(), [-1, 2, -3]);
|
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(65020)).unwrap().into_i32().unwrap(), -1);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65021)).unwrap().into_i64_vec().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_i64_vec().unwrap(), [-1, 2]);
|
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(65030)).unwrap().into_i32_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(65031)).unwrap().into_i32_vec().unwrap(), [-1_i32, 100, 2, 100]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user