Skip to content

Commit

Permalink
Merge pull request #430 from Schmidor/megeWithSubIFD
Browse files Browse the repository at this point in the history
Merging of TIFF Pages with Sub-IFDs caused a NPE
  • Loading branch information
haraldk committed Aug 2, 2018
2 parents 7cb1c68 + 18800b6 commit 99e27c1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,13 @@ else if (oldJpegDataLength != null) {
}

Entry compressionEntry = IFD.getEntryById(TIFF.TAG_COMPRESSION);
Number compression = (Number) compressionEntry.getValue();
if (compression.shortValue() == TIFFExtension.COMPRESSION_OLD_JPEG) {
newIFD.remove(compressionEntry);
newIFD.add(new TIFFEntry(TIFF.TAG_COMPRESSION, TIFF.TYPE_SHORT, TIFFExtension.COMPRESSION_JPEG));
if(compressionEntry != null) {
Number compression = (Number) compressionEntry.getValue();
if (compression.shortValue() == TIFFExtension.COMPRESSION_OLD_JPEG) {
newIFD.remove(compressionEntry);
newIFD.add(new TIFFEntry(TIFF.TAG_COMPRESSION, TIFF.TYPE_SHORT, TIFFExtension.COMPRESSION_JPEG));
}
}

return newIFD;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,34 @@ public void testMergeBogusInterchangeFormatLength() throws IOException {
}
}

@Test
public void testMergeWithSubIFD() throws IOException {
String testFile = "/tiff/cmyk_jpeg.tif";

File output = File.createTempFile("imageiotest", ".tif");
ImageOutputStream outputStream = ImageIO.createImageOutputStream(output);
InputStream inputStream1 = getClassLoaderResource(testFile).openStream();
ImageInputStream imageInput1 = ImageIO.createImageInputStream(inputStream1);
InputStream inputStream2 = getClassLoaderResource(testFile).openStream();
ImageInputStream imageInput2 = ImageIO.createImageInputStream(inputStream2);
ArrayList<TIFFUtilities.TIFFPage> pages = new ArrayList<>();
pages.addAll(TIFFUtilities.getPages(imageInput1));
pages.addAll(TIFFUtilities.getPages(imageInput2));
TIFFUtilities.writePages(outputStream, pages);

ImageInputStream testOutput = ImageIO.createImageInputStream(output);
ImageReader reader = ImageIO.getImageReaders(testOutput).next();
reader.setInput(testOutput);
int numImages = reader.getNumImages(true);
for (int i = 0; i < numImages; i++) {
reader.read(i);
}

imageInput1.close();
imageInput2.close();
outputStream.close();
}

protected URL getClassLoaderResource(final String pName) {
return getClass().getResource(pName);
}
Expand Down

0 comments on commit 99e27c1

Please sign in to comment.