?? image.java
字號:
bmiHeader.biHeight = -height; bmiHeader.biPlanes = 1; bmiHeader.biBitCount = (short)depth; if (useBitfields) bmiHeader.biCompression = OS.BI_BITFIELDS; else bmiHeader.biCompression = OS.BI_RGB; byte[] bmi; if (isDirect) bmi = new byte[BITMAPINFOHEADER.sizeof + (useBitfields ? 12 : 0)]; else bmi = new byte[BITMAPINFOHEADER.sizeof + rgbs.length * 4]; OS.MoveMemory(bmi, bmiHeader, BITMAPINFOHEADER.sizeof); /* Set the rgb colors into the bitmap info */ int offset = BITMAPINFOHEADER.sizeof; if (isDirect) { if (useBitfields) { int redMask = 0; int greenMask = 0; int blueMask = 0; switch (depth) { case 16: redMask = 0x7C00; greenMask = 0x3E0; blueMask = 0x1F; /* little endian */ bmi[offset] = (byte)((redMask & 0xFF) >> 0); bmi[offset + 1] = (byte)((redMask & 0xFF00) >> 8); bmi[offset + 2] = (byte)((redMask & 0xFF0000) >> 16); bmi[offset + 3] = (byte)((redMask & 0xFF000000) >> 24); bmi[offset + 4] = (byte)((greenMask & 0xFF) >> 0); bmi[offset + 5] = (byte)((greenMask & 0xFF00) >> 8); bmi[offset + 6] = (byte)((greenMask & 0xFF0000) >> 16); bmi[offset + 7] = (byte)((greenMask & 0xFF000000) >> 24); bmi[offset + 8] = (byte)((blueMask & 0xFF) >> 0); bmi[offset + 9] = (byte)((blueMask & 0xFF00) >> 8); bmi[offset + 10] = (byte)((blueMask & 0xFF0000) >> 16); bmi[offset + 11] = (byte)((blueMask & 0xFF000000) >> 24); break; case 32: redMask = 0xFF00; greenMask = 0xFF0000; blueMask = 0xFF000000; /* big endian */ bmi[offset] = (byte)((redMask & 0xFF000000) >> 24); bmi[offset + 1] = (byte)((redMask & 0xFF0000) >> 16); bmi[offset + 2] = (byte)((redMask & 0xFF00) >> 8); bmi[offset + 3] = (byte)((redMask & 0xFF) >> 0); bmi[offset + 4] = (byte)((greenMask & 0xFF000000) >> 24); bmi[offset + 5] = (byte)((greenMask & 0xFF0000) >> 16); bmi[offset + 6] = (byte)((greenMask & 0xFF00) >> 8); bmi[offset + 7] = (byte)((greenMask & 0xFF) >> 0); bmi[offset + 8] = (byte)((blueMask & 0xFF000000) >> 24); bmi[offset + 9] = (byte)((blueMask & 0xFF0000) >> 16); bmi[offset + 10] = (byte)((blueMask & 0xFF00) >> 8); bmi[offset + 11] = (byte)((blueMask & 0xFF) >> 0); break; default: SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); } } } else { for (int j = 0; j < rgbs.length; j++) { bmi[offset] = (byte)rgbs[j].blue; bmi[offset + 1] = (byte)rgbs[j].green; bmi[offset + 2] = (byte)rgbs[j].red; bmi[offset + 3] = 0; offset += 4; } } int[] pBits = new int[1]; int hDib = OS.CreateDIBSection(0, bmi, OS.DIB_RGB_COLORS, pBits, 0, 0); if (hDib == 0) SWT.error(SWT.ERROR_NO_HANDLES); /* Bitblt DDB into DIB */ int hdcSource = OS.CreateCompatibleDC(hDC); int hdcDest = OS.CreateCompatibleDC(hDC); int hOldSrc = OS.SelectObject(hdcSource, hBitmap); int hOldDest = OS.SelectObject(hdcDest, hDib); OS.BitBlt(hdcDest, 0, 0, width, height, hdcSource, 0, 0, OS.SRCCOPY); OS.SelectObject(hdcSource, hOldSrc); OS.SelectObject(hdcDest, hOldDest); OS.DeleteDC(hdcSource); OS.DeleteDC(hdcDest); return hDib;}/** * Disposes of the operating system resources associated with * the image. Applications must dispose of all images which * they allocate. */public void dispose () { if (handle == 0) return; if (device.isDisposed()) return; if (memGC != null) memGC.dispose(); if (type == SWT.ICON) { if (OS.IsWinCE) data = null; OS.DestroyIcon (handle); } else { OS.DeleteObject (handle); } handle = 0; memGC = null; if (device.tracking) device.dispose_Object(this); device = null;}/** * Compares the argument to the receiver, and returns true * if they represent the <em>same</em> object using a class * specific comparison. * * @param object the object to compare with this object * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise * * @see #hashCode */public boolean equals (Object object) { if (object == this) return true; if (!(object instanceof Image)) return false; Image image = (Image) object; return device == image.device && handle == image.handle;}/** * Returns the color to which to map the transparent pixel, or null if * the receiver has no transparent pixel. * <p> * There are certain uses of Images that do not support transparency * (for example, setting an image into a button or label). In these cases, * it may be desired to simulate transparency by using the background * color of the widget to paint the transparent pixels of the image. * in place of transparency. This value may be set with setBackground(). * <p> * * @return the background color of the image, or null if there is no transparency in the image * * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> */public Color getBackground() { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); if (transparentPixel == -1) return null; /* Get the HDC for the device */ int hDC = device.internal_new_GC(null); /* Compute the background color */ BITMAP bm = new BITMAP(); OS.GetObject(handle, BITMAP.sizeof, bm); int hdcMem = OS.CreateCompatibleDC(hDC); int hOldObject = OS.SelectObject(hdcMem, handle); int red = 0, green = 0, blue = 0; if (bm.bmBitsPixel <= 8) { if (OS.IsWinCE) { byte[] pBits = new byte[1]; OS.MoveMemory(pBits, bm.bmBits, 1); byte oldValue = pBits[0]; int mask = (0xFF << (8 - bm.bmBitsPixel)) & 0x00FF; pBits[0] = (byte)((transparentPixel << (8 - bm.bmBitsPixel)) | (pBits[0] & ~mask)); OS.MoveMemory(bm.bmBits, pBits, 1); int color = OS.GetPixel(hdcMem, 0, 0); pBits[0] = oldValue; OS.MoveMemory(bm.bmBits, pBits, 1); blue = (color & 0xFF0000) >> 16; green = (color & 0xFF00) >> 8; red = color & 0xFF; } else { byte[] color = new byte[4]; OS.GetDIBColorTable(hdcMem, transparentPixel, 1, color); blue = color[0] & 0xFF; green = color[1] & 0xFF; red = color[2] & 0xFF; } } else { switch (bm.bmBitsPixel) { case 16: blue = (transparentPixel & 0x1F) << 3; green = (transparentPixel & 0x3E0) >> 2; red = (transparentPixel & 0x7C00) >> 7; break; case 24: blue = (transparentPixel & 0xFF0000) >> 16; green = (transparentPixel & 0xFF00) >> 8; red = transparentPixel & 0xFF; break; case 32: blue = (transparentPixel & 0xFF000000) >>> 24; green = (transparentPixel & 0xFF0000) >> 16; red = (transparentPixel & 0xFF00) >> 8; break; default: return null; } } OS.SelectObject(hdcMem, hOldObject); OS.DeleteDC(hdcMem); /* Release the HDC for the device */ device.internal_dispose_GC(hDC, null); return Color.win32_new(device, 0x02000000 | (blue << 16) | (green << 8) | red);}/** * Returns the bounds of the receiver. The rectangle will always * have x and y values of 0, and the width and height of the * image. * * @return a rectangle specifying the image's bounds * * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_INVALID_IMAGE - if the image is not a bitmap or an icon</li> * </ul> */public Rectangle getBounds() { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); switch (type) { case SWT.BITMAP: BITMAP bm = new BITMAP(); OS.GetObject(handle, BITMAP.sizeof, bm); return new Rectangle(0, 0, bm.bmWidth, bm.bmHeight); case SWT.ICON: if (OS.IsWinCE) { return new Rectangle(0, 0, data.width, data.height); } else { ICONINFO info = new ICONINFO(); OS.GetIconInfo(handle, info); int hBitmap = info.hbmColor; if (hBitmap == 0) hBitmap = info.hbmMask; bm = new BITMAP(); OS.GetObject(hBitmap, BITMAP.sizeof, bm); if (hBitmap == info.hbmMask) bm.bmHeight /= 2; if (info.hbmColor != 0) OS.DeleteObject(info.hbmColor); if (info.hbmMask != 0) OS.DeleteObject(info.hbmMask); return new Rectangle(0, 0, bm.bmWidth, bm.bmHeight); } default: SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); return null; }}/** * Returns an <code>ImageData</code> based on the receiver * Modifications made to this <code>ImageData</code> will not * affect the Image. * * @return an <code>ImageData</code> containing the image's data and attributes * * @exception SWTException <ul> * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_INVALID_IMAGE - if the image is not a bitmap or an icon</li> * </ul> * * @see ImageData */public ImageData getImageData() { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); BITMAP bm; int depth, width, height; switch (type) { case SWT.ICON: { if (OS.IsWinCE) return data; ICONINFO info = new ICONINFO(); if (OS.IsWinCE) SWT.error(SWT.ERROR_NOT_IMPLEMENTED); OS.GetIconInfo(handle, info); /* Get the basic BITMAP information */ int hBitmap = info.hbmColor; if (hBitmap == 0) hBitmap = info.hbmMask; bm = new BITMAP(); OS.GetObject(hBitmap, BITMAP.sizeof, bm); depth = bm.bmPlanes * bm.bmBitsPixel; width = bm.bmWidth; if (hBitmap == info.hbmMask) bm.bmHeight /= 2; height = bm.bmHeight; int numColors = 0; if (depth <= 8) numColors = 1 << depth; /* Create the BITMAPINFO */ BITMAPINFOHEADER 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; byte[] bmi = new byte[BITMAPINFOHEADER.sizeof + numColors * 4]; OS.MoveMemory(bmi, bmiHeader, BITMAPINFOHEADER.sizeof); /* Get the HDC for the device */ int hDC = device.internal_new_GC(null); /* Create the DC and select the bitmap */ int hBitmapDC = OS.CreateCompatibleDC(hDC); int hOldBitmap = OS.SelectObject(hBitmapDC, hBitmap); /* Select the palette if necessary */ int oldPalette = 0; if (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; /* Call with null lpBits to get the image size */ if (OS.IsWinCE) SWT.error(SWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, hBitmap, 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 */ 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, hBitmap, 0, height, lpvBits, bmi, OS.DIB_RGB_COLORS); OS.MoveMemory(data, lpvBits, imageSize); /* Calculate the palette */ PaletteData palette = null; if (depth <= 8) { RGB[] rgbs = new RGB[numColors]; int srcIndex = 40; 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); } /* Do the mask */ byte [] maskData = null; if (info.hbmColor == 0) { /* Do the bottom half of the mask */ maskData = new byte[imageSize]; if (OS.IsWinCE) SWT.error(SWT.ERROR_NOT_IMPLEMENTED); OS.GetDIBits(hBitmapDC, hBitmap, height, height, lpvBits, bmi, OS.DIB_RGB_COLORS); OS.MoveMemory(maskData, lpvBits, imageSize); } else { /* Do the entire mask */ /* Create the BITMAPINFO */ bmiHeader = new BITMAPINFOHEADER(); bmiHeader.biSize = BITMAPINFOHEADER.sizeof; bmiHeader.biWidth = width; bmiHeader.biHeight = -height; bmiHeader.biPlanes = 1; bmiHeader.biBitCount = 1; bmiHeader.biCompression = OS.BI_RGB; bmi = new byte[BITMAPINFOHEADER.sizeof + 8]; OS.MoveMemory(bmi, bmiHeader, BITMAPINFOHEADER.sizeof); /* First color black, second color white */ int offset = BITMAPINFOHEADER.sizeof; bmi[offset + 4] = bmi[offset + 5] = bmi[offset + 6] = (byte)0xFF; bmi[offset + 7] = 0; OS.SelectObject(hBitmapDC, info.hbmMask); /* Call with null lpBits to get the image size */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -