Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for iterating over all tags #255

Merged
merged 4 commits into from
Nov 26, 2024

Conversation

gschulze
Copy link
Contributor

Closes #243

Copy link
Contributor

@fintelia fintelia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question I have is whether the strip/tile offsets and lengths should be included in the iteration over all tags. On one hand it is better for consistency, but they're also likely to be larger than the other tags and not especially helpful for users

@@ -895,6 +898,18 @@ impl<R: Read + Seek> Decoder<R> {
self.get_tag(tag)?.into_string()
}

pub fn tag_iter(&mut self) -> impl Iterator<Item = TiffResult<(Tag, ifd::Value)>> + '_ {
match self.image().ifd.as_ref() {
None => Either::Left(std::iter::empty()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know when this case happens? Wondering if we should return None or an error rather than an empty iterator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea, honestly. I think from a developer perspective, it is more convenient to have an empty iterator instead of having to deal with an Option.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I went through the existing code. It should be impossible for ifd to be None here, so please just .unwrap() it

tests/encode_images.rs Outdated Show resolved Hide resolved
pub fn tag_iter(&mut self) -> impl Iterator<Item = TiffResult<(Tag, ifd::Value)>> + '_ {
match self.image().ifd.as_ref() {
None => Either::Left(std::iter::empty()),
Some(ifd) => Either::Right(TagIter::new(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than constructing a TagIter here, I think we could directly return:

ifd.clone().into_iter().map(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried but failed to make the borrow checker happy with this approach. Happy to change it if you can provide a working solution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to get it to work with:

        self.image.ifd.as_ref().unwrap().iter().map(|(tag, entry)| {
            entry
                .val(&self.limits, self.bigtiff, &mut self.reader)
                .map(|value| (*tag, value))
        })

(You have to directly access the self.image field rather than calling the self.image() method to make the borrow check happy. There was a reason why the two are separate, though I don't remember what it is right now)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, cool, thx! I incorporated your changes and pushed the branch.

@gschulze
Copy link
Contributor Author

I noticed tests are failing on CI. However, they run fine on my local machine (MacBook Pro M1). Maybe something is architecture/platform-dependent in the decoder @fintelia?

@fintelia
Copy link
Contributor

fintelia commented Nov 21, 2024

The CI works by merging the branch into main and testing the resulting version. You might have to merge the lest changes from main locally to get the same results?

Edit: This is what's happening. Your branch is three commits behind:
image

@gschulze gschulze force-pushed the feature/retrieve-all-tags branch from 09f6d60 to 395a755 Compare November 24, 2024 19:10
@@ -895,6 +898,18 @@ impl<R: Read + Seek> Decoder<R> {
self.get_tag(tag)?.into_string()
}

pub fn tag_iter(&mut self) -> impl Iterator<Item = TiffResult<(Tag, ifd::Value)>> + '_ {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a short doc comment explaining what this method does? Something like "Returns an iterator over all tags in the current image"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

@gschulze
Copy link
Contributor Author

You might have to merge the lest changes from main locally to get the same results?

Thanks for pointing that out. I occasionally forget about that when I'm on a fork.

@gschulze gschulze force-pushed the feature/retrieve-all-tags branch from e4c1fd2 to fd2bd31 Compare November 25, 2024 07:38
@fintelia fintelia merged commit ff52aea into image-rs:master Nov 26, 2024
6 checks passed
@fintelia
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide a way to retrieve all tags defined within a file
2 participants