1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use std::collections::HashMap;
use std::io;
use std::io::Read;
use crate::error::Error;
use crate::isobmff;
use crate::jpeg;
use crate::png;
use crate::tag::Tag;
use crate::tiff;
use crate::tiff::{Field, IfdEntry, In, ProvideUnit};
use crate::webp;
pub struct Reader {
}
impl Reader {
pub fn new() -> Self {
Self {}
}
pub fn read_raw(&self, data: Vec<u8>) -> Result<Exif, Error> {
let mut parser = tiff::Parser::new();
parser.parse(&data)?;
let entry_map = parser.entries.iter().enumerate()
.map(|(i, e)| (e.ifd_num_tag(), i)).collect();
Ok(Exif {
buf: data,
entries: parser.entries,
entry_map: entry_map,
little_endian: parser.little_endian,
})
}
pub fn read_from_container<R>(&self, reader: &mut R) -> Result<Exif, Error>
where R: io::BufRead + io::Seek {
let mut buf = Vec::new();
reader.by_ref().take(4096).read_to_end(&mut buf)?;
if tiff::is_tiff(&buf) {
reader.read_to_end(&mut buf)?;
} else if jpeg::is_jpeg(&buf) {
buf = jpeg::get_exif_attr(&mut buf.chain(reader))?;
} else if png::is_png(&buf) {
buf = png::get_exif_attr(&mut buf.chain(reader))?;
} else if isobmff::is_heif(&buf) {
reader.seek(io::SeekFrom::Start(0))?;
buf = isobmff::get_exif_attr(reader)?;
} else if webp::is_webp(&buf) {
buf = webp::get_exif_attr(&mut buf.chain(reader))?;
} else {
return Err(Error::InvalidFormat("Unknown image format"));
}
self.read_raw(buf)
}
}
pub struct Exif {
buf: Vec<u8>,
entries: Vec<IfdEntry>,
entry_map: HashMap<(In, Tag), usize>,
little_endian: bool,
}
impl Exif {
#[inline]
pub fn buf(&self) -> &[u8] {
&self.buf[..]
}
#[inline]
pub fn fields(&self) -> impl ExactSizeIterator<Item = &Field> {
self.entries.iter()
.map(move |e| e.ref_field(&self.buf, self.little_endian))
}
#[inline]
pub fn little_endian(&self) -> bool {
self.little_endian
}
#[inline]
pub fn get_field(&self, tag: Tag, ifd_num: In) -> Option<&Field> {
self.entry_map.get(&(ifd_num, tag))
.map(|&i| self.entries[i].ref_field(&self.buf, self.little_endian))
}
}
impl<'a> ProvideUnit<'a> for &'a Exif {
fn get_field(self, tag: Tag, ifd_num: In) -> Option<&'a Field> {
self.get_field(tag, ifd_num)
}
}
#[cfg(test)]
mod tests {
use std::fs::File;
use std::io::BufReader;
use crate::tag::Context;
use crate::value::Value;
use super::*;
#[test]
fn get_field() {
let file = File::open("tests/yaminabe.tif").unwrap();
let exif = Reader::new().read_from_container(
&mut BufReader::new(&file)).unwrap();
match exif.get_field(Tag::ImageDescription, In(0)).unwrap().value {
Value::Ascii(ref vec) => assert_eq!(vec, &[b"Test image"]),
ref v => panic!("wrong variant {:?}", v)
}
match exif.get_field(Tag::ImageDescription, In(1)).unwrap().value {
Value::Ascii(ref vec) => assert_eq!(vec, &[b"Test thumbnail"]),
ref v => panic!("wrong variant {:?}", v)
}
match exif.get_field(Tag::ImageDescription, In(2)).unwrap().value {
Value::Ascii(ref vec) => assert_eq!(vec, &[b"Test 2nd IFD"]),
ref v => panic!("wrong variant {:?}", v)
}
}
#[test]
fn display_value_with_unit() {
let file = File::open("tests/yaminabe.tif").unwrap();
let exif = Reader::new().read_from_container(
&mut BufReader::new(&file)).unwrap();
let exifver = exif.get_field(Tag::ExifVersion, In::PRIMARY).unwrap();
assert_eq!(exifver.display_value().with_unit(&exif).to_string(),
"2.31");
let width = exif.get_field(Tag::ImageWidth, In::PRIMARY).unwrap();
assert_eq!(width.display_value().with_unit(&exif).to_string(),
"17 pixels");
let gpsalt = exif.get_field(Tag::GPSAltitude, In::PRIMARY).unwrap();
assert_eq!(gpsalt.display_value().with_unit(&exif).to_string(),
"0.5 meters below sea level");
let xres = exif.get_field(Tag::XResolution, In::PRIMARY).unwrap();
assert_eq!(xres.display_value().with_unit(&exif).to_string(),
"72 pixels per inch");
let gpslat = exif.get_field(Tag::GPSLatitude, In::PRIMARY).unwrap();
assert_eq!(gpslat.display_value().with_unit(&exif).to_string(),
"10 deg 0 min 0 sec [GPSLatitudeRef missing]");
}
#[test]
fn yaminabe() {
let file = File::open("tests/yaminabe.tif").unwrap();
let be = Reader::new().read_from_container(
&mut BufReader::new(&file)).unwrap();
let file = File::open("tests/yaminale.tif").unwrap();
let le = Reader::new().read_from_container(
&mut BufReader::new(&file)).unwrap();
assert!(!be.little_endian());
assert!(le.little_endian());
for exif in &[be, le] {
assert_eq!(exif.fields().len(), 26);
let f = exif.get_field(Tag::ImageWidth, In(0)).unwrap();
assert_eq!(f.display_value().to_string(), "17");
let f = exif.get_field(Tag::Humidity, In(0)).unwrap();
assert_eq!(f.display_value().to_string(), "65");
let f = exif.get_field(Tag(Context::Tiff, 65000), In(0)).unwrap();
match f.value {
Value::Float(ref v) => assert_eq!(v[0], std::f32::MIN),
_ => panic!(),
}
let f = exif.get_field(Tag(Context::Tiff, 65001), In(0)).unwrap();
match f.value {
Value::Double(ref v) => assert_eq!(v[0], std::f64::MIN),
_ => panic!(),
}
}
}
#[test]
fn heif() {
let file = std::fs::File::open("tests/exif.heic").unwrap();
let exif = Reader::new().read_from_container(
&mut std::io::BufReader::new(&file)).unwrap();
assert_eq!(exif.fields().len(), 2);
let exifver = exif.get_field(Tag::ExifVersion, In::PRIMARY).unwrap();
assert_eq!(exifver.display_value().to_string(), "2.31");
}
#[test]
fn png() {
let file = std::fs::File::open("tests/exif.png").unwrap();
let exif = Reader::new().read_from_container(
&mut std::io::BufReader::new(&file)).unwrap();
assert_eq!(exif.fields().len(), 6);
let exifver = exif.get_field(Tag::ExifVersion, In::PRIMARY).unwrap();
assert_eq!(exifver.display_value().to_string(), "2.32");
}
#[test]
fn webp() {
let file = std::fs::File::open("tests/exif.webp").unwrap();
let exif = Reader::new().read_from_container(
&mut std::io::BufReader::new(&file)).unwrap();
assert_eq!(exif.fields().len(), 6);
let exifver = exif.get_field(Tag::ExifVersion, In::PRIMARY).unwrap();
assert_eq!(exifver.display_value().to_string(), "2.32");
let desc = exif.get_field(Tag::ImageDescription, In::PRIMARY).unwrap();
assert_eq!(desc.display_value().to_string(), "\"WebP test\"");
}
}