-
Notifications
You must be signed in to change notification settings - Fork 483
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
Fixed invalid extraction of image dimensions for HEIF files w/ multiple images #445
Open
rjean-gilles
wants to merge
2
commits into
drewnoakes:main
Choose a base branch
from
sportagraph:heif-dimensions-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
Tests/com/drew/imaging/heif/HeifMetadataReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright 2002-2019 Drew Noakes and contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* More information about this project is available at: | ||
* | ||
* https://drewnoakes.com/code/exif/ | ||
* https://github.com/drewnoakes/metadata-extractor | ||
*/ | ||
package com.drew.imaging.heif; | ||
|
||
import com.drew.lang.annotations.NotNull; | ||
import com.drew.metadata.Directory; | ||
import com.drew.metadata.Metadata; | ||
import com.drew.metadata.MetadataException; | ||
import com.drew.metadata.heif.HeifDirectory; | ||
import org.junit.Test; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
import static com.drew.metadata.heif.HeifDirectory.*; | ||
import static org.junit.Assert.*; | ||
|
||
public class HeifMetadataReaderTest | ||
{ | ||
@NotNull | ||
private static Metadata processFile(@NotNull String filePath) throws IOException | ||
{ | ||
FileInputStream inputStream = null; | ||
try { | ||
inputStream = new FileInputStream(filePath); | ||
return HeifMetadataReader.readMetadata(inputStream); | ||
} finally { | ||
if (inputStream != null) { | ||
inputStream.close(); | ||
} | ||
} | ||
} | ||
|
||
private <D extends Directory> int getIntInAnyDir(Collection<D> directories, int tagType, int defaultValue) throws MetadataException { | ||
for (D dir : directories) { | ||
if (dir.containsTag(tagType)) { | ||
return dir.getInt(tagType); | ||
} | ||
} | ||
return defaultValue; | ||
} | ||
|
||
private void doTestImage( | ||
@NotNull String filePath, int expectedWidth, int expectedHeight, int expectedRotation | ||
) throws Exception | ||
{ | ||
Metadata metadata = processFile(filePath); | ||
Collection<HeifDirectory> dirs = metadata.getDirectoriesOfType(HeifDirectory.class); | ||
|
||
assertNotNull(dirs); | ||
assertFalse(dirs.isEmpty()); | ||
|
||
assertEquals(expectedWidth, getIntInAnyDir(dirs, TAG_IMAGE_WIDTH, -1)); | ||
assertEquals(expectedHeight, getIntInAnyDir(dirs, TAG_IMAGE_HEIGHT, -1)); | ||
assertEquals(expectedRotation, getIntInAnyDir(dirs, TAG_IMAGE_ROTATION, -1)); | ||
} | ||
|
||
@Test | ||
public void testStillImage() throws Exception | ||
{ | ||
doTestImage("Tests/Data/heif/still-images/autumn_1440x960.heic", 1440, 960, -1); | ||
} | ||
|
||
@Test | ||
public void testDerivedImageGrid() throws Exception | ||
{ | ||
doTestImage("Tests/Data/heif/image-derivations/grid_960x640.heic", 960, 640, -1); | ||
} | ||
|
||
@Test | ||
public void testDerivedImageOverlay() throws Exception | ||
{ | ||
doTestImage("Tests/Data/heif/image-derivations/overlay_1000x680.heic", 1000, 680, -1); | ||
} | ||
|
||
@Test | ||
public void testImageCollection1() throws Exception | ||
{ | ||
doTestImage("Tests/Data/heif/image-collections/random_collection_1440x960.heic", 1440, 960, -1); | ||
} | ||
|
||
@Test | ||
public void testImageCollection2() throws Exception | ||
{ | ||
doTestImage("Tests/Data/heif/image-collections/season_collection_1440x960.heic", 1440, 960, -1); | ||
} | ||
|
||
@Test | ||
public void testImageSequence1() throws Exception | ||
{ | ||
doTestImage("Tests/Data/heif/image-sequences/sea1_animation.heic", 256, 144, -1); | ||
} | ||
|
||
@Test | ||
public void testImageSequence2() throws Exception | ||
{ | ||
doTestImage("Tests/Data/heif/image-sequences/starfield_animation.heic", 256, 144, -1); | ||
} | ||
|
||
@Test | ||
public void testAuxiliaryImageStorage() throws Exception | ||
{ | ||
doTestImage("Tests/Data/heif/auxiliary-image-storage/alpha_1440x960.heic", 1440, 960, -1); | ||
} | ||
|
||
// Does not currently work | ||
/* | ||
@Test | ||
public void testImageBurst1() throws Exception | ||
{ | ||
doTestImage("Tests/Data/heif/image-bursts/bird_burst.heic", 640, 360, -1); | ||
} | ||
|
||
@Test | ||
public void testImageBurst2() throws Exception | ||
{ | ||
doTestImage("Tests/Data/heif/image-bursts/rally_burst.heic", 640, 360, -1); | ||
} | ||
*/ | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually looking at this further I think there is a different problem here. We have other image formats that can contain multiple images (e.g. GIF, JPEG, TIFF). In such cases, we produce a directory per image. These kinds of assumptions don't tend to work out well for all users. Instead it's best to reflect the actual data in the directories we produce. Fixing this is probably a larger change, but it would be correct for all users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I completely agree that there should be multiple directories, but I didn't say anything because I assumed that there was some characteristic of the HEIF files that meant that this didn't make sense. If this is a simple "multi image container" situation, multiple directories should be generated. Developers would then make their own logic for picking which one they are interested in, if only one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes indeed, as I hinted in my summary the ideal behaviour would be for the library to report all the included images obviously, which (as I said) will probably require a major overhaul if I'm not mistaken. Which means it might not come soon. In the meantime, the library as it is basically pretends that there is only one image in the HEIC file when there might actually be more. The only change that this PR does is to acknowledge that limitation and make it somewhat useful by at least making sure that we report information about the "main" image.
So we're just going from a situation where we just cannot use the library at all (as is) if we ever want to handle HEIC files coming from an iPhone (and let's be frank, that's the main reason today to bother supporting it), to a behaviour that while imperfect (because the lib pretends that there is only one image) will report the correct dimensions for all those iPhone HEIC photos. This is basically a worthwile compromise as I see it (and a temporary one at that, until the correct solution is implemented).
As for the test images, I have tried to contact Nokia about the permission to use their sample images and got no reply. That was more than a week ago, so I was about to replace them with some iPhone images that I would take myself, but sure, I can remove all that and keep the tests to myself, no problem.
Would you then consider approving the PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to validate the assumption that the correct change here is a major overhaul.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you.
Looking forward to your conclusion.