diff dwt/graphics/Image.d @ 32:b9226997409c

Ported dwt.graphics.Image*
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Fri, 12 Sep 2008 13:44:30 +0200
parents a9ab4c738ed8
children 7d135fe0caf2
line wrap: on
line diff
--- a/dwt/graphics/Image.d	Fri Sep 12 12:36:09 2008 +0200
+++ b/dwt/graphics/Image.d	Fri Sep 12 13:44:30 2008 +0200
@@ -7,13 +7,14 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ *     
+ * Port to the D programming language:
+ *     Jacob Carlborg <jacob.carlborg@gmail.com>
  *******************************************************************************/
 module dwt.graphics.Image;
 
-import dwt.dwthelper.utils;
-
  
-import java.io.InputStream;
+import dwt.dwthelper.InputStream;
 
 import dwt.DWT;
 import dwt.DWTError;
@@ -26,6 +27,22 @@
 import dwt.internal.cocoa.NSSize;
 import dwt.internal.cocoa.NSString;
 import dwt.internal.cocoa.OS;
+
+import tango.text.convert.Format;
+
+import dwt.dwthelper.utils;
+import dwt.graphics.Color;
+import dwt.graphics.Device;
+import dwt.graphics.Drawable;
+import dwt.graphics.GC;
+import dwt.graphics.GCData;
+import dwt.graphics.ImageData;
+import dwt.graphics.PaletteData;
+import dwt.graphics.RGB;
+import dwt.graphics.Rectangle;
+import dwt.graphics.Resource;
+import dwt.internal.cocoa.NSInteger;
+import objc = dwt.internal.objc.runtime;
  
 /**
  * Instances of this class are graphics which have been prepared
@@ -74,7 +91,7 @@
  * @see ImageData
  * @see ImageLoader
  */
-public final class Image : Resource , Drawable {
+public final class Image : Resource, Drawable {
 
     /**
      * specifies whether the receiver is a bitmap or an icon
@@ -227,7 +244,7 @@
     int width = cast(int)size.width;
     int height = cast(int)size.height;
     NSBitmapImageRep srcRep = srcImage.imageRep;
-    int bpr = srcRep.bytesPerRow();
+    NSInteger bpr = srcRep.bytesPerRow();
 
     /* Copy transparent pixel and alpha data when necessary */
     transparentPixel = srcImage.transparentPixel;
@@ -238,14 +255,14 @@
     }
 
     /* Create the image */
-    handle = cast(NSImage)new NSImage().alloc();
+    handle = cast(NSImage)(new NSImage()).alloc();
     handle = handle.initWithSize(size);
-    NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)new NSBitmapImageRep().alloc();
-    rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(0, width, height, srcRep.bitsPerSample(), srcRep.samplesPerPixel(), srcRep.samplesPerPixel() is 4, srcRep.isPlanar(), new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, srcRep.bytesPerRow(), srcRep.bitsPerPixel());
+    NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
+    rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(null, cast(NSInteger) width, cast(NSInteger) height, srcRep.bitsPerSample(), srcRep.samplesPerPixel(), srcRep.samplesPerPixel() is 4, srcRep.isPlanar(), new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, srcRep.bytesPerRow(), srcRep.bitsPerPixel());
     handle.addRepresentation(rep);
     
-    int data = rep.bitmapData();
-    OS.memmove(data, srcImage.imageRep.bitmapData(), width * height * 4);
+    objc.id data = rep.bitmapData();
+    OS.memmove(data, srcImage.imageRep.bitmapData(), cast(size_t) (width * height * 4));
     if (flag !is DWT.IMAGE_COPY) {
         
         /* Apply transformation */
@@ -263,7 +280,7 @@
                 byte oneBlue = cast(byte)oneRGB.blue;
                 byte[] line = new byte[bpr];
                 for (int y=0; y<height; y++) {
-                    OS.memmove(line, data + (y * bpr), bpr);
+                    OS.memmove(line.ptr, data + (y * bpr), bpr);
                     int offset = 0;
                     for (int x=0; x<width; x++) {
                         int red = line[offset+1] & 0xFF;
@@ -281,14 +298,14 @@
                         }
                         offset += 4;
                     }
-                    OS.memmove(data + (y * bpr), line, bpr);
+                    OS.memmove(data + (y * bpr), line.ptr, bpr);
                 }
                 break;
             }
             case DWT.IMAGE_GRAY: {          
                 byte[] line = new byte[bpr];
                 for (int y=0; y<height; y++) {
-                    OS.memmove(line, data + (y * bpr), bpr);
+                    OS.memmove(line.ptr, data + (y * bpr), bpr);
                     int offset = 0;
                     for (int x=0; x<width; x++) {
                         int red = line[offset+1] & 0xFF;
@@ -298,7 +315,7 @@
                         line[offset+1] = line[offset+2] = line[offset+3] = intensity;
                         offset += 4;
                     }
-                    OS.memmove(data + (y * bpr), line, bpr);
+                    OS.memmove(data + (y * bpr), line.ptr, bpr);
                 }
                 break;
             }
