public final class PDImageXObject extends PDXObject implements PDImage
Modifier and Type | Field and Description |
---|---|
private java.lang.ref.SoftReference<java.awt.image.BufferedImage> |
cachedImage |
private int |
cachedImageSubsampling |
private PDColorSpace |
colorSpace |
private static org.apache.commons.logging.Log |
LOG
Log instance.
|
private PDResources |
resources
current resource dictionary (has color spaces)
|
Constructor and Description |
---|
PDImageXObject(PDDocument document)
Creates an Image XObject in the given document.
|
PDImageXObject(PDDocument document,
java.io.InputStream encodedStream,
COSBase cosFilter,
int width,
int height,
int bitsPerComponent,
PDColorSpace initColorSpace)
Creates an Image XObject in the given document using the given filtered stream.
|
PDImageXObject(PDStream stream,
PDResources resources)
Creates an Image XObject with the given stream as its contents and current color spaces.
|
Modifier and Type | Method and Description |
---|---|
private java.awt.image.BufferedImage |
applyMask(java.awt.image.BufferedImage image,
java.awt.image.BufferedImage mask,
boolean isSoft,
float[] matte) |
private float |
clampColor(float color) |
static PDImageXObject |
createFromByteArray(PDDocument document,
byte[] byteArray,
java.lang.String name)
Create a PDImageXObject from bytes of an image file.
|
static PDImageXObject |
createFromFile(java.lang.String imagePath,
PDDocument doc)
Create a PDImageXObject from an image file, see
createFromFileByExtension(File, PDDocument) for
more details. |
static PDImageXObject |
createFromFileByContent(java.io.File file,
PDDocument doc)
Create a PDImageXObject from an image file.
|
static PDImageXObject |
createFromFileByExtension(java.io.File file,
PDDocument doc)
Create a PDImageXObject from an image file.
|
java.io.InputStream |
createInputStream()
Returns an InputStream containing the image data, irrespective of whether this is an
inline image or an image XObject.
|
java.io.InputStream |
createInputStream(DecodeOptions options)
Returns an InputStream, passing additional options to each filter.
|
java.io.InputStream |
createInputStream(java.util.List<java.lang.String> stopFilters)
Returns an InputStream containing the image data, irrespective of whether this is an
inline image or an image XObject.
|
private static COSStream |
createRawStream(PDDocument document,
java.io.InputStream rawInput)
Creates a COS stream from raw (encoded) data.
|
static PDImageXObject |
createThumbnail(COSStream cosStream)
Creates a thumbnail Image XObject from the given COSBase and name.
|
private float[] |
extractMatte(PDImageXObject softMask)
Extract the matte color from a softmask.
|
int |
getBitsPerComponent()
Returns bits per component of this image, or -1 if one has not been set.
|
COSArray |
getColorKeyMask()
Returns the color key mask array associated with this image, or null if there is none.
|
PDColorSpace |
getColorSpace()
Returns the image's color space.
|
COSArray |
getDecode()
Returns the decode array.
|
int |
getHeight()
Returns height of this image, or -1 if one has not been set.
|
java.awt.image.BufferedImage |
getImage()
Returns the content of this image as an AWT buffered image with an (A)RGB color space.
|
java.awt.image.BufferedImage |
getImage(java.awt.Rectangle region,
int subsampling)
Returns the content of this image as an AWT buffered image with an (A)RGB colored space.
|
boolean |
getInterpolate()
Returns true if the image should be interpolated when rendered.
|
PDImageXObject |
getMask()
Returns the Mask Image XObject associated with this image, or null if there is none.
|
PDMetadata |
getMetadata()
Returns the metadata associated with this XObject, or null if there is none.
|
java.awt.image.BufferedImage |
getOpaqueImage()
Returns an RGB buffered image containing the opaque image stream without any masks applied.
|
PDImageXObject |
getSoftMask()
Returns the Soft Mask Image XObject associated with this image, or null if there is none.
|
java.awt.image.BufferedImage |
getStencilImage(java.awt.Paint paint)
Returns an ARGB image filled with the given paint and using this image as a mask.
|
int |
getStructParent()
Returns the key of this XObject in the structural parent tree.
|
java.lang.String |
getSuffix()
This will get the suffix for this image type, e.g.
|
int |
getWidth()
Returns the width of this image, or -1 if one has not been set.
|
boolean |
isEmpty()
Returns true if the image has no data.
|
boolean |
isStencil()
Returns true if the image is a stencil mask.
|
private java.awt.image.BufferedImage |
scaleImage(java.awt.image.BufferedImage image,
int width,
int height)
High-quality image scaling.
|
void |
setBitsPerComponent(int bpc)
Set the number of bits per component.
|
void |
setColorSpace(PDColorSpace cs)
Sets the color space for this image.
|
void |
setDecode(COSArray decode)
Sets the decode array.
|
void |
setHeight(int h)
Sets the height of the image.
|
void |
setInterpolate(boolean value)
Sets the Interpolate flag, true for high-quality image scaling.
|
void |
setMetadata(PDMetadata meta)
Sets the metadata associated with this XObject, or null if there is none.
|
void |
setStencil(boolean isStencil)
Sets whether or not the image is a stencil.
|
void |
setStructParent(int key)
Sets the key of this XObject in the structural parent tree.
|
void |
setWidth(int w)
Sets the width of the image.
|
createXObject, getCOSObject, getCOSStream, getPDStream, getStream
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getCOSObject
private static final org.apache.commons.logging.Log LOG
private java.lang.ref.SoftReference<java.awt.image.BufferedImage> cachedImage
private PDColorSpace colorSpace
private int cachedImageSubsampling
private final PDResources resources
public PDImageXObject(PDDocument document) throws java.io.IOException
createFromFileByExtension(File, PDDocument)
.document
- the current documentjava.io.IOException
- if there is an error creating the XObject.public PDImageXObject(PDDocument document, java.io.InputStream encodedStream, COSBase cosFilter, int width, int height, int bitsPerComponent, PDColorSpace initColorSpace) throws java.io.IOException
createFromFileByExtension(File, PDDocument)
.document
- the current documentencodedStream
- an encoded stream of image datacosFilter
- the filter or a COSArray of filterswidth
- the image widthheight
- the image heightbitsPerComponent
- the bits per componentinitColorSpace
- the color spacejava.io.IOException
- if there is an error creating the XObject.public PDImageXObject(PDStream stream, PDResources resources) throws java.io.IOException
createFromFileByExtension(File, PDDocument)
.stream
- the XObject stream to readresources
- the current resourcesjava.io.IOException
- if there is an error creating the XObject.public static PDImageXObject createThumbnail(COSStream cosStream) throws java.io.IOException
cosStream
- the COS streamjava.io.IOException
- if there is an error creating the XObject.private static COSStream createRawStream(PDDocument document, java.io.InputStream rawInput) throws java.io.IOException
java.io.IOException
public static PDImageXObject createFromFile(java.lang.String imagePath, PDDocument doc) throws java.io.IOException
createFromFileByExtension(File, PDDocument)
for
more details.imagePath
- the image file path.doc
- the document that shall use this PDImageXObject.java.io.IOException
- if there is an error when reading the file or creating the
PDImageXObject, or if the image type is not supported.public static PDImageXObject createFromFileByExtension(java.io.File file, PDDocument doc) throws java.io.IOException
JPEGFactory.createFromStream(org.apache.pdfbox.pdmodel.PDDocument, java.io.InputStream)
,
CCITTFactory.createFromFile(org.apache.pdfbox.pdmodel.PDDocument, java.io.File)
or ImageIO.read(java.io.File)
combined with
LosslessFactory.createFromImage(org.apache.pdfbox.pdmodel.PDDocument, java.awt.image.BufferedImage)
. (The later can also be used to create a
PDImageXObject from a BufferedImage). Starting with 2.0.18, this call will create an image
directly from a PNG file without decoding it (when possible), which is faster. However the
result size depends on the compression skill of the software that created the PNG file. If
file size or bandwidth are important to you or to your clients, then create your PNG files
with a tool that has implemented the
Zopfli
algorithm, or use the two-step process mentioned above.file
- the image file.doc
- the document that shall use this PDImageXObject.java.io.IOException
- if there is an error when reading the file or creating the
PDImageXObject.java.lang.IllegalArgumentException
- if the image type is not supported.public static PDImageXObject createFromFileByContent(java.io.File file, PDDocument doc) throws java.io.IOException
JPEGFactory.createFromStream(org.apache.pdfbox.pdmodel.PDDocument, java.io.InputStream)
,
CCITTFactory.createFromFile(org.apache.pdfbox.pdmodel.PDDocument, java.io.File)
or ImageIO.read(java.io.File)
combined with
LosslessFactory.createFromImage(org.apache.pdfbox.pdmodel.PDDocument, java.awt.image.BufferedImage)
. (The later can also be used to create a
PDImageXObject from a BufferedImage). Starting with 2.0.18, this call will create an image
directly from a png file without decoding it (when possible), which is faster. However the
result size depends on the compression skill of the software that created the PNG file. If
file size or bandwidth are important to you or to your clients, then create your PNG files
with a tool that has implemented the
Zopfli
algorithm, or use the two-step process mentioned above.file
- the image file.doc
- the document that shall use this PDImageXObject.java.io.IOException
- if there is an error when reading the file or creating the
PDImageXObject.java.lang.IllegalArgumentException
- if the image type is not supported.public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray, java.lang.String name) throws java.io.IOException
JPEGFactory.createFromByteArray(org.apache.pdfbox.pdmodel.PDDocument, byte[])
,
CCITTFactory.createFromFile(org.apache.pdfbox.pdmodel.PDDocument, java.io.File)
or ImageIO.read(java.io.File)
combined with
LosslessFactory.createFromImage(org.apache.pdfbox.pdmodel.PDDocument, java.awt.image.BufferedImage)
. (The later can also be used to create a
PDImageXObject from a BufferedImage). Starting with 2.0.18, this call will create an image
directly from a PNG file without decoding it (when possible), which is faster. However the
result size depends on the compression skill of the software that created the PNG file. If
file size or bandwidth are important to you or to your clients, then create your PNG files
with a tool that has implemented the
Zopfli
algorithm, or use the two-step process mentioned above.byteArray
- bytes from an image file.document
- the document that shall use this PDImageXObject.name
- name of image file for exception messages, can be null.java.io.IOException
- if there is an error when reading the file or creating the
PDImageXObject.java.lang.IllegalArgumentException
- if the image type is not supported.public PDMetadata getMetadata()
public void setMetadata(PDMetadata meta)
meta
- the metadata associated with this objectpublic int getStructParent()
public void setStructParent(int key)
key
- the new key for this XObjectpublic java.awt.image.BufferedImage getImage() throws java.io.IOException
public java.awt.image.BufferedImage getImage(java.awt.Rectangle region, int subsampling) throws java.io.IOException
the unparameterized version
, this method does not
cache the resulting image.getImage
in interface PDImage
region
- The region of the source image to get, or null if the entire image is needed.
The actual region will be clipped to the dimensions of the source image.subsampling
- The amount of rows and columns to advance for every output pixel, a value
of 1 meaning every pixel will be readjava.io.IOException
private float[] extractMatte(PDImageXObject softMask) throws java.io.IOException
softMask
- java.io.IOException
- if the color conversion fails.public java.awt.image.BufferedImage getStencilImage(java.awt.Paint paint) throws java.io.IOException
getStencilImage
in interface PDImage
paint
- the paint to fill the visible portions of the image withjava.io.IOException
- if the image cannot be readpublic java.awt.image.BufferedImage getOpaqueImage() throws java.io.IOException
java.io.IOException
- if the image cannot be readprivate java.awt.image.BufferedImage applyMask(java.awt.image.BufferedImage image, java.awt.image.BufferedImage mask, boolean isSoft, float[] matte)
private float clampColor(float color)
private java.awt.image.BufferedImage scaleImage(java.awt.image.BufferedImage image, int width, int height)
public PDImageXObject getMask() throws java.io.IOException
java.io.IOException
public COSArray getColorKeyMask()
public PDImageXObject getSoftMask() throws java.io.IOException
java.io.IOException
public int getBitsPerComponent()
PDImage
getBitsPerComponent
in interface PDImage
public void setBitsPerComponent(int bpc)
PDImage
setBitsPerComponent
in interface PDImage
bpc
- The number of bits per component.public PDColorSpace getColorSpace() throws java.io.IOException
PDImage
getColorSpace
in interface PDImage
java.io.IOException
- If there is an error getting the color space.public java.io.InputStream createInputStream() throws java.io.IOException
PDImage
createInputStream
in interface PDImage
java.io.IOException
- if the data could not be read.public java.io.InputStream createInputStream(DecodeOptions options) throws java.io.IOException
PDImage
DecodeOptions
.createInputStream
in interface PDImage
options
- Additional decoding options passed to the filters usedjava.io.IOException
- if the data could not be readpublic java.io.InputStream createInputStream(java.util.List<java.lang.String> stopFilters) throws java.io.IOException
PDImage
createInputStream
in interface PDImage
stopFilters
- A list of filters to stop decoding at.java.io.IOException
- if the data could not be read.public boolean isEmpty()
PDImage
public void setColorSpace(PDColorSpace cs)
PDImage
setColorSpace
in interface PDImage
cs
- The color space for this image.public int getHeight()
PDImage
public void setHeight(int h)
PDImage
public int getWidth()
PDImage
public void setWidth(int w)
PDImage
public boolean getInterpolate()
PDImage
getInterpolate
in interface PDImage
public void setInterpolate(boolean value)
PDImage
setInterpolate
in interface PDImage
public void setDecode(COSArray decode)
PDImage
public COSArray getDecode()
PDImage
public boolean isStencil()
PDImage
public void setStencil(boolean isStencil)
PDImage
ImageMask
entry in the image stream's dictionary.setStencil
in interface PDImage
isStencil
- True to make the image a stencil.