comparison dwt/dnd/ImageTransfer.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2007 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 * Outhink - support for typeFileURL
11 *******************************************************************************/
12 module dwt.dnd;
13
14 import dwt.DWT;
15 import dwt.graphics.*;
16 import dwt.internal.carbon.*;
17 import dwt.widgets.*;
18
19 /**
20 * The class <code>ImageTransfer</code> provides a platform specific mechanism
21 * for converting an Image represented as a java <code>ImageData</code> to a
22 * platform specific representation of the data and vice versa. The
23 * <code>ImageData</code> contains infomration about the Image. See
24 * <code>Transfer</code> for additional information.
25 *
26 * <p>
27 * An example of a java <code>Image</code> containing an ImageData is shown
28 * below:
29 * </p>
30 *
31 * <code><pre>
32 * Image image = new Image(display, fileName);
33 * ImageData imgData = image.getImageData();
34 * </code></pre>
35 */
36 public class ImageTransfer : ByteArrayTransfer {
37
38 static ImageTransfer _instance = new ImageTransfer();
39 static final String PICT = "PICT"; //$NON-NLS-1$
40 static final String TIFF = "TIFF"; //$NON-NLS-1$
41 static final int PICTID = registerType(PICT);
42 static final int TIFFID = registerType(TIFF);
43
44 ImageTransfer() {
45 }
46
47 /**
48 * Returns the singleton instance of the ImageTransfer class.
49 *
50 * @return the singleton instance of the ImageTransfer class
51 */
52 public static ImageTransfer getInstance() {
53 return _instance;
54 }
55
56 /**
57 * This implementation of <code>javaToNative</code> converts an ImageData
58 * object represented by a java <code>ImageData</code> to a platform
59 * specific representation. For additional information see
60 * <code>Transfer#javaToNative</code>.
61 *
62 * @param object
63 * a java <code>ImageData</code>
64 * @param transferData
65 * an empty <code>TransferData</code> object; this object will
66 * be filled in on return with the platform specific format of
67 * the data
68 */
69 public void javaToNative(Object object, TransferData transferData) {
70 if (!checkImage(object) || !isSupportedType(transferData)) {
71 DND.error(DND.ERROR_INVALID_DATA);
72 }
73 transferData.result = -1;
74
75 ImageData imgData = (ImageData) object;
76 Image image = new Image(Display.getCurrent(), imgData);
77 int handle = image.handle;
78 int width = OS.CGImageGetWidth(handle);
79 int height = OS.CGImageGetHeight(handle);
80 int alphaInfo = OS.CGImageGetAlphaInfo(handle);
81 int bpr = OS.CGImageGetBytesPerRow(handle);
82
83 Rect rect = new Rect();
84 rect.left = 0;
85 rect.top = 0;
86 rect.right = (short) width;
87 rect.bottom = (short) height;
88
89 int[] gWorld = new int[1];
90 int format = OS.k24RGBPixelFormat;
91 if (alphaInfo !is OS.kCGImageAlphaNoneSkipFirst) {
92 format = OS.k32ARGBPixelFormat;
93 }
94 OS.NewGWorldFromPtr(gWorld, format, rect, 0, 0, 0, image.data, bpr);
95 int[] curPort = new int[1];
96 int[] curGWorld = new int[1];
97 OS.GetGWorld(curPort, curGWorld);
98 OS.SetGWorld(gWorld[0], curGWorld[0]);
99 int pictHandle = OS.OpenPicture(rect);
100 int portBitMap = OS.GetPortBitMapForCopyBits(gWorld[0]);
101 OS.CopyBits(portBitMap, portBitMap, rect, rect, (short) OS.srcCopy, 0);
102 OS.ClosePicture();
103 OS.SetGWorld(curPort[0], curGWorld[0]);
104 OS.DisposeGWorld(gWorld[0]);
105 int length = OS.GetHandleSize(pictHandle);
106 OS.HLock(pictHandle);
107 int[] buffer = new int[1];
108 OS.memmove(buffer, pictHandle, 4);
109 byte[] pictData = new byte[length];
110 OS.memmove(pictData, buffer[0], length);
111 OS.HUnlock(pictHandle);
112 OS.KillPicture(pictHandle);
113 image.dispose();
114
115 transferData.data = new byte[][] { pictData };
116 transferData.result = OS.noErr;
117 }
118
119 /**
120 * This implementation of <code>nativeToJava</code> converts a platform
121 * specific representation of an ImageData <code>ImageData</code>. For
122 * additional information see <code>Transfer#nativeToJava</code>.
123 *
124 * @param transferData
125 * the platform specific representation of the data to be been
126 * converted
127 * @return a java <code>ImageData</code> object if the conversion was
128 * successful; otherwise null
129 */
130 public Object nativeToJava(TransferData transferData) {
131 if (!isSupportedType(transferData) || transferData.data is null)
132 return null;
133 if (transferData.data.length is 0)
134 return null;
135 byte[] dataArr = transferData.data[0];
136 int size = dataArr.length;
137 int pictPtr = OS.NewPtr(size);
138 OS.memmove(pictPtr, dataArr, size);
139 int dataProvider = OS.CGDataProviderCreateWithData(0, pictPtr, size, 0);
140 if (dataProvider !is 0) {
141 int pictDataRef = OS.QDPictCreateWithProvider(dataProvider);
142 // get bounds for the image
143 CGRect rect = new CGRect();
144 OS.QDPictGetBounds(pictDataRef, rect);
145 int width = (int) rect.width;
146 int height = (int) rect.height;
147
148 /* Create the image */
149 int bpr = width * 4;
150 int dataSize = height * bpr;
151 int data = OS.NewPtr(dataSize);
152 if (data is 0)
153 DWT.error(DWT.ERROR_NO_HANDLES);
154 int provider = OS
155 .CGDataProviderCreateWithData(0, data, dataSize, 0);
156 if (provider is 0) {
157 OS.DisposePtr(data);
158 DWT.error(DWT.ERROR_NO_HANDLES);
159 }
160 int colorspace = OS.CGColorSpaceCreateDeviceRGB();
161 if (colorspace is 0)
162 DWT.error(DWT.ERROR_NO_HANDLES);
163 int handle = OS.CGImageCreate(width, height, 8, 32, bpr,
164 colorspace, OS.kCGImageAlphaNoneSkipFirst, provider, null,
165 true, 0);
166 OS.CGDataProviderRelease(provider);
167 if (handle is 0) {
168 OS.DisposePtr(data);
169 DWT.error(DWT.ERROR_NO_HANDLES);
170 }
171 int bpc = OS.CGImageGetBitsPerComponent(handle);
172 int context = OS.CGBitmapContextCreate(data, width, height, bpc,
173 bpr, colorspace, OS.kCGImageAlphaNoneSkipFirst);
174 if (context is 0) {
175 OS.CGImageRelease(handle);
176 OS.DisposePtr(data);
177 DWT.error(DWT.ERROR_NO_HANDLES);
178 }
179 int status = OS.QDPictDrawToCGContext(context, rect, pictDataRef);
180 ImageData imgData = null;
181 if (status is 0) {
182 Image image = Image.carbon_new(Display.getCurrent(),
183 DWT.BITMAP, handle, data);
184 imgData = image.getImageData();
185 image.dispose();
186 }
187 OS.CGContextRelease(context);
188 OS.QDPictRelease(pictDataRef);
189 return imgData;
190 }
191 return null;
192 }
193
194 protected int[] getTypeIds() {
195 return new int[] { PICTID };
196 }
197
198 protected String[] getTypeNames() {
199 return new String[] { PICT };
200 }
201
202 bool checkImage(Object object) {
203 if (object is null || !(object instanceof ImageData)) return false;
204 return true;
205 }
206
207 protected bool validate(Object object) {
208 return checkImage(object);
209 }
210 }