Class PNGConverter


  • final class PNGConverter
    extends java.lang.Object
    This factory tries to encode a PNG given as byte array into a PDImageXObject by directly coping the image data into the PDF streams without decoding/encoding and re-compressing the PNG data.

    If this is for any reason not possible, the factory will return null. You must then encode the image by loading it and using the LosslessFactory.

    The W3C PNG spec was used to implement this class: https://www.w3.org/TR/2003/REC-PNG-20031110

    • Constructor Detail

      • PNGConverter

        private PNGConverter()
    • Method Detail

      • convertPNGImage

        static PDImageXObject convertPNGImage​(PDDocument doc,
                                              byte[] imageData)
                                       throws java.io.IOException
        Try to convert a PNG into a PDImageXObject. If for any reason the PNG can not be converted, null is returned.

        This usually means the PNG structure is damaged (CRC error, etc.) or it uses some features which can not be mapped to PDF.

        Parameters:
        doc - the document to put the image in
        imageData - the byte data of the PNG
        Returns:
        null or the PDImageXObject built from the png
        Throws:
        java.io.IOException
      • convertPng

        private static PDImageXObject convertPng​(PDDocument doc,
                                                 PNGConverter.PNGConverterState state)
                                          throws java.io.IOException
        Convert the image using the state.
        Parameters:
        doc - the document to put the image in
        state - the parser state containing the PNG chunks.
        Returns:
        null or the converted image
        Throws:
        java.io.IOException
      • setupIndexedColorSpace

        private static void setupIndexedColorSpace​(PDDocument doc,
                                                   PNGConverter.Chunk lookupTable,
                                                   PDImageXObject image,
                                                   int highVal)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • getIDATInputStream

        private static java.io.InputStream getIDATInputStream​(PNGConverter.PNGConverterState state)
        Build an input stream for the IDAT data. May need to concat multiple IDAT chunks.
        Parameters:
        state - the converter state.
        Returns:
        a input stream with the IDAT data.
      • mapPNGRenderIntent

        static COSName mapPNGRenderIntent​(int renderIntent)
        Map the renderIntent int to a PDF render intent. See also https://www.w3.org/TR/2003/REC-PNG-20031110/#11sRGB
        Parameters:
        renderIntent - the PNG render intent
        Returns:
        the matching PDF Render Intent or null
      • checkConverterState

        static boolean checkConverterState​(PNGConverter.PNGConverterState state)
        Check if the converter state is sane.
        Parameters:
        state - the parsed converter state
        Returns:
        true if the state seems plausible
      • checkChunkSane

        static boolean checkChunkSane​(PNGConverter.Chunk chunk)
        Check if the chunk is sane, i.e. CRC matches and offsets and lengths in the byte array
      • readInt

        private static int readInt​(byte[] data,
                                   int offset)
      • readPNGFloat

        private static float readPNGFloat​(byte[] bytes,
                                          int offset)
      • parsePNGChunks

        private static PNGConverter.PNGConverterState parsePNGChunks​(byte[] imageData)
        Parse the PNG structure into the PNGConverterState. If we can't handle something, this method will return null.
        Parameters:
        imageData - the byte array with the PNG data
        Returns:
        null or the converter state with all relevant chunks
      • makeCrcTable

        private static void makeCrcTable()
      • updateCrc

        private static int updateCrc​(byte[] buf,
                                     int offset,
                                     int len)
      • crc

        static int crc​(byte[] buf,
                       int offset,
                       int len)