Search in sources :

Example 46 with ImageData

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

the class Test_org_eclipse_swt_graphics_Image method test_setBackgroundLorg_eclipse_swt_graphics_Color.

@Test
public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
    if (SwtTestUtil.isGTK) {
        // TODO Fix GTK failure.
        if (SwtTestUtil.verbose) {
            System.out.println("Excluded test_setBackgroundLorg_eclipse_swt_graphics_Color(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_Image)");
        }
        return;
    }
    Image image = new Image(display, 10, 10);
    try {
        image.setBackground(null);
        fail("No exception thrown for color == null");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for color == null", SWT.ERROR_NULL_ARGUMENT, e);
    } finally {
        image.dispose();
    }
    image = new Image(display, 10, 10);
    Color color = new Color(display, 255, 255, 255);
    color.dispose();
    try {
        image.setBackground(color);
        fail("No exception thrown for disposed color");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for disposed color", SWT.ERROR_INVALID_ARGUMENT, e);
    } finally {
        image.dispose();
    }
    image = new Image(display, 10, 10);
    image.dispose();
    color = new Color(display, 255, 255, 255);
    try {
        image.setBackground(color);
        fail("No exception thrown for disposed image");
    } catch (SWTException e) {
        assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
    } finally {
        color.dispose();
    }
    // this image does not have a transparent pixel by default so setBackground has no effect
    image = new Image(display, 10, 10);
    image.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
    color = image.getBackground();
    assertNull("background color should be null for non-transparent image", color);
    image.dispose();
    // create an image with transparency and then set the background color
    ImageData imageData = new ImageData(10, 10, 2, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255), new RGB(50, 100, 150)));
    // transparent pixel is currently black
    imageData.transparentPixel = 0;
    image = new Image(display, imageData);
    image.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
    color = image.getBackground();
    assertEquals("background color should have been set to green", display.getSystemColor(SWT.COLOR_GREEN), color);
    image.dispose();
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) SWTException(org.eclipse.swt.SWTException) ImageData(org.eclipse.swt.graphics.ImageData) Color(org.eclipse.swt.graphics.Color) Image(org.eclipse.swt.graphics.Image) RGB(org.eclipse.swt.graphics.RGB) Test(org.junit.Test)

Example 47 with ImageData

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

the class Test_org_eclipse_swt_graphics_ImageData method test_getPixelII.

@Test
public void test_getPixelII() {
    int value;
    assertEquals(":a:", 0, imageData.getPixel(0, 0));
    value = 0xAA;
    imageData.setPixel(0, 0, value);
    assertEquals(":b:", value, imageData.getPixel(0, 0));
    // exception cases
    try {
        imageData.getPixel(-1, 1);
        fail("No exception thrown for x out of bounds");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for x out of bounds", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    try {
        imageData.getPixel(IMAGE_DIMENSION, 1);
        fail("No exception thrown for x out of bounds");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for x out of bounds", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    try {
        imageData.getPixel(0, -1);
        fail("No exception thrown for y out of bounds");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for y out of bounds", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    try {
        imageData.getPixel(0, IMAGE_DIMENSION);
        fail("No exception thrown for y out of bounds");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for y out of bounds", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    int width = 3;
    int height = 3;
    int depth = 4;
    byte pixelValue = 1;
    byte[] data = { (byte) ((pixelValue << 4) + pixelValue), (byte) (pixelValue << 4), (byte) ((pixelValue << 4) + pixelValue), (byte) (pixelValue << 4), (byte) ((pixelValue << 4) + pixelValue), (byte) (pixelValue << 4) };
    imageData = new ImageData(width, height, depth, new PaletteData(new RGB(0, 0, 255), new RGB(111, 111, 111)), 1, data);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int pixel = imageData.getPixel(x, y);
            assertEquals("Bad pixel data", pixelValue, pixel);
        }
    }
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData) RGB(org.eclipse.swt.graphics.RGB) Test(org.junit.Test)

Example 48 with ImageData

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

the class Test_org_eclipse_swt_graphics_ImageData method test_getTransparencyMask.

@Test
public void test_getTransparencyMask() {
    try (InputStream stream = getClass().getResourceAsStream(SwtTestUtil.transparentImageFilenames[0])) {
        Image image = new Image(Display.getDefault(), stream);
        imageData = image.getImageData();
        ImageData maskData = imageData.getTransparencyMask();
        assertNotNull(":b:", maskData);
        image.dispose();
    } catch (IOException e) {
    }
// Bug 71472 - transparency mask should be null
/*	image = new Image(Display.getDefault(), getClass().getResourceAsStream(imageFilenames[0] + '.' + imageFormats[imageFormats.length-1]));
	imageData = image.getImageData();
	maskData = imageData.getTransparencyMask();
	assertNull(":c:", maskData);
*/
}
Also used : InputStream(java.io.InputStream) ImageData(org.eclipse.swt.graphics.ImageData) IOException(java.io.IOException) Image(org.eclipse.swt.graphics.Image) Test(org.junit.Test)

Example 49 with ImageData

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

the class Test_org_eclipse_swt_graphics_ImageData method test_getPixelsIII$II.

