source: trunk/src/org/expeditee/core/Image.java@ 1097

Last change on this file since 1097 was 1097, checked in by davidb, 6 years ago

Newly structured files from Corey's work on logic/graphics separation

File size: 6.3 KB
Line 
1package org.expeditee.core;
2
3import java.io.File;
4import java.io.IOException;
5import java.io.Serializable;
6import java.net.HttpURLConnection;
7import java.net.URL;
8
9import org.expeditee.core.bounds.AxisAlignedBoxBounds;
10import org.expeditee.gio.ImageManager;
11
12import com.sun.pdfview.PDFPage;
13
14/**
15 * A handle to an internal-type image maintained by the ImageManager.
16 *
17 * @author cts16
18 */
19public 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
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 {
158 return _manager.getImage(filename);
159 }
160
161 /** Convenience access to ImageManager.getImage(HttpURLConnection). */
162 public static Image getImage(HttpURLConnection connection) throws IOException
163 {
164 return _manager.getImage(connection);
165 }
166
167 /** Convenience access to ImageManager.releaseImage(Image). */
168 public void releaseImage()
169 {
170 _manager.releaseImage(this);
171 }
172
173 /** Convenience access to ImageManager.isImageValid(Image). */
174 public boolean isValid()
175 {
176 return _manager.isImageValid(this);
177 }
178
179 /** Gets the width of this image. */
180 public int getWidth()
181 {
182 // Cache the width of the image if its isn't already
183 if (_width == INVALID_SIZE) _width = _manager.getWidth(this);
184
185 return _width;
186 }
187
188 /** Gets the height of this image. */
189 public int getHeight()
190 {
191 // Cache the height of the image if its isn't already
192 if (_height == INVALID_SIZE) _height = _manager.getHeight(this);
193
194 return _height;
195 }
196
197 /** Gets the size of this image. */
198 public Dimension getSize()
199 {
200 return new Dimension(getWidth(), getHeight());
201 }
202
203 /** Convenience access to ImageManager.getPixel(Image, int, int). */
204 public Colour getPixel(int x, int y)
205 {
206 return _manager.getPixel(this, x, y);
207 }
208
209 /** Convenience access to ImageManager.setPixel(Image, int, int, Colour). */
210 public void setPixel(int x, int y, Colour c)
211 {
212 _manager.setPixel(this, x, y, c);
213 }
214
215 /** Convenience access to ImageManager.writeToDisk(Image, String, File). */
216 public boolean writeToDisk(String format, File file) throws IOException
217 {
218 return _manager.writeImageToDisk(this, format, file);
219 }
220
221 /** Returns the bounds of this image. */
222 public AxisAlignedBoxBounds getBounds()
223 {
224 return new AxisAlignedBoxBounds(0, 0, getWidth(), getHeight());
225 }
226
227 @Override
228 protected void finalize()
229 {
230 // Tell the image manager that nobody's using this image anymore
231 releaseImage();
232 }
233}
Note: See TracBrowser for help on using the repository browser.