Skip to content

Commit aeab243

Browse files
authored
Read Short as u16, SignedShort as i16 (#235)
1 parent 36f7b3b commit aeab243

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

src/decoder/ifd.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ impl Value {
5959
Short(val) => Ok(val),
6060
Unsigned(val) => Ok(u16::try_from(val)?),
6161
UnsignedBig(val) => Ok(u16::try_from(val)?),
62-
val => Err(TiffError::FormatError(
63-
TiffFormatError::UnsignedIntegerExpected(val),
64-
)),
62+
val => Err(TiffError::FormatError(TiffFormatError::ShortExpected(val))),
6563
}
6664
}
6765

@@ -163,6 +161,7 @@ impl Value {
163161
}
164162
Ok(new_vec)
165163
}
164+
Short(val) => Ok(vec![val.into()]),
166165
Unsigned(val) => Ok(vec![val]),
167166
UnsignedBig(val) => Ok(vec![u32::try_from(val)?]),
168167
Rational(numerator, denominator) => Ok(vec![numerator, denominator]),
@@ -205,9 +204,7 @@ impl Value {
205204
Ok(new_vec)
206205
}
207206
Short(val) => Ok(vec![val]),
208-
val => Err(TiffError::FormatError(
209-
TiffFormatError::UnsignedIntegerExpected(val),
210-
)),
207+
val => Err(TiffError::FormatError(TiffFormatError::ShortExpected(val))),
211208
}
212209
}
213210

