boolean isDib = false; DIBSECTION dib = null; if (bm.bmBits != 0) {  dib = new DIBSECTION(); OS.GetObject(handle, DIBSECTION.sizeof, dib); if (dib.biHeight < 0) isDib = true; }
boolean isDib = (bm.bmBits != 0);
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 */				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 = false;			DIBSECTION dib = null;			if (bm.bmBits != 0) {				/* Only use DIB if image is top-down. */				dib = new DIBSECTION();				OS.GetObject(handle, DIBSECTION.sizeof, dib);				if (dib.biHeight < 0) isDib = true; 			}			/* 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()) {						memGC.flush ();						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;					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;	}}