Class PNGConverter
- java.lang.Object
-
- org.apache.pdfbox.pdmodel.graphics.image.PNGConverter
-
final class PNGConverter extends java.lang.ObjectThis 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 Classes Modifier and Type Class Description (package private) static classPNGConverter.ChunkHolds the information about a chunksprivate static classPNGConverter.MultipleInputStream(package private) static classPNGConverter.PNGConverterStateHolds all relevant chunks of the PNG
-
Field Summary
Fields Modifier and Type Field Description private static intCHUNK_CHRMprivate static intCHUNK_GAMAprivate static intCHUNK_HISTprivate static intCHUNK_ICCPprivate static intCHUNK_IDATprivate static intCHUNK_IENDprivate static intCHUNK_IHDRprivate static intCHUNK_ITXTprivate static intCHUNK_KBKGprivate static intCHUNK_PHYSprivate static intCHUNK_PLTEprivate static intCHUNK_SBITprivate static intCHUNK_SPLTprivate static intCHUNK_SRGBprivate static intCHUNK_TEXTprivate static intCHUNK_TIMEprivate static intCHUNK_TRNSprivate static intCHUNK_ZTXTprivate static int[]CRC_TABLEprivate static org.apache.commons.logging.LogLOG
-
Constructor Summary
Constructors Modifier Constructor Description privatePNGConverter()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static COSDictionarybuildDecodeParams(PNGConverter.PNGConverterState state, PDColorSpace colorSpace)private static PDImageXObjectbuildImageObject(PDDocument document, PNGConverter.PNGConverterState state)Build the base image object from the IDATs and profile informationprivate static PDImageXObjectbuildIndexImage(PDDocument doc, PNGConverter.PNGConverterState state)Build a indexed imageprivate static PDImageXObjectbuildTransparencyMaskFromIndexedData(PDDocument doc, PDImageXObject image, PNGConverter.PNGConverterState state)(package private) static booleancheckChunkSane(PNGConverter.Chunk chunk)Check if the chunk is sane, i.e.(package private) static booleancheckConverterState(PNGConverter.PNGConverterState state)Check if the converter state is sane.private static PDImageXObjectconvertPng(PDDocument doc, PNGConverter.PNGConverterState state)Convert the image using the state.(package private) static PDImageXObjectconvertPNGImage(PDDocument doc, byte[] imageData)Try to convert a PNG into a PDImageXObject.(package private) static intcrc(byte[] buf, int offset, int len)private static COSStreamcreateCOSStreamwithIccProfile(PDDocument document, PDColorSpace colorSpace, PNGConverter.PNGConverterState state)private static java.io.InputStreamgetIDATInputStream(PNGConverter.PNGConverterState state)Build an input stream for the IDAT data.private static voidmakeCrcTable()(package private) static COSNamemapPNGRenderIntent(int renderIntent)Map the renderIntent int to a PDF render intent.private static PNGConverter.PNGConverterStateparsePNGChunks(byte[] imageData)Parse the PNG structure into the PNGConverterState.private static intreadInt(byte[] data, int offset)private static floatreadPNGFloat(byte[] bytes, int offset)private static voidsetupIndexedColorSpace(PDDocument doc, PNGConverter.Chunk lookupTable, PDImageXObject image, int highVal)private static intupdateCrc(byte[] buf, int offset, int len)
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
CHUNK_IHDR
private static final int CHUNK_IHDR
- See Also:
- Constant Field Values
-
CHUNK_IDAT
private static final int CHUNK_IDAT
- See Also:
- Constant Field Values
-
CHUNK_PLTE
private static final int CHUNK_PLTE
- See Also:
- Constant Field Values
-
CHUNK_IEND
private static final int CHUNK_IEND
- See Also:
- Constant Field Values
-
CHUNK_TRNS
private static final int CHUNK_TRNS
- See Also:
- Constant Field Values
-
CHUNK_CHRM
private static final int CHUNK_CHRM
- See Also:
- Constant Field Values
-
CHUNK_GAMA
private static final int CHUNK_GAMA
- See Also:
- Constant Field Values
-
CHUNK_ICCP
private static final int CHUNK_ICCP
- See Also:
- Constant Field Values
-
CHUNK_SBIT
private static final int CHUNK_SBIT
- See Also:
- Constant Field Values
-
CHUNK_SRGB
private static final int CHUNK_SRGB
- See Also:
- Constant Field Values
-
CHUNK_TEXT
private static final int CHUNK_TEXT
- See Also:
- Constant Field Values
-
CHUNK_ZTXT
private static final int CHUNK_ZTXT
- See Also:
- Constant Field Values
-
CHUNK_ITXT
private static final int CHUNK_ITXT
- See Also:
- Constant Field Values
-
CHUNK_KBKG
private static final int CHUNK_KBKG
- See Also:
- Constant Field Values
-
CHUNK_HIST
private static final int CHUNK_HIST
- See Also:
- Constant Field Values
-
CHUNK_PHYS
private static final int CHUNK_PHYS
- See Also:
- Constant Field Values
-
CHUNK_SPLT
private static final int CHUNK_SPLT
- See Also:
- Constant Field Values
-
CHUNK_TIME
private static final int CHUNK_TIME
- See Also:
- Constant Field Values
-
CRC_TABLE
private static final int[] CRC_TABLE
-
-
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 inimageData- 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 instate- the parser state containing the PNG chunks.- Returns:
- null or the converted image
- Throws:
java.io.IOException
-
buildIndexImage
private static PDImageXObject buildIndexImage(PDDocument doc, PNGConverter.PNGConverterState state) throws java.io.IOException
Build a indexed image- Throws:
java.io.IOException
-
buildTransparencyMaskFromIndexedData
private static PDImageXObject buildTransparencyMaskFromIndexedData(PDDocument doc, PDImageXObject image, PNGConverter.PNGConverterState state) throws java.io.IOException
- 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
-
buildImageObject
private static PDImageXObject buildImageObject(PDDocument document, PNGConverter.PNGConverterState state) throws java.io.IOException
Build the base image object from the IDATs and profile information- Throws:
java.io.IOException
-
createCOSStreamwithIccProfile
private static COSStream createCOSStreamwithIccProfile(PDDocument document, PDColorSpace colorSpace, PNGConverter.PNGConverterState state) throws java.io.IOException
- Throws:
java.io.IOException
-
buildDecodeParams
private static COSDictionary buildDecodeParams(PNGConverter.PNGConverterState state, PDColorSpace colorSpace)
-
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)
-
-