Skip to content

Commit fab4e59

Browse files
introfogiText-CI
authored andcommitted
Add support processing inline image with ICCBased color space in resources
DEVSIX-5295 Autoported commit. Original commit hash: [df4013c8f]
1 parent ce57a17 commit fab4e59

File tree

4 files changed

+190
-45
lines changed

4 files changed

+190
-45
lines changed

itext.tests/itext.kernel.tests/itext/kernel/pdf/canvas/parser/PdfCanvasProcessorIntegrationTest.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,20 @@ source product.
5656

5757
namespace iText.Kernel.Pdf.Canvas.Parser {
5858
public class PdfCanvasProcessorIntegrationTest : ExtendedITextTest {
59-
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
59+
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
6060
.CurrentContext.TestDirectory) + "/resources/itext/kernel/parser/PdfCanvasProcessorTest/";
6161

62+
private static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
63+
+ "/test/itext/kernel/parser/PdfCanvasProcessorTest/";
64+
65+
[NUnit.Framework.OneTimeSetUp]
66+
public static void SetUp() {
67+
CreateDestinationFolder(DESTINATION_FOLDER);
68+
}
69+
6270
[NUnit.Framework.Test]
6371
public virtual void ContentStreamProcessorTest() {
64-
PdfDocument document = new PdfDocument(new PdfReader(sourceFolder + "tableWithImageAndText.pdf"), new PdfWriter
72+
PdfDocument document = new PdfDocument(new PdfReader(SOURCE_FOLDER + "tableWithImageAndText.pdf"), new PdfWriter
6573
(new ByteArrayOutputStream()));
6674
StringBuilder pageEventsLog = new StringBuilder();
6775
for (int i = 1; i <= document.GetNumberOfPages(); ++i) {
@@ -70,7 +78,7 @@ public virtual void ContentStreamProcessorTest() {
7078
(pageEventsLog));
7179
processor.ProcessPageContent(page);
7280
}
73-
byte[] logBytes = File.ReadAllBytes(System.IO.Path.Combine(sourceFolder + "contentStreamProcessorTest_events_log.dat"
81+
byte[] logBytes = File.ReadAllBytes(System.IO.Path.Combine(SOURCE_FOLDER + "contentStreamProcessorTest_events_log.dat"
7482
));
7583
String expectedPageEventsLog = iText.IO.Util.JavaUtil.GetStringForBytes(logBytes, System.Text.Encoding.UTF8
7684
);
@@ -79,7 +87,7 @@ public virtual void ContentStreamProcessorTest() {
7987

8088
[NUnit.Framework.Test]
8189
public virtual void ProcessGraphicsStateResourceOperatorFillOpacityTest() {
82-
PdfDocument document = new PdfDocument(new PdfReader(sourceFolder + "transparentText.pdf"));
90+
PdfDocument document = new PdfDocument(new PdfReader(SOURCE_FOLDER + "transparentText.pdf"));
8391
float? expOpacity = 0.5f;
8492
IDictionary<String, Object> textRenderInfo = new Dictionary<String, Object>();
8593
for (int i = 1; i <= document.GetNumberOfPages(); ++i) {
@@ -94,7 +102,7 @@ public virtual void ProcessGraphicsStateResourceOperatorFillOpacityTest() {
94102

95103
[NUnit.Framework.Test]
96104
public virtual void ProcessGraphicsStateResourceOperatorStrokeOpacityTest() {
97-
PdfDocument document = new PdfDocument(new PdfReader(sourceFolder + "hiddenText.pdf"));
105+
PdfDocument document = new PdfDocument(new PdfReader(SOURCE_FOLDER + "hiddenText.pdf"));
98106
float? expOpacity = 0.0f;
99107
IDictionary<String, Object> textRenderInfo = new Dictionary<String, Object>();
100108
for (int i = 1; i <= document.GetNumberOfPages(); ++i) {
@@ -110,7 +118,7 @@ public virtual void ProcessGraphicsStateResourceOperatorStrokeOpacityTest() {
110118
[NUnit.Framework.Test]
111119
public virtual void TestClosingEmptyPath() {
112120
String fileName = "closingEmptyPath.pdf";
113-
PdfDocument document = new PdfDocument(new PdfReader(sourceFolder + fileName));
121+
PdfDocument document = new PdfDocument(new PdfReader(SOURCE_FOLDER + fileName));
114122
PdfCanvasProcessor processor = new PdfCanvasProcessor(new PdfCanvasProcessorIntegrationTest.NoOpEventListener
115123
());
116124
// Assert than no exception is thrown when an empty path is handled
@@ -121,7 +129,7 @@ public virtual void TestClosingEmptyPath() {
121129
[LogMessage(iText.IO.LogMessageConstant.FAILED_TO_PROCESS_A_TRANSFORMATION_MATRIX, Count = 1)]
122130
public virtual void TestNoninvertibleMatrix() {
123131
String fileName = "noninvertibleMatrix.pdf";
124-
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + fileName));
132+
PdfDocument pdfDocument = new PdfDocument(new PdfReader(SOURCE_FOLDER + fileName));
125133
LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
126134
PdfCanvasProcessor processor = new PdfCanvasProcessor(strategy);
127135
PdfPage page = pdfDocument.GetFirstPage();
@@ -138,7 +146,7 @@ public virtual void TestNoninvertibleMatrix() {
138146
public virtual void ParseCircularReferencesInResourcesTest() {
139147
NUnit.Framework.Assert.That(() => {
140148
String fileName = "circularReferencesInResources.pdf";
141-
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + fileName));
149+
PdfDocument pdfDocument = new PdfDocument(new PdfReader(SOURCE_FOLDER + fileName));
142150
PdfCanvasProcessor processor = new PdfCanvasProcessor(new PdfCanvasProcessorIntegrationTest.NoOpEventListener
143151
());
144152
PdfPage page = pdfDocument.GetFirstPage();
@@ -152,7 +160,7 @@ public virtual void ParseCircularReferencesInResourcesTest() {
152160
[NUnit.Framework.Test]
153161
[LogMessage(KernelLogMessageConstant.UNABLE_TO_PARSE_COLOR_WITHIN_COLORSPACE)]
154162
public virtual void PatternColorParsingNotValidPdfTest() {
155-
String inputFile = sourceFolder + "patternColorParsingNotValidPdfTest.pdf";
163+
String inputFile = SOURCE_FOLDER + "patternColorParsingNotValidPdfTest.pdf";
156164
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFile));
157165
for (int i = 1; i <= pdfDocument.GetNumberOfPages(); ++i) {
158166
PdfPage page = pdfDocument.GetPage(i);
@@ -167,7 +175,7 @@ public virtual void PatternColorParsingNotValidPdfTest() {
167175

168176
[NUnit.Framework.Test]
169177
public virtual void PatternColorParsingValidPdfTest() {
170-
String inputFile = sourceFolder + "patternColorParsingValidPdfTest.pdf";
178+
String inputFile = SOURCE_FOLDER + "patternColorParsingValidPdfTest.pdf";
171179
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inputFile));
172180
for (int i = 1; i <= pdfDocument.GetNumberOfPages(); ++i) {
173181
PdfPage page = pdfDocument.GetPage(i);
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: Bruno Lowagie, Paulo Soares, et al.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
using iText.IO.Util;
44+
using iText.Kernel;
45+
using iText.Kernel.Pdf;
46+
using iText.Test;
47+
48+
namespace iText.Kernel.Pdf.Canvas.Parser.Util {
49+
public class InlineImageParsingUtilsTest : ExtendedITextTest {
50+
[NUnit.Framework.Test]
51+
public virtual void IccBasedCsTest() {
52+
PdfName colorSpace = PdfName.ICCBased;
53+
PdfDictionary dictionary = new PdfDictionary();
54+
PdfArray array = new PdfArray();
55+
array.Add(colorSpace);
56+
PdfStream stream = new PdfStream();
57+
stream.Put(PdfName.N, new PdfNumber(4));
58+
array.Add(stream);
59+
dictionary.Put(colorSpace, array);
60+
NUnit.Framework.Assert.AreEqual(4, InlineImageParsingUtils.GetComponentsPerPixel(colorSpace, dictionary));
61+
}
62+
63+
[NUnit.Framework.Test]
64+
public virtual void IndexedCsTest() {
65+
PdfName colorSpace = PdfName.Indexed;
66+
PdfDictionary dictionary = new PdfDictionary();
67+
PdfArray array = new PdfArray();
68+
array.Add(colorSpace);
69+
dictionary.Put(colorSpace, array);
70+
NUnit.Framework.Assert.AreEqual(1, InlineImageParsingUtils.GetComponentsPerPixel(colorSpace, dictionary));
71+
}
72+
73+
[NUnit.Framework.Test]
74+
public virtual void CsInDictAsNameTest() {
75+
PdfName colorSpace = PdfName.ICCBased;
76+
PdfDictionary dictionary = new PdfDictionary();
77+
dictionary.Put(colorSpace, PdfName.DeviceCMYK);
78+
NUnit.Framework.Assert.AreEqual(4, InlineImageParsingUtils.GetComponentsPerPixel(colorSpace, dictionary));
79+
}
80+
81+
[NUnit.Framework.Test]
82+
public virtual void CsInDictAsNameNullTest() {
83+
PdfName colorSpace = PdfName.ICCBased;
84+
PdfDictionary dictionary = new PdfDictionary();
85+
NUnit.Framework.Assert.That(() => {
86+
InlineImageParsingUtils.GetComponentsPerPixel(colorSpace, dictionary);
87+
}
88+
, NUnit.Framework.Throws.InstanceOf<InlineImageParsingUtils.InlineImageParseException>().With.Message.EqualTo(MessageFormatUtil.Format(PdfException.UnexpectedColorSpace1, "/ICCBased")))
89+
;
90+
}
91+
92+
[NUnit.Framework.Test]
93+
public virtual void NotSupportedCsWithCsDictionaryTest() {
94+
PdfName colorSpace = PdfName.ICCBased;
95+
PdfDictionary dictionary = new PdfDictionary();
96+
PdfArray array = new PdfArray();
97+
array.Add(PdfName.Pattern);
98+
PdfStream stream = new PdfStream();
99+
stream.Put(PdfName.N, new PdfNumber(4));
100+
array.Add(stream);
101+
dictionary.Put(colorSpace, array);
102+
NUnit.Framework.Assert.That(() => {
103+
InlineImageParsingUtils.GetComponentsPerPixel(colorSpace, dictionary);
104+
}
105+
, NUnit.Framework.Throws.InstanceOf<InlineImageParsingUtils.InlineImageParseException>().With.Message.EqualTo(MessageFormatUtil.Format(PdfException.UnexpectedColorSpace1, "/ICCBased")))
106+
;
107+
}
108+
109+
[NUnit.Framework.Test]
110+
public virtual void NullCsTest() {
111+
NUnit.Framework.Assert.AreEqual(1, InlineImageParsingUtils.GetComponentsPerPixel(null, null));
112+
}
113+
114+
[NUnit.Framework.Test]
115+
public virtual void DeviceGrayCsTest() {
116+
PdfName colorSpace = PdfName.DeviceGray;
117+
NUnit.Framework.Assert.AreEqual(1, InlineImageParsingUtils.GetComponentsPerPixel(colorSpace, null));
118+
}
119+
120+
[NUnit.Framework.Test]
121+
public virtual void DeviceRGBCsTest() {
122+
PdfName colorSpace = PdfName.DeviceRGB;
123+
NUnit.Framework.Assert.AreEqual(3, InlineImageParsingUtils.GetComponentsPerPixel(colorSpace, null));
124+
}
125+
126+
[NUnit.Framework.Test]
127+
public virtual void DeviceCMYKCsTest() {
128+
PdfName colorSpace = PdfName.DeviceCMYK;
129+
NUnit.Framework.Assert.AreEqual(4, InlineImageParsingUtils.GetComponentsPerPixel(colorSpace, null));
130+
}
131+
}
132+
}

itext/itext.kernel/itext/kernel/pdf/canvas/parser/util/InlineImageParsingUtils.cs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,45 @@ public static PdfStream Parse(PdfCanvasParser ps, PdfDictionary colorSpaceDic) {
137137
return inlineImageAsStreamObject;
138138
}
139139

140+
/// <param name="colorSpaceName">the name of the color space. If null, a bi-tonal (black and white) color space is assumed.
141+
/// </param>
142+
/// <returns>the components per pixel for the specified color space</returns>
143+
internal static int GetComponentsPerPixel(PdfName colorSpaceName, PdfDictionary colorSpaceDic) {
144+
if (colorSpaceName == null) {
145+
return 1;
146+
}
147+
if (colorSpaceName.Equals(PdfName.DeviceGray)) {
148+
return 1;
149+
}
150+
if (colorSpaceName.Equals(PdfName.DeviceRGB)) {
151+
return 3;
152+
}
153+
if (colorSpaceName.Equals(PdfName.DeviceCMYK)) {
154+
return 4;
155+
}
156+
if (colorSpaceDic != null) {
157+
PdfArray colorSpace = colorSpaceDic.GetAsArray(colorSpaceName);
158+
if (colorSpace != null) {
159+
if (PdfName.Indexed.Equals(colorSpace.GetAsName(0))) {
160+
return 1;
161+
}
162+
else {
163+
if (PdfName.ICCBased.Equals(colorSpace.GetAsName(0))) {
164+
return colorSpace.GetAsStream(1).GetAsNumber(PdfName.N).IntValue();
165+
}
166+
}
167+
}
168+
else {
169+
PdfName tempName = colorSpaceDic.GetAsName(colorSpaceName);
170+
if (tempName != null) {
171+
return GetComponentsPerPixel(tempName, colorSpaceDic);
172+
}
173+
}
174+
}
175+
throw new InlineImageParsingUtils.InlineImageParseException(PdfException.UnexpectedColorSpace1).SetMessageParams
176+
(colorSpaceName);
177+
}
178+
140179
/// <summary>Parses the next inline image dictionary from the parser.</summary>
141180
/// <remarks>
142181
/// Parses the next inline image dictionary from the parser. The parser must be positioned immediately following the BI operator.
@@ -200,40 +239,6 @@ private static PdfObject GetAlternateValue(PdfName key, PdfObject value) {
200239
return value;
201240
}
202241

203-
/// <param name="colorSpaceName">the name of the color space. If null, a bi-tonal (black and white) color space is assumed.
204-
/// </param>
205-
/// <returns>the components per pixel for the specified color space</returns>
206-
private static int GetComponentsPerPixel(PdfName colorSpaceName, PdfDictionary colorSpaceDic) {
207-
if (colorSpaceName == null) {
208-
return 1;
209-
}
210-
if (colorSpaceName.Equals(PdfName.DeviceGray)) {
211-
return 1;
212-
}
213-
if (colorSpaceName.Equals(PdfName.DeviceRGB)) {
214-
return 3;
215-
}
216-
if (colorSpaceName.Equals(PdfName.DeviceCMYK)) {
217-
return 4;
218-
}
219-
if (colorSpaceDic != null) {
220-
PdfArray colorSpace = colorSpaceDic.GetAsArray(colorSpaceName);
221-
if (colorSpace != null) {
222-
if (PdfName.Indexed.Equals(colorSpace.GetAsName(0))) {
223-
return 1;
224-
}
225-
}
226-
else {
227-
PdfName tempName = colorSpaceDic.GetAsName(colorSpaceName);
228-
if (tempName != null) {
229-
return GetComponentsPerPixel(tempName, colorSpaceDic);
230-
}
231-
}
232-
}
233-
throw new InlineImageParsingUtils.InlineImageParseException(PdfException.UnexpectedColorSpace1).SetMessageParams
234-
(colorSpaceName);
235-
}
236-
237242
/// <summary>Computes the number of unfiltered bytes that each row of the image will contain.</summary>
238243
/// <remarks>
239244
/// Computes the number of unfiltered bytes that each row of the image will contain.

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20c774d6cb6af3d170b1ac1dbc2ae37cff2e6b48
1+
df4013c8f141059a91373008ac4a7013f6be0852

0 commit comments

Comments
 (0)