@Test
public void test_getPixelsIII$II() {
    final int SIZE = 20;
    final int GET_WIDTH = 10;
    final int OFFSET = 10;
    int[] pixelData = new int[SIZE];
    // test 1 bit
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 1, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255)));
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        assertEquals(":a:", 0, pixelData[i]);
    }
    int[] values = new int[] { 0x1, 0x1, 0x1, 0x1, 0x1 };
    imageData.setPixels(0, 1, values.length, values, 0);
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        if (i < OFFSET) {
            assertEquals(":b:", 0, pixelData[i]);
        } else if (i < OFFSET + values.length) {
            assertEquals(":c:", values[i - OFFSET], pixelData[i]);
        } else if (i < OFFSET + GET_WIDTH) {
            assertEquals(":d:", 0, pixelData[i]);
        }
    }
    // test 2 bit
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 2, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255)));
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        assertEquals(":e:", 0, pixelData[i]);
    }
    values = new int[] { 0x1, 0x2, 0x3, 0x2, 0x1 };
    imageData.setPixels(0, 1, values.length, values, 0);
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        if (i < OFFSET) {
            assertEquals(":f:", 0, pixelData[i]);
        } else if (i < OFFSET + values.length) {
            assertEquals(":g:", values[i - OFFSET], pixelData[i]);
        } else if (i < OFFSET + GET_WIDTH) {
            assertEquals(":h:", 0, pixelData[i]);
        }
    }
    // test 4 bit
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 4, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255)));
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        assertEquals(":i:", 0, pixelData[i]);
    }
    values = new int[] { 0x1, 0x2, 0x3, 0x4, 0xF };
    imageData.setPixels(0, 1, values.length, values, 0);
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        if (i < OFFSET) {
            assertEquals(":j:", 0, pixelData[i]);
        } else if (i < OFFSET + values.length) {
            assertEquals(":k:", values[i - OFFSET], pixelData[i]);
        } else if (i < OFFSET + GET_WIDTH) {
            assertEquals(":l:", 0, pixelData[i]);
        }
    }
    // test 8 bit
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 8, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255)));
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        assertEquals(":m:", 0, pixelData[i]);
    }
    values = new int[] { 0x1, 0x2, 0x3, 0xF, 0xFF };
    imageData.setPixels(0, 1, values.length, values, 0);
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        if (i < OFFSET) {
            assertEquals(":n:", 0, pixelData[i]);
        } else if (i < OFFSET + values.length) {
            assertEquals(":o:", values[i - OFFSET], pixelData[i]);
        } else if (i < OFFSET + GET_WIDTH) {
            assertEquals(":p:", 0, pixelData[i]);
        }
    }
    // test 16 bit
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 16, new PaletteData(0xF800, 0x7E0, 0x1F));
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        assertEquals(":q:", 0, pixelData[i]);
    }
    values = new int[] { 0, 0x2, 0xF, 0xFF, 0xFFAA };
    imageData.setPixels(0, 1, values.length, values, 0);
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        if (i < OFFSET) {
            assertEquals(":r:", 0, pixelData[i]);
        } else if (i < OFFSET + values.length) {
            assertEquals(":s:", values[i - OFFSET], pixelData[i]);
        } else if (i < OFFSET + GET_WIDTH) {
            assertEquals(":t:", 0, pixelData[i]);
        }
    }
    // test 24 bit
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 24, new PaletteData(0xFF0000, 0xFF00, 0xFF));
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        assertEquals(":u:", 0, pixelData[i]);
    }
    values = new int[] { 0, 0xFF, 0xFFAA, 0xFF00AA };
    imageData.setPixels(0, 1, values.length, values, 0);
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        if (i < OFFSET) {
            assertEquals(":v:", 0, pixelData[i]);
        } else if (i < OFFSET + values.length) {
            assertEquals(":w:", values[i - OFFSET], pixelData[i]);
        } else if (i < OFFSET + GET_WIDTH) {
            assertEquals(":x:", 0, pixelData[i]);
        }
    }
    // test 32 bit
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 32, new PaletteData(0xFF000000, 0xFF00, 0xFF));
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        assertEquals(":y:", 0, pixelData[i]);
    }
    values = new int[] { 0, 0xFF, 0xFFAA, 0xFF00AA00 };
    imageData.setPixels(0, 1, values.length, values, 0);
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        if (i < OFFSET) {
            assertEquals(":z:", 0, pixelData[i]);
        } else if (i < OFFSET + values.length) {
            assertEquals(":aa:", values[i - OFFSET], pixelData[i]);
        } else if (i < OFFSET + GET_WIDTH) {
            assertEquals(":ab:", 0, pixelData[i]);
        }
    }
    // exception cases
    try {
        imageData.getPixels(0, 1, GET_WIDTH * GET_WIDTH, pixelData, OFFSET);
        fail("No exception thrown for getWidth out of bounds");
    } catch (IndexOutOfBoundsException e) {
    }
    try {
        imageData.getPixels(0, 1, GET_WIDTH, (int[]) null, OFFSET);
        fail("No exception thrown for pixels == null");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for pixels == null", SWT.ERROR_NULL_ARGUMENT, e);
    }
    try {
        imageData.getPixels(-1, 1, GET_WIDTH, pixelData, OFFSET);
        fail("No exception thrown for x out of bounds");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for x out of bounds", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    try {
        imageData.getPixels(IMAGE_DIMENSION, 1, GET_WIDTH, pixelData, OFFSET);
        fail("No exception thrown for x out of bounds");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for x out of bounds", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    try {
        imageData.getPixels(0, -1, GET_WIDTH, pixelData, OFFSET);
        fail("No exception thrown for y out of bounds");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for y out of bounds", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    try {
        imageData.getPixels(0, IMAGE_DIMENSION, GET_WIDTH, pixelData, OFFSET);
        fail("No exception thrown for y out of bounds");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for y out of bounds", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    try {
        imageData.getPixels(0, 1, -1, pixelData, OFFSET);
        fail("No exception thrown for getWidth < 0");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for getWidth < 0", SWT.ERROR_INVALID_ARGUMENT, e);
    }
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData) RGB(org.eclipse.swt.graphics.RGB) Test(org.junit.Test)

