org.eclipse.swt.graphics.PaletteData Java Examples

The following examples show how to use org.eclipse.swt.graphics.PaletteData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeviceSocketClient.java    From AndroidRobot with Apache License 2.0 6 votes vote down vote up
private static ImageData getImageData2(BufferedImage bufferedImage){
    DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
    //System.out.println("robot:" +colorModel.getRedMask() + " "+colorModel.getGreenMask() + " "+colorModel.getBlueMask());   
    PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel
            .getBlueMask());
    ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel
            .getPixelSize(), palette);
    WritableRaster raster = bufferedImage.getRaster();
    int[] pixelArray = new int[3];
    for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
            raster.getPixel(x, y, pixelArray);
            int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
            data.setPixel(x, y, pixel);
        }
    }
    return data;
}
 
Example #2
Source File: SetCheckPoint2.java    From AndroidRobot with Apache License 2.0 6 votes vote down vote up
public ImageData getImageData(BufferedImage bufferedImage) {
    DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
    PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
        colorModel.getBlueMask());
    ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
        colorModel.getPixelSize(), palette);
    WritableRaster raster = bufferedImage.getRaster();
    int[] pixelArray = new int[3];
    for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
            raster.getPixel(x, y, pixelArray);
            int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
            data.setPixel(x, y, pixel);
        }
    }
    return data;
}
 
Example #3
Source File: SVGFigure.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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.
 */
protected static org.eclipse.swt.graphics.Image toSWT(final Device device, final BufferedImage awtImage) {
    // We can force bitdepth to be 24 bit because BufferedImage getRGB
    // allows us to always retrieve 24 bit data regardless of source color depth.
    final PaletteData palette = new PaletteData(0xFF0000, 0xFF00, 0xFF);
    final ImageData swtImageData = new ImageData(awtImage.getWidth(), awtImage.getHeight(), 24, palette);
    // Ensure scansize is aligned on 32 bit.
    final int scansize = (awtImage.getWidth() * 3 + 3) * 4 / 4;
    final WritableRaster alphaRaster = awtImage.getAlphaRaster();
    final byte[] alphaBytes = new byte[awtImage.getWidth()];
    for (int y = 0; y < awtImage.getHeight(); y++) {
        final int[] buff = awtImage.getRGB(0, y, awtImage.getWidth(), 1, null, 0, scansize);
        swtImageData.setPixels(0, y, awtImage.getWidth(), buff, 0);
        if (alphaRaster != null) {
            final 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 new org.eclipse.swt.graphics.Image(device, swtImageData);
}
 
Example #4
Source File: ImageServiceBean.java    From dawnsci with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Clones everything apart from the data, mask and palette.
 */
public ImageServiceBean  clone() {
	ImageServiceBean ret = new ImageServiceBean();
	
	ret.min             = (min == null) ? null : min.doubleValue();
	ret.max             = (max == null) ? null : max.doubleValue();
	ret.lo              = lo;
	ret.hi              = hi;
	ret.alpha           = alpha;
	ret.histogramType   = histogramType;
	ret.logColorScale   = logColorScale;
	ret.logOffset       = logOffset;
	ret.maximumCutBound = cloneBound(maximumCutBound);
	ret.minimumCutBound = cloneBound(minimumCutBound);
	ret.nanBound        = cloneBound(nanBound);
	ret.origin          = origin;
	
	if (getPalette()!=null) {
	    ret.palette = new PaletteData(getPalette().getRGBs());
	}
	return ret;
}
 
Example #5
Source File: PatternImageEditorDialog.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static Image createImageFromPattern( PatternImage patternImage )
{
	Device device = Display.getCurrent( );

	PaletteData paletteData = new PaletteData( 0xFF00,
			0xFF0000,
			0xFF000000 );
	byte[] data = PatternImageUtil.createImageData( patternImage,
			ByteColorModel.BGRA );

	ImageData imageData = new ImageData( 8, 8, 32, paletteData, 4, data );

	return new Image( device, imageData );
}
 
Example #6
Source File: CSSColorsUI.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public static Image toImage(String color, int height, int width)
{
	RGB actualColor = toRGB(color);
	PaletteData paletteData = new PaletteData(new RGB[] { actualColor, new RGB(0, 0, 0) });
	ImageData imageData = new ImageData(16, 16, 1, paletteData);
	return new Image(Display.getDefault(), imageData);
}
 
Example #7
Source File: SliceRequest.java    From dawnsci with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Make 256 level grayscale palette.
 */
public static PaletteData makeGrayScalePalette() {
	RGB grayscale[] = new RGB[256];
	for (int i = 0; i < 256; i++) {
		grayscale[i] = new RGB(i, i, i);
	}
	return new PaletteData(grayscale);
}
 
Example #8
Source File: SWTImageUtils.java    From dawnsci with Eclipse Public License 1.0 5 votes vote down vote up
static private ImageData createImageFromDataset(Dataset a, PaletteData paletteData) throws Exception {
	final IImageService iservice = (IImageService)PlatformUI.getWorkbench().getService(IImageService.class);
	ImageServiceBean ibean = new ImageServiceBean();
	ibean.setImage(a);
	ibean.setPalette(paletteData);
	return iservice.getImageData(ibean);
}
 
Example #9
Source File: ImageServiceMock.java    From dawnsci with Eclipse Public License 1.0 5 votes vote down vote up
static PaletteData makeJetPalette() {
	RGB jet[] = new RGB[256];
	
	int nb = 256;

	for (int i = 0; i < nb; i++) {
		
		double value = (double)i/(double)255;

		double outBlue = 0;
		if (value <= 0.1) {outBlue  =  5*value + 0.5;}
		if (value > 0.1 && value <= 1.0/3.0 ) {outBlue  =  1;}
		if (value >1.0/3.0 && value <= 1.0/2.0) {outBlue  =  -6*value +3;}
		
		double outGreen = 0;
		if (value > 1.0/3.0 && value < 2.0/3.0  ) {outGreen = 1;}
		if (value <= 1.0/3.0 && value >= 1.0/8.0) {outGreen = 24.0/5*value - 0.6;}
		if (value >= 2.0/3.0 && value <= 7.0/8.0) {outGreen = -24.0/5*value + 4.2;}
		
		double outRed = 0;
		if (value >= 0.9) {outRed = -5*value +5.5;}
		if (value > 2.0/3.0 && value <= 0.9 ) {outRed = 1;}
		if (value >=1.0/2.0 && value <= 2.0/3.0 ) {outRed = 6*value -3;}
		
		jet[i] = new RGB((int)(outRed*255),
				(int)(outGreen*255),
				(int)(outBlue*255));

	}
	return new PaletteData(jet);
}
 
Example #10
Source File: SurfaceExample.java    From dawnsci with Eclipse Public License 1.0 5 votes vote down vote up
public void createExampleContent(Composite parent) {
try {
	// We create a basic plot
	system.createPlotPart(parent, "Image Example", getViewSite().getActionBars(), PlotType.IMAGE, this);

	// We read an image
	final File loc = new File(BundleUtils.getBundleLocation(Activator.PLUGIN_ID), getFileName());
	final IDataset image = service.getDataset(loc.getAbsolutePath(), new IMonitor.Stub());
	// NOTE IMonitor is an alternative to IProgressMonitor which cannot be seen in the data layer.
	
	// We plot the image
	system.setPlotType(PlotType.SURFACE); // Set to a 3D plot type.
	
	ISurfaceTrace surface = system.createSurfaceTrace("Example surface");
	surface.setData(image, null);
	// NOTE We are viewing a window of the data. The user can 
	// open a tool from the toolbar to move around this window.
	surface.setWindow(new RectangularROI(300,300,600,600,0), false, null);
	
	// Let's make it something colorful!
	final IPaletteService pservice = Examples.getCurrent().getPaletteService();
	final PaletteData     pData    = pservice.getDirectPaletteData("NCD");
	surface.setPaletteData(pData); 
	surface.setMax(10);
	surface.setMin(0);
	
	system.addTrace(surface);
	
	
} catch (Throwable ne) {
	ne.printStackTrace(); // Or your favourite logging.
}
  }
 
Example #11
Source File: AbstractSortableHeader.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
private ImageData getEmptyImageData() {
	final RGB whiteRGB = new RGB(255, 255, 255); // The transparancy color
	final RGB blackRGB = new RGB(0, 0, 0);

	PaletteData palette = new PaletteData(new RGB[] { whiteRGB, blackRGB });
	ImageData imageData = new ImageData(width, height, 1, palette);
	int whitePixel = imageData.palette.getPixel(whiteRGB);
	imageData.transparentPixel = whitePixel;
	return imageData;
}
 
Example #12
Source File: SwtRendererImpl.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private Image createImageFromPattern( PatternImage patternImage )
{
	Device device = ( (SwtDisplayServer) _ids ).getDevice( );

	PaletteData paletteData = new PaletteData( 0xFF00, 0xFF0000, 0xFF000000 );
	byte[] data = PatternImageUtil.createImageData( patternImage,
			ByteColorModel.BGRA );

	ImageData imageData = new ImageData( 8, 8, 32, paletteData, 4, data );

	return new Image( device, imageData );
}
 
Example #13
Source File: BonitaRulerGridPropertySection.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see org.eclipse.jface.resource.CompositeImageDescriptor#drawCompositeImage(int,
 *      int)
 */
protected void drawCompositeImage(int width, int height) {

	// draw the thin color bar underneath
	if (rgb != null) {
		ImageData colorBar = new ImageData(width, height / 5, 1,

				new PaletteData(new RGB[] {rgb}));
		drawImage(colorBar, 0, height - height / 5);

	}
	// draw the base image
	drawImage(basicImgData, 0, 0);
}
 
Example #14
Source File: CustomColorPalettePopup.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see org.eclipse.jface.resource.ImageDescriptor#getImageData()
 */
public ImageData getImageData() {
	ImageData data = new ImageData(ICON_SIZE.x, ICON_SIZE.y, 1,
		new PaletteData(new RGB[] {rgb, OUTLINE_COLOR}));

	for (int i = 0; i < ICON_SIZE.y; i++)
		data.setPixel(0, i, 1);
	for (int i = 0; i < ICON_SIZE.y; i++)
		data.setPixel(ICON_SIZE.x - 1, i, 1);
	for (int i = 0; i < ICON_SIZE.x; i++)
		data.setPixel(i, 0, 1);
	for (int i = 0; i < ICON_SIZE.x; i++)
		data.setPixel(i, ICON_SIZE.y - 1, 1);
	return data;
}
 
Example #15
Source File: ForkedColorsAndFontsPropertySection.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @see org.eclipse.jface.resource.CompositeImageDescriptor#drawCompositeImage(int,
 *      int)
 */
protected void drawCompositeImage(int width, int height) {

	// draw the thin color bar underneath
	if (rgb != null) {
		ImageData colorBar = new ImageData(width, height / 5, 1,
		
			new PaletteData(new RGB[] {rgb}));
		drawImage(colorBar, 0, height - height / 5);
		
	}
	// draw the base image
	drawImage(basicImgData, 0, 0);
}
 
Example #16
Source File: Pics.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ImageData getImageData() {
    InputStream resourceAsStream = Pics.class.getResourceAsStream(file);
    if (resourceAsStream != null) {
        return new ImageData(resourceAsStream);
    }
    return new ImageData(1, 1, 16, new PaletteData(new RGB[] {}));
}
 
Example #17
Source File: ViewList.java    From arx with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a transparent image
 * @param display
 * @param width
 * @param height
 * @return
 */
private Image getTransparentImage(Display display, int width, int height) {
    ImageData imData = new ImageData(width,
                                     height,
                                     24,
                                     new PaletteData(0xff0000,
                                                     0x00ff00,
                                                     0x0000ff));
    imData.setAlpha(0, 0, 0);
    Arrays.fill(imData.alphaData, (byte) 0);
    return new Image(display, imData);
}
 
Example #18
Source File: SwtUniversalImage.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Converts BufferedImage to SWT/Image with alpha channel.
 */
protected Image swing2swt( Device device, BufferedImage img ) {
  PaletteData palette = new PaletteData( 0xFF0000, 0xFF00, 0xFF );
  ImageData data = new ImageData( img.getWidth(), img.getHeight(), 32, palette );
  for ( int y = 0; y < data.height; y++ ) {
    for ( int x = 0; x < data.width; x++ ) {
      int rgba = img.getRGB( x, y );
      int rgb = palette.getPixel( new RGB( ( rgba >> 16 ) & 0xFF, ( rgba >> 8 ) & 0xFF, rgba & 0xFF ) );
      int a = ( rgba >> 24 ) & 0xFF;
      data.setPixel( x, y, rgb );
      data.setAlpha( x, y, a );
    }
  }
  return new Image( device, data );
}
 
Example #19
Source File: SwtUniversalImage.java    From hop with Apache License 2.0 5 votes vote down vote up
/**
 * Converts BufferedImage to SWT/Image with alpha channel.
 */
protected Image swing2swt( Device device, BufferedImage img ) {
  PaletteData palette = new PaletteData( 0xFF0000, 0xFF00, 0xFF );
  ImageData data = new ImageData( img.getWidth(), img.getHeight(), 32, palette );
  for ( int y = 0; y < data.height; y++ ) {
    for ( int x = 0; x < data.width; x++ ) {
      int rgba = img.getRGB( x, y );
      int rgb = palette.getPixel( new RGB( ( rgba >> 16 ) & 0xFF, ( rgba >> 8 ) & 0xFF, rgba & 0xFF ) );
      int a = ( rgba >> 24 ) & 0xFF;
      data.setPixel( x, y, rgb );
      data.setAlpha( x, y, a );
    }
  }
  return new Image( device, data );
}
 
Example #20
Source File: XYChartLegendImageProvider.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Image getLegendImage(int imageHeight, int imageWidth, @NonNull Long id) {
    /*
     * If series exists in chart, then image legend match that series. Image will
     * make sense if series exists in chart. If it does not exists, an image will
     * still be created.
     */
    OutputElementStyle appearance = fChartViewer.getSeriesStyle(id);
    BaseXYPresentationProvider presProvider = fChartViewer.getPresentationProvider2();
    RGBAColor rgb = presProvider.getColorStyleOrDefault(appearance, StyleProperties.COLOR, DEFAULT_COLOR);


    Color lineColor = new Color(Display.getDefault(), rgb.getRed(), rgb.getGreen(), rgb.getBlue());
    Color background = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);

    PaletteData palette = new PaletteData(background.getRGB(), lineColor.getRGB());
    ImageData imageData = new ImageData(imageWidth, imageHeight, 8, palette);
    imageData.transparentPixel = 0;
    Image image = new Image(Display.getDefault(), imageData);
    GC gc = new GC(image);

    gc.setBackground(background);
    gc.fillRectangle(0, 0, imageWidth, imageHeight);
    drawStyleLine(gc, lineColor, imageWidth, imageHeight, appearance);

    drawStyledDot(gc, lineColor, imageWidth, imageHeight, appearance);

    gc.dispose();
    lineColor.dispose();
    return image;
}
 
Example #21
Source File: XYChartLegendImageProvider.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Image getLegendImage(int imageHeight, int imageWidth, @NonNull Long id) {
    /*
     * If series exists in chart, then image legend match that series. Image will
     * make sense if series exists in chart. If it does not exists, an image will
     * still be created.
     */
    OutputElementStyle appearance = fChartViewer.getSeriesStyle(id);
    BaseXYPresentationProvider presProvider = fChartViewer.getPresentationProvider();
    RGBAColor rgb = presProvider.getColorStyleOrDefault(appearance, StyleProperties.COLOR, DEFAULT_COLOR);


    Color lineColor = new Color(Display.getDefault(), rgb.getRed(), rgb.getGreen(), rgb.getBlue());
    Color background = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);

    PaletteData palette = new PaletteData(background.getRGB(), lineColor.getRGB());
    ImageData imageData = new ImageData(imageWidth, imageHeight, 8, palette);
    imageData.transparentPixel = 0;
    Image image = new Image(Display.getDefault(), imageData);
    GC gc = new GC(image);

    gc.setBackground(background);
    gc.fillRectangle(0, 0, imageWidth, imageHeight);
    drawStyleLine(gc, lineColor, imageWidth, imageHeight, appearance);

    drawStyledDot(gc, lineColor, imageWidth, imageHeight, appearance);

    gc.dispose();
    lineColor.dispose();
    return image;
}
 
