Skip to content

Commit 4415e26

Browse files
IdamkinIitext-teamcity
authored andcommitted
Make png helper keep incompatible icc profile. Add tests
This was done so color profile, even if incompatible can be extracted from image data. Note that the warning is still thrown and incompatible color profile will be removed on creation of PdfImageXObject. Color profile tests were also moved to separate class. DEVSIX-1714 Autoported commit. Original commit hash: [a9e3be262]
1 parent 1c01b0a commit 4415e26

20 files changed

+155
-37
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using iText.IO.Colors;
4+
using iText.IO.Font;
5+
using iText.IO.Image;
6+
using iText.Kernel.Pdf;
7+
using iText.Kernel.Utils;
8+
using iText.Test;
9+
using iText.Test.Attributes;
10+
11+
namespace iText.Layout {
12+
public class ImageColorProfileTest : ExtendedITextTest {
13+
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
14+
+ "/test/itext/layout/ImageColorProfileTest/";
15+
16+
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
17+
.CurrentContext.TestDirectory) + "/resources/itext/layout/ImageColorProfileTest/";
18+
19+
[NUnit.Framework.OneTimeSetUp]
20+
public static void BeforeClass() {
21+
CreateDestinationFolder(destinationFolder);
22+
}
23+
24+
/// <exception cref="System.IO.IOException"/>
25+
[NUnit.Framework.Test]
26+
[LogMessage(iText.IO.LogMessageConstant.PNG_IMAGE_HAS_ICC_PROFILE_WITH_INCOMPATIBLE_NUMBER_OF_COLOR_COMPONENTS
27+
)]
28+
public virtual void ExtractIncompatibleColorProfileTest() {
29+
ImageData imageData = ImageDataFactory.Create(sourceFolder + "png-incorrect-embedded-color-profile.png");
30+
NUnit.Framework.Assert.IsNotNull(imageData.GetProfile());
31+
}
32+
33+
/// <exception cref="System.IO.IOException"/>
34+
/// <exception cref="System.Exception"/>
35+
[NUnit.Framework.Test]
36+
public virtual void PngEmbeddedColorProfileTest() {
37+
RunTest("pngEmbeddedColorProfile.pdf", "png-embedded-color-profile.png");
38+
}
39+
40+
/// <exception cref="System.IO.IOException"/>
41+
/// <exception cref="System.Exception"/>
42+
[NUnit.Framework.Test]
43+
[LogMessage(iText.IO.LogMessageConstant.PNG_IMAGE_HAS_ICC_PROFILE_WITH_INCOMPATIBLE_NUMBER_OF_COLOR_COMPONENTS
44+
)]
45+
[LogMessage(iText.IO.LogMessageConstant.IMAGE_HAS_ICC_PROFILE_WITH_INCOMPATIBLE_NUMBER_OF_COLOR_COMPONENTS_COMPARED_TO_BASE_COLOR_SPACE_IN_INDEXED_COLOR_SPACE
46+
)]
47+
public virtual void PngIncorrectEmbeddedColorProfileTest() {
48+
RunTest("pngIncorrectEmbeddedColorProfile.pdf", "png-incorrect-embedded-color-profile.png");
49+
}
50+
51+
/// <exception cref="System.IO.IOException"/>
52+
/// <exception cref="System.Exception"/>
53+
[NUnit.Framework.Test]
54+
[LogMessage(iText.IO.LogMessageConstant.PNG_IMAGE_HAS_ICC_PROFILE_WITH_INCOMPATIBLE_NUMBER_OF_COLOR_COMPONENTS
55+
)]
56+
public virtual void PngReplaceIncorrectEmbeddedColorProfileTest() {
57+
RunTest("pngReplaceIncorrectColorProfile.pdf", "png-incorrect-embedded-color-profile.png", "sRGB_v4_ICC_preference.icc"
58+
);
59+
}
60+
61+
/// <exception cref="System.IO.IOException"/>
62+
/// <exception cref="System.Exception"/>
63+
[NUnit.Framework.Test]
64+
public virtual void PngIndexedEmbeddedColorProfileTest() {
65+
RunTest("pngIndexedEmbeddedColorProfile.pdf", "png-indexed-embedded-color-profile.png");
66+
}
67+
68+
/// <exception cref="System.IO.IOException"/>
69+
/// <exception cref="System.Exception"/>
70+
[NUnit.Framework.Test]
71+
public virtual void PngGreyscaleEmbeddedColorProfileTest() {
72+
RunTest("pngGreyscaleEmbeddedColorProfile.pdf", "png-greyscale-embedded-color-profile.png");
73+
}
74+
75+
/// <exception cref="System.IO.IOException"/>
76+
/// <exception cref="System.Exception"/>
77+
[NUnit.Framework.Test]
78+
[LogMessage(iText.IO.LogMessageConstant.IMAGE_HAS_ICC_PROFILE_WITH_INCOMPATIBLE_NUMBER_OF_COLOR_COMPONENTS_COMPARED_TO_COLOR_SPACE
79+
)]
80+
public virtual void PngGreyscaleIncorrectColorProfileTest() {
81+
RunTest("pngGreyscaleIncorrectColorProfile.pdf", "png-greyscale.png", "sRGB_v4_ICC_preference.icc");
82+
}
83+
84+
/// <exception cref="System.IO.IOException"/>
85+
/// <exception cref="System.Exception"/>
86+
[NUnit.Framework.Test]
87+
[LogMessage(iText.IO.LogMessageConstant.IMAGE_HAS_INCORRECT_OR_UNSUPPORTED_COLOR_SPACE_OVERRIDDEN_BY_ICC_PROFILE
88+
)]
89+
public virtual void PngUnsupportedColorSpaceTest() {
90+
IDictionary<String, Object> fakeColorSpaceAttributes = new Dictionary<String, Object>();
91+
fakeColorSpaceAttributes.Put("ColorSpace", "/FakeColorSpace");
92+
RunTest("pngUnsupportedColorSpace.pdf", "png-embedded-color-profile.png", null, fakeColorSpaceAttributes);
93+
}
94+
95+
/// <exception cref="System.IO.IOException"/>
96+
/// <exception cref="System.Exception"/>
97+
[NUnit.Framework.Test]
98+
[LogMessage(iText.IO.LogMessageConstant.IMAGE_HAS_INCORRECT_OR_UNSUPPORTED_BASE_COLOR_SPACE_IN_INDEXED_COLOR_SPACE_OVERRIDDEN_BY_ICC_PROFILE
99+
)]
100+
public virtual void PngUnsupportedBaseColorSpace() {
101+
IDictionary<String, Object> fakeColorSpaceAttributes = new Dictionary<String, Object>();
102+
String lookup = PdfEncodings.ConvertToString(new byte[] { 0, 0, 0, (byte)0xff, (byte)0xff, (byte)0xff }, null
103+
);
104+
fakeColorSpaceAttributes.Put("ColorSpace", new Object[] { "/Indexed", "/FakeColorSpace", 1, lookup });
105+
RunTest("pngUnsupportedBaseColorSpace.pdf", "png-indexed-embedded-color-profile.png", "sRGB_v4_ICC_preference.icc"
106+
, fakeColorSpaceAttributes);
107+
}
108+
109+
/// <exception cref="System.IO.IOException"/>
110+
/// <exception cref="System.Exception"/>
111+
[NUnit.Framework.Test]
112+
public virtual void PngNoColorProfileTest() {
113+
RunTest("pngNoColorProfile.pdf", "png-greyscale.png");
114+
}
115+
116+
/// <exception cref="System.IO.IOException"/>
117+
/// <exception cref="System.Exception"/>
118+
private void RunTest(String pdfName, String imageName) {
119+
RunTest(pdfName, imageName, null, null);
120+
}
121+
122+
/// <exception cref="System.IO.IOException"/>
123+
/// <exception cref="System.Exception"/>
124+
private void RunTest(String pdfName, String imageName, String colorProfileName) {
125+
RunTest(pdfName, imageName, colorProfileName, null);
126+
}
127+
128+
/// <exception cref="System.IO.IOException"/>
129+
/// <exception cref="System.Exception"/>
130+
private void RunTest(String pdfName, String imageName, String colorProfileName, IDictionary<String, Object
131+
> customImageAttribute) {
132+
String outFileName = destinationFolder + pdfName;
133+
String cmpFileName = sourceFolder + "cmp_" + pdfName;
134+
String diff = "diff_" + pdfName + "_";
135+
PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));
136+
Document document = new Document(pdf);
137+
ImageData imageData = ImageDataFactory.Create(sourceFolder + imageName);
138+
if (customImageAttribute != null) {
139+
imageData.GetImageAttributes().AddAll(customImageAttribute);
140+
}
141+
if (colorProfileName != null) {
142+
imageData.SetProfile(IccProfile.GetInstance(sourceFolder + colorProfileName));
143+
}
144+
iText.Layout.Element.Image png = new iText.Layout.Element.Image(imageData);
145+
png.SetAutoScale(true);
146+
document.Add(png);
147+
document.Close();
148+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
149+
, diff));
150+
}
151+
}
152+
}

