Add SignedByte and SignedShort enum variants (#234)

This commit is contained in:
Wei Ji authored and GitHub committed 2024-07-21 20:37:25 -07:00
1 parent 0c54a18e21
commit 307cd8be97
5 files changed
+47 -1

No files matched your search

+31 -1
View File
@@ -11,7 +11,7 @@ use crate::{TiffError, TiffFormatError, TiffResult};
use self::Value::{
Ascii, Byte, Double, Float, Ifd, IfdBig, List, Rational, RationalBig, SRational, SRationalBig,
Short, Signed, SignedBig, Unsigned, UnsignedBig,
Short, Signed, SignedBig, SignedByte, SignedShort, Unsigned, UnsignedBig,
};
#[allow(unused_qualifications)]
@@ -20,6 +20,8 @@ use self::Value::{
pub enum Value {
Byte(u8),
Short(u16),
SignedByte(i8),
SignedShort(i16),
Signed(i32),
SignedBig(i64),
Unsigned(u32),
@@ -43,6 +45,14 @@ impl Value {
val => Err(TiffError::FormatError(TiffFormatError::ByteExpected(val))),
}
}
pub fn into_i8(self) -> TiffResult<i8> {
match self {
SignedByte(val) => Ok(val),
val => Err(TiffError::FormatError(TiffFormatError::SignedByteExpected(
val,
))),
}
}
pub fn into_u16(self) -> TiffResult<u16> {
match self {
@@ -55,6 +65,18 @@ impl Value {
}
}
pub fn into_i16(self) -> TiffResult<i16> {
match self {
SignedByte(val) => Ok(val.into()),
SignedShort(val) => Ok(val),
Signed(val) => Ok(i16::try_from(val)?),
SignedBig(val) => Ok(i16::try_from(val)?),
val => Err(TiffError::FormatError(
TiffFormatError::SignedShortExpected(val),
)),
}
}
pub fn into_u32(self) -> TiffResult<u32> {
match self {
Short(val) => Ok(val.into()),
@@ -70,6 +92,8 @@ impl Value {
pub fn into_i32(self) -> TiffResult<i32> {
match self {
SignedByte(val) => Ok(val.into()),
SignedShort(val) => Ok(val.into()),
Signed(val) => Ok(val),
SignedBig(val) => Ok(i32::try_from(val)?),
val => Err(TiffError::FormatError(
@@ -93,6 +117,8 @@ impl Value {
pub fn into_i64(self) -> TiffResult<i64> {
match self {
SignedByte(val) => Ok(val.into()),
SignedShort(val) => Ok(val.into()),
Signed(val) => Ok(val.into()),
SignedBig(val) => Ok(val),
val => Err(TiffError::FormatError(
@@ -204,6 +230,8 @@ impl Value {
}
Ok(new_vec)
}
SignedByte(val) => Ok(vec![val.into()]),
SignedShort(val) => Ok(vec![val.into()]),
Signed(val) => Ok(vec![val]),
SignedBig(val) => Ok(vec![i32::try_from(val)?]),
SRational(numerator, denominator) => Ok(vec![numerator, denominator]),
@@ -289,6 +317,8 @@ impl Value {
}
Ok(new_vec)
}
SignedByte(val) => Ok(vec![val.into()]),
SignedShort(val) => Ok(vec![val.into()]),
Signed(val) => Ok(vec![val.into()]),
SignedBig(val) => Ok(vec![val]),
SRational(numerator, denominator) => Ok(vec![numerator.into(), denominator.into()]),
+4
View File
@@ -68,6 +68,8 @@ pub enum TiffFormatError {
UnknownPredictor(u16),
UnknownPlanarConfiguration(u16),
ByteExpected(Value),
SignedByteExpected(Value),
SignedShortExpected(Value),
UnsignedIntegerExpected(Value),
SignedIntegerExpected(Value),
Format(String),
@@ -119,6 +121,8 @@ impl fmt::Display for TiffFormatError {
write!(fmt, "Unknown planar configuration “{}” encountered", planar_config)
}
ByteExpected(ref val) => write!(fmt, "Expected byte, {:?} found.", val),
SignedByteExpected(ref val) => write!(fmt, "Expected signed byte, {:?} found.", val),
SignedShortExpected(ref val) => write!(fmt, "Expected signed short, {:?} found.", val),
UnsignedIntegerExpected(ref val) => {
write!(fmt, "Expected unsigned integer, {:?} found.", val)
}
+12
View File
@@ -188,7 +188,9 @@ macro_rules! test_roundtrip {
}
test_roundtrip!(test_u8_roundtrip, U8, u8);
test_roundtrip!(test_i8_roundtrip, I8, i8);
test_roundtrip!(test_u16_roundtrip, U16, u16);
test_roundtrip!(test_i16_roundtrip, I16, i16);
test_roundtrip!(test_u32_roundtrip, U32, u32);
test_roundtrip!(test_u64_roundtrip, U64, u64);
test_roundtrip!(test_f32_roundtrip, F32, f32);
@@ -199,6 +201,11 @@ fn test_gray_u8_roundtrip() {
test_u8_roundtrip::<colortype::Gray8>("minisblack-1c-8b.tiff", ColorType::Gray(8));
}
#[test]
fn test_gray_i8_roundtrip() {
test_i8_roundtrip::<colortype::GrayI8>("minisblack-1c-i8b.tiff", ColorType::Gray(8));
}
#[test]
fn test_rgb_u8_roundtrip() {
test_u8_roundtrip::<colortype::RGB8>("rgb-3c-8b.tiff", ColorType::RGB(8));
@@ -214,6 +221,11 @@ fn test_gray_u16_roundtrip() {
test_u16_roundtrip::<colortype::Gray16>("minisblack-1c-16b.tiff", ColorType::Gray(16));
}
#[test]
fn test_gray_i16_roundtrip() {
test_i16_roundtrip::<colortype::GrayI16>("minisblack-1c-i16b.tiff", ColorType::Gray(16));
}
#[test]
fn test_rgb_u16_roundtrip() {
test_u16_roundtrip::<colortype::RGB16>("rgb-3c-16b.tiff", ColorType::RGB(16));
Binary file not shown.
Binary file not shown.