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