Move everything to the tags macro

This commit is contained in:
Andreas Molzer committed 2019-11-10 18:02:42 +01:00
1 parent f80f4a06d5
commit f88c9f00c3
2 files changed
+61 -15

No files matched your search

+3 -3
View File
@@ -13,7 +13,7 @@ macro_rules! tags {
{ {
// Permit arbitrary meta items, which include documentation. // Permit arbitrary meta items, which include documentation.
$( #[$enum_attr:meta] )* $( #[$enum_attr:meta] )*
pub enum $name:ident($ty:ty) $(unknown($unknown_doc:literal))* { $vis:vis enum $name:ident($ty:ty) $(unknown($unknown_doc:literal))* {
// Each of the `Name = Val,` permitting documentation. // Each of the `Name = Val,` permitting documentation.
$($(#[$ident_attr:meta])* $tag:ident = $val:expr,)* $($(#[$ident_attr:meta])* $tag:ident = $val:expr,)*
} }
@@ -33,14 +33,14 @@ macro_rules! tags {
impl $name { impl $name {
#[inline(always)] #[inline(always)]
fn __from_inner_type(n: u16) -> Result<Self, $ty> { fn __from_inner_type(n: $ty) -> Result<Self, $ty> {
$(if n == $val { Ok($name::$tag) } else)* { $(if n == $val { Ok($name::$tag) } else)* {
Err(n) Err(n)
} }
} }
#[inline(always)] #[inline(always)]
fn __to_inner_type(&self) -> u16 { fn __to_inner_type(&self) -> $ty {
match *self { match *self {
$( $name::$tag => $val, )* $( $name::$tag => $val, )*
$( $name::Unknown(n) => { $unknown_doc; n }, )* $( $name::Unknown(n) => { $unknown_doc; n }, )*
+58 -12
View File
@@ -1,4 +1,4 @@
use num_traits::{FromPrimitive, Num}; use num_traits::{Num};
use std::collections::HashMap; use std::collections::HashMap;
use std::io::{self, Read, Seek}; use std::io::{self, Read, Seek};
use std::cmp; use std::cmp;
@@ -9,6 +9,7 @@ use self::ifd::Directory;
use self::stream::{ByteOrder, EndianReader, LZWReader, DeflateReader, PackBitsReader, SmartReader}; use self::stream::{ByteOrder, EndianReader, LZWReader, DeflateReader, PackBitsReader, SmartReader};
#[macro_use]
pub mod ifd; pub mod ifd;
mod stream; mod stream;
@@ -80,8 +81,8 @@ impl<'a> DecodingBuffer<'a> {
} }
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, FromPrimitive)] tags! {
pub enum PhotometricInterpretation { pub enum PhotometricInterpretation(u32) {
WhiteIsZero = 0, WhiteIsZero = 0,
BlackIsZero = 1, BlackIsZero = 1,
RGB = 2, RGB = 2,
@@ -91,9 +92,10 @@ pub enum PhotometricInterpretation {
YCbCr = 6, YCbCr = 6,
CIELab = 8, CIELab = 8,
} }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, FromPrimitive)] tags! {
pub enum CompressionMethod { pub enum CompressionMethod(u32) {
None = 1, None = 1,
Huffman = 2, Huffman = 2,
Fax3 = 3, Fax3 = 3,
@@ -104,18 +106,61 @@ pub enum CompressionMethod {
OldDeflate = 0x80B2, OldDeflate = 0x80B2,
PackBits = 0x8005, PackBits = 0x8005,
} }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, FromPrimitive)] tags! {
pub enum PlanarConfiguration { pub enum PlanarConfiguration(u32) {
Chunky = 1, Chunky = 1,
Planar = 2, Planar = 2,
} }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, FromPrimitive)] tags! {
enum Predictor { enum Predictor(u32) {
None = 1, None = 1,
Horizontal = 2, Horizontal = 2,
} }
}
impl PhotometricInterpretation {
pub fn from_u32(val: u32) -> Option<Self> {
Self::__from_inner_type(val).ok()
}
pub fn to_u32(&self) -> u32 {
Self::__to_inner_type(self)
}
}
impl CompressionMethod {
pub fn from_u32(val: u32) -> Option<Self> {
Self::__from_inner_type(val).ok()
}
pub fn to_u32(&self) -> u32 {
Self::__to_inner_type(self)
}
}
impl PlanarConfiguration {
pub fn from_u32(val: u32) -> Option<Self> {
Self::__from_inner_type(val).ok()
}
pub fn to_u32(&self) -> u32 {
Self::__to_inner_type(self)
}
}
impl Predictor {
pub fn from_u32(val: u32) -> Option<Self> {
Self::__from_inner_type(val).ok()
}
pub fn to_u32(&self) -> u32 {
Self::__to_inner_type(self)
}
}
#[derive(Debug)] #[derive(Debug)]
struct StripDecodeState { struct StripDecodeState {
@@ -334,7 +379,7 @@ impl<R: Read + Seek> Decoder<R> {
self.width = self.get_tag_u32(ifd::Tag::ImageWidth)?; self.width = self.get_tag_u32(ifd::Tag::ImageWidth)?;
self.height = self.get_tag_u32(ifd::Tag::ImageLength)?; self.height = self.get_tag_u32(ifd::Tag::ImageLength)?;
self.strip_decoder = None; self.strip_decoder = None;
self.photometric_interpretation = match FromPrimitive::from_u32( self.photometric_interpretation = match PhotometricInterpretation::from_u32(
self.get_tag_u32(ifd::Tag::PhotometricInterpretation)? self.get_tag_u32(ifd::Tag::PhotometricInterpretation)?
) { ) {
Some(val) => val, Some(val) => val,
@@ -345,7 +390,7 @@ impl<R: Read + Seek> Decoder<R> {
} }
}; };
if let Some(val) = self.find_tag_u32(ifd::Tag::Compression)? { if let Some(val) = self.find_tag_u32(ifd::Tag::Compression)? {
match FromPrimitive::from_u32(val) { match CompressionMethod::from_u32(val) {
Some(method) => self.compression_method = method, Some(method) => self.compression_method = method,
None => { None => {
return Err(TiffError::UnsupportedError( return Err(TiffError::UnsupportedError(
@@ -720,7 +765,7 @@ impl<R: Read + Seek> Decoder<R> {
)); ));
} }
if let Ok(predictor) = self.get_tag_u32(ifd::Tag::Predictor) { if let Ok(predictor) = self.get_tag_u32(ifd::Tag::Predictor) {
match FromPrimitive::from_u32(predictor) { match Predictor::from_u32(predictor) {
Some(Predictor::None) => (), Some(Predictor::None) => (),
Some(Predictor::Horizontal) => { Some(Predictor::Horizontal) => {
rev_hpredict( rev_hpredict(
@@ -734,6 +779,7 @@ impl<R: Read + Seek> Decoder<R> {
predictor, predictor,
))) )))
} }
Some(Predictor::__NonExhaustive) => unreachable!(),
} }
} }
Ok(()) Ok(())