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 #[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 #[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 #[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 #[inline]
78 #[must_use]
79 pub fn length(&self) -> usize {
80 (self.size() as usize) * (self.count as usize)
81 }
82
83 #[inline]
87 #[must_use]
88 pub fn in_ifd(&self) -> bool {
89 self.length() <= 4
90 }
91
92 pub fn copy_data(&mut self, contents: &[u8]) -> bool {
97 if self.in_ifd() {
98 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}