Example #22
Source File: TagCloud.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Calculates the extents of a word, based on its rendered image.
 */
private void calcWordExtents(final Word word, final ImageData id) {
	final int[] pixels = new int[id.width];
	final PaletteData palette = id.palette;
	Set<SmallRect> inserted = new HashSet<>();
	for (int y = 0; y < id.height; y++) {
		id.getPixels(0, y, id.width, pixels, 0);
		for (int i = 0; i < pixels.length; i++) {
			int pixel = pixels[i];
			// Extracting color values as in PaletteData.getRGB(int pixel):
			int r = pixel & palette.redMask;
			r = (palette.redShift < 0) ? r >>> -palette.redShift : r << palette.redShift;
			int g = pixel & palette.greenMask;
			g = (palette.greenShift < 0) ? g >>> -palette.greenShift : g << palette.greenShift;
			int b = pixel & palette.blueMask;
			b = (palette.blueShift < 0) ? b >>> -palette.blueShift : b << palette.blueShift;
			if (r < 250 || g < 250 || b < 250) {
				SmallRect rect = new SmallRect((i / accuracy) * accuracy, (y / accuracy) * accuracy, accuracy,
						accuracy);
				if (!inserted.contains(rect)) {
					word.tree.insert(rect, word.id);
					inserted.add(rect);
				}
				i += accuracy - 1;
			}
		}
	}
	word.tree.releaseRects();
}
 
Example #23
Source File: SWTUtil.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns a hash code for the PaletteData.
 * 
 * @param data
 *            the PaletteData
 * @return a hash code for the PaletteData.
 */
public static int hashCode(PaletteData data) {
	final int prime = 31;
	int result = 1;
	result = prime * result + (data.isDirect ? 1231 : 1237);
	result = prime * result + data.blueMask;
	result = prime * result + data.blueShift;
	result = prime * result + data.greenMask;
	result = prime * result + data.greenShift;
	result = prime * result + data.redMask;
	result = prime * result + data.redShift;
	result = prime * result + hashCode(data.colors);
	return result;
}
 
Example #24
Source File: SWTUtil.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns whether the PaletteData arguments are equivalent.
 * 
 * @param left
 *            the left PaletteData
 * @param right
 *            the right PaletteData
 * @return whether the PaletteData arguments are equivalent.
 */
public static boolean equal(PaletteData left, PaletteData right) {
	if (left == right)
		return true;
	if (left == null || right == null)
		return false;
	return left.isDirect == right.isDirect
			&& left.blueMask == right.blueMask
			&& left.blueShift == right.blueShift
			&& left.greenMask == right.greenMask
			&& left.greenShift == right.greenShift
			&& left.redMask == right.redMask
			&& left.redShift == right.redShift
			&& Util.equal(left.colors, right.colors);
}
 
Example #25
Source File: XYChartLegendImageProvider.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
@Deprecated
@Override
public Image getLegendImage(int imageHeight, int imageWidth, @NonNull String name) {
    /*
     * If series exists in chart, then image legend match that series. Image will
     * make sense if series exists in chart. If it does not exists, an image will
     * still be created.
     */
    IYAppearance appearance = fChartViewer.getSeriesAppearance(name);
    RGBAColor rgb = appearance.getColor();
    Color lineColor = new Color(Display.getDefault(), rgb.getRed(), rgb.getGreen(), rgb.getBlue());
    Color background = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);

    PaletteData palette = new PaletteData(background.getRGB(), lineColor.getRGB());
    ImageData imageData = new ImageData(imageWidth, imageHeight, 8, palette);
    imageData.transparentPixel = 0;
    Image image = new Image(Display.getDefault(), imageData);
    GC gc = new GC(image);

    gc.setBackground(background);
    gc.fillRectangle(0, 0, imageWidth, imageHeight);
    drawStyleLine(gc, lineColor, imageWidth, imageHeight, appearance.toOutputElementStyle());

    drawStyledDot(gc, lineColor, imageWidth, imageHeight, appearance.toOutputElementStyle());

    gc.dispose();
    lineColor.dispose();
    return image;
}
 