@@ -498,10 +515,10 @@
     if (transparentPixel is -1 && alpha is -1 && alphaData is null) return;
     NSSize size = handle.size();
     int height = cast(int)size.height;
-    int bpr = imageRep.bytesPerRow();
-    int dataSize = height * bpr;
+    NSInteger bpr = imageRep.bytesPerRow();
+    size_t dataSize = height * bpr;
     byte[] srcData = new byte[dataSize];
-    OS.memmove(srcData, imageRep.bitmapData(), dataSize);
+    OS.memmove(srcData.ptr, imageRep.bitmapData(), dataSize);
     if (transparentPixel !is -1) {
         for (int i=0; i<dataSize; i+=4) {
             int pixel = ((srcData[i+1] & 0xFF) << 16) | ((srcData[i+2] & 0xFF) << 8) | (srcData[i+3] & 0xFF);
@@ -523,7 +540,7 @@
             }
         }
     }
-    OS.memmove(imageRep.bitmapData(), srcData, dataSize);
+    OS.memmove(imageRep.bitmapData(), srcData.ptr, dataSize);
 }
 
 void destroy() {
@@ -545,7 +562,7 @@
  *
  * @see #hashCode
  */
-public bool equals (Object object) {
+public bool opEquals (Object object) {
     if (object is this) return true;
     if (!( null !is cast(Image)object )) return false;
     Image image = cast(Image)object;
@@ -553,6 +570,8 @@
         transparentPixel is image.transparentPixel;
 }
 
+alias opEquals equals;
+
 /**
  * Returns the color to which to map the transparent pixel, or null if
  * the receiver has no transparent pixel.
@@ -577,7 +596,7 @@
     int red = (transparentPixel >> 16) & 0xFF;
     int green = (transparentPixel >> 8) & 0xFF;
     int blue = (transparentPixel >> 0) & 0xFF;
-    return Color.cocoa_new(device, new float[]{red / 255f, green / 255f, blue / 255f, 1});
+    return Color.cocoa_new(device, new float[][red / 255f, green / 255f, blue / 255f, 1]);
 }
 
 /**
@@ -622,11 +641,11 @@
     int width = cast(int)size.width;
     int height = cast(int)size.height;
     NSBitmapImageRep imageRep = this.imageRep;
-    int bpr = imageRep.bytesPerRow();
-    int bpp = imageRep.bitsPerPixel();
-    int dataSize = height * bpr;
+    NSInteger  bpr = imageRep.bytesPerRow();
+    NSInteger  bpp = imageRep.bitsPerPixel();
+    size_t dataSize = height * bpr;
     byte[] srcData = new byte[dataSize];
-    OS.memmove(srcData, imageRep.bitmapData(), dataSize);
+    OS.memmove(srcData.ptr, imageRep.bitmapData(), dataSize);
     
     PaletteData palette = new PaletteData(0xFF0000, 0xFF00, 0xFF);
     ImageData data = new ImageData(width, height, bpp, palette);
@@ -688,7 +707,7 @@
     image.handle = nsImage;
     NSImageRep rep = nsImage.bestRepresentationForDevice(null);
     if (rep.isKindOfClass(NSBitmapImageRep.static_class())) { 
-        image.imageRep = new NSBitmapImageRep(rep.id);
+        image.imageRep = new NSBitmapImageRep(rep.id_);
     }
     return image;
 }
@@ -703,10 +722,12 @@
  *
  * @see #equals
  */
-public int hashCode () {
-    return handle !is null ? handle.id : 0;
+public hash_t toHash () {
+    return handle !is null ? handle.id_ : null;
 }
 
+alias toHash hashCode;
+
 void init(int width, int height) {
     if (width <= 0 || height <= 0) {
         DWT.error (DWT.ERROR_INVALID_ARGUMENT);
@@ -715,14 +736,14 @@
     this.width = width;
     this.height = height;
 
-    handle = cast(NSImage)new NSImage().alloc();
-    NSSize size = new NSSize();
+    handle = cast(NSImage)(new NSImage()).alloc();
+    NSSize size = NSSize();
     size.width = width;
     size.height = height;
     handle = handle.initWithSize(size);
-    NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)new NSBitmapImageRep().alloc();
-    rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(0, width, height, 8, 3, false, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, width * 4, 32);
-    OS.memset(rep.bitmapData(), 0xFF, width * height * 4);
+    NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
+    rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(0, cast(NSInteger) width, cast(NSInteger) height, 8, 3, false, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, cast(NSInteger) (width * 4), 32);
+    OS.memset(rep.bitmapData(), 0xFF, cast(size_t) (width * height * 4));
     handle.addRepresentation(rep);
 //  rep.release();
 }
@@ -737,7 +758,7 @@
                 DWT.error(DWT.ERROR_UNSUPPORTED_DEPTH);
     
     /* Create the image */
-    int dataSize = width * height * 4;
+    size_t dataSize = width * height * 4;
     
     /* Initialize data */
     int bpr = width * 4;
@@ -824,15 +845,15 @@
             }
         }
     }