itext.tests/itext.layout.tests/itext/layout/ImageTest.cs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -514,22 +514,6 @@ public virtual void ImageTest21() {
514514
, "diff"));
515515
}
516516

517-
/// <exception cref="System.IO.IOException"/>
518-
/// <exception cref="System.Exception"/>
519-
[NUnit.Framework.Test]
520-
public virtual void PngImageColorProfileTest() {
521-
SimpleImageTest("pngColorProfileTest.pdf", "png-color-profile-test.png");
522-
}
523-
524-
/// <exception cref="System.IO.IOException"/>
525-
/// <exception cref="System.Exception"/>
526-
[NUnit.Framework.Test]
527-
[LogMessage(iText.IO.LogMessageConstant.PNG_IMAGE_HAS_ICC_PROFILE_WITH_INCOMPATIBLE_NUMBER_OF_COLOR_COMPONENTS
528-
)]
529-
public virtual void PngImageIncorrectColorProfileTest() {
530-
SimpleImageTest("pngIncorrectColorProfileTest.pdf", "png-incorrect-color-profile-test.png");
531-
}
532-
533517
/// <summary>Image can be reused in layout, so flushing it on the very first draw is a bad thing.</summary>
534518
/// <exception cref="System.IO.IOException"/>
535519
/// <exception cref="System.Exception"/>
@@ -776,22 +760,5 @@ public virtual void ImageWithMinMaxHeightTest01() {
776760
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
777761
, "diff"));
778762
}
779-
780-
/// <exception cref="System.IO.IOException"/>
781-
/// <exception cref="System.Exception"/>
782-
private void SimpleImageTest(String pdfName, String imageName) {
783-
String outFileName = destinationFolder + pdfName;
784-
String cmpFileName = sourceFolder + "cmp_" + pdfName;
785-
String diff = "diff_" + pdfName + "_";
786-
PdfDocument pdf = new PdfDocument(new PdfWriter(outFileName));
787-
Document document = new Document(pdf);
788-
iText.Layout.Element.Image png = new iText.Layout.Element.Image(ImageDataFactory.Create(sourceFolder + imageName
789-
));
790-
png.SetAutoScale(true);
791-
document.Add(png);
792-
document.Close();
793-
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
794-
, diff));
795-
}
796763
}
797764
}

0 commit comments

Comments
 (0)