Search in sources :

Example 31 with PaletteData

use of org.eclipse.swt.graphics.PaletteData in project yamcs-studio by yamcs.

the class SVGUtils method toSWT.

/**
 * Converts an AWT based buffered image into an SWT <code>Image</code>. This will always return an
 * <code>Image</code> that has 24 bit depth regardless of the type of AWT buffered image that is passed into the
 * method.
 *
 * @param awtImage
 *            the {@link java.awt.image.BufferedImage} to be converted to an <code>Image</code>
 * @return an <code>Image</code> that represents the same image data as the AWT <code>BufferedImage</code> type.
 */
public static ImageData toSWT(Device device, BufferedImage awtImage) {
    // We can force bit depth to be 24 bit because BufferedImage getRGB
    // allows us to always retrieve 24 bit data regardless of source color depth.
    PaletteData palette = new PaletteData(0xFF0000, 0xFF00, 0xFF);
    ImageData swtImageData = new ImageData(awtImage.getWidth(), awtImage.getHeight(), 24, palette);
    // Ensure scan size is aligned on 32 bit.
    int scansize = (((awtImage.getWidth() * 3) + 3) * 4) / 4;
    WritableRaster alphaRaster = awtImage.getAlphaRaster();
    byte[] alphaBytes = new byte[awtImage.getWidth()];
    for (int y = 0; y < awtImage.getHeight(); y++) {
        int[] buff = awtImage.getRGB(0, y, awtImage.getWidth(), 1, null, 0, scansize);
        swtImageData.setPixels(0, y, awtImage.getWidth(), buff, 0);
        if (alphaRaster != null) {
            int[] alpha = alphaRaster.getPixels(0, y, awtImage.getWidth(), 1, (int[]) null);
            for (int i = 0; i < awtImage.getWidth(); i++) {
                alphaBytes[i] = (byte) alpha[i];
            }
            swtImageData.setAlphas(0, y, awtImage.getWidth(), alphaBytes, 0);
        }
    }
    return swtImageData;
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData) WritableRaster(java.awt.image.WritableRaster)

Example 32 with PaletteData

use of org.eclipse.swt.graphics.PaletteData in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_GC method test_drawImageLorg_eclipse_swt_graphics_ImageII.

@Test
public void test_drawImageLorg_eclipse_swt_graphics_ImageII() {
    Color c1 = new Color(display, 255, 0, 0);
    Color c2 = new Color(display, 0, 0, 0);
    Color c3 = new Color(display, 255, 255, 0);
    PaletteData paletteData = new PaletteData(c1.getRGB(), c2.getRGB(), c3.getRGB());
    ImageData data = new ImageData(30, 30, 8, paletteData);
    for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
            if (x > y)
                data.setPixel(x, y, paletteData.getPixel(c1.getRGB()));
            else if (x < y)
                data.setPixel(x, y, paletteData.getPixel(c2.getRGB()));
            else
                data.setPixel(x, y, paletteData.getPixel(c3.getRGB()));
        }
    }
    Image image = new Image(display, data);
    data = image.getImageData();
    data.transparentPixel = paletteData.getPixel(c1.getRGB());
    Image imageTransparent = new Image(display, data);
    data.transparentPixel = -1;
    for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
            data.setAlpha(x, y, 127);
        }
    }
    Image imageAlpha = new Image(display, data);
    gc.drawImage(image, 100, 100);
    gc.drawImage(imageTransparent, 130, 100);
    gc.drawImage(imageAlpha, 160, 100);
    try {
        gc.drawImage(null, 100, 100);
        fail("No exception thrown");
    } catch (IllegalArgumentException e) {
    }
    image.dispose();
    imageTransparent.dispose();
    imageAlpha.dispose();
    c1.dispose();
    c2.dispose();
    c3.dispose();
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData) Color(org.eclipse.swt.graphics.Color) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.swt.graphics.Point) Test(org.junit.Test)

Example 33 with PaletteData

use of org.eclipse.swt.graphics.PaletteData in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_GC method test_copyAreaIIIIII.

@Test
public void test_copyAreaIIIIII() {
    Color white = display.getSystemColor(SWT.COLOR_WHITE);
    Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    RGB whiteRGB = getRealRGB(white);
    RGB blueRGB = getRealRGB(blue);
    int width = 20;
    int height = 20;
    int destX = 10;
    int destY = 50;
    gc.setBackground(white);
    gc.fillRectangle(image.getBounds());
    gc.setBackground(blue);
    gc.fillRectangle(5, 0, 6, 1);
    gc.copyArea(0, 0, width, height, destX, destY);
    ImageData imageData = image.getImageData();
    PaletteData palette = imageData.palette;
    if (DPIUtil.getDeviceZoom() != 100) {
        // TODO Fix non integer scaling factors.
        if (SwtTestUtil.verbose) {
            System.out.println("Excluded test_copyAreaIIIIII(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_GC)");
        }
        return;
    }
    int pixel = imageData.getPixel(destX + 4, destY);
    assertEquals(":a:", whiteRGB, palette.getRGB(pixel));
    pixel = imageData.getPixel(destX + 6, destY);
    assertEquals(":b:", blueRGB, palette.getRGB(pixel));
    pixel = imageData.getPixel(destX + 10, destY);
    assertEquals(":c:", blueRGB, palette.getRGB(pixel));
    pixel = imageData.getPixel(destX + 12, destY);
    assertEquals(":d:", whiteRGB, palette.getRGB(pixel));
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData) Color(org.eclipse.swt.graphics.Color) RGB(org.eclipse.swt.graphics.RGB) Point(org.eclipse.swt.graphics.Point) Test(org.junit.Test)

