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
@@ -601,14 +601,24 @@ public class JPEGImageReader extends ImageReaderBase {
segments = JPEGSegmentUtil.readSegments(imageInput, SEGMENT_IDENTIFIERS);
}
catch (IOException ignore) {
catch (IIOException ignore) {
if (DEBUG) {
ignore.printStackTrace();
}
}
catch (IllegalArgumentException foo) {
foo.printStackTrace();
if (DEBUG) {
foo.printStackTrace();
}
}
finally {
imageInput.reset();
}
// In case of an exception, avoid NPE when referencing segments later
if (segments == null) {
segments = Collections.emptyList();
}
}
private List<JPEGSegment> getAppSegments(final int marker, final String identifier) throws IOException {
@@ -90,6 +90,12 @@ final class JPEGSegmentImageInputStream extends ImageInputStreamImpl {
long realPosition = stream.getStreamPosition();
int marker = stream.readUnsignedShort();
// Skip over 0xff padding between markers
while (marker == 0xffff) {
realPosition++;
marker = (marker & 0xff) << 8 | stream.readUnsignedByte();
}
// TODO: Refactor to make various segments optional, we probably only want the "Adobe" APP14 segment, 'Exif' APP1 and very few others
if (isAppSegmentMarker(marker) && marker != JPEG.APP0 && !(marker == JPEG.APP1 && isAppSegmentWithId("Exif", stream)) && marker != JPEG.APP14) {
int length = stream.readUnsignedShort(); // Length including length field itself