Top Description Inners Fields Constructors Methods
org.apache.pdfbox.pdmodel.graphics.image

pack-priv final Class PNGConverter

extends Object
Class Inheritance
Imports
java.awt.color.ColorSpace, .ICC_Profile, java.io.ByteArrayInputStream, .ByteArrayOutputStream, .IOException, .InputStream, .OutputStream, java.util.ArrayList, .Arrays, .List, javax.imageio.stream.ImageInputStream, .MemoryCacheImageInputStream, org.apache.commons.logging.Log, .LogFactory, org.apache.pdfbox.cos.COSArray, .COSDictionary, .COSInteger, .COSName, .COSStream, org.apache.pdfbox.filter.Filter, .FilterFactory, org.apache.pdfbox.pdmodel.PDDocument, org.apache.pdfbox.pdmodel.common.PDStream, org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace, .PDDeviceGray, .PDDeviceRGB, .PDICCBased, .PDIndexed

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

Author
Emmeran Seehuber

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
PNGConverter.Chunk

Holds the information about a chunks

private static class
pack-priv static class
PNGConverter.PNGConverterState

Holds all relevant chunks of the PNG

Field Summary

Modifier and TypeField and Description
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 int[]
private static final Log

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static COSDictionary
private static PDImageXObject
buildImageObject(PDDocument document, PNGConverter.PNGConverterState state)

Build the base image object from the IDATs and profile information

private static PDImageXObject
buildIndexImage(PDDocument doc, PNGConverter.PNGConverterState state)

Build a indexed image

private static PDImageXObject
pack-priv static boolean
checkChunkSane(PNGConverter.Chunk chunk)

Check if the chunk is sane, i.e. CRC matches and offsets and lengths in the byte array

pack-priv static boolean

Returns:

true if the state seems plausible
checkConverterState
(PNGConverter.PNGConverterState
the parsed converter state
state
)

Check if the converter state is sane.

private static PDImageXObject

Returns:

null or the converted image
convertPng
(PDDocument
the document to put the image in
doc
,
PNGConverter.PNGConverterState
the parser state containing the PNG chunks.
state
)

Convert the image using the state.

pack-priv static PDImageXObject

Returns:

null or the PDImageXObject built from the png
convertPNGImage
(PDDocument
the document to put the image in
doc
,
byte[]
the byte data of the PNG
imageData
)

Try to convert a PNG into a PDImageXObject.

pack-priv static int
crc(byte[] buf, int offset, int len)

private static COSStream
private static InputStream

Returns:

a input stream with the IDAT data.
getIDATInputStream
(PNGConverter.PNGConverterState
the converter state.
state
)

Build an input stream for the IDAT data.

private static void
pack-priv static COSName

Returns:

the matching PDF Render Intent or null
mapPNGRenderIntent
(int
the PNG render intent
renderIntent
)

Map the renderIntent int to a PDF render intent.

private static PNGConverter.PNGConverterState

Returns:

null or the converter state with all relevant chunks
parsePNGChunks
(byte[]
the byte array with the PNG data
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)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CHUNK_CHRMback to summary
private static final int CHUNK_CHRM
CHUNK_GAMAback to summary
private static final int CHUNK_GAMA
CHUNK_HISTback to summary
private static final int CHUNK_HIST
CHUNK_ICCPback to summary
private static final int CHUNK_ICCP
CHUNK_IDATback to summary
private static final int CHUNK_IDAT
CHUNK_IENDback to summary
private static final int CHUNK_IEND
CHUNK_IHDRback to summary
private static final int CHUNK_IHDR
CHUNK_ITXTback to summary
private static final int CHUNK_ITXT
CHUNK_KBKGback to summary
private static final int CHUNK_KBKG
CHUNK_PHYSback to summary
private static final int CHUNK_PHYS
CHUNK_PLTEback to summary
private static final int CHUNK_PLTE
CHUNK_SBITback to summary
private static final int CHUNK_SBIT
CHUNK_SPLTback to summary
private static final int CHUNK_SPLT
CHUNK_SRGBback to summary
private static final int CHUNK_SRGB
CHUNK_TEXTback to summary
private static final int CHUNK_TEXT
CHUNK_TIMEback to summary
private static final int CHUNK_TIME
CHUNK_TRNSback to summary
private static final int CHUNK_TRNS
CHUNK_ZTXTback to summary
private static final int CHUNK_ZTXT
CRC_TABLEback to summary
private static final int[] CRC_TABLE
LOGback to summary
private static final Log LOG

