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

[IMAGING-355] Add option to skip reading GIF metadata #301

Merged
merged 5 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,11 @@ public byte[] getIccProfileBytes(final ByteSource byteSource, final GifImagingPa
@Override
public ImageInfo getImageInfo(final ByteSource byteSource, final GifImagingParameters params)
throws ImagingException, IOException {
final GifImageContents blocks = readFile(byteSource, false);
boolean stopReadingBeforeImageData = true;
Copy link
Member

@garydgregory garydgregory Dec 23, 2023

Choose a reason for hiding this comment

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

You've changed the default behavior here which makes the parameter a forced usage, which might be a surprise, I'll flip it post mege.

if (params != null) {
stopReadingBeforeImageData = params.getStopReadingBeforeImageData();
}
final GifImageContents blocks = readFile(byteSource, stopReadingBeforeImageData);

final GifHeaderInfo bhi = blocks.gifHeaderInfo;
if (bhi == null) {
Expand Down Expand Up @@ -482,7 +486,11 @@ public Dimension getImageSize(final ByteSource byteSource, final GifImagingParam
@Override
public ImageMetadata getMetadata(final ByteSource byteSource, final GifImagingParameters params)
throws ImagingException, IOException {
final GifImageContents imageContents = readFile(byteSource, false);
boolean stopReadingBeforeImageData = true;
if (params != null) {
stopReadingBeforeImageData = params.getStopReadingBeforeImageData();
}
final GifImageContents imageContents = readFile(byteSource, stopReadingBeforeImageData);

final GifHeaderInfo bhi = imageContents.gifHeaderInfo;
if (bhi == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,14 @@
* @since 1.0-alpha3
*/
public class GifImagingParameters extends XmpImagingParameters<GifImagingParameters> {
// empty
private boolean stopReadingBeforeImageData;

public boolean getStopReadingBeforeImageData() {
return stopReadingBeforeImageData;
}

public void setStopReadingBeforeImageData(final boolean stopReadingBeforeImageData) {
this.stopReadingBeforeImageData = stopReadingBeforeImageData;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.stream.Stream;

import org.apache.commons.imaging.common.ImageMetadata;
import org.apache.commons.imaging.ImageInfo;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.ImagingException;
Expand Down Expand Up @@ -116,7 +117,7 @@ public void testImageDimensions(final File imageFile) throws Exception {

int width = 0;
int height = 0;
for(int i = 0; i < images.size(); i++) {
for (int i = 0; i < images.size(); i++) {
final BufferedImage image = images.get(i);
final GifImageMetadataItem metadataItem = metadata.getItems().get(i);
final int xOffset = metadataItem.getLeftPosition();
Expand All @@ -139,11 +140,15 @@ public void testImageInfo(final File imageFile) throws Exception {
// TODO assert more
}

@Disabled(value = "RoundtripTest has to be fixed before implementation can throw UnsupportedOperationException")
@ParameterizedTest
@MethodSource("data")
public void testMetadata(final File imageFile) {
assertThrows(UnsupportedOperationException.class, () -> Imaging.getMetadata(imageFile));
public void testMetadata(final File imageFile) throws IOException {
final ImageMetadata metadata = Imaging.getMetadata(imageFile);
assertNotNull(metadata);
assertTrue(metadata instanceof GifImageMetadata);
assertTrue(((GifImageMetadata)metadata).getWidth() > 0);
assertTrue(((GifImageMetadata)metadata).getHeight() > 0);
assertNotNull(metadata.getItems());
}

/**
Expand Down