|
| 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 | +} |
0 commit comments