package org.expeditee.core; import java.io.File; import java.io.IOException; import java.io.Serializable; import java.net.HttpURLConnection; import java.net.URL; import org.expeditee.core.bounds.AxisAlignedBoxBounds; import org.expeditee.encryption.core.EncryptedImage; import org.expeditee.gio.ImageManager; import com.sun.pdfview.PDFPage; /** * A handle to an internal-type image maintained by the ImageManager. * * @author cts16 */ public class Image implements Serializable { /** Auto-generated serialisation ID#; */ private static final long serialVersionUID = -5716308599893528323L; /** Integer representing an invalid size for width or height. */ public static final int INVALID_SIZE = -1; /** The ImageManager used to perform platform-specific image interactions. */ protected static ImageManager _manager = null; /** Sets the platform-specific ImageManager for all images. Can only be set once. */ public static void setManager(ImageManager manager) { if (_manager == null) _manager = manager; } /** Gets a reference to the designated platform-specific image manager. */ public static ImageManager getManager() { return _manager; } /** The handle ID# to use for the next-created image. */ private static long nextHandle = 1; /** The handle ID# for this image. */ private long _handle; /** Whether this image has a corresponding file on disk */ private boolean _isStoredOnDisk; /** Whether or not this image is animated (e.g. a GIF). */ private boolean _animated = false; /** The width of this image. */ private int _width = INVALID_SIZE; /** The height of this image. */ private int _height = INVALID_SIZE; /** * Controls creation of image handles. Will only return null until an ImageManager is provided * with Image.setManager(ImageManager). This should only be used by the ImageManager to * create a handle to associate with a real internal image. Handles created by anyone else * will not be accepted by the image manager. */ public static Image get(boolean fromDisk) { // Return null until an image manager is available if (_manager == null) return null; return new Image(fromDisk); } /** Private constructor as instantiation handled through Image.get(boolean). */ protected Image(boolean fromDisk) { // Assign the next available handle ID number _handle = nextHandle; nextHandle++; // Remember if this image is stored on disk or not _isStoredOnDisk = fromDisk; } /** Returns the ID# for this handle. */ public long getHandle() { return _handle; } /** Returns whether or not this image is stored on disk. */ public boolean isStoredOnDisk() { return _isStoredOnDisk; } /** Sets the animated property of this image. */ public void setIsAnimated(boolean isAnimated) { _animated = isAnimated; } /** Returns whether this image is animated. */ public boolean isAnimated() { return _animated; } /** Convenience access to ImageManager.createImage(Dimension). */ public static Image createImage(Dimension size) { return _manager.createImage(size); } /** Convenience access to ImageManager.createImage(int, int). */ public static Image createImage(int width, int height) { return _manager.createImage(width, height); } /** Convenience access to ImageManager.createImage(Dimension, boolean). */ public static Image createImage(Dimension size, boolean hintUseAcceleratedMemory) { return _manager.createImage(size, hintUseAcceleratedMemory); } /** Convenience access to ImageManager.createImage(int, int, boolean). */ public static Image createImage(int width, int height, boolean hintUseAcceleratedMemory) { return _manager.createImage(width, height, hintUseAcceleratedMemory); } /** Convenience access to ImageManager.createImage(int, int, int[]). */ public static Image createImage(int width, int height, int[] pixelData) { return _manager.createImage(width, height, pixelData); } /** Convenience access to ImageManager.createImage(int, int, PDFPage). */ public static Image createImage(int width, int height, PDFPage page) { return _manager.createImage(width, height, page); } /** Convenience access to ImageManager.createImageAsCroppedCopy(Image, int, int, int, int). */ public static Image createImageAsCroppedCopy(Image orig, int x, int y, int width, int height) { return _manager.createImageAsCroppedCopy(orig, x, y, width, height); } /** Convenience access to ImageManager.getImage(URL). */ public static Image getImage(URL url) { return _manager.getImage(url); } /** Convenience access to ImageManager.getImage(String). */ public static Image getImage(String filename) { if (filename.endsWith(EncryptedImage.EXPEDITEE_ENCRYPTED_IMAGE_EXTENSION)) { return EncryptedImage.getImage(filename); } else { return _manager.getImage(filename); } } /** Convenience access to ImageManager.getImage(HttpURLConnection). */ public static Image getImage(HttpURLConnection connection) throws IOException { return _manager.getImage(connection); } /** Convenience access to ImageManager.releaseImage(Image). */ public void releaseImage() { _manager.releaseImage(this); } /** Convenience access to ImageManager.isImageValid(Image). */ public boolean isValid() { return _manager.isImageValid(this); } /** Gets the width of this image. */ public int getWidth() { // Cache the width of the image if its isn't already if (_width == INVALID_SIZE) _width = _manager.getWidth(this); return _width; } /** Gets the height of this image. */ public int getHeight() { // Cache the height of the image if its isn't already if (_height == INVALID_SIZE) _height = _manager.getHeight(this); return _height; } /** Gets the size of this image. */ public Dimension getSize() { return new Dimension(getWidth(), getHeight()); } /** Convenience access to ImageManager.getPixel(Image, int, int). */ public Colour getPixel(int x, int y) { return _manager.getPixel(this, x, y); } /** Convenience access to ImageManager.setPixel(Image, int, int, Colour). */ public void setPixel(int x, int y, Colour c) { _manager.setPixel(this, x, y, c); } /** Convenience access to ImageManager.writeToDisk(Image, String, File). */ public boolean writeToDisk(String format, File file) throws IOException { return _manager.writeImageToDisk(this, format, file); } /** Returns the bounds of this image. */ public AxisAlignedBoxBounds getBounds() { return new AxisAlignedBoxBounds(0, 0, getWidth(), getHeight()); } @Override protected void finalize() { // Tell the image manager that nobody's using this image anymore releaseImage(); } }