Read Short as u16, SignedShort as i16 (#235)

This commit is contained in:
Wei Ji authored and GitHub committed 2025-06-08 18:03:08 -07:00
1 parent 36f7b3b4aa
commit aeab243a96
3 files changed
+27 -27

No files matched your search

+9 -11
View File
@@ -59,9 +59,7 @@ impl Value {
Short(val) => Ok(val), Short(val) => Ok(val),
Unsigned(val) => Ok(u16::try_from(val)?), Unsigned(val) => Ok(u16::try_from(val)?),
UnsignedBig(val) => Ok(u16::try_from(val)?), UnsignedBig(val) => Ok(u16::try_from(val)?),
val => Err(TiffError::FormatError( val => Err(TiffError::FormatError(TiffFormatError::ShortExpected(val))),
TiffFormatError::UnsignedIntegerExpected(val),
)),
} }
} }
@@ -163,6 +161,7 @@ impl Value {
} }
Ok(new_vec) Ok(new_vec)
} }
Short(val) => Ok(vec![val.into()]),
Unsigned(val) => Ok(vec![val]), Unsigned(val) => Ok(vec![val]),
UnsignedBig(val) => Ok(vec![u32::try_from(val)?]), UnsignedBig(val) => Ok(vec![u32::try_from(val)?]),
Rational(numerator, denominator) => Ok(vec![numerator, denominator]), Rational(numerator, denominator) => Ok(vec![numerator, denominator]),
@@ -205,9 +204,7 @@ impl Value {
Ok(new_vec) Ok(new_vec)
} }
Short(val) => Ok(vec![val]), Short(val) => Ok(vec![val]),
val => Err(TiffError::FormatError( val => Err(TiffError::FormatError(TiffFormatError::ShortExpected(val))),
TiffFormatError::UnsignedIntegerExpected(val),
)),
} }
} }
@@ -285,6 +282,7 @@ impl Value {
} }
Ok(new_vec) Ok(new_vec)
} }
Short(val) => Ok(vec![val.into()]),
Unsigned(val) => Ok(vec![val.into()]), Unsigned(val) => Ok(vec![val.into()]),
UnsignedBig(val) => Ok(vec![val]), UnsignedBig(val) => Ok(vec![val]),
Rational(numerator, denominator) => Ok(vec![numerator.into(), denominator.into()]), Rational(numerator, denominator) => Ok(vec![numerator.into(), denominator.into()]),
@@ -433,8 +431,8 @@ impl Entry {
Type::BYTE => Unsigned(u32::from(self.offset[0])), Type::BYTE => Unsigned(u32::from(self.offset[0])),
Type::SBYTE => Signed(i32::from(self.offset[0] as i8)), Type::SBYTE => Signed(i32::from(self.offset[0] as i8)),
Type::UNDEFINED => Byte(self.offset[0]), Type::UNDEFINED => Byte(self.offset[0]),
Type::SHORT => Unsigned(u32::from(self.r(bo).read_u16()?)), Type::SHORT => Short(self.r(bo).read_u16()?),
Type::SSHORT => Signed(i32::from(self.r(bo).read_i16()?)), Type::SSHORT => SignedShort(self.r(bo).read_i16()?),
Type::LONG => Unsigned(self.r(bo).read_u32()?), Type::LONG => Unsigned(self.r(bo).read_u32()?),
Type::SLONG => Signed(self.r(bo).read_i32()?), Type::SLONG => Signed(self.r(bo).read_i32()?),
Type::FLOAT => Float(self.r(bo).read_f32()?), Type::FLOAT => Float(self.r(bo).read_f32()?),
@@ -509,7 +507,7 @@ impl Entry {
let mut r = self.r(bo); let mut r = self.r(bo);
let mut v = Vec::new(); let mut v = Vec::new();
for _ in 0..self.count { for _ in 0..self.count {
v.push(Signed(i32::from(r.read_i16()?))); v.push(SignedShort(r.read_i16()?));
} }
return Ok(List(v)); return Ok(List(v));
} }
@@ -569,10 +567,10 @@ impl Entry {
Ok(SignedBig(i64::from(reader.read_i8()?))) Ok(SignedBig(i64::from(reader.read_i8()?)))
}), }),
Type::SHORT => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| { Type::SHORT => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
Ok(UnsignedBig(u64::from(reader.read_u16()?))) Ok(Short(reader.read_u16()?))
}), }),
Type::SSHORT => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| { Type::SSHORT => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
Ok(SignedBig(i64::from(reader.read_i16()?))) Ok(SignedShort(reader.read_i16()?))
}), }),
Type::LONG => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| { Type::LONG => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
Ok(Unsigned(reader.read_u32()?)) Ok(Unsigned(reader.read_u32()?))
+2
View File
@@ -69,6 +69,7 @@ pub enum TiffFormatError {
UnknownPlanarConfiguration(u16), UnknownPlanarConfiguration(u16),
ByteExpected(Value), ByteExpected(Value),
SignedByteExpected(Value), SignedByteExpected(Value),
ShortExpected(Value),
SignedShortExpected(Value), SignedShortExpected(Value),
UnsignedIntegerExpected(Value), UnsignedIntegerExpected(Value),
SignedIntegerExpected(Value), SignedIntegerExpected(Value),
@@ -122,6 +123,7 @@ impl fmt::Display for TiffFormatError {
} }
ByteExpected(ref val) => write!(fmt, "Expected byte, {:?} found.", val), ByteExpected(ref val) => write!(fmt, "Expected byte, {:?} found.", val),
SignedByteExpected(ref val) => write!(fmt, "Expected signed byte, {:?} found.", val), SignedByteExpected(ref val) => write!(fmt, "Expected signed byte, {:?} found.", val),
ShortExpected(ref val) => write!(fmt, "Expected short, {:?} found.", val),
SignedShortExpected(ref val) => write!(fmt, "Expected signed short, {:?} found.", val), SignedShortExpected(ref val) => write!(fmt, "Expected signed short, {:?} found.", val),
UnsignedIntegerExpected(ref val) => { UnsignedIntegerExpected(ref val) => {
write!(fmt, "Expected unsigned integer, {:?} found.", val) write!(fmt, "Expected unsigned integer, {:?} found.", val)
+16 -16
View File
@@ -50,28 +50,28 @@ fn encode_decode() {
( (
Tag::BitsPerSample, Tag::BitsPerSample,
ifd::Value::List(vec![ ifd::Value::List(vec![
ifd::Value::UnsignedBig(8), ifd::Value::Short(8),
ifd::Value::UnsignedBig(8), ifd::Value::Short(8),
ifd::Value::UnsignedBig(8) ifd::Value::Short(8)
]) ])
), ),
(Tag::Compression, ifd::Value::Unsigned(1)), (Tag::Compression, ifd::Value::Short(1)),
(Tag::PhotometricInterpretation, ifd::Value::Unsigned(2)), (Tag::PhotometricInterpretation, ifd::Value::Short(2)),
(Tag::StripOffsets, ifd::Value::Unsigned(8)), (Tag::StripOffsets, ifd::Value::Unsigned(8)),
(Tag::SamplesPerPixel, ifd::Value::Unsigned(3)), (Tag::SamplesPerPixel, ifd::Value::Short(3)),
(Tag::RowsPerStrip, ifd::Value::Unsigned(3334)), (Tag::RowsPerStrip, ifd::Value::Unsigned(3334)),
(Tag::StripByteCounts, ifd::Value::Unsigned(30000)), (Tag::StripByteCounts, ifd::Value::Unsigned(30000)),
(Tag::XResolution, ifd::Value::Rational(1, 1)), (Tag::XResolution, ifd::Value::Rational(1, 1)),
(Tag::YResolution, ifd::Value::Rational(1, 1)), (Tag::YResolution, ifd::Value::Rational(1, 1)),
(Tag::ResolutionUnit, ifd::Value::Unsigned(1)), (Tag::ResolutionUnit, ifd::Value::Short(1)),
(Tag::Artist, ifd::Value::Ascii("Image-tiff".into())), (Tag::Artist, ifd::Value::Ascii("Image-tiff".into())),
(Tag::Predictor, ifd::Value::Unsigned(1)), (Tag::Predictor, ifd::Value::Short(1)),
( (
Tag::SampleFormat, Tag::SampleFormat,
ifd::Value::List(vec![ ifd::Value::List(vec![
ifd::Value::UnsignedBig(1), ifd::Value::Short(1),
ifd::Value::UnsignedBig(1), ifd::Value::Short(1),
ifd::Value::UnsignedBig(1) ifd::Value::Short(1)
]) ])
), ),
] ]
@@ -135,17 +135,17 @@ fn encode_decode_big() {
ifd::Value::Short(8) ifd::Value::Short(8)
]) ])
), ),
(Tag::Compression, ifd::Value::Unsigned(1)), (Tag::Compression, ifd::Value::Short(1)),
(Tag::PhotometricInterpretation, ifd::Value::Unsigned(2)), (Tag::PhotometricInterpretation, ifd::Value::Short(2)),
(Tag::StripOffsets, ifd::Value::UnsignedBig(16)), (Tag::StripOffsets, ifd::Value::UnsignedBig(16)),
(Tag::SamplesPerPixel, ifd::Value::Unsigned(3)), (Tag::SamplesPerPixel, ifd::Value::Short(3)),
(Tag::RowsPerStrip, ifd::Value::Unsigned(3334)), (Tag::RowsPerStrip, ifd::Value::Unsigned(3334)),
(Tag::StripByteCounts, ifd::Value::UnsignedBig(30000)), (Tag::StripByteCounts, ifd::Value::UnsignedBig(30000)),
(Tag::XResolution, ifd::Value::Rational(1, 1)), (Tag::XResolution, ifd::Value::Rational(1, 1)),
(Tag::YResolution, ifd::Value::Rational(1, 1)), (Tag::YResolution, ifd::Value::Rational(1, 1)),
(Tag::ResolutionUnit, ifd::Value::Unsigned(1)), (Tag::ResolutionUnit, ifd::Value::Short(1)),
(Tag::Artist, ifd::Value::Ascii("Image-tiff".into())), (Tag::Artist, ifd::Value::Ascii("Image-tiff".into())),
(Tag::Predictor, ifd::Value::Unsigned(1)), (Tag::Predictor, ifd::Value::Short(1)),
( (
Tag::SampleFormat, Tag::SampleFormat,
ifd::Value::List(vec![ ifd::Value::List(vec![