@@ -42,6 +42,12 @@ This file is part of the iText (R) project.
42
42
*/
43
43
package com .itextpdf .kernel .pdf .xobject ;
44
44
45
+ import com .itextpdf .io .codec .TIFFConstants ;
46
+ import com .itextpdf .io .codec .TIFFDirectory ;
47
+ import com .itextpdf .io .codec .TIFFField ;
48
+ import com .itextpdf .io .source .RandomAccessFileOrArray ;
49
+ import com .itextpdf .io .source .RandomAccessSourceFactory ;
50
+ import com .itextpdf .kernel .Version ;
45
51
import com .itextpdf .kernel .pdf .PdfDictionary ;
46
52
import com .itextpdf .kernel .pdf .PdfDocument ;
47
53
import com .itextpdf .kernel .pdf .PdfIndirectReference ;
@@ -52,11 +58,15 @@ This file is part of the iText (R) project.
52
58
import com .itextpdf .kernel .pdf .PdfStream ;
53
59
import com .itextpdf .test .ExtendedITextTest ;
54
60
import com .itextpdf .test .annotations .type .IntegrationTest ;
61
+
62
+ import java .io .IOException ;
63
+ import java .nio .charset .StandardCharsets ;
55
64
import java .nio .file .Files ;
56
65
import java .nio .file .Paths ;
66
+ import java .util .Arrays ;
67
+
57
68
import org .junit .Assert ;
58
69
import org .junit .BeforeClass ;
59
- import org .junit .Ignore ;
60
70
import org .junit .Test ;
61
71
import org .junit .experimental .categories .Category ;
62
72
@@ -106,13 +116,11 @@ public void testjbig2Filters() throws Exception {
106
116
}
107
117
108
118
@ Test
109
- @ Ignore ("Ignored during the latest release. Please unignore after DEVSIX-3021" )
110
- public void testFlateCmyk () throws Exception {
119
+ public void testFlateCmyk () throws Exception {
111
120
testFile ("img_cmyk.pdf" , "Im1" , "tif" );
112
121
}
113
122
114
123
@ Test
115
- @ Ignore ("Ignored during the latest release. Please unignore after DEVSIX-3021" )
116
124
public void testFlateCmykIcc () throws Exception {
117
125
testFile ("img_cmyk_icc.pdf" , "Im1" , "tif" );
118
126
}
@@ -149,11 +157,113 @@ private void testFile(String filename, String objectid, String expectedImageForm
149
157
PdfImageXObject img = new PdfImageXObject ((PdfStream ) (obj .isIndirectReference () ? ((PdfIndirectReference ) obj ).getRefersTo () : obj ));
150
158
Assert .assertEquals (expectedImageFormat , img .identifyImageFileExtension ());
151
159
160
+
152
161
byte [] result = img .getImageBytes (true );
153
162
byte [] cmpBytes = Files .readAllBytes (Paths .get (sourceFolder , filename .substring (0 , filename .length () - 4 ) + "." + expectedImageFormat ));
154
- Assert .assertArrayEquals (cmpBytes , result );
163
+
164
+ if (img .identifyImageFileExtension ().equals ("tif" )) {
165
+ compareTiffImages (cmpBytes , result );
166
+ } else {
167
+ Assert .assertArrayEquals (cmpBytes , result );
168
+ }
155
169
} finally {
156
170
pdfDocument .close ();
157
171
}
158
172
}
173
+
174
+ private void compareTiffImages (byte [] cmpBytes , byte [] resultBytes ) throws IOException {
175
+ int cmpNumDirectories = TIFFDirectory .getNumDirectories (new RandomAccessFileOrArray (new RandomAccessSourceFactory ().createSource (cmpBytes )));
176
+ int resultNumDirectories = TIFFDirectory .getNumDirectories (new RandomAccessFileOrArray (new RandomAccessSourceFactory ().createSource (resultBytes )));
177
+
178
+ Assert .assertEquals (cmpNumDirectories , resultNumDirectories );
179
+
180
+ for (int dirNum = 0 ; dirNum < cmpNumDirectories ; ++dirNum ) {
181
+ TIFFDirectory cmpDir = new TIFFDirectory (new RandomAccessFileOrArray (new RandomAccessSourceFactory ().createSource (cmpBytes )), dirNum );
182
+ TIFFDirectory resultDir = new TIFFDirectory (new RandomAccessFileOrArray (new RandomAccessSourceFactory ().createSource (resultBytes )), dirNum );
183
+
184
+ Assert .assertEquals (cmpDir .getNumEntries (), resultDir .getNumEntries ());
185
+ Assert .assertEquals (cmpDir .getIFDOffset (), resultDir .getIFDOffset ());
186
+ Assert .assertEquals (cmpDir .getNextIFDOffset (), resultDir .getNextIFDOffset ());
187
+ Assert .assertArrayEquals (cmpDir .getTags (), resultDir .getTags ());
188
+
189
+ for (int tag : cmpDir .getTags ()) {
190
+ Assert .assertEquals (cmpDir .isTagPresent (tag ), resultDir .isTagPresent (tag ));
191
+
192
+ TIFFField cmpField = cmpDir .getField (tag );
193
+ TIFFField resultField = resultDir .getField (tag );
194
+
195
+ if (tag == TIFFConstants .TIFFTAG_SOFTWARE ) {
196
+ compareSoftwareVersion (cmpField , resultField );
197
+ } else {
198
+ compareFields (cmpField , resultField );
199
+ }
200
+ }
201
+
202
+ compareImageData (cmpDir , resultDir , cmpBytes , resultBytes );
203
+ }
204
+ }
205
+
206
+ private void compareSoftwareVersion (TIFFField cmpField , TIFFField resultField ) {
207
+ byte [] versionBytes = resultField .getAsString (0 ).getBytes (StandardCharsets .US_ASCII );
208
+ byte [] versionToCompare = subArray (versionBytes , 0 , versionBytes .length - 2 ); //drop last always zero byte
209
+
210
+ Assert .assertArrayEquals (Version .getInstance ().getVersion ().getBytes (StandardCharsets .US_ASCII ), versionToCompare );
211
+ }
212
+
213
+ private void compareFields (TIFFField cmpField , TIFFField resultField ) {
214
+ if (cmpField .getType () == TIFFField .TIFF_LONG ) {
215
+ Assert .assertArrayEquals (cmpField .getAsLongs (), resultField .getAsLongs ());
216
+ } else if (cmpField .getType () == TIFFField .TIFF_BYTE ) {
217
+ Assert .assertArrayEquals (cmpField .getAsBytes (), resultField .getAsBytes ());
218
+ } else if (cmpField .getType () == TIFFField .TIFF_SBYTE ) {
219
+ Assert .assertArrayEquals (cmpField .getAsBytes (), resultField .getAsBytes ());
220
+ } else if (cmpField .getType () == TIFFField .TIFF_SHORT ) {
221
+ Assert .assertArrayEquals (cmpField .getAsChars (), resultField .getAsChars ());
222
+ } else if (cmpField .getType () == TIFFField .TIFF_SLONG ) {
223
+ Assert .assertArrayEquals (cmpField .getAsInts (), resultField .getAsInts ());
224
+ } else if (cmpField .getType () == TIFFField .TIFF_SSHORT ) {
225
+ Assert .assertArrayEquals (cmpField .getAsChars (), resultField .getAsChars ());
226
+ } else if (cmpField .getType () == TIFFField .TIFF_UNDEFINED ) {
227
+ Assert .assertArrayEquals (cmpField .getAsBytes (), resultField .getAsBytes ());
228
+ } else if (cmpField .getType () == TIFFField .TIFF_DOUBLE ) {
229
+ Assert .assertArrayEquals (cmpField .getAsDoubles (), resultField .getAsDoubles (), 0 );
230
+ } else if (cmpField .getType () == TIFFField .TIFF_FLOAT ) {
231
+ Assert .assertArrayEquals (cmpField .getAsFloats (), resultField .getAsFloats (), 0 );
232
+ } else if (cmpField .getType () == TIFFField .TIFF_RATIONAL ) {
233
+ Assert .assertArrayEquals (cmpField .getAsRationals (), resultField .getAsRationals ());
234
+ } else if (cmpField .getType () == TIFFField .TIFF_SRATIONAL ) {
235
+ Assert .assertArrayEquals (cmpField .getAsSRationals (), resultField .getAsSRationals ());
236
+ } else if (cmpField .getType () == TIFFField .TIFF_ASCII ) {
237
+ Assert .assertArrayEquals (cmpField .getAsStrings (), resultField .getAsStrings ());
238
+ } else {
239
+ Assert .assertArrayEquals (cmpField .getAsBytes (), resultField .getAsBytes ());
240
+ }
241
+ }
242
+
243
+ private void compareImageData (TIFFDirectory cmpDir , TIFFDirectory resultDir , byte [] cmpBytes , byte [] resultBytes ) {
244
+ Assert .assertTrue (cmpDir .isTagPresent (TIFFConstants .TIFFTAG_STRIPOFFSETS ));
245
+ Assert .assertTrue (cmpDir .isTagPresent (TIFFConstants .TIFFTAG_STRIPBYTECOUNTS ));
246
+ Assert .assertTrue (resultDir .isTagPresent (TIFFConstants .TIFFTAG_STRIPOFFSETS ));
247
+ Assert .assertTrue (resultDir .isTagPresent (TIFFConstants .TIFFTAG_STRIPBYTECOUNTS ));
248
+
249
+ long [] cmpImageOffsets = cmpDir .getField (TIFFConstants .TIFFTAG_STRIPOFFSETS ).getAsLongs ();
250
+ long [] cmpStripByteCountsArray = cmpDir .getField (TIFFConstants .TIFFTAG_STRIPOFFSETS ).getAsLongs ();
251
+ long [] resultImageOffsets = resultDir .getField (TIFFConstants .TIFFTAG_STRIPOFFSETS ).getAsLongs ();
252
+ long [] resultStripByteCountsArray = resultDir .getField (TIFFConstants .TIFFTAG_STRIPOFFSETS ).getAsLongs ();
253
+
254
+ Assert .assertEquals (cmpImageOffsets .length , resultImageOffsets .length );
255
+ Assert .assertEquals (cmpStripByteCountsArray .length , resultStripByteCountsArray .length );
256
+
257
+ for (int i = 0 ; i < cmpImageOffsets .length ; ++i ) {
258
+ int cmpOffset = (int ) cmpImageOffsets [i ], cmpCounts = (int ) cmpStripByteCountsArray [i ];
259
+ int resultOffset = (int ) resultImageOffsets [i ], resultCounts = (int ) resultStripByteCountsArray [i ];
260
+
261
+ Assert .assertArrayEquals (subArray (cmpBytes , cmpOffset , (cmpOffset + cmpCounts - 1 )),
262
+ subArray (resultBytes , resultOffset , (resultOffset + resultCounts - 1 )));
263
+ }
264
+ }
265
+
266
+ private byte [] subArray (byte [] array , int beg , int end ) {
267
+ return Arrays .copyOfRange (array , beg , end + 1 );
268
+ }
159
269
}
0 commit comments