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;
}
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();
}
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));
}
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);
}
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));
}
Aggregations