diff --git a/imageio/imageio-tiff/src/main/java/com/twelvemonkeys/imageio/plugins/tiff/CCITTFaxDecoderStream.java b/imageio/imageio-tiff/src/main/java/com/twelvemonkeys/imageio/plugins/tiff/CCITTFaxDecoderStream.java index 16ac23b8..7ca032a2 100644 --- a/imageio/imageio-tiff/src/main/java/com/twelvemonkeys/imageio/plugins/tiff/CCITTFaxDecoderStream.java +++ b/imageio/imageio-tiff/src/main/java/com/twelvemonkeys/imageio/plugins/tiff/CCITTFaxDecoderStream.java @@ -88,8 +88,8 @@ final class CCITTFaxDecoderStream extends FilterInputStream { fillOrder, "Expected fill order 1 or 2: %s" ); - this.changesReferenceRow = new int[columns + 1]; - this.changesCurrentRow = new int[columns + 1]; + this.changesReferenceRow = new int[columns + 2]; + this.changesCurrentRow = new int[columns + 2]; switch (type) { 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); if (start > 2) { start -= 2; diff --git a/imageio/imageio-tiff/src/test/java/com/twelvemonkeys/imageio/plugins/tiff/CCITTFaxDecoderStreamTest.java b/imageio/imageio-tiff/src/test/java/com/twelvemonkeys/imageio/plugins/tiff/CCITTFaxDecoderStreamTest.java index 599d26f7..d5ded55a 100644 --- a/imageio/imageio-tiff/src/test/java/com/twelvemonkeys/imageio/plugins/tiff/CCITTFaxDecoderStreamTest.java +++ b/imageio/imageio-tiff/src/test/java/com/twelvemonkeys/imageio/plugins/tiff/CCITTFaxDecoderStreamTest.java @@ -233,7 +233,8 @@ public class CCITTFaxDecoderStreamTest { // Produces an CCITT Stream with 9 changes on 8 columns. byte[] data = new byte[] {(byte) 0b10101010}; 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.close(); @@ -243,4 +244,21 @@ public class CCITTFaxDecoderStreamTest { byte decoded = (byte) inputStream.read(); 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); + } } diff --git a/imageio/imageio-tiff/src/test/java/com/twelvemonkeys/imageio/plugins/tiff/TIFFImageReaderTest.java b/imageio/imageio-tiff/src/test/java/com/twelvemonkeys/imageio/plugins/tiff/TIFFImageReaderTest.java index f902c61d..1591bd60 100644 --- a/imageio/imageio-tiff/src/test/java/com/twelvemonkeys/imageio/plugins/tiff/TIFFImageReaderTest.java +++ b/imageio/imageio-tiff/src/test/java/com/twelvemonkeys/imageio/plugins/tiff/TIFFImageReaderTest.java @@ -109,6 +109,7 @@ public class TIFFImageReaderTest extends ImageReaderAbstractTest