@@ -54,15 +54,15 @@ source product.
54
54
55
55
namespace iText . Kernel . Pdf . Xobject {
56
56
public class GetImageBytesTest : ExtendedITextTest {
57
- private static readonly String sourceFolder = iText . Test . TestUtil . GetParentProjectDirectory ( NUnit . Framework . TestContext
57
+ private static readonly String SOURCE_FOLDER = iText . Test . TestUtil . GetParentProjectDirectory ( NUnit . Framework . TestContext
58
58
. CurrentContext . TestDirectory ) + "/resources/itext/kernel/pdf/xobject/GetImageBytesTest/" ;
59
59
60
- private static readonly String destinationFolder = NUnit . Framework . TestContext . CurrentContext . TestDirectory
60
+ private static readonly String DESTINATION_FOLDER = NUnit . Framework . TestContext . CurrentContext . TestDirectory
61
61
+ "/test/itext/kernel/pdf/xobject/GetImageBytesTest/" ;
62
62
63
63
[ NUnit . Framework . OneTimeSetUp ]
64
64
public static void BeforeClass ( ) {
65
- CreateOrClearDestinationFolder ( destinationFolder ) ;
65
+ CreateOrClearDestinationFolder ( DESTINATION_FOLDER ) ;
66
66
}
67
67
68
68
[ NUnit . Framework . Test ]
@@ -134,11 +134,38 @@ public virtual void TestJPXDecode() {
134
134
TestFile ( "JPXDecode.pdf" , "Im1" , "jp2" ) ;
135
135
}
136
136
137
+ [ NUnit . Framework . Test ]
138
+ public virtual void TestSeparationCSWithICCBasedAsAlternative ( ) {
139
+ // TODO: DEVSIX-3538 (update test after fix)
140
+ Exception e = NUnit . Framework . Assert . Catch ( typeof ( iText . IO . Exceptions . IOException ) , ( ) => TestFile ( "separationCSWithICCBasedAsAlternative.pdf"
141
+ , "Im1" , "tif" ) ) ;
142
+ NUnit . Framework . Assert . AreEqual ( MessageFormatUtil . Format ( iText . IO . Exceptions . IOException . ColorSpaceIsNotSupported
143
+ , PdfName . Separation ) , e . Message ) ;
144
+ }
145
+
146
+ [ NUnit . Framework . Test ]
147
+ public virtual void TestSeparationCSWithDeviceCMYKAsAlternative ( ) {
148
+ // TODO: DEVSIX-3538 (update test after fix)
149
+ Exception e = NUnit . Framework . Assert . Catch ( typeof ( iText . IO . Exceptions . IOException ) , ( ) => TestFile ( "separationCSWithDeviceCMYKAsAlternative.pdf"
150
+ , "Im1" , "tif" ) ) ;
151
+ NUnit . Framework . Assert . AreEqual ( MessageFormatUtil . Format ( iText . IO . Exceptions . IOException . ColorSpaceIsNotSupported
152
+ , PdfName . Separation ) , e . Message ) ;
153
+ }
154
+
155
+ [ NUnit . Framework . Test ]
156
+ public virtual void TestSeparationCSWithDeviceRGBAsAlternative ( ) {
157
+ // TODO: DEVSIX-3538 (update test after fix)
158
+ Exception e = NUnit . Framework . Assert . Catch ( typeof ( iText . IO . Exceptions . IOException ) , ( ) => TestFile ( "separationCSWithDeviceRgbAsAlternative.pdf"
159
+ , "Im1" , "tif" ) ) ;
160
+ NUnit . Framework . Assert . AreEqual ( MessageFormatUtil . Format ( iText . IO . Exceptions . IOException . ColorSpaceIsNotSupported
161
+ , PdfName . Separation ) , e . Message ) ;
162
+ }
163
+
137
164
[ NUnit . Framework . Test ]
138
165
public virtual void ExtractByteAlignedG4TiffImageTest ( ) {
139
- String inFileName = sourceFolder + "extractByteAlignedG4TiffImage.pdf" ;
140
- String outImageFileName = destinationFolder + "extractedByteAlignedImage.png" ;
141
- String cmpImageFileName = sourceFolder + "cmp_extractByteAlignedG4TiffImage.png" ;
166
+ String inFileName = SOURCE_FOLDER + "extractByteAlignedG4TiffImage.pdf" ;
167
+ String outImageFileName = DESTINATION_FOLDER + "extractedByteAlignedImage.png" ;
168
+ String cmpImageFileName = SOURCE_FOLDER + "cmp_extractByteAlignedG4TiffImage.png" ;
142
169
PdfDocument pdfDocument = new PdfDocument ( new PdfReader ( inFileName ) ) ;
143
170
GetImageBytesTest . ImageExtractor listener = new GetImageBytesTest . ImageExtractor ( this ) ;
144
171
PdfCanvasProcessor processor = new PdfCanvasProcessor ( listener ) ;
@@ -163,7 +190,7 @@ public virtual void ExtractByteAlignedG4TiffImageTest() {
163
190
[ NUnit . Framework . Test ]
164
191
public virtual void ExpectedByteAlignedTiffImageExtractionTest ( ) {
165
192
//Byte-aligned image is expected in pdf file, but in fact it's not
166
- String inFileName = sourceFolder + "expectedByteAlignedTiffImageExtraction.pdf" ;
193
+ String inFileName = SOURCE_FOLDER + "expectedByteAlignedTiffImageExtraction.pdf" ;
167
194
PdfDocument pdfDocument = new PdfDocument ( new PdfReader ( inFileName ) ) ;
168
195
GetImageBytesTest . ImageExtractor listener = new GetImageBytesTest . ImageExtractor ( this ) ;
169
196
PdfCanvasProcessor processor = new PdfCanvasProcessor ( listener ) ;
@@ -207,31 +234,28 @@ internal ImageExtractor(GetImageBytesTest _enclosing) {
207
234
}
208
235
209
236
private void TestFile ( String filename , String objectid , String expectedImageFormat ) {
210
- PdfDocument pdfDocument = new PdfDocument ( new PdfReader ( sourceFolder + filename ) ) ;
211
- try {
212
- PdfResources resources = pdfDocument . GetPage ( 1 ) . GetResources ( ) ;
213
- PdfDictionary xobjects = resources . GetResource ( PdfName . XObject ) ;
214
- PdfObject obj = xobjects . Get ( new PdfName ( objectid ) ) ;
215
- if ( obj == null ) {
216
- throw new ArgumentException ( "Reference " + objectid + " not found - Available keys are " + xobjects . KeySet
217
- ( ) ) ;
218
- }
219
- PdfImageXObject img = new PdfImageXObject ( ( PdfStream ) ( obj . IsIndirectReference ( ) ? ( ( PdfIndirectReference ) obj
220
- ) . GetRefersTo ( ) : obj ) ) ;
221
- NUnit . Framework . Assert . AreEqual ( expectedImageFormat , img . IdentifyImageFileExtension ( ) ) ;
222
- byte [ ] result = img . GetImageBytes ( true ) ;
223
- byte [ ] cmpBytes = File . ReadAllBytes ( System . IO . Path . Combine ( sourceFolder , filename . JSubstring ( 0 , filename . Length
224
- - 4 ) + "." + expectedImageFormat ) ) ;
225
- if ( img . IdentifyImageFileExtension ( ) . Equals ( "tif" ) ) {
226
- CompareTiffImages ( cmpBytes , result ) ;
227
- }
228
- else {
229
- NUnit . Framework . Assert . AreEqual ( cmpBytes , result ) ;
237
+ using ( PdfReader reader = new PdfReader ( SOURCE_FOLDER + filename ) ) {
238
+ using ( PdfDocument pdfDocument = new PdfDocument ( reader ) ) {
239
+ PdfResources resources = pdfDocument . GetPage ( 1 ) . GetResources ( ) ;
240
+ PdfDictionary xobjects = resources . GetResource ( PdfName . XObject ) ;
241
+ PdfObject obj = xobjects . Get ( new PdfName ( objectid ) ) ;
242
+ if ( obj == null ) {
243
+ throw new ArgumentException ( "Reference " + objectid + " not found - Available keys are " + xobjects . KeySet
244
+ ( ) ) ;
245
+ }
246
+ PdfImageXObject img = new PdfImageXObject ( ( PdfStream ) obj ) ;
247
+ NUnit . Framework . Assert . AreEqual ( expectedImageFormat , img . IdentifyImageFileExtension ( ) ) ;
248
+ byte [ ] result = img . GetImageBytes ( true ) ;
249
+ byte [ ] cmpBytes = File . ReadAllBytes ( System . IO . Path . Combine ( SOURCE_FOLDER , filename . JSubstring ( 0 , filename .
250
+ Length - 4 ) + "." + expectedImageFormat ) ) ;
251
+ if ( img . IdentifyImageFileExtension ( ) . Equals ( "tif" ) ) {
252
+ CompareTiffImages ( cmpBytes , result ) ;
253
+ }
254
+ else {
255
+ NUnit . Framework . Assert . AreEqual ( cmpBytes , result ) ;
256
+ }
230
257
}
231
258
}
232
- finally {
233
- pdfDocument . Close ( ) ;
234
- }
235
259
}
236
260
237
261
private void CompareTiffImages ( byte [ ] cmpBytes , byte [ ] resultBytes ) {
@@ -253,9 +277,7 @@ private void CompareTiffImages(byte[] cmpBytes, byte[] resultBytes) {
253
277
NUnit . Framework . Assert . AreEqual ( cmpDir . IsTagPresent ( tag ) , resultDir . IsTagPresent ( tag ) ) ;
254
278
TIFFField cmpField = cmpDir . GetField ( tag ) ;
255
279
TIFFField resultField = resultDir . GetField ( tag ) ;
256
- if ( tag == TIFFConstants . TIFFTAG_SOFTWARE ) {
257
- }
258
- else {
280
+ if ( tag != TIFFConstants . TIFFTAG_SOFTWARE ) {
259
281
CompareFields ( cmpField , resultField ) ;
260
282
}
261
283
}
0 commit comments