From b91d02a5626452b70b5b102def4d8c25cd37e991 Mon Sep 17 00:00:00 2001 From: Harald Kuhr Date: Sat, 16 Dec 2023 17:57:49 +0100 Subject: [PATCH] #878: Now detects APP14/Adobe markers with full 2 byte version --- .../twelvemonkeys/imageio/plugins/jpeg/AdobeDCT.java | 8 ++++---- .../imageio/metadata/jpeg/JPEGSegmentUtil.java | 12 +++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/imageio/imageio-jpeg/src/main/java/com/twelvemonkeys/imageio/plugins/jpeg/AdobeDCT.java b/imageio/imageio-jpeg/src/main/java/com/twelvemonkeys/imageio/plugins/jpeg/AdobeDCT.java index 219606337..c528e907c 100644 --- a/imageio/imageio-jpeg/src/main/java/com/twelvemonkeys/imageio/plugins/jpeg/AdobeDCT.java +++ b/imageio/imageio-jpeg/src/main/java/com/twelvemonkeys/imageio/plugins/jpeg/AdobeDCT.java @@ -53,7 +53,7 @@ final class AdobeDCT extends Application { final int transform; private AdobeDCT(int version, int flags0, int flags1, int transform) { - super(JPEG.APP14, "Adobe", new byte[]{'A', 'd', 'o', 'b', 'e', 0, (byte) version, (byte) (flags0 >> 8), (byte) (flags0 & 0xff), (byte) (flags1 >> 8), (byte) (flags1 & 0xff), (byte) transform}); + super(JPEG.APP14, "Adobe", new byte[]{'A', 'd', 'o', 'b', 'e', (byte) (version >> 8), (byte) (version & 0xff), (byte) (flags0 >> 8), (byte) (flags0 & 0xff), (byte) (flags1 >> 8), (byte) (flags1 & 0xff), (byte) transform}); this.version = version; // 100 or 101 this.flags0 = flags0; @@ -72,11 +72,11 @@ public String toString() { public static AdobeDCT read(final DataInput data, final int length) throws IOException { // TODO: Investigate http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6355567: 33/35 byte Adobe APP14 markers - data.skipBytes(6); // A, d, o, b, e, \0 + data.skipBytes(5); // A, d, o, b, e - // version (byte), flags (4bytes), color transform (byte: 0=unknown, 1=YCC, 2=YCCK) + // version (2 bytes), flags (4bytes), color transform (byte: 0=unknown, 1=YCC, 2=YCCK) return new AdobeDCT( - data.readUnsignedByte(), + data.readUnsignedShort(), data.readUnsignedShort(), data.readUnsignedShort(), data.readUnsignedByte() diff --git a/imageio/imageio-metadata/src/main/java/com/twelvemonkeys/imageio/metadata/jpeg/JPEGSegmentUtil.java b/imageio/imageio-metadata/src/main/java/com/twelvemonkeys/imageio/metadata/jpeg/JPEGSegmentUtil.java index 988205adc..44275cd51 100644 --- a/imageio/imageio-metadata/src/main/java/com/twelvemonkeys/imageio/metadata/jpeg/JPEGSegmentUtil.java +++ b/imageio/imageio-metadata/src/main/java/com/twelvemonkeys/imageio/metadata/jpeg/JPEGSegmentUtil.java @@ -43,13 +43,17 @@ import javax.imageio.IIOException; import javax.imageio.ImageIO; import javax.imageio.stream.ImageInputStream; -import java.awt.color.ICC_Profile; +import java.awt.color.*; import java.io.ByteArrayOutputStream; import java.io.EOFException; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import static com.twelvemonkeys.lang.Validate.notNull; @@ -144,8 +148,10 @@ private static boolean isImageDone(final JPEGSegment segment) { } static String asNullTerminatedAsciiString(final byte[] data, final int offset) { + // TODO: JPEG App segment identifiers are not always 0-terminated... + // Need to rewrite. For now, make sure we read only ASCII non-control chars. for (int i = 0; i < data.length - offset; i++) { - if (data[offset + i] == 0 || i > 255) { + if (data[offset + i] < 20 || i > 255) { return asAsciiString(data, offset, offset + i); } }