Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Convert AWT image to SWT image
Convert AWT image to SWT image [message #435094] Thu, 22 April 2004 17:07 Go to next message
Joseph is currently offline Joseph
Messages: 4
Registered: July 2009
Junior Member
Hi,

I have seen tips on converting SWT image to AWT image, but still don't
know how to do the reverse conversion.

One way I can think of is to encode a SWT image as a JPEG and then
decode the ByteArry into a SWT ImageData. How can I do it better?
Thanks in advance.
Re: Convert AWT image to SWT image [message #435099 is a reply to message #435094] Fri, 23 April 2004 03:46 Go to previous messageGo to next message
Yannick Saillet is currently offline Yannick Saillet
Messages: 24
Registered: July 2009
Junior Member
Hi Joseph,

if your AWT image is a BufferedImage, you can extract the RGB values of the
pixels into an array of int, by using public int [] getRGB(.....)
Then you create a new ImageData having the same dimensions and a direct
color model, and with a simple loop you can copy the pixel values from the
array of int to the ImageData.

I have just written an article explaining how to do this. The article will
be published on IBM developerWorks. Unfortunately it is still in review and
it may take a couple of weeks before it is public.

Yannick

Joseph wrote:

> Hi,
>
> I have seen tips on converting SWT image to AWT image, but still don't
> know how to do the reverse conversion.
>
> One way I can think of is to encode a SWT image as a JPEG and then
> decode the ByteArry into a SWT ImageData. How can I do it better?
> Thanks in advance.
Re: Convert AWT image to SWT image [message #435867 is a reply to message #435099] Wed, 05 May 2004 11:39 Go to previous messageGo to next message
Cameron Jones is currently offline Cameron Jones
Messages: 60
Registered: July 2009
Member
any chance of a snippet until the article comes out?

"Yannick Saillet" <ysaillet@de.ibm.com> wrote in message
news:c6aha5$vvt$1@eclipse.org...
> Hi Joseph,
>
> if your AWT image is a BufferedImage, you can extract the RGB values of
the
> pixels into an array of int, by using public int [] getRGB(.....)
> Then you create a new ImageData having the same dimensions and a direct
> color model, and with a simple loop you can copy the pixel values from the
> array of int to the ImageData.
>
> I have just written an article explaining how to do this. The article will
> be published on IBM developerWorks. Unfortunately it is still in review
and
> it may take a couple of weeks before it is public.
>
> Yannick
>
> Joseph wrote:
>
> > Hi,
> >
> > I have seen tips on converting SWT image to AWT image, but still don't
> > know how to do the reverse conversion.
> >
> > One way I can think of is to encode a SWT image as a JPEG and then
> > decode the ByteArry into a SWT ImageData. How can I do it better?
> > Thanks in advance.
>
Re: Convert AWT image to SWT image [message #435882 is a reply to message #435094] Wed, 05 May 2004 15:33 Go to previous message
Stefan Zeiger is currently offline Stefan Zeiger
Messages: 102
Registered: July 2009
Senior Member
Joseph wrote:

> I have seen tips on converting SWT image to AWT image, but still
> don't know how to do the reverse conversion.

This works for me:

private static Image makeSWTImage(Display display, java.awt.Image ai)
throws Exception
{
int width = ai.getWidth(null);
int height = ai.getHeight(null);
BufferedImage bufferedImage =
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.drawImage(ai, 0, 0, null);
g2d.dispose();
int[] data =
((DataBufferInt)bufferedImage.getData().getDataBuffer())
.getData();
ImageData imageData =
new ImageData(width, height, 24,
new PaletteData(0xFF0000, 0x00FF00, 0x0000FF));
imageData.setPixels(0, 0, data.length, data, 0);
Image swtImage = new Image(display, imageData);
return swtImage;
}

Unfortunately it's quite slow because the image data has to be copied
instead of sharing a buffer between SWT and AWT. The latter would
require native code. Even without a shared buffer, loading a (JPEG)
image with AWT and converting it to a SWT image is faster than loading
it directly with SWT :-/

--
Stefan Zeiger http://www.szeiger.de http://www.novocode.com
My SWT controls: http://www.novocode.com/swt
Previous Topic:confused about Display.asyncExec()
Next Topic:Does the class A implement the interface I, if A extends class B, B implements I
Goto Forum:
  


Current Time: Tue Jul 29 02:40:45 EDT 2014

Powered by FUDForum. Page generated in 0.02138 seconds