From 9fee8f63cdf9c2aa9d6e8a4aca04315a14273731 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Mon, 22 Jul 2024 19:22:15 +1200 Subject: [PATCH 1/2] Cast Byte to u8 instead of u32 Also handle casting of Byte (u8) into other unsigned types. --- src/decoder/ifd.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index 9be0d35..6cc2364 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -56,6 +56,7 @@ impl Value { pub fn into_u16(self) -> TiffResult { match self { + Byte(val) => Ok(val.into()), Short(val) => Ok(val), Unsigned(val) => Ok(u16::try_from(val)?), UnsignedBig(val) => Ok(u16::try_from(val)?), @@ -79,6 +80,7 @@ impl Value { pub fn into_u32(self) -> TiffResult { match self { + Byte(val) => Ok(val.into()), Short(val) => Ok(val.into()), Unsigned(val) => Ok(val), UnsignedBig(val) => Ok(u32::try_from(val)?), @@ -104,6 +106,7 @@ impl Value { pub fn into_u64(self) -> TiffResult { match self { + Byte(val) => Ok(val.into()), Short(val) => Ok(val.into()), Unsigned(val) => Ok(val.into()), UnsignedBig(val) => Ok(val), @@ -163,6 +166,7 @@ impl Value { } Ok(new_vec) } + Byte(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]), @@ -204,6 +208,7 @@ impl Value { } Ok(new_vec) } + Byte(val) => Ok(vec![val.into()]), Short(val) => Ok(vec![val]), val => Err(TiffError::FormatError( TiffFormatError::UnsignedIntegerExpected(val), @@ -285,6 +290,7 @@ impl Value { } Ok(new_vec) } + Byte(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()]), @@ -430,7 +436,7 @@ impl Entry { // 2b: the value is at most 4 bytes or doesn't fit in the offset field. return Ok(match self.type_ { - Type::BYTE => Unsigned(u32::from(self.offset[0])), + Type::BYTE => Byte(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()?)), From dd783c36d67ba4a32460aa54d8d6fbfa447c976d Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Mon, 22 Jul 2024 19:24:58 +1200 Subject: [PATCH 2/2] Cast SignedByte to i8 instead of i32 Also handle casting of SignedByte (i8) into other signed types. --- src/decoder/ifd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decoder/ifd.rs b/src/decoder/ifd.rs index 6cc2364..b0e2d1f 100644 --- a/src/decoder/ifd.rs +++ b/src/decoder/ifd.rs @@ -437,7 +437,7 @@ impl Entry { // 2b: the value is at most 4 bytes or doesn't fit in the offset field. return Ok(match self.type_ { Type::BYTE => Byte(self.offset[0]), - Type::SBYTE => Signed(i32::from(self.offset[0] as i8)), + Type::SBYTE => SignedByte(i8::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()?)),