Eclipse Community Forums - RDF feed
https://www.eclipse.org/forums/
Eclipse Community ForumsConvert AWT image to SWT image
https://www.eclipse.org/forums/index.php/mv/msg/137949/435094/#msg_435094
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.]]>Joseph2004-04-22T21:07:53-00:00Re: Convert AWT image to SWT image
https://www.eclipse.org/forums/index.php/mv/msg/137949/435099/#msg_435099
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.]]>Yannick Saillet2004-04-23T07:46:57-00:00Re: Convert AWT image to SWT image
https://www.eclipse.org/forums/index.php/mv/msg/137949/435867/#msg_435867
"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.
>]]>Cameron Jones2004-05-05T15:39:28-00:00Re: Convert AWT image to SWT image
https://www.eclipse.org/forums/index.php/mv/msg/137949/435882/#msg_435882
> 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 :-/