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