Skip to content

Commit 44c5b1f

Browse files
Kate IvanovaUbuntu
authored andcommitted
Add tests for /Separation CS support
DEVSIX-5931
1 parent 0ce78fb commit 44c5b1f

File tree

4 files changed

+58
-23
lines changed

4 files changed

+58
-23
lines changed

kernel/src/test/java/com/itextpdf/kernel/pdf/xobject/GetImageBytesTest.java

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ This file is part of the iText (R) project.
5050
import com.itextpdf.commons.utils.MessageFormatUtil;
5151
import com.itextpdf.kernel.pdf.PdfDictionary;
5252
import com.itextpdf.kernel.pdf.PdfDocument;
53-
import com.itextpdf.kernel.pdf.PdfIndirectReference;
5453
import com.itextpdf.kernel.pdf.PdfName;
5554
import com.itextpdf.kernel.pdf.PdfObject;
5655
import com.itextpdf.kernel.pdf.PdfReader;
@@ -81,12 +80,12 @@ This file is part of the iText (R) project.
8180
@Category(IntegrationTest.class)
8281
public class GetImageBytesTest extends ExtendedITextTest {
8382

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/";
8685

8786
@BeforeClass
8887
public static void beforeClass() {
89-
createOrClearDestinationFolder(destinationFolder);
88+
createOrClearDestinationFolder(DESTINATION_FOLDER);
9089
}
9190

9291
@Test
@@ -158,11 +157,41 @@ public void testJPXDecode() throws Exception {
158157
testFile("JPXDecode.pdf", "Im1", "jp2");
159158
}
160159

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+
161190
@Test
162191
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";
166195

167196
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inFileName));
168197

@@ -192,7 +221,7 @@ public void extractByteAlignedG4TiffImageTest() throws IOException {
192221
@Test
193222
public void expectedByteAlignedTiffImageExtractionTest() throws IOException {
194223
//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";
196225

197226
PdfDocument pdfDocument = new PdfDocument(new PdfReader(inFileName));
198227

@@ -202,7 +231,9 @@ public void expectedByteAlignedTiffImageExtractionTest() throws IOException {
202231
Exception e = Assert.assertThrows(com.itextpdf.io.exceptions.IOException.class,
203232
() -> processor.processPageContent(pdfDocument.getPage(1))
204233
);
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());
206237
}
207238

208239
private class ImageExtractor implements IEventListener {
@@ -230,40 +261,45 @@ public java.util.List<byte[]> getImages() {
230261
}
231262

232263
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)) {
235266
PdfResources resources = pdfDocument.getPage(1).getResources();
236267
PdfDictionary xobjects = resources.getResource(PdfName.XObject);
237268
PdfObject obj = xobjects.get(new PdfName(objectid));
238269
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());
240272
}
241-
PdfImageXObject img = new PdfImageXObject((PdfStream) (obj.isIndirectReference() ? ((PdfIndirectReference) obj).getRefersTo() : obj));
242-
Assert.assertEquals(expectedImageFormat, img.identifyImageFileExtension());
243273

274+
PdfImageXObject img = new PdfImageXObject((PdfStream) obj);
275+
276+
Assert.assertEquals(expectedImageFormat, img.identifyImageFileExtension());
244277

245278
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));
247281

248282
if (img.identifyImageFileExtension().equals("tif")) {
249283
compareTiffImages(cmpBytes, result);
250284
} else {
251285
Assert.assertArrayEquals(cmpBytes, result);
252286
}
253-
} finally {
254-
pdfDocument.close();
255287
}
256288
}
257289

258290
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)));
261295

262296
Assert.assertEquals(cmpNumDirectories, resultNumDirectories);
263297

264298
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);
267303

268304
Assert.assertEquals(cmpDir.getNumEntries(), resultDir.getNumEntries());
269305
Assert.assertEquals(cmpDir.getIFDOffset(), resultDir.getIFDOffset());
@@ -276,8 +312,7 @@ private void compareTiffImages(byte[] cmpBytes, byte[] resultBytes) throws IOExc
276312
TIFFField cmpField = cmpDir.getField(tag);
277313
TIFFField resultField = resultDir.getField(tag);
278314

279-
if (tag == TIFFConstants.TIFFTAG_SOFTWARE) {
280-
} else {
315+
if (tag != TIFFConstants.TIFFTAG_SOFTWARE) {
281316
compareFields(cmpField, resultField);
282317
}
283318
}

0 commit comments

Comments
 (0)