240
|
1 /*******************************************************************************
|
259
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
240
|
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 *******************************************************************************/
|
|
11 module dwt.dnd.ImageTransfer;
|
|
12
|
|
13 import dwt.DWT;
|
|
14 import dwt.graphics.Image;
|
|
15 import dwt.graphics.ImageData;
|
|
16 import dwt.internal.Converter;
|
|
17 import dwt.internal.gtk.OS;
|
|
18 import dwt.widgets.Display;
|
|
19 import dwt.dnd.ByteArrayTransfer;
|
|
20 import dwt.dnd.TransferData;
|
|
21 import dwt.dnd.DND;
|
|
22
|
|
23 import dwt.dwthelper.utils;
|
|
24
|
|
25 /**
|
|
26 * The class <code>ImageTransfer</code> provides a platform specific mechanism
|
259
|
27 * for converting an Image represented as a java <code>ImageData</code> to a
|
240
|
28 * platform specific representation of the data and vice versa.
|
|
29 *
|
259
|
30 * <p>An example of a java <code>ImageData</code> is shown below:</p>
|
240
|
31 *
|
|
32 * <code><pre>
|
259
|
33 * Image image = new Image(display, "C:\temp\img1.gif");
|
240
|
34 * ImageData imgData = image.getImageData();
|
|
35 * </code></pre>
|
259
|
36 *
|
|
37 * @see Transfer
|
|
38 *
|
|
39 * @since 3.4
|
240
|
40 */
|
|
41 public class ImageTransfer : ByteArrayTransfer {
|
|
42
|
|
43 private static ImageTransfer _instance;
|
|
44
|
|
45 private static const String JPEG = "image/jpge"; //$NON-NLS-1$
|
|
46 private static int JPEG_ID;
|
|
47 private static const String PNG = "image/png"; //$NON-NLS-1$
|
|
48 private static int PNG_ID;
|
|
49 private static const String BMP = "image/bmp"; //$NON-NLS-1$
|
|
50 private static int BMP_ID;
|
|
51 private static const String EPS = "image/eps"; //$NON-NLS-1$
|
|
52 private static int EPS_ID;
|
|
53 private static const String PCX = "image/pcx"; //$NON-NLS-1$
|
|
54 private static int PCX_ID;
|
|
55 private static const String PPM = "image/ppm"; //$NON-NLS-1$
|
|
56 private static int PPM_ID;
|
|
57 private static const String RGB = "image/ppm"; //$NON-NLS-1$
|
|
58 private static int RGB_ID;
|
|
59 private static const String TGA = "image/tga"; //$NON-NLS-1$
|
|
60 private static int TGA_ID;
|
|
61 private static const String XBM = "image/xbm"; //$NON-NLS-1$
|
|
62 private static int XBM_ID;
|
|
63 private static const String XPM = "image/xpm"; //$NON-NLS-1$
|
|
64 private static int XPM_ID;
|
|
65 private static const String XV = "image/xv"; //$NON-NLS-1$
|
|
66 private static int XV_ID;
|
|
67
|
|
68 static this(){
|
|
69 JPEG_ID = registerType(JPEG);
|
|
70 PNG_ID = registerType(PNG);
|
|
71 BMP_ID = registerType(BMP);
|
|
72 EPS_ID = registerType(EPS);
|
|
73 PCX_ID = registerType(PCX);
|
|
74 PPM_ID = registerType(PPM);
|
|
75 RGB_ID = registerType(RGB);
|
|
76 TGA_ID = registerType(TGA);
|
|
77 XBM_ID = registerType(XBM);
|
|
78 XPM_ID = registerType(XPM);
|
|
79 XV_ID = registerType(XV);
|
|
80 _instance = new ImageTransfer();
|
|
81 }
|
|
82
|
|
83 private this() {
|
|
84 }
|
|
85
|
|
86 /**
|
|
87 * Returns the singleton instance of the ImageTransfer class.
|
|
88 *
|
|
89 * @return the singleton instance of the ImageTransfer class
|
|
90 */
|
|
91 public static ImageTransfer getInstance () {
|
|
92 return _instance;
|
|
93 }
|
|
94
|
|
95 /**
|
|
96 * This implementation of <code>javaToNative</code> converts an ImageData object represented
|
|
97 * by java <code>ImageData</code> to a platform specific representation.
|
|
98 *
|
259
|
99 * @param object a java <code>ImageData</code> containing the ImageData to be converted
|
|
100 * @param transferData an empty <code>TransferData</code> object that will
|
|
101 * be filled in on return with the platform specific format of the data
|
|
102 *
|
|
103 * @see Transfer#nativeToJava
|
240
|
104 */
|
|
105 public void javaToNative(Object object, TransferData transferData) {
|
|
106 if (!checkImage(object) || !isSupportedType(transferData)) {
|
|
107 DND.error(DND.ERROR_INVALID_DATA);
|
|
108 }
|
|
109 if (OS.GTK_VERSION < OS.buildVERSION (2, 4, 0)) return;
|
|
110
|
|
111 ImageData imgData = cast(ImageData)object;
|
|
112 if (imgData is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
|
|
113 Image image = new Image(Display.getCurrent(), imgData);
|
|
114 auto pixmap = image.pixmap;
|
|
115 int width = imgData.width;
|
|
116 int height = imgData.height;
|
|
117 auto pixbuf = OS.gdk_pixbuf_new(OS.GDK_COLORSPACE_RGB, true, 8, width, height);
|
|
118 if (pixbuf is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
119 auto colormap = OS.gdk_colormap_get_system();
|
|
120 OS.gdk_pixbuf_get_from_drawable(pixbuf, pixmap, colormap, 0, 0, 0, 0, width, height);
|
|
121
|
|
122 String typeStr = "";
|
|
123 if (transferData.type is cast(void*)JPEG_ID) typeStr = "jpeg";
|
|
124 if (transferData.type is cast(void*)PNG_ID) typeStr = "png";
|
|
125 if (transferData.type is cast(void*)BMP_ID) typeStr = "bmp";
|
|
126 if (transferData.type is cast(void*)EPS_ID) typeStr = "eps";
|
|
127 if (transferData.type is cast(void*)PCX_ID) typeStr = "pcx";
|
|
128 if (transferData.type is cast(void*)PPM_ID) typeStr = "ppm";
|
|
129 if (transferData.type is cast(void*)RGB_ID) typeStr = "rgb";
|
|
130 if (transferData.type is cast(void*)TGA_ID) typeStr = "tga";
|
|
131 if (transferData.type is cast(void*)XBM_ID) typeStr = "xbm";
|
|
132 if (transferData.type is cast(void*)XPM_ID) typeStr = "xpm";
|
|
133 if (transferData.type is cast(void*)XV_ID) typeStr = "xv";
|
|
134 char* type = typeStr.ptr;
|
|
135 char* buffer;
|
|
136 uint len;
|
|
137 if (type is null) return;
|
|
138 OS.gdk_pixbuf_save_to_buffer0(pixbuf, &buffer, &len, type, null);
|
|
139 OS.g_object_unref(pixbuf);
|
|
140 image.dispose();
|
|
141 transferData.pValue = buffer;
|
|
142 transferData.length = (len + 3) / 4 * 4;
|
|
143 transferData.result = 1;
|
|
144 transferData.format = 32;
|
|
145 }
|
|
146
|
|
147 /**
|
|
148 * This implementation of <code>nativeToJava</code> converts a platform specific
|
259
|
149 * representation of an image to java <code>ImageData</code>.
|
|
150 *
|
|
151 * @param transferData the platform specific representation of the data to be converted
|
|
152 * @return a java <code>ImageData</code> of the image if the conversion was successful;
|
|
153 * otherwise null
|
|
154 *
|
|
155 * @see Transfer#javaToNative
|
240
|
156 */
|
|
157 public Object nativeToJava(TransferData transferData) {
|
|
158 ImageData imgData = null;
|
|
159 if (transferData.length > 0)
|
|
160 {
|
|
161 auto loader = OS.gdk_pixbuf_loader_new();
|
|
162 OS.gdk_pixbuf_loader_write(loader, transferData.pValue, transferData.length, null);
|
|
163 OS.gdk_pixbuf_loader_close(loader, null);
|
|
164 auto pixbuf = OS.gdk_pixbuf_loader_get_pixbuf(loader);
|
|
165 if (pixbuf !is null) {
|
|
166 OS.g_object_ref(pixbuf);
|
|
167 GdkPixmap* pixmap_return;
|
|
168 OS.gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap_return, null, 0);
|
|
169 auto handle = pixmap_return;
|
|
170 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES);
|
|
171 OS.g_object_unref(loader);
|
|
172 Image img = Image.gtk_new(Display.getCurrent(), DWT.BITMAP, handle, null);
|
|
173 imgData = img.getImageData();
|
|
174 img.dispose();
|
|
175 }
|
|
176 }
|
|
177 return imgData;
|
|
178 }
|
|
179
|
|
180 protected int[] getTypeIds(){
|
|
181 return [JPEG_ID, PNG_ID, BMP_ID, EPS_ID, PCX_ID, PPM_ID, RGB_ID, TGA_ID, XBM_ID, XPM_ID, XV_ID];
|
|
182 }
|
|
183
|
|
184 protected String[] getTypeNames(){
|
|
185 return [JPEG, PNG, BMP, EPS, PCX, PPM, RGB, TGA, XBM, XPM, XV];
|
|
186 }
|
|
187
|
|
188 bool checkImage(Object object) {
|
|
189 if (object is null || !( null !is cast(ImageData)object )) return false;
|
|
190 return true;
|
|
191 }
|
|
192
|
|
193 protected bool validate(Object object) {
|
|
194 return checkImage(object);
|
|
195 }
|
|
196 }
|