feat: Support decoding big tiff
This commit is contained in:
1 parent
472cf6d07d
commit
23751bd8c5
3 files changed
+270
-156
No files matched your search
+167
-100
@@ -15,18 +15,18 @@ use self::Value::{Ascii, List, Rational, Unsigned, Signed, SRational};
|
|||||||
#[allow(unused_qualifications)]
|
#[allow(unused_qualifications)]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
Signed(i32),
|
Signed(i64),
|
||||||
Unsigned(u32),
|
Unsigned(u64),
|
||||||
List(Vec<Value>),
|
List(Vec<Value>),
|
||||||
Rational(u32, u32),
|
Rational(u64, u64),
|
||||||
SRational(i32, i32),
|
SRational(i64, i64),
|
||||||
Ascii(String),
|
Ascii(String),
|
||||||
#[doc(hidden)] // Do not match against this.
|
#[doc(hidden)] // Do not match against this.
|
||||||
__NonExhaustive,
|
__NonExhaustive,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
pub fn into_u32(self) -> TiffResult<u32> {
|
pub fn into_u64(self) -> TiffResult<u64> {
|
||||||
match self {
|
match self {
|
||||||
Unsigned(val) => Ok(val),
|
Unsigned(val) => Ok(val),
|
||||||
val => Err(TiffError::FormatError(
|
val => Err(TiffError::FormatError(
|
||||||
@@ -35,7 +35,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_i32(self) -> TiffResult<i32> {
|
pub fn into_i64(self) -> TiffResult<i64> {
|
||||||
match self {
|
match self {
|
||||||
Signed(val) => Ok(val),
|
Signed(val) => Ok(val),
|
||||||
val => Err(TiffError::FormatError(
|
val => Err(TiffError::FormatError(
|
||||||
@@ -44,25 +44,25 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_u32_vec(self) -> TiffResult<Vec<u32>> {
|
pub fn into_u64_vec(self) -> TiffResult<Vec<u64>> {
|
||||||
match self {
|
match self {
|
||||||
List(vec) => {
|
List(vec) => {
|
||||||
let mut new_vec = Vec::with_capacity(vec.len());
|
let mut new_vec = Vec::with_capacity(vec.len());
|
||||||
for v in vec {
|
for v in vec {
|
||||||
new_vec.push(v.into_u32()?)
|
new_vec.push(v.into_u64()?)
|
||||||
}
|
}
|
||||||
Ok(new_vec)
|
Ok(new_vec)
|
||||||
}
|
}
|
||||||
Unsigned(val) => Ok(vec![val]),
|
Unsigned(val) => Ok(vec![val]),
|
||||||
Rational(numerator, denominator) => Ok(vec![numerator, denominator]),
|
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(
|
val => Err(TiffError::FormatError(
|
||||||
TiffFormatError::UnsignedIntegerExpected(val),
|
TiffFormatError::UnsignedIntegerExpected(val),
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_i32_vec(self) -> TiffResult<Vec<i32>> {
|
pub fn into_i64_vec(self) -> TiffResult<Vec<i64>> {
|
||||||
match self {
|
match self {
|
||||||
List(vec) => {
|
List(vec) => {
|
||||||
let mut new_vec = Vec::with_capacity(vec.len());
|
let mut new_vec = Vec::with_capacity(vec.len());
|
||||||
@@ -72,7 +72,7 @@ impl Value {
|
|||||||
new_vec.push(numerator);
|
new_vec.push(numerator);
|
||||||
new_vec.push(denominator);
|
new_vec.push(denominator);
|
||||||
}
|
}
|
||||||
_ => new_vec.push(v.into_i32()?)
|
_ => new_vec.push(v.into_i64()?),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(new_vec)
|
Ok(new_vec)
|
||||||
@@ -85,12 +85,12 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Entry {
|
pub struct Entry {
|
||||||
type_: Type,
|
type_: Type,
|
||||||
count: u32,
|
count: u64,
|
||||||
offset: [u8; 4],
|
offset: [u8; 8],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::fmt::Debug for Entry {
|
impl ::std::fmt::Debug for Entry {
|
||||||
@@ -103,7 +103,7 @@ impl ::std::fmt::Debug for Entry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
Entry {
|
||||||
type_,
|
type_,
|
||||||
count,
|
count,
|
||||||
@@ -122,94 +122,156 @@ impl Entry {
|
|||||||
decoder: &mut super::Decoder<R>,
|
decoder: &mut super::Decoder<R>,
|
||||||
) -> TiffResult<Value> {
|
) -> TiffResult<Value> {
|
||||||
let bo = decoder.byte_order();
|
let bo = decoder.byte_order();
|
||||||
match (self.type_, self.count) {
|
let bigtiff_value = if decoder.bigtiff && 4 < self.count && self.count <= 8 {
|
||||||
// TODO check if this could give wrong results
|
match (self.type_, self.count) {
|
||||||
// at a different endianess of file/computer.
|
(Type::BYTE, n) => Some(offset_to_bytes(n as usize, self)?),
|
||||||
(Type::BYTE, 1) => Ok(Unsigned(u32::from(self.offset[0]))),
|
(Type::SBYTE, n) => Some(offset_to_sbytes(n as usize, self)?),
|
||||||
(Type::BYTE, 2) => offset_to_bytes(2, self),
|
(Type::SHORT, 3) => {
|
||||||
(Type::BYTE, 3) => offset_to_bytes(3, self),
|
let mut r = self.r(bo);
|
||||||
(Type::BYTE, 4) => offset_to_bytes(4, self),
|
Some(List(vec![
|
||||||
(Type::BYTE, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
Unsigned(u64::from(r.read_u16()?)),
|
||||||
Ok(Unsigned(u32::from(decoder.read_byte()?)))
|
Unsigned(u64::from(r.read_u16()?)),
|
||||||
}),
|
Unsigned(u64::from(r.read_u16()?)),
|
||||||
(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);
|
|
||||||
}
|
}
|
||||||
decoder.goto_offset(self.r(bo).read_u32()?)?;
|
(Type::SSHORT, 3) => {
|
||||||
let string = decoder.read_string(n)?;
|
let mut r = self.r(bo);
|
||||||
Ok(Ascii(string))
|
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]
|
#[inline]
|
||||||
fn decode_offset<R, F>(&self, value_count: u32, bo: ByteOrder, limits: &super::Limits, decoder: &mut super::Decoder<R>, decode_fn: F) -> TiffResult<Value>
|
fn decode_offset<R, F>(&self, value_count: u64, bo: ByteOrder, limits: &super::Limits, decoder: &mut super::Decoder<R>, decode_fn: F) -> TiffResult<Value>
|
||||||
where
|
where
|
||||||
R: Read + Seek,
|
R: Read + Seek,
|
||||||
F: Fn(&mut super::Decoder<R>) -> TiffResult<Value>,
|
F: Fn(&mut super::Decoder<R>) -> TiffResult<Value>,
|
||||||
@@ -220,7 +282,12 @@ impl Entry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut v = Vec::with_capacity(value_count);
|
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 {
|
for _ in 0..value_count {
|
||||||
v.push(decode_fn(decoder)?)
|
v.push(decode_fn(decoder)?)
|
||||||
}
|
}
|
||||||
@@ -234,7 +301,7 @@ fn offset_to_bytes(n: usize, entry: &Entry) -> TiffResult<Value> {
|
|||||||
Ok(List(
|
Ok(List(
|
||||||
entry.offset[0..n]
|
entry.offset[0..n]
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&e| Unsigned(u32::from(e)))
|
.map(|&e| Unsigned(u64::from(e)))
|
||||||
.collect()
|
.collect()
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@@ -245,7 +312,7 @@ fn offset_to_sbytes(n: usize, entry: &Entry) -> TiffResult<Value> {
|
|||||||
Ok(List(
|
Ok(List(
|
||||||
entry.offset[0..n]
|
entry.offset[0..n]
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&e| Signed(i32::from(e as i8)))
|
.map(|&e| Signed(i64::from(e as i8)))
|
||||||
.collect()
|
.collect()
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|||||||
+83
-36
@@ -1,4 +1,4 @@
|
|||||||
use std::convert::TryFrom;
|
use std::convert::{TryInto, TryFrom};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::{self, Read, Seek};
|
use std::io::{self, Read, Seek};
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
@@ -116,8 +116,8 @@ impl<'a> DecodingBuffer<'a> {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct StripDecodeState {
|
struct StripDecodeState {
|
||||||
strip_index: usize,
|
strip_index: usize,
|
||||||
strip_offsets: Vec<u32>,
|
strip_offsets: Vec<u64>,
|
||||||
strip_bytes: Vec<u32>,
|
strip_bytes: Vec<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decoding limits
|
/// Decoding limits
|
||||||
@@ -157,8 +157,9 @@ where
|
|||||||
{
|
{
|
||||||
reader: SmartReader<R>,
|
reader: SmartReader<R>,
|
||||||
byte_order: ByteOrder,
|
byte_order: ByteOrder,
|
||||||
|
bigtiff: bool,
|
||||||
limits: Limits,
|
limits: Limits,
|
||||||
next_ifd: Option<u32>,
|
next_ifd: Option<u64>,
|
||||||
ifd: Option<Directory>,
|
ifd: Option<Directory>,
|
||||||
width: u32,
|
width: u32,
|
||||||
height: u32,
|
height: u32,
|
||||||
@@ -248,6 +249,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
Decoder {
|
Decoder {
|
||||||
reader: SmartReader::wrap(r, ByteOrder::LittleEndian),
|
reader: SmartReader::wrap(r, ByteOrder::LittleEndian),
|
||||||
byte_order: ByteOrder::LittleEndian,
|
byte_order: ByteOrder::LittleEndian,
|
||||||
|
bigtiff: false,
|
||||||
limits: Default::default(),
|
limits: Default::default(),
|
||||||
next_ifd: None,
|
next_ifd: None,
|
||||||
ifd: None,
|
ifd: None,
|
||||||
@@ -330,12 +332,28 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if self.read_short()? != 42 {
|
match self.read_short()? {
|
||||||
return Err(TiffError::FormatError(
|
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,
|
TiffFormatError::TiffSignatureInvalid,
|
||||||
));
|
))
|
||||||
}
|
}
|
||||||
self.next_ifd = match self.read_long()? {
|
self.next_ifd = match self.read_ifd_offset()? {
|
||||||
0 => None,
|
0 => None,
|
||||||
n => Some(n),
|
n => Some(n),
|
||||||
};
|
};
|
||||||
@@ -354,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 = self.get_tag_u32(Tag::ImageWidth)?;
|
self.width = u32::try_from(self.get_tag_u64(Tag::ImageWidth)?)?;
|
||||||
self.height = self.get_tag_u32(Tag::ImageLength)?;
|
self.height = u32::try_from(self.get_tag_u64(Tag::ImageLength)?)?;
|
||||||
self.strip_decoder = None;
|
self.strip_decoder = None;
|
||||||
|
|
||||||
self.photometric_interpretation = self
|
self.photometric_interpretation = self
|
||||||
@@ -389,6 +407,15 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
self.byte_order
|
self.byte_order
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn read_ifd_offset(&mut self) -> Result<u64, io::Error> {
|
||||||
|
if self.bigtiff {
|
||||||
|
self.read_long8()
|
||||||
|
} else {
|
||||||
|
self.read_long().map(u64::from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Reads a TIFF byte value
|
/// Reads a TIFF byte value
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn read_byte(&mut self) -> Result<u8, io::Error> {
|
pub fn read_byte(&mut self) -> Result<u8, io::Error> {
|
||||||
@@ -421,6 +448,12 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
self.reader.read_i32()
|
self.reader.read_i32()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn read_long8(&mut self) -> Result<u64, io::Error> {
|
||||||
|
self.reader.read_u64()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Reads a string
|
/// Reads a string
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn read_string(&mut self, length: usize) -> TiffResult<String> {
|
pub fn read_string(&mut self, length: usize) -> TiffResult<String> {
|
||||||
@@ -433,17 +466,21 @@ 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; 4], io::Error> {
|
pub fn read_offset(&mut self) -> Result<[u8; 8], io::Error> {
|
||||||
let mut val = [0; 4];
|
let bytes_to_read = if self.bigtiff { 8 } else { 4 };
|
||||||
|
let mut val = vec![0; bytes_to_read];
|
||||||
self.reader.read_exact(&mut val)?;
|
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
|
/// Moves the cursor to the specified offset
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn goto_offset(&mut self, offset: u32) -> io::Result<()> {
|
pub fn goto_offset(&mut self, offset: u64) -> io::Result<()> {
|
||||||
self.reader
|
self.reader
|
||||||
.seek(io::SeekFrom::Start(u64::from(offset)))
|
.seek(io::SeekFrom::Start(offset))
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -465,12 +502,17 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let count= if self.bigtiff {
|
||||||
|
self.read_long8()?
|
||||||
|
} else {
|
||||||
|
self.read_long()?.into()
|
||||||
|
};
|
||||||
Ok(Some((
|
Ok(Some((
|
||||||
tag,
|
tag,
|
||||||
ifd::Entry::new(
|
ifd::Entry::new(
|
||||||
type_,
|
type_,
|
||||||
self.read_long()?, // count
|
count,
|
||||||
self.read_offset()?, // offset
|
self.read_offset()?
|
||||||
),
|
),
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
@@ -493,7 +535,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
};
|
};
|
||||||
dir.insert(tag, entry);
|
dir.insert(tag, entry);
|
||||||
}
|
}
|
||||||
self.next_ifd = match self.read_long()? {
|
self.next_ifd = match self.read_ifd_offset()? {
|
||||||
0 => None,
|
0 => None,
|
||||||
n => Some(n),
|
n => Some(n),
|
||||||
};
|
};
|
||||||
@@ -503,6 +545,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
/// Tries to retrieve a tag.
|
/// Tries to retrieve a tag.
|
||||||
/// Return `Ok(None)` if the tag is not present.
|
/// Return `Ok(None)` if the tag is not present.
|
||||||
pub fn find_tag(&mut self, tag: Tag) -> TiffResult<Option<ifd::Value>> {
|
pub fn find_tag(&mut self, tag: Tag) -> TiffResult<Option<ifd::Value>> {
|
||||||
|
println!("IFD {:?}", self.ifd.as_ref().unwrap().get(&tag));
|
||||||
let entry = match self.ifd.as_ref().unwrap().get(&tag) {
|
let entry = match self.ifd.as_ref().unwrap().get(&tag) {
|
||||||
None => return Ok(None),
|
None => return Ok(None),
|
||||||
Some(entry) => entry.clone(),
|
Some(entry) => entry.clone(),
|
||||||
@@ -514,9 +557,9 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Tries to retrieve a tag and convert it to the desired unsigned type.
|
/// Tries to retrieve a tag and convert it to the desired unsigned type.
|
||||||
pub fn find_tag_unsigned<T: TryFrom<u32>>(&mut self, tag: Tag) -> TiffResult<Option<T>> {
|
pub fn find_tag_unsigned<T: TryFrom<u64>>(&mut self, tag: Tag) -> TiffResult<Option<T>> {
|
||||||
self.find_tag(tag)?
|
self.find_tag(tag)?
|
||||||
.map(|v| v.into_u32()).transpose()?
|
.map(|v| v.into_u64()).transpose()?
|
||||||
.map(|value| T::try_from(value)
|
.map(|value| T::try_from(value)
|
||||||
.map_err(|_| TiffFormatError::InvalidTagValueType(tag).into()))
|
.map_err(|_| TiffFormatError::InvalidTagValueType(tag).into()))
|
||||||
.transpose()
|
.transpose()
|
||||||
@@ -524,9 +567,9 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
|
|
||||||
/// Tries to retrieve a vector of all a tag's values and convert them to
|
/// Tries to retrieve a vector of all a tag's values and convert them to
|
||||||
/// the desired unsigned type.
|
/// the desired unsigned type.
|
||||||
pub fn find_tag_unsigned_vec<T: TryFrom<u32>>(&mut self, tag: Tag) -> TiffResult<Option<Vec<T>>> {
|
pub fn find_tag_unsigned_vec<T: TryFrom<u64>>(&mut self, tag: Tag) -> TiffResult<Option<Vec<T>>> {
|
||||||
self.find_tag(tag)?
|
self.find_tag(tag)?
|
||||||
.map(|v| v.into_u32_vec()).transpose()?
|
.map(|v| v.into_u64_vec()).transpose()?
|
||||||
.map(|v| v.into_iter()
|
.map(|v| v.into_iter()
|
||||||
.map(|u| T::try_from(u).map_err(|_| TiffFormatError::InvalidTagValueType(tag).into()))
|
.map(|u| T::try_from(u).map_err(|_| TiffFormatError::InvalidTagValueType(tag).into()))
|
||||||
.collect())
|
.collect())
|
||||||
@@ -535,7 +578,7 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
|
|
||||||
/// Tries to retrieve a tag and convert it to the desired unsigned type.
|
/// Tries to retrieve a tag and convert it to the desired unsigned type.
|
||||||
/// Returns an error if the tag is not present.
|
/// Returns an error if the tag is not present.
|
||||||
pub fn get_tag_unsigned<T: TryFrom<u32>>(&mut self, tag: Tag) -> TiffResult<T> {
|
pub fn get_tag_unsigned<T: TryFrom<u64>>(&mut self, tag: Tag) -> TiffResult<T> {
|
||||||
self.find_tag_unsigned(tag)?
|
self.find_tag_unsigned(tag)?
|
||||||
.ok_or_else(|| TiffFormatError::RequiredTagNotFound(tag).into())
|
.ok_or_else(|| TiffFormatError::RequiredTagNotFound(tag).into())
|
||||||
}
|
}
|
||||||
@@ -552,13 +595,13 @@ 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> {
|
pub fn get_tag_u64(&mut self, tag: Tag) -> TiffResult<u64> {
|
||||||
self.get_tag(tag)?.into_u32()
|
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>> {
|
pub fn get_tag_u64_vec(&mut self, tag: Tag) -> TiffResult<Vec<u64>> {
|
||||||
self.get_tag(tag)?.into_u32_vec()
|
self.get_tag(tag)?.into_u64_vec()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decompresses the strip into the supplied buffer.
|
/// Decompresses the strip into the supplied buffer.
|
||||||
@@ -566,13 +609,13 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
fn expand_strip<'a>(
|
fn expand_strip<'a>(
|
||||||
&mut self,
|
&mut self,
|
||||||
buffer: DecodingBuffer<'a>,
|
buffer: DecodingBuffer<'a>,
|
||||||
offset: u32,
|
offset: u64,
|
||||||
length: u32,
|
length: u64,
|
||||||
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(offset)?;
|
||||||
let (bytes, mut reader): (usize, Box<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;
|
||||||
(
|
(
|
||||||
@@ -686,7 +729,8 @@ 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_u32(Tag::RowsPerStrip)
|
.get_tag_u64(Tag::RowsPerStrip)
|
||||||
|
.map(u32::try_from)?
|
||||||
.unwrap_or(self.height);
|
.unwrap_or(self.height);
|
||||||
|
|
||||||
if rows_per_strip == 0 {
|
if rows_per_strip == 0 {
|
||||||
@@ -698,8 +742,8 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
|
|
||||||
fn initialize_strip_decoder(&mut self) -> TiffResult<()> {
|
fn initialize_strip_decoder(&mut self) -> TiffResult<()> {
|
||||||
if self.strip_decoder.is_none() {
|
if self.strip_decoder.is_none() {
|
||||||
let strip_offsets = self.get_tag_u32_vec(Tag::StripOffsets)?;
|
let strip_offsets = self.get_tag_u64_vec(Tag::StripOffsets)?;
|
||||||
let strip_bytes = self.get_tag_u32_vec(Tag::StripByteCounts)?;
|
let strip_bytes = self.get_tag_u64_vec(Tag::StripByteCounts)?;
|
||||||
|
|
||||||
self.strip_decoder = Some(StripDecodeState {
|
self.strip_decoder = Some(StripDecodeState {
|
||||||
strip_index: 0,
|
strip_index: 0,
|
||||||
@@ -734,7 +778,8 @@ impl<R: Read + Seek> Decoder<R> {
|
|||||||
))?;
|
))?;
|
||||||
|
|
||||||
let rows_per_strip = usize::try_from(self
|
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))?;
|
.unwrap_or(self.height))?;
|
||||||
|
|
||||||
let strip_height = cmp::min(
|
let strip_height = cmp::min(
|
||||||
@@ -806,7 +851,8 @@ 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_u32(Tag::RowsPerStrip)
|
.get_tag_u64(Tag::RowsPerStrip)
|
||||||
|
.map(u32::try_from)?
|
||||||
.unwrap_or(self.height))?;
|
.unwrap_or(self.height))?;
|
||||||
|
|
||||||
let strip_height = cmp::min(
|
let strip_height = cmp::min(
|
||||||
@@ -825,7 +871,8 @@ 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_u32(Tag::RowsPerStrip)
|
.get_tag_u64(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_u32_vec().unwrap(), [1]);
|
assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_u64_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(65001)).unwrap().into_u64_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(65002)).unwrap().into_u64_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(65003)).unwrap().into_u64_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(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();
|
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(65000)).unwrap().into_i64().unwrap(), -1, );
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_i32_vec().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_i32_vec().unwrap(), [-1, 2]);
|
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_i32_vec().unwrap(), [-1, 2, -3]);
|
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_i32_vec().unwrap(), [-1, 2, -3, 4]);
|
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_i32_vec().unwrap(), [-1, 2, -3, 4, -5], );
|
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(65010)).unwrap().into_i64().unwrap(), -1);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65011)).unwrap().into_i32_vec().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_i32_vec().unwrap(), [-1, 2]);
|
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_i32_vec().unwrap(), [-1, 2, -3]);
|
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(65020)).unwrap().into_i64().unwrap(), -1);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65021)).unwrap().into_i32_vec().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_i32_vec().unwrap(), [-1, 2]);
|
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(65030)).unwrap().into_i64_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(65031)).unwrap().into_i64_vec().unwrap(), [-1_i64, 100, 2, 100]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user