| 1 | use core::fmt; |
| 2 | use std::{collections::BTreeMap, num::NonZeroU64}; |
| 3 | |
| 4 | use crate::{ |
| 5 | decoder::ifd::Entry, |
| 6 | tags::{IfdPointer, Tag}, |
| 7 | }; |
| 8 | |
| 9 | /// An Image File Directory (IFD). |
| 10 | /// |
| 11 | /// A directory is a map of [`Tag`]s to [`Value`](crate::decoder::ifd::Value)s. The values are |
| 12 | /// stored anywhere in the file, with the directory containing the offsets and length of the |
| 13 | /// associated values for each tag present in the directory. |
| 14 | /// |
| 15 | /// A directory can be created with with |
| 16 | /// [`Decoder::read_directory`](crate::decoder::Decoder::read_directory) or as an empty directory |
| 17 | /// to be extended with entries. A directory may be used with |
| 18 | /// [`Decoder::read_directory_tags`](crate::decoder::Decoder::read_directory_tags) to read the |
| 19 | /// values associated with tags from an underlying file. |
| 20 | #[doc(alias = "IFD")] |
| 21 | pub struct Directory { |
| 22 | /// There are at most `u16::MAX` entries in any single directory, the count is stored as a |
| 23 | /// 2-byte value. The order in the file is implied to be ascending by tag value (the decoder |
| 24 | /// does not mind unordered entries). |
| 25 | pub(crate) entries: BTreeMap<u16, Entry>, |
| 26 | pub(crate) next_ifd: Option<NonZeroU64>, |
| 27 | } |
| 28 | |
| 29 | impl Directory { |
| 30 | /// Create a directory in an initial state without entries. Note that an empty directory can |
| 31 | /// not be encoded in a file, it must contain at least one entry. |
| 32 | pub fn empty() -> Self { |
| 33 | Directory { |
| 34 | entries: BTreeMap::new(), |
| 35 | next_ifd: None, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// Retrieve the value associated with a tag. |
| 40 | pub fn get(&self, tag: Tag) -> Option<&Entry> { |
| 41 | self.entries.get(&tag.to_u16()) |
| 42 | } |
| 43 | |
| 44 | /// Check if the directory contains a specified tag. |
| 45 | pub fn contains(&self, tag: Tag) -> bool { |
| 46 | self.entries.contains_key(&tag.to_u16()) |
| 47 | } |
| 48 | |
| 49 | /// Iterate over all known and unknown tags in this directory. |
| 50 | pub fn iter(&self) -> impl Iterator<Item = (Tag, &Entry)> + '_ { |
| 51 | self.entries |
| 52 | .iter() |
| 53 | .map(|(k, v)| (Tag::from_u16_exhaustive(*k), v)) |
| 54 | } |
| 55 | |
| 56 | /// Insert additional entries into the directory. |
| 57 | /// |
| 58 | /// Note that a directory can contain at most `u16::MAX` values. There may be one entry that |
| 59 | /// does not fit into the directory. This entry is silently ignored (please check [`Self::len`] |
| 60 | /// to detect the condition). Providing a tag multiple times or a tag that already exists |
| 61 | /// within this directory overwrites the entry. |
| 62 | pub fn extend(&mut self, iter: impl IntoIterator<Item = (Tag, Entry)>) { |
| 63 | // Code size conscious extension, avoid monomorphic extensions with the assumption of these |
| 64 | // not being performance sensitive in practice. (Maybe we have a polymorphic interface for |
| 65 | // the crate usage in the future. |
| 66 | self.extend_inner(iter.into_iter().by_ref()) |
| 67 | } |
| 68 | |
| 69 | /// Get the number of entries. |
| 70 | pub fn len(&self) -> usize { |
| 71 | // The keys are `u16`. Since IFDs are required to have at least one entry this would have |
| 72 | // been a trivial thing to do in the specification by storing it minus one but alas. In |
| 73 | // BigTIFF the count is stored as 8-bit anyways. |
| 74 | self.entries.len() |
| 75 | } |
| 76 | |
| 77 | /// Check if there are any entries in this directory. Note that an empty directory can not be |
| 78 | /// encoded in the file, it must contain at least one entry. |
| 79 | pub fn is_empty(&self) -> bool { |
| 80 | self.entries.is_empty() |
| 81 | } |
| 82 | |
| 83 | /// Get the pointer to the next IFD, if it was defined. |
| 84 | pub fn next(&self) -> Option<IfdPointer> { |
| 85 | self.next_ifd.map(|n| IfdPointer(n.get())) |
| 86 | } |
| 87 | |
| 88 | pub fn set_next(&mut self, next: Option<IfdPointer>) { |
| 89 | self.next_ifd = next.and_then(|n| NonZeroU64::new(n.0)); |
| 90 | } |
| 91 | |
| 92 | fn extend_inner(&mut self, iter: &mut dyn Iterator<Item = (Tag, Entry)>) { |
| 93 | for (tag, entry) in iter { |
| 94 | // If the tag is already present, it will be overwritten. |
| 95 | let map_entry = self.entries.entry(tag.to_u16()); |
| 96 | |
| 97 | match map_entry { |
| 98 | std::collections::btree_map::Entry::Vacant(vacant_entry) => { |
| 99 | vacant_entry.insert(entry); |
| 100 | } |
| 101 | std::collections::btree_map::Entry::Occupied(mut occupied_entry) => { |
| 102 | occupied_entry.insert(entry); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | pub(crate) fn encoded_len<K: crate::encoder::TiffKind>(&self) -> u64 { |
| 109 | let (len_count, len_offset); |
| 110 | |
| 111 | match std::mem::size_of::<K::OffsetType>() { |
| 112 | offset @ 4 => { |
| 113 | // non-BigTiff uses a `u16` here. |
| 114 | len_count = 2; |
| 115 | len_offset = offset as u64; |
| 116 | } |
| 117 | offset => { |
| 118 | let offset = offset as u64; |
| 119 | // assuming BigTIFF |
| 120 | len_count = offset; |
| 121 | len_offset = offset; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Each entry is 12 bytes in classic TIFF, 20 bytes in BigTIFF. |
| 126 | let entry_bytes = 4 + 2 * len_offset; |
| 127 | len_count + entry_bytes * self.entries.len() as u64 + len_offset |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | impl fmt::Debug for Directory { |
| 132 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 133 | f.debug_struct("Directory") |
| 134 | .field( |
| 135 | "entries", |
| 136 | &self.entries.iter().map(|(k, v)| (Tag::from_u16(*k), v)), |
| 137 | ) |
| 138 | .field("next_ifd", &self.next_ifd) |
| 139 | .finish() |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | impl core::iter::FromIterator<(Tag, Entry)> for Directory { |
| 144 | fn from_iter<T: IntoIterator<Item = (Tag, Entry)>>(iter: T) -> Self { |
| 145 | let mut dir = Directory::empty(); |
| 146 | dir.extend(iter); |
| 147 | dir |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | #[cfg(test)] |
| 152 | mod tests { |
| 153 | use super::Directory; |
| 154 | use crate::{decoder::ifd::Entry, tags::Tag}; |
| 155 | |
| 156 | #[test] |
| 157 | fn directory_multiple_entries() { |
| 158 | let mut dir = Directory::empty(); |
| 159 | assert_eq!(dir.len(), 0); |
| 160 | |
| 161 | dir.extend((0..=u16::MAX).map(|i| { |
| 162 | let tag = Tag::Unknown(1); |
| 163 | let entry = Entry::new_u64(crate::tags::Type::BYTE, i.into(), [0; 8]); |
| 164 | (tag, entry) |
| 165 | })); |
| 166 | |
| 167 | assert_eq!(dir.len(), 1, "Only one tag was ever modified"); |
| 168 | |
| 169 | assert_eq!( |
| 170 | dir.get(Tag::Unknown(1)) |
| 171 | .expect("tag 1 should be present after this chain") |
| 172 | .count(), |
| 173 | u16::MAX.into() |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn iteration_order() { |
| 179 | let mut dir = Directory::empty(); |
| 180 | assert_eq!(dir.len(), 0); |
| 181 | |
| 182 | let fake_entry = Entry::new_u64(crate::tags::Type::BYTE, 0, [0; 8]); |
| 183 | dir.extend((0..32).map(|i| { |
| 184 | let tag = Tag::Unknown(i); |
| 185 | let entry = fake_entry.clone(); |
| 186 | (tag, entry) |
| 187 | })); |
| 188 | |
| 189 | let iter_order: Vec<u16> = dir.iter().map(|(tag, _e)| tag.to_u16()).collect(); |
| 190 | assert_eq!( |
| 191 | iter_order, |
| 192 | (0..32).collect::<Vec<_>>(), |
| 193 | "Tags must be in ascending order according to the specification" |
| 194 | ); |
| 195 | } |
| 196 | } |