null
+ */
+ private ImageParser getImageParser(String filename) {
+ if (filename == null) {
+ return null;
+ }
+
+ filename = filename.toLowerCase(Locale.ENGLISH);
+
+ final ImageParser[] imageParsers = ImageParser.getAllImageParsers();
+ for (final ImageParser imageParser : imageParsers) {
+ final String[] exts = imageParser.getAcceptedExtensions();
+
+ for (final String ext : exts) {
+ if (filename.endsWith(ext.toLowerCase(Locale.ENGLISH))) {
+ return imageParser;
+ }
+ }
+ }
+
+ return null;
+ }
+}
diff --git a/src/main/java/org/apache/commons/imaging/ImageConverterTask.java b/src/main/java/org/apache/commons/imaging/ImageConverterTask.java
new file mode 100644
index 000000000..b0496eddb
--- /dev/null
+++ b/src/main/java/org/apache/commons/imaging/ImageConverterTask.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging;
+
+import java.io.File;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Task;
+
+/**
+ * Ant task of image conversion
+ *
+ */
+public class ImageConverterTask extends Task {
+
+ /**path of the source image*/
+ private String src;
+
+ /**path of the destination image*/
+ private String dst;
+
+ /**image converter*/
+ private final ImageConverter delegate;
+
+ /**
+ * Constructor
+ */
+ public ImageConverterTask() {
+ super();
+ this.delegate = new ImageConverter();
+ }
+
+ @Override
+ public void execute() throws BuildException {
+ if (src == null) {
+ throw new BuildException("The src attribute cannot be null");
+ }
+ if (dst == null) {
+ throw new BuildException("The dst attribute cannot be null");
+ }
+ final File srcFile = new File(getProject().getBaseDir(), src);
+ final File dstFile = new File(getProject().getBaseDir(), dst);
+ try {
+ this.delegate.convertImage(srcFile, dstFile);
+ } catch (Throwable t) {
+ throw new BuildException("The conversion of the image file " + src
+ + " failed", t, getLocation());
+ }
+ }
+
+ /**
+ * Sets the path of the destination image
+ *
+ * @param dst
+ */
+ public void setDst(String dst) {
+ this.dst = dst;
+ }
+
+ /**
+ * Sets the path of the source image
+ *
+ * @param src
+ */
+ public void setSrc(String src) {
+ this.src = src;
+ }
+
+ public void setSourceImageIndex(final int sourceImageIndex) {
+ this.delegate.setSourceImageIndex(sourceImageIndex);
+ }
+
+ public void setIndexOfSmallestSourceImageComputationEnabled(
+ boolean indexOfSmallestSourceImageComputationEnabled) {
+ this.delegate.setIndexOfSmallestSourceImageComputationEnabled(indexOfSmallestSourceImageComputationEnabled);
+ }
+
+ public void setIndexOfBiggestSourceImageComputationEnabled(
+ boolean indexOfBiggestSourceImageComputationEnabled) {
+ this.delegate.setIndexOfBiggestSourceImageComputationEnabled(indexOfBiggestSourceImageComputationEnabled);
+ }
+
+ public void setSourceImageDuplicationAndRescaleEnabled(
+ boolean sourceImageDuplicationAndRescaleEnabled) {
+ this.delegate.setSourceImageDuplicationAndRescaleEnabled(sourceImageDuplicationAndRescaleEnabled);
+ }
+}
diff --git a/src/main/java/org/apache/commons/imaging/ImageParser.java b/src/main/java/org/apache/commons/imaging/ImageParser.java
index 164f9b06f..54efe1f17 100644
--- a/src/main/java/org/apache/commons/imaging/ImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/ImageParser.java
@@ -24,6 +24,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -52,936 +53,1103 @@
import org.apache.commons.imaging.formats.xpm.XpmImageParser;
/**
- * 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.
+ * 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.
*
* Additionally, developers creating or enhancing classes derived - * from ImageParser are encouraged to include such checks in their code. + *
+ * Additionally, developers creating or enhancing classes derived from + * ImageParser are encouraged to include such checks in their code. */ public abstract class ImageParser extends BinaryFileParser { - /** - * Gets an array of new instances of all image parsers. - * - * @return A valid array of image parsers - */ - public static ImageParser[] getAllImageParsers() { - - return new ImageParser[]{ - new BmpImageParser(), - new DcxImageParser(), - new GifImageParser(), - new IcnsImageParser(), - new IcoImageParser(), - new JpegImageParser(), - new PcxImageParser(), - new PngImageParser(), - new PnmImageParser(), - new PsdImageParser(), - new RgbeImageParser(), - new TiffImageParser(), - new WbmpImageParser(), - new XbmImageParser(), - new XpmImageParser(), - // new JBig2ImageParser(), - // new TgaImageParser(), - }; - } - - /** - * 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. - * - * @param byteSource A valid byte source. - * @return 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 the ByteSource - * content does not conform to the format of the specific parser - * implementation. - * @throws IOException In the event of unsuccessful data read operation. - */ - public final IImageMetadata getMetadata(final ByteSource byteSource) throws ImageReadException, IOException { - return getMetadata(byteSource, null); - } - - /** - * 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.
- *
- * @param byteSource A valid byte source.
- * @param params Optional instructions for special-handling or
- * interpretation of the input data (null objects are permitted and
- * must be supported by implementations).
- * @return 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 the ByteSource
- * content does not conform to the format of the specific parser
- * implementation.
- * @throws IOException In the event of unsuccessful data read operation.
- */
- public abstract IImageMetadata getMetadata(ByteSource byteSource, Map 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.
- *
- * @param bytes A valid array of bytes
- * @param params Optional instructions for special-handling or
- * interpretation of the input data (null objects are permitted and
- * must be supported by implementations).
- * @return A valid image metadata object describing the content extracted
- * from the specified content.
- * @throws ImageReadException In the event that the the specified content
- * does not conform to the format of the specific
- * parser implementation.
- * @throws IOException In the event of unsuccessful data read operation.
- */
- public final IImageMetadata getMetadata(final byte[] bytes, final Map 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.
- *
- * @param file A valid reference to a file.
- * @param params Optional instructions for special-handling or
- * interpretation of the input data (null objects are permitted and
- * must be supported by implementations).
- * @return A valid image metadata object describing the content extracted
- * from the specified content.
- * @throws ImageReadException In the event that the the specified content
- * does not conform to the format of the specific
- * parser implementation.
- * @throws IOException In the event of unsuccessful file read or
- * access operation.
- */
- public final IImageMetadata getMetadata(final File file, final Map
+ * 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.
+ *
+ * @param byteSource
+ * A valid byte source.
+ * @param params
+ * Optional instructions for special-handling or interpretation
+ * of the input data (null objects are permitted and must be
+ * supported by implementations).
+ * @return 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 the ByteSource content does not conform
+ * to the format of the specific parser implementation.
+ * @throws IOException
+ * In the event of unsuccessful data read operation.
+ */
+ public abstract IImageMetadata getMetadata(ByteSource byteSource,
+ Map
+ * 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.
+ *
+ * @param bytes
+ * A valid array of bytes
+ * @param params
+ * Optional instructions for special-handling or interpretation
+ * of the input data (null objects are permitted and must be
+ * supported by implementations).
+ * @return A valid image metadata object describing the content extracted
+ * from the specified content.
+ * @throws ImageReadException
+ * In the event that the the specified content does not conform
+ * to the format of the specific parser implementation.
+ * @throws IOException
+ * In the event of unsuccessful data read operation.
+ */
+ public final IImageMetadata getMetadata(final byte[] bytes,
+ final Map
+ * 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.
+ *
+ * @param file
+ * A valid reference to a file.
+ * @param params
+ * Optional instructions for special-handling or interpretation
+ * of the input data (null objects are permitted and must be
+ * supported by implementations).
+ * @return A valid image metadata object describing the content extracted
+ * from the specified content.
+ * @throws ImageReadException
+ * In the event that the the specified content does not conform
+ * to the format of the specific parser implementation.
+ * @throws IOException
+ * In the event of unsuccessful file read or access operation.
+ */
+ public final IImageMetadata getMetadata(final File file,
+ final Map
+ * 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.
+ *
+ * @param byteSource
+ * A valid ByteSource object
+ * @param params
+ * Optional instructions for special-handling or interpretation
+ * of the input data (null objects are permitted and must be
+ * supported by implementations).
+ * @return A valid image information object describing the content extracted
+ * from the specified data.
+ * @throws ImageReadException
+ * In the event that the the specified content does not conform
+ * to the format of the specific parser implementation.
+ * @throws IOException
+ * In the event of unsuccessful data access operation.
+ */
+ public abstract ImageInfo getImageInfo(ByteSource byteSource,
+ Map
+ * 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.
+ *
+ * @param bytes
+ * A valid array of bytes
+ * @param params
+ * Optional instructions for special-handling or interpretation
+ * of the input data (null objects are permitted and must be
+ * supported by implementations).
+ * @return A valid image information object describing the content extracted
+ * from the specified data.
+ * @throws ImageReadException
+ * In the event that the the specified content does not conform
+ * to the format of the specific parser implementation.
+ * @throws IOException
+ * In the event of unsuccessful data access operation.
+ */
+ public final ImageInfo getImageInfo(final byte[] bytes,
+ final Map
+ * 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.
+ *
+ * @param file
+ * A valid File object
+ * @param params
+ * Optional instructions for special-handling or interpretation
+ * of the input data (null objects are permitted and must be
+ * supported by implementations).
+ * @return A valid image information object describing the content extracted
+ * from the specified data.
+ * @throws ImageReadException
+ * In the event that the the specified content does not conform
+ * to the format of the specific parser implementation.
+ * @throws IOException
+ * In the event of unsuccessful file read or access operation.
+ */
+ public final ImageInfo getImageInfo(final File file,
+ final Map 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.
- *
- * @param byteSource A valid ByteSource object
- * @param params Optional instructions for special-handling or interpretation
- * of the input data (null objects are permitted and
- * must be supported by implementations).
- * @return A valid image information object describing the content extracted
- * from the specified data.
- * @throws ImageReadException In the event that the the specified content
- * does not conform to the format of the specific
- * parser implementation.
- * @throws IOException In the event of unsuccessful data access operation.
- */
- public abstract ImageInfo getImageInfo(ByteSource byteSource, Map 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.
- *
- * @param bytes A valid array of bytes
- * @param params Optional instructions for special-handling or
- * interpretation of the input data (null objects are permitted and
- * must be supported by implementations).
- * @return A valid image information object describing the content extracted
- * from the specified data.
- * @throws ImageReadException In the event that the the specified content
- * does not conform to the format of the specific
- * parser implementation.
- * @throws IOException In the event of unsuccessful data
- * access operation.
- */
- public final ImageInfo getImageInfo(final byte[] bytes, final Map 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.
- *
- * @param file A valid File object
- * @param params Optional instructions for special-handling or
- * interpretation of the input data (null objects are permitted and
- * must be supported by implementations).
- * @return A valid image information object describing the content extracted
- * from the specified data.
- * @throws ImageReadException In the event that the the specified content
- * does not conform to the format of the specific
- * parser implementation.
- * @throws IOException In the event of unsuccessful file read or
- * access operation.
- */
- public final ImageInfo getImageInfo(final File file, final Map 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).
- *
- * @param src An image giving the source content for output
- * @param os A valid output stream for storing the formatted image
- * @param 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.
- * @throws IOException In the event of an write error from
- * the output stream.
- */
- public void writeImage(final BufferedImage src, final OutputStream os, final Map
+ * 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).
+ *
+ * @param src
+ * An image giving the source content for output
+ * @param os
+ * A valid output stream for storing the formatted image
+ * @param 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.
+ * @throws IOException
+ * In the event of an write error from the output stream.
+ */
+ public void writeImage(final BufferedImage src, final OutputStream os,
+ final Map
+ * 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).
+ *
+ * @param srcs
+ * Some images giving the source content for output
+ * @param os
+ * A valid output stream for storing the formatted image
+ * @param 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.
+ * @throws IOException
+ * In the event of an write error from the output stream.
+ */
+ public void writeImages(final List
-
+ *
* All of methods provided by the Imaging class are declared static.
- * The Apache Commons Imaging package is a pure Java implementation.
+ * Almost all of the Apache Commons Imaging library's core functionality can be
+ * accessed through the methods provided by this class. The use of the Imaging
+ * class is similar to the Java API's ImageIO class, though Imaging supports
+ * formats and options not included in the standard Java API.
+ *
+ * All of methods provided by the Imaging class are declared static.
+ *
+ * The Apache Commons Imaging package is a pure Java implementation.
* Optional parameters are specified using a Map object (typically,
- * a Java HashMap) to specify a set of keys and values for input.
- * The specification for support keys is provided by the ImagingConstants
- * interface as well as by format-specific interfaces such as
- * JpegContants or TiffConstants.
+ * elements for special handling. If these specifications are not required by
+ * the application, the params argument may be omitted (as appropriate) or a
+ * null argument may be provided. In image-writing operations, the option
+ * parameters may include options such as data-compression type (if any), color
+ * model, or other format-specific data representations. The parameters map may
+ * also be used to provide EXIF Tags and other metadata to those formats that
+ * support them. In image-reading operations, the parameters may include
+ * information about special handling in reading the image data.
+ *
+ * Optional parameters are specified using a Map object (typically, a Java
+ * HashMap) to specify a set of keys and values for input. The specification for
+ * support keys is provided by the ImagingConstants interface as well as by
+ * format-specific interfaces such as JpegContants or TiffConstants.
* Many graphics format specify identifying byte
- * values that appear at the beginning of the data file. This method
- * checks for such identifying elements and returns a ImageFormat
- * enumeration indicating what it detects. Note that this
- * method can return "false positives" in cases where non-image files
- * begin with the specified byte values.
- *
- * @param bytes Byte array containing an image file.
- * @return An ImageFormat, such as ImageFormat.IMAGE_FORMAT_JPEG. Returns
- * ImageFormat.IMAGE_FORMAT_UNKNOWN if the image type cannot be
- * determined.
- */
- public static ImageFormat guessFormat(final byte[] bytes)
- throws ImageReadException, IOException {
- return guessFormat(new ByteSourceArray(bytes));
- }
-
- /**
- * Attempts to determine the image format of a file based on its
- * "magic numbers," the first bytes of the data.
- * Many graphics formats specify identifying byte
- * values that appear at the beginning of the data file. This method
- * checks for such identifying elements and returns a ImageFormat
- * enumeration indicating what it detects. Note that this
- * method can return "false positives" in cases where non-image files
- * begin with the specified byte values.
- *
- * @param file File containing image data.
- * @return An ImageFormat, such as ImageFormat.IMAGE_FORMAT_JPEG. Returns
- * ImageFormat.IMAGE_FORMAT_UNKNOWN if the image type cannot be
- * determined.
- */
- public static ImageFormat guessFormat(final File file) throws ImageReadException,
- IOException {
- return guessFormat(new ByteSourceFile(file));
- }
-
- private static boolean compareBytePair(final int[] a, final int[] b) {
- if (a.length != 2 && b.length != 2) {
- throw new RuntimeException("Invalid Byte Pair.");
- }
- return (a[0] == b[0]) && (a[1] == b[1]);
- }
-
-
- /**
- * Attempts to determine the image format of a file based on its
- * "magic numbers," the first bytes of the data.
- * Many graphics formats specify identifying byte
- * values that appear at the beginning of the data file. This method
- * checks for such identifying elements and returns a ImageFormat
- * enumeration indicating what it detects. Note that this
- * method can return "false positives" in cases where non-image files
- * begin with the specified byte values.
- *
- * @param byteSource a valid ByteSource object potentially supplying
- * data for an image.
- * @return An ImageFormat, such as ImageFormat.IMAGE_FORMAT_JPEG. Returns
- * ImageFormat.IMAGE_FORMAT_UNKNOWN if the image type cannot be
- * determined.
- * @throws ImageReadException in the event of an unsuccessful
- * attempt to read the image data
- * @throws IOException in the event of an unrecoverable I/O condition.
- */
- public static ImageFormat guessFormat(final ByteSource byteSource)
- throws ImageReadException, IOException {
-
- if (byteSource == null) {
- return ImageFormats.UNKNOWN;
- }
-
- InputStream is = null;
- boolean canThrow = false;
- try {
- is = byteSource.getInputStream();
-
- final int i1 = is.read();
- final int i2 = is.read();
- if ((i1 < 0) || (i2 < 0)) {
- throw new ImageReadException(
- "Couldn't read magic numbers to guess format.");
- }
-
- final int b1 = i1 & 0xff;
- final int b2 = i2 & 0xff;
- final int[] bytePair = { b1, b2, };
-
- if (compareBytePair(MAGIC_NUMBERS_GIF, bytePair)) {
- canThrow = true;
- return ImageFormats.GIF;
- }
- // else if (b1 == 0x00 && b2 == 0x00) // too similar to TGA
- // {
- // return ImageFormat.IMAGE_FORMAT_ICO;
- // }
- else if (compareBytePair(MAGIC_NUMBERS_PNG, bytePair)) {
- canThrow = true;
- return ImageFormats.PNG;
- } else if (compareBytePair(MAGIC_NUMBERS_JPEG, bytePair)) {
- canThrow = true;
- return ImageFormats.JPEG;
- } else if (compareBytePair(MAGIC_NUMBERS_BMP, bytePair)) {
- canThrow = true;
- return ImageFormats.BMP;
- } else if (compareBytePair(MAGIC_NUMBERS_TIFF_MOTOROLA, bytePair)) {
- canThrow = true;
- return ImageFormats.TIFF;
- } else if (compareBytePair(MAGIC_NUMBERS_TIFF_INTEL, bytePair)) {
- canThrow = true;
- return ImageFormats.TIFF;
- } else if (compareBytePair(MAGIC_NUMBERS_PSD, bytePair)) {
- canThrow = true;
- return ImageFormats.PSD;
- } else if (compareBytePair(MAGIC_NUMBERS_PAM, bytePair)) {
- canThrow = true;
- return ImageFormats.PAM;
- } else if (compareBytePair(MAGIC_NUMBERS_PBM_A, bytePair)) {
- canThrow = true;
- return ImageFormats.PBM;
- } else if (compareBytePair(MAGIC_NUMBERS_PBM_B, bytePair)) {
- canThrow = true;
- return ImageFormats.PBM;
- } else if (compareBytePair(MAGIC_NUMBERS_PGM_A, bytePair)) {
- canThrow = true;
- return ImageFormats.PGM;
- } else if (compareBytePair(MAGIC_NUMBERS_PGM_B, bytePair)) {
- canThrow = true;
- return ImageFormats.PGM;
- } else if (compareBytePair(MAGIC_NUMBERS_PPM_A, bytePair)) {
- canThrow = true;
- return ImageFormats.PPM;
- } else if (compareBytePair(MAGIC_NUMBERS_PPM_B, bytePair)) {
- canThrow = true;
- return ImageFormats.PPM;
- } else if (compareBytePair(MAGIC_NUMBERS_JBIG2_1, bytePair)) {
- final int i3 = is.read();
- final int i4 = is.read();
- if ((i3 < 0) || (i4 < 0)) {
- throw new ImageReadException(
- "Couldn't read magic numbers to guess format.");
- }
-
- final int b3 = i3 & 0xff;
- final int b4 = i4 & 0xff;
- final int[] bytePair2 = { b3, b4, };
- if (compareBytePair(MAGIC_NUMBERS_JBIG2_2, bytePair2)) {
- canThrow = true;
- return ImageFormats.JBIG2;
- }
- } else if (compareBytePair(MAGIC_NUMBERS_ICNS, bytePair)) {
- canThrow = true;
- return ImageFormats.ICNS;
- } else if (compareBytePair(MAGIC_NUMBERS_DCX, bytePair)) {
- canThrow = true;
- return ImageFormats.DCX;
- } else if (compareBytePair(MAGIC_NUMBERS_RGBE, bytePair)) {
- canThrow = true;
- return ImageFormats.RGBE;
- }
- canThrow = true;
- return ImageFormats.UNKNOWN;
- } finally {
- IoUtils.closeQuietly(canThrow, is);
- }
- }
-
- /**
- * Extracts an ICC Profile (if present) from JPEG, PNG, PSD (Photoshop) and
- * TIFF images.
- *
- *
- * @param bytes
- * Byte array containing an image file.
- * @return An instance of ICC_Profile or null if the image contains no ICC
- * profile.
- */
- public static ICC_Profile getICCProfile(final byte[] bytes)
- throws ImageReadException, IOException {
- return getICCProfile(bytes, null);
- }
-
- /**
- * Extracts an ICC Profile (if present) from JPEG, PNG, PSD (Photoshop) and
- * TIFF images.
- *
- *
- * @param bytes
- * Byte array containing an image file.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return An instance of ICC_Profile or null if the image contains no ICC
- * profile..
- */
- public static ICC_Profile getICCProfile(final byte[] bytes, final Map
- *
- * @param is
- * InputStream from which to read image data.
- * @param filename
- * Filename associated with image data (optional).
- * @return An instance of ICC_Profile or null if the image contains no ICC
- * profile..
- */
- public static ICC_Profile getICCProfile(final InputStream is, final String filename)
- throws ImageReadException, IOException {
- return getICCProfile(is, filename, null);
- }
-
- /**
- * Extracts an ICC Profile (if present) from JPEG, PNG, PSD (Photoshop) and
- * TIFF images.
- *
- *
- * @param is
- * InputStream from which to read image data.
- * @param filename
- * Filename associated with image data (optional).
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return An instance of ICC_Profile or null if the image contains no ICC
- * profile..
- */
- public static ICC_Profile getICCProfile(final InputStream is, final String filename,
- final Map
- *
- * @param file
- * File containing image data.
- * @return An instance of ICC_Profile or null if the image contains no ICC
- * profile..
- */
- public static ICC_Profile getICCProfile(final File file)
- throws ImageReadException, IOException {
- return getICCProfile(file, null);
- }
-
- /**
- * Extracts an ICC Profile (if present) from JPEG, PNG, PSD (Photoshop) and
- * TIFF images.
- *
- *
- * @param file
- * File containing image data.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return An instance of ICC_Profile or null if the image contains no ICC
- * profile..
- */
- public static ICC_Profile getICCProfile(final File file, final Map
- * To parse the result use IccProfileParser or
- * ICC_Profile.getInstance(bytes).
- *
- *
- * @param bytes
- * Byte array containing an image file.
- * @return A byte array.
- * @see IccProfileParser
- * @see ICC_Profile
- */
- public static byte[] getICCProfileBytes(final byte[] bytes)
- throws ImageReadException, IOException {
- return getICCProfileBytes(bytes, null);
- }
-
- /**
- * Extracts the raw bytes of an ICC Profile (if present) from JPEG, PNG, PSD
- * (Photoshop) and TIFF images.
- *
- * To parse the result use IccProfileParser or
- * ICC_Profile.getInstance(bytes).
- *
- *
- * @param bytes
- * Byte array containing an image file.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return A byte array.
- * @see IccProfileParser
- * @see ICC_Profile
- */
- public static byte[] getICCProfileBytes(final byte[] bytes, final Map
- * To parse the result use IccProfileParser or
- * ICC_Profile.getInstance(bytes).
- *
- *
- * @param file
- * File containing image data.
- * @return A byte array.
- * @see IccProfileParser
- * @see ICC_Profile
- */
- public static byte[] getICCProfileBytes(final File file)
- throws ImageReadException, IOException {
- return getICCProfileBytes(file, null);
- }
-
- /**
- * Extracts the raw bytes of an ICC Profile (if present) from JPEG, PNG, PSD
- * (Photoshop) and TIFF images.
- *
- * To parse the result use IccProfileParser or
- * ICC_Profile.getInstance(bytes).
- *
- *
- * @param file
- * File containing image data.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return A byte array.
- * @see IccProfileParser
- * @see ICC_Profile
- */
- public static byte[] getICCProfileBytes(final File file, final Map
- * "Image info" is a summary of basic information about the image such as:
- * width, height, file format, bit depth, color type, etc.
- *
- * Not to be confused with "image metadata."
- *
- *
- * @param filename
- * String.
- * @param bytes
- * Byte array containing an image file.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return An instance of ImageInfo.
- * @see ImageInfo
- */
- public static ImageInfo getImageInfo(final String filename, final byte[] bytes,
- final Map
- * "Image info" is a summary of basic information about the image such as:
- * width, height, file format, bit depth, color type, etc.
- *
- * Not to be confused with "image metadata."
- *
- *
- * @param filename
- * String.
- * @param bytes
- * Byte array containing an image file.
- * @return An instance of ImageInfo.
- * @see ImageInfo
- */
- public static ImageInfo getImageInfo(final String filename, final byte[] bytes)
- throws ImageReadException, IOException {
- return getImageInfo(new ByteSourceArray(filename, bytes), null);
- }
-
- /**
- * Parses the "image info" of an image.
- *
- * "Image info" is a summary of basic information about the image such as:
- * width, height, file format, bit depth, color type, etc.
- *
- * Not to be confused with "image metadata."
- *
- *
- * @param is
- * InputStream from which to read image data.
- * @param filename
- * Filename associated with image data (optional).
- * @return An instance of ImageInfo.
- * @see ImageInfo
- */
- public static ImageInfo getImageInfo(final InputStream is, final String filename)
- throws ImageReadException, IOException {
- return getImageInfo(new ByteSourceInputStream(is, filename), null);
- }
-
- /**
- * Parses the "image info" of an image.
- *
- * "Image info" is a summary of basic information about the image such as:
- * width, height, file format, bit depth, color type, etc.
- *
- * Not to be confused with "image metadata."
- *
- *
- * @param is
- * InputStream from which to read image data.
- * @param filename
- * Filename associated with image data (optional).
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return An instance of ImageInfo.
- * @see ImageInfo
- */
- public static ImageInfo getImageInfo(final InputStream is, final String filename,
- final Map
- * "Image info" is a summary of basic information about the image such as:
- * width, height, file format, bit depth, color type, etc.
- *
- * Not to be confused with "image metadata."
- *
- *
- * @param bytes
- * Byte array containing an image file.
- * @return An instance of ImageInfo.
- * @see ImageInfo
- */
- public static ImageInfo getImageInfo(final byte[] bytes)
- throws ImageReadException, IOException {
- return getImageInfo(new ByteSourceArray(bytes), null);
- }
-
- /**
- * Parses the "image info" of an image.
- *
- * "Image info" is a summary of basic information about the image such as:
- * width, height, file format, bit depth, color type, etc.
- *
- * Not to be confused with "image metadata."
- *
- *
- * @param bytes
- * Byte array containing an image file.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return An instance of ImageInfo.
- * @see ImageInfo
- */
- public static ImageInfo getImageInfo(final byte[] bytes, final Map
- * "Image info" is a summary of basic information about the image such as:
- * width, height, file format, bit depth, color type, etc.
- *
- * Not to be confused with "image metadata."
- *
- *
- * @param file
- * File containing image data.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return An instance of ImageInfo.
- * @see ImageInfo
- */
- public static ImageInfo getImageInfo(final File file, final Map
- * "Image info" is a summary of basic information about the image such as:
- * width, height, file format, bit depth, color type, etc.
- *
- * Not to be confused with "image metadata."
- *
- *
- * @param file
- * File containing image data.
- * @return An instance of ImageInfo.
- * @see ImageInfo
- */
- public static ImageInfo getImageInfo(final File file) throws ImageReadException,
- IOException {
- return getImageInfo(file, null);
- }
-
- private static ImageInfo getImageInfo(final ByteSource byteSource, final Map
- *
- * @param is
- * InputStream from which to read image data.
- * @param filename
- * Filename associated with image data (optional).
- * @return The width and height of the image.
- */
- public static Dimension getImageSize(final InputStream is, final String filename)
- throws ImageReadException, IOException {
- return getImageSize(is, filename, null);
- }
-
- /**
- * Determines the width and height of an image.
- *
- *
- * @param is
- * InputStream from which to read image data.
- * @param filename
- * Filename associated with image data (optional).
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return The width and height of the image.
- */
- public static Dimension getImageSize(final InputStream is, final String filename,
- final Map
- *
- * @param bytes
- * Byte array containing an image file.
- * @return The width and height of the image.
- */
- public static Dimension getImageSize(final byte[] bytes)
- throws ImageReadException, IOException {
- return getImageSize(bytes, null);
- }
-
- /**
- * Determines the width and height of an image.
- *
- *
- * @param bytes
- * Byte array containing an image file.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return The width and height of the image.
- */
- public static Dimension getImageSize(final byte[] bytes, final Map
- *
- * @param file
- * File containing image data.
- * @return The width and height of the image.
- */
- public static Dimension getImageSize(final File file) throws ImageReadException,
- IOException {
- return getImageSize(file, null);
- }
-
- /**
- * Determines the width and height of an image file.
- *
- *
- * @param file
- * File containing image data.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return The width and height of the image.
- */
- public static Dimension getImageSize(final File file, final Map
- *
- * @param is
- * InputStream from which to read image data.
- * @param filename
- * Filename associated with image data (optional).
- * @return Xmp Xml as String, if present. Otherwise, returns null.
- */
- public static String getXmpXml(final InputStream is, final String filename)
- throws ImageReadException, IOException {
- return getXmpXml(is, filename, null);
- }
-
- /**
- * Extracts the embedded XML metadata as an XML string.
- *
- *
- * @param is
- * InputStream from which to read image data.
- * @param filename
- * Filename associated with image data (optional).
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return Xmp Xml as String, if present. Otherwise, returns null.
- */
- public static String getXmpXml(final InputStream is, final String filename, final Map
- *
- * @param bytes
- * Byte array containing an image file.
- * @return Xmp Xml as String, if present. Otherwise, returns null.
- */
- public static String getXmpXml(final byte[] bytes) throws ImageReadException,
- IOException {
- return getXmpXml(bytes, null);
- }
-
- /**
- * Extracts the embedded XML metadata as an XML string.
- *
- *
- * @param bytes
- * Byte array containing an image file.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return Xmp Xml as String, if present. Otherwise, returns null.
- */
- public static String getXmpXml(final byte[] bytes, final Map
- *
- * @param file
- * File containing image data.
- * @return Xmp Xml as String, if present. Otherwise, returns null.
- */
- public static String getXmpXml(final File file) throws ImageReadException,
- IOException {
- return getXmpXml(file, null);
- }
-
- /**
- * Extracts the embedded XML metadata as an XML string.
- *
- *
- * @param file
- * File containing image data.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return Xmp Xml as String, if present. Otherwise, returns null.
- */
- public static String getXmpXml(final File file, final Map
- *
- * @param byteSource
- * File containing image data.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return Xmp Xml as String, if present. Otherwise, returns null.
- */
- public static String getXmpXml(final ByteSource byteSource, final Map
- * JPEG/JFIF files may contain EXIF and/or IPTC metadata. PNG files may
- * contain comments. TIFF files may contain metadata.
- *
- * The instance of IImageMetadata returned by getMetadata() should be upcast
- * (depending on image format).
- *
- * Not to be confused with "image info."
- *
- *
- * @param bytes
- * Byte array containing an image file.
- * @return An instance of IImageMetadata.
- * @see IImageMetadata
- */
- public static IImageMetadata getMetadata(final byte[] bytes)
- throws ImageReadException, IOException {
- return getMetadata(bytes, null);
- }
-
- /**
- * Parses the metadata of an image. This metadata depends on the format of
- * the image.
- *
- * JPEG/JFIF files may contain EXIF and/or IPTC metadata. PNG files may
- * contain comments. TIFF files may contain metadata.
- *
- * The instance of IImageMetadata returned by getMetadata() should be upcast
- * (depending on image format).
- *
- * Not to be confused with "image info."
- *
- *
- * @param bytes
- * Byte array containing an image file.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return An instance of IImageMetadata.
- * @see IImageMetadata
- */
- public static IImageMetadata getMetadata(final byte[] bytes, final Map
- * JPEG/JFIF files may contain EXIF and/or IPTC metadata. PNG files may
- * contain comments. TIFF files may contain metadata.
- *
- * The instance of IImageMetadata returned by getMetadata() should be upcast
- * (depending on image format).
- *
- * Not to be confused with "image info."
- *
- *
- * @param is
- * InputStream from which to read image data.
- * @param filename
- * Filename associated with image data (optional).
- * @return An instance of IImageMetadata.
- * @see IImageMetadata
- */
- public static IImageMetadata getMetadata(final InputStream is, final String filename)
- throws ImageReadException, IOException {
- return getMetadata(is, filename, null);
- }
-
- /**
- * Parses the metadata of an image file. This metadata depends on the format
- * of the image.
- *
- * JPEG/JFIF files may contain EXIF and/or IPTC metadata. PNG files may
- * contain comments. TIFF files may contain metadata.
- *
- * The instance of IImageMetadata returned by getMetadata() should be upcast
- * (depending on image format).
- *
- * Not to be confused with "image info."
- *
- *
- * @param is
- * InputStream from which to read image data.
- * @param filename
- * Filename associated with image data (optional).
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return An instance of IImageMetadata.
- * @see IImageMetadata
- */
- public static IImageMetadata getMetadata(final InputStream is, final String filename,
- final Map
- * JPEG/JFIF files may contain EXIF and/or IPTC metadata. PNG files may
- * contain comments. TIFF files may contain metadata.
- *
- * The instance of IImageMetadata returned by getMetadata() should be upcast
- * (depending on image format).
- *
- * Not to be confused with "image info."
- *
- *
- * @param file
- * File containing image data.
- * @return An instance of IImageMetadata.
- * @see IImageMetadata
- */
- public static IImageMetadata getMetadata(final File file)
- throws ImageReadException, IOException {
- return getMetadata(file, null);
- }
-
- /**
- * Parses the metadata of an image file. This metadata depends on the format
- * of the image.
- *
- * JPEG/JFIF files may contain EXIF and/or IPTC metadata. PNG files may
- * contain comments. TIFF files may contain metadata.
- *
- * The instance of IImageMetadata returned by getMetadata() should be upcast
- * (depending on image format).
- *
- * Not to be confused with "image info."
- *
- *
- * @param file
- * File containing image data.
- * @param params
- * Map of optional parameters, defined in ImagingConstants.
- * @return An instance of IImageMetadata.
- * @see IImageMetadata
- */
- public static IImageMetadata getMetadata(final File file, final Map
- * For the most recent information on support for specific formats, refer to
- * Format Support
- * at the main project development web site. While the Apache Commons
- * Imaging package does not fully support all formats, it can read
- * image info, metadata and ICC profiles from all image formats that
- * provide this data.
- * @param is a valid ImageStream from which to read data.
- * @return if successful, a valid buffered image
- * @throws ImageReadException in the event of a processing error
- * while reading an image (i.e. a format violation, etc.).
- * @throws IOException in the event of an unrecoverable I/O exception.
- */
-
- public static BufferedImage getBufferedImage(final InputStream is)
- throws ImageReadException, IOException {
- return getBufferedImage(is, null);
- }
-
-
-
- /**
- * Reads the first image from an InputStream
- * using data-processing options specified through a parameters
- * map. Options may be configured using the ImagingContants
- * interface or the various format-specific implementations provided
- * by this package.
- *
- * For the most recent information on support for specific formats, refer to
- * Format Support
- * at the main project development web site. While the Apache Commons
- * Imaging package does not fully support all formats, it can read
- * image info, metadata and ICC profiles from all image formats that
- * provide this data.
- * @param is a valid ImageStream from which to read data.
- * @param params an optional parameters map specifying options
- * @return if successful, a valid buffered image
- * @throws ImageReadException in the event of a processing error
- * while reading an image (i.e. a format violation, etc.).
- * @throws IOException in the event of an unrecoverable I/O exception.
- */
- public static BufferedImage getBufferedImage(final InputStream is, final Map
- * For the most recent information on support for specific formats, refer to
- * Format Support
- * at the main project development web site. While the Apache Commons
- * Imaging package does not fully support all formats, it can read
- * image info, metadata and ICC profiles from all image formats that
- * provide this data.
- * @param bytes a valid array of bytes from which to read data.
- * @return if successful, a valid buffered image
- * @throws ImageReadException in the event of a processing error
- * while reading an image (i.e. a format violation, etc.).
- * @throws IOException in the event of an unrecoverable I/O exception.
- */
- public static BufferedImage getBufferedImage(final byte[] bytes)
- throws ImageReadException, IOException {
- return getBufferedImage(new ByteSourceArray(bytes), null);
- }
-
-
- /**
- * Reads the first image from a byte array
- * using data-processing options specified through a parameters
- * map. Options may be configured using the ImagingContants
- * interface or the various format-specific implementations provided
- * by this package.
- *
- * For the most recent information on support for specific formats, refer to
- * Format Support
- * at the main project development web site. While the Apache Commons
- * Imaging package does not fully support all formats, it can read
- * image info, metadata and ICC profiles from all image formats that
- * provide this data.
- * @param bytes a valid array of bytes from which to read data.
- * @param params an optional parameters map specifying options.
- * @return if successful, a valid buffered image
- * @throws ImageReadException in the event of a processing error
- * while reading an image (i.e. a format violation, etc.).
- * @throws IOException in the event of an unrecoverable I/O exception.
- */
- public static BufferedImage getBufferedImage(final byte[] bytes, final Map
- * For the most recent information on support for specific formats, refer to
- * Format Support
- * at the main project development web site. While the Apache Commons
- * Imaging package does not fully support all formats, it can read
- * image info, metadata and ICC profiles from all image formats that
- * provide this data.
- * @param file a valid reference to a file containing image data.
- * @return if successful, a valid buffered image
- * @throws ImageReadException in the event of a processing error
- * while reading an image (i.e. a format violation, etc.).
- * @throws IOException in the event of an unrecoverable I/O exception.
- */
- public static BufferedImage getBufferedImage(final File file)
- throws ImageReadException, IOException {
- return getBufferedImage(new ByteSourceFile(file), null);
- }
-
-
- /**
- * Reads the first image from a file
- * using data-processing options specified through a parameters
- * map. Options may be configured using the ImagingContants
- * interface or the various format-specific implementations provided
- * by this package.
- *
- * For the most recent information on support for specific formats, refer to
- * Format Support
- * at the main project development web site. While the Apache Commons
- * Imaging package does not fully support all formats, it can read
- * image info, metadata and ICC profiles from all image formats that
- * provide this data.
- * @param file a valid reference to a file containing image data.
- * @return if successful, a valid buffered image
- * @throws ImageReadException in the event of a processing error
- * while reading an image (i.e. a format violation, etc.).
- * @throws IOException in the event of an unrecoverable I/O exception.
- */
- public static BufferedImage getBufferedImage(final File file, final Map
- * Image writing is not supported for all graphics formats.
- * For the most recent information on support for specific formats, refer to
- * Format Support
- * at the main project development web site. While the Apache Commons
- * Imaging package does not fully support all formats, it can read
- * image info, metadata and ICC profiles from all image formats that
- * provide this data.
- * @param src a valid BufferedImage object
- * @param file the file to which the output image is to be written
- * @param format the format in which the output image is to be written
- * @param params an optional parameters map (nulls permitted)
- * @throws ImageWriteException in the event of a format violation,
- * unsupported image format, etc.
- * @throws IOException in the event of an unrecoverable I/O exception.
- * @see ImagingConstants
- */
- public static void writeImage(final BufferedImage src, final File file,
- final ImageFormat format, final Map
- * Image writing is not supported for all graphics formats.
- * For the most recent information on support for specific formats, refer to
- * Format Support
- * at the main project development web site. While the Apache Commons
- * Imaging package does not fully support all formats, it can read
- * image info, metadata and ICC profiles from all image formats that
- * provide this data.
- * @param src a valid BufferedImage object
- * @param format the format in which the output image is to be written
- * @param params an optional parameters map (nulls permitted)
- * @return if successful, a valid array of bytes.
- * @throws ImageWriteException in the event of a format violation,
- * unsupported image format, etc.
- * @throws IOException in the event of an unrecoverable I/O exception.
- * @see ImagingConstants
- */
- public static byte[] writeImageToBytes(final BufferedImage src,
- final ImageFormat format, final Map
- * Image writing is not supported for all graphics formats.
- * For the most recent information on support for specific formats, refer to
- * Format Support
- * at the main project development web site. While the Apache Commons
- * Imaging package does not fully support all formats, it can read
- * image info, metadata and ICC profiles from all image formats that
- * provide this data.
- * @param src a valid BufferedImage object
- * @param os the OutputStream to which the output image is to be written
- * @param format the format in which the output image is to be written
- * @param params an optional parameters map (nulls permitted)
- * @throws ImageWriteException in the event of a format violation,
- * unsupported image format, etc.
- * @throws IOException in the event of an unrecoverable I/O exception.
- * @see ImagingConstants
- */
- public static void writeImage(final BufferedImage src, final OutputStream os,
- final ImageFormat format, Map
+ * Many graphics format specify identifying byte values that appear at the
+ * beginning of the data file. This method checks for such identifying
+ * elements and returns a ImageFormat enumeration indicating what it
+ * detects. Note that this method can return "false positives" in cases
+ * where non-image files begin with the specified byte values.
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @return An ImageFormat, such as ImageFormat.IMAGE_FORMAT_JPEG. Returns
+ * ImageFormat.IMAGE_FORMAT_UNKNOWN if the image type cannot be
+ * determined.
+ */
+ public static ImageFormat guessFormat(final byte[] bytes)
+ throws ImageReadException, IOException {
+ return guessFormat(new ByteSourceArray(bytes));
+ }
+
+ /**
+ * Attempts to determine the image format of a file based on its
+ * "magic numbers," the first bytes of the data.
+ *
+ * Many graphics formats specify identifying byte values that appear at the
+ * beginning of the data file. This method checks for such identifying
+ * elements and returns a ImageFormat enumeration indicating what it
+ * detects. Note that this method can return "false positives" in cases
+ * where non-image files begin with the specified byte values.
+ *
+ * @param file
+ * File containing image data.
+ * @return An ImageFormat, such as ImageFormat.IMAGE_FORMAT_JPEG. Returns
+ * ImageFormat.IMAGE_FORMAT_UNKNOWN if the image type cannot be
+ * determined.
+ */
+ public static ImageFormat guessFormat(final File file)
+ throws ImageReadException, IOException {
+ return guessFormat(new ByteSourceFile(file));
+ }
+
+ private static boolean compareBytePair(final int[] a, final int[] b) {
+ if (a.length != 2 && b.length != 2) {
+ throw new RuntimeException("Invalid Byte Pair.");
+ }
+ return (a[0] == b[0]) && (a[1] == b[1]);
+ }
+
+ /**
+ * Attempts to determine the image format of a file based on its
+ * "magic numbers," the first bytes of the data.
+ *
+ * Many graphics formats specify identifying byte values that appear at the
+ * beginning of the data file. This method checks for such identifying
+ * elements and returns a ImageFormat enumeration indicating what it
+ * detects. Note that this method can return "false positives" in cases
+ * where non-image files begin with the specified byte values.
+ *
+ * @param byteSource
+ * a valid ByteSource object potentially supplying data for an
+ * image.
+ * @return An ImageFormat, such as ImageFormat.IMAGE_FORMAT_JPEG. Returns
+ * ImageFormat.IMAGE_FORMAT_UNKNOWN if the image type cannot be
+ * determined.
+ * @throws ImageReadException
+ * in the event of an unsuccessful attempt to read the image
+ * data
+ * @throws IOException
+ * in the event of an unrecoverable I/O condition.
+ */
+ public static ImageFormat guessFormat(final ByteSource byteSource)
+ throws ImageReadException, IOException {
+
+ if (byteSource == null) {
+ return ImageFormats.UNKNOWN;
+ }
+
+ InputStream is = null;
+ boolean canThrow = false;
+ try {
+ is = byteSource.getInputStream();
+
+ final int i1 = is.read();
+ final int i2 = is.read();
+ if ((i1 < 0) || (i2 < 0)) {
+ throw new ImageReadException(
+ "Couldn't read magic numbers to guess format.");
+ }
+
+ final int b1 = i1 & 0xff;
+ final int b2 = i2 & 0xff;
+ final int[] bytePair = { b1, b2, };
+
+ if (compareBytePair(MAGIC_NUMBERS_GIF, bytePair)) {
+ canThrow = true;
+ return ImageFormats.GIF;
+ }
+ // else if (b1 == 0x00 && b2 == 0x00) // too similar to TGA
+ // {
+ // return ImageFormat.IMAGE_FORMAT_ICO;
+ // }
+ else if (compareBytePair(MAGIC_NUMBERS_PNG, bytePair)) {
+ canThrow = true;
+ return ImageFormats.PNG;
+ } else if (compareBytePair(MAGIC_NUMBERS_JPEG, bytePair)) {
+ canThrow = true;
+ return ImageFormats.JPEG;
+ } else if (compareBytePair(MAGIC_NUMBERS_BMP, bytePair)) {
+ canThrow = true;
+ return ImageFormats.BMP;
+ } else if (compareBytePair(MAGIC_NUMBERS_TIFF_MOTOROLA, bytePair)) {
+ canThrow = true;
+ return ImageFormats.TIFF;
+ } else if (compareBytePair(MAGIC_NUMBERS_TIFF_INTEL, bytePair)) {
+ canThrow = true;
+ return ImageFormats.TIFF;
+ } else if (compareBytePair(MAGIC_NUMBERS_PSD, bytePair)) {
+ canThrow = true;
+ return ImageFormats.PSD;
+ } else if (compareBytePair(MAGIC_NUMBERS_PAM, bytePair)) {
+ canThrow = true;
+ return ImageFormats.PAM;
+ } else if (compareBytePair(MAGIC_NUMBERS_PBM_A, bytePair)) {
+ canThrow = true;
+ return ImageFormats.PBM;
+ } else if (compareBytePair(MAGIC_NUMBERS_PBM_B, bytePair)) {
+ canThrow = true;
+ return ImageFormats.PBM;
+ } else if (compareBytePair(MAGIC_NUMBERS_PGM_A, bytePair)) {
+ canThrow = true;
+ return ImageFormats.PGM;
+ } else if (compareBytePair(MAGIC_NUMBERS_PGM_B, bytePair)) {
+ canThrow = true;
+ return ImageFormats.PGM;
+ } else if (compareBytePair(MAGIC_NUMBERS_PPM_A, bytePair)) {
+ canThrow = true;
+ return ImageFormats.PPM;
+ } else if (compareBytePair(MAGIC_NUMBERS_PPM_B, bytePair)) {
+ canThrow = true;
+ return ImageFormats.PPM;
+ } else if (compareBytePair(MAGIC_NUMBERS_JBIG2_1, bytePair)) {
+ final int i3 = is.read();
+ final int i4 = is.read();
+ if ((i3 < 0) || (i4 < 0)) {
+ throw new ImageReadException(
+ "Couldn't read magic numbers to guess format.");
+ }
+
+ final int b3 = i3 & 0xff;
+ final int b4 = i4 & 0xff;
+ final int[] bytePair2 = { b3, b4, };
+ if (compareBytePair(MAGIC_NUMBERS_JBIG2_2, bytePair2)) {
+ canThrow = true;
+ return ImageFormats.JBIG2;
+ }
+ } else if (compareBytePair(MAGIC_NUMBERS_ICNS, bytePair)) {
+ canThrow = true;
+ return ImageFormats.ICNS;
+ } else if (compareBytePair(MAGIC_NUMBERS_DCX, bytePair)) {
+ canThrow = true;
+ return ImageFormats.DCX;
+ } else if (compareBytePair(MAGIC_NUMBERS_RGBE, bytePair)) {
+ canThrow = true;
+ return ImageFormats.RGBE;
+ }
+ canThrow = true;
+ return ImageFormats.UNKNOWN;
+ } finally {
+ IoUtils.closeQuietly(canThrow, is);
+ }
+ }
+
+ /**
+ * Extracts an ICC Profile (if present) from JPEG, PNG, PSD (Photoshop) and
+ * TIFF images.
+ *
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @return An instance of ICC_Profile or null if the image contains no ICC
+ * profile.
+ */
+ public static ICC_Profile getICCProfile(final byte[] bytes)
+ throws ImageReadException, IOException {
+ return getICCProfile(bytes, null);
+ }
+
+ /**
+ * Extracts an ICC Profile (if present) from JPEG, PNG, PSD (Photoshop) and
+ * TIFF images.
+ *
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return An instance of ICC_Profile or null if the image contains no ICC
+ * profile..
+ */
+ public static ICC_Profile getICCProfile(final byte[] bytes,
+ final Map
+ *
+ * @param is
+ * InputStream from which to read image data.
+ * @param filename
+ * Filename associated with image data (optional).
+ * @return An instance of ICC_Profile or null if the image contains no ICC
+ * profile..
+ */
+ public static ICC_Profile getICCProfile(final InputStream is,
+ final String filename) throws ImageReadException, IOException {
+ return getICCProfile(is, filename, null);
+ }
+
+ /**
+ * Extracts an ICC Profile (if present) from JPEG, PNG, PSD (Photoshop) and
+ * TIFF images.
+ *
+ *
+ * @param is
+ * InputStream from which to read image data.
+ * @param filename
+ * Filename associated with image data (optional).
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return An instance of ICC_Profile or null if the image contains no ICC
+ * profile..
+ */
+ public static ICC_Profile getICCProfile(final InputStream is,
+ final String filename, final Map
+ *
+ * @param file
+ * File containing image data.
+ * @return An instance of ICC_Profile or null if the image contains no ICC
+ * profile..
+ */
+ public static ICC_Profile getICCProfile(final File file)
+ throws ImageReadException, IOException {
+ return getICCProfile(file, null);
+ }
+
+ /**
+ * Extracts an ICC Profile (if present) from JPEG, PNG, PSD (Photoshop) and
+ * TIFF images.
+ *
+ *
+ * @param file
+ * File containing image data.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return An instance of ICC_Profile or null if the image contains no ICC
+ * profile..
+ */
+ public static ICC_Profile getICCProfile(final File file,
+ final Map
+ * To parse the result use IccProfileParser or
+ * ICC_Profile.getInstance(bytes).
+ *
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @return A byte array.
+ * @see IccProfileParser
+ * @see ICC_Profile
+ */
+ public static byte[] getICCProfileBytes(final byte[] bytes)
+ throws ImageReadException, IOException {
+ return getICCProfileBytes(bytes, null);
+ }
+
+ /**
+ * Extracts the raw bytes of an ICC Profile (if present) from JPEG, PNG, PSD
+ * (Photoshop) and TIFF images.
+ *
+ * To parse the result use IccProfileParser or
+ * ICC_Profile.getInstance(bytes).
+ *
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return A byte array.
+ * @see IccProfileParser
+ * @see ICC_Profile
+ */
+ public static byte[] getICCProfileBytes(final byte[] bytes,
+ final Map
+ * To parse the result use IccProfileParser or
+ * ICC_Profile.getInstance(bytes).
+ *
+ *
+ * @param file
+ * File containing image data.
+ * @return A byte array.
+ * @see IccProfileParser
+ * @see ICC_Profile
+ */
+ public static byte[] getICCProfileBytes(final File file)
+ throws ImageReadException, IOException {
+ return getICCProfileBytes(file, null);
+ }
+
+ /**
+ * Extracts the raw bytes of an ICC Profile (if present) from JPEG, PNG, PSD
+ * (Photoshop) and TIFF images.
+ *
+ * To parse the result use IccProfileParser or
+ * ICC_Profile.getInstance(bytes).
+ *
+ *
+ * @param file
+ * File containing image data.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return A byte array.
+ * @see IccProfileParser
+ * @see ICC_Profile
+ */
+ public static byte[] getICCProfileBytes(final File file,
+ final Map
+ * "Image info" is a summary of basic information about the image such as:
+ * width, height, file format, bit depth, color type, etc.
+ *
+ * Not to be confused with "image metadata."
+ *
+ *
+ * @param filename
+ * String.
+ * @param bytes
+ * Byte array containing an image file.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return An instance of ImageInfo.
+ * @see ImageInfo
+ */
+ public static ImageInfo getImageInfo(final String filename,
+ final byte[] bytes, final Map
+ * "Image info" is a summary of basic information about the image such as:
+ * width, height, file format, bit depth, color type, etc.
+ *
+ * Not to be confused with "image metadata."
+ *
+ *
+ * @param filename
+ * String.
+ * @param bytes
+ * Byte array containing an image file.
+ * @return An instance of ImageInfo.
+ * @see ImageInfo
+ */
+ public static ImageInfo getImageInfo(final String filename,
+ final byte[] bytes) throws ImageReadException, IOException {
+ return getImageInfo(new ByteSourceArray(filename, bytes), null);
+ }
+
+ /**
+ * Parses the "image info" of an image.
+ *
+ * "Image info" is a summary of basic information about the image such as:
+ * width, height, file format, bit depth, color type, etc.
+ *
+ * Not to be confused with "image metadata."
+ *
+ *
+ * @param is
+ * InputStream from which to read image data.
+ * @param filename
+ * Filename associated with image data (optional).
+ * @return An instance of ImageInfo.
+ * @see ImageInfo
+ */
+ public static ImageInfo getImageInfo(final InputStream is,
+ final String filename) throws ImageReadException, IOException {
+ return getImageInfo(new ByteSourceInputStream(is, filename), null);
+ }
+
+ /**
+ * Parses the "image info" of an image.
+ *
+ * "Image info" is a summary of basic information about the image such as:
+ * width, height, file format, bit depth, color type, etc.
+ *
+ * Not to be confused with "image metadata."
+ *
+ *
+ * @param is
+ * InputStream from which to read image data.
+ * @param filename
+ * Filename associated with image data (optional).
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return An instance of ImageInfo.
+ * @see ImageInfo
+ */
+ public static ImageInfo getImageInfo(final InputStream is,
+ final String filename, final Map
+ * "Image info" is a summary of basic information about the image such as:
+ * width, height, file format, bit depth, color type, etc.
+ *
+ * Not to be confused with "image metadata."
+ *
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @return An instance of ImageInfo.
+ * @see ImageInfo
+ */
+ public static ImageInfo getImageInfo(final byte[] bytes)
+ throws ImageReadException, IOException {
+ return getImageInfo(new ByteSourceArray(bytes), null);
+ }
+
+ /**
+ * Parses the "image info" of an image.
+ *
+ * "Image info" is a summary of basic information about the image such as:
+ * width, height, file format, bit depth, color type, etc.
+ *
+ * Not to be confused with "image metadata."
+ *
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return An instance of ImageInfo.
+ * @see ImageInfo
+ */
+ public static ImageInfo getImageInfo(final byte[] bytes,
+ final Map
+ * "Image info" is a summary of basic information about the image such as:
+ * width, height, file format, bit depth, color type, etc.
+ *
+ * Not to be confused with "image metadata."
+ *
+ *
+ * @param file
+ * File containing image data.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return An instance of ImageInfo.
+ * @see ImageInfo
+ */
+ public static ImageInfo getImageInfo(final File file,
+ final Map
+ * "Image info" is a summary of basic information about the image such as:
+ * width, height, file format, bit depth, color type, etc.
+ *
+ * Not to be confused with "image metadata."
+ *
+ *
+ * @param file
+ * File containing image data.
+ * @return An instance of ImageInfo.
+ * @see ImageInfo
+ */
+ public static ImageInfo getImageInfo(final File file)
+ throws ImageReadException, IOException {
+ return getImageInfo(file, null);
+ }
+
+ private static ImageInfo getImageInfo(final ByteSource byteSource,
+ final Map
+ *
+ * @param is
+ * InputStream from which to read image data.
+ * @param filename
+ * Filename associated with image data (optional).
+ * @return The width and height of the image.
+ */
+ public static Dimension getImageSize(final InputStream is,
+ final String filename) throws ImageReadException, IOException {
+ return getImageSize(is, filename, null);
+ }
+
+ /**
+ * Determines the width and height of an image.
+ *
+ *
+ * @param is
+ * InputStream from which to read image data.
+ * @param filename
+ * Filename associated with image data (optional).
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return The width and height of the image.
+ */
+ public static Dimension getImageSize(final InputStream is,
+ final String filename, final Map
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @return The width and height of the image.
+ */
+ public static Dimension getImageSize(final byte[] bytes)
+ throws ImageReadException, IOException {
+ return getImageSize(bytes, null);
+ }
+
+ /**
+ * Determines the width and height of an image.
+ *
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return The width and height of the image.
+ */
+ public static Dimension getImageSize(final byte[] bytes,
+ final Map
+ *
+ * @param file
+ * File containing image data.
+ * @return The width and height of the image.
+ */
+ public static Dimension getImageSize(final File file)
+ throws ImageReadException, IOException {
+ return getImageSize(file, null);
+ }
+
+ /**
+ * Determines the width and height of an image file.
+ *
+ *
+ * @param file
+ * File containing image data.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return The width and height of the image.
+ */
+ public static Dimension getImageSize(final File file,
+ final Map
+ *
+ * @param is
+ * InputStream from which to read image data.
+ * @param filename
+ * Filename associated with image data (optional).
+ * @return Xmp Xml as String, if present. Otherwise, returns null.
+ */
+ public static String getXmpXml(final InputStream is, final String filename)
+ throws ImageReadException, IOException {
+ return getXmpXml(is, filename, null);
+ }
+
+ /**
+ * Extracts the embedded XML metadata as an XML string.
+ *
+ *
+ * @param is
+ * InputStream from which to read image data.
+ * @param filename
+ * Filename associated with image data (optional).
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return Xmp Xml as String, if present. Otherwise, returns null.
+ */
+ public static String getXmpXml(final InputStream is, final String filename,
+ final Map
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @return Xmp Xml as String, if present. Otherwise, returns null.
+ */
+ public static String getXmpXml(final byte[] bytes)
+ throws ImageReadException, IOException {
+ return getXmpXml(bytes, null);
+ }
+
+ /**
+ * Extracts the embedded XML metadata as an XML string.
+ *
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return Xmp Xml as String, if present. Otherwise, returns null.
+ */
+ public static String getXmpXml(final byte[] bytes,
+ final Map
+ *
+ * @param file
+ * File containing image data.
+ * @return Xmp Xml as String, if present. Otherwise, returns null.
+ */
+ public static String getXmpXml(final File file) throws ImageReadException,
+ IOException {
+ return getXmpXml(file, null);
+ }
+
+ /**
+ * Extracts the embedded XML metadata as an XML string.
+ *
+ *
+ * @param file
+ * File containing image data.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return Xmp Xml as String, if present. Otherwise, returns null.
+ */
+ public static String getXmpXml(final File file,
+ final Map
+ *
+ * @param byteSource
+ * File containing image data.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return Xmp Xml as String, if present. Otherwise, returns null.
+ */
+ public static String getXmpXml(final ByteSource byteSource,
+ final Map
+ * JPEG/JFIF files may contain EXIF and/or IPTC metadata. PNG files may
+ * contain comments. TIFF files may contain metadata.
+ *
+ * The instance of IImageMetadata returned by getMetadata() should be upcast
+ * (depending on image format).
+ *
+ * Not to be confused with "image info."
+ *
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @return An instance of IImageMetadata.
+ * @see IImageMetadata
+ */
+ public static IImageMetadata getMetadata(final byte[] bytes)
+ throws ImageReadException, IOException {
+ return getMetadata(bytes, null);
+ }
+
+ /**
+ * Parses the metadata of an image. This metadata depends on the format of
+ * the image.
+ *
+ * JPEG/JFIF files may contain EXIF and/or IPTC metadata. PNG files may
+ * contain comments. TIFF files may contain metadata.
+ *
+ * The instance of IImageMetadata returned by getMetadata() should be upcast
+ * (depending on image format).
+ *
+ * Not to be confused with "image info."
+ *
+ *
+ * @param bytes
+ * Byte array containing an image file.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return An instance of IImageMetadata.
+ * @see IImageMetadata
+ */
+ public static IImageMetadata getMetadata(final byte[] bytes,
+ final Map
+ * JPEG/JFIF files may contain EXIF and/or IPTC metadata. PNG files may
+ * contain comments. TIFF files may contain metadata.
+ *
+ * The instance of IImageMetadata returned by getMetadata() should be upcast
+ * (depending on image format).
+ *
+ * Not to be confused with "image info."
+ *
+ *
+ * @param is
+ * InputStream from which to read image data.
+ * @param filename
+ * Filename associated with image data (optional).
+ * @return An instance of IImageMetadata.
+ * @see IImageMetadata
+ */
+ public static IImageMetadata getMetadata(final InputStream is,
+ final String filename) throws ImageReadException, IOException {
+ return getMetadata(is, filename, null);
+ }
+
+ /**
+ * Parses the metadata of an image file. This metadata depends on the format
+ * of the image.
+ *
+ * JPEG/JFIF files may contain EXIF and/or IPTC metadata. PNG files may
+ * contain comments. TIFF files may contain metadata.
+ *
+ * The instance of IImageMetadata returned by getMetadata() should be upcast
+ * (depending on image format).
+ *
+ * Not to be confused with "image info."
+ *
+ *
+ * @param is
+ * InputStream from which to read image data.
+ * @param filename
+ * Filename associated with image data (optional).
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return An instance of IImageMetadata.
+ * @see IImageMetadata
+ */
+ public static IImageMetadata getMetadata(final InputStream is,
+ final String filename, final Map
+ * JPEG/JFIF files may contain EXIF and/or IPTC metadata. PNG files may
+ * contain comments. TIFF files may contain metadata.
+ *
+ * The instance of IImageMetadata returned by getMetadata() should be upcast
+ * (depending on image format).
+ *
+ * Not to be confused with "image info."
+ *
+ *
+ * @param file
+ * File containing image data.
+ * @return An instance of IImageMetadata.
+ * @see IImageMetadata
+ */
+ public static IImageMetadata getMetadata(final File file)
+ throws ImageReadException, IOException {
+ return getMetadata(file, null);
+ }
+
+ /**
+ * Parses the metadata of an image file. This metadata depends on the format
+ * of the image.
+ *
+ * JPEG/JFIF files may contain EXIF and/or IPTC metadata. PNG files may
+ * contain comments. TIFF files may contain metadata.
+ *
+ * The instance of IImageMetadata returned by getMetadata() should be upcast
+ * (depending on image format).
+ *
+ * Not to be confused with "image info."
+ *
+ *
+ * @param file
+ * File containing image data.
+ * @param params
+ * Map of optional parameters, defined in ImagingConstants.
+ * @return An instance of IImageMetadata.
+ * @see IImageMetadata
+ */
+ public static IImageMetadata getMetadata(final File file,
+ final Map
+ * For the most recent information on support for specific formats, refer to
+ * Format
+ * Support at the main project development web site. While the Apache
+ * Commons Imaging package does not fully support all formats, it can read
+ * image info, metadata and ICC profiles from all image formats that provide
+ * this data.
+ *
+ * @param is
+ * a valid ImageStream from which to read data.
+ * @return if successful, a valid buffered image
+ * @throws ImageReadException
+ * in the event of a processing error while reading an image
+ * (i.e. a format violation, etc.).
+ * @throws IOException
+ * in the event of an unrecoverable I/O exception.
+ */
+
+ public static BufferedImage getBufferedImage(final InputStream is)
+ throws ImageReadException, IOException {
+ return getBufferedImage(is, null);
+ }
+
+ /**
+ * Reads the first image from an InputStream using data-processing options
+ * specified through a parameters map. Options may be configured using the
+ * ImagingContants interface or the various format-specific implementations
+ * provided by this package.
+ *
+ * For the most recent information on support for specific formats, refer to
+ * Format
+ * Support at the main project development web site. While the Apache
+ * Commons Imaging package does not fully support all formats, it can read
+ * image info, metadata and ICC profiles from all image formats that provide
+ * this data.
+ *
+ * @param is
+ * a valid ImageStream from which to read data.
+ * @param params
+ * an optional parameters map specifying options
+ * @return if successful, a valid buffered image
+ * @throws ImageReadException
+ * in the event of a processing error while reading an image
+ * (i.e. a format violation, etc.).
+ * @throws IOException
+ * in the event of an unrecoverable I/O exception.
+ */
+ public static BufferedImage getBufferedImage(final InputStream is,
+ final Map
+ * For the most recent information on support for specific formats, refer to
+ * Format
+ * Support at the main project development web site. While the Apache
+ * Commons Imaging package does not fully support all formats, it can read
+ * image info, metadata and ICC profiles from all image formats that provide
+ * this data.
+ *
+ * @param bytes
+ * a valid array of bytes from which to read data.
+ * @return if successful, a valid buffered image
+ * @throws ImageReadException
+ * in the event of a processing error while reading an image
+ * (i.e. a format violation, etc.).
+ * @throws IOException
+ * in the event of an unrecoverable I/O exception.
+ */
+ public static BufferedImage getBufferedImage(final byte[] bytes)
+ throws ImageReadException, IOException {
+ return getBufferedImage(new ByteSourceArray(bytes), null);
+ }
+
+ /**
+ * Reads the first image from a byte array using data-processing options
+ * specified through a parameters map. Options may be configured using the
+ * ImagingContants interface or the various format-specific implementations
+ * provided by this package.
+ *
+ * For the most recent information on support for specific formats, refer to
+ * Format
+ * Support at the main project development web site. While the Apache
+ * Commons Imaging package does not fully support all formats, it can read
+ * image info, metadata and ICC profiles from all image formats that provide
+ * this data.
+ *
+ * @param bytes
+ * a valid array of bytes from which to read data.
+ * @param params
+ * an optional parameters map specifying options.
+ * @return if successful, a valid buffered image
+ * @throws ImageReadException
+ * in the event of a processing error while reading an image
+ * (i.e. a format violation, etc.).
+ * @throws IOException
+ * in the event of an unrecoverable I/O exception.
+ */
+ public static BufferedImage getBufferedImage(final byte[] bytes,
+ final Map
+ * For the most recent information on support for specific formats, refer to
+ * Format
+ * Support at the main project development web site. While the Apache
+ * Commons Imaging package does not fully support all formats, it can read
+ * image info, metadata and ICC profiles from all image formats that provide
+ * this data.
+ *
+ * @param file
+ * a valid reference to a file containing image data.
+ * @return if successful, a valid buffered image
+ * @throws ImageReadException
+ * in the event of a processing error while reading an image
+ * (i.e. a format violation, etc.).
+ * @throws IOException
+ * in the event of an unrecoverable I/O exception.
+ */
+ public static BufferedImage getBufferedImage(final File file)
+ throws ImageReadException, IOException {
+ return getBufferedImage(new ByteSourceFile(file), null);
+ }
+
+ /**
+ * Reads the first image from a file using data-processing options specified
+ * through a parameters map. Options may be configured using the
+ * ImagingContants interface or the various format-specific implementations
+ * provided by this package.
+ *
+ * For the most recent information on support for specific formats, refer to
+ * Format
+ * Support at the main project development web site. While the Apache
+ * Commons Imaging package does not fully support all formats, it can read
+ * image info, metadata and ICC profiles from all image formats that provide
+ * this data.
+ *
+ * @param file
+ * a valid reference to a file containing image data.
+ * @return if successful, a valid buffered image
+ * @throws ImageReadException
+ * in the event of a processing error while reading an image
+ * (i.e. a format violation, etc.).
+ * @throws IOException
+ * in the event of an unrecoverable I/O exception.
+ */
+ public static BufferedImage getBufferedImage(final File file,
+ final Map
+ * Image writing is not supported for all graphics formats. For the most
+ * recent information on support for specific formats, refer to Format
+ * Support at the main project development web site. While the Apache
+ * Commons Imaging package does not fully support all formats, it can read
+ * image info, metadata and ICC profiles from all image formats that provide
+ * this data.
+ *
+ * @param src
+ * a valid BufferedImage object
+ * @param file
+ * the file to which the output image is to be written
+ * @param format
+ * the format in which the output image is to be written
+ * @param params
+ * an optional parameters map (nulls permitted)
+ * @throws ImageWriteException
+ * in the event of a format violation, unsupported image format,
+ * etc.
+ * @throws IOException
+ * in the event of an unrecoverable I/O exception.
+ * @see ImagingConstants
+ */
+ public static void writeImage(final BufferedImage src, final File file,
+ final ImageFormat format, final Map
+ * Image writing is not supported for all graphics formats. For the most
+ * recent information on support for specific formats, refer to Format
+ * Support at the main project development web site. While the Apache
+ * Commons Imaging package does not fully support all formats, it can read
+ * image info, metadata and ICC profiles from all image formats that provide
+ * this data.
+ *
+ * @param src
+ * a valid BufferedImage object
+ * @param format
+ * the format in which the output image is to be written
+ * @param params
+ * an optional parameters map (nulls permitted)
+ * @return if successful, a valid array of bytes.
+ * @throws ImageWriteException
+ * in the event of a format violation, unsupported image format,
+ * etc.
+ * @throws IOException
+ * in the event of an unrecoverable I/O exception.
+ * @see ImagingConstants
+ */
+ public static byte[] writeImageToBytes(final BufferedImage src,
+ final ImageFormat format, final Map
+ * Image writing is not supported for all graphics formats. For the most
+ * recent information on support for specific formats, refer to Format
+ * Support at the main project development web site. While the Apache
+ * Commons Imaging package does not fully support all formats, it can read
+ * image info, metadata and ICC profiles from all image formats that provide
+ * this data.
+ *
+ * @param src
+ * a valid BufferedImage object
+ * @param os
+ * the OutputStream to which the output image is to be written
+ * @param format
+ * the format in which the output image is to be written
+ * @param params
+ * an optional parameters map (nulls permitted)
+ * @throws ImageWriteException
+ * in the event of a format violation, unsupported image format,
+ * etc.
+ * @throws IOException
+ * in the event of an unrecoverable I/O exception.
+ * @see ImagingConstants
+ */
+ public static void writeImage(final BufferedImage src,
+ final OutputStream os, final ImageFormat format,
+ MapApplication Notes
* Using this class
- * Almost all of the Apache Commons Imaging library's core functionality can
- * be accessed through the methods provided by this class.
- * The use of the Imaging class is similar to the Java API's ImageIO class,
- * though Imaging supports formats and options not included in the standard
- * Java API.
- * Format support
- * While the Apache Commons Imaging package handles a number of different
- * graphics formats, support for some formats is not yet complete.
- * For the most recent information on support for specific formats, refer to
- * Format Support
- * at the main project development web site.
+ * While the Apache Commons Imaging package handles a number of different
+ * graphics formats, support for some formats is not yet complete. For the most
+ * recent information on support for specific formats, refer to Format
+ * Support at the main project development web site.
* Optional parameters for image reading and writing
- * Some of the methods provided by this class accept an optional
+ * Some of the methods provided by this class accept an optional
* params argument that permits the application to specify
- * elements for special handling. If these specifications are not required by
- * the application, the params argument may be omitted (as appropriate) or
- * a null argument may be provided. In image-writing operations, the option
- * parameters may include options such as data-compression type (if any),
- * color model, or other format-specific data representations. The parameters
- * map may also be used to provide EXIF Tags and other metadata to those
- * formats that support them. In image-reading operations,
- * the parameters may include information about special handling in reading
- * the image data.
- * Example code
* See the source of the SampleUsage class and other classes in the
* org.apache.commons.imaging.examples package for examples.
*
* @see org.apache.commons.imaging.examples.SampleUsage
- * @see Format Support
+ * @see Format
+ * Support
*/
public abstract class Imaging {
- private static final int[] MAGIC_NUMBERS_GIF = { 0x47, 0x49, };
- private static final int[] MAGIC_NUMBERS_PNG = { 0x89, 0x50, };
- private static final int[] MAGIC_NUMBERS_JPEG = { 0xff, 0xd8, };
- private static final int[] MAGIC_NUMBERS_BMP = { 0x42, 0x4d, };
- private static final int[] MAGIC_NUMBERS_TIFF_MOTOROLA = { 0x4D, 0x4D, };
- private static final int[] MAGIC_NUMBERS_TIFF_INTEL = { 0x49, 0x49, };
- private static final int[] MAGIC_NUMBERS_PAM = { 0x50, 0x37, };
- private static final int[] MAGIC_NUMBERS_PSD = { 0x38, 0x42, };
- private static final int[] MAGIC_NUMBERS_PBM_A = { 0x50, 0x31, };
- private static final int[] MAGIC_NUMBERS_PBM_B = { 0x50, 0x34, };
- private static final int[] MAGIC_NUMBERS_PGM_A = { 0x50, 0x32, };
- private static final int[] MAGIC_NUMBERS_PGM_B = { 0x50, 0x35, };
- private static final int[] MAGIC_NUMBERS_PPM_A = { 0x50, 0x33, };
- private static final int[] MAGIC_NUMBERS_PPM_B = { 0x50, 0x36, };
- private static final int[] MAGIC_NUMBERS_JBIG2_1 = { 0x97, 0x4A, };
- private static final int[] MAGIC_NUMBERS_JBIG2_2 = { 0x42, 0x32, };
- private static final int[] MAGIC_NUMBERS_ICNS = { 0x69, 0x63, };
- private static final int[] MAGIC_NUMBERS_DCX = { 0xB1, 0x68, };
- private static final int[] MAGIC_NUMBERS_RGBE = { 0x23, 0x3F, };
-
- /**
- * Attempts to determine if a file contains an image recorded in
- * a supported graphics format based on its file-name extension
- * (for example ".jpg", ".gif", ".png", etc.).
- *
- * @param file A valid File object providing a reference to
- * a file that may contain an image.
- * @return true if the file-name includes a supported image
- * format file extension; otherwise, false.
- */
- public static boolean hasImageFileExtension(final File file) {
- if (file == null || !file.isFile()) {
- return false;
- }
- return hasImageFileExtension(file.getName());
- }
-
- /**
- * Attempts to determine if a file contains an image recorded in
- * a supported graphics format based on its file-name extension
- * (for example ".jpg", ".gif", ".png", etc.).
- *
- * @param filename A valid string representing name of file
- * which may contain an image.
- * @return true if the filename has an image format file extension.
- */
- public static boolean hasImageFileExtension(String filename) {
- if (filename == null) {
- return false;
- }
-
- filename = filename.toLowerCase(Locale.ENGLISH);
-
- final ImageParser[] imageParsers = ImageParser.getAllImageParsers();
- for (final ImageParser imageParser : imageParsers) {
- final String[] exts = imageParser.getAcceptedExtensions();
-
- for (final String ext : exts) {
- if (filename.endsWith(ext.toLowerCase(Locale.ENGLISH))) {
- return true;
- }
- }
- }
-
- return false;
- }
-
- /**
- * Attempts to determine the image format of a file based on its
- * "magic numbers," the first bytes of the data.
- *