@@ -41,11 +41,15 @@ source product.
41
41
42
42
*/
43
43
using System ;
44
+ using System . Collections . Generic ;
44
45
using System . IO ;
45
46
using iText . IO . Codec ;
46
47
using iText . IO . Source ;
47
48
using iText . IO . Util ;
48
49
using iText . Kernel . Pdf ;
50
+ using iText . Kernel . Pdf . Canvas . Parser ;
51
+ using iText . Kernel . Pdf . Canvas . Parser . Data ;
52
+ using iText . Kernel . Pdf . Canvas . Parser . Listener ;
49
53
using iText . Test ;
50
54
51
55
namespace iText . Kernel . Pdf . Xobject {
@@ -125,6 +129,79 @@ public virtual void TestFlateCalRgb() {
125
129
TestFile ( "img_calrgb.pdf" , "Im1" , "png" ) ;
126
130
}
127
131
132
+ [ NUnit . Framework . Test ]
133
+ public virtual void ExtractByteAlignedG4TiffImageTest ( ) {
134
+ String inFileName = sourceFolder + "extractByteAlignedG4TiffImage.pdf" ;
135
+ String outImageFileName = destinationFolder + "extractedByteAlignedImage.png" ;
136
+ String cmpImageFileName = sourceFolder + "cmp_extractByteAlignedG4TiffImage.png" ;
137
+ PdfDocument pdfDocument = new PdfDocument ( new PdfReader ( inFileName ) ) ;
138
+ GetImageBytesTest . ImageExtractor listener = new GetImageBytesTest . ImageExtractor ( this ) ;
139
+ PdfCanvasProcessor processor = new PdfCanvasProcessor ( listener ) ;
140
+ processor . ProcessPageContent ( pdfDocument . GetPage ( 1 ) ) ;
141
+ IList < byte [ ] > images = listener . GetImages ( ) ;
142
+ NUnit . Framework . Assert . AreEqual ( 1 , images . Count ) ;
143
+ using ( FileStream fos = new FileStream ( outImageFileName , FileMode . Create ) ) {
144
+ fos . Write ( images [ 0 ] , 0 , images . Count ) ;
145
+ }
146
+ // expected and actual are swapped here for simplicity
147
+ int expectedLen = images [ 0 ] . Length ;
148
+ byte [ ] buf = new byte [ expectedLen ] ;
149
+ using ( FileStream @is = new FileStream ( cmpImageFileName , FileMode . Open , FileAccess . Read ) ) {
150
+ int read = @is . JRead ( buf , 0 , buf . Length ) ;
151
+ NUnit . Framework . Assert . AreEqual ( expectedLen , read ) ;
152
+ read = @is . JRead ( buf , 0 , buf . Length ) ;
153
+ NUnit . Framework . Assert . IsTrue ( read <= 0 ) ;
154
+ }
155
+ NUnit . Framework . Assert . AreEqual ( images [ 0 ] , buf ) ;
156
+ }
157
+
158
+ [ NUnit . Framework . Test ]
159
+ public virtual void ExpectedByteAlignedTiffImageExtractionTest ( ) {
160
+ NUnit . Framework . Assert . That ( ( ) => {
161
+ //Byte-aligned image is expected in pdf file, but in fact it's not
162
+ String inFileName = sourceFolder + "expectedByteAlignedTiffImageExtraction.pdf" ;
163
+ PdfDocument pdfDocument = new PdfDocument ( new PdfReader ( inFileName ) ) ;
164
+ GetImageBytesTest . ImageExtractor listener = new GetImageBytesTest . ImageExtractor ( this ) ;
165
+ PdfCanvasProcessor processor = new PdfCanvasProcessor ( listener ) ;
166
+ processor . ProcessPageContent ( pdfDocument . GetPage ( 1 ) ) ;
167
+ }
168
+ , NUnit . Framework . Throws . InstanceOf < iText . IO . IOException > ( ) . With . Message . EqualTo ( MessageFormatUtil . Format ( iText . IO . IOException . ExpectedTrailingZeroBitsForByteAlignedLines ) ) )
169
+ ;
170
+ }
171
+
172
+ private class ImageExtractor : IEventListener {
173
+ private IList < byte [ ] > images = new List < byte [ ] > ( ) ;
174
+
175
+ public virtual void EventOccurred ( IEventData data , EventType type ) {
176
+ switch ( type ) {
177
+ case EventType . RENDER_IMAGE : {
178
+ ImageRenderInfo renderInfo = ( ImageRenderInfo ) data ;
179
+ byte [ ] bytes = renderInfo . GetImage ( ) . GetImageBytes ( ) ;
180
+ this . images . Add ( bytes ) ;
181
+ break ;
182
+ }
183
+
184
+ default : {
185
+ break ;
186
+ }
187
+ }
188
+ }
189
+
190
+ public virtual ICollection < EventType > GetSupportedEvents ( ) {
191
+ return null ;
192
+ }
193
+
194
+ public virtual IList < byte [ ] > GetImages ( ) {
195
+ return this . images ;
196
+ }
197
+
198
+ internal ImageExtractor ( GetImageBytesTest _enclosing ) {
199
+ this . _enclosing = _enclosing ;
200
+ }
201
+
202
+ private readonly GetImageBytesTest _enclosing ;
203
+ }
204
+
128
205
private void TestFile ( String filename , String objectid , String expectedImageFormat ) {
129
206
PdfDocument pdfDocument = new PdfDocument ( new PdfReader ( sourceFolder + filename ) ) ;
130
207
try {
0 commit comments