@@ -285,6 +282,7 @@ impl Value {
285282
}
286283
Ok(new_vec)
287284
}
285+
Short(val) => Ok(vec![val.into()]),
288286
Unsigned(val) => Ok(vec![val.into()]),
289287
UnsignedBig(val) => Ok(vec![val]),
290288
Rational(numerator, denominator) => Ok(vec![numerator.into(), denominator.into()]),
@@ -433,8 +431,8 @@ impl Entry {
433431
Type::BYTE => Unsigned(u32::from(self.offset[0])),
434432
Type::SBYTE => Signed(i32::from(self.offset[0] as i8)),
435433
Type::UNDEFINED => Byte(self.offset[0]),
436-
Type::SHORT => Unsigned(u32::from(self.r(bo).read_u16()?)),
437-
Type::SSHORT => Signed(i32::from(self.r(bo).read_i16()?)),
434+
Type::SHORT => Short(self.r(bo).read_u16()?),
435+
Type::SSHORT => SignedShort(self.r(bo).read_i16()?),
438436
Type::LONG => Unsigned(self.r(bo).read_u32()?),
439437
Type::SLONG => Signed(self.r(bo).read_i32()?),
440438
Type::FLOAT => Float(self.r(bo).read_f32()?),
@@ -509,7 +507,7 @@ impl Entry {
509507
let mut r = self.r(bo);
510508
let mut v = Vec::new();
511509
for _ in 0..self.count {
512-
v.push(Signed(i32::from(r.read_i16()?)));
510+
v.push(SignedShort(r.read_i16()?));
513511
}
514512
return Ok(List(v));
515513
}
@@ -569,10 +567,10 @@ impl Entry {
569567
Ok(SignedBig(i64::from(reader.read_i8()?)))
570568
}),
571569
Type::SHORT => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
572-
Ok(UnsignedBig(u64::from(reader.read_u16()?)))
570+
Ok(Short(reader.read_u16()?))
573571
}),
574572
Type::SSHORT => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
575-
Ok(SignedBig(i64::from(reader.read_i16()?)))
573+
Ok(SignedShort(reader.read_i16()?))
576574
}),
577575
Type::LONG => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
578576
Ok(Unsigned(reader.read_u32()?))

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub enum TiffFormatError {
6969
UnknownPlanarConfiguration(u16),
7070
ByteExpected(Value),
7171
SignedByteExpected(Value),
72+
ShortExpected(Value),
7273
SignedShortExpected(Value),
7374
UnsignedIntegerExpected(Value),
7475
SignedIntegerExpected(Value),
@@ -122,6 +123,7 @@ impl fmt::Display for TiffFormatError {
122123
}
123124
ByteExpected(ref val) => write!(fmt, "Expected byte, {:?} found.", val),
124125
SignedByteExpected(ref val) => write!(fmt, "Expected signed byte, {:?} found.", val),
126+
ShortExpected(ref val) => write!(fmt, "Expected short, {:?} found.", val),
125127
SignedShortExpected(ref val) => write!(fmt, "Expected signed short, {:?} found.", val),
126128
UnsignedIntegerExpected(ref val) => {
127129
write!(fmt, "Expected unsigned integer, {:?} found.", val)

tests/encode_images.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,28 @@ fn encode_decode() {
5050
(
5151
Tag::BitsPerSample,
5252
ifd::Value::List(vec![
53-
ifd::Value::UnsignedBig(8),
54-
ifd::Value::UnsignedBig(8),
55-
ifd::Value::UnsignedBig(8)
53+
ifd::Value::Short(8),
54+
ifd::Value::Short(8),
55+
ifd::Value::Short(8)
5656
])
5757
),
58-
(Tag::Compression, ifd::Value::Unsigned(1)),
59-
(Tag::PhotometricInterpretation, ifd::Value::Unsigned(2)),
58+
(Tag::Compression, ifd::Value::Short(1)),
59+
(Tag::PhotometricInterpretation, ifd::Value::Short(2)),
6060
(Tag::StripOffsets, ifd::Value::Unsigned(8)),
61-
(Tag::SamplesPerPixel, ifd::Value::Unsigned(3)),
61+
(Tag::SamplesPerPixel, ifd::Value::Short(3)),
6262
(Tag::RowsPerStrip, ifd::Value::Unsigned(3334)),
6363
(Tag::StripByteCounts, ifd::Value::Unsigned(30000)),
6464
(Tag::XResolution, ifd::Value::Rational(1, 1)),
6565
(Tag::YResolution, ifd::Value::Rational(1, 1)),
66-
(Tag::ResolutionUnit, ifd::Value::Unsigned(1)),
66+
(Tag::ResolutionUnit, ifd::Value::Short(1)),
6767
(Tag::Artist, ifd::Value::Ascii("Image-tiff".into())),
68-
(Tag::Predictor, ifd::Value::Unsigned(1)),
68+
(Tag::Predictor, ifd::Value::Short(1)),
6969
(
7070
Tag::SampleFormat,
7171
ifd::Value::List(vec![
72-
ifd::Value::UnsignedBig(1),
73-
ifd::Value::UnsignedBig(1),
74-
ifd::Value::UnsignedBig(1)
72+
ifd::Value::Short(1),
73+
ifd::Value::Short(1),
74+
ifd::Value::Short(1)
7575
])
7676
),
7777
]
@@ -135,17 +135,17 @@ fn encode_decode_big() {
135135
ifd::Value::Short(8)
136136
])
137137
),
138-
(Tag::Compression, ifd::Value::Unsigned(1)),
139-
(Tag::PhotometricInterpretation, ifd::Value::Unsigned(2)),
138+
(Tag::Compression, ifd::Value::Short(1)),
139+
(Tag::PhotometricInterpretation, ifd::Value::Short(2)),
140140
(Tag::StripOffsets, ifd::Value::UnsignedBig(16)),
141-
(Tag::SamplesPerPixel, ifd::Value::Unsigned(3)),
141+
(Tag::SamplesPerPixel, ifd::Value::Short(3)),
142142
(Tag::RowsPerStrip, ifd::Value::Unsigned(3334)),
143143
(Tag::StripByteCounts, ifd::Value::UnsignedBig(30000)),
144144
(Tag::XResolution, ifd::Value::Rational(1, 1)),
145145
(Tag::YResolution, ifd::Value::Rational(1, 1)),
146-
(Tag::ResolutionUnit, ifd::Value::Unsigned(1)),
146+
(Tag::ResolutionUnit, ifd::Value::Short(1)),
147147
(Tag::Artist, ifd::Value::Ascii("Image-tiff".into())),
148-
(Tag::Predictor, ifd::Value::Unsigned(1)),
148+
(Tag::Predictor, ifd::Value::Short(1)),
149149
(
150150
Tag::SampleFormat,
151151
ifd::Value::List(vec![

0 commit comments

Comments
 (0)