Search in sources :

Example 51 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$BI.

@Test
public void test_getPixelsIII$BI() {
    final int SIZE = 20;
    final int GET_WIDTH = 10;
    final int OFFSET = 10;
    byte[] pixelData = new byte[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]);
    }
    byte[] values = new byte[] { 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 byte[] { 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 byte[] { 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 byte[] { 0x1, 0x2, 0x3, 0xF, (byte) 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]);
        }
    }
    // 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, (byte[]) 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);
    }
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 32, new PaletteData(0xFF0000, 0xFF00, 0xFF));
    try {
        imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
        fail("No exception thrown for invalid depth");
    } catch (SWTException e) {
        assertSWTProblem("Incorrect exception thrown for invalid depth", SWT.ERROR_UNSUPPORTED_DEPTH, e);
    }
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) SWTException(org.eclipse.swt.SWTException) ImageData(org.eclipse.swt.graphics.ImageData) RGB(org.eclipse.swt.graphics.RGB) Test(org.junit.Test)

Example 52 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_getRGBs.

@Test
public void test_getRGBs() {
    assertNull(":a:", imageData.getRGBs());
    RGB[] rgbs = new RGB[] { new RGB(0, 0, 0), new RGB(255, 255, 255) };
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 8, new PaletteData(rgbs));
    assertArrayEquals(":b:", rgbs, imageData.getRGBs());
}
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 53 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_clone.

@Test
public void test_clone() {
    try (InputStream stream = SwtTestUtil.class.getResourceAsStream(SwtTestUtil.imageFilenames[0] + "." + SwtTestUtil.imageFormats[0])) {
        ImageData data1 = new ImageData(stream);
        ImageData data2 = (ImageData) data1.clone();
        // imageData does not implement an equals(Object) method
        assertEquals(":a:", data1.alpha, data2.alpha);
        assertArrayEquals(":b:", data1.alphaData, data2.alphaData);
        assertEquals(":c:", data1.bytesPerLine, data2.bytesPerLine);
        assertArrayEquals(":d:", data1.data, data2.data);
        assertEquals(":e:", data1.delayTime, data2.delayTime);
        assertEquals(":f:", data1.depth, data2.depth);
        assertEquals(":g:", data1.disposalMethod, data2.disposalMethod);
        assertEquals(":h:", data1.height, data2.height);
        assertArrayEquals(":i:", data1.maskData, data2.maskData);
        assertEquals(":j:", data1.maskPad, data2.maskPad);
        assertEquals(":k:", data1.palette, data2.palette);
        assertEquals(":l:", data1.scanlinePad, data2.scanlinePad);
        assertEquals(":m:", data1.transparentPixel, data2.transparentPixel);
        assertEquals(":n:", data1.type, data2.type);
        assertEquals(":o:", data1.width, data2.width);
        assertEquals(":p:", data1.x, data2.x);
        assertEquals(":q:", data1.y, data2.y);
    } catch (IOException e) {
    }
}
Also used : InputStream(java.io.InputStream) ImageData(org.eclipse.swt.graphics.ImageData) IOException(java.io.IOException) Test(org.junit.Test)

Example 54 with ImageData

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

the class PaintExample method initResources.

/**
 * Loads the image resources.
 */
public void initResources() {
    final Class<PaintExample> clazz = PaintExample.class;
    if (resourceBundle != null) {
        try {
            for (Tool tool2 : tools) {
                Tool tool = tool2;
                String id = tool.group + '.' + tool.name;
                try (InputStream sourceStream = clazz.getResourceAsStream(getResourceString(id + ".image"))) {
                    ImageData source = new ImageData(sourceStream);
                    ImageData mask = source.getTransparencyMask();
                    tool.image = new Image(null, source, mask);
                }
            }
            return;
        } catch (Throwable t) {
        }
    }
    String error = (resourceBundle != null) ? getResourceString("error.CouldNotLoadResources") : "Unable to load resources";
    freeResources();
    throw new RuntimeException(error);
}
Also used : InputStream(java.io.InputStream) ImageData(org.eclipse.swt.graphics.ImageData) Image(org.eclipse.swt.graphics.Image)

Example 55 with ImageData

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

the class ImageDataUtil method convertImageData.

public static ImageData convertImageData(ImageData source) {
    PaletteData palette = new PaletteData(0xff0000, 0xff00, 0xff);
    ImageData newSource = new ImageData(source.width, source.height, 24, palette);
    ImageDataUtil.blit(1, source.data, source.depth, source.bytesPerLine, (source.depth != 16) ? MSB_FIRST : LSB_FIRST, 0, 0, source.width, source.height, source.palette.redMask, source.palette.greenMask, source.palette.blueMask, 255, null, 0, 0, 0, newSource.data, newSource.depth, newSource.bytesPerLine, (newSource.depth != 16) ? MSB_FIRST : LSB_FIRST, 0, 0, newSource.width, newSource.height, newSource.palette.redMask, newSource.palette.greenMask, newSource.palette.blueMask, false, true);
    return newSource;
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData)

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