Example 50 with ImageData

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

the class Test_org_eclipse_swt_graphics_ImageData method test_ConstructorIIILorg_eclipse_swt_graphics_PaletteDataI$B.

@Test
public void test_ConstructorIIILorg_eclipse_swt_graphics_PaletteDataI$B() {
    byte[] validData = new byte[] { 0, 0x4f, 0x4f, 0 };
    try {
        new ImageData(-1, 1, 1, new PaletteData(new RGB(0, 0, 0)), 1, validData);
        fail("No exception thrown for width < 0");
    } catch (IllegalArgumentException e) {
    }
    try {
        new ImageData(1, -1, 1, new PaletteData(new RGB(0, 0, 0)), 1, validData);
        fail("No exception thrown for height < 0");
    } catch (IllegalArgumentException e) {
    }
    try {
        new ImageData(1, 1, 1, null, 0, validData);
        fail("No exception thrown for paletteData == null");
    } catch (IllegalArgumentException e) {
    }
    try {
        new ImageData(1, 1, 1, new PaletteData(new RGB(0, 0, 0)), 1, null);
        fail("No exception thrown for data == null");
    } catch (IllegalArgumentException e) {
    }
    try {
        new ImageData(1, 1, 1, new PaletteData(new RGB(0, 0, 0)), 1, new byte[] {});
        fail("No exception thrown for data array too small");
    } catch (IllegalArgumentException e) {
    }
    try {
        new ImageData(1, 1, 16, new PaletteData(new RGB(0, 0, 0)), 1, new byte[] { 0x4f });
        fail("No exception thrown for data array too small");
    } catch (IllegalArgumentException e) {
    }
    try {
        new ImageData(1, 1, 32, new PaletteData(new RGB(0, 0, 0)), 1, new byte[] { 0x4f, 0x4f });
        fail("No exception thrown for data array too small");
    } catch (IllegalArgumentException e) {
    }
    try {
        new ImageData(2, 2, 8, new PaletteData(new RGB(0, 0, 0)), 1, new byte[] { 0x4f, 0x4f, 0x4f });
        fail("No exception thrown for data array too small");
    } catch (IllegalArgumentException e) {
    }
    try {
        new ImageData(1, 1, 3, new PaletteData(new RGB(0, 0, 0)), 1, validData);
        fail("No exception thrown for unsupported depth");
    } catch (IllegalArgumentException e) {
    }
    // verify all valid depths
    int[] validDepths = { 1, 2, 4, 8, 16, 24, 32 };
    for (int i = 0; i < validDepths.length; i++) {
        new ImageData(1, 1, validDepths[i], new PaletteData(new RGB(0, 0, 0)), 1, validData);
    }
    // verify no divide by zero exception if scanlinePad == 0
    try {
        new ImageData(1, 1, 8, new PaletteData(new RGB(0, 0, 0)), 0, validData);
        fail("No exception thrown for scanlinePad == 0");
    } catch (IllegalArgumentException e) {
    }
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData) RGB(org.eclipse.swt.graphics.RGB) Test(org.junit.Test)

Aggregations

ImageData (org.eclipse.swt.graphics.ImageData)132 Image (org.eclipse.swt.graphics.Image)78 PaletteData (org.eclipse.swt.graphics.PaletteData)38 RGB (org.eclipse.swt.graphics.RGB)33 Point (org.eclipse.swt.graphics.Point)32 Test (org.junit.Test)26 InputStream (java.io.InputStream)24 GC (org.eclipse.swt.graphics.GC)19 IOException (java.io.IOException)18 SWTException (org.eclipse.swt.SWTException)14 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)13 Rectangle (org.eclipse.swt.graphics.Rectangle)12 Color (org.eclipse.swt.graphics.Color)11 ImageLoader (org.eclipse.swt.graphics.ImageLoader)11 Display (org.eclipse.swt.widgets.Display)11 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Cursor (org.eclipse.swt.graphics.Cursor)7 Composite (org.eclipse.swt.widgets.Composite)7 GradientPaint (java.awt.GradientPaint)5 URL (java.net.URL)5