?? image.java
字號:
if (OS.IsWinCE) SWT.error(SWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, info.hbmMask, 0, height, 0, bmi, OS.DIB_RGB_COLORS); OS.MoveMemory(bmiHeader, bmi, BITMAPINFOHEADER.sizeof); imageSize = bmiHeader.biSizeImage; maskData = new byte[imageSize]; int lpvMaskBits = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, imageSize); if (OS.IsWinCE) SWT.error(SWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, info.hbmMask, 0, height, lpvMaskBits, bmi, OS.DIB_RGB_COLORS); OS.MoveMemory(maskData, lpvMaskBits, imageSize); OS.HeapFree(hHeap, 0, lpvMaskBits); /* Loop to invert the mask */ for (int i = 0; i < maskData.length; i++) { maskData[i] ^= -1; } /* Make sure mask scanlinePad is 2 */ int maskPad; int bpl = imageSize / height; for (maskPad = 1; maskPad < 128; maskPad++) { int calcBpl = (((width + 7) / 8) + (maskPad - 1)) / maskPad * maskPad; if (calcBpl == bpl) break; } maskData = ImageData.convertPad(maskData, width, height, 1, maskPad, 2); } /* Clean up */ OS.HeapFree(hHeap, 0, lpvBits); OS.SelectObject(hBitmapDC, hOldBitmap); if (oldPalette != 0) { OS.SelectPalette(hBitmapDC, oldPalette, false); OS.RealizePalette(hBitmapDC); } OS.DeleteDC(hBitmapDC); /* Release the HDC for the device */ device.internal_dispose_GC(hDC, null); if (info.hbmColor != 0) OS.DeleteObject(info.hbmColor); if (info.hbmMask != 0) OS.DeleteObject(info.hbmMask); /* Construct and return the ImageData */ ImageData imageData = new ImageData(width, height, depth, palette, 4, data); imageData.maskData = maskData; imageData.maskPad = 2; return imageData; } case SWT.BITMAP: { /* Get the basic BITMAP information */ bm = new BITMAP(); OS.GetObject(handle, BITMAP.sizeof, bm); depth = bm.bmPlanes * bm.bmBitsPixel; width = bm.bmWidth; height = bm.bmHeight; /* Find out whether this is a DIB or a DDB. */ boolean isDib = (bm.bmBits != 0); /* Get the HDC for the device */ int hDC = device.internal_new_GC(null); /* * Feature in WinCE. GetDIBits is not available in WinCE. The * workaround is to create a temporary DIB from the DDB and use * the bmBits field of DIBSECTION to retrieve the image data. */ int handle = this.handle; if (OS.IsWinCE) { if (!isDib) { boolean mustRestore = false; if (memGC != null && !memGC.isDisposed()) { mustRestore = true; GCData data = memGC.data; if (data.hNullBitmap != 0) { OS.SelectObject(memGC.handle, data.hNullBitmap); data.hNullBitmap = 0; } } handle = createDIBFromDDB(hDC, this.handle, width, height); if (mustRestore) { int hOldBitmap = OS.SelectObject(memGC.handle, this.handle); memGC.data.hNullBitmap = hOldBitmap; } isDib = true; } } DIBSECTION dib = null; if (isDib) { dib = new DIBSECTION(); OS.GetObject(handle, DIBSECTION.sizeof, dib); } /* Calculate number of colors */ int numColors = 0; if (depth <= 8) { if (isDib) { numColors = dib.biClrUsed; } else { numColors = 1 << depth; } } /* Create the BITMAPINFO */ byte[] bmi = null; BITMAPINFOHEADER bmiHeader = null; if (!isDib) { bmiHeader = new BITMAPINFOHEADER(); bmiHeader.biSize = BITMAPINFOHEADER.sizeof; bmiHeader.biWidth = width; bmiHeader.biHeight = -height; bmiHeader.biPlanes = 1; bmiHeader.biBitCount = (short)depth; bmiHeader.biCompression = OS.BI_RGB; bmi = new byte[BITMAPINFOHEADER.sizeof + numColors * 4]; OS.MoveMemory(bmi, bmiHeader, BITMAPINFOHEADER.sizeof); } /* Create the DC and select the bitmap */ int hBitmapDC = OS.CreateCompatibleDC(hDC); int hOldBitmap = OS.SelectObject(hBitmapDC, handle); /* Select the palette if necessary */ int oldPalette = 0; if (!isDib && depth <= 8) { int hPalette = device.hPalette; if (hPalette != 0) { oldPalette = OS.SelectPalette(hBitmapDC, hPalette, false); OS.RealizePalette(hBitmapDC); } } /* Find the size of the image and allocate data */ int imageSize; if (isDib) { imageSize = dib.biSizeImage; } else { /* Call with null lpBits to get the image size */ if (OS.IsWinCE) SWT.error(SWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, handle, 0, height, 0, bmi, OS.DIB_RGB_COLORS); OS.MoveMemory(bmiHeader, bmi, BITMAPINFOHEADER.sizeof); imageSize = bmiHeader.biSizeImage; } byte[] data = new byte[imageSize]; /* Get the bitmap data */ if (isDib) { if (OS.IsWinCE && this.handle != handle) { /* get image data from the temporary DIB */ OS.MoveMemory(data, dib.bmBits, imageSize); } else { OS.MoveMemory(data, bm.bmBits, imageSize); } } else { int hHeap = OS.GetProcessHeap(); int lpvBits = OS.HeapAlloc(hHeap, OS.HEAP_ZERO_MEMORY, imageSize); if (OS.IsWinCE) SWT.error(SWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, handle, 0, height, lpvBits, bmi, OS.DIB_RGB_COLORS); OS.MoveMemory(data, lpvBits, imageSize); OS.HeapFree(hHeap, 0, lpvBits); } /* Calculate the palette */ PaletteData palette = null; if (depth <= 8) { RGB[] rgbs = new RGB[numColors]; if (isDib) { if (OS.IsWinCE) { /* * Feature on WinCE. GetDIBColorTable is not supported. * The workaround is to set a pixel to the desired * palette index and use getPixel to get the corresponding * RGB value. */ int red = 0, green = 0, blue = 0; byte[] pBits = new byte[1]; OS.MoveMemory(pBits, bm.bmBits, 1); byte oldValue = pBits[0]; int mask = (0xFF << (8 - bm.bmBitsPixel)) & 0x00FF; for (int i = 0; i < numColors; i++) { pBits[0] = (byte)((i << (8 - bm.bmBitsPixel)) | (pBits[0] & ~mask)); OS.MoveMemory(bm.bmBits, pBits, 1); int color = OS.GetPixel(hBitmapDC, 0, 0); blue = (color & 0xFF0000) >> 16; green = (color & 0xFF00) >> 8; red = color & 0xFF; rgbs[i] = new RGB(red, green, blue); } pBits[0] = oldValue; OS.MoveMemory(bm.bmBits, pBits, 1); } else { byte[] colors = new byte[numColors * 4]; OS.GetDIBColorTable(hBitmapDC, 0, numColors, colors); int colorIndex = 0; for (int i = 0; i < rgbs.length; i++) { rgbs[i] = new RGB(colors[colorIndex + 2] & 0xFF, colors[colorIndex + 1] & 0xFF, colors[colorIndex] & 0xFF); colorIndex += 4; } } } else { int srcIndex = BITMAPINFOHEADER.sizeof; for (int i = 0; i < numColors; i++) { rgbs[i] = new RGB(bmi[srcIndex + 2] & 0xFF, bmi[srcIndex + 1] & 0xFF, bmi[srcIndex] & 0xFF); srcIndex += 4; } } palette = new PaletteData(rgbs); } else if (depth == 16) { palette = new PaletteData(0x7C00, 0x3E0, 0x1F); } else if (depth == 24) { palette = new PaletteData(0xFF, 0xFF00, 0xFF0000); } else if (depth == 32) { palette = new PaletteData(0xFF00, 0xFF0000, 0xFF000000); } else { SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); } /* Clean up */ OS.SelectObject(hBitmapDC, hOldBitmap); if (oldPalette != 0) { OS.SelectPalette(hBitmapDC, oldPalette, false); OS.RealizePalette(hBitmapDC); } if (OS.IsWinCE) { if (handle != this.handle) { /* free temporary DIB */ OS.DeleteObject (handle); } } OS.DeleteDC(hBitmapDC); /* Release the HDC for the device */ device.internal_dispose_GC(hDC, null); /* Construct and return the ImageData */ ImageData imageData = new ImageData(width, height, depth, palette, 4, data); imageData.transparentPixel = this.transparentPixel; imageData.alpha = alpha; if (alpha == -1 && alphaData != null) { imageData.alphaData = new byte[alphaData.length]; System.arraycopy(alphaData, 0, imageData.alphaData, 0, alphaData.length); } return imageData; } default: SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); return null; }}/** * Returns an integer hash code for the receiver. Any two * objects which return <code>true</code> when passed to * <code>equals</code> must return the same value for this * method. * * @return the receiver's hash * * @see #equals */public int hashCode () { return handle;}void init(Device device, int width, int height) { if (width <= 0 || height <= 0) { SWT.error (SWT.ERROR_INVALID_ARGUMENT); } this.device = device; type = SWT.BITMAP; int hDC = device.internal_new_GC(null); handle = OS.CreateCompatibleBitmap(hDC, width, height); if (handle != 0) { int memDC = OS.CreateCompatibleDC(hDC); int hOldBitmap = OS.SelectObject(memDC, handle); OS.PatBlt(memDC, 0, 0, width, height, OS.PATCOPY); OS.SelectObject(memDC, hOldBitmap); OS.DeleteDC(memDC); } device.internal_dispose_GC(hDC, null); if (handle == 0) { SWT.error(SWT.ERROR_NO_HANDLES, null, device.getLastError()); }}/** * Feature in WinCE. GetIconInfo is not available in WinCE. * The workaround is to cache the object ImageData for images * of type SWT.ICON. The bitmaps hbmMask and hbmColor can then * be reconstructed by using our version of getIconInfo. * This function takes an ICONINFO object and sets the fields * hbmMask and hbmColor with the corresponding bitmaps it has * created. * Note. These bitmaps must be freed - as they would have to be * if the regular GetIconInfo had been used. */static void GetIconInfo(Image image, ICONINFO info) { int[] result = init(image.device, null, image.data); info.hbmColor = result[0]; info.hbmMask = result[1];}static int[] init(Device device, Image image, ImageData i) { if (image != null) image.device = device; /* * BUG in Windows 98: * A monochrome DIBSection will display as solid black * on Windows 98 machines, even though it contains the * correct data. The fix is to convert 1-bit ImageData * into 4-bit ImageData before creating the image. */ /* Windows does not support 2-bit images. Convert to 4-bit image. */ if ((i.depth == 1 && i.getTransparencyType() != SWT.TRANSPARENCY_MASK) || i.depth == 2) { ImageData img = new ImageData(i.width, i.height, 4, i.palette); ImageData.blit(ImageData.BLIT_SRC, i.data, i.depth, i.bytesPerLine, i.getByteOrder(), 0, 0, i.width, i.height, null, null, null, ImageData.ALPHA_OPAQUE, null, 0, 0, 0, img.data, img.depth, img.bytesPerLine, i.getByteOrder(), 0, 0, img.width, img.height, null, null, null, false, false); img.transparentPixel = i.transparentPixel; img.maskPad = i.maskPad; img.maskData = i.maskData; img.alpha = i.alpha; img.alphaData = i.alphaData; i = img; } /* * Windows supports 16-bit mask of (0x7C00, 0x3E0, 0x1F), * 24-bit mask of (0xFF0000, 0xFF00, 0xFF) and 32-bit mask * (0x00FF0000, 0x0000FF00, 0x000000FF) as documented in * MSDN BITMAPINFOHEADER. Make sure the image is * Windows-supported. */ /* * Note on WinCE. CreateDIBSection requires the biCompression * field of the BITMAPINFOHEADER to be set to BI_BITFIELDS for * 16 and 32 bit direct images (see MSDN for CreateDIBSection). * In this case, the color mask can be set to any value. For * consistency, it is set to the same mask used by non WinCE * platforms in BI_RGB mode. */ if (i.palette.isDirect) { final PaletteData palette = i.palette; final int redMask = palette.redMask; final int greenMask = palette.greenMask; final int blueMask = palette.blueMask; int newDepth = i.depth; int newOrder = ImageData.MSB_FIRST; PaletteData newPalette = null; switch (i.depth) { case 8: newDepth = 16; newOrder = ImageData.LSB_FIRST; newPalette = new PaletteData(0x7C00, 0x3E0, 0x1F); break; case 16: newOrder = ImageData.LSB_FIRST; if (!(redMask == 0x7C00 && greenMask == 0x3E0 && blueMask == 0x1F)) { newPalette = new PaletteData(0x7C00, 0x3E0, 0x1F); } break; case 24: if (!(redMask == 0xFF && greenMask == 0xFF00 && blueMask == 0xFF0000)) { newPalette = new PaletteData(0xFF, 0xFF00, 0xFF0000); } break; case 32: if (!(redMask == 0xFF00 && greenMask == 0xFF0000 && blueMask == 0xFF000000)) { newPalette = new PaletteData(0xFF00, 0xFF0000, 0xFF000000); } break; default: SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); } if (newPalette != null) { ImageData img = new ImageData(i.width, i.height, newDepth, newPalette); ImageData.blit(ImageData.BLIT_SRC, i.data, i.depth, i.bytesPerLine, i.getByteOrder(), 0, 0, i.width, i.height, redMask, greenMask, blueMask,
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -