kadreg profil: Utilisateur | ouuuiiiii
Code :
- /**
- * Constructs a new instance of this class based on the
- * provided image, with an appearance that varies depending
- * on the value of the flag. The possible flag values are:
- * <dl>
- * <dt><b>IMAGE_COPY</b></dt>
- * <dd>the result is an identical copy of srcImage</dd>
- * <dt><b>IMAGE_DISABLE</b></dt>
- * <dd>the result is a copy of srcImage which has a <em>disabled</em> look</dd>
- * <dt><b>IMAGE_GRAY</b></dt>
- * <dd>the result is a copy of srcImage which has a <em>gray scale</em> look</dd>
- * </dl>
- *
- * @param device the device on which to create the image
- * @param srcImage the image to use as the source
- * @param flag the style, either <code>IMAGE_COPY</code>, <code>IMAGE_DISABLE</code> or <code>IMAGE_GRAY</code>
- *
- * @exception IllegalArgumentException <ul>
- * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
- * <li>ERROR_NULL_ARGUMENT - if srcImage is null</li>
- * <li>ERROR_INVALID_ARGUMENT - if the flag is not one of <code>IMAGE_COPY</code>, <code>IMAGE_DISABLE</code> or <code>IMAGE_GRAY</code></li>
- * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
- * </ul>
- * @exception SWTException <ul>
- * <li>ERROR_INVALID_IMAGE - if the image is not a bitmap or an icon, or
- * is otherwise in an invalid state</li>
- * <li>ERROR_UNSUPPORTED_DEPTH - if the depth of the Image is not supported</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, Image srcImage, int flag) {
- if (device == null) device = Device.getDevice();
- if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
- this.device = device;
- if (srcImage == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
- if (srcImage.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
- switch (flag) {
- case SWT.IMAGE_COPY: {
- Rectangle r = srcImage.getBounds();
- this.type = srcImage.type;
- switch (type) {
- case SWT.BITMAP:
- /* Get the HDC for the device */
- int hDC = device.internal_new_GC(null);
- /* Copy the bitmap */
- int hdcSource = OS.CreateCompatibleDC(hDC);
- int hdcDest = OS.CreateCompatibleDC(hDC);
- int hOldSrc = OS.SelectObject(hdcSource, srcImage.handle);
- handle = OS.CreateCompatibleBitmap(hdcSource, r.width, r.height);
- if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
- int hOldDest = OS.SelectObject(hdcDest, handle);
- OS.BitBlt(hdcDest, 0, 0, r.width, r.height, hdcSource, 0, 0, OS.SRCCOPY);
- OS.SelectObject(hdcSource, hOldSrc);
- OS.SelectObject(hdcDest, hOldDest);
- OS.DeleteDC(hdcSource);
- OS.DeleteDC(hdcDest);
- /* Release the HDC for the device */
- device.internal_dispose_GC(hDC, null);
- transparentPixel = srcImage.transparentPixel;
- alpha = srcImage.alpha;
- if (srcImage.alphaData != null) {
- alphaData = new byte[srcImage.alphaData.length];
- System.arraycopy(srcImage.alphaData, 0, alphaData, 0, alphaData.length);
- }
- break;
- case SWT.ICON:
- if (OS.IsWinCE) {
- init(device, srcImage.data);
- } else {
- handle = OS.CopyImage(srcImage.handle, OS.IMAGE_ICON, r.width, r.height, 0);
- if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
- }
- break;
- default:
- SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
- }
- if (device.tracking) device.new_Object(this);
- return;
- }
- case SWT.IMAGE_DISABLE: {
- Rectangle r = srcImage.getBounds();
- this.type = srcImage.type;
- byte[] rgbBwBitmapInfo = {
- 40,0,0,0, /* biSize */
- (byte)(r.width & 0xFF), /* biWidth */
- (byte)((r.width & 0xFF00) >> 8),
- (byte)((r.width & 0xFF0000) >> 16),
- (byte)((r.width & 0xFF000000) >> 24),
- (byte)(r.height & 0xFF), /* biHeight */
- (byte)((r.height & 0xFF00) >> 8),
- (byte)((r.height & 0xFF0000) >> 16),
- (byte)((r.height & 0xFF000000) >> 24),
- 1,0, /* biPlanes */
- 1,0, /* biBitCount */
- 0,0,0,0, /* biCompression */
- 0,0,0,0, /* biSizeImage */
- 0,0,0,0, /* biXPelsPerMeter */
- 0,0,0,0, /* biYPelsPerMeter */
- 0,0,0,0, /* biClrUsed */
- 0,0,0,0, /* biClrImportant */
- 0,0,0,0, /* First color: black */
- (byte)0xFF,(byte)0xFF,(byte)0xFF,0 /* Second color: white */
- };
- /* Get the HDC for the device */
- int hDC = device.internal_new_GC(null);
- /* Source DC */
- int hdcSource = OS.CreateCompatibleDC(hDC);
- if (hdcSource == 0) SWT.error(SWT.ERROR_NO_HANDLES);
- /* Monochrome (Intermediate) DC */
- int bwDC = OS.CreateCompatibleDC(hdcSource);
- if (bwDC == 0) SWT.error(SWT.ERROR_NO_HANDLES);
- /* Destination DC */
- int hdcBmp = OS.CreateCompatibleDC(hDC);
- if (hdcBmp == 0) SWT.error(SWT.ERROR_NO_HANDLES);
- /* Monochrome (Intermediate) DIB section */
- int[] pbitsBW = new int[1];
- int hbmBW = OS.CreateDIBSection(bwDC, rgbBwBitmapInfo, OS.DIB_RGB_COLORS, pbitsBW, 0, 0);
- if (hbmBW == 0) SWT.error(SWT.ERROR_NO_HANDLES);
- switch (type) {
- case SWT.BITMAP:
- /* Attach the bitmap to the source DC */
- int hOldSrc = OS.SelectObject(hdcSource, srcImage.handle);
- /* Create the destination bitmap */
- handle = OS.CreateCompatibleBitmap(hDC, r.width, r.height);
- if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
- /* Attach the DIB section and the new bitmap to the DCs */
- int hOldBw = OS.SelectObject(bwDC, hbmBW);
- int hOldBmp = OS.SelectObject(hdcBmp, handle);
- /* BitBlt the bitmap into the monochrome DIB section */
- OS.BitBlt(bwDC, 0, 0, r.width, r.height, hdcSource, 0, 0, OS.SRCCOPY);
- /* Paint the destination rectangle in gray */
- RECT rect = new RECT();
- rect.left = 0;
- rect.top = 0;
- rect.right = r.width;
- rect.bottom = r.height;
- 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
- */
- int hb = OS.CreateSolidBrush(OS.GetSysColor(OS.COLOR_3DHILIGHT));
- int oldBrush = OS.SelectObject(hdcBmp, hb);
- OS.BitBlt(hdcBmp, 1, 1, r.width, r.height, bwDC, 0, 0, 0xB8074A);
- /*
- * BitBlt the black bits in the monochrome bitmap into
- * COLOR_3DSHADOW bits in the destination DC.
- */
- hb = OS.CreateSolidBrush(OS.GetSysColor(OS.COLOR_3DSHADOW));
- OS.DeleteObject(OS.SelectObject(hdcBmp, hb));
- 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.SelectObject(hdcBmp, hOldBmp);
- OS.SelectObject(bwDC, hOldBw);
- OS.DeleteDC(hdcSource);
- OS.DeleteDC(bwDC);
- OS.DeleteDC(hdcBmp);
- OS.DeleteObject(hbmBW);
- /* Release the HDC for the device */
- device.internal_dispose_GC(hDC, null);
- break;
- case SWT.ICON:
- /* Get icon information */
- ICONINFO iconInfo = new ICONINFO();
- if (OS.IsWinCE) {
- GetIconInfo(srcImage, iconInfo);
- } else {
- if (!OS.GetIconInfo(srcImage.handle, iconInfo))
- SWT.error(SWT.ERROR_INVALID_IMAGE);
- }
- int hdcMask = OS.CreateCompatibleDC(hDC);
- /* Create the destination bitmaps */
- if (iconInfo.hbmColor == 0)
- hOldSrc = OS.SelectObject(hdcSource, iconInfo.hbmMask);
- else
- hOldSrc = OS.SelectObject(hdcSource, iconInfo.hbmColor);
- int newHbmp = OS.CreateCompatibleBitmap(hdcSource, r.width, r.height);
- if (newHbmp == 0) SWT.error(SWT.ERROR_NO_HANDLES);
- int newHmask = OS.CreateBitmap(r.width, r.height, 1, 1, null);
- if (newHmask == 0) SWT.error(SWT.ERROR_NO_HANDLES);
- /* BitBlt the source mask into the destination mask */
- int hOldMask = OS.SelectObject(hdcMask, newHmask);
- if (iconInfo.hbmColor != 0)
- OS.SelectObject(hdcSource, iconInfo.hbmMask);
- OS.SelectObject(hdcSource, iconInfo.hbmMask);
- OS.BitBlt(hdcMask, 0, 0, r.width, r.height, hdcSource, 0, 0, OS.SRCCOPY);
- /* Attach the monochrome DIB section and the destination bitmap to the DCs */
- hOldBw = OS.SelectObject(bwDC, hbmBW);
- /* BitBlt the bitmap into the monochrome DIB section */
- if (iconInfo.hbmColor == 0) {
- OS.SelectObject(hdcSource, iconInfo.hbmMask);
- 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;
- if (data.transparentPixel != -1) newData.transparentPixel = 254;
- /* 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];
- if (pixel != data.transparentPixel) {
- 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;
- int intensity = (red+red+green+green+green+green+green+blue) >> 3;
- if (newData.transparentPixel == intensity) intensity = 255;
- newData.data[offset] = (byte)intensity;
- } else {
- newData.data[offset] = (byte)254;
- }
- offset++;
- }
- }
- }
- init (device, newData);
- if (device.tracking) device.new_Object(this);
- return;
- }
- default:
- SWT.error(SWT.ERROR_INVALID_ARGUMENT);
- }
- }
|
---------------
brisez les rêves des gens, il en restera toujours quelque chose... -- laissez moi troller sur discu !
|