|
| 1 | +// |
| 2 | +// Copyright (c) 2017 KAMADA Ken'ichi. |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions |
| 7 | +// are met: |
| 8 | +// 1. Redistributions of source code must retain the above copyright |
| 9 | +// notice, this list of conditions and the following disclaimer. |
| 10 | +// 2. Redistributions in binary form must reproduce the above copyright |
| 11 | +// notice, this list of conditions and the following disclaimer in the |
| 12 | +// documentation and/or other materials provided with the distribution. |
| 13 | +// |
| 14 | +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 15 | +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 18 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 20 | +// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 21 | +// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 22 | +// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 23 | +// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 24 | +// SUCH DAMAGE. |
| 25 | +// |
| 26 | + |
| 27 | +use std::collections::HashMap; |
| 28 | + |
| 29 | +use crate::tag::Tag; |
| 30 | +use crate::tiff::{Field, IfdEntry, In, ProvideUnit}; |
| 31 | + |
| 32 | +/// A struct that holds the parsed Exif attributes. |
| 33 | +/// |
| 34 | +/// # Examples |
| 35 | +/// ``` |
| 36 | +/// # fn main() { sub(); } |
| 37 | +/// # fn sub() -> Option<()> { |
| 38 | +/// # use exif::{In, Reader, Tag}; |
| 39 | +/// # let file = std::fs::File::open("tests/exif.jpg").unwrap(); |
| 40 | +/// # let exif = Reader::new().read_from_container( |
| 41 | +/// # &mut std::io::BufReader::new(&file)).unwrap(); |
| 42 | +/// // Get a specific field. |
| 43 | +/// let xres = exif.get_field(Tag::XResolution, In::PRIMARY)?; |
| 44 | +/// assert_eq!(xres.display_value().with_unit(&exif).to_string(), |
| 45 | +/// "72 pixels per inch"); |
| 46 | +/// // Iterate over all fields. |
| 47 | +/// for f in exif.fields() { |
| 48 | +/// println!("{} {} {}", f.tag, f.ifd_num, f.display_value()); |
| 49 | +/// } |
| 50 | +/// # Some(()) } |
| 51 | +/// ``` |
| 52 | +pub struct Exif { |
| 53 | + // TIFF data. |
| 54 | + buf: Vec<u8>, |
| 55 | + // Exif fields. Vec is used to keep the ability to enumerate all fields |
| 56 | + // even if there are duplicates. |
| 57 | + entries: Vec<IfdEntry>, |
| 58 | + // HashMap to the index of the Vec for faster random access. |
| 59 | + entry_map: HashMap<(In, Tag), usize>, |
| 60 | + // True if the TIFF data is little endian. |
| 61 | + little_endian: bool, |
| 62 | +} |
| 63 | + |
| 64 | +impl Exif { |
| 65 | + /// Constructs a new `Exif`. |
| 66 | + pub(crate) fn new(buf: Vec<u8>, |
| 67 | + entries: Vec<IfdEntry>, little_endian: bool) -> Self { |
| 68 | + let entry_map = entries.iter().enumerate() |
| 69 | + .map(|(i, e)| (e.ifd_num_tag(), i)).collect(); |
| 70 | + Self { |
| 71 | + buf: buf, |
| 72 | + entries: entries, |
| 73 | + entry_map: entry_map, |
| 74 | + little_endian: little_endian, |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// Returns the slice that contains the TIFF data. |
| 79 | + #[inline] |
| 80 | + pub fn buf(&self) -> &[u8] { |
| 81 | + &self.buf[..] |
| 82 | + } |
| 83 | + |
| 84 | + /// Returns an iterator of Exif fields. |
| 85 | + #[inline] |
| 86 | + pub fn fields(&self) -> impl ExactSizeIterator<Item = &Field> { |
| 87 | + self.entries.iter() |
| 88 | + .map(move |e| e.ref_field(&self.buf, self.little_endian)) |
| 89 | + } |
| 90 | + |
| 91 | + /// Returns true if the Exif data (TIFF structure) is in the |
| 92 | + /// little-endian byte order. |
| 93 | + #[inline] |
| 94 | + pub fn little_endian(&self) -> bool { |
| 95 | + self.little_endian |
| 96 | + } |
| 97 | + |
| 98 | + /// Returns a reference to the Exif field specified by the tag |
| 99 | + /// and the IFD number. |
| 100 | + #[inline] |
| 101 | + pub fn get_field(&self, tag: Tag, ifd_num: In) -> Option<&Field> { |
| 102 | + self.entry_map.get(&(ifd_num, tag)) |
| 103 | + .map(|&i| self.entries[i].ref_field(&self.buf, self.little_endian)) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl<'a> ProvideUnit<'a> for &'a Exif { |
| 108 | + fn get_field(self, tag: Tag, ifd_num: In) -> Option<&'a Field> { |
| 109 | + self.get_field(tag, ifd_num) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +#[cfg(test)] |
| 114 | +mod tests { |
| 115 | + use std::fs::File; |
| 116 | + use std::io::BufReader; |
| 117 | + use crate::reader::Reader; |
| 118 | + use crate::value::Value; |
| 119 | + use super::*; |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn get_field() { |
| 123 | + let file = File::open("tests/yaminabe.tif").unwrap(); |
| 124 | + let exif = Reader::new().read_from_container( |
| 125 | + &mut BufReader::new(&file)).unwrap(); |
| 126 | + match exif.get_field(Tag::ImageDescription, In(0)).unwrap().value { |
| 127 | + Value::Ascii(ref vec) => assert_eq!(vec, &[b"Test image"]), |
| 128 | + ref v => panic!("wrong variant {:?}", v) |
| 129 | + } |
| 130 | + match exif.get_field(Tag::ImageDescription, In(1)).unwrap().value { |
| 131 | + Value::Ascii(ref vec) => assert_eq!(vec, &[b"Test thumbnail"]), |
| 132 | + ref v => panic!("wrong variant {:?}", v) |
| 133 | + } |
| 134 | + match exif.get_field(Tag::ImageDescription, In(2)).unwrap().value { |
| 135 | + Value::Ascii(ref vec) => assert_eq!(vec, &[b"Test 2nd IFD"]), |
| 136 | + ref v => panic!("wrong variant {:?}", v) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn display_value_with_unit() { |
| 142 | + let file = File::open("tests/yaminabe.tif").unwrap(); |
| 143 | + let exif = Reader::new().read_from_container( |
| 144 | + &mut BufReader::new(&file)).unwrap(); |
| 145 | + // No unit. |
| 146 | + let exifver = exif.get_field(Tag::ExifVersion, In::PRIMARY).unwrap(); |
| 147 | + assert_eq!(exifver.display_value().with_unit(&exif).to_string(), |
| 148 | + "2.31"); |
| 149 | + // Fixed string. |
| 150 | + let width = exif.get_field(Tag::ImageWidth, In::PRIMARY).unwrap(); |
| 151 | + assert_eq!(width.display_value().with_unit(&exif).to_string(), |
| 152 | + "17 pixels"); |
| 153 | + // Unit tag (with a non-default value). |
| 154 | + let gpsalt = exif.get_field(Tag::GPSAltitude, In::PRIMARY).unwrap(); |
| 155 | + assert_eq!(gpsalt.display_value().with_unit(&exif).to_string(), |
| 156 | + "0.5 meters below sea level"); |
| 157 | + // Unit tag is missing but the default is specified. |
| 158 | + let xres = exif.get_field(Tag::XResolution, In::PRIMARY).unwrap(); |
| 159 | + assert_eq!(xres.display_value().with_unit(&exif).to_string(), |
| 160 | + "72 pixels per inch"); |
| 161 | + // Unit tag is missing and the default is not specified. |
| 162 | + let gpslat = exif.get_field(Tag::GPSLatitude, In::PRIMARY).unwrap(); |
| 163 | + assert_eq!(gpslat.display_value().with_unit(&exif).to_string(), |
| 164 | + "10 deg 0 min 0 sec [GPSLatitudeRef missing]"); |
| 165 | + } |
| 166 | +} |
0 commit comments