diff --git a/openpdf/src/main/java/com/lowagie/text/pdf/codec/TiffImage.java b/openpdf/src/main/java/com/lowagie/text/pdf/codec/TiffImage.java index 8ade08ea7..61f531a81 100644 --- a/openpdf/src/main/java/com/lowagie/text/pdf/codec/TiffImage.java +++ b/openpdf/src/main/java/com/lowagie/text/pdf/codec/TiffImage.java @@ -325,11 +325,12 @@ else if (rot == TIFFConstants.ORIENTATION_RIGHTTOP || rot == TIFFConstants.ORIEN if (dir.isTagPresent(TIFFConstants.TIFFTAG_PLANARCONFIG) && dir.getFieldAsLong(TIFFConstants.TIFFTAG_PLANARCONFIG) == TIFFConstants.PLANARCONFIG_SEPARATE) throw new IllegalArgumentException(MessageLocalization.getComposedMessage("planar.images.are.not.supported")); - if (dir.isTagPresent(TIFFConstants.TIFFTAG_EXTRASAMPLES)) - throw new IllegalArgumentException(MessageLocalization.getComposedMessage("extra.samples.are.not.supported")); int samplePerPixel = 1; if (dir.isTagPresent(TIFFConstants.TIFFTAG_SAMPLESPERPIXEL)) // 1,3,4 samplePerPixel = (int)dir.getFieldAsLong(TIFFConstants.TIFFTAG_SAMPLESPERPIXEL); + if (dir.isTagPresent(TIFFConstants.TIFFTAG_EXTRASAMPLES)) + if(samplePerPixel != 4) // TIFFTAG_EXTRASAMPLES is supposed to be set when RGB image data has an alpha channel (the 4th channel in this case). + throw new IllegalArgumentException(MessageLocalization.getComposedMessage("extra.samples.are.not.supported")); int bitsPerSample = 1; if (dir.isTagPresent(TIFFConstants.TIFFTAG_BITSPERSAMPLE)) bitsPerSample = (int)dir.getFieldAsLong(TIFFConstants.TIFFTAG_BITSPERSAMPLE); diff --git a/openpdf/src/test/java/com/lowagie/text/pdf/codec/TiffReadingTest.java b/openpdf/src/test/java/com/lowagie/text/pdf/codec/TiffReadingTest.java new file mode 100644 index 000000000..ecd351b39 --- /dev/null +++ b/openpdf/src/test/java/com/lowagie/text/pdf/codec/TiffReadingTest.java @@ -0,0 +1,42 @@ +package com.lowagie.text.pdf.codec; + +import com.lowagie.text.pdf.RandomAccessFileOrArray; +import org.junit.Assert; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * Tiff2PdfTest + * + * @author tellef + * @date 02.10.2017 + */ +public class TiffReadingTest { + + @Test + public void transparentTiffTest() throws IOException { + InputStream inputStream = TiffReadingTest.class.getClassLoader().getResourceAsStream("gradient.tiff"); + byte[] data; + + try(ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream()) { + int bytesRead; + byte[] buffer = new byte[8192]; + + while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) { + byteOutputStream.write(buffer, 0, bytesRead); + } + + data = byteOutputStream.toByteArray(); + } + + RandomAccessFileOrArray ra = new RandomAccessFileOrArray(data); + int pages = TiffImage.getNumberOfPages(ra); + + for (int i = 1; i <= pages; i++) { + Assert.assertNotNull(TiffImage.getTiffImage(ra, i)); + } + } +} diff --git a/openpdf/src/test/resources/gradient.tiff b/openpdf/src/test/resources/gradient.tiff new file mode 100644 index 000000000..60ad6a478 Binary files /dev/null and b/openpdf/src/test/resources/gradient.tiff differ