Class PDImageXObject

java.lang.Object
org.sejda.sambox.pdmodel.graphics.PDXObject
org.sejda.sambox.pdmodel.graphics.image.PDImageXObject
All Implemented Interfaces:
COSObjectable, PDImage

public final class PDImageXObject extends PDXObject implements PDImage
An Image XObject.
Author:
John Hewson, Ben Litchfield
  • Constructor Details

    • PDImageXObject

      public PDImageXObject() throws IOException
      Creates an Image XObject in the given document.
      Throws:
      IOException - if there is an error creating the XObject.
    • PDImageXObject

      public PDImageXObject(InputStream encodedStream, COSBase cosFilter, int width, int height, int bitsPerComponent, PDColorSpace initColorSpace) throws IOException
      Creates an Image XObject in the given document using the given filtered stream.
      Parameters:
      encodedStream - a filtered stream of image data
      cosFilter - the filter or a COSArray of filters
      width - the image width
      height - the image height
      bitsPerComponent - the bits per component
      initColorSpace - the color space
      Throws:
      IOException - if there is an error creating the XObject.
    • PDImageXObject

      public PDImageXObject(PDStream stream, PDResources resources) throws IOException
      Creates an Image XObject with the given stream as its contents and current color spaces.
      Parameters:
      stream - the XObject stream to read
      resources - the current resources
      Throws:
      IOException - if there is an error creating the XObject.
  • Method Details

    • createThumbnail

      public static PDImageXObject createThumbnail(COSStream cosStream) throws IOException
      Creates a thumbnail Image XObject from the given COSBase and name.
      Parameters:
      cosStream - the COS stream
      Returns:
      an XObject
      Throws:
      IOException - if there is an error creating the XObject.
    • createFromFile

      public static PDImageXObject createFromFile(String imagePath) throws IOException
      Throws:
      IOException
    • createFromFile

      public static PDImageXObject createFromFile(File file) throws IOException
      Throws:
      IOException
    • createFromSeekableSource

      public static PDImageXObject createFromSeekableSource(org.sejda.io.SeekableSource source, String filename) throws IOException
      Throws:
      IOException
    • getMetadata

      public PDMetadata getMetadata()
      Returns the metadata associated with this XObject, or null if there is none.
      Returns:
      the metadata associated with this object.
    • setMetadata

      public void setMetadata(PDMetadata meta)
      Sets the metadata associated with this XObject, or null if there is none.
      Parameters:
      meta - the metadata associated with this object
    • getStructParent

      public int getStructParent()
      Returns the key of this XObject in the structural parent tree.
      Returns:
      this object's key the structural parent tree
    • setStructParent

      public void setStructParent(int key)
      Sets the key of this XObject in the structural parent tree.
      Parameters:
      key - the new key for this XObject
    • getImage

      public BufferedImage getImage() throws IOException
      Returns the content of this image as an AWT buffered image with an (A)RGB color space. The size of the returned image is the larger of the size of the image itself or its mask. The returned images are cached for the lifetime of this XObject.
      Specified by:
      getImage in interface PDImage
      Returns:
      content of this image as a buffered image.
      Throws:
      IOException
    • getRawImage

      public BufferedImage getRawImage() throws IOException
      Description copied from interface: PDImage
      Try to get the raw image as AWT buffered image with it's original colorspace. No color conversion is performed.

      You could use the returned BufferedImage for draw operations. But this would be very slow as the color conversion would happen on demand. You rather should use PDImage.getImage() for that.

      This method returns null if it is not possible to map the underlying colorspace into a java.awt.ColorSpace.

      Use this method if you want to extract the image without loosing any color information, as no color conversion will be performed.

      You can alwoys use PDImage.getRawRaster(), if you want to access the raw data even if no matching java.awt.ColorSpace exists

      Specified by:
      getRawImage in interface PDImage
      Returns:
      the raw image with a java.awt.ColorSpace or null
      Throws:
      IOException
    • getRawRaster

      public WritableRaster getRawRaster() throws IOException
      Description copied from interface: PDImage
      Return the image data as WritableRaster. You should consult the PDColorSpace returned by PDImage.getColorSpace() to know how to interpret the data in this WritableRaster.

      Use this if e.g. want access to the raw color information of a PDDeviceN image.

      Specified by:
      getRawRaster in interface PDImage
      Returns:
      the raw writable raster for this image
      Throws:
      IOException
    • getImageWithoutMasks

      public BufferedImage getImageWithoutMasks() throws IOException
      Returns:
      the image without mask applied. The image is not cached
      Throws:
      IOException
    • getStencilImage

      public BufferedImage getStencilImage(Paint paint) throws IOException
      Returns an ARGB image filled with the given paint and using this image as a mask. The returned images are not cached.
      Specified by:
      getStencilImage in interface PDImage
      Parameters:
      paint - the paint to fill the visible portions of the image with
      Returns:
      a masked image filled with the given paint
      Throws:
      IOException - if the image cannot be read
    • getOpaqueImage

      public BufferedImage getOpaqueImage() throws IOException
      Returns an RGB buffered image containing the opaque image stream without any masks applied. If this Image XObject is a mask then the buffered image will contain the raw mask.
      Returns:
      the image without any masks applied
      Throws:
      IOException - if the image cannot be read
    • hasMask

      public boolean hasMask()
    • getMask

      public PDImageXObject getMask() throws IOException
      Returns the Mask Image XObject associated with this image, or null if there is none.
      Returns:
      Mask Image XObject
      Throws:
      IOException
    • getColorKeyMask

      public COSArray getColorKeyMask()
      Returns the color key mask array associated with this image, or null if there is none.
      Returns:
      Mask Image XObject
    • hasSoftMask

      public boolean hasSoftMask()
    • getSoftMask

      public PDImageXObject getSoftMask() throws IOException
      Returns the Soft Mask Image XObject associated with this image, or null if there is none.
      Returns:
      the SMask Image XObject, or null.
      Throws:
      IOException
    • getBitsPerComponent

      public int getBitsPerComponent()
      Description copied from interface: PDImage
      Returns bits per component of this image, or -1 if one has not been set.
      Specified by:
      getBitsPerComponent in interface PDImage
    • setBitsPerComponent

      public void setBitsPerComponent(int bpc)
      Description copied from interface: PDImage
      Set the number of bits per component.
      Specified by:
      setBitsPerComponent in interface PDImage
      Parameters:
      bpc - The number of bits per component.
    • getColorSpace

      public PDColorSpace getColorSpace() throws IOException
      Description copied from interface: PDImage
      Returns the image's color space.
      Specified by:
      getColorSpace in interface PDImage
      Throws:
      IOException - If there is an error getting the color space.
    • createInputStream

      public InputStream createInputStream() throws IOException
      Description copied from interface: PDImage
      Returns an InputStream containing the image data, irrespective of whether this is an inline image or an image XObject.
      Specified by:
      createInputStream in interface PDImage
      Returns:
      Decoded stream
      Throws:
      IOException - if the data could not be read.
    • asByteBuffer

      public ByteBuffer asByteBuffer() throws IOException
      Specified by:
      asByteBuffer in interface PDImage
      Returns:
      image data in the form of a ByteBuffer
      Throws:
      IOException
    • isEmpty

      public boolean isEmpty() throws IOException
      Description copied from interface: PDImage
      Returns true if the image has no data.
      Specified by:
      isEmpty in interface PDImage
      Throws:
      IOException
    • setColorSpace

      public void setColorSpace(PDColorSpace cs)
      Description copied from interface: PDImage
      Sets the color space for this image.
      Specified by:
      setColorSpace in interface PDImage
      Parameters:
      cs - The color space for this image.
    • getHeight

      public int getHeight()
      Description copied from interface: PDImage
      Returns height of this image, or -1 if one has not been set.
      Specified by:
      getHeight in interface PDImage
    • setHeight

      public void setHeight(int h)
      Description copied from interface: PDImage
      Sets the height of the image.
      Specified by:
      setHeight in interface PDImage
      Parameters:
      h - The height of the image.
    • getWidth

      public int getWidth()
      Description copied from interface: PDImage
      Returns the width of this image, or -1 if one has not been set.
      Specified by:
      getWidth in interface PDImage
    • setWidth

      public void setWidth(int w)
      Description copied from interface: PDImage
      Sets the width of the image.
      Specified by:
      setWidth in interface PDImage
      Parameters:
      w - The width of the image.
    • getInterpolate

      public boolean getInterpolate()
      Description copied from interface: PDImage
      Returns true if the image should be interpolated when rendered.
      Specified by:
      getInterpolate in interface PDImage
    • setInterpolate

      public void setInterpolate(boolean value)
      Description copied from interface: PDImage
      Sets the Interpolate flag, true for high-quality image scaling.
      Specified by:
      setInterpolate in interface PDImage
    • setDecode

      public void setDecode(COSArray decode)
      Description copied from interface: PDImage
      Sets the decode array.
      Specified by:
      setDecode in interface PDImage
      Parameters:
      decode - the new decode array.
    • getDecode

      public COSArray getDecode()
      Description copied from interface: PDImage
      Returns the decode array.
      Specified by:
      getDecode in interface PDImage
    • isStencil

      public boolean isStencil()
      Description copied from interface: PDImage
      Returns true if the image is a stencil mask.
      Specified by:
      isStencil in interface PDImage
    • setStencil

      public void setStencil(boolean isStencil)
      Description copied from interface: PDImage
      Sets whether or not the image is a stencil. This corresponds to the ImageMask entry in the image stream's dictionary.
      Specified by:
      setStencil in interface PDImage
      Parameters:
      isStencil - True to make the image a stencil.
    • getOptionalContent

      public PDPropertyList getOptionalContent()
      Returns:
      The optional content group or optional content membership dictionary or null if there is none.
    • setOptionalContent

      public void setOptionalContent(PDPropertyList oc)
      Sets the optional content group or optional content membership dictionary.