Browse Source

Update CCITT EOL handling to cope with fill.

DEVSIX-5565

Autoported commit.
Original commit hash: [acdfde501]
master
glenn.volckaert 3 years ago
committed by iText Software
parent
commit
227d7532c8
  1. 29
      itext.tests/itext.io.tests/itext/io/image/TiffTest.cs
  2. 38
      itext.tests/itext.kernel.tests/itext/kernel/pdf/xobject/PdfImageXObjectTest.cs
  3. 1
      itext.tests/itext.kernel.tests/resources/itext/kernel/pdf/xobject/PdfImageXObjectTest/cmp_group3CompTiffImgNoRecoverErrorAndNotDirect.pdf
  4. 1
      itext.tests/itext.kernel.tests/resources/itext/kernel/pdf/xobject/PdfImageXObjectTest/cmp_group3CompressionTiffImage.pdf
  5. 74
      itext/itext.io/itext/io/codec/TIFFFaxDecoder.cs
  6. 2
      port-hash

29
itext.tests/itext.io.tests/itext/io/image/TiffTest.cs

@ -118,20 +118,17 @@ namespace iText.IO.Image {
[NUnit.Framework.Test]
public virtual void Group3CompressionCreateTiffImageTest() {
// TODO: DEVSIX-5565 (update test when support for T4 compression tiff image will be realized)
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
.CreateTiff(UrlUtil.ToURL(SOURCE_FOLDER + "group3CompressionImage.tif"), false, 1, false));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
), e.Message);
String sourceFile = SOURCE_FOLDER + "group3CompressionImage.tif";
CreateTiff(sourceFile, 1, 1024D, 768D);
}
[NUnit.Framework.Test]
public virtual void Group3CompressionCreateImageDataTest() {
// TODO: DEVSIX-5565 (update test when support for T4 compression tiff image will be realized)
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
.Create(UrlUtil.ToURL(SOURCE_FOLDER + "group3CompressionImage.tif")));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
), e.Message);
String sourceFile = SOURCE_FOLDER + "group3CompressionImage.tif";
ImageData img = ImageDataFactory.Create(UrlUtil.ToURL(SOURCE_FOLDER + "group3CompressionImage.tif"));
NUnit.Framework.Assert.AreEqual(1024, img.GetWidth(), 0);
NUnit.Framework.Assert.AreEqual(768, img.GetHeight(), 0);
NUnit.Framework.Assert.AreEqual(1, img.GetBpc());
}
[NUnit.Framework.Test]
@ -172,7 +169,7 @@ namespace iText.IO.Image {
[NUnit.Framework.Test]
public virtual void AdobeDeflateComp16BitMinIsBlackCreateTiffTest() {
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
// TODO: DEVSIX-5791 (update test when support for adobeDeflate compression tiff image will be realized)
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
.CreateTiff(UrlUtil.ToURL(SOURCE_FOLDER + "adobeDeflateCompression16BitMinIsBlack.tif"), false, 1, false
));
@ -182,7 +179,7 @@ namespace iText.IO.Image {
[NUnit.Framework.Test]
public virtual void AdobeDeflateComp16BitMinIsBlackCreateImageTest() {
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
// TODO: DEVSIX-5791 (update test when support for adobeDeflate compression tiff image will be realized)
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
.Create(UrlUtil.ToURL(SOURCE_FOLDER + "adobeDeflateCompression16BitMinIsBlack.tif")));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
@ -191,7 +188,7 @@ namespace iText.IO.Image {
[NUnit.Framework.Test]
public virtual void AdobeDeflateComp16BitMinIsWhiteCreateTiffTest() {
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
// TODO: DEVSIX-5791 (update test when support for adobeDeflate compression tiff image will be realized)
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
.CreateTiff(UrlUtil.ToURL(SOURCE_FOLDER + "adobeDeflateCompression16BitMinIsWhite.tif"), false, 1, false
));
@ -201,7 +198,7 @@ namespace iText.IO.Image {
[NUnit.Framework.Test]
public virtual void AdobeDeflateComp16BitMinIsWhiteCreateImageTest() {
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
// TODO: DEVSIX-5791 (update test when support for adobeDeflate compression tiff image will be realized)
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
.Create(UrlUtil.ToURL(SOURCE_FOLDER + "adobeDeflateCompression16BitMinIsWhite.tif")));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
@ -210,7 +207,7 @@ namespace iText.IO.Image {
[NUnit.Framework.Test]
public virtual void AdobeDeflateCompression16BitRgbCreateTiffTest() {
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
// TODO: DEVSIX-5791 (update test when support for adobeDeflate compression tiff image will be realized)
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
.CreateTiff(UrlUtil.ToURL(SOURCE_FOLDER + "adobeDeflateCompression16BitRgb.tif"), false, 1, false));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
@ -219,7 +216,7 @@ namespace iText.IO.Image {
[NUnit.Framework.Test]
public virtual void AdobeDeflateCompression16BitRgbCreateImageTest() {
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
// TODO: DEVSIX-5791 (update test when support for adobeDeflate compression tiff image will be realized)
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
.Create(UrlUtil.ToURL(SOURCE_FOLDER + "adobeDeflateCompression16BitRgb.tif")));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage

38
itext.tests/itext.kernel.tests/itext/kernel/pdf/xobject/PdfImageXObjectTest.cs

@ -41,7 +41,6 @@ For more information, please contact iText Software Corp. at this
address: sales@itextpdf.com
*/
using System;
using iText.Commons.Utils;
using iText.IO.Image;
using iText.IO.Util;
using iText.Kernel.Geom;
@ -141,12 +140,9 @@ namespace iText.Kernel.Pdf.Xobject {
[NUnit.Framework.Test]
public virtual void Group3CompressionTiffImageTest() {
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
String image = SOURCE_FOLDER + "group3CompressionImage.tif";
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
.Create(UrlUtil.ToURL(image)));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
), e.Message);
ConvertAndCompare(DESTINATION_FOLDER + "group3CompressionTiffImage.pdf", SOURCE_FOLDER + "cmp_group3CompressionTiffImage.pdf"
, new PdfImageXObject(ImageDataFactory.Create(UrlUtil.ToURL(image))));
}
[NUnit.Framework.Test]
@ -167,17 +163,16 @@ namespace iText.Kernel.Pdf.Xobject {
[NUnit.Framework.Test]
public virtual void Group3CompTiffImgNoRecoverErrorAndNotDirectTest() {
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
String image = SOURCE_FOLDER + "group3CompressionImage.tif";
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
.CreateTiff(UrlUtil.ToURL(image), false, 1, false));
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
), e.Message);
ConvertAndCompare(DESTINATION_FOLDER + "group3CompTiffImgNoRecoverErrorAndNotDirect.pdf", SOURCE_FOLDER +
"cmp_group3CompTiffImgNoRecoverErrorAndNotDirect.pdf", new PdfImageXObject(ImageDataFactory.CreateTiff
(UrlUtil.ToURL(image), false, 1, false)));
}
private void ConvertAndCompare(String outFilename, String cmpFilename, String imageFilename) {
System.Console.Out.WriteLine("Out pdf: " + UrlUtil.GetNormalizedFileUriString(outFilename));
System.Console.Out.WriteLine("Cmp pdf: " + UrlUtil.GetNormalizedFileUriString(cmpFilename) + "\n");
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFilename));
PdfDocument cmpDoc = new PdfDocument(new PdfReader(cmpFilename));
PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(imageFilename));
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
canvas.AddXObjectFittedIntoRectangle(imageXObject, new Rectangle(50, 500, 346, imageXObject.GetHeight()));
@ -185,6 +180,25 @@ namespace iText.Kernel.Pdf.Xobject {
PdfDocument outDoc = new PdfDocument(new PdfReader(outFilename));
PdfStream outStream = outDoc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName
("Im1"));
PdfDocument cmpDoc = new PdfDocument(new PdfReader(cmpFilename));
PdfStream cmpStream = cmpDoc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName
("Im1"));
NUnit.Framework.Assert.IsNull(new CompareTool().CompareStreamsStructure(outStream, cmpStream));
cmpDoc.Close();
outDoc.Close();
}
private void ConvertAndCompare(String outFilename, String cmpFilename, PdfImageXObject imageXObject) {
System.Console.Out.WriteLine("Out pdf: " + UrlUtil.GetNormalizedFileUriString(outFilename));
System.Console.Out.WriteLine("Cmp pdf: " + UrlUtil.GetNormalizedFileUriString(cmpFilename) + "\n");
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFilename));
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
canvas.AddXObjectFittedIntoRectangle(imageXObject, new Rectangle(10, 20, 575, 802));
pdfDoc.Close();
PdfDocument outDoc = new PdfDocument(new PdfReader(outFilename));
PdfStream outStream = outDoc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName
("Im1"));
PdfDocument cmpDoc = new PdfDocument(new PdfReader(cmpFilename));
PdfStream cmpStream = cmpDoc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName
("Im1"));
NUnit.Framework.Assert.IsNull(new CompareTool().CompareStreamsStructure(outStream, cmpStream));