Example #26
Source File: SWTUtilTest.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
private ImageData createImageData(int size, int color, byte alpha) {
	ImageData imageData = new ImageData(size, size, 24,
			new PaletteData(0xFF0000, 0x00FF00, 0x0000FF));
	int[] pixels = new int[size];
	byte[] alphas = new byte[size];
	for (int x = 0; x < size; x++) {
		pixels[x] = color;
		alphas[x] = alpha;
	}
	for (int y = 0; y < size; y++) {
		imageData.setPixels(0, y, size, pixels, 0);
		imageData.setAlphas(0, y, size, alphas, 0);
	}
	return imageData;
}
 
Example #27
Source File: GamaColors.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the color of the icon passed in parameter (supposing it's mono-colored)
 *
 * @param create
 * @return
 */
public static GamaUIColor get(final GamaIcon icon) {
	final Image image = icon.image();
	final ImageData data = image.getImageData();
	final PaletteData palette = data.palette;
	final int pixelValue = data.getPixel(0, 0);
	return get(palette.getRGB(pixelValue));
}
 
Example #28
Source File: ImagePrintTest.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
private ImageData createImageData(int size) {
	return new ImageData(size, size, 24, new PaletteData(0xFF0000,
			0x00FF00, 0x0000FF));
}
 
Example #29
Source File: SWTGraphicUtil.java    From nebula with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @param originalImageData The ImageData to be average blurred. Transparency
 *            information will be ignored.
 * @param radius the number of radius pixels to consider when blurring image.
 * @return A blurred copy of the image data, or null if an error occured.
 * @author Nicholas Rajendram
 * @see http://www.eclipse.org/articles/article.php?file=Article-
 *      SimpleImageEffectsForSWT/index.html
 */
public static ImageData blur(final ImageData originalImageData, int radius) {

	if (radius < 1) {
		return originalImageData;
	}

	// prepare new image data with 24-bit direct palette to hold blurred
	// copy of image
	final ImageData newImageData = new ImageData(originalImageData.width, originalImageData.height, 24, new PaletteData(0xFF, 0xFF00, 0xFF0000));
	if (radius >= newImageData.height || radius >= newImageData.width) {
		radius = Math.min(newImageData.height, newImageData.width) - 1;
	}
	// initialize cache
	final ArrayList<RGB[]> rowCache = new ArrayList<>();
	// number of rows of imageData we cache
	final int cacheSize = radius * 2 + 1 > newImageData.height ? newImageData.height : radius * 2 + 1;
	int cacheStartIndex = 0; // which row of imageData the cache begins with
	for (int row = 0; row < cacheSize; row++) {
		// row data is horizontally blurred before caching
		rowCache.add(rowCache.size(), blurRow(originalImageData, row, radius));
	}

	// sum red, green, and blue values separately for averaging
	final RGB[] rowRGBSums = new RGB[newImageData.width];
	final int[] rowRGBAverages = new int[newImageData.width];
	int topSumBoundary = 0; // current top row of summed values scope
	int targetRow = 0; // row with RGB averages to be determined
	int bottomSumBoundary = 0; // current bottom row of summed values scope
	int numRows = 0; // number of rows included in current summing scope
	for (int i = 0; i < newImageData.width; i++) {
		rowRGBSums[i] = new RGB(0, 0, 0);
	}

	while (targetRow < newImageData.height) {
		if (bottomSumBoundary < newImageData.height) {
			do {
				// sum pixel RGB values for each column in our radius scope
				for (int col = 0; col < newImageData.width; col++) {
					rowRGBSums[col].red += rowCache.get(bottomSumBoundary - cacheStartIndex)[col].red;
					rowRGBSums[col].green += rowCache.get(bottomSumBoundary - cacheStartIndex)[col].green;
					rowRGBSums[col].blue += rowCache.get(bottomSumBoundary - cacheStartIndex)[col].blue;
				}
				numRows++;
				bottomSumBoundary++; // move bottom scope boundary lower
				if (bottomSumBoundary < newImageData.height && bottomSumBoundary - cacheStartIndex > radius * 2) {
					// grow cache
					rowCache.add(rowCache.size(), blurRow(originalImageData, bottomSumBoundary, radius));
				}
			} while (bottomSumBoundary <= radius); // to initialize
			// rowRGBSums at start
		}

		if (targetRow - topSumBoundary > radius) {
			// subtract values of top row from sums as scope of summed
			// values moves down
			for (int col = 0; col < newImageData.width; col++) {
				rowRGBSums[col].red -= rowCache.get(topSumBoundary - cacheStartIndex)[col].red;
				rowRGBSums[col].green -= rowCache.get(topSumBoundary - cacheStartIndex)[col].green;
				rowRGBSums[col].blue -= rowCache.get(topSumBoundary - cacheStartIndex)[col].blue;
			}
			numRows--;
			topSumBoundary++; // move top scope boundary lower
			rowCache.remove(0); // remove top row which is out of summing
			// scope
			cacheStartIndex++;
		}

		// calculate each column's RGB-averaged pixel
		for (int col = 0; col < newImageData.width; col++) {
			rowRGBAverages[col] = newImageData.palette.getPixel(new RGB(rowRGBSums[col].red / numRows, rowRGBSums[col].green / numRows, rowRGBSums[col].blue / numRows));
		}

		// replace original pixels
		newImageData.setPixels(0, targetRow, newImageData.width, rowRGBAverages, 0);
		targetRow++;
	}
	return newImageData;
}
 
Example #30
Source File: ImageLabel.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
void onPaint( PaintEvent event )
{
	Rectangle rect = getClientArea( );
	if ( rect.width == 0 || rect.height == 0 )
		return;

	Image img = image;
	Point extent = getTotalSize( img );

	GC gc = event.gc;

	// determine horizontal position
	int x = rect.x + hIndent;
	if ( align == SWT.CENTER )
	{
		x = ( rect.width - extent.x ) / 2;
	}
	if ( align == SWT.RIGHT )
	{
		x = rect.width - extent.x - hIndent;
	}

	if ( this.backgroundColor != null )
	{
		Color oldBackground = gc.getBackground( );
		gc.setBackground( backgroundColor );
		gc.fillRectangle( 0, 0, rect.width, rect.height );
		gc.setBackground( oldBackground );
	}
	else
	{
		if ( ( getStyle( ) & SWT.NO_BACKGROUND ) != 0 )
		{
			gc.setBackground( getBackground( ) );
			gc.fillRectangle( rect );
		}
	}

	// draw border
	int style = getStyle( );
	if ( ( style & SWT.SHADOW_IN ) != 0 || ( style & SWT.SHADOW_OUT ) != 0 )
	{
		paintBorder( gc, rect );
	}
	// draw the image
	if ( img != null )
	{
		Rectangle imageRect = img.getBounds( );
		if ( this.isFocusControl( ) )
		{

			ImageData data = img.getImageData( );
			PaletteData palette = new PaletteData( new RGB[]{
					this.getDisplay( )
							.getSystemColor( SWT.COLOR_WHITE )
							.getRGB( ),
					this.getDisplay( )
							.getSystemColor( SWT.COLOR_LIST_SELECTION )
							.getRGB( ),
			} );
			ImageData sourceData = new ImageData( data.width,
					data.height,
					1,
					palette );
			for ( int i = 0; i < data.width; i++ )
			{
				for ( int j = 0; j < data.height; j++ )
				{
					if ( data.getPixel( i, j ) != data.transparentPixel )
						sourceData.setPixel( i, j, 1 );
				}
			}

			Image highlightImage = new Image( this.getDisplay( ),
					sourceData );

			gc.drawImage( highlightImage,
					0,
					0,
					imageRect.width,
					imageRect.height,
					x,
					( rect.height - imageRect.height ) / 2,
					rect.width - 10,
					imageRect.height );
			highlightImage.dispose( );
		}
		else
			gc.drawImage( img,
					0,
					0,
					imageRect.width,
					imageRect.height,
					x,
					( rect.height - imageRect.height ) / 2,
					rect.width - 10,
					imageRect.height );
		x += imageRect.width;
	}
}