Class ImageParser

Direct Known Subclasses:
BmpImageParser, DcxImageParser, GifImageParser, IcnsImageParser, IcoImageParser, JpegImageParser, PcxImageParser, PngImageParser, PnmImageParser, PsdImageParser, RgbeImageParser, TiffImageParser, WbmpImageParser, XbmImageParser, XpmImageParser

public abstract class ImageParser extends BinaryFileParser
Provides the abstract base class for all image reading and writing utilities. ImageParser implementations are expected to extend this class providing logic for identifying and processing data in their own specific format. Specific implementations are found under the com.apache.commons.imaging.formats package.

Application Notes

Format support

For the most recent information on format support for the Apache Commons Imaging package, refer to Format Support at the main project development web site.

On the accuracy of this Javadoc

The original authors of this class did not supply documentation. The Javadoc for this class is based on inspection of the source code. In some cases, the purpose and usage for particular methods was deduced from the source and may not perfectly reflect the intentions of the original. Therefore, you should not assume that the documentation is perfect, especially in the more obscure and specialized areas of implementation.

The "Map params" argument

Many of the methods specified by this class accept an argument of type Map giving a list of parameters to be used when processing an image. For example, some of the output formats permit the specification of different kinds of image compression or color models. Some of the reading methods permit the calling application to require strict format compliance. In many cases, however, an application will not require the use of this argument. While some of the ImageParser implementations check for (and ignore) null arguments for this parameter, not all of them do (at least not at the time these notes were written). Therefore, a prudent programmer will always supply an valid, though empty instance of a Map implementation when calling such methods. Generally, the java HashMap class is useful for this purpose.