-    handle = cast(NSImage)new NSImage().alloc();
-    NSSize size = new NSSize();
+    handle = cast(NSImage)(new NSImage()).alloc();
+    NSSize size = NSSize();
     size.width = width;
     size.height = height;
     handle = handle.initWithSize(size);
-    NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)new NSBitmapImageRep().alloc();
+    NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
     rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(
-            0, width, height, 8, hasAlpha ? 4 : 3, hasAlpha, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, bpr, 32);
-    OS.memmove(rep.bitmapData(), buffer, dataSize); 
+            null, cast(NSInteger) width, cast(NSInteger) height, 8, hasAlpha ? 4 : 3, hasAlpha, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, cast(NSInteger) bpr, 32);
+    OS.memmove(rep.bitmapData(), buffer.ptr, dataSize); 
     handle.addRepresentation(rep);
 //  rep.release();
 }
@@ -858,14 +879,14 @@
     NSGraphicsContext current = NSGraphicsContext.currentContext();
     NSBitmapImageRep rep = imageRep;
     if (imageRep.hasAlpha()) {
-        int bpr = width * 4;
-        rep = cast(NSBitmapImageRep)new NSBitmapImageRep().alloc();
+        NSInteger bpr = width * 4;
+        rep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
         int bitmapData = imageRep.bitmapData();
-        if (data.bitmapDataAddress !is 0) OS.free(data.bitmapDataAddress);
-        data.bitmapDataAddress = OS.malloc(4);
-        OS.memmove(data.bitmapDataAddress, new int[] {bitmapData}, 4);
+        if (data.bitmapDataAddress !is null) OS.free(data.bitmapDataAddress);
+        data.bitmapDataAddress = OS.malloc((void*).sizeof);
+        OS.memmove(data.bitmapDataAddress, (new int[] (bitmapData)).ptr, (void*).sizeof);
         rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(
-                data.bitmapDataAddress, width, height, 8, 3, false, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat , bpr, 32);
+                &data.bitmapDataAddress, cast(NSInteger) width, cast(NSInteger) height, 8, 3, false, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat , bpr, 32);
         rep.autorelease();
     }
     NSGraphicsContext context = NSGraphicsContext.graphicsContextWithBitmapImageRep(rep);
@@ -904,8 +925,8 @@
  * @param data the platform specific GC data 
  */
 public void internal_dispose_GC (int context, GCData data) {
-    if (data.bitmapDataAddress !is 0) OS.free(data.bitmapDataAddress);
-    data.bitmapDataAddress = 0;
+    if (data.bitmapDataAddress !is null) OS.free(data.bitmapDataAddress);
+    data.bitmapDataAddress = null;
 }
 
 /**
@@ -992,7 +1013,7 @@
  */
 public String toString () {
     if (isDisposed()) return "Image {*DISPOSED*}";
-    return "Image {" + handle + "}";
+    return Format("Image {{}{}" , handle , "}");
 }
 
 }