transBlue = oldColors[offset+2] & 0xFF;
transRed = oldColors[offset+2] & 0xFF;
void drawBitmapTransparent(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple, BITMAP bm, int imgWidth, int imgHeight) {	/* Get the HDC for the device */	Device device = data.device; 	int hDC = device.internal_new_GC(null); 			/* Find the RGB values for the transparent pixel. */	int transBlue = 0, transGreen = 0, transRed = 0;	boolean isDib = bm.bmBits != 0;	int hBitmap = srcImage.handle;	int srcHdc = OS.CreateCompatibleDC(handle);	int oldSrcBitmap = OS.SelectObject(srcHdc, hBitmap);	byte[] originalColors = null;	if (bm.bmBitsPixel <= 8) {		if (isDib) {			/* Palette-based DIBSECTION */			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)((srcImage.transparentPixel << (8 - bm.bmBitsPixel)) | (pBits[0] & ~mask));				OS.MoveMemory(bm.bmBits, pBits, 1);				int color = OS.GetPixel(srcHdc, 0, 0);          		pBits[0] = oldValue;           		OS.MoveMemory(bm.bmBits, pBits, 1);								transBlue = (color & 0xFF0000) >> 16;				transGreen = (color & 0xFF00) >> 8;				transRed = color & 0xFF;							} else {				int maxColors = 1 << bm.bmBitsPixel;				byte[] oldColors = new byte[maxColors * 4];				OS.GetDIBColorTable(srcHdc, 0, maxColors, oldColors);				int offset = srcImage.transparentPixel * 4;								boolean fixPalette = false;				for (int i = 0; i < oldColors.length; i += 4) {					if (i != offset) {						if (oldColors[offset] == oldColors[i] && oldColors[offset+1] == oldColors[i+1] && oldColors[offset+2] == oldColors[i+2]) {							fixPalette = true;							break;						}					}				}				if (fixPalette) {					byte[] newColors = new byte[oldColors.length];					transRed = transGreen = transBlue = 0xff;					newColors[offset] = (byte)transBlue;					newColors[offset+1] = (byte)transGreen;					newColors[offset+2] = (byte)transRed;					OS.SetDIBColorTable(srcHdc, 0, maxColors, newColors);					originalColors = oldColors;				} else {					transRed = oldColors[offset] & 0xFF;					transGreen = oldColors[offset+1] & 0xFF;					transBlue = oldColors[offset+2] & 0xFF;				}			}		} else {			/* Palette-based bitmap */			int numColors = 1 << bm.bmBitsPixel;			/* Set the few fields necessary to get the RGB data out */			BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER();			bmiHeader.biSize = BITMAPINFOHEADER.sizeof;			bmiHeader.biPlanes = bm.bmPlanes;			bmiHeader.biBitCount = bm.bmBitsPixel;			byte[] bmi = new byte[BITMAPINFOHEADER.sizeof + numColors * 4];			OS.MoveMemory(bmi, bmiHeader, BITMAPINFOHEADER.sizeof);			if (OS.IsWinCE) SWT.error(SWT.ERROR_NOT_IMPLEMENTED);			OS.GetDIBits(srcHdc, srcImage.handle, 0, 0, 0, bmi, OS.DIB_RGB_COLORS);			int offset = BITMAPINFOHEADER.sizeof + 4 * srcImage.transparentPixel;			transRed = bmi[offset + 2] & 0xFF;			transGreen = bmi[offset + 1] & 0xFF;			transBlue = bmi[offset] & 0xFF;		}	} else {		/* Direct color image */		int pixel = srcImage.transparentPixel;		switch (bm.bmBitsPixel) {			case 16:				transBlue = (pixel & 0x1F) << 3;				transGreen = (pixel & 0x3E0) >> 2;				transRed = (pixel & 0x7C00) >> 7;				break;			case 24:				transBlue = (pixel & 0xFF0000) >> 16;				transGreen = (pixel & 0xFF00) >> 8;				transRed = pixel & 0xFF;				break;			case 32:				transBlue = (pixel & 0xFF000000) >>> 24;				transGreen = (pixel & 0xFF0000) >> 16;				transRed = (pixel & 0xFF00) >> 8;				break;		}	}	int transparentColor = transBlue << 16 | transGreen << 8 | transRed;	if (OS.IsWinCE) {		/*		* Note in WinCE. TransparentImage uses the first entry of a palette		* based image when there are multiple entries that have the same		* transparent color.		*/		OS.TransparentImage(handle, destX, destY, destWidth, destHeight, srcHdc, srcX, srcY, srcWidth, srcHeight, transparentColor);	} else if (originalColors == null && OS.IsWinNT && OS.WIN32_VERSION >= OS.VERSION(4, 10)) {		int mode = OS.SetStretchBltMode(handle, OS.COLORONCOLOR);		OS.TransparentBlt(handle, destX, destY, destWidth, destHeight, srcHdc, srcX, srcY, srcWidth, srcHeight, transparentColor);		OS.SetStretchBltMode(handle, mode);	} else {		/* Create the mask for the source image */		int maskHdc = OS.CreateCompatibleDC(hDC);		int maskBitmap = OS.CreateBitmap(imgWidth, imgHeight, 1, 1, null);		int oldMaskBitmap = OS.SelectObject(maskHdc, maskBitmap);		OS.SetBkColor(srcHdc, transparentColor);		OS.BitBlt(maskHdc, 0, 0, imgWidth, imgHeight, srcHdc, 0, 0, OS.SRCCOPY);		if (originalColors != null) OS.SetDIBColorTable(srcHdc, 0, 1 << bm.bmBitsPixel, originalColors);			if (OS.GetDeviceCaps(handle, OS.TECHNOLOGY) == OS.DT_RASPRINTER) {			/* Most printers do not support BitBlt(), draw the source bitmap transparently using clipping */			drawBitmapTransparentByClipping(srcHdc, maskHdc, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight, simple, imgWidth, imgHeight);		} else {			/* Draw the source bitmap transparently using invert/and mask/invert */			int tempHdc = OS.CreateCompatibleDC(hDC);			int tempBitmap = OS.CreateCompatibleBitmap(hDC, destWidth, destHeight);				int oldTempBitmap = OS.SelectObject(tempHdc, tempBitmap);			OS.BitBlt(tempHdc, 0, 0, destWidth, destHeight, handle, destX, destY, OS.SRCCOPY);			if (!simple && (srcWidth != destWidth || srcHeight != destHeight)) {				if (!OS.IsWinCE) OS.SetStretchBltMode(tempHdc, OS.COLORONCOLOR);				OS.StretchBlt(tempHdc, 0, 0, destWidth, destHeight, srcHdc, srcX, srcY, srcWidth, srcHeight, OS.SRCINVERT);				OS.StretchBlt(tempHdc, 0, 0, destWidth, destHeight, maskHdc, srcX, srcY, srcWidth, srcHeight, OS.SRCAND);				OS.StretchBlt(tempHdc, 0, 0, destWidth, destHeight, srcHdc, srcX, srcY, srcWidth, srcHeight, OS.SRCINVERT);			} else {				OS.BitBlt(tempHdc, 0, 0, destWidth, destHeight, srcHdc, srcX, srcY, OS.SRCINVERT);				OS.BitBlt(tempHdc, 0, 0, destWidth, destHeight, maskHdc, srcX, srcY, OS.SRCAND);				OS.BitBlt(tempHdc, 0, 0, destWidth, destHeight, srcHdc, srcX, srcY, OS.SRCINVERT);			}			OS.BitBlt(handle, destX, destY, destWidth, destHeight, tempHdc, 0, 0, OS.SRCCOPY);			OS.SelectObject(tempHdc, oldTempBitmap);			OS.DeleteDC(tempHdc);			OS.DeleteObject(tempBitmap);		}		OS.SelectObject(maskHdc, oldMaskBitmap);		OS.DeleteDC(maskHdc);		OS.DeleteObject(maskBitmap);	}	OS.SelectObject(srcHdc, oldSrcBitmap);	if (hBitmap != srcImage.handle) OS.DeleteObject(hBitmap);	OS.DeleteDC(srcHdc);		/* Release the HDC for the device */	device.internal_dispose_GC(hDC, null);}