Additionally, developers creating or enhancing classes derived from ImageParser are encouraged to include such checks in their code.

  • Field Details

    • LOGGER

      private static final Logger LOGGER
  • Constructor Details

    • ImageParser

      public ImageParser()
  • Method Details

    • getAllImageParsers

      public static ImageParser[] getAllImageParsers()
      Gets an array of new instances of all image parsers.
      Returns:
      A valid array of image parsers
    • getMetadata

      public final ImageMetadata getMetadata(ByteSource byteSource) throws ImageReadException, IOException
      Get image metadata from the specified byte source. Format-specific ImageParser implementations are expected to return a valid IImageMetadata object or to throw an ImageReadException if unable to process the specified byte source.
      Parameters:
      byteSource - A valid byte source.
      Returns:
      A valid, potentially subject-matter-specific implementation of the IImageMetadata interface describing the content extracted from the source content.
      Throws:
      ImageReadException - In the event that the ByteSource content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful data read operation.
    • getMetadata

      public abstract ImageMetadata getMetadata(ByteSource byteSource, Map<String,Object> params) throws ImageReadException, IOException
      Get image metadata from the specified byte source. Format-specific ImageParser implementations are expected to return a valid IImageMetadata object or to throw an ImageReadException if unable to process the specified byte source.

      The params argument provides a mechanism for individual implementations to pass optional information into the parser. Not all formats will require this capability. Because the base class may call this method with a null params argument, implementations should always include logic for ignoring null input.

      Parameters:
      byteSource - A valid byte source.
      params - Optional instructions for special-handling or interpretation of the input data (null objects are permitted and must be supported by implementations).
      Returns:
      A valid, potentially subject-matter-specific implementation of the IImageMetadata interface describing the content extracted from the source content.
      Throws:
      ImageReadException - In the event that the ByteSource content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful data read operation.
    • getMetadata

      public final ImageMetadata getMetadata(byte[] bytes) throws ImageReadException, IOException
      Get image metadata from the specified array of bytes. Format-specific ImageParser implementations are expected to return a valid IImageMetadata object or to throw an ImageReadException if unable to process the specified data.
      Parameters:
      bytes - A valid array of bytes
      Returns:
      A valid, potentially subject-matter-specific implementation of the IImageMetadata interface describing the content extracted from the source content.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful data read operation.
    • getMetadata

      public final ImageMetadata getMetadata(byte[] bytes, Map<String,Object> params) throws ImageReadException, IOException
      Get image metadata from the specified array of bytes. Format-specific ImageParser implementations are expected to return a valid IImageMetadata object or to throw an ImageReadException if unable to process the specified data.

      The params argument provides a mechanism for individual implementations to pass optional information into the parser. Not all formats will require this capability. Because the base class may call this method with a null params argument, implementations should always include logic for ignoring null input.

      Parameters:
      bytes - A valid array of bytes
      params - Optional instructions for special-handling or interpretation of the input data (null objects are permitted and must be supported by implementations).
      Returns:
      A valid image metadata object describing the content extracted from the specified content.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful data read operation.
    • getMetadata

      public final ImageMetadata getMetadata(File file) throws ImageReadException, IOException
      Get image metadata from the specified file. Format-specific ImageParser implementations are expected to return a valid IImageMetadata object or to throw an ImageReadException if unable to process the specified data.
      Parameters:
      file - A valid reference to a file.
      Returns:
      A valid image metadata object describing the content extracted from the specified content.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful file read or access operation.
    • getMetadata

      public final ImageMetadata getMetadata(File file, Map<String,Object> params) throws ImageReadException, IOException
      Get image metadata from the specified file. Format-specific ImageParser implementations are expected to return a valid IImageMetadata object or to throw an ImageReadException if unable to process the specified data.

      The params argument provides a mechanism for individual implementations to pass optional information into the parser. Not all formats will require this capability. Because the base class may call this method with a null params argument, implementations should always include logic for ignoring null input.

      Parameters:
      file - A valid reference to a file.
      params - Optional instructions for special-handling or interpretation of the input data (null objects are permitted and must be supported by implementations).
      Returns:
      A valid image metadata object describing the content extracted from the specified content.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful file read or access operation.
    • getImageInfo

      public abstract ImageInfo getImageInfo(ByteSource byteSource, Map<String,Object> params) throws ImageReadException, IOException
      Get image information from the specified ByteSource. Format-specific ImageParser implementations are expected to return a valid ImageInfo object or to throw an ImageReadException if unable to process the specified data.

      The params argument provides a mechanism for individual implementations to pass optional information into the parser. Not all formats will require this capability. Because the base class may call this method with a null params argument, implementations should always include logic for ignoring null input.

      Parameters:
      byteSource - A valid ByteSource object
      params - Optional instructions for special-handling or interpretation of the input data (null objects are permitted and must be supported by implementations).
      Returns:
      A valid image information object describing the content extracted from the specified data.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful data access operation.
    • getImageInfo

      public final ImageInfo getImageInfo(ByteSource byteSource) throws ImageReadException, IOException
      Get image information from the specified ByteSource. Format-specific ImageParser implementations are expected to return a valid ImageInfo object or to throw an ImageReadException if unable to process the specified data.
      Parameters:
      byteSource - A valid ByteSource object
      Returns:
      A valid image information object describing the content extracted from the specified data.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful data access operation.
    • getImageInfo

      public final ImageInfo getImageInfo(byte[] bytes, Map<String,Object> params) throws ImageReadException, IOException
      Get image information from the specified array of bytes. Format-specific ImageParser implementations are expected to return a valid ImageInfo object or to throw an ImageReadException if unable to process the specified data.

      The params argument provides a mechanism for individual implementations to pass optional information into the parser. Not all formats will require this capability. Because the base class may call this method with a null params argument, implementations should always include logic for ignoring null input.

      Parameters:
      bytes - A valid array of bytes
      params - Optional instructions for special-handling or interpretation of the input data (null objects are permitted and must be supported by implementations).
      Returns:
      A valid image information object describing the content extracted from the specified data.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful data access operation.
    • getImageInfo

      public final ImageInfo getImageInfo(File file, Map<String,Object> params) throws ImageReadException, IOException
      Get image information from the specified file Format-specific ImageParser implementations are expected to return a valid ImageInfo object or to throw an ImageReadException if unable to process the specified data.

      The params argument provides a mechanism for individual implementations to pass optional information into the parser. Not all formats will require this capability. Because the base class may call this method with a null params argument, implementations should always include logic for ignoring null input.

      Parameters:
      file - A valid File object
      params - Optional instructions for special-handling or interpretation of the input data (null objects are permitted and must be supported by implementations).
      Returns:
      A valid image information object describing the content extracted from the specified data.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful file read or access operation.
    • getFormatCompliance

      public FormatCompliance getFormatCompliance(ByteSource byteSource) throws ImageReadException, IOException
      Determines the format compliance of the content of the supplied byte source based on rules provided by a specific implementation.
      Parameters:
      byteSource - A valid instance of ByteSource
      Returns:
      true if the content is format-compliant; otherwise, false
      Throws:
      ImageReadException - may be thrown by sub-classes
      IOException - may be thrown by sub-classes
    • getFormatCompliance

      public final FormatCompliance getFormatCompliance(byte[] bytes) throws ImageReadException, IOException
      Determines the format compliance of the content of the supplied byte array based on rules provided by a specific implementation.
      Parameters:
      bytes - A valid byte array.
      Returns:
      A valid FormatCompliance object.
      Throws:
      ImageReadException - may be thrown by sub-classes
      IOException - may be thrown by sub-classes
    • getFormatCompliance

      public final FormatCompliance getFormatCompliance(File file) throws ImageReadException, IOException
      Determines the format compliance of the specified file based on rules provided by a specific implementation.
      Parameters:
      file - A valid reference to a file.
      Returns:
      A valid format compliance object.
      Throws:
      ImageReadException - may be thrown by sub-classes
      IOException - may be thrown by sub-classes
    • getAllBufferedImages

      public List<BufferedImage> getAllBufferedImages(ByteSource byteSource) throws ImageReadException, IOException
      Gets all images specified by the byte source (some formats may include multiple images within a single data source).
      Parameters:
      byteSource - A valid instance of ByteSource.
      Returns:
      A valid (potentially empty) list of BufferedImage objects.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getAllBufferedImages

      public final List<BufferedImage> getAllBufferedImages(byte[] bytes) throws ImageReadException, IOException
      Gets all images specified by the byte array (some formats may include multiple images within a single data source).
      Parameters:
      bytes - A valid byte array
      Returns:
      A valid (potentially empty) list of BufferedImage objects.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getAllBufferedImages

      public final List<BufferedImage> getAllBufferedImages(File file) throws ImageReadException, IOException
      Gets all images specified by indicated file (some formats may include multiple images within a single data source).
      Parameters:
      file - A valid reference to a file.
      Returns:
      A valid (potentially empty) list of BufferedImage objects.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getBufferedImage

      public abstract BufferedImage getBufferedImage(ByteSource byteSource, Map<String,Object> params) throws ImageReadException, IOException
      Gets a buffered image specified by the byte source (for sources that specify multiple images, choice of which image is returned is implementation dependent).
      Parameters:
      byteSource - A valid instance of ByteSource
      params - Optional instructions for special-handling or interpretation of the input data (null objects are permitted and must be supported by implementations).
      Returns:
      A valid instance of BufferedImage.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getBufferedImage

      public final BufferedImage getBufferedImage(byte[] bytes, Map<String,Object> params) throws ImageReadException, IOException
      Gets a buffered image specified by the byte array (for sources that specify multiple images, choice of which image is returned is implementation dependent).
      Parameters:
      bytes - A valid byte array
      params - Optional instructions for special-handling or interpretation of the input data (null objects are permitted and must be supported by implementations).
      Returns:
      A valid instance of BufferedImage.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getBufferedImage

      public final BufferedImage getBufferedImage(File file, Map<String,Object> params) throws ImageReadException, IOException
      Gets a buffered image specified by the indicated file (for sources that specify multiple images, choice of which image is returned is implementation dependent).
      Parameters:
      file - A valid file reference.
      params - Optional instructions for special-handling or interpretation of the input data (null objects are permitted and must be supported by implementations).
      Returns:
      A valid instance of BufferedImage.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • writeImage

      public void writeImage(BufferedImage src, OutputStream os, Map<String,Object> params) throws ImageWriteException, IOException
      Writes the content of a BufferedImage to the specified output stream.

      The params argument provides a mechanism for individual implementations to pass optional information into the parser. Not all formats will support this capability. Currently, some of the parsers do not check for null arguments. So in cases where no optional specifications are supported, application code should pass in an empty instance of an implementation of the map interface (i.e. an empty HashMap).

      Parameters:
      src - An image giving the source content for output
      os - A valid output stream for storing the formatted image
      params - A non-null Map implementation supplying optional, format-specific instructions for output (such as selections for data compression, color models, etc.)
      Throws:
      ImageWriteException - In the event that the output format cannot handle the input image or invalid params are specified.
      IOException - In the event of an write error from the output stream.
    • getImageSize

      public final Dimension getImageSize(byte[] bytes) throws ImageReadException, IOException
      Get the size of the image described by the specified byte array.
      Parameters:
      bytes - A valid byte array.
      Returns:
      A valid instance of Dimension.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getImageSize

      public final Dimension getImageSize(byte[] bytes, Map<String,Object> params) throws ImageReadException, IOException
      Get the size of the image described by the specified byte array.
      Parameters:
      bytes - A valid byte array.
      params - Optional instructions for special-handling or interpretation of the input data.
      Returns:
      A valid instance of Dimension.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getImageSize

      public final Dimension getImageSize(File file) throws ImageReadException, IOException
      Get the size of the image described by the specified file.
      Parameters:
      file - A valid reference to a file.
      Returns:
      A valid instance of Dimension.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getImageSize

      public final Dimension getImageSize(File file, Map<String,Object> params) throws ImageReadException, IOException
      Get the size of the image described by the specified file.
      Parameters:
      file - A valid reference to a file.
      params - Optional instructions for special-handling or interpretation of the input data.
      Returns:
      A valid instance of Dimension.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getImageSize

      public abstract Dimension getImageSize(ByteSource byteSource, Map<String,Object> params) throws ImageReadException, IOException
      Get the size of the image described by the specified ByteSource.
      Parameters:
      byteSource - A valid reference to a ByteSource.
      params - Optional instructions for special-handling or interpretation of the input data.
      Returns:
      A valid instance of Dimension.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getICCProfileBytes

      public final byte[] getICCProfileBytes(byte[] bytes) throws ImageReadException, IOException
      Get an array of bytes describing the International Color Consortium (ICC) specification for the color space of the image contained in the input byte array. Not all formats support ICC profiles.
      Parameters:
      bytes - A valid array of bytes.
      Returns:
      If available, a valid array of bytes; otherwise, a null
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getICCProfileBytes

      public final byte[] getICCProfileBytes(byte[] bytes, Map<String,Object> params) throws ImageReadException, IOException
      Get an array of bytes describing the International Color Consortium (ICC) specification for the color space of the image contained in the input byte array. Not all formats support ICC profiles.
      Parameters:
      bytes - A valid array of bytes.
      params - Optional instructions for special-handling or interpretation of the input data.
      Returns:
      If available, a valid array of bytes; otherwise, a null
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getICCProfileBytes

      public final byte[] getICCProfileBytes(File file) throws ImageReadException, IOException
      Get an array of bytes describing the International Color Consortium (ICC) specification for the color space of the image contained in the input file. Not all formats support ICC profiles.
      Parameters:
      file - A valid file reference.
      Returns:
      If available, a valid array of bytes; otherwise, a null
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getICCProfileBytes

      public final byte[] getICCProfileBytes(File file, Map<String,Object> params) throws ImageReadException, IOException
      Get an array of bytes describing the International Color Consortium (ICC) specification for the color space of the image contained in the input file. Not all formats support ICC profiles.
      Parameters:
      file - A valid file reference.
      params - Optional instructions for special-handling or interpretation of the input data.
      Returns:
      If available, a valid array of bytes; otherwise, a null
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getICCProfileBytes

      public abstract byte[] getICCProfileBytes(ByteSource byteSource, Map<String,Object> params) throws ImageReadException, IOException
      Get an array of bytes describing the International Color Consortium (ICC) specification for the color space of the image contained in the input byteSource. Not all formats support ICC profiles.
      Parameters:
      byteSource - A valid ByteSource.
      params - Optional instructions for special-handling or interpretation of the input data.
      Returns:
      If available, a valid array of bytes; otherwise, a null
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • dumpImageFile

      public final String dumpImageFile(byte[] bytes) throws ImageReadException, IOException
      Write the ImageInfo and format-specific information for the image content of the specified byte array to a string.
      Parameters:
      bytes - A valid array of bytes.
      Returns:
      A valid string.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • dumpImageFile

      public final String dumpImageFile(File file) throws ImageReadException, IOException
      Write the ImageInfo and format-specific information for the image content of the specified file to a string.
      Parameters:
      file - A valid file reference.
      Returns:
      A valid string.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • dumpImageFile

      public final String dumpImageFile(ByteSource byteSource) throws ImageReadException, IOException
      Write the ImageInfo and format-specific information for the image content of the specified byte source to a string.
      Parameters:
      byteSource - A valid byte source.
      Returns:
      A valid string.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • dumpImageFile

      public boolean dumpImageFile(PrintWriter pw, ByteSource byteSource) throws ImageReadException, IOException
      Write the ImageInfo and format-specific information for the image content of the specified byte source to a PrintWriter
      Parameters:
      pw - print writer used for writing the ImageInfo
      byteSource - A valid byte source.
      Returns:
      A valid PrintWriter.
      Throws:
      ImageReadException - In the event that the specified content does not conform to the format of the specific parser implementation.
      IOException - In the event of unsuccessful read or access operation.
    • getName

      public abstract String getName()
      Get a descriptive name for the implementation of an ImageParser.
      Returns:
      a valid, subject-matter-specific string.
    • getDefaultExtension

      public abstract String getDefaultExtension()
      Get the default extension for the format specified by an implementation of ImageParser. Some parsers can support more than one extension (i.e. .JPEG, .JPG; .TIF, .TIFF, etc.).
      Returns:
      A valid string.
    • getAcceptedExtensions

      protected abstract String[] getAcceptedExtensions()
      Get an array of all accepted extensions
      Returns:
      A valid array of one or more elements.
    • getAcceptedTypes

      protected abstract ImageFormat[] getAcceptedTypes()
      Get an array of ImageFormat objects describing all accepted types
      Returns:
      A valid array of one or more elements.
    • canAcceptType

      public boolean canAcceptType(ImageFormat type)
      Indicates whether the ImageParser implementation can accept the specified format
      Parameters:
      type - An instance of ImageFormat.
      Returns:
      If the parser can accept the format, true; otherwise, false.
    • canAcceptExtension

      protected final boolean canAcceptExtension(File file)
      Indicates whether the ImageParser implementation can accept the specified file based on its extension.
      Parameters:
      file - An valid file reference.
      Returns:
      If the parser can accept the format, true; otherwise, false.
    • canAcceptExtension

      protected final boolean canAcceptExtension(String fileName)
      Indicates whether the ImageParser implementation can accept the specified file name based on its extension.
      Parameters:
      fileName - A valid string giving a file name or file path.
      Returns:
      If the parser can accept the format, true; otherwise, false.
    • getBufferedImageFactory

      protected BufferedImageFactory getBufferedImageFactory(Map<String,Object> params)
      Get an instance of IBufferedImageFactory based on the presence of a specification for ImagingConstants..BUFFERED_IMAGE_FACTORY within the supplied params.
      Parameters:
      params - A valid Map object, or a null.
      Returns:
      A valid instance of an implementation of a IBufferedImageFactory.
    • isStrict

      public static boolean isStrict(Map<String,Object> params)
      A utility method to search a params specification and determine whether it contains the ImagingConstants.PARAM_KEY_STRICT specification. Intended for internal use by ImageParser implementations.
      Parameters:
      params - A valid Map object (or a null).
      Returns:
      If the params specify strict format compliance, true; otherwise, false.