comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/graphics/ImageLoader.d @ 0:6dd524f61e62

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