Mercurial > projects > dwt-win
changeset 95:04d34191e2e9
Fix: MoveMemory, Images are now colored
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Feb 2008 02:08:50 +0100 |
parents | e37415f1e763 |
children | aee1890f38c3 |
files | dwt/graphics/Image.d |
diffstat | 1 files changed, 40 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/dwt/graphics/Image.d Wed Feb 06 23:59:02 2008 +0100 +++ b/dwt/graphics/Image.d Thu Feb 07 02:08:50 2008 +0100 @@ -696,11 +696,14 @@ 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]; + //int[] entries = new int[colorPalette.Count]; //OS.MoveMemory(entries, palette + 8, entries.length * 4); + + //PORTING_COMMENT: moved down + //OS.HeapFree(hHeap, 0, palette); RGB[] rgbs = new RGB[colorPalette.Count]; paletteData = new PaletteData(rgbs); - for (int i = 0; i < entries.length; i++) { + for (int i = 0; i < colorPalette.Count; i++) { if (((palette.Entries[i] >> 24) & 0xFF) is 0 && (colorPalette.Flags & Gdip.PaletteFlagsHasAlpha) !is 0) { transparentPixel = i; } @@ -716,8 +719,8 @@ case Gdip.PixelFormat32bppARGB: paletteData = new PaletteData(0xFF00, 0xFF0000, 0xFF000000); break; default: } - byte[] data = (cast(byte*)data)[ 0 .. stride * height], alphaData = null; - //OS.MoveMemory(data, pixels, data.length); + byte[] data = new byte[ stride * height ], alphaData = null; + OS.MoveMemory(data.ptr, pixels, data.length); switch (lockedBitmapData.PixelFormat) { case Gdip.PixelFormat16bppARGB1555: alphaData = new byte[width * height]; @@ -791,7 +794,7 @@ byte[] bmi; if (isDirect) bmi = new byte[BITMAPINFOHEADER.sizeof + (useBitfields ? 12 : 0)]; else bmi = new byte[BITMAPINFOHEADER.sizeof + rgbs.length * 4]; - *cast(BITMAPINFOHEADER*)bmi.ptr = bmiHeader; + OS.MoveMemory(bmi.ptr, &bmiHeader, BITMAPINFOHEADER.sizeof); /* Set the rgb colors into the bitmap info */ int offset = BITMAPINFOHEADER.sizeof; @@ -935,7 +938,8 @@ OS.SelectObject(memHdc, oldMemBitmap); OS.DeleteObject(srcHdc); OS.DeleteObject(memHdc); - ubyte[] srcData = (cast(ubyte*)dibBM.bmBits)[ 0 .. sizeInBytes ].dup; + ubyte[] srcData = new ubyte[sizeInBytes]; + OS.MoveMemory(srcData.ptr, dibBM.bmBits, sizeInBytes); OS.DeleteObject(memDib); device.internal_dispose_GC(hDC, null); if (alpha !is -1) { @@ -1005,7 +1009,8 @@ OS.BitBlt(memHdc, 0, 0, imgWidth, imgHeight, srcHdc, 0, hBitmap is iconInfo.hbmMask ? imgHeight : 0, OS.SRCCOPY); OS.SelectObject(memHdc, oldMemBitmap); OS.DeleteObject(memHdc); - ubyte[] srcData = (cast(ubyte*)dibBM.bmBits)[ 0 .. dibBM.bmWidthBytes * dibBM.bmHeight].dup; + ubyte[] srcData = new ubyte[dibBM.bmWidthBytes * dibBM.bmHeight]; + OS.MoveMemory(srcData.ptr, dibBM.bmBits, srcData.length); OS.DeleteObject(memDib); OS.SelectObject(srcHdc, iconInfo.hbmMask); for (int y = 0, dp = 0; y < imgHeight; ++y) { @@ -1024,7 +1029,7 @@ auto hHeap = OS.GetProcessHeap(); 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[]; + OS.MoveMemory(pixels, srcData.ptr, srcData.length); img = Gdip.Bitmap_new(imgWidth, imgHeight, dibBM.bmWidthBytes, Gdip.PixelFormat32bppARGB, pixels); } else { img = Gdip.Bitmap_new(handle); @@ -1109,15 +1114,15 @@ int red = 0, green = 0, blue = 0; if (bm.bmBitsPixel <= 8) { if (OS.IsWinCE) { - byte* pBits = cast(byte*)bm.bmBits; - //OS.MoveMemory(pBits, bm.bmBits, 1); + byte[1] pBits; + OS.MoveMemory(pBits.ptr, bm.bmBits, 1); byte oldValue = pBits[0]; int mask = (0xFF << (8 - bm.bmBitsPixel)) & 0x00FF; pBits[0] = cast(byte)((transparentPixel << (8 - bm.bmBitsPixel)) | (pBits[0] & ~mask)); - //OS.MoveMemory(bm.bmBits, bm.bmBits, 1); + OS.MoveMemory(bm.bmBits, pBits.ptr, 1); int color = OS.GetPixel(hdcMem, 0, 0); pBits[0] = oldValue; - //OS.MoveMemory(bm.bmBits, bm.bmBits, 1); + OS.MoveMemory(bm.bmBits, pBits.ptr, 1); blue = (color & 0xFF0000) >> 16; green = (color & 0xFF00) >> 8; red = color & 0xFF; @@ -1243,7 +1248,7 @@ bmiHeader.biBitCount = cast(short)depth; bmiHeader.biCompression = OS.BI_RGB; byte[] bmi = new byte[BITMAPINFOHEADER.sizeof + numColors * 4]; - *cast(BITMAPINFOHEADER*)bmi.ptr = bmiHeader; + OS.MoveMemory(bmi.ptr, &bmiHeader, BITMAPINFOHEADER.sizeof); /* Get the HDC for the device */ auto hDC = device.internal_new_GC(null); @@ -1265,15 +1270,16 @@ /* Call with null lpBits to get the image size */ if (OS.IsWinCE) DWT.error(DWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, hBitmap, 0, height, null, cast(BITMAPINFO*)bmi.ptr, OS.DIB_RGB_COLORS); - bmiHeader = *cast(BITMAPINFOHEADER*)bmi.ptr; + OS.MoveMemory(&bmiHeader, bmi.ptr, BITMAPINFOHEADER.sizeof); imageSize = bmiHeader.biSizeImage; + byte[] data = new byte[imageSize]; /* Get the bitmap data */ auto hHeap = OS.GetProcessHeap(); auto lpvBits = cast(byte*) OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, imageSize); if (lpvBits is null) DWT.error(DWT.ERROR_NO_HANDLES); if (OS.IsWinCE) DWT.error(DWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, hBitmap, 0, height, lpvBits, cast(BITMAPINFO*)bmi.ptr, OS.DIB_RGB_COLORS); - byte[] data = lpvBits[ 0 .. imageSize].dup; /*PORTING_OPTIMIZE?*/ + OS.MoveMemory(data.ptr, lpvBits, imageSize); /* Calculate the palette */ PaletteData palette = null; if (depth <= 8) { @@ -1300,7 +1306,7 @@ /* Do the bottom half of the mask */ if (OS.IsWinCE) DWT.error(DWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, hBitmap, height, height, lpvBits, cast(BITMAPINFO*)bmi.ptr, OS.DIB_RGB_COLORS); - maskData = lpvBits[ 0 .. imageSize].dup; + OS.MoveMemory(maskData.ptr, lpvBits, imageSize); } else { /* Do the entire mask */ /* Create the BITMAPINFO */ @@ -1312,7 +1318,7 @@ bmiHeader.biBitCount = 1; bmiHeader.biCompression = OS.BI_RGB; bmi = new byte[BITMAPINFOHEADER.sizeof + 8]; - *cast(BITMAPINFOHEADER*)bmi.ptr = bmiHeader; + OS.MoveMemory(bmi.ptr, &bmiHeader, BITMAPINFOHEADER.sizeof); /* First color black, second color white */ int offset = BITMAPINFOHEADER.sizeof; @@ -1322,14 +1328,14 @@ /* Call with null lpBits to get the image size */ if (OS.IsWinCE) DWT.error(DWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, info.hbmMask, 0, height, null, cast(BITMAPINFO*)bmi.ptr, OS.DIB_RGB_COLORS); - bmiHeader = *cast(BITMAPINFOHEADER*)bmi.ptr; + OS.MoveMemory(&bmiHeader, bmi.ptr, BITMAPINFOHEADER.sizeof); imageSize = bmiHeader.biSizeImage; maskData = new byte[imageSize]; auto lpvMaskBits = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, imageSize); if (lpvMaskBits is null) DWT.error(DWT.ERROR_NO_HANDLES); if (OS.IsWinCE) DWT.error(DWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, info.hbmMask, 0, height, lpvMaskBits, cast(BITMAPINFO*)bmi.ptr, OS.DIB_RGB_COLORS); - maskData[] = (cast(byte*)lpvMaskBits)[ 0 .. imageSize ]; + OS.MoveMemory(maskData.ptr, lpvMaskBits, imageSize); OS.HeapFree(hHeap, 0, lpvMaskBits); /* Loop to invert the mask */ for (int i = 0; i < maskData.length; i++) { @@ -1426,7 +1432,7 @@ bmiHeader.biBitCount = cast(short)depth; bmiHeader.biCompression = OS.BI_RGB; bmi = new byte[BITMAPINFOHEADER.sizeof + numColors * 4]; - *cast(BITMAPINFOHEADER*)bmi.ptr = bmiHeader; + OS.MoveMemory(bmi.ptr, &bmiHeader, BITMAPINFOHEADER.sizeof); } /* Create the DC and select the bitmap */ @@ -1449,7 +1455,7 @@ /* Call with null lpBits to get the image size */ if (OS.IsWinCE) DWT.error(DWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, handle, 0, height, null, cast(BITMAPINFO*)bmi.ptr, OS.DIB_RGB_COLORS); - bmiHeader = *cast(BITMAPINFOHEADER*)bmi.ptr; + OS.MoveMemory(&bmiHeader, bmi.ptr, BITMAPINFOHEADER.sizeof); imageSize = bmiHeader.biSizeImage; } byte[] data = new byte[imageSize]; @@ -1457,9 +1463,9 @@ if (isDib) { if (OS.IsWinCE && this.handle !is handle) { /* get image data from the temporary DIB */ - data[] = (cast(byte*)dib.bmBits)[ 0 .. imageSize ]; + OS.MoveMemory(data.ptr, dib.bmBits, imageSize); } else { - data[] = (cast(byte*)bm.bmBits)[ 0 .. imageSize ]; + OS.MoveMemory(data.ptr, bm.bmBits, imageSize); } } else { auto hHeap = OS.GetProcessHeap(); @@ -1467,7 +1473,7 @@ if (lpvBits is null) DWT.error(DWT.ERROR_NO_HANDLES); if (OS.IsWinCE) DWT.error(DWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, handle, 0, height, lpvBits, cast(BITMAPINFO*)bmi.ptr, OS.DIB_RGB_COLORS); - data[] = (cast(byte*)lpvBits)[ 0 .. imageSize ]; + OS.MoveMemory(data.ptr, lpvBits, imageSize); OS.HeapFree(hHeap, 0, lpvBits); } /* Calculate the palette */ @@ -1483,12 +1489,13 @@ * RGB value. */ int red = 0, green = 0, blue = 0; - byte* pBits = cast(byte*)bm.bmBits; + byte[1] pBits; + OS.MoveMemory(pBits.ptr, bm.bmBits, 1); byte oldValue = pBits[0]; int mask = (0xFF << (8 - bm.bmBitsPixel)) & 0x00FF; for (int i = 0; i < numColors; i++) { pBits[0] = cast(byte)((i << (8 - bm.bmBitsPixel)) | (pBits[0] & ~mask)); - //OS.MoveMemory(bm.bmBits, pBits, 1); + OS.MoveMemory(bm.bmBits, pBits.ptr, 1); int color = OS.GetPixel(hBitmapDC, 0, 0); blue = (color & 0xFF0000) >> 16; green = (color & 0xFF00) >> 8; @@ -1496,7 +1503,7 @@ rgbs[i] = new RGB(red, green, blue); } pBits[0] = oldValue; - //OS.MoveMemory(bm.bmBits, pBits, 1); + OS.MoveMemory(bm.bmBits, pBits.ptr, 1); } else { byte[] colors = new byte[numColors * 4]; OS.GetDIBColorTable(hBitmapDC, 0, numColors, cast(RGBQUAD*)colors.ptr); @@ -1613,7 +1620,7 @@ if (OS.IsWinCE) bmiHeader.biCompression = OS.BI_BITFIELDS; else bmiHeader.biCompression = OS.BI_RGB; byte[] bmi = new byte[BITMAPINFOHEADER.sizeof + (OS.IsWinCE ? 12 : 0)]; - *cast(BITMAPINFOHEADER*)bmi.ptr = bmiHeader; + OS.MoveMemory(bmi.ptr, &bmiHeader, BITMAPINFOHEADER.sizeof); /* Set the rgb colors into the bitmap info */ if (OS.IsWinCE) { int redMask = 0xFF00; @@ -1635,8 +1642,8 @@ bmi[offset + 11] = cast(byte)((blueMask & 0xFF) >> 0); } - int pBits; - return OS.CreateDIBSection(null, cast(BITMAPINFO*)bmi.ptr, OS.DIB_RGB_COLORS, &pBits, null, 0); + int[1] pBits; + return OS.CreateDIBSection(null, cast(BITMAPINFO*)bmi.ptr, OS.DIB_RGB_COLORS, pBits.ptr, null, 0); } /** @@ -1764,7 +1771,7 @@ bmi = new byte[BITMAPINFOHEADER.sizeof + (useBitfields ? 12 : 0)]; else bmi = new byte[BITMAPINFOHEADER.sizeof + rgbs.length * 4]; - *cast(BITMAPINFOHEADER*)bmi.ptr = bmiHeader; + OS.MoveMemory(bmi.ptr, &bmiHeader, BITMAPINFOHEADER.sizeof); /* Set the rgb colors into the bitmap info */ int offset = BITMAPINFOHEADER.sizeof; if (i.palette.isDirect) { @@ -1814,7 +1821,7 @@ offset += 4; } } - int pBits; + void* pBits; auto hDib = OS.CreateDIBSection(null, cast(BITMAPINFO*)bmi.ptr, OS.DIB_RGB_COLORS, &pBits, null, 0); if (hDib is null) DWT.error(DWT.ERROR_NO_HANDLES); /* In case of a scanline pad other than 4, do the work to convert it */ @@ -1822,7 +1829,7 @@ if (i.scanlinePad !is 4 && (i.bytesPerLine % 4 !is 0)) { data = ImageData.convertPad(data, i.width, i.height, i.depth, i.scanlinePad, 4); } - //OS.MoveMemory(pBits[0], data, data.length); + OS.MoveMemory(pBits, data.ptr, data.length); int[] result = null; if (i.getTransparencyType() is DWT.TRANSPARENCY_MASK) {