How to save swt.opengl image to a PNG file [message #450866] |
Fri, 18 February 2005 18:35 |
Eclipse User |
|
|
|
Originally posted by: yshen.radintl.com
Hi,
I am a newbe to swt.opengl. I want to save image to a PNG file from
opengl. I've tried the following method to save image but without success.
void saveImage()
{
int width = canvas.getSize().x;
int height = canvas.getSize().y;
// Note: originally I want to use byte[], but glReadPixels() doesn't
// support byte[]
int[] imageIntArray = new int[width*height];
// set pixel-storage mode
GL.glPixelStorei(GL.GL_PACK_ALIGNMENT,1);
GL.glReadBuffer(GL.GL_FRONT);
// glReadPixels() and glDrawPixels() of swt.opengl.win32_3.1.0 only
accept
// int[]. Is there any version which accept byte[] ???
GL.glReadPixels(0, 0, width, height, GL.GL_RGB, GL.GL_UNSIGNED_INT,
imageIntArray);
//Note: I didn't figure out how to use swt's ImageLoader to save image
when // pixel data is as int[] instead of byte[].
Iterator writers = ImageIO.getImageWritersByFormatName("png");
ImageWriter writer = (ImageWriter)writers.next();
File outImageFile = new File("E:\\downloads\\myTest.png");
try{
ImageOutputStream ios = ImageIO.createImageOutputStream(outImageFile);
writer.setOutput(ios);
BufferedImage bimg = new BufferedImage (width, height,
BufferedImage.TYPE_INT_RGB);
bimg.setRGB(0, 0, width, height, imageIntArray, 0,width);
writer.write(bimg);
ios.flush();
ios.close();
}catch (Exception ex) {}
}
The image created by the previous function is incorrect. Anyone know how
to do this? Please throw some light on this topic. Thanks.
Regards.
Yalin
|
|
|
Re: How to save swt.opengl image to a PNG file [message #451025 is a reply to message #450866] |
Mon, 21 February 2005 16:16 |
Christophe Cornu Messages: 304 Registered: July 2009 |
Senior Member |
|
|
PNG encoding is not supported (only decoding is). At the moment, you could
use BMP, assuming you have an instance of org.eclipse.swt.graphics.Image .
Assuming you are drawing to an SWT Canvas, first capture an image of your
canvas. Then save that image to a BMP file. In the snippet below, canvas is
the widget you are using Open GL on.
GC gc = new GC(canvas);
Image image = new Image(display, canvasSize.x, canvasSize.y);
gc.copyArea(image, 0, 0);
gc.dispose();
ImageData data = image.getImageData();
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] {data};
loader.save("d:/tmp/snap.bmp", SWT.IMAGE_BMP);
image.dispose();
Chris
|
|
|
Powered by
FUDForum. Page generated in 0.03337 seconds