Skip to content

Commit 8d779be

Browse files
Kate IvanovaiText-CI
authored andcommitted
Cover custom image masks setting API with tests
DEVSIX-4408 Autoported commit. Original commit hash: [078bcc407]
1 parent 4b08ee1 commit 8d779be

20 files changed

+172
-1
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 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 iText.IO.Image;
45+
using iText.IO.Util;
46+
using iText.Kernel.Geom;
47+
using iText.Kernel.Pdf;
48+
using iText.Kernel.Pdf.Xobject;
49+
using iText.Kernel.Utils;
50+
using iText.Test;
51+
52+
namespace iText.Kernel.Pdf.Canvas {
53+
public class ImageMasksTest : ExtendedITextTest {
54+
private static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
55+
.CurrentContext.TestDirectory) + "/resources/itext/kernel/pdf/canvas/ImageMasksTest/";
56+
57+
private static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
58+
+ "/test/itext/kernel/pdf/canvas/ImageMasksTest/";
59+
60+
[NUnit.Framework.OneTimeSetUp]
61+
public static void BeforeClass() {
62+
CreateOrClearDestinationFolder(destinationFolder);
63+
}
64+
65+
[NUnit.Framework.Test]
66+
public virtual void ImageResizedParentWithHardMaskTest() {
67+
String outFileName = destinationFolder + "imageResizedParentWithHardMask.pdf";
68+
String cmpFileName = sourceFolder + "cmp_imageResizedParentWithHardMask.pdf";
69+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
70+
ImageData mask = ImageDataFactory.Create(sourceFolder + "hardMask.png");
71+
mask.MakeMask();
72+
ImageData img1 = ImageDataFactory.Create(sourceFolder + "sRGBImageBig.png");
73+
img1.SetImageMask(mask);
74+
ImageData img2 = ImageDataFactory.Create(sourceFolder + "sRGBImage.png");
75+
img2.SetImageMask(mask);
76+
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
77+
canvas.AddImageAt(img1, 30, 500, false);
78+
canvas.AddImageAt(img2, 430, 500, false);
79+
pdfDoc.Close();
80+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
81+
));
82+
}
83+
84+
[NUnit.Framework.Test]
85+
public virtual void DiffMasksOnSameImageXObjectTest() {
86+
// TODO: DEVSIX-4992
87+
String outFileName = destinationFolder + "diffMasksOnSameImageXObject.pdf";
88+
String cmpFileName = sourceFolder + "cmp_diffMasksOnSameImageXObject.pdf";
89+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
90+
ImageData sMask = ImageDataFactory.Create(sourceFolder + "SMask3px.jpg");
91+
sMask.MakeMask();
92+
ImageData hardMask = ImageDataFactory.Create(sourceFolder + "hardMask.png");
93+
hardMask.MakeMask();
94+
PdfImageXObject hardMaskXObject = new PdfImageXObject(hardMask);
95+
ImageData img = ImageDataFactory.Create(sourceFolder + "sRGBImageBig.png");
96+
img.SetImageMask(sMask);
97+
PdfImageXObject hardXObject = new PdfImageXObject(img, hardMaskXObject);
98+
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
99+
canvas.AddXObjectAt(hardXObject, 300, 500);
100+
pdfDoc.Close();
101+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
102+
));
103+
}
104+
105+
[NUnit.Framework.Test]
106+
public virtual void ImageResizedParentWithSoftMaskTest() {
107+
String outFileName = destinationFolder + "imageResizedParentWithSoftMask.pdf";
108+
String cmpFileName = sourceFolder + "cmp_imageResizedParentWithSoftMask.pdf";
109+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
110+
ImageData mask = ImageDataFactory.Create(sourceFolder + "SMask3px.jpg");
111+
mask.MakeMask();
112+
ImageData img1 = ImageDataFactory.Create(sourceFolder + "sRGBImageBig.png");
113+
img1.SetImageMask(mask);
114+
ImageData img2 = ImageDataFactory.Create(sourceFolder + "sRGBImage.png");
115+
img2.SetImageMask(mask);
116+
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
117+
canvas.AddImageAt(img1, 30, 500, false);
118+
canvas.AddImageAt(img2, 430, 500, false);
119+
pdfDoc.Close();
120+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
121+
));
122+
}
123+
124+
[NUnit.Framework.Test]
125+
public virtual void ImageWithSoftMaskMatteTest() {
126+
String outFileName = destinationFolder + "imageWithSoftMaskMatte.pdf";
127+
String cmpFileName = sourceFolder + "cmp_imageWithSoftMaskMatte.pdf";
128+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
129+
ImageData mask = ImageDataFactory.Create(sourceFolder + "matteMask.jpg");
130+
mask.MakeMask();
131+
PdfImageXObject maskXObject = new PdfImageXObject(mask);
132+
maskXObject.Put(new PdfName("Matte"), new PdfArray(new float[] { 1, 1, 1 }));
133+
ImageData img1 = ImageDataFactory.Create(sourceFolder + "imageForMatteMask.jpg");
134+
PdfImageXObject xObject = new PdfImageXObject(img1, maskXObject);
135+
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage(PageSize.A4));
136+
canvas.AddXObjectAt(xObject, 50, 500);
137+
pdfDoc.Close();
138+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
139+
));
140+
}
141+
142+
[NUnit.Framework.Test]
143+
public virtual void SMaskMatteDifferentSizeOfImgTest() {
144+
// TODO: DEVSIX-4991
145+
String outFileName = destinationFolder + "sMaskMatteDifferentSizeOfImg.pdf";
146+
String cmpFileName = sourceFolder + "cmp_sMaskMatteDifferentSizeOfImg.pdf";
147+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
148+
ImageData mask = ImageDataFactory.Create(sourceFolder + "matteMask.jpg");
149+
mask.MakeMask();
150+
PdfImageXObject maskXObject = new PdfImageXObject(mask);
151+
maskXObject.Put(new PdfName("Matte"), new PdfArray(new float[] { 1, 1, 1 }));
152+
ImageData img1 = ImageDataFactory.Create(sourceFolder + "resizedImageForMatteMask.jpg");
153+
PdfImageXObject xObject = new PdfImageXObject(img1, maskXObject);
154+
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage(PageSize.A4));
155+
canvas.AddXObjectAt(xObject, 50, 500);
156+
pdfDoc.Close();
157+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
158+
));
159+
}
160+
161+
[NUnit.Framework.Test]
162+
public virtual void ImageWithInvalidMaskTest() {
163+
ImageData mask = ImageDataFactory.Create(sourceFolder + "mask.png");
164+
NUnit.Framework.Assert.That(() => {
165+
mask.MakeMask();
166+
}
167+
, NUnit.Framework.Throws.InstanceOf<iText.IO.IOException>().With.Message.EqualTo(MessageFormatUtil.Format(iText.IO.IOException.ThisImageCanNotBeAnImageMask)))
168+
;
169+
}
170+
}
171+
}
20.9 KB
Loading
23.7 KB
Loading
12.4 KB
Loading
218 Bytes
Loading

0 commit comments

Comments
 (0)