@@ -47,6 +47,7 @@ This file is part of the iText (R) project.
47
47
import com .itextpdf .io .codec .TIFFField ;
48
48
import com .itextpdf .io .source .RandomAccessFileOrArray ;
49
49
import com .itextpdf .io .source .RandomAccessSourceFactory ;
50
+ import com .itextpdf .io .util .MessageFormatUtil ;
50
51
import com .itextpdf .kernel .Version ;
51
52
import com .itextpdf .kernel .pdf .PdfDictionary ;
52
53
import com .itextpdf .kernel .pdf .PdfDocument ;
@@ -56,26 +57,40 @@ This file is part of the iText (R) project.
56
57
import com .itextpdf .kernel .pdf .PdfReader ;
57
58
import com .itextpdf .kernel .pdf .PdfResources ;
58
59
import com .itextpdf .kernel .pdf .PdfStream ;
60
+ import com .itextpdf .kernel .pdf .canvas .parser .EventType ;
61
+ import com .itextpdf .kernel .pdf .canvas .parser .PdfCanvasProcessor ;
62
+ import com .itextpdf .kernel .pdf .canvas .parser .data .IEventData ;
63
+ import com .itextpdf .kernel .pdf .canvas .parser .data .ImageRenderInfo ;
64
+ import com .itextpdf .kernel .pdf .canvas .parser .listener .IEventListener ;
59
65
import com .itextpdf .test .ExtendedITextTest ;
60
66
import com .itextpdf .test .annotations .type .IntegrationTest ;
61
67
68
+ import java .io .FileInputStream ;
69
+ import java .io .FileOutputStream ;
62
70
import java .io .IOException ;
63
71
import java .nio .charset .StandardCharsets ;
64
72
import java .nio .file .Files ;
65
73
import java .nio .file .Paths ;
74
+ import java .util .ArrayList ;
66
75
import java .util .Arrays ;
67
76
77
+ import java .util .Set ;
68
78
import org .junit .Assert ;
69
79
import org .junit .BeforeClass ;
80
+ import org .junit .Rule ;
70
81
import org .junit .Test ;
71
82
import org .junit .experimental .categories .Category ;
83
+ import org .junit .rules .ExpectedException ;
72
84
73
85
@ Category (IntegrationTest .class )
74
86
public class GetImageBytesTest extends ExtendedITextTest {
75
87
76
88
private static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/pdf/xobject/GetImageBytesTest/" ;
77
89
private static final String destinationFolder = "./target/test/com/itextpdf/kernel/pdf/xobject/GetImageBytesTest/" ;
78
90
91
+ @ Rule
92
+ public ExpectedException junitExpectedException = ExpectedException .none ();
93
+
79
94
@ BeforeClass
80
95
public static void beforeClass () {
81
96
createOrClearDestinationFolder (destinationFolder );
@@ -145,6 +160,77 @@ public void testFlateCalRgb() throws Exception {
145
160
testFile ("img_calrgb.pdf" , "Im1" , "png" );
146
161
}
147
162
163
+ @ Test
164
+ public void extractByteAlignedG4TiffImageTest () throws IOException {
165
+ String inFileName = sourceFolder + "extractByteAlignedG4TiffImage.pdf" ;
166
+ String outImageFileName = destinationFolder + "extractedByteAlignedImage.png" ;
167
+ String cmpImageFileName = sourceFolder + "cmp_extractByteAlignedG4TiffImage.png" ;
168
+
169
+ PdfDocument pdfDocument = new PdfDocument (new PdfReader (inFileName ));
170
+
171
+ ImageExtractor listener = new ImageExtractor ();
172
+ PdfCanvasProcessor processor = new PdfCanvasProcessor (listener );
173
+ processor .processPageContent (pdfDocument .getPage (1 ));
174
+
175
+ java .util .List <byte []> images = listener .getImages ();
176
+ Assert .assertEquals (1 , images .size ());
177
+
178
+ try (FileOutputStream fos = new FileOutputStream (outImageFileName )) {
179
+ fos .write (images .get (0 ), 0 , images .size ());
180
+ }
181
+
182
+ // expected and actual are swapped here for simplicity
183
+ int expectedLen = images .get (0 ).length ;
184
+ byte [] buf = new byte [expectedLen ];
185
+ try (FileInputStream is = new FileInputStream (cmpImageFileName )) {
186
+ int read = is .read (buf , 0 , buf .length );
187
+ Assert .assertEquals (expectedLen , read );
188
+ read = is .read (buf , 0 , buf .length );
189
+ Assert .assertTrue (read <= 0 );
190
+ }
191
+ Assert .assertArrayEquals (images .get (0 ), buf );
192
+ }
193
+
194
+ @ Test
195
+ public void expectedByteAlignedTiffImageExtractionTest () throws IOException {
196
+ //Byte-aligned image is expected in pdf file, but in fact it's not
197
+ junitExpectedException .expect (com .itextpdf .io .IOException .class );
198
+ junitExpectedException .expectMessage (MessageFormatUtil .format
199
+ (com .itextpdf .io .IOException .ExpectedTrailingZeroBitsForByteAlignedLines ));
200
+
201
+ String inFileName = sourceFolder + "expectedByteAlignedTiffImageExtraction.pdf" ;
202
+
203
+ PdfDocument pdfDocument = new PdfDocument (new PdfReader (inFileName ));
204
+
205
+ ImageExtractor listener = new ImageExtractor ();
206
+ PdfCanvasProcessor processor = new PdfCanvasProcessor (listener );
207
+ processor .processPageContent (pdfDocument .getPage (1 ));
208
+ }
209
+
210
+ private class ImageExtractor implements IEventListener {
211
+ private java .util .List <byte []> images = new ArrayList <>();
212
+
213
+ public void eventOccurred (IEventData data , EventType type ) {
214
+ switch (type ) {
215
+ case RENDER_IMAGE :
216
+ ImageRenderInfo renderInfo = (ImageRenderInfo ) data ;
217
+ byte [] bytes = renderInfo .getImage ().getImageBytes ();
218
+ images .add (bytes );
219
+ break ;
220
+ default :
221
+ break ;
222
+ }
223
+ }
224
+
225
+ public Set <EventType > getSupportedEvents () {
226
+ return null ;
227
+ }
228
+
229
+ public java .util .List <byte []> getImages () {
230
+ return images ;
231
+ }
232
+ }
233
+
148
234
private void testFile (String filename , String objectid , String expectedImageFormat ) throws Exception {
149
235
PdfDocument pdfDocument = new PdfDocument (new PdfReader (sourceFolder + filename ));
150
236
try {
0 commit comments