Skip to content

Commit 0f3f55f

Browse files
author
Kate Ivanova
committed
Add tests for TIF images with different types of compression
DEVSIX-6390
1 parent 44c5b1f commit 0f3f55f

30 files changed

+309
-50
lines changed

io/src/test/java/com/itextpdf/io/image/TiffTest.java

Lines changed: 225 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.io.image;
4444

45+
import com.itextpdf.commons.utils.MessageFormatUtil;
4546
import com.itextpdf.io.codec.TIFFDirectory;
4647
import com.itextpdf.io.source.RandomAccessFileOrArray;
4748
import com.itextpdf.io.source.RandomAccessSourceFactory;
@@ -52,6 +53,7 @@ This file is part of the iText (R) project.
5253

5354
import java.io.FileInputStream;
5455
import java.io.IOException;
56+
import java.net.MalformedURLException;
5557
import java.nio.file.Files;
5658
import java.nio.file.Paths;
5759
import org.junit.Assert;
@@ -61,11 +63,12 @@ This file is part of the iText (R) project.
6163
@Category(UnitTest.class)
6264
public class TiffTest extends ExtendedITextTest {
6365

64-
public static final String sourceFolder = "./src/test/resources/com/itextpdf/io/image/";
66+
private static final double DELTA = 1e-5;
67+
public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/io/image/TiffTest/";
6568

6669
@Test
6770
public void openTiff1() throws IOException {
68-
byte[] imageBytes = StreamUtil.inputStreamToArray(new FileInputStream(sourceFolder + "WP_20140410_001.tif"));
71+
byte[] imageBytes = StreamUtil.inputStreamToArray(new FileInputStream(SOURCE_FOLDER + "WP_20140410_001.tif"));
6972
// Test a more specific entry point
7073
ImageData img = ImageDataFactory.createTiff(imageBytes, false, 1, false);
7174
Assert.assertEquals(2592, img.getWidth(), 0);
@@ -76,50 +79,259 @@ public void openTiff1() throws IOException {
7679
@Test
7780
public void openTiff2() throws IOException {
7881
// Test a more specific entry point
79-
ImageData img = ImageDataFactory.createTiff(UrlUtil.toURL(sourceFolder + "WP_20140410_001_gray.tiff"),
80-
false, 1, false);
81-
Assert.assertEquals(2592, img.getWidth(), 0);
82-
Assert.assertEquals(1456, img.getHeight(), 0);
83-
Assert.assertEquals(8, img.getBpc());
82+
String sourceFile = SOURCE_FOLDER + "WP_20140410_001_gray.tiff";
83+
84+
createTiff(sourceFile, 8, 2592D, 1456D);
8485
}
8586

8687
@Test
8788
public void openTiff3() throws IOException {
88-
ImageData img = ImageDataFactory.create(sourceFolder + "WP_20140410_001_monochrome.tiff");
89+
ImageData img = ImageDataFactory.create(SOURCE_FOLDER + "WP_20140410_001_monochrome.tiff");
90+
8991
Assert.assertEquals(2592, img.getWidth(), 0);
9092
Assert.assertEquals(1456, img.getHeight(), 0);
9193
Assert.assertEquals(8, img.getBpc());
9294
}
9395

9496
@Test
9597
public void openTiff4() throws IOException {
96-
ImageData img = ImageDataFactory.create(sourceFolder + "WP_20140410_001_negate.tiff");
98+
ImageData img = ImageDataFactory.create(SOURCE_FOLDER + "WP_20140410_001_negate.tiff");
99+
97100
Assert.assertEquals(2592, img.getWidth(), 0);
98101
Assert.assertEquals(1456, img.getHeight(), 0);
99102
Assert.assertEquals(8, img.getBpc());
100103
}
101104

102105
@Test
103106
public void openTiff5() throws IOException {
104-
ImageData img = ImageDataFactory.create(sourceFolder + "WP_20140410_001_year1900.tiff");
107+
ImageData img = ImageDataFactory.create(SOURCE_FOLDER + "WP_20140410_001_year1900.tiff");
108+
105109
Assert.assertEquals(2592, img.getWidth(), 0);
106110
Assert.assertEquals(1456, img.getHeight(), 0);
107111
Assert.assertEquals(8, img.getBpc());
108112
}
109113

110114
@Test
111115
public void openTiff6() throws IOException {
112-
ImageData img = ImageDataFactory.create(sourceFolder + "WP_20140410_001_year1980.tiff");
116+
ImageData img = ImageDataFactory.create(SOURCE_FOLDER + "WP_20140410_001_year1980.tiff");
117+
113118
Assert.assertEquals(2592, img.getWidth(), 0);
114119
Assert.assertEquals(1456, img.getHeight(), 0);
115120
Assert.assertEquals(8, img.getBpc());
116121
}
117122

118123
@Test
119124
public void getStringDataFromTiff() throws IOException {
120-
byte[] bytes = Files.readAllBytes(Paths.get(sourceFolder, "img_cmyk.tif"));
121-
TIFFDirectory dir = new TIFFDirectory(new RandomAccessFileOrArray(new RandomAccessSourceFactory().createSource(bytes)), 0);
125+
byte[] bytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER, "img_cmyk.tif"));
126+
TIFFDirectory dir = new TIFFDirectory(new RandomAccessFileOrArray(
127+
new RandomAccessSourceFactory().createSource(bytes)), 0);
122128
String[] stringArray = new String[] {"iText? 7.1.7-SNAPSHOT ?2000-2019 iText Group NV (AGPL-version)\u0000"};
129+
123130
Assert.assertArrayEquals(stringArray, dir.getField(305).getAsStrings());
124131
}
132+
133+
@Test
134+
// TODO: DEVSIX-5565 (update test when support for T4 compression tiff image will be realized)
135+
public void group3CompressionCreateTiffImageTest() {
136+
Exception e = Assert.assertThrows(com.itextpdf.io.exceptions.IOException.class,
137+
() -> ImageDataFactory.createTiff(UrlUtil.toURL(SOURCE_FOLDER + "group3CompressionImage.tif"),
138+
false, 1, false));
139+
140+
Assert.assertEquals(MessageFormatUtil.format(
141+
com.itextpdf.io.exceptions.IOException.CannotReadTiffImage), e.getMessage());
142+
}
143+
144+
@Test
145+
// TODO: DEVSIX-5565 (update test when support for T4 compression tiff image will be realized)
146+
public void group3CompressionCreateImageDataTest() {
147+
Exception e = Assert.assertThrows(com.itextpdf.io.exceptions.IOException.class,
148+
() -> ImageDataFactory.create(UrlUtil.toURL(SOURCE_FOLDER + "group3CompressionImage.tif")));
149+
150+
Assert.assertEquals(MessageFormatUtil.format(
151+
com.itextpdf.io.exceptions.IOException.CannotReadTiffImage), e.getMessage());
152+
}
153+
154+
@Test
155+
public void group4CompressionTiffImageTest() throws IOException {
156+
String sourceFile = SOURCE_FOLDER + "group4CompressionImage.tif";
157+
158+
createTiff(sourceFile, 1, 1024D, 768D);
159+
}
160+
161+
@Test
162+
public void adobeDeflateCompression1BitMinIsBlackTest() throws IOException {
163+
String sourceFile = SOURCE_FOLDER + "adobeDeflateCompression1BitMinIsBlack.tif";
164+
165+
createTiff(sourceFile, 1, 1024D, 768D);
166+
}
167+
168+
@Test
169+
public void adobeDeflateCompression1BitMinIsWhiteTest() throws IOException {
170+
String sourceFile = SOURCE_FOLDER + "adobeDeflateCompression1BitMinIsWhite.tif";
171+
172+
createTiff(sourceFile, 1, 1024D, 768D);
173+
}
174+
175+
@Test
176+
public void adobeDeflateCompression8BitMinIsBlackTest() throws IOException {
177+
String sourceFile = SOURCE_FOLDER + "adobeDeflateCompression8BitMinIsBlack.tif";
178+
179+
createTiff(sourceFile, 8, 1024D, 768D);
180+
}
181+
182+
@Test
183+
public void adobeDeflateCompression8BitMinIsWhiteTest() throws IOException {
184+
String sourceFile = SOURCE_FOLDER + "adobeDeflateCompression8BitMinIsWhite.tif";
185+
186+
createTiff(sourceFile, 8, 1024D, 768D);
187+
}
188+
189+
@Test
190+
public void adobeDeflateCompression8BitRgbTest() throws IOException {
191+
String sourceFile = SOURCE_FOLDER + "adobeDeflateCompression8BitRgb.tif";
192+
193+
createTiff(sourceFile, 8, 1024D, 768D);
194+
}
195+
196+
@Test
197+
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
198+
public void adobeDeflateComp16BitMinIsBlackCreateTiffTest() {
199+
Exception e = Assert.assertThrows(com.itextpdf.io.exceptions.IOException.class,
200+
() -> ImageDataFactory.createTiff(UrlUtil.toURL(
201+
SOURCE_FOLDER + "adobeDeflateCompression16BitMinIsBlack.tif"),
202+
false, 1, false));
203+
204+
Assert.assertEquals(MessageFormatUtil.format(
205+
com.itextpdf.io.exceptions.IOException.CannotReadTiffImage), e.getMessage());
206+
}
207+
208+
@Test
209+
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
210+
public void adobeDeflateComp16BitMinIsBlackCreateImageTest() {
211+
Exception e = Assert.assertThrows(com.itextpdf.io.exceptions.IOException.class,
212+
() -> ImageDataFactory.create(UrlUtil.toURL(
213+
SOURCE_FOLDER + "adobeDeflateCompression16BitMinIsBlack.tif")));
214+
215+
Assert.assertEquals(MessageFormatUtil.format(
216+
com.itextpdf.io.exceptions.IOException.CannotReadTiffImage), e.getMessage());
217+
}
218+
219+
@Test
220+
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
221+
public void adobeDeflateComp16BitMinIsWhiteCreateTiffTest() {
222+
Exception e = Assert.assertThrows(com.itextpdf.io.exceptions.IOException.class,
223+
() -> ImageDataFactory.createTiff(UrlUtil.toURL(
224+
SOURCE_FOLDER + "adobeDeflateCompression16BitMinIsWhite.tif"),
225+
false, 1, false));
226+
227+
Assert.assertEquals(MessageFormatUtil.format(
228+
com.itextpdf.io.exceptions.IOException.CannotReadTiffImage), e.getMessage());
229+
}
230+
231+
@Test
232+
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
233+
public void adobeDeflateComp16BitMinIsWhiteCreateImageTest() {
234+
Exception e = Assert.assertThrows(com.itextpdf.io.exceptions.IOException.class,
235+
() -> ImageDataFactory.create(UrlUtil.toURL(
236+
SOURCE_FOLDER + "adobeDeflateCompression16BitMinIsWhite.tif")));
237+
238+
Assert.assertEquals(MessageFormatUtil.format(
239+
com.itextpdf.io.exceptions.IOException.CannotReadTiffImage), e.getMessage());
240+
}
241+
242+
@Test
243+
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
244+
public void adobeDeflateCompression16BitRgbCreateTiffTest() {
245+
Exception e = Assert.assertThrows(com.itextpdf.io.exceptions.IOException.class,
246+
() -> ImageDataFactory.createTiff(UrlUtil.toURL(
247+
SOURCE_FOLDER + "adobeDeflateCompression16BitRgb.tif"),
248+
false, 1, false));
249+
250+
Assert.assertEquals(MessageFormatUtil.format(
251+
com.itextpdf.io.exceptions.IOException.CannotReadTiffImage), e.getMessage());
252+
}
253+
254+
@Test
255+
// TODO: DEVSIX-5565 (update test when support for adobeDeflate compression tiff image will be realized)
256+
public void adobeDeflateCompression16BitRgbCreateImageTest() {
257+
Exception e = Assert.assertThrows(com.itextpdf.io.exceptions.IOException.class,
258+
() -> ImageDataFactory.create(UrlUtil.toURL(
259+
SOURCE_FOLDER + "adobeDeflateCompression16BitRgb.tif")));
260+
261+
Assert.assertEquals(MessageFormatUtil.format(
262+
com.itextpdf.io.exceptions.IOException.CannotReadTiffImage), e.getMessage());
263+
}
264+
265+
@Test
266+
public void ccittRleCompressionTest() throws IOException {
267+
String sourceFile = SOURCE_FOLDER + "ccittRleCompression.tif";
268+
269+
createTiff(sourceFile, 1, 1024D, 768D);
270+
}
271+
272+
@Test
273+
public void deflateCompression8BitRgbTest() throws IOException {
274+
String sourceFile = SOURCE_FOLDER + "deflateCompression8BitRgb.tif";
275+
276+
createTiff(sourceFile, 8, 1024D, 768D);
277+
}
278+
279+
@Test
280+
public void deflateCompression8BitPaletteTest() throws IOException {
281+
String sourceFile = SOURCE_FOLDER + "deflateCompression8BitPalette.tif";
282+
283+
createTiff(sourceFile, 8, 1024D, 768D);
284+
}
285+
286+
@Test
287+
public void jpegCompression8BitYcbcrTest() throws IOException {
288+
String sourceFile = SOURCE_FOLDER + "jpegCompression8BitYcbcr.tif";
289+
290+
createTiff(sourceFile, 8, 1024D, 768D);
291+
}
292+
293+
@Test
294+
public void oldJpegCompression8BitYcbcrTest() throws IOException {
295+
String sourceFile = SOURCE_FOLDER + "oldJpegCompression8BitYcbcr.tif";
296+
297+
createTiff(sourceFile, 8, 1024D, 768D);
298+
}
299+
300+
@Test
301+
public void lzwCompression8BitRgbTest() throws IOException {
302+
String sourceFile = SOURCE_FOLDER + "lzwCompression8BitRgb.tif";
303+
304+
createTiff(sourceFile, 8, 1024D, 768D);
305+
}
306+
307+
@Test
308+
public void lzwCompression8BitPaletteTest() throws IOException {
309+
String sourceFile = SOURCE_FOLDER + "lzwCompression8BitPalette.tif";
310+
311+
createTiff(sourceFile, 8, 1024D, 768D);
312+
}
313+
314+
@Test
315+
public void packbitsCompression8BitMinIsBlackTest() throws IOException {
316+
String sourceFile = SOURCE_FOLDER + "packbitsCompression8BitMinIsBlack.tif";
317+
318+
createTiff(sourceFile, 8, 1024D, 768D);
319+
}
320+
321+
@Test
322+
public void packbitsCompression8BitMinIsWhiteTest() throws IOException {
323+
String sourceFile = SOURCE_FOLDER + "packbitsCompression8BitMinIsWhite.tif";
324+
325+
createTiff(sourceFile, 8, 1024D, 768D);
326+
}
327+
328+
private static void createTiff (String sourceFile, int bpc, double width, double height)
329+
throws MalformedURLException {
330+
ImageData img = ImageDataFactory.createTiff(UrlUtil.toURL(sourceFile),
331+
false, 1, false);
332+
333+
Assert.assertEquals(bpc, img.getBpc(), DELTA);
334+
Assert.assertEquals(width, img.getWidth(), DELTA);
335+
Assert.assertEquals(height, img.getHeight(), DELTA);
336+
}
125337
}

0 commit comments

Comments
 (0)