1
itext.tests/itext.kernel.tests/resources/itext/kernel/pdf/xobject/PdfImageXObjectTest/cmp_group3CompTiffImgNoRecoverErrorAndNotDirect.pdf

@ -0,0 +1 @@

1
itext.tests/itext.kernel.tests/resources/itext/kernel/pdf/xobject/PdfImageXObjectTest/cmp_group3CompressionTiffImage.pdf

@ -0,0 +1 @@

74
itext/itext.io/itext/io/codec/TIFFFaxDecoder.cs

@ -802,7 +802,7 @@ namespace iText.IO.Codec {
uncompressedMode = (int)((tiffT4Options & 0x02) >> 1);
fillBits = (int)((tiffT4Options & 0x04) >> 2);
// The data must start with an EOL code
if (ReadEOL(true) != 1) {
if (ReadEOL() != 1) {
throw new iText.IO.Exceptions.IOException(iText.IO.Exceptions.IOException.FirstScanlineMustBe1dEncoded);
}
int lineOffset = 0;
@ -814,7 +814,7 @@ namespace iText.IO.Codec {
for (int lines = 1; lines < height; lines++) {
// Every line must begin with an EOL followed by a bit which
// indicates whether the following scanline is 1D or 2D encoded.
if (ReadEOL(false) == 0) {
if (ReadEOL() == 0) {
// 2D encoded scanline follows
// Initialize previous scanlines changing elements, and
// initialize current scanline's changing elements array
@ -1285,57 +1285,16 @@ escape_break: ;
return runLength;
}
private int ReadEOL(bool isFirstEOL) {
if (fillBits == 0) {
int next12Bits = NextNBits(12);
if (isFirstEOL && next12Bits == 0) {
// Might have the case of EOL padding being used even
// though it was not flagged in the T4Options field.
// This was observed to be the case in TIFFs produced
// by a well known vendor who shall remain nameless.
if (NextNBits(4) == 1) {
// EOL must be padded: reset the fillBits flag.
fillBits = 1;
return 1;
}
}
if (next12Bits != 1) {
throw new iText.IO.Exceptions.IOException(iText.IO.Exceptions.IOException.ScanlineMustBeginWithEolCodeWord
);
}
private int ReadEOL() {
// scan to first none 0 bit and return 12 bits
while (NextLesserThan8Bits(1) == 0) {
}
else {
if (fillBits == 1) {
// First EOL code word xxxx 0000 0000 0001 will occur
// As many fill bits will be present as required to make
// the EOL code of 12 bits end on a byte boundary.
int bitsLeft = 8 - bitPointer;
if (NextNBits(bitsLeft) != 0) {
throw new iText.IO.Exceptions.IOException(iText.IO.Exceptions.IOException.AllFillBitsPrecedingEolCodeMustBe0
);
}
// If the number of bitsLeft is less than 8, then to have a 12
// bit EOL sequence, two more bytes are certainly going to be
// required. The first of them has to be all zeros, so ensure
// that.
if (bitsLeft < 4) {
if (NextNBits(8) != 0) {
throw new iText.IO.Exceptions.IOException(iText.IO.Exceptions.IOException.AllFillBitsPrecedingEolCodeMustBe0
);
}
}
// There might be a random number of fill bytes with 0s, so
// loop till the EOL of 0000 0001 is found, as long as all
// the bytes preceding it are 0's.
int n;
while ((n = NextNBits(8)) != 1) {
// If not all zeros
if (n != 0) {
throw new iText.IO.Exceptions.IOException(iText.IO.Exceptions.IOException.AllFillBitsPrecedingEolCodeMustBe0
);
}
}
}
// nothing to do here
UpdatePointer(12);
int next12Bits = NextNBits(12);
if (next12Bits != 1) {
throw new iText.IO.Exceptions.IOException(iText.IO.Exceptions.IOException.AllFillBitsPrecedingEolCodeMustBe0
);
}
// If one dimensional encoding mode, then always return 1
if (oneD == 0) {
@ -1513,14 +1472,9 @@ escape_break: ;
// Move pointer backwards by given amount of bits
private void UpdatePointer(int bitsToMoveBack) {
int i = bitPointer - bitsToMoveBack;
if (i < 0) {
bytePointer--;
bitPointer = 8 + i;
}
else {
bitPointer = i;
}
int totalBits = bytePointer * 8 + bitPointer - bitsToMoveBack;
bitPointer = totalBits % 8;
bytePointer = totalBits / 8;
}
// Move to the next byte boundary

2
port-hash

@ -1 +1 @@
1f84b91f57fea314a01c7d9f94937b87303c47b4
acdfde5011ab4695d84ba098c57ba0fef546c862
Loading…
Cancel
Save