org.geotoolkit.image.io
Class XImageIO

Object
  extended by Static
      extended by XImageIO

public final class XImageIO
extends Static

Extensions to the set of static methods provided in the standard ImageIO class. The methods defined in this XImageIO class fetch an ImageReader or an ImageWriter for a given input, output, suffix, format name or mime type. They are equivalent in purpose to the methods defined in ImageIO except that:

For example, the standard Java API provides the following method:

ImageIO.getImageReadersBySuffix(String) with suffix argument
while this class provides the following equivalent API:
getReaderBySuffix(String, Object, Boolean, Boolean) with suffix, input, seekForwardOnly and ignoreMetadata arguments.
Note: Why XImageIO methods expect an optional input argument: The standard ImageIO methods do not take input argument because the Java Image I/O framework expects every ImageReaders to accept an ImageInputStream. Actually, ImageIO creates image input streams unconditionally, without checking whatever it was necessary or not.

However, some plugins used by the Geotk library can not work with streams. Some plugins (e.g. the HDF format) open the file in native C/C++ code. Those libraries can not work with Java streams - they require a plain filename. Even in pure Java code, sometime a filename or URL is required. For example the Geotk WorldFileImageReader needs to open many files for the same image. Consequently if the user wants to read an image from a File, it is preferable to check if an ImageReader accepts directly such File input before to create an ImageInputStream.


How the input/output is defined in the returned reader/writer
The ImageReader and ImageWriter returned by the methods in this class will have their input or output set. This is different than the standard ImageIO API (which returns uninitialized readers or writers) and is done that way because the input or output may be different than the one specified by the caller if it was necessary to create an ImageInputStream or ImageOutputStream. If such stream has been created, then it is the caller responsibility to close it after usage. The close(ImageReader) and close(ImageWriter) convenience methods can be used for this purpose.

The output in the returned image writer is defined by a call to the ImageWriter.setOutput(Object) method. The input in the returned image reader is defined by a call to the ImageReader.setInput(Object), setInput(Object, boolean) or setInput(Object, boolean, boolean) method depending on whatever the seekForwardOnly and ignoreMetadata arguments are non-null or not. For example if those two Boolean arguments are null, then the setInput(Object) flavor is invoked. This is usually equivalent to setting the two boolean argument to false, but this behavior could have been overridden by the plugin.


Example
The following example reads a TIFF image. Because we use an input of type File instead than ImageInputStream, the WorldFileImageReader can be used. Consequently the metadata associated with the TIFF image can contain geolocalization information if a ".tfw" file was found together with the ".tiff" file.

File          input    = new File("my_image.tiff");
ImageReader   reader   = XImageIO.getReaderBySuffix(input, true, false);
IIOMetadata   metadata = reader.getImageMetadata(0);
BufferedImage image    = reader.read(0);
XImageIO.close(reader);
reader.dispose();


Mandatory and optional arguments
Every methods defined in this class expect exactly one mandatory argument, which is always the first argument. All other arguments are optional and can be null.

Since:
3.07
Version:
3.20
Author:
Martin Desruisseaux (Geomatys)
Module:
coverage/geotk-coverageio (download)    View source code for this class

Method Summary
static void close(ImageReader reader)
          Closes the input stream of the given reader, and sets the input to null.
static void close(ImageWriter writer)
          Closes the output stream of the given writer, and sets the output to null.
static String[] getFormatNamesByMimeType(String mime, boolean read, boolean write)
          Returns the format names of all ImageReaderSpi and/or ImageWriterSpi instances registered for the given MIME type.
static ImageReader getReader(Object input, Boolean seekForwardOnly, Boolean ignoreMetadata)
          Creates a new reader for the given input.
static ImageReader getReaderByFormatName(String name, Object input, Boolean seekForwardOnly, Boolean ignoreMetadata)
          Creates a new reader for the given optional input, considering only the readers of the given format name.
static ImageReader getReaderByMIMEType(String mime, Object input, Boolean seekForwardOnly, Boolean ignoreMetadata)
          Creates a new reader for the given optional input, considering only the readers for the given MIME type.
static ImageReader getReaderBySuffix(Object input, Boolean seekForwardOnly, Boolean ignoreMetadata)
          Creates a new reader for the given input, considering only the readers that claim to decode files having the suffix found in the input.
static ImageReader getReaderBySuffix(String suffix, Object input, Boolean seekForwardOnly, Boolean ignoreMetadata)
          Creates a new reader for the given optional input, considering only the readers that claim to decode files having the given suffix.
static ImageReaderSpi getReaderSpiByFormatName(String format)
          Returns the image reader provider for the given format name.
static ImageWriter getWriterByFormatName(String name, Object output, RenderedImage image)
          Creates a new writer for the given output, considering only the writers of the given format name.
static ImageWriter getWriterByMIMEType(String mime, Object output, RenderedImage image)
          Creates a new writer for the given output, considering only the writers for the given MIME type.
static ImageWriter getWriterBySuffix(Object output, RenderedImage image)
          Creates a new writer for the given output, considering only the writers that claim to encode files having the suffix of the given output.
static ImageWriter getWriterBySuffix(String suffix, Object output, RenderedImage image)
          Creates a new writer for the given output, considering only the writers that claim to encode files having the given suffix.
static ImageWriterSpi getWriterSpiByFormatName(String format)
          Returns the image writer provider for the given format name.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getReader

public static ImageReader getReader(Object input,
                                    Boolean seekForwardOnly,
                                    Boolean ignoreMetadata)
                             throws IOException
Creates a new reader for the given input. The input argument is mandatory and will be used for initializing the reader by a call to one of the ImageReader.setInput(Object) methods. The method invoked depends on whatever the seekForwardOnly and ignoreMetadata parameters are null or non-null.

Parameters:
input - The mandatory input to be given to the new reader instance.
seekForwardOnly - Optional parameter to be given (if non-null) to the setInput method: if true, images and metadata may only be read in ascending order from the input source. If false, they may be read in any order. If null, this parameter is not given to the reader which is free to use a plugin-dependent default (usually false).
ignoreMetadata - Optional parameter to be given (if non-null) to the setInput method: if true, metadata may be ignored during reads. If false, metadata will be parsed. If null, this parameter is not given to the reader which is free to use a plugin-dependent default (usually false).
Returns:
The new image reader instance with its input initialized.
Throws:
IOException - If no suitable image reader has been found, or if an error occurred while creating it.
See Also:
ImageIO.getImageReaders(Object)

getReaderBySuffix

public static ImageReader getReaderBySuffix(Object input,
                                            Boolean seekForwardOnly,
                                            Boolean ignoreMetadata)
                                     throws IOException
Creates a new reader for the given input, considering only the readers that claim to decode files having the suffix found in the input. The input argument is mandatory and will be used for initializing the reader as documented in the getReader(...) method.

If this method doesn't know how to get the suffix from the given input (for example if the input is an instance of ImageInputStream), then this method delegates to getReader(Object, Boolean, Boolean).

Parameters:
input - The mandatory input to be given to the new reader instance.
seekForwardOnly - Optional parameter to be given (if non-null) to the setInput method.
ignoreMetadata - Optional parameter to be given (if non-null) to the setInput method.
Returns:
The new image reader instance with its input initialized.
Throws:
IOException - If no suitable image reader has been found, or if an error occurred while creating it.
See Also:
ImageIO.getImageReadersBySuffix(String)

getReaderBySuffix

public static ImageReader getReaderBySuffix(String suffix,
                                            Object input,
                                            Boolean seekForwardOnly,
                                            Boolean ignoreMetadata)
                                     throws IOException
Creates a new reader for the given optional input, considering only the readers that claim to decode files having the given suffix. Only the suffix argument is mandatory. The other ones are optional and will be used (if non-null) for initializing the reader as documented in the getReader(...) method.

Parameters:
suffix - The file suffix for which we want a reader.
input - An optional input to be given to the new reader instance, or null if none.
seekForwardOnly - Optional parameter to be given (if non-null) to the setInput method.
ignoreMetadata - Optional parameter to be given (if non-null) to the setInput method.
Returns:
The new image reader instance with its input initialized (if input is not null).
Throws:
IOException - If no suitable image reader has been found, or if an error occurred while creating it.
See Also:
ImageIO.getImageReadersBySuffix(String)

getReaderByFormatName

public static ImageReader getReaderByFormatName(String name,
                                                Object input,
                                                Boolean seekForwardOnly,
                                                Boolean ignoreMetadata)
                                         throws IOException
Creates a new reader for the given optional input, considering only the readers of the given format name. Only the name argument is mandatory. The other ones are optional and will be used (if non-null) for initializing the reader as documented in the getReader(...) method.

Parameters:
name - The name of the format looked for.
input - An optional input to be given to the new reader instance, or null if none.
seekForwardOnly - Optional parameter to be given (if non-null) to the setInput method.
ignoreMetadata - Optional parameter to be given (if non-null) to the setInput method.
Returns:
The new image reader instance with its input initialized (if input is not null).
Throws:
IOException - If no suitable image reader has been found, or if an error occurred while creating it.
See Also:
ImageIO.getImageReadersByFormatName(String)

getReaderByMIMEType

public static ImageReader getReaderByMIMEType(String mime,
                                              Object input,
                                              Boolean seekForwardOnly,
                                              Boolean ignoreMetadata)
                                       throws IOException
Creates a new reader for the given optional input, considering only the readers for the given MIME type. Only the mime argument is mandatory. The other ones are optional and will be used (if non-null) for initializing the reader as documented in the getReader(...) method.

Parameters:
mime - The MIME type of the format looked for.
input - An optional input to be given to the new reader instance, or null if none.
seekForwardOnly - Optional parameter to be given (if non-null) to the setInput method.
ignoreMetadata - Optional parameter to be given (if non-null) to the setInput method.
Returns:
The new image reader instance with its input initialized (if input is not null).
Throws:
IOException - If no suitable image reader has been found, or if an error occurred while creating it.
See Also:
ImageIO.getImageReadersByMIMEType(String)

close

public static void close(ImageReader reader)
                  throws IOException
Closes the input stream of the given reader, and sets the input to null.

Parameters:
reader - The reader for which to close the input stream.
Throws:
IOException - If an error occurred while closing the stream.

getWriterBySuffix

public static ImageWriter getWriterBySuffix(Object output,
                                            RenderedImage image)
                                     throws IOException
Creates a new writer for the given output, considering only the writers that claim to encode files having the suffix of the given output. If a writer is found, then the writer will be initialized to the given output by a call to its setOutput method.

Parameters:
output - A mandatory output to be given to the new writer instance.
image - The image to encode, or null if unknown.
Returns:
The new image writer instance with its output initialized.
Throws:
IOException - If no suitable image writer has been found, or if an error occurred while creating it.
See Also:
ImageIO.getImageWritersBySuffix(String)

getWriterBySuffix

public static ImageWriter getWriterBySuffix(String suffix,
                                            Object output,
                                            RenderedImage image)
                                     throws IOException
Creates a new writer for the given output, considering only the writers that claim to encode files having the given suffix. If a writer is found and the given output is non-null, then the writer will be initialized to the given output by a call to its setOutput method.

Parameters:
suffix - The file suffix for which we want a writer.
output - An optional output to be given to the new writer instance, or null if none.
image - The image to encode, or null if unknown.
Returns:
The new image writer instance with its output initialized (if output is not null).
Throws:
IOException - If no suitable image writer has been found, or if an error occurred while creating it.
See Also:
ImageIO.getImageWritersBySuffix(String)

getWriterByFormatName

public static ImageWriter getWriterByFormatName(String name,
                                                Object output,
                                                RenderedImage image)
                                         throws IOException
Creates a new writer for the given output, considering only the writers of the given format name. If a writer is found and the given output is non-null, then the writer will be initialized to the given output by a call to its setOutput method.

Parameters:
name - The format name for which we want a writer.
output - An optional output to be given to the new writer instance, or null if none.
image - The image to encode, or null if unknown.
Returns:
The new image writer instance with its output initialized (if output is not null).
Throws:
IOException - If no suitable image writer has been found, or if an error occurred while creating it.
See Also:
ImageIO.getImageWritersByFormatName(String)

getWriterByMIMEType

public static ImageWriter getWriterByMIMEType(String mime,
                                              Object output,
                                              RenderedImage image)
                                       throws IOException
Creates a new writer for the given output, considering only the writers for the given MIME type. If a writer is found and the given output is non-null, then the writer will be initialized to the given output by a call to its setOutput method.

Parameters:
mime - The MIME type for which we want a writer.
output - An optional output to be given to the new writer instance, or null if none.
image - The image to encode, or null if unknown.
Returns:
The new image writer instance with its output initialized (if output is not null).
Throws:
IOException - If no suitable image writer has been found, or if an error occurred while creating it.
See Also:
ImageIO.getImageWritersByMIMEType(String)

close

public static void close(ImageWriter writer)
                  throws IOException
Closes the output stream of the given writer, and sets the output to null.

Parameters:
writer - The writer for which to close the output stream.
Throws:
IOException - If an error occurred while closing the stream.

getReaderSpiByFormatName

public static ImageReaderSpi getReaderSpiByFormatName(String format)
Returns the image reader provider for the given format name.

Parameters:
format - The name of the provider to fetch.
Returns:
The reader provider for the given format.
Throws:
IllegalArgumentException - If no provider is found for the given format.

getWriterSpiByFormatName

public static ImageWriterSpi getWriterSpiByFormatName(String format)
Returns the image writer provider for the given format name.

Parameters:
format - The name of the provider to fetch.
Returns:
The reader provider for the given format.
Throws:
IllegalArgumentException - If no provider is found for the given format.

getFormatNamesByMimeType

public static String[] getFormatNamesByMimeType(String mime,
                                                boolean read,
                                                boolean write)
Returns the format names of all ImageReaderSpi and/or ImageWriterSpi instances registered for the given MIME type. If there is many format names for the same MIME type, then the most frequently used names will be sorted first.

Parameters:
mime - The MIME type to search for.
read - true if the format name is required to be supported by at least one ImageReader.
write - true if the format name is required to be supported by at least one ImageWriter.
Returns:
The format names, or an empty array if none.
Since:
3.14


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.