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.Image;
|
|
14
|
|
15 import org.eclipse.swt.internal.Converter;
|
|
16 import org.eclipse.swt.internal.cairo.Cairo;
|
|
17 import org.eclipse.swt.internal.gtk.OS;
|
|
18 import org.eclipse.swt.SWT;
|
|
19 import org.eclipse.swt.SWTException;
|
|
20 import org.eclipse.swt.graphics.Color;
|
|
21 import org.eclipse.swt.graphics.Device;
|
|
22 import org.eclipse.swt.graphics.Drawable;
|
|
23 import org.eclipse.swt.graphics.GC;
|
|
24 import org.eclipse.swt.graphics.GCData;
|
|
25 import org.eclipse.swt.graphics.ImageData;
|
|
26 import org.eclipse.swt.graphics.PaletteData;
|
|
27 import org.eclipse.swt.graphics.RGB;
|
|
28 import org.eclipse.swt.graphics.Rectangle;
|
|
29 import org.eclipse.swt.graphics.Resource;
|
|
30
|
|
31 import java.io.InputStream;
|
|
32 import java.lang.all;
|
|
33
|
|
34 import tango.stdc.string;
|
|
35
|
|
36 /**
|
|
37 * Instances of this class are graphics which have been prepared
|
|
38 * for display on a specific device. That is, they are ready
|
|
39 * to paint using methods such as <code>GC.drawImage()</code>
|
|
40 * and display on widgets with, for example, <code>Button.setImage()</code>.
|
|
41 * <p>
|
|
42 * If loaded from a file format that supports it, an
|
|
43 * <code>Image</code> may have transparency, meaning that certain
|
|
44 * pixels are specified as being transparent when drawn. Examples
|
|
45 * of file formats that support transparency are GIF and PNG.
|
|
46 * </p><p>
|
|
47 * There are two primary ways to use <code>Images</code>.
|
|
48 * The first is to load a graphic file from disk and create an
|
|
49 * <code>Image</code> from it. This is done using an <code>Image</code>
|
|
50 * constructor, for example:
|
|
51 * <pre>
|
|
52 * Image i = new Image(device, "C:\\graphic.bmp");
|
|
53 * </pre>
|
|
54 * A graphic file may contain a color table specifying which
|
|
55 * colors the image was intended to possess. In the above example,
|
|
56 * these colors will be mapped to the closest available color in
|
|
57 * SWT. It is possible to get more control over the mapping of
|
|
58 * colors as the image is being created, using code of the form:
|
|
59 * <pre>
|
|
60 * ImageData data = new ImageData("C:\\graphic.bmp");
|
|
61 * RGB[] rgbs = data.getRGBs();
|
|
62 * // At this point, rgbs contains specifications of all
|
|
63 * // the colors contained within this image. You may
|
|
64 * // allocate as many of these colors as you wish by
|
|
65 * // using the Color constructor Color(RGB), then
|
|
66 * // create the image:
|
|
67 * Image i = new Image(device, data);
|
|
68 * </pre>
|
|
69 * <p>
|
|
70 * Applications which require even greater control over the image
|
|
71 * loading process should use the support provided in class
|
|
72 * <code>ImageLoader</code>.
|
|
73 * </p><p>
|
|
74 * Application code must explicitly invoke the <code>Image.dispose()</code>
|
|
75 * method to release the operating system resources managed by each instance
|
|
76 * when those instances are no longer required.
|
|
77 * </p>
|
|
78 *
|
|
79 * @see Color
|
|
80 * @see ImageData
|
|
81 * @see ImageLoader
|
|
82 * @see <a href="http://www.eclipse.org/swt/snippets/#image">Image snippets</a>
|
|
83 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Examples: GraphicsExample, ImageAnalyzer</a>
|
|
84 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
|
|
85 */
|
|
86 public final class Image : Resource, Drawable {
|
|
87 alias Resource.init_ init_;
|
|
88 /**
|
|
89 * specifies whether the receiver is a bitmap or an icon
|
|
90 * (one of <code>SWT.BITMAP</code>, <code>SWT.ICON</code>)
|
|
91 * <p>
|
|
92 * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
|
|
93 * public API. It is marked public only so that it can be shared
|
|
94 * within the packages provided by SWT. It is not available on all
|
|
95 * platforms and should never be accessed from application code.
|
|
96 * </p>
|
|
97 */
|
|
98 public int type;
|
|
99
|
|
100 /**
|
|
101 * The handle to the OS pixmap resource.
|
|
102 * (Warning: This field is platform dependent)
|
|
103 * <p>
|
|
104 * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
|
|
105 * public API. It is marked public only so that it can be shared
|
|
106 * within the packages provided by SWT. It is not available on all
|
|
107 * platforms and should never be accessed from application code.
|
|
108 * </p>
|
|
109 */
|
|
110 public GdkDrawable* pixmap;
|
|
111
|
|
112 /**
|
|
113 * The handle to the OS mask resource.
|
|
114 * (Warning: This field is platform dependent)
|
|
115 * <p>
|
|
116 * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
|
|
117 * public API. It is marked public only so that it can be shared
|
|
118 * within the packages provided by SWT. It is not available on all
|
|
119 * platforms and should never be accessed from application code.
|
|
120 * </p>
|
|
121 */
|
|
122 public GdkDrawable* mask;
|
|
123
|
|
124 cairo_surface_t* surface;
|
|
125 cairo_surface_t* surfaceData;
|
|
126
|
|
127 /**
|
|
128 * specifies the transparent pixel
|
|
129 */
|
|
130 int transparentPixel = -1;
|
|
131
|
|
132 /**
|
|
133 * The GC the image is currently selected in.
|
|
134 */
|
|
135 GC memGC;
|
|
136
|
|
137 /**
|
|
138 * The alpha data of the image.
|
|
139 */
|
|
140 byte[] alphaData;
|
|
141
|
|
142 /**
|
|
143 * The global alpha value to be used for every pixel.
|
|
144 */
|
|
145 int alpha = -1;
|
|
146
|
|
147 /**
|
|
148 * The width of the image.
|
|
149 */
|
|
150 int width = -1;
|
|
151
|
|
152 /**
|
|
153 * The height of the image.
|
|
154 */
|
|
155 int height = -1;
|
|
156
|
|
157 /**
|
|
158 * Specifies the default scanline padding.
|
|
159 */
|
|
160 static const int DEFAULT_SCANLINE_PAD = 4;
|
|
161
|
|
162 this(Device device) {
|
|
163 super(device);
|
|
164 }
|
|
165
|
|
166 /**
|
|
167 * Constructs an empty instance of this class with the
|
|
168 * specified width and height. The result may be drawn upon
|
|
169 * by creating a GC and using any of its drawing operations,
|
|
170 * as shown in the following example:
|
|
171 * <pre>
|
|
172 * Image i = new Image(device, width, height);
|
|
173 * GC gc = new GC(i);
|
|
174 * gc.drawRectangle(0, 0, 50, 50);
|
|
175 * gc.dispose();
|
|
176 * </pre>
|
|
177 * <p>
|
|
178 * Note: Some platforms may have a limitation on the size
|
|
179 * of image that can be created (size depends on width, height,
|
|
180 * and depth). For example, Windows 95, 98, and ME do not allow
|
|
181 * images larger than 16M.
|
|
182 * </p>
|
|
183 *
|
|
184 * @param device the device on which to create the image
|
|
185 * @param width the width of the new image
|
|
186 * @param height the height of the new image
|
|
187 *
|
|
188 * @exception IllegalArgumentException <ul>
|
|
189 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
190 * <li>ERROR_INVALID_ARGUMENT - if either the width or height is negative or zero</li>
|
|
191 * </ul>
|
|
192 * @exception SWTError <ul>
|
|
193 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
|
|
194 * </ul>
|
|
195 */
|
|
196 public this(Device device, int width, int height) {
|
|
197 super(device);
|
|
198 init_(width, height);
|
|
199 init_();
|
|
200 }
|
|
201
|
|
202 /**
|
|
203 * Constructs a new instance of this class based on the
|
|
204 * provided image, with an appearance that varies depending
|
|
205 * on the value of the flag. The possible flag values are:
|
|
206 * <dl>
|
|
207 * <dt><b>{@link SWT#IMAGE_COPY}</b></dt>
|
|
208 * <dd>the result is an identical copy of srcImage</dd>
|
|
209 * <dt><b>{@link SWT#IMAGE_DISABLE}</b></dt>
|
|
210 * <dd>the result is a copy of srcImage which has a <em>disabled</em> look</dd>
|
|
211 * <dt><b>{@link SWT#IMAGE_GRAY}</b></dt>
|
|
212 * <dd>the result is a copy of srcImage which has a <em>gray scale</em> look</dd>
|
|
213 * </dl>
|
|
214 *
|
|
215 * @param device the device on which to create the image
|
|
216 * @param srcImage the image to use as the source
|
|
217 * @param flag the style, either <code>IMAGE_COPY</code>, <code>IMAGE_DISABLE</code> or <code>IMAGE_GRAY</code>
|
|
218 *
|
|
219 * @exception IllegalArgumentException <ul>
|
|
220 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
221 * <li>ERROR_NULL_ARGUMENT - if srcImage is null</li>
|
|
222 * <li>ERROR_INVALID_ARGUMENT - if the flag is not one of <code>IMAGE_COPY</code>, <code>IMAGE_DISABLE</code> or <code>IMAGE_GRAY</code></li>
|
|
223 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
|
|
224 * </ul>
|
|
225 * @exception SWTException <ul>
|
|
226 * <li>ERROR_INVALID_IMAGE - if the image is not a bitmap or an icon, or is otherwise in an invalid state</li>
|
|
227 * <li>ERROR_UNSUPPORTED_DEPTH - if the depth of the image is not supported</li>
|
|
228 * </ul>
|
|
229 * @exception SWTError <ul>
|
|
230 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
|
|
231 * </ul>
|
|
232 */
|
|
233 public this(Device device, Image srcImage, int flag) {
|
|
234 super(device);
|
|
235 if (srcImage is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
236 if (srcImage.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
237 switch (flag) {
|
|
238 case SWT.IMAGE_COPY:
|
|
239 case SWT.IMAGE_DISABLE:
|
|
240 case SWT.IMAGE_GRAY:
|
|
241 break;
|
|
242 default:
|
|
243 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
244 }
|
|
245 device = this.device;
|
|
246 this.type = srcImage.type;
|
|
247
|
|
248 /* Get source image size */
|
|
249 int w, h;
|
|
250 OS.gdk_drawable_get_size(srcImage.pixmap, &w, &h);
|
|
251 int width = w;
|
|
252 int height = h;
|
|
253
|
|
254 /* Copy the mask */
|
|
255 if ((srcImage.type is SWT.ICON && srcImage.mask !is null ) || srcImage.transparentPixel !is -1) {
|
|
256 /* Generate the mask if necessary. */
|
|
257 if (srcImage.transparentPixel !is -1) srcImage.createMask();
|
|
258 //PORTING_FIXME cast
|
|
259 GdkDrawable* mask = cast(GdkDrawable*) OS.gdk_pixmap_new( null, width, height, 1);
|
|
260 if (mask is null ) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
261 auto gdkGC = OS.gdk_gc_new(mask);
|
|
262 if (gdkGC is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
263 OS.gdk_draw_drawable(mask, gdkGC, srcImage.mask, 0, 0, 0, 0, width, height);
|
|
264 OS.g_object_unref(gdkGC);
|
|
265 this.mask = mask;
|
|
266 /* Destroy the image mask if the there is a GC created on the image */
|
|
267 if (srcImage.transparentPixel !is -1 && srcImage.memGC !is null) srcImage.destroyMask();
|
|
268 }
|
|
269
|
|
270 /* Copy transparent pixel and alpha data */
|
|
271 if (flag !is SWT.IMAGE_DISABLE) transparentPixel = srcImage.transparentPixel;
|
|
272 alpha = srcImage.alpha;
|
|
273 if (srcImage.alphaData !is null) {
|
|
274 alphaData = new byte[srcImage.alphaData.length];
|
|
275 System.arraycopy(srcImage.alphaData, 0, alphaData, 0, alphaData.length);
|
|
276 }
|
|
277 createAlphaMask(width, height);
|
|
278
|
|
279 /* Create the new pixmap */
|
|
280 auto pixmap = cast(GdkDrawable*) OS.gdk_pixmap_new (cast(GdkDrawable*)OS.GDK_ROOT_PARENT(), width, height, -1);
|
|
281 if (pixmap is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
282 auto gdkGC = OS.gdk_gc_new(pixmap);
|
|
283 if (gdkGC is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
284 this.pixmap = pixmap;
|
|
285
|
|
286 if (flag is SWT.IMAGE_COPY) {
|
|
287 OS.gdk_draw_drawable(pixmap, gdkGC, srcImage.pixmap, 0, 0, 0, 0, width, height);
|
|
288 OS.g_object_unref(gdkGC);
|
|
289 } else {
|
|
290
|
|
291 /* Retrieve the source pixmap data */
|
|
292 auto pixbuf = OS.gdk_pixbuf_new(OS.GDK_COLORSPACE_RGB, false, 8, width, height);
|
|
293 if (pixbuf is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
294 auto colormap = OS.gdk_colormap_get_system();
|
|
295 OS.gdk_pixbuf_get_from_drawable(pixbuf, srcImage.pixmap, colormap, 0, 0, 0, 0, width, height);
|
|
296 int stride = OS.gdk_pixbuf_get_rowstride(pixbuf);
|
|
297 auto pixels = OS.gdk_pixbuf_get_pixels(pixbuf);
|
|
298
|
|
299 /* Apply transformation */
|
|
300 switch (flag) {
|
|
301 case SWT.IMAGE_DISABLE: {
|
|
302 Color zeroColor = device.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
|
|
303 RGB zeroRGB = zeroColor.getRGB();
|
|
304 byte zeroRed = cast(byte)zeroRGB.red;
|
|
305 byte zeroGreen = cast(byte)zeroRGB.green;
|
|
306 byte zeroBlue = cast(byte)zeroRGB.blue;
|
|
307 Color oneColor = device.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
|
|
308 RGB oneRGB = oneColor.getRGB();
|
|
309 byte oneRed = cast(byte)oneRGB.red;
|
|
310 byte oneGreen = cast(byte)oneRGB.green;
|
|
311 byte oneBlue = cast(byte)oneRGB.blue;
|
|
312 byte[] line = new byte[stride];
|
|
313 for (int y=0; y<height; y++) {
|
|
314 memmove(line.ptr, pixels + (y * stride), stride);
|
|
315 for (int x=0; x<width; x++) {
|
|
316 int offset = x*3;
|
|
317 int red = line[offset] & 0xFF;
|
|
318 int green = line[offset+1] & 0xFF;
|
|
319 int blue = line[offset+2] & 0xFF;
|
|
320 int intensity = red * red + green * green + blue * blue;
|
|
321 if (intensity < 98304) {
|
|
322 line[offset] = zeroRed;
|
|
323 line[offset+1] = zeroGreen;
|
|
324 line[offset+2] = zeroBlue;
|
|
325 } else {
|
|
326 line[offset] = oneRed;
|
|
327 line[offset+1] = oneGreen;
|
|
328 line[offset+2] = oneBlue;
|
|
329 }
|
|
330 }
|
|
331 memmove(pixels + (y * stride), line.ptr, stride);
|
|
332 }
|
|
333 break;
|
|
334 }
|
|
335 case SWT.IMAGE_GRAY: {
|
|
336 byte[] line = new byte[stride];
|
|
337 for (int y=0; y<height; y++) {
|
|
338 memmove(line.ptr, pixels + (y * stride), stride);
|
|
339 for (int x=0; x<width; x++) {
|
|
340 int offset = x*3;
|
|
341 int red = line[offset] & 0xFF;
|
|
342 int green = line[offset+1] & 0xFF;
|
|
343 int blue = line[offset+2] & 0xFF;
|
|
344 byte intensity = cast(byte)((red+red+green+green+green+green+green+blue) >> 3);
|
|
345 line[offset] = line[offset+1] = line[offset+2] = intensity;
|
|
346 }
|
|
347 memmove(pixels + (y * stride), line.ptr, stride);
|
|
348 }
|
|
349 break;
|
|
350 }
|
|
351 default:
|
|
352 }
|
|
353
|
|
354 /* Copy data back to destination pixmap */
|
|
355 OS.gdk_pixbuf_render_to_drawable(pixbuf, pixmap, gdkGC, 0, 0, 0, 0, width, height, OS.GDK_RGB_DITHER_NORMAL, 0, 0);
|
|
356
|
|
357 /* Free resources */
|
|
358 OS.g_object_unref(pixbuf);
|
|
359 OS.g_object_unref(gdkGC);
|
|
360 }
|
|
361 init_();
|
|
362 }
|
|
363
|
|
364 /**
|
|
365 * Constructs an empty instance of this class with the
|
|
366 * width and height of the specified rectangle. The result
|
|
367 * may be drawn upon by creating a GC and using any of its
|
|
368 * drawing operations, as shown in the following example:
|
|
369 * <pre>
|
|
370 * Image i = new Image(device, boundsRectangle);
|
|
371 * GC gc = new GC(i);
|
|
372 * gc.drawRectangle(0, 0, 50, 50);
|
|
373 * gc.dispose();
|
|
374 * </pre>
|
|
375 * <p>
|
|
376 * Note: Some platforms may have a limitation on the size
|
|
377 * of image that can be created (size depends on width, height,
|
|
378 * and depth). For example, Windows 95, 98, and ME do not allow
|
|
379 * images larger than 16M.
|
|
380 * </p>
|
|
381 *
|
|
382 * @param device the device on which to create the image
|
|
383 * @param bounds a rectangle specifying the image's width and height (must not be null)
|
|
384 *
|
|
385 * @exception IllegalArgumentException <ul>
|
|
386 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
387 * <li>ERROR_NULL_ARGUMENT - if the bounds rectangle is null</li>
|
|
388 * <li>ERROR_INVALID_ARGUMENT - if either the rectangle's width or height is negative</li>
|
|
389 * </ul>
|
|
390 * @exception SWTError <ul>
|
|
391 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
|
|
392 * </ul>
|
|
393 */
|
|
394 public this(Device device, Rectangle bounds) {
|
|
395 super(device);
|
|
396 if (bounds is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
397 init_(bounds.width, bounds.height);
|
|
398 init_();
|
|
399 }
|
|
400
|
|
401 /**
|
|
402 * Constructs an instance of this class from the given
|
|
403 * <code>ImageData</code>.
|
|
404 *
|
|
405 * @param device the device on which to create the image
|
|
406 * @param data the image data to create the image from (must not be null)
|
|
407 *
|
|
408 * @exception IllegalArgumentException <ul>
|
|
409 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
410 * <li>ERROR_NULL_ARGUMENT - if the image data is null</li>
|
|
411 * </ul>
|
|
412 * @exception SWTException <ul>
|
|
413 * <li>ERROR_UNSUPPORTED_DEPTH - if the depth of the ImageData is not supported</li>
|
|
414 * </ul>
|
|
415 * @exception SWTError <ul>
|
|
416 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
|
|
417 * </ul>
|
|
418 */
|
|
419 public this(Device device, ImageData data) {
|
|
420 super(device);
|
|
421 init_(data);
|
|
422 init_();
|
|
423 }
|
|
424
|
|
425 /**
|
|
426 * Constructs an instance of this class, whose type is
|
|
427 * <code>SWT.ICON</code>, from the two given <code>ImageData</code>
|
|
428 * objects. The two images must be the same size. Pixel transparency
|
|
429 * in either image will be ignored.
|
|
430 * <p>
|
|
431 * The mask image should contain white wherever the icon is to be visible,
|
|
432 * and black wherever the icon is to be transparent. In addition,
|
|
433 * the source image should contain black wherever the icon is to be
|
|
434 * transparent.
|
|
435 * </p>
|
|
436 *
|
|
437 * @param device the device on which to create the icon
|
|
438 * @param source the color data for the icon
|
|
439 * @param mask the mask data for the icon
|
|
440 *
|
|
441 * @exception IllegalArgumentException <ul>
|
|
442 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
443 * <li>ERROR_NULL_ARGUMENT - if either the source or mask is null </li>
|
|
444 * <li>ERROR_INVALID_ARGUMENT - if source and mask are different sizes</li>
|
|
445 * </ul>
|
|
446 * @exception SWTError <ul>
|
|
447 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
|
|
448 * </ul>
|
|
449 */
|
|
450 public this(Device device, ImageData source, ImageData mask) {
|
|
451 super(device);
|
|
452 if (source is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
453 if (mask is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
454 if (source.width !is mask.width || source.height !is mask.height) {
|
|
455 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
456 }
|
|
457 mask = ImageData.convertMask (mask);
|
|
458 ImageData image = new ImageData(source.width, source.height, source.depth, source.palette, source.scanlinePad, source.data);
|
|
459 image.maskPad = mask.scanlinePad;
|
|
460 image.maskData = mask.data;
|
|
461 init_(image);
|
|
462 init_();
|
|
463 }
|
|
464
|
|
465 /**
|
|
466 * Constructs an instance of this class by loading its representation
|
|
467 * from the specified input stream. Throws an error if an error
|
|
468 * occurs while loading the image, or if the result is an image
|
|
469 * of an unsupported type. Application code is still responsible
|
|
470 * for closing the input stream.
|
|
471 * <p>
|
|
472 * This constructor is provided for convenience when loading a single
|
|
473 * image only. If the stream contains multiple images, only the first
|
|
474 * one will be loaded. To load multiple images, use
|
|
475 * <code>ImageLoader.load()</code>.
|
|
476 * </p><p>
|
|
477 * This constructor may be used to load a resource as follows:
|
|
478 * </p>
|
|
479 * <pre>
|
|
480 * static Image loadImage (Display display, Class clazz, String string) {
|
|
481 * InputStream stream = clazz.getResourceAsStream (string);
|
|
482 * if (stream is null) return null;
|
|
483 * Image image = null;
|
|
484 * try {
|
|
485 * image = new Image (display, stream);
|
|
486 * } catch (SWTException ex) {
|
|
487 * } finally {
|
|
488 * try {
|
|
489 * stream.close ();
|
|
490 * } catch (IOException ex) {}
|
|
491 * }
|
|
492 * return image;
|
|
493 * }
|
|
494 * </pre>
|
|
495 *
|
|
496 * @param device the device on which to create the image
|
|
497 * @param stream the input stream to load the image from
|
|
498 *
|
|
499 * @exception IllegalArgumentException <ul>
|
|
500 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
501 * <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
|
|
502 * </ul>
|
|
503 * @exception SWTException <ul>
|
|
504 * <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
|
|
505 * <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data </li>
|
|
506 * <li>ERROR_UNSUPPORTED_DEPTH - if the image stream describes an image with an unsupported depth</li>
|
|
507 * <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
|
|
508 * </ul>
|
|
509 * @exception SWTError <ul>
|
|
510 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
|
|
511 * </ul>
|
|
512 */
|
|
513 public this(Device device, InputStream stream) {
|
|
514 super(device);
|
|
515 init_(new ImageData(stream));
|
|
516 init_();
|
|
517 }
|
|
518
|
|
519 /**
|
|
520 * Constructs an instance of this class by loading its representation
|
|
521 * from the file with the specified name. Throws an error if an error
|
|
522 * occurs while loading the image, or if the result is an image
|
|
523 * of an unsupported type.
|
|
524 * <p>
|
|
525 * This constructor is provided for convenience when loading
|
|
526 * a single image only. If the specified file contains
|
|
527 * multiple images, only the first one will be used.
|
|
528 *
|
|
529 * @param device the device on which to create the image
|
|
530 * @param filename the name of the file to load the image from
|
|
531 *
|
|
532 * @exception IllegalArgumentException <ul>
|
|
533 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
534 * <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
|
|
535 * </ul>
|
|
536 * @exception SWTException <ul>
|
|
537 * <li>ERROR_IO - if an IO error occurs while reading from the file</li>
|
|
538 * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data </li>
|
|
539 * <li>ERROR_UNSUPPORTED_DEPTH - if the image file describes an image with an unsupported depth</li>
|
|
540 * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
|
|
541 * </ul>
|
|
542 * @exception SWTError <ul>
|
|
543 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
|
|
544 * </ul>
|
|
545 */
|
|
546 public this(Device device, String filename) {
|
|
547 super(device);
|
|
548 if (filename is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
549 try {
|
|
550 int length = filename.length;
|
|
551 auto pixbuf = OS.gdk_pixbuf_new_from_file(toStringz(filename), null);
|
|
552 if (pixbuf !is null) {
|
|
553 bool hasAlpha = cast(bool)OS.gdk_pixbuf_get_has_alpha(pixbuf);
|
|
554 if (hasAlpha) {
|
|
555 /*
|
|
556 * Bug in GTK. Depending on the image (seems to affect images that have
|
|
557 * some degree of transparency all over the image), gdk_pixbuff_render_pixmap_and_mask()
|
|
558 * will return a corrupt pixmap. To avoid this, read in and store the alpha channel data
|
|
559 * for the image and then set it to 0xFF to prevent any possible corruption from
|
|
560 * gdk_pixbuff_render_pixmap_and_mask().
|
|
561 */
|
|
562 int width = OS.gdk_pixbuf_get_width(pixbuf);
|
|
563 int height = OS.gdk_pixbuf_get_height(pixbuf);
|
|
564 int stride = OS.gdk_pixbuf_get_rowstride(pixbuf);
|
|
565 auto pixels = OS.gdk_pixbuf_get_pixels(pixbuf);
|
|
566 byte[] line = new byte[stride];
|
|
567 alphaData = new byte[width * height];
|
|
568 for (int y = 0; y < height; y++) {
|
|
569 memmove(line.ptr, pixels + (y * stride), stride);
|
|
570 for (int x = 0; x < width; x++) {
|
|
571 alphaData[y*width+x] = line[x*4 + 3];
|
|
572 line[x*4 + 3] = cast(byte) 0xFF;
|
|
573 }
|
|
574 memmove(pixels + (y * stride), line.ptr, stride);
|
|
575 }
|
|
576 createAlphaMask(width, height);
|
|
577 }
|
|
578 GdkPixmap* pixmap_return;
|
|
579 OS.gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap_return, null, 0);
|
|
580 this.type = SWT.BITMAP;
|
|
581 this.pixmap = cast(GdkDrawable*)pixmap_return;
|
|
582 if (pixmap is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
583 OS.g_object_unref (pixbuf);
|
|
584 return;
|
|
585 }
|
|
586 } catch (SWTException e) {}
|
|
587 init_(new ImageData(filename));
|
|
588 init_();
|
|
589 }
|
|
590
|
|
591 void createAlphaMask (int width, int height) {
|
|
592 if (device.useXRender && (alpha !is -1 || alphaData !is null)) {
|
|
593 mask = cast(GdkDrawable*)OS.gdk_pixmap_new(null, alpha !is -1 ? 1 : width, alpha !is -1 ? 1 : height, 8);
|
|
594 if (mask is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
595 auto gc = OS.gdk_gc_new(mask);
|
|
596 if (alpha !is -1) {
|
|
597 GdkColor* color = new GdkColor();
|
|
598 color.pixel = (alpha & 0xFF) << 8 | (alpha & 0xFF);
|
|
599 OS.gdk_gc_set_foreground(gc, color);
|
|
600 OS.gdk_draw_rectangle(mask, gc, 1, 0, 0, 1, 1);
|
|
601 } else {
|
|
602 GdkImage* imagePtr = OS.gdk_drawable_get_image(mask, 0, 0, width, height);
|
|
603 if (imagePtr is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
604 GdkImage* gdkImage = new GdkImage();
|
|
605 *gdkImage = *imagePtr;
|
|
606 if (gdkImage.bpl is width) {
|
|
607 memmove(gdkImage.mem, alphaData.ptr, alphaData.length);
|
|
608 } else {
|
|
609 byte[] line = new byte[gdkImage.bpl];
|
|
610 for (int y = 0; y < height; y++) {
|
|
611 System.arraycopy(alphaData, width * y, line, 0, width);
|
|
612 memmove(gdkImage.mem + (gdkImage.bpl * y), line.ptr, gdkImage.bpl);
|
|
613 }
|
|
614 }
|
|
615 OS.gdk_draw_image(mask, gc, imagePtr, 0, 0, 0, 0, width, height);
|
|
616 OS.g_object_unref(imagePtr);
|
|
617 }
|
|
618 OS.g_object_unref(gc);
|
|
619 }
|
|
620 }
|
|
621
|
|
622 /**
|
|
623 * Create the receiver's mask if necessary.
|
|
624 */
|
|
625 void createMask() {
|
|
626 if (mask !is null ) return;
|
|
627 mask = createMask(getImageData(), false);
|
|
628 if (mask is null ) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
629 }
|
|
630
|
|
631 GdkDrawable* createMask(ImageData image, bool copy) {
|
|
632 ImageData mask = image.getTransparencyMask();
|
|
633 byte[] data = mask.data;
|
|
634 byte[] maskData = copy ? new byte[data.length] : data;
|
|
635 for (int i = 0; i < maskData.length; i++) {
|
|
636 byte s = data[i];
|
|
637 maskData[i] = cast(byte)(((s & 0x80) >> 7) | ((s & 0x40) >> 5) |
|
|
638 ((s & 0x20) >> 3) | ((s & 0x10) >> 1) | ((s & 0x08) << 1) |
|
|
639 ((s & 0x04) << 3) | ((s & 0x02) << 5) | ((s & 0x01) << 7));
|
|
640 }
|
|
641 maskData = ImageData.convertPad(maskData, mask.width, mask.height, mask.depth, mask.scanlinePad, 1);
|
|
642 return cast(GdkDrawable*)OS.gdk_bitmap_create_from_data(null, cast(char*)maskData.ptr, mask.width, mask.height);
|
|
643 }
|
|
644
|
|
645 void createSurface() {
|
|
646 if (surface !is null ) return;
|
|
647 /* Generate the mask if necessary. */
|
|
648 if (transparentPixel !is -1) createMask();
|
|
649 int w, h;
|
|
650 OS.gdk_drawable_get_size(pixmap, &w, &h);
|
|
651 int width = w, height = h;
|
|
652 if (mask !is null || alpha !is -1 || alphaData !is null) {
|
|
653 auto pixbuf = OS.gdk_pixbuf_new( OS.GDK_COLORSPACE_RGB, true, 8, width, height);
|
|
654 if (pixbuf is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
655 auto colormap = OS.gdk_colormap_get_system();
|
|
656 OS.gdk_pixbuf_get_from_drawable(pixbuf, pixmap, colormap, 0, 0, 0, 0, width, height);
|
|
657 int stride = OS.gdk_pixbuf_get_rowstride(pixbuf);
|
|
658 auto pixels = OS.gdk_pixbuf_get_pixels(pixbuf);
|
|
659 byte[] line = new byte[stride];
|
|
660 if (mask !is null && OS.gdk_drawable_get_depth(mask) is 1) {
|
|
661 auto maskPixbuf = OS.gdk_pixbuf_new(OS.GDK_COLORSPACE_RGB, false, 8, width, height);
|
|
662 if (maskPixbuf is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
663 OS.gdk_pixbuf_get_from_drawable(maskPixbuf, mask, null, 0, 0, 0, 0, width, height);
|
|
664 int maskStride = OS.gdk_pixbuf_get_rowstride(maskPixbuf);
|
|
665 auto maskPixels = OS.gdk_pixbuf_get_pixels(maskPixbuf);
|
|
666 byte[] maskLine = new byte[maskStride];
|
|
667 auto offset = pixels, maskOffset = maskPixels;
|
|
668 for (int y=0; y<height; y++) {
|
|
669 memmove(line.ptr, offset, stride);
|
|
670 memmove(maskLine.ptr, maskOffset, maskStride);
|
|
671 for (int x=0, offset1=0; x<width; x++, offset1 += 4) {
|
|
672 if (maskLine[x * 3] is 0) {
|
|
673 line[offset1 + 0] = line[offset1 + 1] = line[offset1 + 2] = line[offset1 + 3] = 0;
|
|
674 }
|
|
675 byte temp = line[offset1];
|
|
676 line[offset1] = line[offset1 + 2];
|
|
677 line[offset1 + 2] = temp;
|
|
678 }
|
|
679 memmove(offset, line.ptr, stride);
|
|
680 offset += stride;
|
|
681 maskOffset += maskStride;
|
|
682 }
|
|
683 OS.g_object_unref(maskPixbuf);
|
|
684 } else if (alpha !is -1) {
|
|
685 auto offset = pixels;
|
|
686 for (int y=0; y<height; y++) {
|
|
687 memmove(line.ptr, offset, stride);
|
|
688 for (int x=0, offset1=0; x<width; x++, offset1 += 4) {
|
|
689 line[offset1+3] = cast(byte)alpha;
|
|
690 /* pre-multiplied alpha */
|
|
691 int r = ((line[offset1 + 0] & 0xFF) * alpha) + 128;
|
|
692 r = (r + (r >> 8)) >> 8;
|
|
693 int g = ((line[offset1 + 1] & 0xFF) * alpha) + 128;
|
|
694 g = (g + (g >> 8)) >> 8;
|
|
695 int b = ((line[offset1 + 2] & 0xFF) * alpha) + 128;
|
|
696 b = (b + (b >> 8)) >> 8;
|
|
697 line[offset1 + 0] = cast(byte)b;
|
|
698 line[offset1 + 1] = cast(byte)g;
|
|
699 line[offset1 + 2] = cast(byte)r;
|
|
700 }
|
|
701 memmove(offset, line.ptr, stride);
|
|
702 offset += stride;
|
|
703 }
|
|
704 } else if (alphaData !is null) {
|
|
705 auto offset = pixels;
|
|
706 for (int y = 0; y < h; y++) {
|
|
707 memmove (line.ptr, offset, stride);
|
|
708 for (int x=0, offset1=0; x<width; x++, offset1 += 4) {
|
|
709 int alpha = alphaData [y*w+x] & 0xFF;
|
|
710 line[offset1+3] = cast(byte)alpha;
|
|
711 /* pre-multiplied alpha */
|
|
712 int r = ((line[offset1 + 0] & 0xFF) * alpha) + 128;
|
|
713 r = (r + (r >> 8)) >> 8;
|
|
714 int g = ((line[offset1 + 1] & 0xFF) * alpha) + 128;
|
|
715 g = (g + (g >> 8)) >> 8;
|
|
716 int b = ((line[offset1 + 2] & 0xFF) * alpha) + 128;
|
|
717 b = (b + (b >> 8)) >> 8;
|
|
718 line[offset1 + 0] = cast(byte)b;
|
|
719 line[offset1 + 1] = cast(byte)g;
|
|
720 line[offset1 + 2] = cast(byte)r;
|
|
721 }
|
|
722 memmove (offset, line.ptr, stride);
|
|
723 offset += stride;
|
|
724 }
|
|
725 } else {
|
|
726 auto offset = pixels;
|
|
727 for (int y = 0; y < h; y++) {
|
|
728 memmove (line.ptr, offset, stride);
|
|
729 for (int x=0, offset1=0; x<width; x++, offset1 += 4) {
|
|
730 line[offset1+3] = cast(byte)0xFF;
|
|
731 byte temp = line[offset1];
|
|
732 line[offset1] = line[offset1 + 2];
|
|
733 line[offset1 + 2] = temp;
|
|
734 }
|
|
735 memmove (offset, line.ptr, stride);
|
|
736 offset += stride;
|
|
737 }
|
|
738 }
|
|
739 surfaceData = cast(cairo_surface_t*) OS.g_malloc(stride * height);
|
|
740 memmove(surfaceData, pixels, stride * height);
|
|
741 surface = Cairo.cairo_image_surface_create_for_data(cast(char*)surfaceData, Cairo.CAIRO_FORMAT_ARGB32, width, height, stride);
|
|
742 OS.g_object_unref(pixbuf);
|
|
743 } else {
|
|
744 auto xDisplay = OS.GDK_DISPLAY();
|
|
745 auto xDrawable = OS.GDK_PIXMAP_XID(pixmap);
|
|
746 auto xVisual = OS.gdk_x11_visual_get_xvisual(OS.gdk_visual_get_system());
|
|
747 // PORTING_FIXME cast and types not good
|
|
748 surface = Cairo.cairo_xlib_surface_create(cast(void*)xDisplay, xDrawable, xVisual, width, height);
|
|
749 }
|
|
750 /* Destroy the image mask if the there is a GC created on the image */
|
|
751 if (transparentPixel !is -1 && memGC !is null) destroyMask();
|
|
752 }
|
|
753
|
|
754 /**
|
|
755 * Destroy the receiver's mask if it exists.
|
|
756 */
|
|
757 void destroyMask() {
|
|
758 if (mask is null) return;
|
|
759 OS.g_object_unref(mask);
|
|
760 mask = null;
|
|
761 }
|
|
762
|
|
763 void destroy() {
|
|
764 if (memGC !is null) memGC.dispose();
|
|
765 if (pixmap !is null) OS.g_object_unref(pixmap);
|
|
766 if (mask !is null) OS.g_object_unref(mask);
|
|
767 if (surface !is null) Cairo.cairo_surface_destroy(surface);
|
|
768 if (surfaceData !is null) OS.g_free(surfaceData);
|
|
769 surfaceData = null;
|
|
770 surface = null;
|
|
771 pixmap = null;
|
|
772 mask = null;
|
|
773 memGC = null;
|
|
774 }
|
|
775
|
|
776 /**
|
|
777 * Compares the argument to the receiver, and returns true
|
|
778 * if they represent the <em>same</em> object using a class
|
|
779 * specific comparison.
|
|
780 *
|
|
781 * @param object the object to compare with this object
|
|
782 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
|
|
783 *
|
|
784 * @see #hashCode
|
|
785 */
|
26
|
786 public override equals_t opEquals (Object object) {
|
25
|
787 if (object is this) return true;
|
|
788 if ( auto image = cast(Image)object ){
|
|
789 return device is image.device && pixmap is image.pixmap;
|
|
790 }
|
|
791 return false;
|
|
792 }
|
|
793
|
|
794 /**
|
|
795 * Returns the color to which to map the transparent pixel, or null if
|
|
796 * the receiver has no transparent pixel.
|
|
797 * <p>
|
|
798 * There are certain uses of Images that do not support transparency
|
|
799 * (for example, setting an image into a button or label). In these cases,
|
|
800 * it may be desired to simulate transparency by using the background
|
|
801 * color of the widget to paint the transparent pixels of the image.
|
|
802 * Use this method to check which color will be used in these cases
|
|
803 * in place of transparency. This value may be set with setBackground().
|
|
804 * <p>
|
|
805 *
|
|
806 * @return the background color of the image, or null if there is no transparency in the image
|
|
807 *
|
|
808 * @exception SWTException <ul>
|
|
809 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
810 * </ul>
|
|
811 */
|
|
812 public Color getBackground() {
|
|
813 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
814 if (transparentPixel is -1) return null;
|
|
815 //NOT DONE
|
|
816 return null;
|
|
817 }
|
|
818
|
|
819 /**
|
|
820 * Returns the bounds of the receiver. The rectangle will always
|
|
821 * have x and y values of 0, and the width and height of the
|
|
822 * image.
|
|
823 *
|
|
824 * @return a rectangle specifying the image's bounds
|
|
825 *
|
|
826 * @exception SWTException <ul>
|
|
827 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
828 * <li>ERROR_INVALID_IMAGE - if the image is not a bitmap or an icon</li>
|
|
829 * </ul>
|
|
830 */
|
|
831 public Rectangle getBounds() {
|
|
832 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
833 if (width !is -1 && height !is -1) {
|
|
834 return new Rectangle(0, 0, width, height);
|
|
835 }
|
|
836 int w; int h;
|
|
837 OS.gdk_drawable_get_size(pixmap, &w, &h);
|
|
838 return new Rectangle(0, 0, width = w, height = h);
|
|
839 }
|
|
840
|
|
841 /**
|
|
842 * Returns an <code>ImageData</code> based on the receiver
|
|
843 * Modifications made to this <code>ImageData</code> will not
|
|
844 * affect the Image.
|
|
845 *
|
|
846 * @return an <code>ImageData</code> containing the image's data and attributes
|
|
847 *
|
|
848 * @exception SWTException <ul>
|
|
849 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
850 * <li>ERROR_INVALID_IMAGE - if the image is not a bitmap or an icon</li>
|
|
851 * </ul>
|
|
852 *
|
|
853 * @see ImageData
|
|
854 */
|
|
855 public ImageData getImageData() {
|
|
856 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
857
|
|
858 int w, h;
|
|
859 OS.gdk_drawable_get_size(pixmap, &w, &h);
|
|
860 int width = w, height = h;
|
|
861 auto pixbuf = OS.gdk_pixbuf_new(OS.GDK_COLORSPACE_RGB, false, 8, width, height);
|
|
862 if (pixbuf is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
863 auto colormap = OS.gdk_colormap_get_system();
|
|
864 OS.gdk_pixbuf_get_from_drawable(pixbuf, pixmap, colormap, 0, 0, 0, 0, width, height);
|
|
865 int stride = OS.gdk_pixbuf_get_rowstride(pixbuf);
|
|
866 auto pixels = OS.gdk_pixbuf_get_pixels(pixbuf);
|
|
867 byte[] srcData = new byte[stride * height];
|
|
868 memmove(srcData.ptr, pixels, srcData.length);
|
|
869 OS.g_object_unref(pixbuf);
|
|
870
|
|
871 PaletteData palette = new PaletteData(0xFF0000, 0xFF00, 0xFF);
|
|
872 ImageData data = new ImageData(width, height, 24, palette);
|
|
873 data.data = srcData;
|
|
874 data.bytesPerLine = stride;
|
|
875
|
|
876 if (transparentPixel is -1 && type is SWT.ICON && mask !is null) {
|
|
877 /* Get the icon mask data */
|
|
878 auto gdkImagePtr = OS.gdk_drawable_get_image(mask, 0, 0, width, height);
|
|
879 if (gdkImagePtr is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
880 GdkImage* gdkImage = new GdkImage();
|
|
881 memmove(gdkImage, gdkImagePtr, GdkImage.sizeof );
|
|
882 byte[] maskData = new byte[gdkImage.bpl * gdkImage.height];
|
|
883 memmove(maskData.ptr, gdkImage.mem, maskData.length);
|
|
884 OS.g_object_unref(gdkImagePtr);
|
|
885 int maskPad;
|
|
886 for (maskPad = 1; maskPad < 128; maskPad++) {
|
|
887 int bpl = (((width + 7) / 8) + (maskPad - 1)) / maskPad * maskPad;
|
|
888 if (gdkImage.bpl is bpl) break;
|
|
889 }
|
|
890 /* Make mask scanline pad equals to 2 */
|
|
891 data.maskPad = 2;
|
|
892 maskData = ImageData.convertPad(maskData, width, height, 1, maskPad, data.maskPad);
|
|
893 /* Bit swap the mask data if necessary */
|
|
894 if (gdkImage.byte_order is OS.GDK_LSB_FIRST) {
|
|
895 for (int i = 0; i < maskData.length; i++) {
|
|
896 byte b = maskData[i];
|
|
897 maskData[i] = cast(byte)(((b & 0x01) << 7) | ((b & 0x02) << 5) |
|
|
898 ((b & 0x04) << 3) | ((b & 0x08) << 1) | ((b & 0x10) >> 1) |
|
|
899 ((b & 0x20) >> 3) | ((b & 0x40) >> 5) | ((b & 0x80) >> 7));
|
|
900 }
|
|
901 }
|
|
902 data.maskData = maskData;
|
|
903 }
|
|
904 data.transparentPixel = transparentPixel;
|
|
905 data.alpha = alpha;
|
|
906 if (alpha is -1 && alphaData !is null) {
|
|
907 data.alphaData = new byte[alphaData.length];
|
|
908 System.arraycopy(alphaData, 0, data.alphaData, 0, alphaData.length);
|
|
909 }
|
|
910 return data;
|
|
911 }
|
|
912
|
|
913 /**
|
|
914 * Invokes platform specific functionality to allocate a new image.
|
|
915 * <p>
|
|
916 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
|
|
917 * API for <code>Image</code>. It is marked public only so that it
|
|
918 * can be shared within the packages provided by SWT. It is not
|
|
919 * available on all platforms, and should never be called from
|
|
920 * application code.
|
|
921 * </p>
|
|
922 *
|
|
923 * @param device the device on which to allocate the color
|
|
924 * @param type the type of the image (<code>SWT.BITMAP</code> or <code>SWT.ICON</code>)
|
|
925 * @param pixmap the OS handle for the image
|
|
926 * @param mask the OS handle for the image mask
|
|
927 *
|
|
928 * @private
|
|
929 */
|
|
930 public static Image gtk_new(Device device, int type, GdkDrawable* pixmap, GdkDrawable* mask) {
|
|
931 Image image = new Image(device);
|
|
932 image.type = type;
|
|
933 image.pixmap = cast(GdkDrawable*)pixmap;
|
|
934 image.mask = cast(GdkDrawable*)mask;
|
|
935 return image;
|
|
936 }
|
|
937
|
|
938 /**
|
|
939 * Returns an integer hash code for the receiver. Any two
|
|
940 * objects that return <code>true</code> when passed to
|
|
941 * <code>equals</code> must return the same value for this
|
|
942 * method.
|
|
943 *
|
|
944 * @return the receiver's hash
|
|
945 *
|
|
946 * @see #equals
|
|
947 */
|
|
948 public override hash_t toHash () {
|
|
949 return cast(hash_t)/*64*/pixmap;
|
|
950 }
|
|
951
|
|
952 void init_(int width, int height) {
|
|
953 if (width <= 0 || height <= 0) {
|
|
954 SWT.error (SWT.ERROR_INVALID_ARGUMENT);
|
|
955 }
|
|
956 this.type = SWT.BITMAP;
|
|
957
|
|
958 /* Create the pixmap */
|
|
959 this.pixmap = cast(GdkDrawable*) OS.gdk_pixmap_new(cast(GdkDrawable*)OS.GDK_ROOT_PARENT(), width, height, -1);
|
|
960 if (pixmap is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
961 /* Fill the bitmap with white */
|
|
962 GdkColor* white = new GdkColor();
|
|
963 white.red = 0xFFFF;
|
|
964 white.green = 0xFFFF;
|
|
965 white.blue = 0xFFFF;
|
|
966 auto colormap = OS.gdk_colormap_get_system();
|
|
967 OS.gdk_colormap_alloc_color(colormap, white, true, true);
|
|
968 auto gdkGC = OS.gdk_gc_new(pixmap);
|
|
969 OS.gdk_gc_set_foreground(gdkGC, white);
|
|
970 OS.gdk_draw_rectangle(pixmap, gdkGC, 1, 0, 0, width, height);
|
|
971 OS.g_object_unref(gdkGC);
|
|
972 OS.gdk_colormap_free_colors(colormap, white, 1);
|
|
973 }
|
|
974
|
|
975 void init_(ImageData image) {
|
|
976 if (image is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
977 int width = image.width;
|
|
978 int height = image.height;
|
|
979 PaletteData palette = image.palette;
|
|
980 if (!(((image.depth is 1 || image.depth is 2 || image.depth is 4 || image.depth is 8) && !palette.isDirect) ||
|
|
981 ((image.depth is 8) || (image.depth is 16 || image.depth is 24 || image.depth is 32) && palette.isDirect)))
|
|
982 SWT.error (SWT.ERROR_UNSUPPORTED_DEPTH);
|
|
983 auto pixbuf = OS.gdk_pixbuf_new( OS.GDK_COLORSPACE_RGB, false, 8, width, height);
|
|
984 if (pixbuf is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
985 int stride = OS.gdk_pixbuf_get_rowstride(pixbuf);
|
|
986 auto data = OS.gdk_pixbuf_get_pixels(pixbuf);
|
|
987 byte[] buffer = image.data;
|
|
988 if (!palette.isDirect || image.depth !is 24 || stride !is image.bytesPerLine || palette.redMask !is 0xFF0000 || palette.greenMask !is 0xFF00 || palette.blueMask !is 0xFF) {
|
|
989 buffer = new byte[stride * height];
|
|
990 if (palette.isDirect) {
|
|
991 ImageData.blit(ImageData.BLIT_SRC,
|
|
992 image.data, image.depth, image.bytesPerLine, image.getByteOrder(), 0, 0, width, height, palette.redMask, palette.greenMask, palette.blueMask,
|
|
993 ImageData.ALPHA_OPAQUE, null, 0, 0, 0,
|
|
994 buffer, 24, stride, ImageData.MSB_FIRST, 0, 0, width, height, 0xFF0000, 0xFF00, 0xFF,
|
|
995 false, false);
|
|
996 } else {
|
|
997 RGB[] rgbs = palette.getRGBs();
|
|
998 int length = rgbs.length;
|
|
999 byte[] srcReds = new byte[length];
|
|
1000 byte[] srcGreens = new byte[length];
|
|
1001 byte[] srcBlues = new byte[length];
|
|
1002 for (int i = 0; i < rgbs.length; i++) {
|
|
1003 RGB rgb = rgbs[i];
|
|
1004 if (rgb is null) continue;
|
|
1005 srcReds[i] = cast(byte)rgb.red;
|
|
1006 srcGreens[i] = cast(byte)rgb.green;
|
|
1007 srcBlues[i] = cast(byte)rgb.blue;
|
|
1008 }
|
|
1009 ImageData.blit(ImageData.BLIT_SRC,
|
|
1010 image.data, image.depth, image.bytesPerLine, image.getByteOrder(), 0, 0, width, height, srcReds, srcGreens, srcBlues,
|
|
1011 ImageData.ALPHA_OPAQUE, null, 0, 0, 0,
|
|
1012 buffer, 24, stride, ImageData.MSB_FIRST, 0, 0, width, height, 0xFF0000, 0xFF00, 0xFF,
|
|
1013 false, false);
|
|
1014 }
|
|
1015 }
|
|
1016 memmove(data, buffer.ptr, stride * height);
|
|
1017 auto pixmap = cast(GdkDrawable*) OS.gdk_pixmap_new (cast(GdkDrawable*) OS.GDK_ROOT_PARENT(), width, height, -1);
|
|
1018 if (pixmap is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
1019 auto gdkGC = OS.gdk_gc_new(pixmap);
|
|
1020 if (gdkGC is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
1021 OS.gdk_pixbuf_render_to_drawable(pixbuf, pixmap, gdkGC, 0, 0, 0, 0, width, height, OS.GDK_RGB_DITHER_NORMAL, 0, 0);
|
|
1022 OS.g_object_unref(gdkGC);
|
|
1023 OS.g_object_unref(pixbuf);
|
|
1024
|
|
1025 bool isIcon = image.getTransparencyType() is SWT.TRANSPARENCY_MASK;
|
|
1026 if (isIcon || image.transparentPixel !is -1) {
|
|
1027 if (image.transparentPixel !is -1) {
|
|
1028 RGB rgb = null;
|
|
1029 if (palette.isDirect) {
|
|
1030 rgb = palette.getRGB(image.transparentPixel);
|
|
1031 } else {
|
|
1032 if (image.transparentPixel < palette.colors.length) {
|
|
1033 rgb = palette.getRGB(image.transparentPixel);
|
|
1034 }
|
|
1035 }
|
|
1036 if (rgb !is null) {
|
|
1037 transparentPixel = rgb.red << 16 | rgb.green << 8 | rgb.blue;
|
|
1038 }
|
|
1039 }
|
|
1040 auto mask = createMask(image, isIcon);
|
|
1041 if (mask is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
1042 this.mask = mask;
|
|
1043 if (isIcon) {
|
|
1044 this.type = SWT.ICON;
|
|
1045 } else {
|
|
1046 this.type = SWT.BITMAP;
|
|
1047 }
|
|
1048 } else {
|
|
1049 this.type = SWT.BITMAP;
|
|
1050 this.mask = null;
|
|
1051 this.alpha = image.alpha;
|
|
1052 if (image.alpha is -1 && image.alphaData !is null) {
|
|
1053 this.alphaData = new byte[image.alphaData.length];
|
|
1054 System.arraycopy(image.alphaData, 0, this.alphaData, 0, alphaData.length);
|
|
1055 }
|
|
1056 createAlphaMask(width, height);
|
|
1057 }
|
|
1058 this.pixmap = pixmap;
|
|
1059 }
|
|
1060
|
|
1061 /**
|
|
1062 * Invokes platform specific functionality to allocate a new GC handle.
|
|
1063 * <p>
|
|
1064 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
|
|
1065 * API for <code>Image</code>. It is marked public only so that it
|
|
1066 * can be shared within the packages provided by SWT. It is not
|
|
1067 * available on all platforms, and should never be called from
|
|
1068 * application code.
|
|
1069 * </p>
|
|
1070 *
|
|
1071 * @param data the platform specific GC data
|
|
1072 * @return the platform specific GC handle
|
|
1073 */
|
|
1074 public GdkGC* internal_new_GC (GCData data) {
|
|
1075 if (pixmap is null) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
1076 if (type !is SWT.BITMAP || memGC !is null) {
|
|
1077 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
1078 }
|
|
1079 auto gdkGC = OS.gdk_gc_new(pixmap);
|
|
1080 if (data !is null) {
|
|
1081 int mask = SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;
|
|
1082 if ((data.style & mask) is 0) {
|
|
1083 data.style |= SWT.LEFT_TO_RIGHT;
|
|
1084 } else {
|
|
1085 if ((data.style & SWT.RIGHT_TO_LEFT) !is 0) {
|
|
1086 data.style |= SWT.MIRRORED;
|
|
1087 }
|
|
1088 }
|
|
1089 data.device = device;
|
|
1090 data.drawable = pixmap;
|
|
1091 data.background = device.COLOR_WHITE.handle;
|
|
1092 data.foreground = device.COLOR_BLACK.handle;
|
|
1093 data.font = device.systemFont;
|
|
1094 data.image = this;
|
|
1095 }
|
|
1096 return gdkGC;
|
|
1097 }
|
|
1098
|
|
1099 /**
|
|
1100 * Invokes platform specific functionality to dispose a GC handle.
|
|
1101 * <p>
|
|
1102 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
|
|
1103 * API for <code>Image</code>. It is marked public only so that it
|
|
1104 * can be shared within the packages provided by SWT. It is not
|
|
1105 * available on all platforms, and should never be called from
|
|
1106 * application code.
|
|
1107 * </p>
|
|
1108 *
|
|
1109 * @param hDC the platform specific GC handle
|
|
1110 * @param data the platform specific GC data
|
|
1111 */
|
|
1112 public void internal_dispose_GC ( GdkGC* gdkGC, GCData data) {
|
|
1113 OS.g_object_unref(gdkGC);
|
|
1114 }
|
|
1115
|
|
1116 /**
|
|
1117 * Returns <code>true</code> if the image has been disposed,
|
|
1118 * and <code>false</code> otherwise.
|
|
1119 * <p>
|
|
1120 * This method gets the dispose state for the image.
|
|
1121 * When an image has been disposed, it is an error to
|
|
1122 * invoke any other method using the image.
|
|
1123 *
|
|
1124 * @return <code>true</code> when the image is disposed and <code>false</code> otherwise
|
|
1125 */
|
|
1126 public override bool isDisposed() {
|
|
1127 return pixmap is null;
|
|
1128 }
|
|
1129
|
|
1130 /**
|
|
1131 * Sets the color to which to map the transparent pixel.
|
|
1132 * <p>
|
|
1133 * There are certain uses of <code>Images</code> that do not support
|
|
1134 * transparency (for example, setting an image into a button or label).
|
|
1135 * In these cases, it may be desired to simulate transparency by using
|
|
1136 * the background color of the widget to paint the transparent pixels
|
|
1137 * of the image. This method specifies the color that will be used in
|
|
1138 * these cases. For example:
|
|
1139 * <pre>
|
|
1140 * Button b = new Button();
|
|
1141 * image.setBackground(b.getBackground());
|
|
1142 * b.setImage(image);
|
|
1143 * </pre>
|
|
1144 * </p><p>
|
|
1145 * The image may be modified by this operation (in effect, the
|
|
1146 * transparent regions may be filled with the supplied color). Hence
|
|
1147 * this operation is not reversible and it is not legal to call
|
|
1148 * this function twice or with a null argument.
|
|
1149 * </p><p>
|
|
1150 * This method has no effect if the receiver does not have a transparent
|
|
1151 * pixel value.
|
|
1152 * </p>
|
|
1153 *
|
|
1154 * @param color the color to use when a transparent pixel is specified
|
|
1155 *
|
|
1156 * @exception IllegalArgumentException <ul>
|
|
1157 * <li>ERROR_NULL_ARGUMENT - if the color is null</li>
|
|
1158 * <li>ERROR_INVALID_ARGUMENT - if the color has been disposed</li>
|
|
1159 * </ul>
|
|
1160 * @exception SWTException <ul>
|
|
1161 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
1162 * </ul>
|
|
1163 */
|
|
1164 public void setBackground(Color color) {
|
|
1165 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
1166 if (color is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
1167 if (color.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
1168 if (transparentPixel is -1) return;
|
|
1169 //NOT DONE
|
|
1170 }
|
|
1171
|
|
1172 /**
|
|
1173 * Returns a string containing a concise, human-readable
|
|
1174 * description of the receiver.
|
|
1175 *
|
|
1176 * @return a string representation of the receiver
|
|
1177 */
|
|
1178 public override String toString () {
|
|
1179 if (isDisposed()) return "Image {*DISPOSED*}";
|
|
1180 return Format( "Image {{{}}", pixmap);
|
|
1181 }
|
|
1182
|
|
1183 }
|
|
1184
|