Class PNGConverter
java.lang.Object
org.apache.pdfbox.pdmodel.graphics.image.PNGConverter
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
Holds the information about a chunksprivate static class
(package private) static final class
Holds all relevant chunks of the PNG -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int[]
private static final org.apache.commons.logging.Log
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static COSDictionary
buildDecodeParams
(PNGConverter.PNGConverterState state, PDColorSpace colorSpace) private static PDImageXObject
buildImageObject
(PDDocument document, PNGConverter.PNGConverterState state) Build the base image object from the IDATs and profile informationprivate static PDImageXObject
buildIndexImage
(PDDocument doc, PNGConverter.PNGConverterState state) Build a indexed imageprivate static PDImageXObject
buildTransparencyMaskFromIndexedData
(PDDocument doc, PDImageXObject image, PNGConverter.PNGConverterState state) (package private) static boolean
checkChunkSane
(PNGConverter.Chunk chunk) Check if the chunk is sane, i.e.(package private) static boolean
Check if the converter state is sane.private static PDImageXObject
convertPng
(PDDocument doc, PNGConverter.PNGConverterState state) Convert the image using the state.(package private) static PDImageXObject
convertPNGImage
(PDDocument doc, byte[] imageData) Try to convert a PNG into a PDImageXObject.(package private) static int
crc
(byte[] buf, int offset, int len) private static COSStream
createCOSStreamwithIccProfile
(PDDocument document, PDColorSpace colorSpace, PNGConverter.PNGConverterState state) private static InputStream
Build an input stream for the IDAT data.private static void
(package private) static COSName
mapPNGRenderIntent
(int renderIntent) Map the renderIntent int to a PDF render intent.private static PNGConverter.PNGConverterState
parsePNGChunks
(byte[] imageData) Parse the PNG structure into the PNGConverterState.private static int
readInt
(byte[] data, int offset) private static float
readPNGFloat
(byte[] bytes, int offset) private static void
setupIndexedColorSpace
(PDDocument doc, PNGConverter.Chunk lookupTable, PDImageXObject image, int highVal) private static int
updateCrc
(byte[] buf, int offset, int len)
-
Field Details
-
LOG
private static final org.apache.commons.logging.Log LOG -
CHUNK_IHDR
private static final int CHUNK_IHDR- See Also:
-
CHUNK_IDAT
private static final int CHUNK_IDAT- See Also:
-
CHUNK_PLTE
private static final int CHUNK_PLTE- See Also:
-
CHUNK_IEND
private static final int CHUNK_IEND- See Also:
-
CHUNK_TRNS
private static final int CHUNK_TRNS- See Also:
-
CHUNK_CHRM
private static final int CHUNK_CHRM- See Also:
-
CHUNK_GAMA
private static final int CHUNK_GAMA- See Also:
-
CHUNK_ICCP
private static final int CHUNK_ICCP- See Also:
-
CHUNK_SBIT
private static final int CHUNK_SBIT- See Also:
-
CHUNK_SRGB
private static final int CHUNK_SRGB- See Also:
-
CHUNK_TEXT
private static final int CHUNK_TEXT- See Also:
-
CHUNK_ZTXT
private static final int CHUNK_ZTXT- See Also:
-
CHUNK_ITXT
private static final int CHUNK_ITXT- See Also:
-
CHUNK_KBKG
private static final int CHUNK_KBKG- See Also:
-
CHUNK_HIST
private static final int CHUNK_HIST- See Also:
-
CHUNK_PHYS
private static final int CHUNK_PHYS- See Also:
-
CHUNK_SPLT
private static final int CHUNK_SPLT- See Also:
-
CHUNK_TIME
private static final int CHUNK_TIME- See Also:
-
CRC_TABLE
private static final int[] CRC_TABLE
-
-
Constructor Details
-
PNGConverter
private PNGConverter()
-
-
Method Details
-
convertPNGImage
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 inimageData
- the byte data of the PNG- Returns:
- null or the PDImageXObject built from the png
- Throws:
IOException
-
convertPng
private static PDImageXObject convertPng(PDDocument doc, PNGConverter.PNGConverterState state) throws IOException Convert the image using the state.- Parameters:
doc
- the document to put the image instate
- the parser state containing the PNG chunks.- Returns:
- null or the converted image
- Throws:
IOException
-
buildIndexImage
private static PDImageXObject buildIndexImage(PDDocument doc, PNGConverter.PNGConverterState state) throws IOException Build a indexed image- Throws:
IOException
-
buildTransparencyMaskFromIndexedData
private static PDImageXObject buildTransparencyMaskFromIndexedData(PDDocument doc, PDImageXObject image, PNGConverter.PNGConverterState state) throws IOException - Throws:
IOException
-
setupIndexedColorSpace
private static void setupIndexedColorSpace(PDDocument doc, PNGConverter.Chunk lookupTable, PDImageXObject image, int highVal) throws IOException - Throws:
IOException
-
buildImageObject
private static PDImageXObject buildImageObject(PDDocument document, PNGConverter.PNGConverterState state) throws IOException Build the base image object from the IDATs and profile information- Throws:
IOException
-
createCOSStreamwithIccProfile
private static COSStream createCOSStreamwithIccProfile(PDDocument document, PDColorSpace colorSpace, PNGConverter.PNGConverterState state) throws IOException - Throws:
IOException
-
buildDecodeParams
private static COSDictionary buildDecodeParams(PNGConverter.PNGConverterState state, PDColorSpace colorSpace) -
getIDATInputStream
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
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
Check if the converter state is sane.- Parameters:
state
- the parsed converter state- Returns:
- true if the state seems plausible
-
checkChunkSane
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
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)
-