Mercurial > projects > dwt-mac
comparison dwt/dnd/ImageTransfer.d @ 45:d8635bb48c7c
Merge with SWT 3.5
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 01 Dec 2008 17:07:00 +0100 |
parents | a9ab4c738ed8 |
children | ed7dd292bfb3 |
comparison
equal
deleted
inserted
replaced
44:ca5e494f2bbf | 45:d8635bb48c7c |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2007 IBM Corporation and others. | 2 * Copyright (c) 2007, 2008 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * Outhink - support for typeFileURL | 10 * Outhink - support for typeFileURL |
11 *******************************************************************************/ | 11 *******************************************************************************/ |
12 module dwt.dnd; | 12 module dwt.dnd.ImageTransfer; |
13 | |
14 import dwt.dwthelper.utils; | |
13 | 15 |
14 import dwt.DWT; | 16 import dwt.DWT; |
15 import dwt.graphics.*; | 17 import dwt.graphics.Image; |
16 import dwt.internal.carbon.*; | 18 import dwt.graphics.ImageData; |
17 import dwt.widgets.*; | 19 import dwt.internal.cocoa.NSData; |
20 import dwt.internal.cocoa.NSImage; | |
21 import dwt.internal.cocoa.OS; | |
22 import dwt.widgets.Display; | |
18 | 23 |
19 /** | 24 /** |
20 * The class <code>ImageTransfer</code> provides a platform specific mechanism | 25 * The class <code>ImageTransfer</code> provides a platform specific mechanism |
21 * for converting an Image represented as a java <code>ImageData</code> to a | 26 * for converting an Image represented as a java <code>ImageData</code> to a |
22 * platform specific representation of the data and vice versa. The | 27 * platform specific representation of the data and vice versa. |
23 * <code>ImageData</code> contains infomration about the Image. See | |
24 * <code>Transfer</code> for additional information. | |
25 * | 28 * |
26 * <p> | 29 * <p>An example of a java <code>ImageData</code> is shown below:</p> |
27 * An example of a java <code>Image</code> containing an ImageData is shown | |
28 * below: | |
29 * </p> | |
30 * | 30 * |
31 * <code><pre> | 31 * <code><pre> |
32 * Image image = new Image(display, fileName); | 32 * Image image = new Image(display, "C:\temp\img1.gif"); |
33 * ImageData imgData = image.getImageData(); | 33 * ImageData imgData = image.getImageData(); |
34 * </code></pre> | 34 * </code></pre> |
35 * | |
36 * @see Transfer | |
37 * | |
38 * @since 3.4 | |
35 */ | 39 */ |
36 public class ImageTransfer : ByteArrayTransfer { | 40 public class ImageTransfer extends ByteArrayTransfer { |
37 | 41 |
38 static ImageTransfer _instance = new ImageTransfer(); | 42 static ImageTransfer _instance = new ImageTransfer(); |
39 static final String PICT = "PICT"; //$NON-NLS-1$ | 43 static final String TIFF = OS.NSTIFFPboardType.getString(); |
40 static final String TIFF = "TIFF"; //$NON-NLS-1$ | |
41 static final int PICTID = registerType(PICT); | |
42 static final int TIFFID = registerType(TIFF); | 44 static final int TIFFID = registerType(TIFF); |
43 | 45 |
44 this() { | 46 ImageTransfer() { |
45 } | 47 } |
46 | 48 |
47 /** | 49 /** |
48 * Returns the singleton instance of the ImageTransfer class. | 50 * Returns the singleton instance of the ImageTransfer class. |
49 * | 51 * |
50 * @return the singleton instance of the ImageTransfer class | 52 * @return the singleton instance of the ImageTransfer class |
51 */ | 53 */ |
52 public static ImageTransfer getInstance() { | 54 public static ImageTransfer getInstance() { |
53 return _instance; | 55 return _instance; |
54 } | 56 } |
55 | 57 |
56 /** | 58 /** |
57 * This implementation of <code>javaToNative</code> converts an ImageData | 59 * This implementation of <code>javaToNative</code> converts an ImageData object represented |
58 * object represented by a java <code>ImageData</code> to a platform | 60 * by java <code>ImageData</code> to a platform specific representation. |
59 * specific representation. For additional information see | |
60 * <code>Transfer#javaToNative</code>. | |
61 * | 61 * |
62 * @param object | 62 * @param object a java <code>ImageData</code> containing the ImageData to be converted |
63 * a java <code>ImageData</code> | 63 * @param transferData an empty <code>TransferData</code> object that will |
64 * @param transferData | 64 * be filled in on return with the platform specific format of the data |
65 * an empty <code>TransferData</code> object; this object will | 65 * |
66 * be filled in on return with the platform specific format of | 66 * @see Transfer#nativeToJava |
67 * the data | |
68 */ | 67 */ |
69 public void javaToNative(Object object, TransferData transferData) { | 68 public void javaToNative(Object object, TransferData transferData) { |
70 if (!checkImage(object) || !isSupportedType(transferData)) { | 69 if (!checkImage(object) || !isSupportedType(transferData)) { |
71 DND.error(DND.ERROR_INVALID_DATA); | 70 DND.error(DND.ERROR_INVALID_DATA); |
72 } | 71 } |
73 transferData.result = -1; | 72 ImageData imgData = (ImageData) object; |
74 | |
75 ImageData imgData = cast(ImageData) object; | |
76 Image image = new Image(Display.getCurrent(), imgData); | 73 Image image = new Image(Display.getCurrent(), imgData); |
77 int handle = image.handle; | 74 NSImage handle = image.handle; |
78 int width = OS.CGImageGetWidth(handle); | 75 transferData.data = handle.TIFFRepresentation(); |
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 = cast(short) width; | |
87 rect.bottom = cast(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, cast(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(); | 76 image.dispose(); |
114 | |
115 transferData.data = new byte[][] { pictData }; | |
116 transferData.result = OS.noErr; | |
117 } | 77 } |
118 | 78 |
119 /** | 79 /** |
120 * This implementation of <code>nativeToJava</code> converts a platform | 80 * This implementation of <code>nativeToJava</code> converts a platform specific |
121 * specific representation of an ImageData <code>ImageData</code>. For | 81 * representation of an image to java <code>ImageData</code>. |
122 * additional information see <code>Transfer#nativeToJava</code>. | |
123 * | 82 * |
124 * @param transferData | 83 * @param transferData the platform specific representation of the data to be converted |
125 * the platform specific representation of the data to be been | 84 * @return a java <code>ImageData</code> of the image if the conversion was successful; |
126 * converted | 85 * otherwise null |
127 * @return a java <code>ImageData</code> object if the conversion was | 86 * |
128 * successful; otherwise null | 87 * @see Transfer#javaToNative |
129 */ | 88 */ |
130 public Object nativeToJava(TransferData transferData) { | 89 public Object nativeToJava(TransferData transferData) { |
131 if (!isSupportedType(transferData) || transferData.data is null) | 90 if (!isSupportedType(transferData) || transferData.data is null) return null; |
132 return null; | 91 NSData data = (NSData) transferData.data; |
133 if (transferData.data.length is 0) | 92 if (data.length() is 0) return null; |
134 return null; | 93 NSImage nsImage = (NSImage) new NSImage().alloc(); |
135 byte[] dataArr = transferData.data[0]; | 94 nsImage.initWithData(data); |
136 int size = dataArr.length; | 95 //TODO: Image representation wrong??? |
137 int pictPtr = OS.NewPtr(size); | 96 Image image = Image.cocoa_new(Display.getCurrent(), DWT.BITMAP, nsImage); |
138 OS.memmove(pictPtr, dataArr, size); | 97 ImageData imageData = image.getImageData(); |
139 int dataProvider = OS.CGDataProviderCreateWithData(0, pictPtr, size, 0); | 98 image.dispose(); |
140 if (dataProvider !is 0) { | 99 return imageData; |
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 = cast(int) rect.width; | |
146 int height = cast(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 } | 100 } |
193 | 101 |
194 protected int[] getTypeIds() { | 102 protected int[] getTypeIds() { |
195 return new int[] { PICTID }; | 103 return new int[] { TIFFID }; |
196 } | 104 } |
197 | 105 |
198 protected String[] getTypeNames() { | 106 protected String[] getTypeNames() { |
199 return new String[] { PICT }; | 107 return new String[] { TIFF }; |
200 } | 108 } |
201 | 109 |
202 bool checkImage(Object object) { | 110 bool checkImage(Object object) { |
203 if (object is null || !( null !is cast(ImageData)object )) return false; | 111 if (object is null || !(object instanceof ImageData)) return false; |
204 return true; | 112 return true; |
205 } | 113 } |
206 | 114 |
207 protected bool validate(Object object) { | 115 protected bool validate(Object object) { |
208 return checkImage(object); | 116 return checkImage(object); |