#328 CCITTFaxDecoderStream AIOOBE fix

This commit is contained in:
Harald Kuhr 2017-03-09 20:33:11 +01:00
parent 97cd8a955f
commit 12050dfe61
4 changed files with 23 additions and 4 deletions

View File

@ -88,8 +88,8 @@ final class CCITTFaxDecoderStream extends FilterInputStream {
fillOrder, "Expected fill order 1 or 2: %s" fillOrder, "Expected fill order 1 or 2: %s"
); );
this.changesReferenceRow = new int[columns + 1]; this.changesReferenceRow = new int[columns + 2];
this.changesCurrentRow = new int[columns + 1]; this.changesCurrentRow = new int[columns + 2];
switch (type) { switch (type) {
case TIFFExtension.COMPRESSION_CCITT_T4: case TIFFExtension.COMPRESSION_CCITT_T4:
@ -220,7 +220,7 @@ final class CCITTFaxDecoderStream extends FilterInputStream {
} }
} }
private int getNextChangingElement(final int a0, final boolean white) throws IOException { private int getNextChangingElement(final int a0, final boolean white) {
int start = (lastChangingElement & 0xFFFF_FFFE) + (white ? 0 : 1); int start = (lastChangingElement & 0xFFFF_FFFE) + (white ? 0 : 1);
if (start > 2) { if (start > 2) {
start -= 2; start -= 2;

View File

@ -233,7 +233,8 @@ public class CCITTFaxDecoderStreamTest {
// Produces an CCITT Stream with 9 changes on 8 columns. // Produces an CCITT Stream with 9 changes on 8 columns.
byte[] data = new byte[] {(byte) 0b10101010}; byte[] data = new byte[] {(byte) 0b10101010};
ByteArrayOutputStream imageOutput = new ByteArrayOutputStream(); ByteArrayOutputStream imageOutput = new ByteArrayOutputStream();
OutputStream outputSteam = new CCITTFaxEncoderStream(imageOutput, 8, 1, TIFFExtension.COMPRESSION_CCITT_T6, 1, 0L); OutputStream outputSteam = new CCITTFaxEncoderStream(imageOutput,
8, 1, TIFFExtension.COMPRESSION_CCITT_T6, 1, 0L);
outputSteam.write(data); outputSteam.write(data);
outputSteam.close(); outputSteam.close();
@ -243,4 +244,21 @@ public class CCITTFaxDecoderStreamTest {
byte decoded = (byte) inputStream.read(); byte decoded = (byte) inputStream.read();
assertEquals(data[0], decoded); assertEquals(data[0], decoded);
} }
@Test
public void testMoreChangesThanColumnsFile() throws IOException {
// See https://github.com/haraldk/TwelveMonkeys/issues/328
// 26 changes on 24 columns: H0w1b, H1w1b, ..., H1w0b
InputStream stream = getClass().getResourceAsStream("/tiff/ccitt-too-many-changes.tif");
// Skip bytes before StripOffsets: 86
for (int i = 0; i < 86; i++) {
stream.read();
}
InputStream inputStream = new CCITTFaxDecoderStream(stream,
24, TIFFExtension.COMPRESSION_CCITT_T6, 1, 0L);
byte decoded = (byte) inputStream.read();
assertEquals((byte) 0b10101010, decoded);
}
} }

View File

@ -109,6 +109,7 @@ public class TIFFImageReaderTest extends ImageReaderAbstractTest<TIFFImageReader
new TestData(getClassLoaderResource("/tiff/ccitt_tolessrows.tif"), new Dimension(6, 6)), // CCITT, metadata claiming 6 rows, stream contains only 4 new TestData(getClassLoaderResource("/tiff/ccitt_tolessrows.tif"), new Dimension(6, 6)), // CCITT, metadata claiming 6 rows, stream contains only 4
new TestData(getClassLoaderResource("/tiff/fivepages-scan-causingerrors.tif"), new Dimension(2480, 3518)), // B/W, CCITT T4 new TestData(getClassLoaderResource("/tiff/fivepages-scan-causingerrors.tif"), new Dimension(2480, 3518)), // B/W, CCITT T4
new TestData(getClassLoaderResource("/tiff/CCITTgetNextChangingElement.tif"), new Dimension(2402,195)), new TestData(getClassLoaderResource("/tiff/CCITTgetNextChangingElement.tif"), new Dimension(2402,195)),
new TestData(getClassLoaderResource("/tiff/ccitt-too-many-changes.tif"), new Dimension(24,153)),
// CIELab // CIELab
new TestData(getClassLoaderResource("/tiff/ColorCheckerCalculator.tif"), new Dimension(798, 546)), // CIELab 8 bit/sample new TestData(getClassLoaderResource("/tiff/ColorCheckerCalculator.tif"), new Dimension(798, 546)), // CIELab 8 bit/sample
// Gray // Gray