diff dwt/graphics/Image.d @ 53:0405e18fec7f

Gdiplus implemented - test build of dwt.lib successful; updated graphics package as necessary
author John Reimer <terminal.node@gmail.com
date Sun, 03 Feb 2008 15:48:14 -0800
parents 9a64a7781bab
children e10cbfc977d2
line wrap: on
line diff
--- a/dwt/graphics/Image.d	Sun Feb 03 05:06:44 2008 +0100
+++ b/dwt/graphics/Image.d	Sun Feb 03 15:48:14 2008 -0800
@@ -607,18 +607,18 @@
         auto bitmap = Gdip.Bitmap_new( .StrToWCHARz( filename ), false);
         if (bitmap !is null) {
             int error = DWT.ERROR_NO_HANDLES;
-            int status = Gdip.Image_GetLastStatus(cast(Gdip.Image*)bitmap);
+            int status = Gdip.Image_GetLastStatus(cast(Gdip.Image)bitmap);
             if (status is 0) {
                 if (filename.toLowerCase().endsWith(".ico")) {
                     this.type = DWT.ICON;
                     HICON hicon;
-                    Gdip.Bitmap_GetHICON(bitmap, &hicon);
+                    Gdip.Bitmap_GetHICON(bitmap, hicon);
                     this.handle = hicon;
                 } else {
                     this.type = DWT.BITMAP;
-                    int width = Gdip.Image_GetWidth(cast(Gdip.Image*)bitmap);
-                    int height = Gdip.Image_GetHeight(cast(Gdip.Image*)bitmap);
-                    int pixelFormat = Gdip.Image_GetPixelFormat(cast(Gdip.Image*)bitmap);
+                    int width = Gdip.Image_GetWidth(cast(Gdip.Image)bitmap);
+                    int height = Gdip.Image_GetHeight(cast(Gdip.Image)bitmap);
+                    int pixelFormat = Gdip.Image_GetPixelFormat(cast(Gdip.Image)bitmap);
                     switch (pixelFormat) {
                         case Gdip.PixelFormat16bppRGB555:
                         case Gdip.PixelFormat16bppRGB565:
@@ -650,7 +650,7 @@
                             Gdip.Rect rect;
                             rect.Width = width;
                             rect.Height = height;
-                            status = Gdip.Graphics_DrawImage(graphics, cast(Gdip.Image*)bitmap, &rect, 0, 0, width, height, Gdip.UnitPixel, null, null, null);
+                            status = Gdip.Graphics_DrawImage(graphics, cast(Gdip.Image)bitmap, rect, 0, 0, width, height, Gdip.UnitPixel, null, null, null);
                             if (status !is 0) {
                                 error = DWT.ERROR_INVALID_IMAGE;
                                 OS.DeleteObject(handle);
@@ -687,11 +687,11 @@
                                     case Gdip.PixelFormat1bppIndexed:
                                     case Gdip.PixelFormat4bppIndexed:
                                     case Gdip.PixelFormat8bppIndexed:
-                                        int paletteSize = Gdip.Image_GetPaletteSize(cast(Gdip.Image*)bitmap);
+                                        int paletteSize = Gdip.Image_GetPaletteSize(cast(Gdip.Image)bitmap);
                                         auto hHeap = OS.GetProcessHeap();
                                         auto palette = cast(Gdip.ColorPalette*) OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, paletteSize);
                                         if (palette is null) DWT.error(DWT.ERROR_NO_HANDLES);
-                                        Gdip.Image_GetPalette(cast(Gdip.Image*)bitmap, palette, paletteSize);
+                                        Gdip.Image_GetPalette(cast(Gdip.Image)bitmap, palette, paletteSize);
                                         Gdip.ColorPalette* colorPalette = palette;
                                         //Gdip.MoveMemory(colorPalette, palette, ColorPalette.sizeof);
                                         int[] entries = new int[colorPalette.Count];
@@ -864,6 +864,12 @@
     return hDib;
 }
 
+// FIXME: Potential crash site in D:  createGdipImage casts pointers to int before
+// returning them in an int[].  Since the D GC does not and cannot scan int's for
+// pointers, there is potential that the pointer's object could be collected while still
+// active, even though it might be unlikely given the short span of time that the
+// function has them stored in the int array.
+
 int[] createGdipImage() {
     switch (type) {
         case DWT.BITMAP: {
@@ -883,10 +889,10 @@
                 OS.GetObject(memDib, BITMAP.sizeof, &dibBM);
                 int sizeInBytes = dibBM.bmWidthBytes * dibBM.bmHeight;
                 OS.BitBlt(memHdc, 0, 0, imgWidth, imgHeight, srcHdc, 0, 0, OS.SRCCOPY);
-                byte red = 0, green = 0, blue = 0;
+                ubyte red = 0, green = 0, blue = 0;
                 if (transparentPixel !is -1) {
                     if (bm.bmBitsPixel <= 8)  {
-                        byte[] color = new byte[4];
+                        ubyte[] color = new ubyte[4];
                         OS.GetDIBColorTable(srcHdc, transparentPixel, 1, cast(RGBQUAD*)color.ptr);
                         blue = color[0];
                         green = color[1];
@@ -908,14 +914,14 @@
                                 red = reds[(transparentPixel & redMask) >> redShift];
                                 break;
                             case 24:
-                                blue = cast(byte)((transparentPixel & 0xFF0000) >> 16);
-                                green = cast(byte)((transparentPixel & 0xFF00) >> 8);
-                                red = cast(byte)(transparentPixel & 0xFF);
+                                blue = cast(ubyte)((transparentPixel & 0xFF0000) >> 16);
+                                green = cast(ubyte)((transparentPixel & 0xFF00) >> 8);
+                                red = cast(ubyte)(transparentPixel & 0xFF);
                                 break;
                             case 32:
-                                blue = cast(byte)((transparentPixel & 0xFF000000) >>> 24);
-                                green = cast(byte)((transparentPixel & 0xFF0000) >> 16);
-                                red = cast(byte)((transparentPixel & 0xFF00) >> 8);
+                                blue = cast(ubyte)((transparentPixel & 0xFF000000) >>> 24);
+                                green = cast(ubyte)((transparentPixel & 0xFF0000) >> 16);
+                                red = cast(ubyte)((transparentPixel & 0xFF00) >> 8);
                                 break;
                         }
                     }
@@ -924,13 +930,13 @@
                 OS.SelectObject(memHdc, oldMemBitmap);
                 OS.DeleteObject(srcHdc);
                 OS.DeleteObject(memHdc);
-                byte[] srcData = (cast(byte*)dibBM.bmBits)[ 0 .. sizeInBytes ].dup;
+                ubyte[] srcData = (cast(ubyte*)dibBM.bmBits)[ 0 .. sizeInBytes ].dup;
                 OS.DeleteObject(memDib);
                 device.internal_dispose_GC(hDC, null);
                 if (alpha !is -1) {
                     for (int y = 0, dp = 0; y < imgHeight; ++y) {
                         for (int x = 0; x < imgWidth; ++x) {
-                            srcData[dp + 3] = cast(byte)alpha;
+                            srcData[dp + 3] = cast(ubyte)alpha;
                             dp += 4;
                         }
                     }
@@ -945,19 +951,19 @@
                     for (int y = 0, dp = 0; y < imgHeight; ++y) {
                         for (int x = 0; x < imgWidth; ++x) {
                             if (srcData[dp] is blue && srcData[dp + 1] is green && srcData[dp + 2] is red) {
-                                srcData[dp + 3] = cast(byte)0;
+                                srcData[dp + 3] = cast(ubyte)0;
                             } else {
-                                srcData[dp + 3] = cast(byte)0xFF;
+                                srcData[dp + 3] = cast(ubyte)0xFF;
                             }
                             dp += 4;
                         }
                     }
                 }
                 auto hHeap = OS.GetProcessHeap();
-                auto pixels = cast(byte*)OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, srcData.length);
+                auto pixels = cast(ubyte*)OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, srcData.length);
                 if (pixels is null) DWT.error(DWT.ERROR_NO_HANDLES);
                 OS.MoveMemory(pixels, srcData.ptr, sizeInBytes);
-                return [ cast(int)Gdip.Bitmap_new(imgWidth, imgHeight, dibBM.bmWidthBytes, Gdip.PixelFormat32bppARGB, pixels), cast(int) pixels];
+                return [ cast(int)(Gdip.Bitmap_new(imgWidth, imgHeight, dibBM.bmWidthBytes, Gdip.PixelFormat32bppARGB, pixels)), cast(int) pixels];
             }
             return [cast(int)Gdip.Bitmap_new(handle, null), 0];
         }
@@ -979,8 +985,8 @@
             OS.GetObject(hBitmap, BITMAP.sizeof, &bm);
             int imgWidth = bm.bmWidth;
             int imgHeight = hBitmap is iconInfo.hbmMask ? bm.bmHeight / 2 : bm.bmHeight;
-            Gdip.Bitmap* img;
-            byte* pixels;
+            Gdip.Bitmap img;
+            ubyte* pixels;
             if (imgWidth > imgHeight) {
                 auto hDC = device.internal_new_GC(null);
                 auto srcHdc = OS.CreateCompatibleDC(hDC);
@@ -994,15 +1000,15 @@
                 OS.BitBlt(memHdc, 0, 0, imgWidth, imgHeight, srcHdc, 0, hBitmap is iconInfo.hbmMask ? imgHeight : 0, OS.SRCCOPY);
                 OS.SelectObject(memHdc, oldMemBitmap);
                 OS.DeleteObject(memHdc);
-                byte[] srcData = (cast(byte*)dibBM.bmBits)[ 0 .. dibBM.bmWidthBytes * dibBM.bmHeight].dup;
+                ubyte[] srcData = (cast(ubyte*)dibBM.bmBits)[ 0 .. dibBM.bmWidthBytes * dibBM.bmHeight].dup;
                 OS.DeleteObject(memDib);
                 OS.SelectObject(srcHdc, iconInfo.hbmMask);
                 for (int y = 0, dp = 0; y < imgHeight; ++y) {
                     for (int x = 0; x < imgWidth; ++x) {
                         if (OS.GetPixel(srcHdc, x, y) !is 0) {
-                            srcData[dp + 3] = cast(byte)0;
+                            srcData[dp + 3] = cast(ubyte)0;
                         } else {
-                            srcData[dp + 3] = cast(byte)0xFF;
+                            srcData[dp + 3] = cast(ubyte)0xFF;
                         }
                         dp += 4;
                     }
@@ -1011,7 +1017,7 @@
                 OS.DeleteObject(srcHdc);
                 device.internal_dispose_GC(hDC, null);
                 auto hHeap = OS.GetProcessHeap();
-                pixels = cast(byte*) OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, srcData.length);
+                pixels = cast(ubyte*) OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, srcData.length);
                 if (pixels is null) DWT.error(DWT.ERROR_NO_HANDLES);
                 pixels[ 0 .. srcData.length ] = srcData[];
                 img = Gdip.Bitmap_new(imgWidth, imgHeight, dibBM.bmWidthBytes, Gdip.PixelFormat32bppARGB, pixels);