Skip to main content

simp_rexif/
types_impl.rs

1use super::lowlevel::*;
2use super::types::*;
3use crate::ifdformat::NumArray;
4use std::error::Error;
5use std::fmt::Display;
6use std::{fmt, io};
7
8#[deprecated(note = "Use IfdFormat::new(n)")]
9#[doc(hidden)]
10#[must_use]
11pub fn ifdformat_new(n: u16) -> IfdFormat {
12    IfdFormat::new(n)
13}
14
15impl IfdFormat {
16    /// Convert an IFD format code to the `IfdFormat` enumeration
17    #[must_use]
18    pub const fn new(code: u16) -> Self {
19        match code {
20            1 => Self::U8,
21            2 => Self::Ascii,
22            3 => Self::U16,
23            4 => Self::U32,
24            5 => Self::URational,
25            6 => Self::I8,
26            7 => Self::Undefined,
27            8 => Self::I16,
28            9 => Self::I32,
29            10 => Self::IRational,
30            11 => Self::F32,
31            12 => Self::F64,
32            _ => Self::Unknown,
33        }
34    }
35}
36
37impl IfdEntry {
38    #[deprecated]
39    #[must_use]
40    pub fn data_as_offset(&self) -> usize {
41        self.try_data_as_offset().unwrap()
42    }
43
44    /// Casts IFD entry data into an offset. Not very useful for the crate client.
45    /// The call can't fail, but the caller must be sure that the IFD entry uses
46    /// the IFD data area as an offset (i.e. when the tag is a Sub-IFD tag, or when
47    /// there are more than 4 bytes of data and it would not fit within IFD).
48    #[inline]
49    #[must_use]
50    pub fn try_data_as_offset(&self) -> Option<usize> {
51        read_u32(self.le, &self.ifd_data).map(|l| l as usize)
52    }
53
54    /// Returns the size of an individual element (e.g. U8=1, U16=2...). Every
55    /// IFD entry contains an array of elements, so this is NOT the size of the
56    /// whole entry!
57    #[must_use]
58    pub const fn size(&self) -> u8 {
59        match self.format {
60            IfdFormat::U8 => 1,
61            IfdFormat::Ascii => 1,
62            IfdFormat::U16 => 2,
63            IfdFormat::U32 => 4,
64            IfdFormat::URational => 8,
65            IfdFormat::I8 => 1,
66            IfdFormat::Undefined => 1,
67            IfdFormat::I16 => 2,
68            IfdFormat::I32 => 4,
69            IfdFormat::IRational => 8,
70            IfdFormat::F32 => 4,
71            IfdFormat::F64 => 8,
72            IfdFormat::Unknown => 1,
73        }
74    }
75
76    /// Total length of the whole IFD entry (element count x element size)
77    #[inline]
78    #[must_use]
79    pub fn length(&self) -> usize {
80        (self.size() as usize) * (self.count as usize)
81    }
82
83    /// Returns true if data is contained within the IFD structure, false when
84    /// data can be found elsewhere in the image (and IFD structure contains the
85    /// data offset, instead of data).
86    #[inline]
87    #[must_use]
88    pub fn in_ifd(&self) -> bool {
89        self.length() <= 4
90    }
91
92    /// Copies data from IFD entry section reserved for data (up to 4 bytes), or
93    /// from another part of the image file (when data wouldn't fit in IFD structure).
94    /// In either case, the data member will contain the data of interest after
95    /// this call.
96    pub fn copy_data(&mut self, contents: &[u8]) -> bool {
97        if self.in_ifd() {
98            // the 4 bytes from IFD have all data
99            self.data = self.ifd_data.clone();
100            return true;
101        }
102
103        let offset = match self.try_data_as_offset() {
104            Some(o) => o,
105            _ => return false,
106        };
107        if let Some(ext_data) = contents.get(offset..(offset + self.length())) {
108            self.ext_data.clear();
109            self.ext_data.extend(ext_data);
110            self.data = self.ext_data.clone();
111            return true;
112        }
113        false
114    }
115}
116
117impl Error for ExifError {
118}
119
120impl Display for ExifError {
121    #[cold]
122    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123        match *self {
124            ExifError::IoError(ref e) => e.fmt(f),
125            ExifError::FileTypeUnknown => f.write_str("File type unknown"),
126            ExifError::JpegWithoutExif(ref s) => write!(f, "JPEG without EXIF section: {s}"),
127            ExifError::TiffTruncated => f.write_str("TIFF truncated at start"),
128            ExifError::TiffBadPreamble(ref s) => write!(f, "TIFF with bad preamble: {s}"),
129            ExifError::IfdTruncated => f.write_str("TIFF IFD truncated"),
130            ExifError::ExifIfdTruncated(ref s) => write!(f, "TIFF Exif IFD truncated: {s}"),
131            ExifError::ExifIfdEntryNotFound => f.write_str("TIFF Exif IFD not found"),
132            ExifError::UnsupportedNamespace => f.write_str("Only standar namespace can be serialized"),
133            ExifError::MissingExifOffset => f.write_str("Expected to have seen ExifOffset tagin IFD0"),
134        }
135    }
136}
137
138impl From<io::Error> for ExifError {
139    #[cold]
140    fn from(err: io::Error) -> Self {
141        Self::IoError(err)
142    }
143}
144
145impl fmt::Display for TagValue {
146    #[cold]
147    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148        match self {
149            TagValue::Ascii(s) => f.write_str(s),
150            TagValue::U16(a) => write!(f, "{}", NumArray::new(a)),
151            TagValue::I16(a) => write!(f, "{}", NumArray::new(a)),
152            TagValue::U8(a) => write!(f, "{}", NumArray::new(a)),
153            TagValue::I8(a) => write!(f, "{}", NumArray::new(a)),
154            TagValue::U32(a) => write!(f, "{}", NumArray::new(a)),
155            TagValue::I32(a) => write!(f, "{}", NumArray::new(a)),
156            TagValue::F32(a) => write!(f, "{}", NumArray::new(a)),
157            TagValue::F64(a) => write!(f, "{}", NumArray::new(a)),
158            TagValue::URational(a) => write!(f, "{}", NumArray::new(a)),
159            TagValue::IRational(a) => write!(f, "{}", NumArray::new(a)),
160            TagValue::Undefined(a, _) => write!(f, "{}", NumArray::new(a)),
161            TagValue::Unknown(a, _) => write!(f, "<unknown {}>", NumArray::new(a)),
162            TagValue::Invalid(a, ..) => write!(f, "<invalid {}>", NumArray::new(a)),
163        }
164    }
165}