Class PNGConverter
- java.lang.Object
-
- org.apache.pdfbox.pdmodel.graphics.image.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
PNGConverter.Chunk
Holds the information about a chunksprivate static class
PNGConverter.MultipleInputStream
(package private) static class
PNGConverter.PNGConverterState
Holds all relevant chunks of the PNG
-
Field Summary
Fields Modifier and Type Field Description private static int
CHUNK_CHRM
private static int
CHUNK_GAMA
private static int
CHUNK_HIST
private static int
CHUNK_ICCP
private static int
CHUNK_IDAT
private static int
CHUNK_IEND
private static int
CHUNK_IHDR
private static int
CHUNK_ITXT
private static int
CHUNK_KBKG
private static int
CHUNK_PHYS
private static int
CHUNK_PLTE
private static int
CHUNK_SBIT
private static int
CHUNK_SPLT
private static int
CHUNK_SRGB
private static int
CHUNK_TEXT
private static int
CHUNK_TIME
private static int
CHUNK_TRNS
private static int
CHUNK_ZTXT
private static int[]
CRC_TABLE
private static org.apache.commons.logging.Log
LOG
-
Constructor Summary
Constructors Modifier Constructor Description private
PNGConverter()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private 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
checkConverterState(PNGConverter.PNGConverterState state)
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 java.io.InputStream
getIDATInputStream(PNGConverter.PNGConverterState state)
Build an input stream for the IDAT data.private static void
makeCrcTable()
(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 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)
-
-