Constructor Detail

PNGConverterback to summary
private PNGConverter()

Method Detail

buildDecodeParamsback to summary
private static COSDictionary buildDecodeParams(PNGConverter.PNGConverterState state, PDColorSpace colorSpace)
buildImageObjectback to summary
private static PDImageXObject buildImageObject(PDDocument document, PNGConverter.PNGConverterState state) throws IOException

Build the base image object from the IDATs and profile information

buildIndexImageback to summary
private static PDImageXObject buildIndexImage(PDDocument doc, PNGConverter.PNGConverterState state) throws IOException

Build a indexed image

buildTransparencyMaskFromIndexedDataback to summary
private static PDImageXObject buildTransparencyMaskFromIndexedData(PDDocument doc, PDImageXObject image, PNGConverter.PNGConverterState state) throws IOException
checkChunkSaneback to summary
pack-priv static boolean checkChunkSane(PNGConverter.Chunk chunk)

Check if the chunk is sane, i.e. CRC matches and offsets and lengths in the byte array

checkConverterStateback to summary
pack-priv static boolean checkConverterState(PNGConverter.PNGConverterState state)

Check if the converter state is sane.

Parameters
state:PNGConverter.PNGConverterState

the parsed converter state

Returns:boolean

true if the state seems plausible

convertPngback to summary
private static PDImageXObject convertPng(PDDocument doc, PNGConverter.PNGConverterState state) throws IOException

Convert the image using the state.

Parameters
doc:PDDocument

the document to put the image in

state:PNGConverter.PNGConverterState

the parser state containing the PNG chunks.

Returns:PDImageXObject

null or the converted image

convertPNGImageback to summary
pack-priv static PDImageXObject convertPNGImage(PDDocument doc, byte[] imageData) throws 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:PDDocument

the document to put the image in

imageData:byte[]

the byte data of the PNG

Returns:PDImageXObject

null or the PDImageXObject built from the png

crcback to summary
pack-priv static int crc(byte[] buf, int offset, int len)
createCOSStreamwithIccProfileback to summary
private static COSStream createCOSStreamwithIccProfile(PDDocument document, PDColorSpace colorSpace, PNGConverter.PNGConverterState state) throws IOException
getIDATInputStreamback to summary
private static InputStream getIDATInputStream(PNGConverter.PNGConverterState state)

Build an input stream for the IDAT data. May need to concat multiple IDAT chunks.

Parameters
state:PNGConverter.PNGConverterState

the converter state.

Returns:InputStream

a input stream with the IDAT data.

makeCrcTableback to summary
private static void makeCrcTable()
mapPNGRenderIntentback to summary
pack-priv 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:int

the PNG render intent

Returns:COSName

the matching PDF Render Intent or null

parsePNGChunksback to summary
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:byte[]

the byte array with the PNG data

Returns:PNGConverter.PNGConverterState

null or the converter state with all relevant chunks

readIntback to summary
private static int readInt(byte[] data, int offset)
readPNGFloatback to summary
private static float readPNGFloat(byte[] bytes, int offset)
setupIndexedColorSpaceback to summary
private static void setupIndexedColorSpace(PDDocument doc, PNGConverter.Chunk lookupTable, PDImageXObject image, int highVal) throws IOException
updateCrcback to summary
private static int updateCrc(byte[] buf, int offset, int len)
org.apache.pdfbox.pdmodel.graphics.image back to summary

pack-priv final Class PNGConverter.Chunk

extends Object
Class Inheritance

Holds the information about a chunks

Field Summary

Modifier and TypeField and Description
pack-priv byte[]
bytes

This field holds the whole byte array; In that it's redundant, as all chunks will have the same byte array.

pack-priv int
chunkType

The chunk type, see the CHUNK_???

pack-priv int
crc

The crc of the chunk data, as stored in the PNG stream.

pack-priv int
length

The length of the data within the byte array.

pack-priv int
start

The start index of the chunk data within bytes.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv byte[]

