Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janhoy committed Aug 9, 2024
1 parent 3273050 commit 4d50703
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,16 @@ private void readOrientationFromEXIF() {
switch (field.getDataType()) {
case LONG:
case SLONG:
orientation = Orientation.forEXIFOrientation(Math.toIntExact((long) value));
break;
case SHORT:
case SSHORT:
orientation = Orientation.forEXIFOrientation((int) value);
// According to spec the orientation must be an unsigned short (16 bit)
// However, we have seen exif data in the wild with LONG and SLONG types
// Thus to be lenient we accept either and convert to int (github issue #548)
if (value instanceof Long) {
orientation = Orientation.forEXIFOrientation(Math.toIntExact((long) value));
} else if (value instanceof Integer) {
orientation = Orientation.forEXIFOrientation((int) value);
}
break;
default:
LOGGER.warn("readOrientationFromEXIF(): Unsupported Orientation data type: {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -230,7 +233,21 @@ void testGetOrientationWithOnlyIllegalEXIFOrientation() throws Exception {
}
}

@Test
@Test
void testGetOrientationWithOnlyLONGEXIFOrientation() throws Exception {
// This image has exif Orientation stored as SLONG, causing a failure (github issue #548)
Path fixture = TestUtil.getImage("jpg-exif-long-orientation.jpg");
ImageReader reader = new ImageReaderFactory()
.newImageReader(Format.get("jpg"), fixture);
try {
Metadata metadata = reader.getMetadata(0);
assertEquals(Orientation.ROTATE_0, metadata.getOrientation());
} finally {
reader.dispose();
}
}

@Test
void testGetOrientationWithOnlyXMPOrientation() throws Exception {
Path fixture = TestUtil.getImage("jpg-xmp-orientation-90.jpg");
ImageReader reader = new ImageReaderFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,15 @@ void testGetValueWithoutMatchingValue() {
assertNull(ifd.getValue(Tag.MAKE));
}

@Test
@Test
void testGetField() {
final Directory ifd = new Directory(TagSet.BASELINE_TIFF);
ifd.put(Tag.MAKE, DataType.ASCII, "Cats");
Field field = ifd.getField(Tag.MAKE);
assertEquals(DataType.ASCII, field.getDataType());
}

@Test
void testHashCodeWithEqualInstances() {
final Directory subIFD1 = new Directory(TagSet.EXIF);
subIFD1.put(Tag.EXPOSURE_TIME, DataType.RATIONAL, new Rational(1, 160));
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4d50703

Please sign in to comment.