Skip to content

Commit cf285c1

Browse files
committed
Support EncodedByteAlign=true for CCITTFaxDecodeFilter with k < 0
DEVSIX-1689
1 parent 06f3273 commit cf285c1

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

io/src/main/java/com/itextpdf/io/IOException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public class IOException extends RuntimeException {
9292
public static final String ExpectedIhdrMarker = "Expected IHDR marker.";
9393
public static final String ExpectedJp2hMarker = "Expected JP2H marker.";
9494
public static final String ExpectedJpMarker = "Expected JP marker.";
95+
public static final String ExpectedTrailingZeroBitsForByteAlignedLines = "Expected trailing zero bits for byte-aligned lines";
9596
public static final String ExtraSamplesAreNotSupported = "Extra samples are not supported.";
9697
public static final String FdfStartxrefNotFound = "FDF startxref not found.";
9798
public static final String FirstScanlineMustBe1dEncoded = "First scanline must be 1D encoded.";

io/src/main/java/com/itextpdf/io/codec/TIFFConstants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ public class TIFFConstants {
427427
* data not compressed
428428
*/
429429
public static final int GROUP4OPT_UNCOMPRESSED = 0x2;
430+
/**
431+
* fill to byte boundary
432+
*/
433+
public static final int GROUP4OPT_FILLBITS = 0x4;
430434
/**
431435
* units of resolutions
432436
*/

io/src/main/java/com/itextpdf/io/codec/TIFFFaxDecoder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,7 @@ public void decodeT6(byte[] buffer,
941941
// has not been tested due to lack of test images using this optional
942942

943943
uncompressedMode = (int) ((tiffT6Options & 0x02) >> 1);
944+
fillBits = (int) ((tiffT6Options & 0x04) >> 2);
944945

945946
// Local cached reference
946947
int[] cce = currChangingElems;
@@ -971,6 +972,17 @@ public void decodeT6(byte[] buffer,
971972
// Start decoding the scanline at startX in the raster
972973
bitOffset = startX;
973974

975+
if (fillBits == 1) {
976+
// filter shall expect extra 0 bits before each
977+
// encoded line so that the line begins on a byte boundary
978+
if (bitPointer > 0) {
979+
int bitsLeft = 8 - bitPointer;
980+
if (nextNBits(bitsLeft) != 0) {
981+
throw new IOException(IOException.ExpectedTrailingZeroBitsForByteAlignedLines);
982+
}
983+
}
984+
}
985+
974986
// Reset search start position for getNextChangingElement
975987
lastChangingElement = 0;
976988

kernel/src/main/java/com/itextpdf/kernel/pdf/filters/CCITTFaxDecodeFilter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ public byte[] decode(byte[] b, PdfName filterName, PdfObject decodeParams, PdfDi
100100
}
101101
}
102102
else {
103+
long tiffT6Options = 0;
104+
tiffT6Options |= byteAlign ? TIFFConstants.GROUP4OPT_FILLBITS : 0;
103105
TIFFFaxDecoder deca = new TIFFFaxDecoder(1, width, height);
104-
deca.decodeT6(outBuf, b, 0, height, 0);
106+
deca.decodeT6(outBuf, b, 0, height, tiffT6Options);
105107
}
106108
if (!blackIs1) {
107109
int len = outBuf.length;

0 commit comments

Comments
 (0)