diff 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
line wrap: on
line diff
--- a/dwt/dnd/ImageTransfer.d	Tue Oct 21 15:20:04 2008 +0200
+++ b/dwt/dnd/ImageTransfer.d	Mon Dec 01 17:07:00 2008 +0100
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007 IBM Corporation and others.
+ * Copyright (c) 2007, 2008 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,44 +9,46 @@
  *     IBM Corporation - initial API and implementation
  *     Outhink - support for typeFileURL
  *******************************************************************************/
-module dwt.dnd;
+module dwt.dnd.ImageTransfer;
+
+import dwt.dwthelper.utils;
 
 import dwt.DWT;
-import dwt.graphics.*;
-import dwt.internal.carbon.*;
-import dwt.widgets.*;
+import dwt.graphics.Image;
+import dwt.graphics.ImageData;
+import dwt.internal.cocoa.NSData;
+import dwt.internal.cocoa.NSImage;
+import dwt.internal.cocoa.OS;
+import dwt.widgets.Display;
 
 /**
- * The class <code>ImageTransfer</code> provides a platform specific mechanism
- * for converting an Image represented as a java <code>ImageData</code> to a
- * platform specific representation of the data and vice versa. The
- * <code>ImageData</code> contains infomration about the Image. See
- * <code>Transfer</code> for additional information.
+ * The class <code>ImageTransfer</code> provides a platform specific mechanism 
+ * for converting an Image represented as a java <code>ImageData</code> to a 
+ * platform specific representation of the data and vice versa.  
  * 
- * <p>
- * An example of a java <code>Image</code> containing an ImageData is shown
- * below:
- * </p>
+ * <p>An example of a java <code>ImageData</code> is shown below:</p>
  * 
  * <code><pre>
- * Image image = new Image(display, fileName);
- * ImageData imgData = image.getImageData();
+ *     Image image = new Image(display, "C:\temp\img1.gif");
+ *     ImageData imgData = image.getImageData();
  * </code></pre>
+ *
+ * @see Transfer
+ * 
+ * @since 3.4
  */
