#556 PICTImageReaderSpi no longer claim to decode known formats

This commit is contained in:
Harald Kuhr 2020-08-07 16:31:29 +02:00
parent 49c7cd1979
commit bda6544a5f
3 changed files with 102 additions and 10 deletions

View File

@ -60,6 +60,13 @@ public final class PICTImageReaderSpi extends ImageReaderSpiBase {
}
ImageInputStream stream = (ImageInputStream) pSource;
// As PICT format don't have good magic and our method often gives false positives,
// so we'll need to check for other known formats (BMP, GIF, JPEG, PNG, PSD, TIFF) first
if (isOtherFormat(stream)) {
return false;
}
stream.mark();
try {
@ -68,13 +75,12 @@ public final class PICTImageReaderSpi extends ImageReaderSpiBase {
return true;
}
else {
// Skip header 512 bytes for file-based streams
stream.reset();
// We need to set mark again, to make sure the reset call in
// We need to reset AND set mark again, to make sure the reset call in
// the finally block will not consume existing marks
stream.reset();
stream.mark();
// Skip header 512 bytes for file-based streams
skipNullHeader(stream);
}
@ -88,6 +94,60 @@ public final class PICTImageReaderSpi extends ImageReaderSpiBase {
}
}
// TODO: Candidate util method.
// Might need to be able to exclude some formats
// Might need to return the format matched...
static boolean isOtherFormat(final ImageInputStream stream) throws IOException {
stream.mark();
try {
byte[] signature = new byte[8];
stream.readFully(signature);
// BMP: off: 0, len: 2, magic: 'B', 'M'
// GIF: off: 0, len: 6, magic: 'G', 'I', 'F', '8', ('7' | '9'), 'a' (use only "GIF8"?)
// JPEG: off 0, len: 2, magic: 0xffd8 (SOI marker)
// PNG: off: 0, len: 8, magic: 0x89, 'P', 'N', 'G', 0x0d0a1a0a
// PSD: off: 0, len: 4, magic: '8', 'B', 'P', 'S'
// TIFF: off: 0, len: 4, magic: ('I', 'I', 0x00, (0x42 | 0x43)) | ('M', 'M', (0x42 | 0x43), 0x00) (43 is bigTiff)
if (signature[0] == 'B' && signature[1] == 'M') {
// BMP
return true;
}
if (signature[0] == 'G' && signature[1] == 'I' && signature[2] == 'F' && signature[3] == '8'
&& (signature[4] == '7' || signature[4] == '9') && signature[5] == 'a') {
// GIF
return true;
}
if (signature[0] == (byte) 0xFF && signature[1] == (byte) 0xD8 && signature[2] == (byte) 0xFF) {
// JPEG
return true;
}
if (signature[0] == (byte) 0x89 && signature[1] == 'P' && signature[2] == 'N' && signature[3] == 'G'
&& signature[4] == 0x0D && signature[5] == 0x0A && signature[6] == 0x1A && signature[7] == 0x0A) {
// PNG
return true;
}
if (signature[0] == '8' && signature[1] == 'B' && signature[2] == 'P' && signature[3] == 'S') {
// PSD
return true;
}
if ((signature[0] == 'I' && signature[1] == 'I' && (signature[2] == 42 || signature[2] == 43) && signature[3] == 0x00)
|| signature[0] == 'M' && signature[1] == 'M' && signature[2] == 0x00 && (signature[3] == 42 || signature[3] == 43)) {
// TIFF/BigTIFF
return true;
}
}
catch (EOFException ignore) {
// Can't be any of the formats
}
finally {
stream.reset();
}
return false;
}
static void skipNullHeader(final ImageInputStream pStream) throws IOException {
// NOTE: Only skip if FILE FORMAT, not needed for Mac OS DnD
// Spec says "platform dependent", may not be all nulls..
@ -102,10 +162,6 @@ public final class PICTImageReaderSpi extends ImageReaderSpiBase {
// Sanity check bounding box
int y1 = pStream.readUnsignedShort();
int x1 = pStream.readUnsignedShort();
// TODO: Figure out if frame can ever start at negative bounds...
// if (x1 != 0 || y1 != 0) {
// return false;
// }
int y2 = pStream.readUnsignedShort();
int x2 = pStream.readUnsignedShort();

View File

@ -46,8 +46,8 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static com.twelvemonkeys.imageio.plugins.pict.PICTImageReaderSpi.isOtherFormat;
import static org.junit.Assert.*;
/**
* ICOImageReaderTestCase
@ -116,6 +116,38 @@ public class PICTImageReaderTest extends ImageReaderAbstractTest<PICTImageReader
return Arrays.asList("image/pict", "image/x-pict");
}
@Test
public void testIsOtherFormat() throws IOException {
assertFalse(isOtherFormat(new ByteArrayImageInputStream(new byte[8])));
// BMP
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {'B', 'M', 0, 0, 0, 0, 0, 0})));
// GIF
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {'G', 'I', 'F', '8', '7', 'a', 0, 0})));
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {'G', 'I', 'F', '8', '9', 'a', 0, 0})));
// JPEG (JFIF, EXIF, "raw")
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {(byte) 0xFF, (byte) 0xD8, (byte) 0xFF, (byte) 0xE0, 0, 0, 0, 0})));
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {(byte) 0xFF, (byte) 0xD8, (byte) 0xFF, (byte) 0xE1, 0, 0, 0, 0})));
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {(byte) 0xFF, (byte) 0xD8, (byte) 0xFF, (byte) 0xDB, 0, 0, 0, 0})));
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {(byte) 0xFF, (byte) 0xD8, (byte) 0xFF, (byte) 0xC4, 0, 0, 0, 0})));
// PNG
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {(byte) 0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A})));
// PSD
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {'8', 'B', 'P', 'S', 0, 0, 0, 0})));
// TIFF (Little Endian, Big Endian)
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {'I', 'I', 42, 0, 0, 0, 0, 0})));
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {'M', 'M', 0, 42, 0, 0, 0, 0})));
// BigTIFF (Little Endian, Big Endian)
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {'I', 'I', 43, 0, 0, 0, 0, 0})));
assertTrue(isOtherFormat(new ByteArrayImageInputStream(new byte[] {'M', 'M', 0, 43, 0, 0, 0, 0})));
}
@Ignore("Known issue")
@Test
@Override
@ -148,6 +180,10 @@ public class PICTImageReaderTest extends ImageReaderAbstractTest<PICTImageReader
new TestData(getClassLoaderResource("/jpeg/R-7439-1151526181.jpeg"),
new Dimension(386, 396)
)));
assertFalse(sProvider.canDecodeInput(
new TestData(getClassLoaderResource("/jpeg/89497426-adc19a00-d7ff-11ea-8ad1-0cbcd727b62a.jpeg"),
new Dimension(640, 480)
)));
}
@Test

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB