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.encryption.core.EncryptedImage;
|
---|
11 | import org.expeditee.gio.ImageManager;
|
---|
12 |
|
---|
13 | import com.sun.pdfview.PDFPage;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * A handle to an internal-type image maintained by the ImageManager.
|
---|
17 | *
|
---|
18 | * @author cts16
|
---|
19 | */
|
---|
20 | public class Image implements Serializable {
|
---|
21 |
|
---|
22 | /** Auto-generated serialisation ID#; */
|
---|
23 | private static final long serialVersionUID = -5716308599893528323L;
|
---|
24 |
|
---|
25 | /** Integer representing an invalid size for width or height. */
|
---|
26 | public static final int INVALID_SIZE = -1;
|
---|
27 |
|
---|
28 | /** The ImageManager used to perform platform-specific image interactions. */
|
---|
29 | protected static ImageManager _manager = null;
|
---|
30 |
|
---|
31 | /** Sets the platform-specific ImageManager for all images. Can only be set once. */
|
---|
32 | public static void setManager(ImageManager manager)
|
---|
33 | {
|
---|
34 | if (_manager == null) _manager = manager;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /** Gets a reference to the designated platform-specific image manager. */
|
---|
38 | public static ImageManager getManager()
|
---|
39 | {
|
---|
40 | return _manager;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /** The handle ID# to use for the next-created image. */
|
---|
44 | private static long nextHandle = 1;
|
---|
45 |
|
---|
46 | /** The handle ID# for this image. */
|
---|
47 | private long _handle;
|
---|
48 |
|
---|
49 | /** Whether this image has a corresponding file on disk */
|
---|
50 | private boolean _isStoredOnDisk;
|
---|
51 |
|
---|
52 | /** Whether or not this image is animated (e.g. a GIF). */
|
---|
53 | private boolean _animated = false;
|
---|
54 |
|
---|
55 | /** The width of this image. */
|
---|
56 | private int _width = INVALID_SIZE;
|
---|
57 | /** The height of this image. */
|
---|
58 | private int _height = INVALID_SIZE;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Controls creation of image handles. Will only return null until an ImageManager is provided
|
---|
62 | * with Image.setManager(ImageManager). This should only be used by the ImageManager to
|
---|
63 | * create a handle to associate with a real internal image. Handles created by anyone else
|
---|
64 | * will not be accepted by the image manager.
|
---|
65 | */
|
---|
66 | public static Image get(boolean fromDisk)
|
---|
67 | {
|
---|
68 | // Return null until an image manager is available
|
---|
69 | if (_manager == null) return null;
|
---|
70 |
|
---|
71 | return new Image(fromDisk);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /** Private constructor as instantiation handled through Image.get(boolean). */
|
---|
75 | protected Image(boolean fromDisk) {
|
---|
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 |
|
---|
149 | /** Convenience access to ImageManager.getImage(URL). */
|
---|
150 | public static Image getImage(URL url)
|
---|
151 | {
|
---|
152 | return _manager.getImage(url);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /** Convenience access to ImageManager.getImage(String). */
|
---|
156 | public static Image getImage(String filename) {
|
---|
157 | if (filename.endsWith(EncryptedImage.EXPEDITEE_ENCRYPTED_IMAGE_EXTENSION)) {
|
---|
158 | return EncryptedImage.getImage(filename);
|
---|
159 | } else {
|
---|
160 | return _manager.getImage(filename);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** Convenience access to ImageManager.getImage(HttpURLConnection). */
|
---|
165 | public static Image getImage(HttpURLConnection connection) throws IOException
|
---|
166 | {
|
---|
167 | return _manager.getImage(connection);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /** Convenience access to ImageManager.releaseImage(Image). */
|
---|
171 | public void releaseImage()
|
---|
172 | {
|
---|
173 | _manager.releaseImage(this);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /** Convenience access to ImageManager.isImageValid(Image). */
|
---|
177 | public boolean isValid()
|
---|
178 | {
|
---|
179 | return _manager.isImageValid(this);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Gets the width of this image. */
|
---|
183 | public int getWidth()
|
---|
184 | {
|
---|
185 | // Cache the width of the image if its isn't already
|
---|
186 | if (_width == INVALID_SIZE) _width = _manager.getWidth(this);
|
---|
187 |
|
---|
188 | return _width;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /** Gets the height of this image. */
|
---|
192 | public int getHeight()
|
---|
193 | {
|
---|
194 | // Cache the height of the image if its isn't already
|
---|
195 | if (_height == INVALID_SIZE) _height = _manager.getHeight(this);
|
---|
196 |
|
---|
197 | return _height;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /** Gets the size of this image. */
|
---|
201 | public Dimension getSize()
|
---|
202 | {
|
---|
203 | return new Dimension(getWidth(), getHeight());
|
---|
204 | }
|
---|
205 |
|
---|
206 | /** Convenience access to ImageManager.getPixel(Image, int, int). */
|
---|
207 | public Colour getPixel(int x, int y)
|
---|
208 | {
|
---|
209 | return _manager.getPixel(this, x, y);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** Convenience access to ImageManager.setPixel(Image, int, int, Colour). */
|
---|
213 | public void setPixel(int x, int y, Colour c)
|
---|
214 | {
|
---|
215 | _manager.setPixel(this, x, y, c);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /** Convenience access to ImageManager.writeToDisk(Image, String, File). */
|
---|
219 | public boolean writeToDisk(String format, File file) throws IOException
|
---|
220 | {
|
---|
221 | return _manager.writeImageToDisk(this, format, file);
|
---|
222 | }
|
---|
223 |
|
---|
224 | /** Returns the bounds of this image. */
|
---|
225 | public AxisAlignedBoxBounds getBounds()
|
---|
226 | {
|
---|
227 | return new AxisAlignedBoxBounds(0, 0, getWidth(), getHeight());
|
---|
228 | }
|
---|
229 |
|
---|
230 | @Override
|
---|
231 | protected void finalize()
|
---|
232 | {
|
---|
233 | // Tell the image manager that nobody's using this image anymore
|
---|
234 | releaseImage();
|
---|
235 | }
|
---|
236 | }
|
---|