?? image.java
字號:
OS.BitBlt(bwDC, 0, 0, r.width, r.height, hdcSource, 0, r.height, OS.SRCCOPY); } else { OS.SelectObject(hdcSource, iconInfo.hbmColor); OS.BitBlt(bwDC, 0, 0, r.width, r.height, hdcSource, 0, 0, OS.SRCCOPY); } /* Paint the destination rectangle in grey */ rect = new RECT(); rect.left = 0; rect.top = 0; rect.right = r.width; rect.bottom = r.height; hOldBmp = OS.SelectObject(hdcBmp, newHbmp); OS.FillRect(hdcBmp, rect, OS.GetSysColorBrush(OS.COLOR_3DFACE)); /* * BitBlt the black bits in the monochrome bitmap into * COLOR_3DHILIGHT bits in the destination DC. * The magic ROP comes from Charles Petzold's book */ hb = OS.CreateSolidBrush(OS.GetSysColor(OS.COLOR_3DSHADOW)); oldBrush = OS.SelectObject(hdcBmp, hb); OS.BitBlt(hdcBmp, 0, 0, r.width, r.height, bwDC, 0, 0, 0xB8074A); /* Invert mask into hdcBw */ OS.BitBlt(bwDC, 0, 0, r.width, r.height, hdcMask, 0, 0, OS.NOTSRCCOPY); /* Select black brush into destination */ hb = OS.CreateSolidBrush(0); OS.DeleteObject(OS.SelectObject(hdcBmp, hb)); /* * Copy black bits from monochrome bitmap into black bits in the * destination DC. */ OS.BitBlt(hdcBmp, 0, 0, r.width, r.height, bwDC, 0, 0, 0xB8074A); OS.DeleteObject(OS.SelectObject(hdcBmp, oldBrush)); /* Free resources */ OS.SelectObject(hdcSource, hOldSrc); OS.DeleteDC(hdcSource); OS.SelectObject(bwDC, hOldBw); OS.DeleteDC(bwDC); OS.SelectObject(hdcBmp, hOldBmp); OS.DeleteDC(hdcBmp); OS.SelectObject(hdcMask, hOldMask); OS.DeleteDC(hdcMask); OS.DeleteObject(hbmBW); /* Release the HDC for the device */ device.internal_dispose_GC(hDC, null); /* Create the new iconinfo */ ICONINFO newIconInfo = new ICONINFO(); newIconInfo.fIcon = iconInfo.fIcon; newIconInfo.hbmMask = newHmask; newIconInfo.hbmColor = newHbmp; /* Create the new icon */ handle = OS.CreateIconIndirect(newIconInfo); if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); /* Free bitmaps */ OS.DeleteObject(newHbmp); OS.DeleteObject(newHmask); if (iconInfo.hbmColor != 0) OS.DeleteObject(iconInfo.hbmColor); OS.DeleteObject(iconInfo.hbmMask); break; default: SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); } if (device.tracking) device.new_Object(this); return; } case SWT.IMAGE_GRAY: { Rectangle r = srcImage.getBounds(); ImageData data = srcImage.getImageData(); PaletteData palette = data.palette; ImageData newData = data; if (!palette.isDirect) { /* Convert the palette entries to gray. */ RGB [] rgbs = palette.getRGBs(); for (int i=0; i<rgbs.length; i++) { if (data.transparentPixel != i) { RGB color = rgbs [i]; int red = color.red; int green = color.green; int blue = color.blue; int intensity = (red+red+green+green+green+green+green+blue) >> 3; color.red = color.green = color.blue = intensity; } } newData.palette = new PaletteData(rgbs); } else { /* Create a 8 bit depth image data with a gray palette. */ RGB[] rgbs = new RGB[256]; for (int i=0; i<rgbs.length; i++) { rgbs[i] = new RGB(i, i, i); } newData = new ImageData(r.width, r.height, 8, new PaletteData(rgbs)); newData.maskData = data.maskData; newData.maskPad = data.maskPad; /* Convert the pixels. */ int[] scanline = new int[r.width]; int redMask = palette.redMask; int greenMask = palette.greenMask; int blueMask = palette.blueMask; int redShift = palette.redShift; int greenShift = palette.greenShift; int blueShift = palette.blueShift; for (int y=0; y<r.height; y++) { int offset = y * newData.bytesPerLine; data.getPixels(0, y, r.width, scanline, 0); for (int x=0; x<r.width; x++) { int pixel = scanline[x]; int red = pixel & redMask; red = (redShift < 0) ? red >>> -redShift : red << redShift; int green = pixel & greenMask; green = (greenShift < 0) ? green >>> -greenShift : green << greenShift; int blue = pixel & blueMask; blue = (blueShift < 0) ? blue >>> -blueShift : blue << blueShift; newData.data[offset++] = (byte)((red+red+green+green+green+green+green+blue) >> 3); } } } init (device, newData); if (device.tracking) device.new_Object(this); return; } default: SWT.error(SWT.ERROR_INVALID_ARGUMENT); }}/** * Constructs an empty instance of this class with the * width and height of the specified rectangle. The result * may be drawn upon by creating a GC and using any of its * drawing operations, as shown in the following example: * <pre> * Image i = new Image(device, boundsRectangle); * GC gc = new GC(i); * gc.drawRectangle(0, 0, 50, 50); * gc.dispose(); * </pre> * <p> * Note: Some platforms may have a limitation on the size * of image that can be created (size depends on width, height, * and depth). For example, Windows 95, 98, and ME do not allow * images larger than 16M. * </p> * * @param device the device on which to create the image * @param bounds a rectangle specifying the image's width and height (must not be null) * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> * <li>ERROR_NULL_ARGUMENT - if the bounds rectangle is null</li> * <li>ERROR_INVALID_ARGUMENT - if either the rectangle's width or height is negative</li> * </ul> * @exception SWTError <ul> * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> * </ul> */public Image(Device device, Rectangle bounds) { if (device == null) device = Device.getDevice(); if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); init(device, bounds.width, bounds.height); if (device.tracking) device.new_Object(this); }/** * Constructs an instance of this class from the given * <code>ImageData</code>. * * @param device the device on which to create the image * @param data the image data to create the image from (must not be null) * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> * <li>ERROR_NULL_ARGUMENT - if the image data is null</li> * </ul> * @exception SWTError <ul> * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> * </ul> * @exception SWTException <ul> * <li>ERROR_UNSUPPORTED_DEPTH - if the depth of the ImageData is not supported</li> * </ul> */public Image(Device device, ImageData data) { if (device == null) device = Device.getDevice(); if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); init(device, data); if (device.tracking) device.new_Object(this); }/** * Constructs an instance of this class, whose type is * <code>SWT.ICON</code>, from the two given <code>ImageData</code> * objects. The two images must be the same size, and the mask image * must have a color depth of 1. Pixel transparency in either image * will be ignored. If either image is an icon to begin with, an * exception is thrown. * <p> * The mask image should contain white wherever the icon is to be visible, * and black wherever the icon is to be transparent. In addition, * the source image should contain black wherever the icon is to be * transparent. * </p> * * @param device the device on which to create the icon * @param source the color data for the icon * @param mask the mask data for the icon * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> * <li>ERROR_NULL_ARGUMENT - if either the source or mask is null </li> * <li>ERROR_INVALID_ARGUMENT - if source and mask are different sizes or * if the mask is not monochrome, or if either the source or mask * is already an icon</li> * </ul> * @exception SWTError <ul> * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> * </ul> */public Image(Device device, ImageData source, ImageData mask) { if (device == null) device = Device.getDevice(); if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (source == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (mask == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); if (source.width != mask.width || source.height != mask.height) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } if (mask.depth != 1) { /* * Feature in Windows. 1-bit DIB sections are buggy on Win98, so we * create 4-bit DIBs when given a 1-bit ImageData. In order to allow * users to draw on the masks, we must also support 4-bit masks in * icon creation by converting them into 1-bit masks. */ if (mask.depth != 4) SWT.error(SWT.ERROR_INVALID_ARGUMENT); PaletteData palette = new PaletteData(new RGB[] {new RGB(0, 0, 0), new RGB(255,255,255)}); ImageData tempMask = new ImageData(mask.width, mask.height, 1, palette); /* Find index of black in mask palette */ RGB[] rgbs = mask.getRGBs(); int blackIndex = 0; while (blackIndex < rgbs.length) { if (rgbs[blackIndex].equals(palette.colors[0])) break; blackIndex++; } if (blackIndex == rgbs.length) SWT.error(SWT.ERROR_INVALID_ARGUMENT); int[] pixels = new int[mask.width]; for (int y = 0; y < mask.height; y++) { mask.getPixels(0, y, mask.width, pixels, 0); for (int i = 0; i < pixels.length; i++) { if (pixels[i] == blackIndex) { pixels[i] = 0; } else { pixels[i] = 1; } } tempMask.setPixels(0, y, mask.width, pixels, 0); } mask = tempMask; } init(device, this, source, mask); if (device.tracking) device.new_Object(this); }/** * Constructs an instance of this class by loading its representation * from the specified input stream. Throws an error if an error * occurs while loading the image, or if the result is an image * of an unsupported type. * <p> * This constructor is provided for convenience when loading a single * image only. If the stream contains multiple images, only the first * one will be loaded. To load multiple images, use * <code>ImageLoader.load()</code>. * </p><p> * This constructor may be used to load a resource as follows: * </p> * <pre> * new Image(device, clazz.getResourceAsStream("file.gif")); * </pre> * * @param device the device on which to create the image * @param stream the input stream to load the image from * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> * <li>ERROR_NULL_ARGUMENT - if the stream is null</li> * </ul> * @exception SWTException <ul> * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data </li> * <li>ERROR_IO - if an IO error occurs while reading data</li> * <li>ERROR_UNSUPPORTED_DEPTH - if the InputStream describes an image with an unsupported depth</li> * </ul> * @exception SWTError <ul> * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> * </ul> */public Image (Device device, InputStream stream) { if (device == null) device = Device.getDevice(); if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); init(device, new ImageData(stream)); if (device.tracking) device.new_Object(this); }/** * Constructs an instance of this class by loading its representation * from the file with the specified name. Throws an error if an error * occurs while loading the image, or if the result is an image * of an unsupported type. * <p> * This constructor is provided for convenience when loading * a single image only. If the specified file contains * multiple images, only the first one will be used. * * @param device the device on which to create the image * @param filename the name of the file to load the image from * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> * <li>ERROR_NULL_ARGUMENT - if the file name is null</li> * </ul> * @exception SWTException <ul> * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data </li> * <li>ERROR_IO - if an IO error occurs while reading data</li> * <li>ERROR_UNSUPPORTED_DEPTH - if the image file has an unsupported depth</li> * </ul> * @exception SWTError <ul> * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> * </ul> */public Image (Device device, String filename) { if (device == null) device = Device.getDevice(); if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); init(device, new ImageData(filename)); if (device.tracking) device.new_Object(this); }/** * Create a DIB from a DDB without using GetDIBits. Note that * the DDB should not be selected into a HDC. */int createDIBFromDDB(int hDC, int hBitmap, int width, int height) { /* Determine the DDB depth */ int bits = OS.GetDeviceCaps (hDC, OS.BITSPIXEL); int planes = OS.GetDeviceCaps (hDC, OS.PLANES); int depth = bits * planes; /* Determine the DIB palette */ boolean isDirect = depth > 8; RGB[] rgbs = null; if (!isDirect) { int numColors = 1 << depth; byte[] logPalette = new byte[4 * numColors]; OS.GetPaletteEntries(device.hPalette, 0, numColors, logPalette); rgbs = new RGB[numColors]; for (int i = 0; i < numColors; i++) { rgbs[i] = new RGB(logPalette[i] & 0xFF, logPalette[i + 1] & 0xFF, logPalette[i + 2] & 0xFF); } } boolean useBitfields = OS.IsWinCE && (depth == 16 || depth == 32); BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER(); bmiHeader.biSize = BITMAPINFOHEADER.sizeof; bmiHeader.biWidth = width;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -