@@ -22,8 +22,10 @@ This file is part of the iText (R) project.
2222 */
2323package com .itextpdf .kernel .pdf .xobject ;
2424
25+ import com .itextpdf .commons .datastructures .Tuple2 ;
2526import com .itextpdf .commons .utils .FileUtil ;
2627import com .itextpdf .commons .utils .MessageFormatUtil ;
28+ import com .itextpdf .commons .utils .Pair ;
2729import com .itextpdf .io .codec .TIFFConstants ;
2830import com .itextpdf .io .codec .TIFFDirectory ;
2931import com .itextpdf .io .codec .TIFFField ;
@@ -47,13 +49,15 @@ This file is part of the iText (R) project.
4749import com .itextpdf .test .ExtendedITextTest ;
4850import com .itextpdf .test .TestUtil ;
4951
52+ import java .io .FileOutputStream ;
5053import java .io .IOException ;
5154import java .io .InputStream ;
5255import java .io .OutputStream ;
5356import java .nio .file .Files ;
5457import java .nio .file .Paths ;
5558import java .util .ArrayList ;
5659import java .util .Arrays ;
60+ import java .util .List ;
5761import java .util .Set ;
5862
5963import org .junit .jupiter .api .Assertions ;
@@ -154,6 +158,7 @@ public void testSeparationCSWithICCBasedAsAlternative() throws Exception {
154158 }
155159
156160 @ Test
161+ // TODO: DEVSIX-6757 (update test after fix)
157162 // Android-Conversion-Ignore-Test (TODO DEVSIX-6445 fix different DeflaterOutputStream behavior)
158163 public void testSeparationCSWithDeviceCMYKAsAlternative () throws Exception {
159164 Assertions .assertThrows (UnsupportedOperationException .class , () ->
@@ -168,12 +173,14 @@ public void testGrayScalePng() throws Exception {
168173 }
169174
170175 @ Test
176+ // TODO: DEVSIX-6757 (update test after fix)
171177 // Android-Conversion-Ignore-Test (TODO DEVSIX-6445 fix different DeflaterOutputStream behavior)
172178 public void testSeparationCSWithDeviceRGBAsAlternative () throws Exception {
173179 testFile ("separationCSWithDeviceRgbAsAlternative.pdf" , "Im1" , "png" );
174180 }
175181
176182 @ Test
183+ // TODO: DEVSIX-6757 (update test after fix)
177184 // Android-Conversion-Ignore-Test (TODO DEVSIX-6445 fix different DeflaterOutputStream behavior)
178185 public void testSeparationCSWithDeviceRGBAsAlternative2 () throws Exception {
179186 testFile ("spotColorImagesSmall.pdf" , "Im1" , "png" );
@@ -228,27 +235,27 @@ public void extractByteAlignedG4TiffImageTest() throws IOException {
228235
229236 PdfDocument pdfDocument = new PdfDocument (new PdfReader (inFileName ));
230237
231- ImageExtractor listener = new ImageExtractor ();
238+ ImageAndTypeExtractor listener = new ImageAndTypeExtractor ();
232239 PdfCanvasProcessor processor = new PdfCanvasProcessor (listener );
233240 processor .processPageContent (pdfDocument .getPage (1 ));
234241
235- java . util . List <byte []> images = listener .getImages ();
242+ List <Tuple2 < String , byte []> > images = listener .getImages ();
236243 Assertions .assertEquals (1 , images .size ());
237244
238245 try (OutputStream fos = FileUtil .getFileOutputStream (outImageFileName )) {
239- fos .write (images .get (0 ), 0 , images .size ());
246+ fos .write (images .get (0 ). getSecond () , 0 , images .size ());
240247 }
241248
242249 // expected and actual are swapped here for simplicity
243- int expectedLen = images .get (0 ).length ;
250+ int expectedLen = images .get (0 ).getSecond (). length ;
244251 byte [] buf = new byte [expectedLen ];
245252 try (InputStream is = FileUtil .getInputStreamForFile (cmpImageFileName )) {
246253 int read = is .read (buf , 0 , buf .length );
247254 Assertions .assertEquals (expectedLen , read );
248255 read = is .read (buf , 0 , buf .length );
249256 Assertions .assertTrue (read <= 0 );
250257 }
251- Assertions .assertArrayEquals (images .get (0 ), buf );
258+ Assertions .assertArrayEquals (images .get (0 ). getSecond () , buf );
252259 }
253260
254261 @ Test
@@ -258,7 +265,7 @@ public void expectedByteAlignedTiffImageExtractionTest() throws IOException {
258265
259266 PdfDocument pdfDocument = new PdfDocument (new PdfReader (inFileName ));
260267
261- ImageExtractor listener = new ImageExtractor ();
268+ ImageAndTypeExtractor listener = new ImageAndTypeExtractor ();
262269 PdfCanvasProcessor processor = new PdfCanvasProcessor (listener );
263270
264271 Exception e = Assertions .assertThrows (com .itextpdf .io .exceptions .IOException .class ,
@@ -269,6 +276,34 @@ public void expectedByteAlignedTiffImageExtractionTest() throws IOException {
269276 e .getMessage ());
270277 }
271278
279+
280+ @ Test
281+ public void inlineImageColorDepth1Test () throws IOException {
282+ //Byte-aligned image is expected in pdf file, but in fact it's not
283+ String inFileName = SOURCE_FOLDER + "inline_image_with_cs_object.pdf" ;
284+
285+ PdfDocument pdfDocument = new PdfDocument (new PdfReader (inFileName ));
286+
287+ ImageAndTypeExtractor listener = new ImageAndTypeExtractor ();
288+ PdfCanvasProcessor processor = new PdfCanvasProcessor (listener );
289+ processor .processPageContent (pdfDocument .getPage (1 ));
290+
291+
292+ Files .write (Paths .get (
293+ DESTINATION_FOLDER ,
294+ "inline_image_with_cs_object.new." + listener .images .get (0 ).getFirst ()),
295+ listener .images .get (0 ).getSecond ());
296+
297+
298+ Assertions .assertEquals (1 , listener .images .size ());
299+ Assertions .assertEquals ("png" , listener .images .get (0 ).getFirst ());
300+
301+ byte [] cmpBytes = Files .readAllBytes (Paths .get (
302+ SOURCE_FOLDER , "inline_image_with_cs_object.png" ));
303+ Assertions .assertArrayEquals (cmpBytes , listener .images .get (0 ).getSecond ());
304+
305+ }
306+
272307 @ Test
273308 public void deviceGray8bitTest () throws Exception {
274309 testFile ("deviceGray8bit.pdf" , "fzImg0" , "png" );
@@ -281,7 +316,7 @@ public void deviceGray8bitFlateDecodeTest() throws Exception {
281316
282317 @ Test
283318 public void deviceGray1bitFlateDecodeInvertedTest () throws Exception {
284- testFile ("deviceGray1bitFlateDecodeInverted.pdf" , "Im0" , "png" , true );
319+ testFile ("deviceGray1bitFlateDecodeInverted.pdf" , "Im0" , "png" );
285320 }
286321
287322 @ Test
@@ -1018,15 +1053,16 @@ private byte[] subArray(byte[] array, int beg, int end) {
10181053 return Arrays .copyOfRange (array , beg , end + 1 );
10191054 }
10201055
1021- private class ImageExtractor implements IEventListener {
1022- private final java .util .List <byte []> images = new ArrayList <>();
1056+ private static class ImageAndTypeExtractor implements IEventListener {
1057+ public final java .util .List <Tuple2 <String , byte []>> images = new ArrayList <>();
1058+
10231059
10241060 public void eventOccurred (IEventData data , EventType type ) {
10251061 switch (type ) {
10261062 case RENDER_IMAGE :
10271063 ImageRenderInfo renderInfo = (ImageRenderInfo ) data ;
10281064 byte [] bytes = renderInfo .getImage ().getImageBytes ();
1029- images .add (bytes );
1065+ images .add (new Tuple2 <>( renderInfo . getImage (). identifyImageFileExtension (), bytes ) );
10301066 break ;
10311067 default :
10321068 break ;
@@ -1037,7 +1073,7 @@ public Set<EventType> getSupportedEvents() {
10371073 return null ;
10381074 }
10391075
1040- public java . util . List <byte []> getImages () {
1076+ public List <Tuple2 < String , byte []> > getImages () {
10411077 return images ;
10421078 }
10431079 }
0 commit comments