Mercurial > projects > dwt-mac
comparison dwt/graphics/ImageLoader.d @ 32:b9226997409c
Ported dwt.graphics.Image*
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Fri, 12 Sep 2008 13:44:30 +0200 |
parents | e831403a80a9 |
children | d8635bb48c7c |
comparison
equal
deleted
inserted
replaced
31:9a3047e87f1d | 32:b9226997409c |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2006 IBM Corporation and others. | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 * Jacob Carlborg <jacob.carlborg@gmail.com> | |
10 *******************************************************************************/ | 13 *******************************************************************************/ |
11 module dwt.graphics.ImageLoader; | 14 module dwt.graphics.ImageLoader; |
12 | 15 |
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 | 16 |
21 import dwt.DWT; | 17 import dwt.DWT; |
22 import dwt.DWTException; | |
23 import dwt.internal.Compatibility; | 18 import dwt.internal.Compatibility; |
24 import dwt.internal.image.FileFormat; | 19 import dwt.internal.image.FileFormat; |
20 | |
21 import tango.core.Exception; | |
22 import tango.core.Array; | |
23 | |
24 import dwt.dwthelper.utils; | |
25 public import dwt.graphics.ImageData; | |
26 public import dwt.graphics.ImageLoaderEvent; | |
27 public import dwt.graphics.ImageLoaderListener; | |
25 | 28 |
26 /** | 29 /** |
27 * Instances of this class are used to load images from, | 30 * Instances of this class are used to load images from, |
28 * and save images to, a file or stream. | 31 * and save images to, a file or stream. |
29 * <p> | 32 * <p> |
42 * <li>load/save multiple images (GIF/ICO/TIFF)</li> | 45 * <li>load/save multiple images (GIF/ICO/TIFF)</li> |
43 * <li>load/save animated GIF images</li> | 46 * <li>load/save animated GIF images</li> |
44 * <li>load interlaced GIF/PNG images</li> | 47 * <li>load interlaced GIF/PNG images</li> |
45 * <li>load progressive JPEG images</li> | 48 * <li>load progressive JPEG images</li> |
46 * </ul> | 49 * </ul> |
47 */ | 50 * |
48 | 51 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ImageAnalyzer</a> |
52 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
53 */ | |
54 | |
49 public class ImageLoader { | 55 public class ImageLoader { |
50 | 56 |
51 /** | 57 /** |
52 * the array of ImageData objects in this ImageLoader. | 58 * the array of ImageData objects in this ImageLoader. |
53 * This array is read in when the load method is called, | 59 * This array is read in when the load method is called, |
54 * and it is written out when the save method is called | 60 * and it is written out when the save method is called |
55 */ | 61 */ |
56 public ImageData[] data; | 62 public ImageData[] data; |
57 | 63 |
58 /** | 64 /** |
59 * the width of the logical screen on which the images | 65 * the width of the logical screen on which the images |
60 * reside, in pixels (this corresponds to the GIF89a | 66 * reside, in pixels (this corresponds to the GIF89a |
61 * Logical Screen Width value) | 67 * Logical Screen Width value) |
62 */ | 68 */ |
68 * Logical Screen Height value) | 74 * Logical Screen Height value) |
69 */ | 75 */ |
70 public int logicalScreenHeight; | 76 public int logicalScreenHeight; |
71 | 77 |
72 /** | 78 /** |
73 * the background pixel for the logical screen (this | 79 * the background pixel for the logical screen (this |
74 * corresponds to the GIF89a Background Color Index value). | 80 * corresponds to the GIF89a Background Color Index value). |
75 * The default is -1 which means 'unspecified background' | 81 * The default is -1 which means 'unspecified background' |
76 * | 82 * |
77 */ | 83 */ |
78 public int backgroundPixel; | 84 public int backgroundPixel; |
79 | 85 |
80 /** | 86 /** |
81 * the number of times to repeat the display of a sequence | 87 * the number of times to repeat the display of a sequence |
82 * of animated images (this corresponds to the commonly-used | 88 * of animated images (this corresponds to the commonly-used |
83 * GIF application extension for "NETSCAPE 2.0 01"). | 89 * GIF application extension for "NETSCAPE 2.0 01"). |
84 * The default is 1. A value of 0 means 'display repeatedly' | 90 * The default is 1. A value of 0 means 'display repeatedly' |
85 */ | 91 */ |
86 public int repeatCount; | 92 public int repeatCount; |
87 | 93 |
88 /* | 94 /* |
89 * the set of ImageLoader event listeners, created on demand | 95 * the set of ImageLoader event listeners, created on demand |
90 */ | 96 */ |
91 Vector imageLoaderListeners; | 97 ImageLoaderListener[] imageLoaderListeners; |
92 | 98 |
93 /** | 99 /** |
94 * Construct a new empty ImageLoader. | 100 * Construct a new empty ImageLoader. |
95 */ | 101 */ |
96 public this() { | 102 public this() { |
153 * </ul> | 159 * </ul> |
154 */ | 160 */ |
155 public ImageData[] load(String filename) { | 161 public ImageData[] load(String filename) { |
156 if (filename is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | 162 if (filename is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
157 InputStream stream = null; | 163 InputStream stream = null; |
164 void close(){ | |
165 try { | |
166 if( stream !is null ) stream.close(); | |
167 } catch (IOException e) { | |
168 // Ignore error | |
169 } | |
170 } | |
158 try { | 171 try { |
159 stream = Compatibility.newFileInputStream(filename); | 172 stream = Compatibility.newFileInputStream(filename); |
173 scope(exit) close(); | |
174 | |
160 return load(stream); | 175 return load(stream); |
161 } catch (IOException e) { | 176 } catch (IOException e) { |
162 DWT.error(DWT.ERROR_IO, e); | 177 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 } | 178 } |
170 return null; | 179 return null; |
171 } | 180 } |
172 | 181 |
173 /** | 182 /** |
248 stream.close(); | 257 stream.close(); |
249 } catch (IOException e) { | 258 } catch (IOException e) { |
250 } | 259 } |
251 } | 260 } |
252 | 261 |
253 /** | 262 /** |
254 * Adds the listener to the collection of listeners who will be | 263 * Adds the listener to the collection of listeners who will be |
255 * notified when image data is either partially or completely loaded. | 264 * notified when image data is either partially or completely loaded. |
256 * <p> | 265 * <p> |
257 * An ImageLoaderListener should be added before invoking | 266 * An ImageLoaderListener should be added before invoking |
258 * one of the receiver's load methods. The listener's | 267 * one of the receiver's load methods. The listener's |
259 * <code>imageDataLoaded</code> method is called when image | 268 * <code>imageDataLoaded</code> method is called when image |
260 * data has been partially loaded, as is supported by interlaced | 269 * data has been partially loaded, as is supported by interlaced |
261 * GIF/PNG or progressive JPEG images. | 270 * GIF/PNG or progressive JPEG images. |
262 * | 271 * |
263 * @param listener the listener which should be notified | 272 * @param listener the listener which should be notified |
264 * | 273 * |
265 * @exception IllegalArgumentException <ul> | 274 * @exception IllegalArgumentException <ul> |
266 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | 275 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> |
267 * </ul> | 276 * </ul> |
268 * | 277 * |
269 * @see ImageLoaderListener | 278 * @see ImageLoaderListener |
270 * @see ImageLoaderEvent | 279 * @see ImageLoaderEvent |
271 */ | 280 */ |
272 public void addImageLoaderListener(ImageLoaderListener listener) { | 281 public void addImageLoaderListener(ImageLoaderListener listener) { |
273 if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); | 282 if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); |
274 if (imageLoaderListeners is null) { | 283 imageLoaderListeners ~= listener; |
275 imageLoaderListeners = new Vector(); | 284 } |
276 } | 285 |
277 imageLoaderListeners.addElement(listener); | 286 /** |
278 } | |
279 | |
280 /** | |
281 * Removes the listener from the collection of listeners who will be | 287 * Removes the listener from the collection of listeners who will be |
282 * notified when image data is either partially or completely loaded. | 288 * notified when image data is either partially or completely loaded. |
283 * | 289 * |
284 * @param listener the listener which should no longer be notified | 290 * @param listener the listener which should no longer be notified |
285 * | 291 * |
286 * @exception IllegalArgumentException <ul> | 292 * @exception IllegalArgumentException <ul> |
287 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | 293 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> |
288 * </ul> | 294 * </ul> |
289 * | 295 * |
290 * @see #addImageLoaderListener(ImageLoaderListener) | 296 * @see #addImageLoaderListener(ImageLoaderListener) |
291 */ | 297 */ |
292 public void removeImageLoaderListener(ImageLoaderListener listener) { | 298 public void removeImageLoaderListener(ImageLoaderListener listener) { |
293 if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); | 299 if (listener is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); |
294 if (imageLoaderListeners is null) return; | 300 if (imageLoaderListeners.length is 0 ) return; |
295 imageLoaderListeners.removeElement(listener); | 301 tango.core.Array.remove( imageLoaderListeners, listener, delegate bool(ImageLoaderListener l1, ImageLoaderListener l2 ){ return l1 is l2; }); |
296 } | 302 } |
297 | 303 |
298 /** | 304 /** |
299 * Returns <code>true</code> if the receiver has image loader | 305 * Returns <code>true</code> if the receiver has image loader |
300 * listeners, and <code>false</code> otherwise. | 306 * listeners, and <code>false</code> otherwise. |
301 * | 307 * |
302 * @return <code>true</code> if there are <code>ImageLoaderListener</code>s, and <code>false</code> otherwise | 308 * @return <code>true</code> if there are <code>ImageLoaderListener</code>s, and <code>false</code> otherwise |
303 * | 309 * |
304 * @see #addImageLoaderListener(ImageLoaderListener) | 310 * @see #addImageLoaderListener(ImageLoaderListener) |
305 * @see #removeImageLoaderListener(ImageLoaderListener) | 311 * @see #removeImageLoaderListener(ImageLoaderListener) |
306 */ | 312 */ |
307 public bool hasListeners() { | 313 public bool hasListeners() { |
308 return imageLoaderListeners !is null && imageLoaderListeners.size() > 0; | 314 return imageLoaderListeners.length > 0; |
309 } | 315 } |
310 | 316 |
311 /** | 317 /** |
312 * Notifies all image loader listeners that an image loader event | 318 * Notifies all image loader listeners that an image loader event |
313 * has occurred. Pass the specified event object to each listener. | 319 * has occurred. Pass the specified event object to each listener. |
314 * | 320 * |
315 * @param event the <code>ImageLoaderEvent</code> to send to each <code>ImageLoaderListener</code> | 321 * @param event the <code>ImageLoaderEvent</code> to send to each <code>ImageLoaderListener</code> |
316 */ | 322 */ |
317 public void notifyListeners(ImageLoaderEvent event) { | 323 public void notifyListeners(ImageLoaderEvent event) { |
318 if (!hasListeners()) return; | 324 if (!hasListeners()) return; |
319 int size = imageLoaderListeners.size(); | 325 foreach( listener; imageLoaderListeners ){ |
320 for (int i = 0; i < size; i++) { | |
321 ImageLoaderListener listener = cast(ImageLoaderListener) imageLoaderListeners.elementAt(i); | |
322 listener.imageDataLoaded(event); | 326 listener.imageDataLoaded(event); |
323 } | 327 } |
324 } | 328 } |
325 | 329 |
326 } | 330 } |