Skip to content

Commit ab68ff6

Browse files
committed
Port 503096b: Fix bug with incorrect icc profile in png
If embedded icc profile will have incorrect number of components iText will now ignore it as most image viewers do. Also add new tests. DEV-1922
1 parent b8da94e commit ab68ff6

File tree

12 files changed

+116
-0
lines changed

12 files changed

+116
-0
lines changed

src/core/iTextSharp/text/error_messages/en.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ path.construction.operator.inside.text.object=Path construction or drawing opera
514514
unbalanced.layer.operators=Unbalanced layer operators.
515515
unbalanced.marked.content.operators=Unbalanced marked content operators.
516516
unbalanced.save.restore.state.operators=Unbalanced save/restore state operators.
517+
unexpected.color.space.in.embedded.icc.profile=Unexpected color space in embedded ICC Profile. It will be ignored.
517518
unexpected.end.of.file=Unexpected end of file.
518519
unexpected.eof=Unexpected EOF
519520
unexpected.gt.gt=Unexpected '>>'

src/core/iTextSharp/text/error_messages/nl.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ path.construction.operator.inside.text.object=Pad constructie of drawing operato
514514
unbalanced.layer.operators=Ongebalanceerde operatoren voor layers.
515515
unbalanced.marked.content.operators=Ongebalanceerde marked content operatoren.
516516
unbalanced.save.restore.state.operators=Ongebalanceerde save/restore state operatoren.
517+
unexpected.color.space.in.embedded.icc.profile=Onverwachte kleurenruimte in ingesloten ICC-profiel. Het zal worden genegeerd.
517518
unexpected.end.of.file=Onverwacht einde van het bestand.
518519
unexpected.eof=Onverwachte EOF
519520
unexpected.gt.gt=Onverwachte '>>'

src/core/iTextSharp/text/pdf/codec/PngImage.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ PdfObject GetColorspace() {
456456

457457
Image GetImage() {
458458
ReadPng();
459+
CheckIccProfile();
459460
int pal0 = 0;
460461
int palIdx = 0;
461462
palShades = false;
@@ -813,6 +814,22 @@ int[] GetPixel(byte[] curr) {
813814
}
814815
}
815816

817+
private int GetExpectedIccNumberOfComponents() {
818+
if (colorType == 0 || colorType == 4) {
819+
return 1;
820+
} else {
821+
return 3;
822+
}
823+
}
824+
825+
private void CheckIccProfile() {
826+
if (icc_profile != null && icc_profile.NumComponents != GetExpectedIccNumberOfComponents()) {
827+
LoggerFactory.GetLogger(typeof(PngImage)).Warn(MessageLocalization.GetComposedMessage("unexpected.color.space.in.embedded.icc.profile"));
828+
icc_profile = null;
829+
}
830+
}
831+
832+
816833
private static void DecodeSubFilter(byte[] curr, int count, int bpp) {
817834
for (int i = bpp; i < count; i++) {
818835
int val;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2017 iText Group NV
4+
Authors: iText Software.
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 System;
44+
using System.IO;
45+
using iTextSharp.testutils;
46+
using iTextSharp.text;
47+
using iTextSharp.text.pdf;
48+
using NUnit.Framework;
49+
50+
namespace itextsharp.tests.iTextSharp.text.pdf {
51+
public class PdfImageTest {
52+
private const String target = "PdfImageTest/";
53+
private const String source = "../../resources/text/pdf/PdfImageTest/";
54+
55+
[SetUp]
56+
public static void setUp() {
57+
Directory.CreateDirectory(target);
58+
}
59+
60+
[Test]
61+
public void pngColorProfileTest() {
62+
simpleImageTest("pngColorProfileImage.pdf", "test_icc.png");
63+
}
64+
65+
[Test]
66+
public void pngColorProfilePalletTest() {
67+
simpleImageTest("pngColorProfilePalletImage.pdf", "test_icc_pallet.png");
68+
}
69+
70+
[Test]
71+
public void pngIncorrectColorProfileTest() {
72+
simpleImageTest("pngIncorrectProfileImage.pdf", "test_incorrect_icc.png");
73+
}
74+
75+
private void simpleImageTest(String fileName, String imageName) {
76+
String outPath = target + fileName;
77+
String cmpPath = source + "cmp_" + fileName;
78+
String imgPath = source + imageName;
79+
String diff = "diff_" + fileName + "_";
80+
81+
Document document = new Document();
82+
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outPath, FileMode.Create));
83+
84+
document.Open();
85+
Image image = Image.GetInstance(imgPath);
86+
image.ScaleToFit(new Rectangle(document.Left, document.Bottom, document.Right, document.Top));
87+
document.Add(image);
88+
document.Close();
89+
writer.Close();
90+
91+
Assert.Null(new CompareTool().CompareByContent(outPath, cmpPath, target, diff));
92+
}
93+
94+
}
95+
}

src/extras/itextsharp.tests/itextsharp.tests(VS2010).csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
<Compile Include="iTextSharp\text\pdf\parser\VectorTest.cs" />
131131
<Compile Include="iTextSharp\text\pdf\pdfcleanup\PdfCleanUpProcessorTest.cs" />
132132
<Compile Include="iTextSharp\text\pdf\PdfCopyTest.cs" />
133+
<Compile Include="iTextSharp\text\pdf\PdfImageTest.cs" />
133134
<Compile Include="iTextSharp\text\pdf\PdfReaderSelectPagesTest.cs" />
134135
<Compile Include="iTextSharp\text\pdf\PdfReaderTest.cs" />
135136
<Compile Include="iTextSharp\text\pdf\PRTokeniserTest.cs" />

src/extras/itextsharp.tests/itextsharp.tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
<Compile Include="iTextSharp\text\pdf\parser\VectorTest.cs" />
116116
<Compile Include="iTextSharp\text\pdf\pdfcleanup\PdfCleanUpProcessorTest.cs" />
117117
<Compile Include="iTextSharp\text\pdf\PdfCopyTest.cs" />
118+
<Compile Include="iTextSharp\text\pdf\PdfImageTest.cs" />
118119
<Compile Include="iTextSharp\text\pdf\PdfReaderSelectPagesTest.cs" />
119120
<Compile Include="iTextSharp\text\pdf\PdfReaderTest.cs" />
120121
<Compile Include="iTextSharp\text\pdf\PRTokeniserTest.cs" />
Loading
Loading
Loading

0 commit comments

Comments
 (0)