⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
					ImageData.ALPHA_OPAQUE, null, 0, 0, 0,					img.data, img.depth, img.bytesPerLine, newOrder, 0, 0, img.width, img.height, newPalette.redMask, newPalette.greenMask, newPalette.blueMask,					false, false);			if (i.transparentPixel != -1) {				img.transparentPixel = newPalette.getPixel(palette.getRGB(i.transparentPixel));			}			img.maskPad = i.maskPad;			img.maskData = i.maskData;			img.alpha = i.alpha;			img.alphaData = i.alphaData;			i = img;		}	}	/* Construct bitmap info header by hand */	RGB[] rgbs = i.palette.getRGBs();	boolean useBitfields = OS.IsWinCE && (i.depth == 16 || i.depth == 32);	BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER();	bmiHeader.biSize = BITMAPINFOHEADER.sizeof;	bmiHeader.biWidth = i.width;	bmiHeader.biHeight = -i.height;	bmiHeader.biPlanes = 1;	bmiHeader.biBitCount = (short)i.depth;	if (useBitfields) bmiHeader.biCompression = OS.BI_BITFIELDS;	else bmiHeader.biCompression = OS.BI_RGB;	bmiHeader.biClrUsed = rgbs == null ? 0 : rgbs.length;	byte[] bmi;	if (i.palette.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 (i.palette.isDirect) {		if (useBitfields) {			PaletteData palette = i.palette;			int redMask = palette.redMask;			int greenMask = palette.greenMask;			int blueMask = palette.blueMask;			/*			 * The color masks must be written based on the			 * endianness of the ImageData.			 */			if (i.getByteOrder() == ImageData.LSB_FIRST) {				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);			} else {				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);			}		}	} 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);	/* In case of a scanline pad other than 4, do the work to convert it */	byte[] data = i.data;	if (i.scanlinePad != 4 && (i.bytesPerLine % 4 != 0)) {		data = ImageData.convertPad(data, i.width, i.height, i.depth, i.scanlinePad, 4);	}	OS.MoveMemory(pBits[0], data, data.length);		int[] result = null;	if (i.getTransparencyType() == SWT.TRANSPARENCY_MASK) {		/* Get the HDC for the device */		int hDC = device.internal_new_GC(null);					/* Create the color bitmap */		int hdcSrc = OS.CreateCompatibleDC(hDC);		OS.SelectObject(hdcSrc, hDib);		int hBitmap = OS.CreateCompatibleBitmap(hDC, i.width, i.height);		if (hBitmap == 0) SWT.error(SWT.ERROR_NO_HANDLES);		int hdcDest = OS.CreateCompatibleDC(hDC);		OS.SelectObject(hdcDest, hBitmap);		OS.BitBlt(hdcDest, 0, 0, i.width, i.height, hdcSrc, 0, 0, OS.SRCCOPY);				/* Release the HDC for the device */			device.internal_dispose_GC(hDC, null);					/* Create the mask. Windows requires icon masks to have a scanline pad of 2. */		byte[] maskData = ImageData.convertPad(i.maskData, i.width, i.height, 1, i.maskPad, 2);		int hMask = OS.CreateBitmap(i.width, i.height, 1, 1, maskData);		if (hMask == 0) SWT.error(SWT.ERROR_NO_HANDLES);			OS.SelectObject(hdcSrc, hMask);		OS.PatBlt(hdcSrc, 0, 0, i.width, i.height, OS.DSTINVERT);		OS.DeleteDC(hdcSrc);		OS.DeleteDC(hdcDest);		OS.DeleteObject(hDib);				if (image == null) {			result = new int[]{hBitmap, hMask}; 		} else {			/* Create the icon */			ICONINFO info = new ICONINFO();			info.fIcon = true;			info.hbmColor = hBitmap;			info.hbmMask = hMask;			int hIcon = OS.CreateIconIndirect(info);			if (hIcon == 0) SWT.error(SWT.ERROR_NO_HANDLES);			OS.DeleteObject(hBitmap);			OS.DeleteObject(hMask);			image.handle = hIcon;			image.type = SWT.ICON;			if (OS.IsWinCE) image.data = i;		}	} else {		if (image == null) {			result = new int[]{hDib};		} else {			image.handle = hDib;			image.type = SWT.BITMAP;			image.transparentPixel = i.transparentPixel;			if (image.transparentPixel == -1) {				image.alpha = i.alpha;				if (i.alpha == -1 && i.alphaData != null) {					int length = i.alphaData.length;					image.alphaData = new byte[length];					System.arraycopy(i.alphaData, 0, image.alphaData, 0, length);				}			}		}	}	return result;}static int[] init(Device device, Image image, ImageData source, ImageData mask) {	/* Create a temporary image and locate the black pixel */	ImageData imageData;	int blackIndex = 0;	if (source.palette.isDirect) {		imageData = new ImageData(source.width, source.height, source.depth, source.palette);	} else {		RGB black = new RGB(0, 0, 0);		RGB[] rgbs = source.getRGBs();		if (source.transparentPixel != -1) {			/*			 * The source had transparency, so we can use the transparent pixel			 * for black.			 */			RGB[] newRGBs = new RGB[rgbs.length];			System.arraycopy(rgbs, 0, newRGBs, 0, rgbs.length);			if (source.transparentPixel >= newRGBs.length) {				/* Grow the palette with black */				rgbs = new RGB[source.transparentPixel + 1];				System.arraycopy(newRGBs, 0, rgbs, 0, newRGBs.length);				for (int i = newRGBs.length; i <= source.transparentPixel; i++) {					rgbs[i] = new RGB(0, 0, 0);				}			} else {				newRGBs[source.transparentPixel] = black;				rgbs = newRGBs;			}			blackIndex = source.transparentPixel;			imageData = new ImageData(source.width, source.height, source.depth, new PaletteData(rgbs));		} else {			while (blackIndex < rgbs.length) {				if (rgbs[blackIndex].equals(black)) break;				blackIndex++;			}			if (blackIndex == rgbs.length) {				/*				 * We didn't find black in the palette, and there is no transparent				 * pixel we can use.				 */				if ((1 << source.depth) > rgbs.length) {					/* We can grow the palette and add black */					RGB[] newRGBs = new RGB[rgbs.length + 1];					System.arraycopy(rgbs, 0, newRGBs, 0, rgbs.length);					newRGBs[rgbs.length] = black;					rgbs = newRGBs;				} else {					/* No room to grow the palette */					blackIndex = -1;				}			}			imageData = new ImageData(source.width, source.height, source.depth, new PaletteData(rgbs));		}	}	if (blackIndex == -1) {		/* There was no black in the palette, so just copy the data over */		System.arraycopy(source.data, 0, imageData.data, 0, imageData.data.length);	} else {		/* Modify the source image to contain black wherever the mask is 0 */		int[] imagePixels = new int[imageData.width];		int[] maskPixels = new int[mask.width];		for (int y = 0; y < imageData.height; y++) {			source.getPixels(0, y, imageData.width, imagePixels, 0);			mask.getPixels(0, y, mask.width, maskPixels, 0);			for (int i = 0; i < imagePixels.length; i++) {				if (maskPixels[i] == 0) imagePixels[i] = blackIndex;			}			imageData.setPixels(0, y, source.width, imagePixels, 0);		}	}	imageData.maskPad = mask.scanlinePad;	imageData.maskData = mask.data;		return init(device, image, imageData);}void init(Device device, ImageData i) {	if (i == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);	init(device, this, i);}/**	  * Invokes platform specific functionality to allocate a new GC handle. * <p> * <b>IMPORTANT:</b> This method is <em>not</em> part of the public * API for <code>Image</code>. It is marked public only so that it * can be shared within the packages provided by SWT. It is not * available on all platforms, and should never be called from * application code. * </p> * * @param data the platform specific GC data  * @return the platform specific GC handle */public int internal_new_GC (GCData data) {	if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);	/*	* Create a new GC that can draw into the image.	* Only supported for bitmaps.	*/	if (type != SWT.BITMAP || memGC != null) {		SWT.error(SWT.ERROR_INVALID_ARGUMENT);	}		/* Create a compatible HDC for the device */	int hDC = device.internal_new_GC(null);	int imageDC = OS.CreateCompatibleDC(hDC);	device.internal_dispose_GC(hDC, null);	if (imageDC == 0) SWT.error(SWT.ERROR_NO_HANDLES);	if (data != null) {		/* Set the GCData fields */		int mask = SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;		if ((data.style & mask) != 0) {			data.layout = (data.style & SWT.RIGHT_TO_LEFT) != 0 ? OS.LAYOUT_RTL : 0;		} else {			data.style |= SWT.LEFT_TO_RIGHT;		}		data.device = device;		data.image = this;		data.hFont = device.systemFont;	}	return imageDC;}/**	  * Invokes platform specific functionality to dispose a GC handle. * <p> * <b>IMPORTANT:</b> This method is <em>not</em> part of the public * API for <code>Image</code>. It is marked public only so that it * can be shared within the packages provided by SWT. It is not * available on all platforms, and should never be called from * application code. * </p> * * @param hDC the platform specific GC handle * @param data the platform specific GC data  */public void internal_dispose_GC (int hDC, GCData data) {	OS.DeleteDC(hDC);}/** * Returns <code>true</code> if the image has been disposed, * and <code>false</code> otherwise. * <p> * This method gets the dispose state for the image. * When an image has been disposed, it is an error to * invoke any other method using the image. * * @return <code>true</code> when the image is disposed and <code>false</code> otherwise */public boolean isDisposed() {	return handle == 0;}/** * Sets the color to which to map the transparent pixel. * <p> * There are certain uses of <code>Images</code> 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. This method specifies the color that will be used in * these cases. For example: * <pre> *    Button b = new Button(); *    image.setBackground(b.getBackground()); *    b.setImage(image); * </pre> * </p><p> * The image may be modified by this operation (in effect, the * transparent regions may be filled with the supplied color).  Hence * this operation is not reversible and it is not legal to call * this function twice or with a null argument. * </p><p> * This method has no effect if the receiver does not have a transparent * pixel value. * </p> * * @param color the color to use when a transparent pixel is specified * * @exception IllegalArgumentException <ul> *    <li>ERROR_NULL_ARGUMENT - if the color is null</li> *    <li>ERROR_INVALID_ARGUMENT - if the color has been disposed</li> * </ul> * @exception SWTException <ul> *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> * </ul> */public void setBackground(Color color) {	/*	* Note.  Not implemented on WinCE.	*/	if (OS.IsWinCE) return;	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);	if (color == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);	if (color.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);	if (transparentPixel == -1) return;	/* Get the HDC for the device */	int hDC = device.internal_new_GC(null);		/* Change the background color in the image */	BITMAP bm = new BITMAP();			OS.GetObject(handle, BITMAP.sizeof, bm);	int hdcMem = OS.CreateCompatibleDC(hDC);	OS.SelectObject(hdcMem, handle);	int maxColors = 1 << bm.bmBitsPixel;	byte[] colors = new byte[maxColors * 4];	if (OS.IsWinCE) SWT.error(SWT.ERROR_NOT_IMPLEMENTED);	int num

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -