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

public final Class LosslessFactory

extends Object
Class Inheritance
Imports
java.awt.Transparency, java.awt.color.ColorSpace, .ICC_ColorSpace, .ICC_Profile, java.awt.image.BufferedImage, .DataBuffer, .Raster, java.io.ByteArrayInputStream, .ByteArrayOutputStream, .IOException, .OutputStream, java.util.Arrays, java.util.zip.Deflater, .DeflaterOutputStream, javax.imageio.stream.MemoryCacheImageOutputStream, org.apache.pdfbox.cos.COSDictionary, .COSInteger, .COSName, org.apache.pdfbox.filter.Filter, .FilterFactory, org.apache.pdfbox.pdmodel.PDDocument, org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace, .PDDeviceCMYK, .PDDeviceColorSpace, .PDDeviceGray, .PDDeviceRGB, .PDICCBased

Factory for creating a PDImageXObject containing a lossless compressed image.
Author
Tilman Hausherr

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
pack-priv static final boolean
USE_PREDICTOR_ENCODER

Internal, only for benchmark purpose

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static PDImageXObject
public static PDImageXObject

Returns:

a new image XObject
createFromImage
(PDDocument
the document where the image will be created
document
,
BufferedImage
the BufferedImage to embed
image
)

Creates a new lossless encoded image XObject from a BufferedImage.

private static PDImageXObject
private static boolean
pack-priv static PDImageXObject

Returns:

the newly created PDImageXObject with the data compressed.
prepareImageXObject
(PDDocument
The document.
document
,
byte[]
array with data.
byteArray
,
int
the image width
width
,
int
the image height
height
,
int
the bits per component
bitsPerComponent
,
PDColorSpace
the color space
initColorSpace
)

Create a PDImageXObject using the Flate filter.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

USE_PREDICTOR_ENCODERback to summary
pack-priv static final boolean USE_PREDICTOR_ENCODER

Internal, only for benchmark purpose

Constructor Detail

LosslessFactoryback to summary
private LosslessFactory()

Method Detail

createFromGrayImageback to summary
private static PDImageXObject createFromGrayImage(BufferedImage image, PDDocument document) throws IOException
createFromImageback to summary
public static PDImageXObject createFromImage(PDDocument document, BufferedImage image) throws IOException

Creates a new lossless encoded image XObject from a BufferedImage.

New for advanced users from 2.0.12 on:
If you created your image with a non standard ICC colorspace, it will be preserved. (If you load images in java using ImageIO then no need to read this segment) However a new colorspace will be created for each image. So if you create a PDF with several such images, consider replacing the colorspace with a common object to save space. This is done with PDImageXObject#getColorSpace() and PDImageXObject.setColorSpace()

Parameters
document:PDDocument

the document where the image will be created

image:BufferedImage

the BufferedImage to embed

Returns:PDImageXObject

a new image XObject

Exceptions
IOException:
if something goes wrong
createFromRGBImageback to summary
private static PDImageXObject createFromRGBImage(BufferedImage image, PDDocument document) throws IOException
isGrayImageback to summary
private static boolean isGrayImage(BufferedImage image)
prepareImageXObjectback to summary
pack-priv static PDImageXObject prepareImageXObject(PDDocument document, byte[] byteArray, int width, int height, int bitsPerComponent, PDColorSpace initColorSpace) throws IOException

Create a PDImageXObject using the Flate filter.

Parameters
document:PDDocument

The document.

byteArray:byte[]

array with data.

width:int

the image width

height:int

the image height

bitsPerComponent:int

the bits per component

initColorSpace:PDColorSpace

the color space

Returns:PDImageXObject

the newly created PDImageXObject with the data compressed.

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

private Class LosslessFactory.PredictorEncoder

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory.PredictorEncoder

Field Summary

Modifier and TypeField and Description
pack-priv final byte[]
pack-priv final byte[]
pack-priv final byte[]
private final int
private final int
private final int
pack-priv final byte[]
private final byte[]
private final byte[]
private final byte[]
private final byte[]
private final byte[]
private final PDDocument
pack-priv final boolean
private final int
private final BufferedImage
pack-priv final int
pack-priv final byte[]
private final int
private final int
pack-priv final byte[]

Constructor Summary

AccessConstructor and Description
pack-priv
PredictorEncoder(PDDocument document, BufferedImage image)

Initialize the encoder and set all final fields

Method Summary

Modifier and TypeMethod and Description
private byte[]

Returns:

the "best" row encoding of the row encodings
chooseDataRowToWrite
()

We look which row encoding is the "best" one, ie.

private void
copyImageBytes(byte[] transferRow, int indexInTranferRow, byte[] targetValues, byte[] alphaImageData, int alphaPtr)

private void
copyIntToBytes(int[] transferRow, int indexInTranferRow, byte[] targetValues, byte[] alphaImageData, int alphaPtr)

private static void
copyShortsToBytes(short[] transferRow, int indexInTranferRow, byte[] targetValues, byte[] alphaImageData, int alphaPtr)

pack-priv PDImageXObject

Returns:

the image or null if it is not possible to encoded the image (e.g. not supported raster format etc.)
encode
()

Tries to compress the image using a predictor.

private static long
estCompressSum(byte[] dataRawRowSub)

private static byte
pngFilterAverage(int x, int a, int b)

private static byte
pngFilterPaeth(int x, int a, int b, int c)

private static byte
pngFilterSub(int x, int a)

private static byte
pngFilterUp(int x, int b)

private PDImageXObject
preparePredictorPDImage(ByteArrayOutputStream stream, int bitsPerComponent)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

alphaImageDataback to summary
pack-priv final byte[] alphaImageData
aValuesback to summary
pack-priv final byte[] aValues
bValuesback to summary
pack-priv final byte[] bValues
bytesPerComponentback to summary
private final int bytesPerComponent
bytesPerPixelback to summary
private final int bytesPerPixel
componentsPerPixelback to summary
private final int componentsPerPixel
cValuesback to summary
pack-priv final byte[] cValues
dataRawRowAverageback to summary
private final byte[] dataRawRowAverage
dataRawRowNoneback to summary
private final byte[] dataRawRowNone
dataRawRowPaethback to summary
private final byte[] dataRawRowPaeth
dataRawRowSubback to summary
private final byte[] dataRawRowSub
dataRawRowUpback to summary
private final byte[] dataRawRowUp
documentback to summary
private final PDDocument document
hasAlphaback to summary
pack-priv final boolean hasAlpha
heightback to summary
private final int height
imageback to summary
private final BufferedImage image
imageTypeback to summary
pack-priv final int imageType
tmpResultValuesback to summary
pack-priv final byte[] tmpResultValues
transferTypeback to summary
private final int transferType
widthback to summary
private final int width
xValuesback to summary
pack-priv final byte[] xValues

Constructor Detail

PredictorEncoderback to summary
pack-priv PredictorEncoder(PDDocument document, BufferedImage image)

Initialize the encoder and set all final fields

Method Detail

chooseDataRowToWriteback to summary
private byte[] chooseDataRowToWrite()

We look which row encoding is the "best" one, ie. has the lowest sum. We don't implement anything fancier to choose the right row encoding. This is just the recommend algorithm in the spec. The get the perfect encoding you would need to do a brute force check how all the different encoded rows compress in the zip stream together. You have would have to check 5*image-height permutations...

Returns:byte[]

the "best" row encoding of the row encodings

copyImageBytesback to summary
private void copyImageBytes(byte[] transferRow, int indexInTranferRow, byte[] targetValues, byte[] alphaImageData, int alphaPtr)
copyIntToBytesback to summary
private void copyIntToBytes(int[] transferRow, int indexInTranferRow, byte[] targetValues, byte[] alphaImageData, int alphaPtr)
copyShortsToBytesback to summary
private static void copyShortsToBytes(short[] transferRow, int indexInTranferRow, byte[] targetValues, byte[] alphaImageData, int alphaPtr)
encodeback to summary
pack-priv PDImageXObject encode() throws IOException

Tries to compress the image using a predictor.

Returns:PDImageXObject

the image or null if it is not possible to encoded the image (e.g. not supported raster format etc.)

estCompressSumback to summary
private static long estCompressSum(byte[] dataRawRowSub)
pngFilterAverageback to summary
private static byte pngFilterAverage(int x, int a, int b)
pngFilterPaethback to summary
private static byte pngFilterPaeth(int x, int a, int b, int c)
pngFilterSubback to summary
private static byte pngFilterSub(int x, int a)
pngFilterUpback to summary
private static byte pngFilterUp(int x, int b)
preparePredictorPDImageback to summary
private PDImageXObject preparePredictorPDImage(ByteArrayOutputStream stream, int bitsPerComponent) throws IOException