comparison dwt/graphics/ImageLoader.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 module dwt.graphics.ImageLoader;
12
13 import dwt.dwthelper.utils;
14
15
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.OutputStream;
19 import java.util.Vector;
20
21 import dwt.DWT;
22 import dwt.DWTException;
23 import dwt.internal.Compatibility;
24 import dwt.internal.image.FileFormat;
25
26 /**
27 * Instances of this class are used to load images from,
28 * and save images to, a file or stream.
29 * <p>
30 * Currently supported image formats are:
31 * </p><ul>
32 * <li>BMP (Windows or OS/2 Bitmap)</li>
33 * <li>ICO (Windows Icon)</li>
34 * <li>JPEG</li>
35 * <li>GIF</li>
36 * <li>PNG</li>
37 * <li>TIFF</li>
38 * </ul>
39 * <code>ImageLoaders</code> can be used to:
40 * <ul>
41 * <li>load/save single images in all formats</li>
42 * <li>load/save multiple images (GIF/ICO/TIFF)</li>
43 * <li>load/save animated GIF images</li>
44 * <li>load interlaced GIF/PNG images</li>
45 * <li>load progressive JPEG images</li>
46 * </ul>
47 */
48
49 public class ImageLoader {
50
51 /**
52 * the array of ImageData objects in this ImageLoader.
53 * This array is read in when the load method is called,
54 * and it is written out when the save method is called
55 */
56 public ImageData[] data;
57
58 /**
59 * the width of the logical screen on which the images
60 * reside, in pixels (this corresponds to the GIF89a
61 * Logical Screen Width value)
62 */
63 public int logicalScreenWidth;
64
65 /**
66 * the height of the logical screen on which the images
67 * reside, in pixels (this corresponds to the GIF89a
68 * Logical Screen Height value)
69 */
70 public int logicalScreenHeight;
71
72 /**
73 * the background pixel for the logical screen (this
74 * corresponds to the GIF89a Background Color Index value).
75 * The default is -1 which means 'unspecified background'
76 *
77 */
78 public int backgroundPixel;
79
80 /**
81 * the number of times to repeat the display of a sequence
82 * of animated images (this corresponds to the commonly-used
83 * GIF application extension for "NETSCAPE 2.0 01").
84 * The default is 1. A value of 0 means 'display repeatedly'
85 */
86 public int repeatCount;
87
88 /*
89 * the set of ImageLoader event listeners, created on demand
90 */
91 Vector imageLoaderListeners;
92
93 /**
94 * Construct a new empty ImageLoader.
95 */
96 public ImageLoader() {
97 reset();
98 }
99
100 /**
101 * Resets the fields of the ImageLoader, except for the
102 * <code>imageLoaderListeners</code> field.
103 */
104 void reset() {
105 data = null;
106 logicalScreenWidth = 0;
107 logicalScreenHeight = 0;
108 backgroundPixel = -1;
109 repeatCount = 1;
110 }
111
112 /**
113 * Loads an array of <code>ImageData</code> objects from the
114 * specified input stream. Throws an error if either an error
115 * occurs while loading the images, or if the images are not
116 * of a supported type. Returns the loaded image data array.
117 *
118 * @param stream the input stream to load the images from
119 * @return an array of <code>ImageData</code> objects loaded from the specified input stream
120 *
121 * @exception IllegalArgumentException <ul>
122 * <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
123 * </ul>
124 * @exception DWTException <ul>
125 * <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
126 * <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
127 * <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
128 * </ul>
129 */
130 public ImageData[] load(InputStream stream) {
131 if (stream is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
132 reset();
133 data = FileFormat.load(stream, this);
134 return data;
135 }
136
137 /**
138 * Loads an array of <code>ImageData</code> objects from the
139 * file with the specified name. Throws an error if either
140 * an error occurs while loading the images, or if the images are
141 * not of a supported type. Returns the loaded image data array.
142 *
143 * @param filename the name of the file to load the images from
144 * @return an array of <code>ImageData</code> objects loaded from the specified file
145 *
146 * @exception IllegalArgumentException <ul>
147 * <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
148 * </ul>
149 * @exception DWTException <ul>
150 * <li>ERROR_IO - if an IO error occurs while reading from the file</li>
151 * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
152 * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
153 * </ul>
154 */
155 public ImageData[] load(String filename) {
156 if (filename is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
157 InputStream stream = null;
158 try {
159 stream = Compatibility.newFileInputStream(filename);
160 return load(stream);
161 } catch (IOException e) {
162 DWT.error(DWT.ERROR_IO, e);
163 } finally {
164 try {
165 if (stream !is null) stream.close();
166 } catch (IOException e) {
167 // Ignore error
168 }
169 }
170 return null;
171 }
172
173 /**
174 * Saves the image data in this ImageLoader to the specified stream.
175 * The format parameter can have one of the following values:
176 * <dl>
177 * <dt><code>IMAGE_BMP</code></dt>
178 * <dd>Windows BMP file format, no compression</dd>
179 * <dt><code>IMAGE_BMP_RLE</code></dt>
180 * <dd>Windows BMP file format, RLE compression if appropriate</dd>
181 * <dt><code>IMAGE_GIF</code></dt>
182 * <dd>GIF file format</dd>
183 * <dt><code>IMAGE_ICO</code></dt>
184 * <dd>Windows ICO file format</dd>
185 * <dt><code>IMAGE_JPEG</code></dt>
186 * <dd>JPEG file format</dd>
187 * <dt><code>IMAGE_PNG</code></dt>
188 * <dd>PNG file format</dd>
189 * </dl>
190 *
191 * @param stream the output stream to write the images to
192 * @param format the format to write the images in
193 *
194 * @exception IllegalArgumentException <ul>
195 * <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
196 * </ul>
197 * @exception DWTException <ul>
198 * <li>ERROR_IO - if an IO error occurs while writing to the stream</li>
199 * <li>ERROR_INVALID_IMAGE - if the image data contains invalid data</li>
200 * <li>ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format</li>
201 * </ul>
202 */
203 public void save(OutputStream stream, int format) {
204 if (stream is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
205 FileFormat.save(stream, format, this);
206 }
207
208 /**
209 * Saves the image data in this ImageLoader to a file with the specified name.
210 * The format parameter can have one of the following values:
211 * <dl>
212 * <dt><code>IMAGE_BMP</code></dt>
213 * <dd>Windows BMP file format, no compression</dd>
214 * <dt><code>IMAGE_BMP_RLE</code></dt>
215 * <dd>Windows BMP file format, RLE compression if appropriate</dd>
216 * <dt><code>IMAGE_GIF</code></dt>
217 * <dd>GIF file format</dd>
218 * <dt><code>IMAGE_ICO</code></dt>
219 * <dd>Windows ICO file format</dd>
220 * <dt><code>IMAGE_JPEG</code></dt>
221 * <dd>JPEG file format</dd>
222 * <dt><code>IMAGE_PNG</code></dt>
223 * <dd>PNG file format</dd>
224 * </dl>
225 *
226 * @param filename the name of the file to write the images to
227 * @param format the format to write the images in
228 *
229 * @exception IllegalArgumentException <ul>
230 * <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
231 * </ul>
232 * @exception DWTException <ul>
233 * <li>ERROR_IO - if an IO error occurs while writing to the file</li>
234 * <li>ERROR_INVALID_IMAGE - if the image data contains invalid data</li>
235 * <li>ERROR_UNSUPPORTED_FORMAT - if the image data cannot be saved to the requested format</li>
236 * </ul>
237 */
238 public void save(String filename, int format) {
239 if (filename is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
240 OutputStream stream = null;
241 try {
242 stream = Compatibility.newFileOutputStream(filename);
243 } catch (IOException e) {
244 DWT.error(DWT.ERROR_IO, e);
245 }
246 save(stream, format);
247 try {
248 stream.close();
249 } catch (IOException e) {
250 }
251 }
252
253 /**
254 * Adds the listener to the collection of listeners who will be
255 * notified when image data is either partially or completely loaded.
256 * <p>
257 * An ImageLoaderListener should be added before invoking
258 * one of the receiver's load methods. The listener's
259 * <code>imageDataLoaded</code> method is called when image
260 * data has been partially loaded, as is supported by interlaced
261 * GIF/PNG or progressive JPEG images.
262 *
263 * @param listener the listener which should be notified
264 *
265 * @exception IllegalArgumentException <ul>
266 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
267 * </ul>
268 *
269 * @see ImageLoaderListener
270 * @see ImageLoaderEvent
271 */
272 public void addImageLoaderListener(ImageLoaderListener listener) {
273 if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
274 if (imageLoaderListeners is null) {
275 imageLoaderListeners = new Vector();
276 }
277 imageLoaderListeners.addElement(listener);
278 }
279
280 /**
281 * Removes the listener from the collection of listeners who will be
282 * notified when image data is either partially or completely loaded.
283 *
284 * @param listener the listener which should no longer be notified
285 *
286 * @exception IllegalArgumentException <ul>
287 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
288 * </ul>
289 *
290 * @see #addImageLoaderListener(ImageLoaderListener)
291 */
292 public void removeImageLoaderListener(ImageLoaderListener listener) {
293 if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
294 if (imageLoaderListeners is null) return;
295 imageLoaderListeners.removeElement(listener);
296 }
297
298 /**
299 * Returns <code>true</code> if the receiver has image loader
300 * listeners, and <code>false</code> otherwise.
301 *
302 * @return <code>true</code> if there are <code>ImageLoaderListener</code>s, and <code>false</code> otherwise
303 *
304 * @see #addImageLoaderListener(ImageLoaderListener)
305 * @see #removeImageLoaderListener(ImageLoaderListener)
306 */
307 public bool hasListeners() {
308 return imageLoaderListeners !is null && imageLoaderListeners.size() > 0;
309 }
310
311 /**
312 * Notifies all image loader listeners that an image loader event
313 * has occurred. Pass the specified event object to each listener.
314 *
315 * @param event the <code>ImageLoaderEvent</code> to send to each <code>ImageLoaderListener</code>
316 */
317 public void notifyListeners(ImageLoaderEvent event) {
318 if (!hasListeners()) return;
319 int size = imageLoaderListeners.size();
320 for (int i = 0; i < size; i++) {
321 ImageLoaderListener listener = (ImageLoaderListener) imageLoaderListeners.elementAt(i);
322 listener.imageDataLoaded(event);
323 }
324 }
325
326 }