us.zuercher.gpx2map.image.generic
Class GenericImageFactory

java.lang.Object
  extended by us.zuercher.gpx2map.image.generic.GenericImageFactory
All Implemented Interfaces:
ImageFactory

public class GenericImageFactory
extends Object
implements ImageFactory

GenericImageFactory is a factory for GenericImage objects. Its generic insofar as it depends entirely on Toolkit to load images and only supports the image formats that the toolkit supports (GIF, JPEG, and PNG).

Author:
Stephan Zuercher

Nested Class Summary
private static class GenericImageFactory.DrawingObserver
          DrawingObserver is an ImageObserver that allows callers to wait for the drawing of an image to complete.
private static class GenericImageFactory.Observer
          Observer provides a base class for size and drawing observations.
private static class GenericImageFactory.SizeObserver
          SizeObserver is an ImageObserver that can be waited upon to determine when an image's size data has been loaded.
 
Field Summary
private static byte _00
          0x00 as a byte.
private static byte _FF
          0xFF pre-cast as a byte.
private static List<ImageFormatDescriptor> formatDescriptors
          List of ImageFormatDescriptor objects for our supported formats.
private static byte[][] gifHeaders
          GIF file header.
static GenericImageFactory instance
          The singleton instance of this image factory.
private static byte[][] jpegHeaders
          JPEG file header.
private static int minimumFileSize
          Minimum number of bytes from an image file necessary to determine the file's type.
private static byte[][] pngHeaders
          PNG file header.
private static byte[][][] supportedHeaders
          Array of supported header data.
private static Toolkit toolkit
          Graphics toolkit used for loading images.
 
Constructor Summary
private GenericImageFactory()
          GenericImageFactory is a singleton.
 
Method Summary
 boolean canLoad(File imageFile)
          Tests whether the given file contains an image in a format supported by this factory.
private  boolean canLoad(InputStream imageStream)
          Test the given input stream's format and return an indication of whether the format is supported or not.
 boolean canLoad(URL imageUrl)
          Tests whether the given URL refers to an image in a format supported by this factory.
private static GenericImage createImage(Image javaImage)
           
 List<ImageFormatDescriptor> getSupportedImageFormats()
          Returns the ImageFormatDescriptors for image formats supported by this factory.
 GenericImage loadImage(File imageFile)
          Load an image from the given file name.
 GenericImage loadImage(URL imageUrl)
          Load an image from the given URL.
private  void testFormat(InputStream stream)
          Test the given input stream's format and throw an UnknownFormatException if it's not in a supported format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

instance

public static final GenericImageFactory instance
The singleton instance of this image factory.


toolkit

private static final Toolkit toolkit
Graphics toolkit used for loading images.


_FF

private static final byte _FF
0xFF pre-cast as a byte.

See Also:
Constant Field Values

_00

private static final byte _00
0x00 as a byte.

See Also:
Constant Field Values

pngHeaders

private static final byte[][] pngHeaders
PNG file header. First element is a set of mask bytes that should be applied the remaining elements and the file data before comparison.


gifHeaders

private static final byte[][] gifHeaders
GIF file header. First element is a set of mask bytes that should be applied the remaining elements and the file data before comparison.


jpegHeaders

private static final byte[][] jpegHeaders
JPEG file header. First element is a set of mask bytes that should be applied the remaining elements and the file data before comparison.


supportedHeaders

private static final byte[][][] supportedHeaders
Array of supported header data.


minimumFileSize

private static final int minimumFileSize
Minimum number of bytes from an image file necessary to determine the file's type.


formatDescriptors

private static final List<ImageFormatDescriptor> formatDescriptors
List of ImageFormatDescriptor objects for our supported formats.

Constructor Detail

GenericImageFactory

private GenericImageFactory()
GenericImageFactory is a singleton.

See Also:
instance
Method Detail

getSupportedImageFormats

public List<ImageFormatDescriptor> getSupportedImageFormats()
Returns the ImageFormatDescriptors for image formats supported by this factory.

Specified by:
getSupportedImageFormats in interface ImageFactory
Returns:
the ImageFormatDescriptors for image formats supported by this factory.

canLoad

public boolean canLoad(File imageFile)
                throws IOException
Tests whether the given file contains an image in a format supported by this factory.

Specified by:
canLoad in interface ImageFactory
Parameters:
imageFile - the file to test
Returns:
true if the image file is in a supported format, false otherwise
Throws:
IOException - if the file cannot be read

canLoad

public boolean canLoad(URL imageUrl)
                throws IOException
Tests whether the given URL refers to an image in a format supported by this factory.

Specified by:
canLoad in interface ImageFactory
Parameters:
imageUrl - the URL to test
Returns:
true if the image URL refers a file in a supported format, false otherwise
Throws:
IOException - if the URL cannot be read

loadImage

public GenericImage loadImage(File imageFile)
                       throws IOException,
                              UnknownFormatException
Load an image from the given file name.

Specified by:
loadImage in interface ImageFactory
Parameters:
imageFile - the image to load
Returns:
a GenericImage
Throws:
IOException - if there's an error reading the file
UnknownFormatException - if the file format is not known

loadImage

public GenericImage loadImage(URL imageUrl)
                       throws IOException
Load an image from the given URL.

Specified by:
loadImage in interface ImageFactory
Parameters:
imageUrl - the image to load
Returns:
a GenericImage
Throws:
IOException - if there's an error reading the file
UnknownFormatException - if the file format is not known

testFormat

private void testFormat(InputStream stream)
                 throws IOException,
                        UnknownFormatException
Test the given input stream's format and throw an UnknownFormatException if it's not in a supported format.

Parameters:
stream - the stream to test
Throws:
IOException - if there's an error reading the stream
UnknownFormatException - if the stream's format is not supported

canLoad

private boolean canLoad(InputStream imageStream)
                 throws IOException
Test the given input stream's format and return an indication of whether the format is supported or not.

Parameters:
imageStream - the stream to test
Returns:
true if the stream is in a supported format, false otherwise
Throws:
IOException - if there's an error reading the stream

createImage

private static GenericImage createImage(Image javaImage)
                                 throws IOException
Throws:
IOException