Skip to content

Commit 60dbc82

Browse files
committed
Don't make ImageFileDirectoryReader stateful
1 parent 650cca1 commit 60dbc82

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

src/metadata/reader.rs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl TiffMetadataReader {
104104
fetch: &F,
105105
) -> AsyncTiffResult<Option<ImageFileDirectory>> {
106106
if let Some(ifd_start) = self.next_ifd_offset {
107-
let mut ifd_reader =
107+
let ifd_reader =
108108
ImageFileDirectoryReader::open(fetch, ifd_start, self.bigtiff, self.endianness)
109109
.await?;
110110
let ifd = ifd_reader.read(fetch).await?;
@@ -149,8 +149,6 @@ pub struct ImageFileDirectoryReader {
149149
ifd_entry_byte_size: u64,
150150
/// The number of bytes that the value for the number of tags takes up.
151151
tag_count_byte_size: u64,
152-
/// The index of the tag to be read next
153-
tag_idx: u64,
154152
}
155153

156154
impl ImageFileDirectoryReader {
@@ -188,51 +186,36 @@ impl ImageFileDirectoryReader {
188186
tag_count,
189187
tag_count_byte_size,
190188
ifd_start_offset,
191-
tag_idx: 0,
192189
})
193190
}
194191

195-
/// Returns `true` if there are more tags to read in this IFD
196-
pub fn has_more_tags(&self) -> bool {
197-
self.tag_idx < self.tag_count
198-
}
199-
200-
/// Manually read the next tag out of the IFD.
192+
/// Manually read the tag with the specified idx.
201193
///
202194
/// If there are no more tags, returns `None`.
203195
///
204196
/// This can be useful if you need to access tags at a low level. You'll need to call
205197
/// [`ImageFileDirectory::from_tags`] on the resulting collection of tags.
206-
pub async fn read_next_tag<F: MetadataFetch>(
207-
&mut self,
198+
pub async fn read_tag<F: MetadataFetch>(
199+
&self,
208200
fetch: &F,
209-
) -> AsyncTiffResult<Option<(Tag, Value)>> {
210-
if self.tag_idx != self.tag_count {
211-
let tag_offset = self.ifd_start_offset
212-
+ self.tag_count_byte_size
213-
+ (self.ifd_entry_byte_size * self.tag_idx);
214-
let (tag_name, tag_value) =
215-
read_tag(fetch, tag_offset, self.endianness, self.bigtiff).await?;
216-
self.tag_idx += 1;
217-
Ok(Some((tag_name, tag_value)))
218-
} else {
219-
Ok(None)
220-
}
201+
tag_idx: u64,
202+
) -> AsyncTiffResult<(Tag, Value)> {
203+
assert!(tag_idx < self.tag_count);
204+
let tag_offset =
205+
self.ifd_start_offset + self.tag_count_byte_size + (self.ifd_entry_byte_size * tag_idx);
206+
let (tag_name, tag_value) =
207+
read_tag(fetch, tag_offset, self.endianness, self.bigtiff).await?;
208+
Ok((tag_name, tag_value))
221209
}
222210

223211
/// Read all tags out of this IFD.
224212
///
225-
/// This will read _all_ tags from this IFD, even if you've already read some of them via
226-
/// [`read_next_tag`][Self::read_next_tag].
227-
///
228213
/// Keep in mind that you'll still need to call [`finish`][Self::finish] to get the byte offset
229214
/// of the next IFD.
230-
pub async fn read<F: MetadataFetch>(
231-
&mut self,
232-
fetch: &F,
233-
) -> AsyncTiffResult<ImageFileDirectory> {
215+
pub async fn read<F: MetadataFetch>(&self, fetch: &F) -> AsyncTiffResult<ImageFileDirectory> {
234216
let mut tags = HashMap::with_capacity(self.tag_count as usize);
235-
while let Some((tag, value)) = self.read_next_tag(fetch).await? {
217+
for tag_idx in 0..self.tag_count {
218+
let (tag, value) = self.read_tag(fetch, tag_idx).await?;
236219
tags.insert(tag, value);
237220
}
238221
ImageFileDirectory::from_tags(tags)

0 commit comments

Comments
 (0)