-public class ImageTransfer : ByteArrayTransfer {
+public class ImageTransfer extends ByteArrayTransfer {
 
 static ImageTransfer _instance = new ImageTransfer();
-static final String PICT = "PICT"; //$NON-NLS-1$
-static final String TIFF = "TIFF"; //$NON-NLS-1$
-static final int PICTID = registerType(PICT);
+static final String TIFF = OS.NSTIFFPboardType.getString();
 static final int TIFFID = registerType(TIFF);
 
-this() {
+ImageTransfer() {
 }
 
 /**
  * Returns the singleton instance of the ImageTransfer class.
- * 
+ *
  * @return the singleton instance of the ImageTransfer class
  */
 public static ImageTransfer getInstance() {
@@ -54,153 +56,59 @@
 }
 
 /**
- * This implementation of <code>javaToNative</code> converts an ImageData
- * object represented by a java <code>ImageData</code> to a platform
- * specific representation. For additional information see
- * <code>Transfer#javaToNative</code>.
+ * This implementation of <code>javaToNative</code> converts an ImageData object represented
+ * by java <code>ImageData</code> to a platform specific representation.
  * 
- * @param object
- *            a java <code>ImageData</code>
- * @param transferData
- *            an empty <code>TransferData</code> object; this object will
- *            be filled in on return with the platform specific format of
- *            the data
+ * @param object a java <code>ImageData</code> containing the ImageData to be converted
+ * @param transferData an empty <code>TransferData</code> object that will
+ *      be filled in on return with the platform specific format of the data
+ * 
+ * @see Transfer#nativeToJava
  */
 public void javaToNative(Object object, TransferData transferData) {
     if (!checkImage(object) || !isSupportedType(transferData)) {
         DND.error(DND.ERROR_INVALID_DATA);
     }
-    transferData.result = -1;
-
-    ImageData imgData = cast(ImageData) object;
+    ImageData imgData = (ImageData) object;
     Image image = new Image(Display.getCurrent(), imgData);
-    int handle = image.handle;
-    int width = OS.CGImageGetWidth(handle);
-    int height = OS.CGImageGetHeight(handle);
-    int alphaInfo = OS.CGImageGetAlphaInfo(handle);
-    int bpr = OS.CGImageGetBytesPerRow(handle);
-
-    Rect rect = new Rect();
-    rect.left = 0;
-    rect.top = 0;
-    rect.right = cast(short) width;
-    rect.bottom = cast(short) height;
-
-    int[] gWorld = new int[1];
-    int format = OS.k24RGBPixelFormat;
-    if (alphaInfo !is OS.kCGImageAlphaNoneSkipFirst) {
-        format = OS.k32ARGBPixelFormat;
-    }
-    OS.NewGWorldFromPtr(gWorld, format, rect, 0, 0, 0, image.data, bpr);
-    int[] curPort = new int[1];
-    int[] curGWorld = new int[1];
-    OS.GetGWorld(curPort, curGWorld);
-    OS.SetGWorld(gWorld[0], curGWorld[0]);
-    int pictHandle = OS.OpenPicture(rect);
-    int portBitMap = OS.GetPortBitMapForCopyBits(gWorld[0]);
-    OS.CopyBits(portBitMap, portBitMap, rect, rect, cast(short) OS.srcCopy, 0);
-    OS.ClosePicture();
-    OS.SetGWorld(curPort[0], curGWorld[0]);
-    OS.DisposeGWorld(gWorld[0]);
-    int length = OS.GetHandleSize(pictHandle);
-    OS.HLock(pictHandle);
-    int[] buffer = new int[1];
-    OS.memmove(buffer, pictHandle, 4);
-    byte[] pictData = new byte[length];
-    OS.memmove(pictData, buffer[0], length);
-    OS.HUnlock(pictHandle);
-    OS.KillPicture(pictHandle);
+    NSImage handle = image.handle;
+    transferData.data = handle.TIFFRepresentation();
     image.dispose();
-
-    transferData.data = new byte[][] { pictData };
-    transferData.result = OS.noErr;
 }
 
 /**
- * This implementation of <code>nativeToJava</code> converts a platform
- * specific representation of an ImageData <code>ImageData</code>. For
- * additional information see <code>Transfer#nativeToJava</code>.
+ * This implementation of <code>nativeToJava</code> converts a platform specific 
+ * representation of an image to java <code>ImageData</code>.  
  * 
- * @param transferData
- *            the platform specific representation of the data to be been
- *            converted
- * @return a java <code>ImageData</code> object if the conversion was
- *         successful; otherwise null
+ * @param transferData the platform specific representation of the data to be converted
+ * @return a java <code>ImageData</code> of the image if the conversion was successful;
+ *      otherwise null
+ * 
+ * @see Transfer#javaToNative
  */
 public Object nativeToJava(TransferData transferData) {
-    if (!isSupportedType(transferData) || transferData.data is null)
-        return null;
-    if (transferData.data.length is 0)
-        return null;
-    byte[] dataArr = transferData.data[0];
-    int size = dataArr.length;
-    int pictPtr = OS.NewPtr(size);
-    OS.memmove(pictPtr, dataArr, size);
-    int dataProvider = OS.CGDataProviderCreateWithData(0, pictPtr, size, 0);
-    if (dataProvider !is 0) {
-        int pictDataRef = OS.QDPictCreateWithProvider(dataProvider);
-        // get bounds for the image
-        CGRect rect = new CGRect();
-        OS.QDPictGetBounds(pictDataRef, rect);
-        int width = cast(int) rect.width;
-        int height = cast(int) rect.height;
-
-        /* Create the image */
-        int bpr = width * 4;
-        int dataSize = height * bpr;
-        int data = OS.NewPtr(dataSize);
-        if (data is 0)
-            DWT.error(DWT.ERROR_NO_HANDLES);
-        int provider = OS
-                .CGDataProviderCreateWithData(0, data, dataSize, 0);
-        if (provider is 0) {
-            OS.DisposePtr(data);
-            DWT.error(DWT.ERROR_NO_HANDLES);
-        }
-        int colorspace = OS.CGColorSpaceCreateDeviceRGB();
-        if (colorspace is 0)
-            DWT.error(DWT.ERROR_NO_HANDLES);
-        int handle = OS.CGImageCreate(width, height, 8, 32, bpr,
-                colorspace, OS.kCGImageAlphaNoneSkipFirst, provider, null,
-                true, 0);
-        OS.CGDataProviderRelease(provider);
-        if (handle is 0) {
-            OS.DisposePtr(data);
-            DWT.error(DWT.ERROR_NO_HANDLES);
-        }
-        int bpc = OS.CGImageGetBitsPerComponent(handle);
-        int context = OS.CGBitmapContextCreate(data, width, height, bpc,
-                bpr, colorspace, OS.kCGImageAlphaNoneSkipFirst);
-        if (context is 0) {
-            OS.CGImageRelease(handle);
-            OS.DisposePtr(data);
-            DWT.error(DWT.ERROR_NO_HANDLES);
-        }
-        int status = OS.QDPictDrawToCGContext(context, rect, pictDataRef);
-        ImageData imgData = null;
-        if (status is 0) {
-            Image image = Image.carbon_new(Display.getCurrent(),
-                    DWT.BITMAP, handle, data);
-            imgData = image.getImageData();
-            image.dispose();
-        }
-        OS.CGContextRelease(context);
-        OS.QDPictRelease(pictDataRef);
-        return imgData;
-    }
-    return null;
+    if (!isSupportedType(transferData) || transferData.data is null) return null;
+    NSData data = (NSData) transferData.data;
+    if (data.length() is 0) return null;
+    NSImage nsImage = (NSImage) new NSImage().alloc();
+    nsImage.initWithData(data);
+    //TODO: Image representation wrong???
+    Image image = Image.cocoa_new(Display.getCurrent(), DWT.BITMAP, nsImage);
+    ImageData imageData = image.getImageData();
+    image.dispose();
+    return imageData;
 }
 
 protected int[] getTypeIds() {
-    return new int[] { PICTID };
+    return new int[] { TIFFID };
 }
 
 protected String[] getTypeNames() {
-    return new String[] { PICT };
+    return new String[] { TIFF };
 }
 
 bool checkImage(Object object) {
-    if (object is null || !( null !is cast(ImageData)object )) return false;
+    if (object is null || !(object instanceof ImageData)) return false;
     return true;
 }