@@ -50,7 +50,6 @@ This file is part of the iText (R) project.
50
50
import com .itextpdf .commons .utils .MessageFormatUtil ;
51
51
import com .itextpdf .kernel .pdf .PdfDictionary ;
52
52
import com .itextpdf .kernel .pdf .PdfDocument ;
53
- import com .itextpdf .kernel .pdf .PdfIndirectReference ;
54
53
import com .itextpdf .kernel .pdf .PdfName ;
55
54
import com .itextpdf .kernel .pdf .PdfObject ;
56
55
import com .itextpdf .kernel .pdf .PdfReader ;
@@ -81,12 +80,12 @@ This file is part of the iText (R) project.
81
80
@ Category (IntegrationTest .class )
82
81
public class GetImageBytesTest extends ExtendedITextTest {
83
82
84
- private static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/pdf/xobject/GetImageBytesTest/" ;
85
- private static final String destinationFolder = "./target/test/com/itextpdf/kernel/pdf/xobject/GetImageBytesTest/" ;
83
+ private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/kernel/pdf/xobject/GetImageBytesTest/" ;
84
+ private static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/kernel/pdf/xobject/GetImageBytesTest/" ;
86
85
87
86
@ BeforeClass
88
87
public static void beforeClass () {
89
- createOrClearDestinationFolder (destinationFolder );
88
+ createOrClearDestinationFolder (DESTINATION_FOLDER );
90
89
}
91
90
92
91
@ Test
@@ -158,11 +157,41 @@ public void testJPXDecode() throws Exception {
158
157
testFile ("JPXDecode.pdf" , "Im1" , "jp2" );
159
158
}
160
159
160
+ @ Test
161
+ // TODO: DEVSIX-3538 (update test after fix)
162
+ public void testSeparationCSWithICCBasedAsAlternative () {
163
+ Exception e = Assert .assertThrows (com .itextpdf .io .exceptions .IOException .class , () -> testFile (
164
+ "separationCSWithICCBasedAsAlternative.pdf" , "Im1" , "tif" ));
165
+
166
+ Assert .assertEquals (MessageFormatUtil .format (
167
+ com .itextpdf .io .exceptions .IOException .ColorSpaceIsNotSupported , PdfName .Separation ), e .getMessage ());
168
+ }
169
+
170
+ @ Test
171
+ // TODO: DEVSIX-3538 (update test after fix)
172
+ public void testSeparationCSWithDeviceCMYKAsAlternative () {
173
+ Exception e = Assert .assertThrows (com .itextpdf .io .exceptions .IOException .class , () -> testFile (
174
+ "separationCSWithDeviceCMYKAsAlternative.pdf" , "Im1" , "tif" ));
175
+
176
+ Assert .assertEquals (MessageFormatUtil .format (
177
+ com .itextpdf .io .exceptions .IOException .ColorSpaceIsNotSupported , PdfName .Separation ), e .getMessage ());
178
+ }
179
+
180
+ @ Test
181
+ // TODO: DEVSIX-3538 (update test after fix)
182
+ public void testSeparationCSWithDeviceRGBAsAlternative () {
183
+ Exception e = Assert .assertThrows (com .itextpdf .io .exceptions .IOException .class , () -> testFile (
184
+ "separationCSWithDeviceRgbAsAlternative.pdf" , "Im1" , "tif" ));
185
+
186
+ Assert .assertEquals (MessageFormatUtil .format (
187
+ com .itextpdf .io .exceptions .IOException .ColorSpaceIsNotSupported , PdfName .Separation ), e .getMessage ());
188
+ }
189
+
161
190
@ Test
162
191
public void extractByteAlignedG4TiffImageTest () throws IOException {
163
- String inFileName = sourceFolder + "extractByteAlignedG4TiffImage.pdf" ;
164
- String outImageFileName = destinationFolder + "extractedByteAlignedImage.png" ;
165
- String cmpImageFileName = sourceFolder + "cmp_extractByteAlignedG4TiffImage.png" ;
192
+ String inFileName = SOURCE_FOLDER + "extractByteAlignedG4TiffImage.pdf" ;
193
+ String outImageFileName = DESTINATION_FOLDER + "extractedByteAlignedImage.png" ;
194
+ String cmpImageFileName = SOURCE_FOLDER + "cmp_extractByteAlignedG4TiffImage.png" ;
166
195
167
196
PdfDocument pdfDocument = new PdfDocument (new PdfReader (inFileName ));
168
197
@@ -192,7 +221,7 @@ public void extractByteAlignedG4TiffImageTest() throws IOException {
192
221
@ Test
193
222
public void expectedByteAlignedTiffImageExtractionTest () throws IOException {
194
223
//Byte-aligned image is expected in pdf file, but in fact it's not
195
- String inFileName = sourceFolder + "expectedByteAlignedTiffImageExtraction.pdf" ;
224
+ String inFileName = SOURCE_FOLDER + "expectedByteAlignedTiffImageExtraction.pdf" ;
196
225
197
226
PdfDocument pdfDocument = new PdfDocument (new PdfReader (inFileName ));
198
227
@@ -202,7 +231,9 @@ public void expectedByteAlignedTiffImageExtractionTest() throws IOException {
202
231
Exception e = Assert .assertThrows (com .itextpdf .io .exceptions .IOException .class ,
203
232
() -> processor .processPageContent (pdfDocument .getPage (1 ))
204
233
);
205
- Assert .assertEquals (MessageFormatUtil .format (com .itextpdf .io .exceptions .IOException .ExpectedTrailingZeroBitsForByteAlignedLines ), e .getMessage ());
234
+ Assert .assertEquals (MessageFormatUtil
235
+ .format (com .itextpdf .io .exceptions .IOException .ExpectedTrailingZeroBitsForByteAlignedLines ),
236
+ e .getMessage ());
206
237
}
207
238
208
239
private class ImageExtractor implements IEventListener {
@@ -230,40 +261,45 @@ public java.util.List<byte[]> getImages() {
230
261
}
231
262
232
263
private void testFile (String filename , String objectid , String expectedImageFormat ) throws Exception {
233
- PdfDocument pdfDocument = new PdfDocument ( new PdfReader (sourceFolder + filename ) );
234
- try {
264
+ try ( PdfReader reader = new PdfReader (SOURCE_FOLDER + filename );
265
+ PdfDocument pdfDocument = new PdfDocument ( reader )) {
235
266
PdfResources resources = pdfDocument .getPage (1 ).getResources ();
236
267
PdfDictionary xobjects = resources .getResource (PdfName .XObject );
237
268
PdfObject obj = xobjects .get (new PdfName (objectid ));
238
269
if (obj == null ) {
239
- throw new IllegalArgumentException ("Reference " + objectid + " not found - Available keys are " + xobjects .keySet ());
270
+ throw new IllegalArgumentException ("Reference " + objectid
271
+ + " not found - Available keys are " + xobjects .keySet ());
240
272
}
241
- PdfImageXObject img = new PdfImageXObject ((PdfStream ) (obj .isIndirectReference () ? ((PdfIndirectReference ) obj ).getRefersTo () : obj ));
242
- Assert .assertEquals (expectedImageFormat , img .identifyImageFileExtension ());
243
273
274
+ PdfImageXObject img = new PdfImageXObject ((PdfStream ) obj );
275
+
276
+ Assert .assertEquals (expectedImageFormat , img .identifyImageFileExtension ());
244
277
245
278
byte [] result = img .getImageBytes (true );
246
- byte [] cmpBytes = Files .readAllBytes (Paths .get (sourceFolder , filename .substring (0 , filename .length () - 4 ) + "." + expectedImageFormat ));
279
+ byte [] cmpBytes = Files .readAllBytes (Paths .get (
280
+ SOURCE_FOLDER , filename .substring (0 , filename .length () - 4 ) + "." + expectedImageFormat ));
247
281
248
282
if (img .identifyImageFileExtension ().equals ("tif" )) {
249
283
compareTiffImages (cmpBytes , result );
250
284
} else {
251
285
Assert .assertArrayEquals (cmpBytes , result );
252
286
}
253
- } finally {
254
- pdfDocument .close ();
255
287
}
256
288
}
257
289
258
290
private void compareTiffImages (byte [] cmpBytes , byte [] resultBytes ) throws IOException {
259
- int cmpNumDirectories = TIFFDirectory .getNumDirectories (new RandomAccessFileOrArray (new RandomAccessSourceFactory ().createSource (cmpBytes )));
260
- int resultNumDirectories = TIFFDirectory .getNumDirectories (new RandomAccessFileOrArray (new RandomAccessSourceFactory ().createSource (resultBytes )));
291
+ int cmpNumDirectories = TIFFDirectory .getNumDirectories (new RandomAccessFileOrArray (
292
+ new RandomAccessSourceFactory ().createSource (cmpBytes )));
293
+ int resultNumDirectories = TIFFDirectory .getNumDirectories (new RandomAccessFileOrArray (
294
+ new RandomAccessSourceFactory ().createSource (resultBytes )));
261
295
262
296
Assert .assertEquals (cmpNumDirectories , resultNumDirectories );
263
297
264
298
for (int dirNum = 0 ; dirNum < cmpNumDirectories ; ++dirNum ) {
265
- TIFFDirectory cmpDir = new TIFFDirectory (new RandomAccessFileOrArray (new RandomAccessSourceFactory ().createSource (cmpBytes )), dirNum );
266
- TIFFDirectory resultDir = new TIFFDirectory (new RandomAccessFileOrArray (new RandomAccessSourceFactory ().createSource (resultBytes )), dirNum );
299
+ TIFFDirectory cmpDir = new TIFFDirectory (new RandomAccessFileOrArray (
300
+ new RandomAccessSourceFactory ().createSource (cmpBytes )), dirNum );
301
+ TIFFDirectory resultDir = new TIFFDirectory (new RandomAccessFileOrArray (
302
+ new RandomAccessSourceFactory ().createSource (resultBytes )), dirNum );
267
303
268
304
Assert .assertEquals (cmpDir .getNumEntries (), resultDir .getNumEntries ());
269
305
Assert .assertEquals (cmpDir .getIFDOffset (), resultDir .getIFDOffset ());
@@ -276,8 +312,7 @@ private void compareTiffImages(byte[] cmpBytes, byte[] resultBytes) throws IOExc
276
312
TIFFField cmpField = cmpDir .getField (tag );
277
313
TIFFField resultField = resultDir .getField (tag );
278
314
279
- if (tag == TIFFConstants .TIFFTAG_SOFTWARE ) {
280
- } else {
315
+ if (tag != TIFFConstants .TIFFTAG_SOFTWARE ) {
281
316
compareFields (cmpField , resultField );
282
317
}
283
318
}
0 commit comments