Skip to content

Commit b13d259

Browse files
authored
Read SignedShort as i16, add TiffFormatError::ShortExpected (#94)
* Add TiffFormatError::ShortExpected enum variant For values expected to be u16. * Allow casting Short u16 values to Vec<u32> Adapted from image-rs/image-tiff@36f8ee0 * Cast SignedShort to i16 instead of i32 Cherry-picked from image-rs/image-tiff@1e3bcf2 * Fixed unit tests that incorrectly decoded SShort as Short * Raise TiffFormatError:ShortExpected in into_u16_vec * Remove sshort comments * More Signed -> SignedShort conversions for reading tags (Case 4)
1 parent 58fcf88 commit b13d259

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

src/metadata/reader.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ async fn read_tag_value<F: MetadataFetch>(
345345
Type::BYTE | Type::UNDEFINED => Value::Byte(data.read_u8()?),
346346
Type::SBYTE => Value::Signed(data.read_i8()? as i32),
347347
Type::SHORT => Value::Short(data.read_u16()?),
348-
Type::SSHORT => Value::Signed(data.read_i16()? as i32),
348+
Type::SSHORT => Value::SignedShort(data.read_i16()?),
349349
Type::LONG => Value::Unsigned(data.read_u32()?),
350350
Type::SLONG => Value::Signed(data.read_i32()?),
351351
Type::FLOAT => Value::Float(data.read_f32()?),
@@ -446,7 +446,7 @@ async fn read_tag_value<F: MetadataFetch>(
446446
Type::SSHORT => {
447447
let mut v = Vec::new();
448448
for _ in 0..count {
449-
v.push(Value::Signed(i32::from(data.read_i16()?)));
449+
v.push(Value::SignedShort(data.read_i16()?));
450450
}
451451
return Ok(Value::List(v));
452452
}
@@ -525,7 +525,7 @@ async fn read_tag_value<F: MetadataFetch>(
525525
Type::SSHORT => {
526526
let mut v = Vec::with_capacity(count as _);
527527
for _ in 0..count {
528-
v.push(Value::Signed(cursor.read_i16().await? as i32))
528+
v.push(Value::SignedShort(cursor.read_i16().await?))
529529
}
530530
Ok(Value::List(v))
531531
}
@@ -657,10 +657,10 @@ mod test {
657657
([1,1, 0, 7, 0,0,0,1, 42, 0, 0, 0], Endianness::BigEndian, Value::Byte (42 )), // undefined
658658
([1,1, 2, 0, 1,0,0,0, 0, 0, 0, 0], Endianness::LittleEndian, Value::Ascii ("".into() )),
659659
([1,1, 0, 2, 0,0,0,1, 0, 0, 0, 0], Endianness::BigEndian, Value::Ascii ("".into() )),
660-
([1,1, 3, 0, 1,0,0,0, 42, 0, 0, 0], Endianness::LittleEndian, Value::Short (42 )),
661-
([1,1, 0, 3, 0,0,0,1, 0,42, 0, 0], Endianness::BigEndian, Value::Short (42 )),
662-
([1,1, 8, 0, 1,0,0,0, 42, 0, 0, 0], Endianness::LittleEndian, Value::Signed (42 )), // signedshort
663-
([1,1, 0, 8, 0,0,0,1, 0,42, 0, 0], Endianness::BigEndian, Value::Signed (42 )), // signedshort
660+
([1,1, 3, 0, 1,0,0,0, 42, 0, 0, 0], Endianness::LittleEndian, Value::Short (42 )),
661+
([1,1, 0, 3, 0,0,0,1, 0,42, 0, 0], Endianness::BigEndian, Value::Short (42 )),
662+
([1,1, 8, 0, 1,0,0,0, 42, 0, 0, 0], Endianness::LittleEndian, Value::SignedShort (42 )),
663+
([1,1, 0, 8, 0,0,0,1, 0,42, 0, 0], Endianness::BigEndian, Value::SignedShort (42 )),
664664
([1,1, 4, 0, 1,0,0,0, 42, 0, 0, 0], Endianness::LittleEndian, Value::Unsigned (42 )),
665665
([1,1, 0, 4, 0,0,0,1, 0, 0, 0,42], Endianness::BigEndian, Value::Unsigned (42 )),
666666
([1,1, 9, 0, 1,0,0,0, 42, 0, 0, 0], Endianness::LittleEndian, Value::Signed (42 )),
@@ -702,8 +702,8 @@ mod test {
702702
([1,1, 0, 2, 0,0,0,0,0,0,0,1, 0, 0, 0, 0, 0, 0, 0, 0], Endianness::BigEndian, Value::Ascii ("".into()) ),
703703
([1,1, 3, 0, 1,0,0,0,0,0,0,0, 42, 0, 0, 0, 0, 0, 0, 0], Endianness::LittleEndian, Value::Short (42) ),
704704
([1,1, 0, 3, 0,0,0,0,0,0,0,1, 0,42, 0, 0, 0, 0, 0, 0], Endianness::BigEndian, Value::Short (42) ),
705-
([1,1, 8, 0, 1,0,0,0,0,0,0,0, 42, 0, 0, 0, 0, 0, 0, 0], Endianness::LittleEndian, Value::Signed (42) ), //sshort
706-
([1,1, 0, 8, 0,0,0,0,0,0,0,1, 0,42, 0, 0, 0, 0, 0, 0], Endianness::BigEndian, Value::Signed (42) ), //sshort
705+
([1,1, 8, 0, 1,0,0,0,0,0,0,0, 42, 0, 0, 0, 0, 0, 0, 0], Endianness::LittleEndian, Value::SignedShort(42) ),
706+
([1,1, 0, 8, 0,0,0,0,0,0,0,1, 0,42, 0, 0, 0, 0, 0, 0], Endianness::BigEndian, Value::SignedShort(42) ),
707707
([1,1, 4, 0, 1,0,0,0,0,0,0,0, 42, 0, 0, 0, 0, 0, 0, 0], Endianness::LittleEndian, Value::Unsigned (42) ),
708708
([1,1, 0, 4, 0,0,0,0,0,0,0,1, 0, 0, 0,42, 0, 0, 0, 0], Endianness::BigEndian, Value::Unsigned (42) ),
709709
([1,1, 9, 0, 1,0,0,0,0,0,0,0, 42, 0, 0, 0, 0, 0, 0, 0], Endianness::LittleEndian, Value::Signed (42) ),
@@ -755,10 +755,10 @@ mod test {
755755
([1,1, 0, 7, 0,0,0,4, 42,42,42,42], Endianness::BigEndian, Value::List(vec![Value::Byte (42); 4]) ), // undefined
756756
([1,1, 2, 0, 4,0,0,0, 42,42,42, 0], Endianness::LittleEndian, Value::Ascii("***".into())),
757757
([1,1, 0, 2, 0,0,0,4, 42,42,42, 0], Endianness::BigEndian, Value::Ascii("***".into())),
758-
([1,1, 3, 0, 2,0,0,0, 42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::Short (42); 2]) ),
759-
([1,1, 0, 3, 0,0,0,2, 0,42, 0,42], Endianness::BigEndian, Value::List(vec![Value::Short (42); 2]) ),
760-
([1,1, 8, 0, 2,0,0,0, 42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::Signed (42); 2]) ), //sshort i16
761-
([1,1, 0, 8, 0,0,0,2, 0,42, 0,42], Endianness::BigEndian, Value::List(vec![Value::Signed (42); 2]) ), //sshort i16
758+
([1,1, 3, 0, 2,0,0,0, 42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::Short (42); 2]) ),
759+
([1,1, 0, 3, 0,0,0,2, 0,42, 0,42], Endianness::BigEndian, Value::List(vec![Value::Short (42); 2]) ),
760+
([1,1, 8, 0, 2,0,0,0, 42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::SignedShort (42); 2]) ),
761+
([1,1, 0, 8, 0,0,0,2, 0,42, 0,42], Endianness::BigEndian, Value::List(vec![Value::SignedShort (42); 2]) ),
762762
([1,1, 0, 2, 0,0,0,4, b'A',b'B',b'C',0], Endianness::BigEndian, Value::Ascii("ABC".into())),
763763
// others don't fit, neither 8-types and we special-case IFD
764764
];
@@ -792,10 +792,10 @@ mod test {
792792
([1,1, 0, 7, 0,0,0,0,0,0,0,8, 42,42,42,42,42,42,42,42], Endianness::BigEndian, Value::List(vec![Value::Byte (42) ; 8])), //undefined u8
793793
([1,1, 2, 0, 8,0,0,0,0,0,0,0, 42,42,42,42,42,42,42, 0], Endianness::LittleEndian, Value::Ascii ("*******".into() )),
794794
([1,1, 0, 2, 0,0,0,0,0,0,0,8, 42,42,42,42,42,42,42, 0], Endianness::BigEndian, Value::Ascii ("*******".into() )),
795-
([1,1, 3, 0, 4,0,0,0,0,0,0,0, 42, 0,42, 0,42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::Short (42) ; 4])),
796-
([1,1, 0, 3, 0,0,0,0,0,0,0,4, 0,42, 0,42, 0,42, 0,42], Endianness::BigEndian, Value::List(vec![Value::Short (42) ; 4])),
797-
([1,1, 8, 0, 4,0,0,0,0,0,0,0, 42, 0,42, 0,42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::Signed (42) ; 4])), //sshort i16
798-
([1,1, 0, 8, 0,0,0,0,0,0,0,4, 0,42, 0,42, 0,42, 0,42], Endianness::BigEndian, Value::List(vec![Value::Signed (42) ; 4])), //sshort i16
795+
([1,1, 3, 0, 4,0,0,0,0,0,0,0, 42, 0,42, 0,42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::Short (42) ; 4])),
796+
([1,1, 0, 3, 0,0,0,0,0,0,0,4, 0,42, 0,42, 0,42, 0,42], Endianness::BigEndian, Value::List(vec![Value::Short (42) ; 4])),
797+
([1,1, 8, 0, 4,0,0,0,0,0,0,0, 42, 0,42, 0,42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::SignedShort (42) ; 4])),
798+
([1,1, 0, 8, 0,0,0,0,0,0,0,4, 0,42, 0,42, 0,42, 0,42], Endianness::BigEndian, Value::List(vec![Value::SignedShort (42) ; 4])),
799799
([1,1, 4, 0, 2,0,0,0,0,0,0,0, 42, 0, 0, 0,42, 0, 0, 0], Endianness::LittleEndian, Value::List(vec![Value::Unsigned (42) ; 2])),
800800
([1,1, 0, 4, 0,0,0,0,0,0,0,2, 0, 0, 0,42, 0, 0, 0,42], Endianness::BigEndian, Value::List(vec![Value::Unsigned (42) ; 2])),
801801
([1,1, 9, 0, 2,0,0,0,0,0,0,0, 42, 0, 0, 0,42, 0, 0, 0], Endianness::LittleEndian, Value::List(vec![Value::Signed (42) ; 2])),
@@ -837,8 +837,8 @@ mod test {
837837
(vec![1,1, 0, 2, 0,0,0,5, 0, 0, 0,12, 42,42,42,42, 0], Endianness::BigEndian , Value::Ascii ("****".into() ) ),
838838
(vec![1,1, 3, 0, 3,0,0,0, 12, 0, 0, 0, 42, 0,42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::Short (42 );3])),
839839
(vec![1,1, 0, 3, 0,0,0,3, 0, 0, 0,12, 0,42, 0,42, 0,42], Endianness::BigEndian , Value::List(vec![Value::Short (42 );3])),
840-
(vec![1,1, 8, 0, 3,0,0,0, 12, 0, 0, 0, 42, 0,42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::Signed (42 );3])), // Type::SSHORT ),
841-
(vec![1,1, 0, 8, 0,0,0,3, 0, 0, 0,12, 0,42, 0,42, 0,42], Endianness::BigEndian , Value::List(vec![Value::Signed (42 );3])), // Type::SSHORT ),
840+
(vec![1,1, 8, 0, 3,0,0,0, 12, 0, 0, 0, 42, 0,42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::SignedShort(42 );3])),
841+
(vec![1,1, 0, 8, 0,0,0,3, 0, 0, 0,12, 0,42, 0,42, 0,42], Endianness::BigEndian , Value::List(vec![Value::SignedShort(42 );3])),
842842
(vec![1,1, 4, 0, 2,0,0,0, 12, 0, 0, 0, 42, 0, 0, 0,42, 0, 0, 0], Endianness::LittleEndian, Value::List(vec![Value::Unsigned (42 );2])),
843843
(vec![1,1, 0, 4, 0,0,0,2, 0, 0, 0,12, 0, 0, 0,42, 0, 0, 0,42], Endianness::BigEndian , Value::List(vec![Value::Unsigned (42 );2])),
844844
(vec![1,1, 9, 0, 2,0,0,0, 12, 0, 0, 0, 42, 0, 0, 0,42, 0, 0, 0], Endianness::LittleEndian, Value::List(vec![Value::Signed (42 );2])),
@@ -891,8 +891,8 @@ mod test {
891891
(vec![1,1, 0, 2, 0,0,0,0,0,0,0,9, 0, 0, 0, 0, 0, 0, 0,20, 42,42,42,42,42,42,42,42, 0], Endianness::BigEndian , Value::Ascii ("********".into() ) ),
892892
(vec![1,1, 3, 0, 5,0,0,0,0,0,0,0, 20, 0, 0, 0, 0, 0, 0, 0, 42, 0,42, 0,42, 0,42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::Short (42 );5])),
893893
(vec![1,1, 0, 3, 0,0,0,0,0,0,0,5, 0, 0, 0, 0, 0, 0, 0,20, 0,42, 0,42, 0,42, 0,42, 0,42], Endianness::BigEndian , Value::List(vec![Value::Short (42 );5])),
894-
(vec![1,1, 8, 0, 5,0,0,0,0,0,0,0, 20, 0, 0, 0, 0, 0, 0, 0, 42, 0,42, 0,42, 0,42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::Signed (42 );5])), //TagType::SSHORT ),
895-
(vec![1,1, 0, 8, 0,0,0,0,0,0,0,5, 0, 0, 0, 0, 0, 0, 0,20, 0,42, 0,42, 0,42, 0,42, 0,42], Endianness::BigEndian , Value::List(vec![Value::Signed (42 );5])), //TagType::SSHORT ),
894+
(vec![1,1, 8, 0, 5,0,0,0,0,0,0,0, 20, 0, 0, 0, 0, 0, 0, 0, 42, 0,42, 0,42, 0,42, 0,42, 0], Endianness::LittleEndian, Value::List(vec![Value::SignedShort(42 );5])),
895+
(vec![1,1, 0, 8, 0,0,0,0,0,0,0,5, 0, 0, 0, 0, 0, 0, 0,20, 0,42, 0,42, 0,42, 0,42, 0,42], Endianness::BigEndian , Value::List(vec![Value::SignedShort(42 );5])),
896896
(vec![1,1, 4, 0, 3,0,0,0,0,0,0,0, 20, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0,42, 0, 0, 0,42, 0, 0, 0], Endianness::LittleEndian, Value::List(vec![Value::Unsigned (42 );3])),
897897
(vec![1,1, 0, 4, 0,0,0,0,0,0,0,3, 0, 0, 0, 0, 0, 0, 0,20, 0, 0, 0,42, 0, 0, 0,42, 0, 0, 0,42], Endianness::BigEndian , Value::List(vec![Value::Unsigned (42 );3])),
898898
(vec![1,1, 9, 0, 3,0,0,0,0,0,0,0, 20, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0,42, 0, 0, 0,42, 0, 0, 0], Endianness::LittleEndian, Value::List(vec![Value::Signed (42 );3])),

