TMI-JPEG: More lenient segment parsing, now allows 0xFF padding between segments + fixed an NPE in JPEGImageReader if the parsing fails.

This commit is contained in:
Harald Kuhr
2013-04-19 16:17:01 +02:00
parent 61e01e3316
commit b966254322
8 changed files with 68 additions and 8 deletions

View File

@@ -153,11 +153,18 @@ public final class JPEGSegmentUtil {
}
}
static JPEGSegment readSegment(final ImageInputStream stream, Map<Integer, List<String>> segmentIdentifiers) throws IOException {
static JPEGSegment readSegment(final ImageInputStream stream, final Map<Integer, List<String>> segmentIdentifiers) throws IOException {
int marker = stream.readUnsignedShort();
// Skip over 0xff padding between markers
while (marker == 0xffff) {
marker = (marker & 0xff) << 8 | stream.readUnsignedByte();
}
if ((marker >> 8 & 0xff) != 0xff) {
throw new IIOException(String.format("Bad marker: %04x", marker));
}
int length = stream.readUnsignedShort(); // Length including length field itself
byte[] data;
@@ -196,7 +203,7 @@ public final class JPEGSegmentUtil {
}
@Override
public boolean contains(Object o) {
public boolean contains(final Object o) {
return true;
}
}
@@ -208,13 +215,13 @@ public final class JPEGSegmentUtil {
}
@Override
public List<String> get(Object key) {
public List<String> get(final Object key) {
return key instanceof Integer && JPEGSegment.isAppSegmentMarker((Integer) key) ? ALL_IDS : null;
}
@Override
public boolean containsKey(Object key) {
public boolean containsKey(final Object key) {
return true;
}
}
@@ -226,7 +233,7 @@ public final class JPEGSegmentUtil {
}
@Override
public List<String> get(Object key) {
public List<String> get(final Object key) {
return containsKey(key) ? ALL_IDS : null;
}

View File

@@ -196,4 +196,17 @@ public class JPEGSegmentUtilTest {
assertEquals(JPEG.APP14, segments.get(21).marker());
assertEquals("Adobe", segments.get(21).identifier());
}
@Test
public void testReadPaddedSegments() throws IOException {
List<JPEGSegment> segments = JPEGSegmentUtil.readSegments(getData("/jpeg/jfif-padded-segments.jpg"), JPEGSegmentUtil.APP_SEGMENTS);
assertEquals(3, segments.size());
assertEquals(JPEG.APP0, segments.get(0).marker());
assertEquals("JFIF", segments.get(0).identifier());
assertEquals(JPEG.APP2, segments.get(1).marker());
assertEquals("ICC_PROFILE", segments.get(1).identifier());
assertEquals(JPEG.APP1, segments.get(2).marker());
assertEquals("Exif", segments.get(2).identifier());
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB