net.sourceforge.jiu.codecs

Class ImageLoader


public class ImageLoader
extends java.lang.Object

A convenience class with static methods to load images from files using JIU codecs. The load methods of this class try to load an image with all codecs registered with this class. This includes almost every codec that resides in the net.sourceforge.jiu.codecs package. You can register additional codecs with registerCodecClass(ImageCodec) or remove the usage of codecs with removeCodecClass(ImageCodec).

A Codec that cannot safely identify a file to be in the format that it supports must not be used with ImageLoader. The failure to identify typically comes from the lack of magic byte sequences defined for the format. In order to load such a file, use the codec manually. Example: PalmCodec.

In order to load an image via java.awt.Toolkit (JPEG, PNG or GIF), use ToolkitLoader. It combines the loading features of java.awt.Toolkit and JIU's ImageLoader.

Usage example

 PixelImage image = null;
 try
 {
   image = ImageLoader.load("image.tif");
 }
 catch (Exception e)
 {
   // handle exception
 }
 
Author:
Marco Schmidt

Field Summary

private static Vector
fileExtensions
private static Vector
imageCodecClasses

Constructor Summary

ImageLoader()

Method Summary

static ImageCodec
createCodec(int index)
Creates an instance of one of the codec classes known to ImageLoader.
static FilenameFilter
createFilenameFilter()
Returns a filename filter (java.io.FilenameFilter) that accepts files with name extensions typical for the image file formats known to ImageLoader.
static int
getNumCodecs()
Returns the number of codec classes currently known to ImageLoader.
static PixelImage
load(File file)
Attempts to load an image from a file.
static PixelImage
load(File file, Vector listeners)
Attempts to load an image from a file, notifying the argument progress listeners.
static PixelImage
load(String fileName)
Load an image from a file given by its name.
static PixelImage
load(String fileName, Vector listeners)
Attempts to load an image from the file with the given name, using the given list of progress listeners.
static PixelImage
loadToolkitImageUri(String uri)
static void
registerCodecClass(ImageCodec codec)
Registers a codec class with ImageLoader.
static void
removeAllCodecClasses()
Removes all codec classes from the internal list of codec classes.
static void
removeCodecClass(ImageCodec codec)
Removes a codec class from the internal list of codec classes.
private static void
updateFileExtensions()

Field Details

fileExtensions

private static Vector fileExtensions

imageCodecClasses

private static Vector imageCodecClasses

Constructor Details

ImageLoader

private ImageLoader()

Method Details

createCodec

public static ImageCodec createCodec(int index)
Creates an instance of one of the codec classes known to ImageLoader.
Parameters:
index - 0-based index of codec number, maximum value is getNumCodecs() - 1
Returns:
new codec object or null if no object could be instantiated

createFilenameFilter

public static FilenameFilter createFilenameFilter()
Returns a filename filter (java.io.FilenameFilter) that accepts files with name extensions typical for the image file formats known to ImageLoader. The filter could then be used in an file dialog like java.awt.FileDialog.

Note that this filter does not include file formats supported by the AWT java.awt.Toolkit (GIF and JPEG, also PNG since Java 1.3).

Returns:
filter for image file names

getNumCodecs

public static int getNumCodecs()
Returns:
number of known codec classes

load

public static PixelImage load(File file)
            throws IOException,
                   InvalidFileStructureException,
                   InvalidImageIndexException,
                   UnsupportedTypeException
Attempts to load an image from a file.
Parameters:
file - the file from which an image is to be loaded
Returns:
the image on success or null on failure

load

public static PixelImage load(File file,
                              Vector listeners)
            throws IOException,
                   InvalidFileStructureException,
                   InvalidImageIndexException,
                   UnsupportedTypeException
Attempts to load an image from a file, notifying the argument progress listeners.
Parameters:
file - the file to load an image from
listeners - a Vector of ProgressListener objects to be notified
Returns:
an instance of a class implementing PixelImage

load

public static PixelImage load(String fileName)
            throws IOException,
                   InvalidFileStructureException,
                   InvalidImageIndexException,
                   UnsupportedTypeException
Load an image from a file given by its name. Simply calls load(fileName, null).
Parameters:
fileName - name of the file from which an image is to be loaded
Returns:
the loaded image on success, null on failure

load

public static PixelImage load(String fileName,
                              Vector listeners)
            throws IOException,
                   InvalidFileStructureException,
                   InvalidImageIndexException,
                   UnsupportedTypeException
Attempts to load an image from the file with the given name, using the given list of progress listeners.
Parameters:
fileName - name of the file from which an image is to be loaded
listeners - a list of objects implementing ProgressListener
Returns:
the loaded image

loadToolkitImageUri

public static PixelImage loadToolkitImageUri(String uri)
            throws IOException,
                   InvalidFileStructureException,
                   InvalidImageIndexException,
                   UnsupportedTypeException

registerCodecClass

public static void registerCodecClass(ImageCodec codec)
Registers a codec class with ImageLoader. The argument is an instance of the class to be registered. Note that the codec class must have an empty constructor.

Example: let's say you have written a new codec called ACMEImageCodec. Your codec supports loading images. Then you could register it like that:

 ImageLoader.registerCodecClass(new ACMEImageCodec());
 

Parameters:
codec - an instance of the codec class to be registered

removeAllCodecClasses

public static void removeAllCodecClasses()
Removes all codec classes from the internal list of codec classes. After a call to this method, ImageLoader will not load anything unless new codecs get registered.

removeCodecClass

public static void removeCodecClass(ImageCodec codec)
Removes a codec class from the internal list of codec classes.
Parameters:
codec - an instance of the codec class to be removed

updateFileExtensions

private static void updateFileExtensions()