Example 34 with PaletteData

use of org.eclipse.swt.graphics.PaletteData in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_GC method getRealRGB.

/**
 * Return the actual RGB value used for rendering for the given Color.
 * This may be different from the Color's RGB value on lower-color displays
 * (16bpp or less).
 */
RGB getRealRGB(Color color) {
    Image colorImage = new Image(display, 10, 10);
    GC imageGc = new GC(colorImage);
    ImageData imageData;
    PaletteData palette;
    int pixel;
    imageGc.setBackground(color);
    imageGc.setForeground(color);
    imageGc.fillRectangle(0, 0, 10, 10);
    imageData = colorImage.getImageData();
    palette = imageData.palette;
    imageGc.dispose();
    colorImage.dispose();
    pixel = imageData.getPixel(0, 0);
    return palette.getRGB(pixel);
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData) Image(org.eclipse.swt.graphics.Image) GC(org.eclipse.swt.graphics.GC) Point(org.eclipse.swt.graphics.Point)

Example 35 with PaletteData

use of org.eclipse.swt.graphics.PaletteData in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_Image method test_getImageDataCurrentZoom.

@SuppressWarnings("deprecation")
@Test
public void test_getImageDataCurrentZoom() {
    Rectangle bounds = new Rectangle(0, 0, 10, 20);
    Image image = new Image(display, bounds.width, bounds.height);
    image.dispose();
    try {
        image.getImageDataAtCurrentZoom();
        fail("No exception thrown for disposed image");
    } catch (SWTException e) {
        assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
    }
    // creates bitmap image and compare size of imageData
    image = new Image(display, bounds.width, bounds.height);
    ImageData imageDataAtCurrentZoom = image.getImageDataAtCurrentZoom();
    image.dispose();
    Rectangle boundsAtCurrentZoom = new Rectangle(0, 0, imageDataAtCurrentZoom.width, imageDataAtCurrentZoom.height);
    assertEquals(":a: Size of ImageData returned from Image.getImageDataAtCurrentZoom method doesn't return matches with bounds in Pixel values.", boundsAtCurrentZoom, DPIUtil.autoScaleUp(bounds));
    // create icon image and compare size of imageData
    ImageData imageData = new ImageData(bounds.width, bounds.height, 1, new PaletteData(new RGB[] { new RGB(0, 0, 0) }));
    image = new Image(display, imageData);
    imageDataAtCurrentZoom = image.getImageDataAtCurrentZoom();
    image.dispose();
    boundsAtCurrentZoom = new Rectangle(0, 0, imageDataAtCurrentZoom.width, imageDataAtCurrentZoom.height);
    assertEquals(":b: Size of ImageData returned from Image.getImageDataAtCurrentZoom method doesn't return matches with bounds in Pixel values.", boundsAtCurrentZoom, DPIUtil.autoScaleUp(bounds));
    // create image with FileNameProvider
    image = new Image(display, imageFileNameProvider);
    imageDataAtCurrentZoom = image.getImageDataAtCurrentZoom();
    boundsAtCurrentZoom = new Rectangle(0, 0, imageDataAtCurrentZoom.width, imageDataAtCurrentZoom.height);
    bounds = image.getBounds();
    image.dispose();
    assertEquals(":c: Size of ImageData returned from Image.getImageDataAtCurrentZoom method doesn't return matches with bounds in Pixel values.", boundsAtCurrentZoom, DPIUtil.autoScaleUp(bounds));
    // create image with ImageDataProvider
    image = new Image(display, imageDataProvider);
    imageDataAtCurrentZoom = image.getImageDataAtCurrentZoom();
    boundsAtCurrentZoom = new Rectangle(0, 0, imageDataAtCurrentZoom.width, imageDataAtCurrentZoom.height);
    bounds = image.getBounds();
    image.dispose();
    assertEquals(":d: Size of ImageData returned from Image.getImageDataAtCurrentZoom method doesn't return matches with bounds in Pixel values.", boundsAtCurrentZoom, DPIUtil.autoScaleUp(bounds));
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) SWTException(org.eclipse.swt.SWTException) ImageData(org.eclipse.swt.graphics.ImageData) Rectangle(org.eclipse.swt.graphics.Rectangle) Image(org.eclipse.swt.graphics.Image) RGB(org.eclipse.swt.graphics.RGB) Test(org.junit.Test)

Aggregations

PaletteData (org.eclipse.swt.graphics.PaletteData)57 ImageData (org.eclipse.swt.graphics.ImageData)47 RGB (org.eclipse.swt.graphics.RGB)46 Test (org.junit.Test)27 Image (org.eclipse.swt.graphics.Image)22 Point (org.eclipse.swt.graphics.Point)12 GC (org.eclipse.swt.graphics.GC)10 Color (org.eclipse.swt.graphics.Color)9 DirectColorModel (java.awt.image.DirectColorModel)8 IndexColorModel (java.awt.image.IndexColorModel)8 WritableRaster (java.awt.image.WritableRaster)8 Rectangle (org.eclipse.swt.graphics.Rectangle)8 SWTException (org.eclipse.swt.SWTException)7 BufferedImage (java.awt.image.BufferedImage)4 ColorModel (java.awt.image.ColorModel)2 PaintEvent (org.eclipse.swt.events.PaintEvent)2 PaintListener (org.eclipse.swt.events.PaintListener)2 Cursor (org.eclipse.swt.graphics.Cursor)2 Composite (org.eclipse.swt.widgets.Composite)2 Event (org.eclipse.swt.widgets.Event)2