From 6e61de81b695027dc4658b525cc393120181f5d6 Mon Sep 17 00:00:00 2001 From: Andrew Champion Date: Wed, 11 Dec 2019 12:56:11 +0000 Subject: [PATCH] Simplify color type matching --- src/decoder/mod.rs | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index 933d6c1..fd75e1c 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -352,30 +352,24 @@ impl Decoder { pub fn colortype(&mut self) -> TiffResult { match self.photometric_interpretation { // TODO: catch also [ 8, 8, 8, _] this does not work due to a bug in rust atm - PhotometricInterpretation::RGB if self.bits_per_sample == [8, 8, 8, 8] => { - Ok(ColorType::RGBA(8)) - } - PhotometricInterpretation::RGB if self.bits_per_sample == [8, 8, 8] => { - Ok(ColorType::RGB(8)) - } - PhotometricInterpretation::RGB if self.bits_per_sample == [16, 16, 16, 16] => { - Ok(ColorType::RGBA(16)) - } - PhotometricInterpretation::RGB if self.bits_per_sample == [16, 16, 16] => { - Ok(ColorType::RGB(16)) - } - PhotometricInterpretation::RGB if self.bits_per_sample == [32, 32, 32, 32] => { - Ok(ColorType::RGBA(32)) - } - PhotometricInterpretation::RGB if self.bits_per_sample == [32, 32, 32] => { - Ok(ColorType::RGB(32)) - } - PhotometricInterpretation::RGB if self.bits_per_sample == [64, 64, 64, 64] => { - Ok(ColorType::RGBA(64)) - } - PhotometricInterpretation::RGB if self.bits_per_sample == [64, 64, 64] => { - Ok(ColorType::RGB(64)) - } + PhotometricInterpretation::RGB => { + match self.bits_per_sample.as_slice() { + [8, 8, 8] => Ok(ColorType::RGB(8)), + [8, 8, 8, 8] => Ok(ColorType::RGBA(8)), + [16, 16, 16] => Ok(ColorType::RGB(16)), + [16, 16, 16, 16] => Ok(ColorType::RGBA(16)), + [32, 32, 32] => Ok(ColorType::RGB(32)), + [32, 32, 32, 32] => Ok(ColorType::RGBA(32)), + [64, 64, 64] => Ok(ColorType::RGB(64)), + [64, 64, 64, 64] => Ok(ColorType::RGBA(64)), + _ => Err(TiffError::UnsupportedError( + TiffUnsupportedError::InterpretationWithBits( + self.photometric_interpretation, + self.bits_per_sample.clone(), + ), + )), + } + }, PhotometricInterpretation::CMYK if self.bits_per_sample == [8, 8, 8, 8] => { Ok(ColorType::CMYK(8)) }