Skip to content

Commit

Permalink
Add SignedByte and SignedShort enum variants (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiji14 authored Jul 22, 2024
1 parent 0c54a18 commit 307cd8b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/decoder/ifd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -20,6 +20,8 @@ use self::Value::{
pub enum Value {
Byte(u8),
Short(u16),
SignedByte(i8),
SignedShort(i16),
Signed(i32),
SignedBig(i64),
Unsigned(u32),
Expand All @@ -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 {
Expand All @@ -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()),
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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]),
Expand Down Expand Up @@ -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()]),
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub enum TiffFormatError {
UnknownPredictor(u16),
UnknownPlanarConfiguration(u16),
ByteExpected(Value),
SignedByteExpected(Value),
SignedShortExpected(Value),
UnsignedIntegerExpected(Value),
SignedIntegerExpected(Value),
Format(String),
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions tests/encode_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
Expand All @@ -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));
Expand Down
Binary file added tests/images/minisblack-1c-i16b.tiff
Binary file not shown.
Binary file added tests/images/minisblack-1c-i8b.tiff
Binary file not shown.

0 comments on commit 307cd8b

Please sign in to comment.