Returns:

a byte-array with only the data of the chunk
getData
()

Get the data of this chunk as a byte array

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bytesback to summary
pack-priv byte[] bytes

This field holds the whole byte array; In that it's redundant, as all chunks will have the same byte array. But have this byte array per chunk makes it easier to validate and pass around. And we won't have that many chunks, so those 8 bytes for the pointer (on 64-bit systems) don't matter.

chunkTypeback to summary
pack-priv int chunkType

The chunk type, see the CHUNK_??? constants.

crcback to summary
pack-priv int crc

The crc of the chunk data, as stored in the PNG stream.

lengthback to summary
pack-priv int length

The length of the data within the byte array.

startback to summary
pack-priv int start

The start index of the chunk data within bytes.

Constructor Detail

Chunkback to summary
pack-priv Chunk()

Method Detail

getDataback to summary
pack-priv byte[] getData()

Get the data of this chunk as a byte array

Returns:byte[]

a byte-array with only the data of the chunk

org.apache.pdfbox.pdmodel.graphics.image back to summary

private Class PNGConverter.MultipleInputStream

extends InputStream
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv InputStream
pack-priv int
pack-priv final List<InputStream>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int
available()

Overrides java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.

private boolean
public int
read()

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the input stream.

public int
read(byte[]
the buffer into which the data is read.
b
,
int
the start offset in array b at which the data is written.
off
,
int
the maximum number of bytes to read.
len
)

Overrides java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes.

Inherited from java.io.InputStream:
closemarkmarkSupportednullInputStreamreadreadAllBytesreadNBytesreadNBytesresetskipskipNBytestransferTo

Field Detail

currentStreamback to summary
pack-priv InputStream currentStream
currentStreamIdxback to summary
pack-priv int currentStreamIdx
inputStreamsback to summary
pack-priv final List<InputStream> inputStreams

Constructor Detail

MultipleInputStreamback to summary
private MultipleInputStream()

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.InputStream.available.

Doc from java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected. The read might be on the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

A subclass's implementation of this method may choose to throw an IOException if this input stream has been closed by invoking the close() method.

Returns:int

an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
ensureStreamback to summary
private boolean ensureStream()
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Returns:int

the next byte of data, or -1 if the end of the stream is reached.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

In every case, elements b[0] through b[off-1] and elements b[off+len] through b[b.length-1] are unaffected.

Parameters
b:byte[]

the buffer into which the data is read.

off:int

the start offset in array b at which the data is written.

len:int

the maximum number of bytes to read.

Returns:int

the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Annotations
@Override
Exceptions
IOException:
If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.
org.apache.pdfbox.pdmodel.graphics.image back to summary

pack-priv final Class PNGConverter.PNGConverterState

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.pdfbox.pdmodel.graphics.image.PNGConverter.PNGConverterState

Holds all relevant chunks of the PNG

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv PNGConverter.Chunk
pack-priv PNGConverter.Chunk
pack-priv int
pack-priv PNGConverter.Chunk
pack-priv List<PNGConverter.Chunk>
pack-priv PNGConverter.Chunk
pack-priv PNGConverter.Chunk
pack-priv PNGConverter.Chunk
pack-priv PNGConverter.Chunk
pack-priv int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bitsPerComponentback to summary
pack-priv int bitsPerComponent
cHRMback to summary
pack-priv PNGConverter.Chunk cHRM
gAMAback to summary
pack-priv PNGConverter.Chunk gAMA
heightback to summary
pack-priv int height
iCCPback to summary
pack-priv PNGConverter.Chunk iCCP
IDATsback to summary
pack-priv List<PNGConverter.Chunk> IDATs
IHDRback to summary
pack-priv PNGConverter.Chunk IHDR
Annotations
@SuppressWarnings:SpellCheckingInspection
PLTEback to summary
pack-priv PNGConverter.Chunk PLTE
Annotations
@SuppressWarnings:SpellCheckingInspection
sRGBback to summary
pack-priv PNGConverter.Chunk sRGB
tRNSback to summary
pack-priv PNGConverter.Chunk tRNS
widthback to summary
pack-priv int width

Constructor Detail

PNGConverterStateback to summary
pack-priv PNGConverterState()