[1097] | 1 | package org.expeditee.core;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 | import java.io.IOException;
|
---|
| 5 | import java.io.Serializable;
|
---|
| 6 | import java.net.HttpURLConnection;
|
---|
| 7 | import java.net.URL;
|
---|
| 8 |
|
---|
| 9 | import org.expeditee.core.bounds.AxisAlignedBoxBounds;
|
---|
| 10 | import org.expeditee.gio.ImageManager;
|
---|
| 11 |
|
---|
| 12 | import com.sun.pdfview.PDFPage;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * A handle to an internal-type image maintained by the ImageManager.
|
---|
| 16 | *
|
---|
| 17 | * @author cts16
|
---|
| 18 | */
|
---|
| 19 | public class Image implements Serializable {
|
---|
| 20 |
|
---|
| 21 | /** Auto-generated serialisation ID#; */
|
---|
| 22 | private static final long serialVersionUID = -5716308599893528323L;
|
---|
| 23 |
|
---|
| 24 | /** Integer representing an invalid size for width or height. */
|
---|
| 25 | public static final int INVALID_SIZE = -1;
|
---|
| 26 |
|
---|
| 27 | /** The ImageManager used to perform platform-specific image interactions. */
|
---|
| 28 | private static ImageManager _manager = null;
|
---|
| 29 |
|
---|
| 30 | /** Sets the platform-specific ImageManager for all images. Can only be set once. */
|
---|
| 31 | public static void setManager(ImageManager manager)
|
---|
| 32 | {
|
---|
| 33 | if (_manager == null) _manager = manager;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | /** Gets a reference to the designated platform-specific image manager. */
|
---|
| 37 | public static ImageManager getManager()
|
---|
| 38 | {
|
---|
| 39 | return _manager;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | /** The handle ID# to use for the next-created image. */
|
---|
| 43 | private static long nextHandle = 1;
|
---|
| 44 |
|
---|
| 45 | /** The handle ID# for this image. */
|
---|
| 46 | private long _handle;
|
---|
| 47 |
|
---|
| 48 | /** Whether this image has a corresponding file on disk */
|
---|
| 49 | private boolean _isStoredOnDisk;
|
---|
| 50 |
|
---|
| 51 | /** Whether or not this image is animated (e.g. a GIF). */
|
---|
| 52 | private boolean _animated = false;
|
---|
| 53 |
|
---|
| 54 | /** The width of this image. */
|
---|
| 55 | private int _width = INVALID_SIZE;
|
---|
| 56 | /** The height of this image. */
|
---|
| 57 | private int _height = INVALID_SIZE;
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * Controls creation of image handles. Will only return null until an ImageManager is provided
|
---|
| 61 | * with Image.setManager(ImageManager). This should only be used by the ImageManager to
|
---|
| 62 | * create a handle to associate with a real internal image. Handles created by anyone else
|
---|
| 63 | * will not be accepted by the image manager.
|
---|
| 64 | */
|
---|
| 65 | public static Image get(boolean fromDisk)
|
---|
| 66 | {
|
---|
| 67 | // Return null until an image manager is available
|
---|
| 68 | if (_manager == null) return null;
|
---|
| 69 |
|
---|
| 70 | return new Image(fromDisk);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /** Private constructor as instantiation handled through Image.get(boolean). */
|
---|
| 74 | private Image(boolean fromDisk)
|
---|
| 75 | {
|
---|
| 76 | // Assign the next available handle ID number
|
---|
| 77 | _handle = nextHandle;
|
---|
| 78 | nextHandle++;
|
---|
| 79 |
|
---|
| 80 | // Remember if this image is stored on disk or not
|
---|
| 81 | _isStoredOnDisk = fromDisk;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /** Returns the ID# for this handle. */
|
---|
| 85 | public long getHandle()
|
---|
| 86 | {
|
---|
| 87 | return _handle;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /** Returns whether or not this image is stored on disk. */
|
---|
| 91 | public boolean isStoredOnDisk()
|
---|
| 92 | {
|
---|
| 93 | return _isStoredOnDisk;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /** Sets the animated property of this image. */
|
---|
| 97 | public void setIsAnimated(boolean isAnimated)
|
---|
| 98 | {
|
---|
| 99 | _animated = isAnimated;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /** Returns whether this image is animated. */
|
---|
| 103 | public boolean isAnimated()
|
---|
| 104 | {
|
---|
| 105 | return _animated;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /** Convenience access to ImageManager.createImage(Dimension). */
|
---|
| 109 | public static Image createImage(Dimension size)
|
---|
| 110 | {
|
---|
| 111 | return _manager.createImage(size);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /** Convenience access to ImageManager.createImage(int, int). */
|
---|
| 115 | public static Image createImage(int width, int height)
|
---|
| 116 | {
|
---|
| 117 | return _manager.createImage(width, height);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /** Convenience access to ImageManager.createImage(Dimension, boolean). */
|
---|
| 121 | public static Image createImage(Dimension size, boolean hintUseAcceleratedMemory)
|
---|
| 122 | {
|
---|
| 123 | return _manager.createImage(size, hintUseAcceleratedMemory);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /** Convenience access to ImageManager.createImage(int, int, boolean). */
|
---|
| 127 | public static Image createImage(int width, int height, boolean hintUseAcceleratedMemory)
|
---|
| 128 | {
|
---|
| 129 | return _manager.createImage(width, height, hintUseAcceleratedMemory);
|
---|
| 130 | }
|
---|
| 131 | /** Convenience access to ImageManager.createImage(int, int, int[]). */
|
---|
| 132 | public static Image createImage(int width, int height, int[] pixelData)
|
---|
| 133 | {
|
---|
| 134 | return _manager.createImage(width, height, pixelData);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /** Convenience access to ImageManager.createImage(int, int, PDFPage). */
|
---|
| 138 | public static Image createImage(int width, int height, PDFPage page)
|
---|
| 139 | {
|
---|
| 140 | return _manager.createImage(width, height, page);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /** Convenience access to ImageManager.createImageAsCroppedCopy(Image, int, int, int, int). */
|
---|
| 144 | public static Image createImageAsCroppedCopy(Image orig, int x, int y, int width, int height)
|
---|
| 145 | {
|
---|
| 146 | return _manager.createImageAsCroppedCopy(orig, x, y, width, height);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[1426] | 149 | public static Image getNoise() {
|
---|
| 150 | return _manager.getNoise();
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[1097] | 153 | /** Convenience access to ImageManager.getImage(URL). */
|
---|
| 154 | public static Image getImage(URL url)
|
---|
| 155 | {
|
---|
| 156 | return _manager.getImage(url);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /** Convenience access to ImageManager.getImage(String). */
|
---|
| 160 | public static Image getImage(String filename)
|
---|
| 161 | {
|
---|
| 162 | return _manager.getImage(filename);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /** Convenience access to ImageManager.getImage(HttpURLConnection). */
|
---|
| 166 | public static Image getImage(HttpURLConnection connection) throws IOException
|
---|
| 167 | {
|
---|
| 168 | return _manager.getImage(connection);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /** Convenience access to ImageManager.releaseImage(Image). */
|
---|
| 172 | public void releaseImage()
|
---|
| 173 | {
|
---|
| 174 | _manager.releaseImage(this);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /** Convenience access to ImageManager.isImageValid(Image). */
|
---|
| 178 | public boolean isValid()
|
---|
| 179 | {
|
---|
| 180 | return _manager.isImageValid(this);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /** Gets the width of this image. */
|
---|
| 184 | public int getWidth()
|
---|
| 185 | {
|
---|
| 186 | // Cache the width of the image if its isn't already
|
---|
| 187 | if (_width == INVALID_SIZE) _width = _manager.getWidth(this);
|
---|
| 188 |
|
---|
| 189 | return _width;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /** Gets the height of this image. */
|
---|
| 193 | public int getHeight()
|
---|
| 194 | {
|
---|
| 195 | // Cache the height of the image if its isn't already
|
---|
| 196 | if (_height == INVALID_SIZE) _height = _manager.getHeight(this);
|
---|
| 197 |
|
---|
| 198 | return _height;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /** Gets the size of this image. */
|
---|
| 202 | public Dimension getSize()
|
---|
| 203 | {
|
---|
| 204 | return new Dimension(getWidth(), getHeight());
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /** Convenience access to ImageManager.getPixel(Image, int, int). */
|
---|
| 208 | public Colour getPixel(int x, int y)
|
---|
| 209 | {
|
---|
| 210 | return _manager.getPixel(this, x, y);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | /** Convenience access to ImageManager.setPixel(Image, int, int, Colour). */
|
---|
| 214 | public void setPixel(int x, int y, Colour c)
|
---|
| 215 | {
|
---|
| 216 | _manager.setPixel(this, x, y, c);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /** Convenience access to ImageManager.writeToDisk(Image, String, File). */
|
---|
| 220 | public boolean writeToDisk(String format, File file) throws IOException
|
---|
| 221 | {
|
---|
| 222 | return _manager.writeImageToDisk(this, format, file);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /** Returns the bounds of this image. */
|
---|
| 226 | public AxisAlignedBoxBounds getBounds()
|
---|
| 227 | {
|
---|
| 228 | return new AxisAlignedBoxBounds(0, 0, getWidth(), getHeight());
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | @Override
|
---|
| 232 | protected void finalize()
|
---|
| 233 | {
|
---|
| 234 | // Tell the image manager that nobody's using this image anymore
|
---|
| 235 | releaseImage();
|
---|
| 236 | }
|
---|
| 237 | }
|
---|