src/tiff/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub enum TiffFormatError {
6464
UnknownPlanarConfiguration(u16),
6565
ByteExpected(Value),
6666
SignedByteExpected(Value),
67+
ShortExpected(Value),
6768
SignedShortExpected(Value),
6869
UnsignedIntegerExpected(Value),
6970
SignedIntegerExpected(Value),
@@ -117,6 +118,7 @@ impl fmt::Display for TiffFormatError {
117118
}
118119
ByteExpected(ref val) => write!(fmt, "Expected byte, {:?} found.", val),
119120
SignedByteExpected(ref val) => write!(fmt, "Expected signed byte, {:?} found.", val),
121+
ShortExpected(ref val) => write!(fmt, "Expected short, {:?} found.", val),
120122
SignedShortExpected(ref val) => write!(fmt, "Expected signed short, {:?} found.", val),
121123
UnsignedIntegerExpected(ref val) => {
122124
write!(fmt, "Expected unsigned integer, {:?} found.", val)

src/tiff/ifd.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ impl Value {
5555
Short(val) => Ok(val),
5656
Unsigned(val) => Ok(u16::try_from(val)?),
5757
UnsignedBig(val) => Ok(u16::try_from(val)?),
58-
val => Err(TiffError::FormatError(
59-
TiffFormatError::UnsignedIntegerExpected(val),
60-
)),
58+
val => Err(TiffError::FormatError(TiffFormatError::ShortExpected(val))),
6159
}
6260
}
6361

@@ -159,6 +157,7 @@ impl Value {
159157
}
160158
Ok(new_vec)
161159
}
160+
Short(val) => Ok(vec![val.into()]),
162161
Unsigned(val) => Ok(vec![val]),
163162
UnsignedBig(val) => Ok(vec![u32::try_from(val)?]),
164163
Rational(numerator, denominator) => Ok(vec![numerator, denominator]),
@@ -201,9 +200,7 @@ impl Value {
201200
Ok(new_vec)
202201
}
203202
Short(val) => Ok(vec![val]),
204-
val => Err(TiffError::FormatError(
205-
TiffFormatError::UnsignedIntegerExpected(val),
206-
)),
203+
val => Err(TiffError::FormatError(TiffFormatError::ShortExpected(val))),
207204
}
208205
}
209206

0 commit comments

Comments
 (0)