JPEG Exif/thumbnail fixes.

(cherry picked from commit fbc738f2d4836e82315831774c0e722a4984e967)
This commit is contained in:
Harald Kuhr 2021-01-30 16:50:54 +01:00
parent 6a66d2e059
commit 2488f6f67c

View File

@ -919,6 +919,9 @@ public final class JPEGImageReader extends ImageReaderBase {
try (ImageInputStream stream = new ByteArrayImageInputStream(exif.data, offset, exif.data.length - offset)) {
return (CompoundDirectory) new TIFFReader().read(stream);
}
catch (IIOException e) {
processWarningOccurred("Exif chunk is present, but can't be read: " + e.getMessage());
}
}
}
@ -1106,6 +1109,7 @@ public final class JPEGImageReader extends ImageReaderBase {
// Read JFIF thumbnails if present
JFIF jfif = getJFIF();
if (jfif != null && jfif.thumbnail != null) {
// TODO: Check if the JFIF segment really has room for this thumbnail?
thumbnails.add(new JFIFThumbnailReader(thumbnailProgressDelegator, imageIndex, thumbnails.size(), jfif));
}
@ -1116,6 +1120,7 @@ public final class JPEGImageReader extends ImageReaderBase {
case JFXX.JPEG:
case JFXX.INDEXED:
case JFXX.RGB:
// TODO: Check if the JFXX segment really has room for this thumbnail?
thumbnails.add(new JFXXThumbnailReader(thumbnailProgressDelegator, getThumbnailReader(), imageIndex, thumbnails.size(), jfxx));
break;
default:
@ -1136,6 +1141,7 @@ public final class JPEGImageReader extends ImageReaderBase {
}
else {
ImageInputStream stream = new ByteArrayImageInputStream(exif.data, dataOffset, exif.data.length - dataOffset);
try {
CompoundDirectory exifMetadata = (CompoundDirectory) new TIFFReader().read(stream);
if (exifMetadata.directoryCount() == 2) {
@ -1154,8 +1160,16 @@ public final class JPEGImageReader extends ImageReaderBase {
long jpegOffset = ((Number) jpegOffEntry.getValue()).longValue();
long jpegLength = jpegLenEntry != null ? ((Number) jpegLenEntry.getValue()).longValue() : -1;
if (jpegLength > 0 && jpegOffset + jpegLength <= stream.length()) {
// Verify first bytes are FFD8
stream.seek(jpegOffset);
if (stream.readUnsignedShort() == JPEG.SOI) {
thumbnails.add(new EXIFThumbnailReader(thumbnailProgressDelegator, getThumbnailReader(), 0, thumbnails.size(), ifd1, stream));
}
// TODO: Simplify this warning fallback stuff...
else {
processWarningOccurred("EXIF IFD with empty or incomplete JPEG thumbnail");
}
}
else {
processWarningOccurred("EXIF IFD with empty or incomplete JPEG thumbnail");
}
@ -1165,9 +1179,18 @@ public final class JPEGImageReader extends ImageReaderBase {
}
}
else if (compression == 1) {
if (ifd1.getEntryById(TIFF.TAG_STRIP_OFFSETS) != null) {
Entry stripOffEntry = ifd1.getEntryById(TIFF.TAG_STRIP_OFFSETS);
if (stripOffEntry != null) {
long stripOffset = ((Number) stripOffEntry.getValue()).longValue();
if (stripOffset < stream.length()) {
// TODO: Verify length of Exif thumbnail vs length of segment like in JPEG
// ...but this requires so many extra values... Instead move this logic to the
// EXIFThumbnailReader?
thumbnails.add(new EXIFThumbnailReader(thumbnailProgressDelegator, getThumbnailReader(), 0, thumbnails.size(), ifd1, stream));
}
}
else {
processWarningOccurred("EXIF IFD with uncompressed thumbnail missing StripOffsets tag");
}
@ -1177,6 +1200,10 @@ public final class JPEGImageReader extends ImageReaderBase {
}
}
}
catch (IIOException e) {
processWarningOccurred("Exif chunk present, but can't be read: " + e.getMessage());
}
}
}
}
}