Class GifImageParser
- java.lang.Object
-
- org.apache.commons.imaging.common.BinaryFileParser
-
- org.apache.commons.imaging.ImageParser
-
- org.apache.commons.imaging.formats.gif.GifImageParser
-
- All Implemented Interfaces:
XmpEmbeddable
public class GifImageParser extends ImageParser implements XmpEmbeddable
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String[]
ACCEPTED_EXTENSIONS
private static int
APPLICATION_EXTENSION_LABEL
private static int
COMMENT_EXTENSION
private static java.lang.String
DEFAULT_EXTENSION
private static int
EXTENSION_CODE
private static byte[]
GIF_HEADER_SIGNATURE
private static int
GRAPHIC_CONTROL_EXTENSION
private static int
IMAGE_SEPARATOR
private static int
INTERLACE_FLAG_MASK
private static int
LOCAL_COLOR_TABLE_FLAG_MASK
private static java.util.logging.Logger
LOGGER
private static int
PLAIN_TEXT_EXTENSION
private static int
SORT_FLAG_MASK
private static int
TERMINATOR_BYTE
private static byte[]
XMP_APPLICATION_ID_AND_AUTH_CODE
private static int
XMP_COMPLETE_CODE
private static int
XMP_EXTENSION
-
Constructor Summary
Constructors Constructor Description GifImageParser()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private int
convertColorTableSize(int tableSize)
(package private) static DisposalMethod
createDisposalMethodFromIntValue(int value)
boolean
dumpImageFile(java.io.PrintWriter pw, ByteSource byteSource)
Write the ImageInfo and format-specific information for the image content of the specified byte source to a PrintWriterprivate <T extends GifBlock>
java.util.List<T>findAllBlocks(java.util.List<GifBlock> blocks, int code)
SeereadBlocks(org.apache.commons.imaging.formats.gif.GifHeaderInfo, java.io.InputStream, boolean, org.apache.commons.imaging.FormatCompliance)
for reference how the blocks are created.private java.util.List<GifImageData>
findAllImageData(GifImageContents imageContents)
private GifBlock
findBlock(java.util.List<GifBlock> blocks, int code)
private GifImageData
findFirstImageData(GifImageContents imageContents)
protected java.lang.String[]
getAcceptedExtensions()
Get an array of all accepted extensionsprotected ImageFormat[]
getAcceptedTypes()
Get an array of ImageFormat objects describing all accepted typesjava.util.List<java.awt.image.BufferedImage>
getAllBufferedImages(ByteSource byteSource)
Gets all images specified by the byte source (some formats may include multiple images within a single data source).java.awt.image.BufferedImage
getBufferedImage(ByteSource byteSource, java.util.Map<java.lang.String,java.lang.Object> params)
Gets a buffered image specified by the byte source (for sources that specify multiple images, choice of which image is returned is implementation dependent).private java.awt.image.BufferedImage
getBufferedImage(GifHeaderInfo headerInfo, GifImageData imageData, byte[] globalColorTable)
private int[]
getColorTable(byte[] bytes)
private java.util.List<java.lang.String>
getComments(java.util.List<GifBlock> blocks)
java.lang.String
getDefaultExtension()
Get the default extension for the format specified by an implementation of ImageParser.FormatCompliance
getFormatCompliance(ByteSource byteSource)
Determines the format compliance of the content of the supplied byte source based on rules provided by a specific implementation.byte[]
getICCProfileBytes(ByteSource byteSource, java.util.Map<java.lang.String,java.lang.Object> params)
Get an array of bytes describing the International Color Consortium (ICC) specification for the color space of the image contained in the input byteSource.ImageInfo
getImageInfo(ByteSource byteSource, java.util.Map<java.lang.String,java.lang.Object> params)
Get image information from the specified ByteSource.java.awt.Dimension
getImageSize(ByteSource byteSource, java.util.Map<java.lang.String,java.lang.Object> params)
Get the size of the image described by the specified ByteSource.ImageMetadata
getMetadata(ByteSource byteSource, java.util.Map<java.lang.String,java.lang.Object> params)
Get image metadata from the specified byte source.java.lang.String
getName()
Get a descriptive name for the implementation of an ImageParser.java.lang.String
getXmpXml(ByteSource byteSource, java.util.Map<java.lang.String,java.lang.Object> params)
Extracts embedded XML metadata as XML string.private java.util.List<GifBlock>
readBlocks(GifHeaderInfo ghi, java.io.InputStream is, boolean stopBeforeImageData, FormatCompliance formatCompliance)
private byte[]
readColorTable(java.io.InputStream is, int tableSize)
private GifImageContents
readFile(ByteSource byteSource, boolean stopBeforeImageData)
private GifImageContents
readFile(ByteSource byteSource, boolean stopBeforeImageData, FormatCompliance formatCompliance)
private GenericGifBlock
readGenericGIFBlock(java.io.InputStream is, int code)
private GenericGifBlock
readGenericGIFBlock(java.io.InputStream is, int code, byte[] first)
private GraphicControlExtension
readGraphicControlExtension(int code, java.io.InputStream is)
private GifHeaderInfo
readHeader(java.io.InputStream is, FormatCompliance formatCompliance)
private ImageDescriptor
readImageDescriptor(GifHeaderInfo ghi, int blockCode, java.io.InputStream is, boolean stopBeforeImageData, FormatCompliance formatCompliance)
private byte[]
readSubBlock(java.io.InputStream is)
private int
simplePow(int base, int power)
private void
writeAsSubBlocks(java.io.OutputStream os, byte[] bytes)
void
writeImage(java.awt.image.BufferedImage src, java.io.OutputStream os, java.util.Map<java.lang.String,java.lang.Object> params)
Writes the content of a BufferedImage to the specified output stream.-
Methods inherited from class org.apache.commons.imaging.ImageParser
canAcceptExtension, canAcceptExtension, canAcceptType, dumpImageFile, dumpImageFile, dumpImageFile, getAllBufferedImages, getAllBufferedImages, getAllImageParsers, getBufferedImage, getBufferedImage, getBufferedImageFactory, getFormatCompliance, getFormatCompliance, getICCProfileBytes, getICCProfileBytes, getICCProfileBytes, getICCProfileBytes, getImageInfo, getImageInfo, getImageInfo, getImageSize, getImageSize, getImageSize, getImageSize, getMetadata, getMetadata, getMetadata, getMetadata, getMetadata, isStrict
-
Methods inherited from class org.apache.commons.imaging.common.BinaryFileParser
debugNumber, debugNumber, getByteOrder, setByteOrder
-
-
-
-
Field Detail
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
DEFAULT_EXTENSION
private static final java.lang.String DEFAULT_EXTENSION
- See Also:
- Constant Field Values
-
ACCEPTED_EXTENSIONS
private static final java.lang.String[] ACCEPTED_EXTENSIONS
-
GIF_HEADER_SIGNATURE
private static final byte[] GIF_HEADER_SIGNATURE
-
EXTENSION_CODE
private static final int EXTENSION_CODE
- See Also:
- Constant Field Values
-
IMAGE_SEPARATOR
private static final int IMAGE_SEPARATOR
- See Also:
- Constant Field Values
-
GRAPHIC_CONTROL_EXTENSION
private static final int GRAPHIC_CONTROL_EXTENSION
- See Also:
- Constant Field Values
-
COMMENT_EXTENSION
private static final int COMMENT_EXTENSION
- See Also:
- Constant Field Values
-
PLAIN_TEXT_EXTENSION
private static final int PLAIN_TEXT_EXTENSION
- See Also:
- Constant Field Values
-
XMP_EXTENSION
private static final int XMP_EXTENSION
- See Also:
- Constant Field Values
-
TERMINATOR_BYTE
private static final int TERMINATOR_BYTE
- See Also:
- Constant Field Values
-
APPLICATION_EXTENSION_LABEL
private static final int APPLICATION_EXTENSION_LABEL
- See Also:
- Constant Field Values
-
XMP_COMPLETE_CODE
private static final int XMP_COMPLETE_CODE
- See Also:
- Constant Field Values
-
LOCAL_COLOR_TABLE_FLAG_MASK
private static final int LOCAL_COLOR_TABLE_FLAG_MASK
- See Also:
- Constant Field Values
-
INTERLACE_FLAG_MASK
private static final int INTERLACE_FLAG_MASK
- See Also:
- Constant Field Values
-
SORT_FLAG_MASK
private static final int SORT_FLAG_MASK
- See Also:
- Constant Field Values
-
XMP_APPLICATION_ID_AND_AUTH_CODE
private static final byte[] XMP_APPLICATION_ID_AND_AUTH_CODE
-
-
Method Detail
-
getName
public java.lang.String getName()
Description copied from class:ImageParser
Get a descriptive name for the implementation of an ImageParser.- Specified by:
getName
in classImageParser
- Returns:
- a valid, subject-matter-specific string.
-
getDefaultExtension
public java.lang.String getDefaultExtension()
Description copied from class:ImageParser
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.).- Specified by:
getDefaultExtension
in classImageParser
- Returns:
- A valid string.
-
getAcceptedExtensions
protected java.lang.String[] getAcceptedExtensions()
Description copied from class:ImageParser
Get an array of all accepted extensions- Specified by:
getAcceptedExtensions
in classImageParser
- Returns:
- A valid array of one or more elements.
-
getAcceptedTypes
protected ImageFormat[] getAcceptedTypes()
Description copied from class:ImageParser
Get an array of ImageFormat objects describing all accepted types- Specified by:
getAcceptedTypes
in classImageParser
- Returns:
- A valid array of one or more elements.
-
readHeader
private GifHeaderInfo readHeader(java.io.InputStream is, FormatCompliance formatCompliance) throws ImageReadException, java.io.IOException
- Throws:
ImageReadException
java.io.IOException
-
readGraphicControlExtension
private GraphicControlExtension readGraphicControlExtension(int code, java.io.InputStream is) throws java.io.IOException
- Throws:
java.io.IOException
-
readSubBlock
private byte[] readSubBlock(java.io.InputStream is) throws java.io.IOException
- Throws:
java.io.IOException
-
readGenericGIFBlock
private GenericGifBlock readGenericGIFBlock(java.io.InputStream is, int code) throws java.io.IOException
- Throws:
java.io.IOException
-
readGenericGIFBlock
private GenericGifBlock readGenericGIFBlock(java.io.InputStream is, int code, byte[] first) throws java.io.IOException
- Throws:
java.io.IOException
-
readBlocks
private java.util.List<GifBlock> readBlocks(GifHeaderInfo ghi, java.io.InputStream is, boolean stopBeforeImageData, FormatCompliance formatCompliance) throws ImageReadException, java.io.IOException
- Throws:
ImageReadException
java.io.IOException
-
readImageDescriptor
private ImageDescriptor readImageDescriptor(GifHeaderInfo ghi, int blockCode, java.io.InputStream is, boolean stopBeforeImageData, FormatCompliance formatCompliance) throws ImageReadException, java.io.IOException
- Throws:
ImageReadException
java.io.IOException
-
simplePow
private int simplePow(int base, int power)
-
convertColorTableSize
private int convertColorTableSize(int tableSize)
-
readColorTable
private byte[] readColorTable(java.io.InputStream is, int tableSize) throws java.io.IOException
- Throws:
java.io.IOException
-
findAllBlocks
private <T extends GifBlock> java.util.List<T> findAllBlocks(java.util.List<GifBlock> blocks, int code)
SeereadBlocks(org.apache.commons.imaging.formats.gif.GifHeaderInfo, java.io.InputStream, boolean, org.apache.commons.imaging.FormatCompliance)
for reference how the blocks are created. They should match the code we are giving here, returning the correct class type. Internal only.
-
readFile
private GifImageContents readFile(ByteSource byteSource, boolean stopBeforeImageData) throws ImageReadException, java.io.IOException
- Throws:
ImageReadException
java.io.IOException
-
readFile
private GifImageContents readFile(ByteSource byteSource, boolean stopBeforeImageData, FormatCompliance formatCompliance) throws ImageReadException, java.io.IOException
- Throws:
ImageReadException
java.io.IOException
-
getICCProfileBytes
public byte[] getICCProfileBytes(ByteSource byteSource, java.util.Map<java.lang.String,java.lang.Object> params) throws ImageReadException, java.io.IOException
Description copied from class:ImageParser
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.- Specified by:
getICCProfileBytes
in classImageParser
- 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.java.io.IOException
- In the event of unsuccessful read or access operation.
-
getImageSize
public java.awt.Dimension getImageSize(ByteSource byteSource, java.util.Map<java.lang.String,java.lang.Object> params) throws ImageReadException, java.io.IOException
Description copied from class:ImageParser
Get the size of the image described by the specified ByteSource.- Specified by:
getImageSize
in classImageParser
- 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.java.io.IOException
- In the event of unsuccessful read or access operation.
-
createDisposalMethodFromIntValue
static DisposalMethod createDisposalMethodFromIntValue(int value) throws ImageReadException
- Throws:
ImageReadException
-
getMetadata
public ImageMetadata getMetadata(ByteSource byteSource, java.util.Map<java.lang.String,java.lang.Object> params) throws ImageReadException, java.io.IOException
Description copied from class:ImageParser
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.
- Specified by:
getMetadata
in classImageParser
- 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.java.io.IOException
- In the event of unsuccessful data read operation.
-
getComments
private java.util.List<java.lang.String> getComments(java.util.List<GifBlock> blocks) throws java.io.IOException
- Throws:
java.io.IOException
-
getImageInfo
public ImageInfo getImageInfo(ByteSource byteSource, java.util.Map<java.lang.String,java.lang.Object> params) throws ImageReadException, java.io.IOException
Description copied from class:ImageParser
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.
- Specified by:
getImageInfo
in classImageParser
- Parameters:
byteSource
- A valid ByteSource objectparams
- 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.java.io.IOException
- In the event of unsuccessful data access operation.
-
dumpImageFile
public boolean dumpImageFile(java.io.PrintWriter pw, ByteSource byteSource) throws ImageReadException, java.io.IOException
Description copied from class:ImageParser
Write the ImageInfo and format-specific information for the image content of the specified byte source to a PrintWriter- Overrides:
dumpImageFile
in classImageParser
- Parameters:
pw
- print writer used for writing the ImageInfobyteSource
- 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.java.io.IOException
- In the event of unsuccessful read or access operation.
-
getColorTable
private int[] getColorTable(byte[] bytes) throws ImageReadException
- Throws:
ImageReadException
-
getFormatCompliance
public FormatCompliance getFormatCompliance(ByteSource byteSource) throws ImageReadException, java.io.IOException
Description copied from class:ImageParser
Determines the format compliance of the content of the supplied byte source based on rules provided by a specific implementation.- Overrides:
getFormatCompliance
in classImageParser
- Parameters:
byteSource
- A valid instance of ByteSource- Returns:
- true if the content is format-compliant; otherwise, false
- Throws:
ImageReadException
- may be thrown by sub-classesjava.io.IOException
- may be thrown by sub-classes
-
findAllImageData
private java.util.List<GifImageData> findAllImageData(GifImageContents imageContents) throws ImageReadException
- Throws:
ImageReadException
-
findFirstImageData
private GifImageData findFirstImageData(GifImageContents imageContents) throws ImageReadException
- Throws:
ImageReadException
-
getBufferedImage
private java.awt.image.BufferedImage getBufferedImage(GifHeaderInfo headerInfo, GifImageData imageData, byte[] globalColorTable) throws ImageReadException
- Throws:
ImageReadException
-
getAllBufferedImages
public java.util.List<java.awt.image.BufferedImage> getAllBufferedImages(ByteSource byteSource) throws ImageReadException, java.io.IOException
Description copied from class:ImageParser
Gets all images specified by the byte source (some formats may include multiple images within a single data source).- Overrides:
getAllBufferedImages
in classImageParser
- 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.java.io.IOException
- In the event of unsuccessful read or access operation.
-
getBufferedImage
public java.awt.image.BufferedImage getBufferedImage(ByteSource byteSource, java.util.Map<java.lang.String,java.lang.Object> params) throws ImageReadException, java.io.IOException
Description copied from class:ImageParser
Gets a buffered image specified by the byte source (for sources that specify multiple images, choice of which image is returned is implementation dependent).- Specified by:
getBufferedImage
in classImageParser
- Parameters:
byteSource
- A valid instance of ByteSourceparams
- 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.java.io.IOException
- In the event of unsuccessful read or access operation.
-
writeAsSubBlocks
private void writeAsSubBlocks(java.io.OutputStream os, byte[] bytes) throws java.io.IOException
- Throws:
java.io.IOException
-
writeImage
public void writeImage(java.awt.image.BufferedImage src, java.io.OutputStream os, java.util.Map<java.lang.String,java.lang.Object> params) throws ImageWriteException, java.io.IOException
Description copied from class:ImageParser
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).
- Overrides:
writeImage
in classImageParser
- Parameters:
src
- An image giving the source content for outputos
- A valid output stream for storing the formatted imageparams
- 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.java.io.IOException
- In the event of an write error from the output stream.
-
getXmpXml
public java.lang.String getXmpXml(ByteSource byteSource, java.util.Map<java.lang.String,java.lang.Object> params) throws ImageReadException, java.io.IOException
Extracts embedded XML metadata as XML string.- Specified by:
getXmpXml
in interfaceXmpEmbeddable
- Parameters:
byteSource
- File containing image data.params
- Map of optional parameters, defined in ImagingConstants.- Returns:
- Xmp Xml as String, if present. Otherwise, returns null.
- Throws:
ImageReadException
- In the event that the specified content does not conform to the format of the specific parser implementation.java.io.IOException
- In the event of unsuccessful read or access operation.
-
-