@@ -42,6 +42,7 @@ This file is part of the iText (R) project.
42
42
*/
43
43
package com .itextpdf .io .image ;
44
44
45
+ import com .itextpdf .commons .utils .MessageFormatUtil ;
45
46
import com .itextpdf .io .codec .TIFFDirectory ;
46
47
import com .itextpdf .io .source .RandomAccessFileOrArray ;
47
48
import com .itextpdf .io .source .RandomAccessSourceFactory ;
@@ -52,6 +53,7 @@ This file is part of the iText (R) project.
52
53
53
54
import java .io .FileInputStream ;
54
55
import java .io .IOException ;
56
+ import java .net .MalformedURLException ;
55
57
import java .nio .file .Files ;
56
58
import java .nio .file .Paths ;
57
59
import org .junit .Assert ;
@@ -61,11 +63,12 @@ This file is part of the iText (R) project.
61
63
@ Category (UnitTest .class )
62
64
public class TiffTest extends ExtendedITextTest {
63
65
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/" ;
65
68
66
69
@ Test
67
70
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" ));
69
72
// Test a more specific entry point
70
73
ImageData img = ImageDataFactory .createTiff (imageBytes , false , 1 , false );
71
74
Assert .assertEquals (2592 , img .getWidth (), 0 );
@@ -76,50 +79,259 @@ public void openTiff1() throws IOException {
76
79
@ Test
77
80
public void openTiff2 () throws IOException {
78
81
// 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 );
84
85
}
85
86
86
87
@ Test
87
88
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
+
89
91
Assert .assertEquals (2592 , img .getWidth (), 0 );
90
92
Assert .assertEquals (1456 , img .getHeight (), 0 );
91
93
Assert .assertEquals (8 , img .getBpc ());
92
94
}
93
95
94
96
@ Test
95
97
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
+
97
100
Assert .assertEquals (2592 , img .getWidth (), 0 );
98
101
Assert .assertEquals (1456 , img .getHeight (), 0 );
99
102
Assert .assertEquals (8 , img .getBpc ());
100
103
}
101
104
102
105
@ Test
103
106
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
+
105
109
Assert .assertEquals (2592 , img .getWidth (), 0 );
106
110
Assert .assertEquals (1456 , img .getHeight (), 0 );
107
111
Assert .assertEquals (8 , img .getBpc ());
108
112
}
109
113
110
114
@ Test
111
115
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
+
113
118
Assert .assertEquals (2592 , img .getWidth (), 0 );
114
119
Assert .assertEquals (1456 , img .getHeight (), 0 );
115
120
Assert .assertEquals (8 , img .getBpc ());
116
121
}
117
122
118
123
@ Test
119
124
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 );
122
128
String [] stringArray = new String [] {"iText? 7.1.7-SNAPSHOT ?2000-2019 iText Group NV (AGPL-version)\u0000 " };
129
+
123
130
Assert .assertArrayEquals (stringArray , dir .getField (305 ).getAsStrings ());
124
131
}
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
+ }
125
337
}
0 commit comments