Skip to content

Read Short as u16, SignedShort as i16 #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/decoder/ifd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ impl Value {
Short(val) => Ok(val),
Unsigned(val) => Ok(u16::try_from(val)?),
UnsignedBig(val) => Ok(u16::try_from(val)?),
val => Err(TiffError::FormatError(
TiffFormatError::UnsignedIntegerExpected(val),
)),
val => Err(TiffError::FormatError(TiffFormatError::ShortExpected(val))),
}
}

Expand Down Expand Up @@ -163,6 +161,7 @@ impl Value {
}
Ok(new_vec)
}
Short(val) => Ok(vec![val.into()]),
Unsigned(val) => Ok(vec![val]),
UnsignedBig(val) => Ok(vec![u32::try_from(val)?]),
Rational(numerator, denominator) => Ok(vec![numerator, denominator]),
Expand Down Expand Up @@ -205,9 +204,7 @@ impl Value {
Ok(new_vec)
}
Short(val) => Ok(vec![val]),
val => Err(TiffError::FormatError(
TiffFormatError::UnsignedIntegerExpected(val),
)),
val => Err(TiffError::FormatError(TiffFormatError::ShortExpected(val))),
}
}

Expand Down Expand Up @@ -285,6 +282,7 @@ impl Value {
}
Ok(new_vec)
}
Short(val) => Ok(vec![val.into()]),
Unsigned(val) => Ok(vec![val.into()]),
UnsignedBig(val) => Ok(vec![val]),
Rational(numerator, denominator) => Ok(vec![numerator.into(), denominator.into()]),
Expand Down Expand Up @@ -433,8 +431,8 @@ impl Entry {
Type::BYTE => Unsigned(u32::from(self.offset[0])),
Type::SBYTE => Signed(i32::from(self.offset[0] as i8)),
Type::UNDEFINED => Byte(self.offset[0]),
Type::SHORT => Unsigned(u32::from(self.r(bo).read_u16()?)),
Type::SSHORT => Signed(i32::from(self.r(bo).read_i16()?)),
Type::SHORT => Short(self.r(bo).read_u16()?),
Type::SSHORT => SignedShort(self.r(bo).read_i16()?),
Type::LONG => Unsigned(self.r(bo).read_u32()?),
Type::SLONG => Signed(self.r(bo).read_i32()?),
Type::FLOAT => Float(self.r(bo).read_f32()?),
Expand Down Expand Up @@ -509,7 +507,7 @@ impl Entry {
let mut r = self.r(bo);
let mut v = Vec::new();
for _ in 0..self.count {
v.push(Signed(i32::from(r.read_i16()?)));
v.push(SignedShort(r.read_i16()?));
}
return Ok(List(v));
}
Expand Down Expand Up @@ -569,10 +567,10 @@ impl Entry {
Ok(SignedBig(i64::from(reader.read_i8()?)))
}),
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| {
Ok(SignedBig(i64::from(reader.read_i16()?)))
Ok(SignedShort(reader.read_i16()?))
}),
Type::LONG => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
Ok(Unsigned(reader.read_u32()?))
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub enum TiffFormatError {
UnknownPlanarConfiguration(u16),
ByteExpected(Value),
SignedByteExpected(Value),
ShortExpected(Value),
SignedShortExpected(Value),
UnsignedIntegerExpected(Value),
SignedIntegerExpected(Value),
Expand Down Expand Up @@ -122,6 +123,7 @@ impl fmt::Display for TiffFormatError {
}
ByteExpected(ref val) => write!(fmt, "Expected 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),
UnsignedIntegerExpected(ref val) => {
write!(fmt, "Expected unsigned integer, {:?} found.", val)
Expand Down
32 changes: 16 additions & 16 deletions tests/encode_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,28 @@ fn encode_decode() {
(
Tag::BitsPerSample,
ifd::Value::List(vec![
ifd::Value::UnsignedBig(8),
ifd::Value::UnsignedBig(8),
ifd::Value::UnsignedBig(8)
ifd::Value::Short(8),
ifd::Value::Short(8),
ifd::Value::Short(8)
])
),
(Tag::Compression, ifd::Value::Unsigned(1)),
(Tag::PhotometricInterpretation, ifd::Value::Unsigned(2)),
(Tag::Compression, ifd::Value::Short(1)),
(Tag::PhotometricInterpretation, ifd::Value::Short(2)),
(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::StripByteCounts, ifd::Value::Unsigned(30000)),
(Tag::XResolution, 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::Predictor, ifd::Value::Unsigned(1)),
(Tag::Predictor, ifd::Value::Short(1)),
Comment on lines -58 to +68
Copy link
Contributor Author

@weiji14 weiji14 Mar 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hard part is still this concern:

This makes sense to fix, though the annoying part is figuring out whether anywhere else in the code is relying on the current behavior. Or whether it is likely to break any downstream users and thus might warrant additional caution

Looks like some tests added in #255 around November 2024 (after this PR was opened) exposed a potential area of breakage? Old behaviour is that some of these tags (e.g. Compression, PhotometricInterpretation, SamplesPerPixel, ResolutionUnit, Predictor) were incorrectly decoded as Unsigned (u32), whereas after this PR, they will be decoded as Short (u16).

(
Tag::SampleFormat,
ifd::Value::List(vec![
ifd::Value::UnsignedBig(1),
ifd::Value::UnsignedBig(1),
ifd::Value::UnsignedBig(1)
ifd::Value::Short(1),
ifd::Value::Short(1),
ifd::Value::Short(1)
])
),
]
Expand Down Expand Up @@ -135,17 +135,17 @@ fn encode_decode_big() {
ifd::Value::Short(8)
])
),
(Tag::Compression, ifd::Value::Unsigned(1)),
(Tag::PhotometricInterpretation, ifd::Value::Unsigned(2)),
(Tag::Compression, ifd::Value::Short(1)),
(Tag::PhotometricInterpretation, ifd::Value::Short(2)),
(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::StripByteCounts, ifd::Value::UnsignedBig(30000)),
(Tag::XResolution, 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::Predictor, ifd::Value::Unsigned(1)),
(Tag::Predictor, ifd::Value::Short(1)),
(
Tag::SampleFormat,
ifd::Value::List(vec![
Expand Down
Loading