Skip to content

Commit e4d00b9

Browse files
Kate IvanovaiText-CI
authored andcommitted
Add tests for TIF images with different types of compression
DEVSIX-6390 Autoported commit. Original commit hash: [0f3f55f35]
1 parent 09cd3c2 commit e4d00b9

31 files changed

+248
-39
lines changed

itext.tests/itext.io.tests/itext/io/image/TiffTest.cs

Lines changed: 184 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,22 @@ source product.
4242
*/
4343
using System;
4444
using System.IO;
45+
using iText.Commons.Utils;
4546
using iText.IO.Codec;
4647
using iText.IO.Source;
4748
using iText.IO.Util;
4849
using iText.Test;
4950

5051
namespace iText.IO.Image {
5152
public class TiffTest : ExtendedITextTest {
52-
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
53-
.CurrentContext.TestDirectory) + "/resources/itext/io/image/";
53+
private const double DELTA = 1e-5;
54+
55+
public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
56+
.CurrentContext.TestDirectory) + "/resources/itext/io/image/TiffTest/";
5457

5558
[NUnit.Framework.Test]
5659
public virtual void OpenTiff1() {
57-
byte[] imageBytes = StreamUtil.InputStreamToArray(new FileStream(sourceFolder + "WP_20140410_001.tif", FileMode.Open
60+
byte[] imageBytes = StreamUtil.InputStreamToArray(new FileStream(SOURCE_FOLDER + "WP_20140410_001.tif", FileMode.Open
5861
, FileAccess.Read));
5962
// Test a more specific entry point
6063
ImageData img = ImageDataFactory.CreateTiff(imageBytes, false, 1, false);
@@ -66,53 +69,221 @@ public virtual void OpenTiff1() {
6669
[NUnit.Framework.Test]
6770
public virtual void OpenTiff2() {
6871
// Test a more specific entry point
69-
ImageData img = ImageDataFactory.CreateTiff(UrlUtil.ToURL(sourceFolder + "WP_20140410_001_gray.tiff"), false
70-
, 1, false);
71-
NUnit.Framework.Assert.AreEqual(2592, img.GetWidth(), 0);
72-
NUnit.Framework.Assert.AreEqual(1456, img.GetHeight(), 0);
73-
NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
72+
String sourceFile = SOURCE_FOLDER + "WP_20140410_001_gray.tiff";
73+
CreateTiff(sourceFile, 8, 2592D, 1456D);
7474
}
7575

7676
[NUnit.Framework.Test]
7777
public virtual void OpenTiff3() {
78-
ImageData img = ImageDataFactory.Create(sourceFolder + "WP_20140410_001_monochrome.tiff");
78+
ImageData img = ImageDataFactory.Create(SOURCE_FOLDER + "WP_20140410_001_monochrome.tiff");
7979
NUnit.Framework.Assert.AreEqual(2592, img.GetWidth(), 0);
8080
NUnit.Framework.Assert.AreEqual(1456, img.GetHeight(), 0);
8181
NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
8282
}
8383

8484
[NUnit.Framework.Test]
8585
public virtual void OpenTiff4() {
86-
ImageData img = ImageDataFactory.Create(sourceFolder + "WP_20140410_001_negate.tiff");
86+
ImageData img = ImageDataFactory.Create(SOURCE_FOLDER + "WP_20140410_001_negate.tiff");
8787
NUnit.Framework.Assert.AreEqual(2592, img.GetWidth(), 0);
8888
NUnit.Framework.Assert.AreEqual(1456, img.GetHeight(), 0);
8989
NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
9090
}
9191

9292
[NUnit.Framework.Test]
9393
public virtual void OpenTiff5() {
94-
ImageData img = ImageDataFactory.Create(sourceFolder + "WP_20140410_001_year1900.tiff");
94+
ImageData img = ImageDataFactory.Create(SOURCE_FOLDER + "WP_20140410_001_year1900.tiff");
9595
NUnit.Framework.Assert.AreEqual(2592, img.GetWidth(), 0);
9696
NUnit.Framework.Assert.AreEqual(1456, img.GetHeight(), 0);
9797
NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
9898
}
9999

100100
[NUnit.Framework.Test]
101101
public virtual void OpenTiff6() {
102-
ImageData img = ImageDataFactory.Create(sourceFolder + "WP_20140410_001_year1980.tiff");
102+
ImageData img = ImageDataFactory.Create(SOURCE_FOLDER + "WP_20140410_001_year1980.tiff");
103103
NUnit.Framework.Assert.AreEqual(2592, img.GetWidth(), 0);
104104
NUnit.Framework.Assert.AreEqual(1456, img.GetHeight(), 0);
105105
NUnit.Framework.Assert.AreEqual(8, img.GetBpc());
106106
}
107107

108108
[NUnit.Framework.Test]
109109
public virtual void GetStringDataFromTiff() {
110-
byte[] bytes = File.ReadAllBytes(System.IO.Path.Combine(sourceFolder, "img_cmyk.tif"));
110+
byte[] bytes = File.ReadAllBytes(System.IO.Path.Combine(SOURCE_FOLDER, "img_cmyk.tif"));
111111
TIFFDirectory dir = new TIFFDirectory(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource
112112
(bytes)), 0);
113113
String[] stringArray = new String[] { "iText? 7.1.7-SNAPSHOT ?2000-2019 iText Group NV (AGPL-version)\u0000"
114114
};
115115
NUnit.Framework.Assert.AreEqual(stringArray, dir.GetField(305).GetAsStrings());
116116
}
117+
118+
[NUnit.Framework.Test]
119+
public virtual void Group3CompressionCreateTiffImageTest() {
120+
// TODO: DEVSIX-5565 (update test when support for T4 compression tiff image will be realized)
121+
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
122+
.CreateTiff(UrlUtil.ToURL(SOURCE_FOLDER + "group3CompressionImage.tif"), false, 1, false));
123+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
124+
), e.Message);
125+
}
126+
127+
[NUnit.Framework.Test]
128+
public virtual void Group3CompressionCreateImageDataTest() {
129+
// TODO: DEVSIX-5565 (update test when support for T4 compression tiff image will be realized)
130+
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
131+
.Create(UrlUtil.ToURL(SOURCE_FOLDER + "group3CompressionImage.tif")));
132+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
133+
), e.Message);
134+
}
135+
136+
[NUnit.Framework.Test]
137+
public virtual void Group4CompressionTiffImageTest() {
138+
String sourceFile = SOURCE_FOLDER + "group4CompressionImage.tif";
139+
CreateTiff(sourceFile, 1, 1024D, 768D);
140+
}
141+
142+
[NUnit.Framework.Test]
143+
public virtual void AdobeDeflateCompression1BitMinIsBlackTest() {
144+
String sourceFile = SOURCE_FOLDER + "adobeDeflateCompression1BitMinIsBlack.tif";
145+
CreateTiff(sourceFile, 1, 1024D, 768D);
146+
}
147+
148+
[NUnit.Framework.Test]
149+
public virtual void AdobeDeflateCompression1BitMinIsWhiteTest() {
150+
String sourceFile = SOURCE_FOLDER + "adobeDeflateCompression1BitMinIsWhite.tif";
151+
CreateTiff(sourceFile, 1, 1024D, 768D);
152+
}
153+
154+
[NUnit.Framework.Test]
155+
public virtual void AdobeDeflateCompression8BitMinIsBlackTest() {
156+
String sourceFile = SOURCE_FOLDER + "adobeDeflateCompression8BitMinIsBlack.tif";
157+
CreateTiff(sourceFile, 8, 1024D, 768D);
158+
}
159+
160+
[NUnit.Framework.Test]
161+
public virtual void AdobeDeflateCompression8BitMinIsWhiteTest() {
162+
String sourceFile = SOURCE_FOLDER + "adobeDeflateCompression8BitMinIsWhite.tif";
163+
CreateTiff(sourceFile, 8, 1024D, 768D);
164+
}
165+
166+
[NUnit.Framework.Test]
167+
public virtual void AdobeDeflateCompression8BitRgbTest() {
168+
String sourceFile = SOURCE_FOLDER + "adobeDeflateCompression8BitRgb.tif";
169+
CreateTiff(sourceFile, 8, 1024D, 768D);
170+
}
171+
172+
[NUnit.Framework.Test]
173+
public virtual void AdobeDeflateComp16BitMinIsBlackCreateTiffTest() {
174+
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
175+
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
176+
.CreateTiff(UrlUtil.ToURL(SOURCE_FOLDER + "adobeDeflateCompression16BitMinIsBlack.tif"), false, 1, false
177+
));
178+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
179+
), e.Message);
180+
}
181+
182+
[NUnit.Framework.Test]
183+
public virtual void AdobeDeflateComp16BitMinIsBlackCreateImageTest() {
184+
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
185+
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
186+
.Create(UrlUtil.ToURL(SOURCE_FOLDER + "adobeDeflateCompression16BitMinIsBlack.tif")));
187+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
188+
), e.Message);
189+
}
190+
191+
[NUnit.Framework.Test]
192+
public virtual void AdobeDeflateComp16BitMinIsWhiteCreateTiffTest() {
193+
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
194+
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
195+
.CreateTiff(UrlUtil.ToURL(SOURCE_FOLDER + "adobeDeflateCompression16BitMinIsWhite.tif"), false, 1, false
196+
));
197+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
198+
), e.Message);
199+
}
200+
201+
[NUnit.Framework.Test]
202+
public virtual void AdobeDeflateComp16BitMinIsWhiteCreateImageTest() {
203+
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
204+
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
205+
.Create(UrlUtil.ToURL(SOURCE_FOLDER + "adobeDeflateCompression16BitMinIsWhite.tif")));
206+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
207+
), e.Message);
208+
}
209+
210+
[NUnit.Framework.Test]
211+
public virtual void AdobeDeflateCompression16BitRgbCreateTiffTest() {
212+
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
213+
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
214+
.CreateTiff(UrlUtil.ToURL(SOURCE_FOLDER + "adobeDeflateCompression16BitRgb.tif"), false, 1, false));
215+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
216+
), e.Message);
217+
}
218+
219+
[NUnit.Framework.Test]
220+
public virtual void AdobeDeflateCompression16BitRgbCreateImageTest() {
221+
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
222+
Exception e = NUnit.Framework.Assert.Catch(typeof(iText.IO.Exceptions.IOException), () => ImageDataFactory
223+
.Create(UrlUtil.ToURL(SOURCE_FOLDER + "adobeDeflateCompression16BitRgb.tif")));
224+
NUnit.Framework.Assert.AreEqual(MessageFormatUtil.Format(iText.IO.Exceptions.IOException.CannotReadTiffImage
225+
), e.Message);
226+
}
227+
228+
[NUnit.Framework.Test]
229+
public virtual void CcittRleCompressionTest() {
230+
String sourceFile = SOURCE_FOLDER + "ccittRleCompression.tif";
231+
CreateTiff(sourceFile, 1, 1024D, 768D);
232+
}
233+
234+
[NUnit.Framework.Test]
235+
public virtual void DeflateCompression8BitRgbTest() {
236+
String sourceFile = SOURCE_FOLDER + "deflateCompression8BitRgb.tif";
237+
CreateTiff(sourceFile, 8, 1024D, 768D);
238+
}
239+
240+
[NUnit.Framework.Test]
241+
public virtual void DeflateCompression8BitPaletteTest() {
242+
String sourceFile = SOURCE_FOLDER + "deflateCompression8BitPalette.tif";
243+
CreateTiff(sourceFile, 8, 1024D, 768D);
244+
}
245+
246+
[NUnit.Framework.Test]
247+
public virtual void JpegCompression8BitYcbcrTest() {
248+
String sourceFile = SOURCE_FOLDER + "jpegCompression8BitYcbcr.tif";
249+
CreateTiff(sourceFile, 8, 1024D, 768D);
250+
}
251+
252+
[NUnit.Framework.Test]
253+
public virtual void OldJpegCompression8BitYcbcrTest() {
254+
String sourceFile = SOURCE_FOLDER + "oldJpegCompression8BitYcbcr.tif";
255+
CreateTiff(sourceFile, 8, 1024D, 768D);
256+
}
257+
258+
[NUnit.Framework.Test]
259+
public virtual void LzwCompression8BitRgbTest() {
260+
String sourceFile = SOURCE_FOLDER + "lzwCompression8BitRgb.tif";
261+
CreateTiff(sourceFile, 8, 1024D, 768D);
262+
}
263+
264+
[NUnit.Framework.Test]
265+
public virtual void LzwCompression8BitPaletteTest() {
266+
String sourceFile = SOURCE_FOLDER + "lzwCompression8BitPalette.tif";
267+
CreateTiff(sourceFile, 8, 1024D, 768D);
268+
}
269+
270+
[NUnit.Framework.Test]
271+
public virtual void PackbitsCompression8BitMinIsBlackTest() {
272+
String sourceFile = SOURCE_FOLDER + "packbitsCompression8BitMinIsBlack.tif";
273+
CreateTiff(sourceFile, 8, 1024D, 768D);
274+
}
275+
276+
[NUnit.Framework.Test]
277+
public virtual void PackbitsCompression8BitMinIsWhiteTest() {
278+
String sourceFile = SOURCE_FOLDER + "packbitsCompression8BitMinIsWhite.tif";
279+
CreateTiff(sourceFile, 8, 1024D, 768D);
280+
}
281+
282+
private static void CreateTiff(String sourceFile, int bpc, double width, double height) {
283+
ImageData img = ImageDataFactory.CreateTiff(UrlUtil.ToURL(sourceFile), false, 1, false);
284+
NUnit.Framework.Assert.AreEqual(bpc, img.GetBpc(), DELTA);
285+
NUnit.Framework.Assert.AreEqual(width, img.GetWidth(), DELTA);
286+
NUnit.Framework.Assert.AreEqual(height, img.GetHeight(), DELTA);
287+
}
117288
}
118289
}

0 commit comments

Comments
 (0)