Creating an Image from a rgb byte array

E

E. Naubauer

Hello

I'm trying to convert a byte array to an image Object via
MemoryImageSource. Three bytes together are a pixel, one byte per component:

[r][g][r][g][r][g][r][g][r][g]....

[] = one byte

This is the code:

int w = 176,h = 144;
Image output = null;
byte rgbBuffer[] = <image data with length w * h>

ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ComponentColorModel cm = new ComponentColorModel(cs,new int[]
{8,8,8},false,false,ComponentColorModel.OPAQUE,DataBuffer.TYPE_BYTE);

output = Toolkit.getDefaultToolkit().createImage(new
MemoryImageSource(w, h, cm,dim, 0, w));


It works if I use a buffer of type int[] with packed RGBA values and
DataBuffer.TYPE_INT. However, with the above configuration, the image
remains black when drawn.

I'm starting to wonder if createImage actually works with interleaved
component data. I used a writable raster before, but it was awfully slow.

If anyone has a solution for this problem, please tell it.
Thanks in advance.
 
K

Knute Johnson

E. Naubauer said:
Hello

I'm trying to convert a byte array to an image Object via
MemoryImageSource. Three bytes together are a pixel, one byte per
component:

[r][g][r][g][r][g][r][g][r][g]....

[] = one byte

This is the code:

int w = 176,h = 144;
Image output = null;
byte rgbBuffer[] = <image data with length w * h>

ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ComponentColorModel cm = new ComponentColorModel(cs,new int[]
{8,8,8},false,false,ComponentColorModel.OPAQUE,DataBuffer.TYPE_BYTE);

output = Toolkit.getDefaultToolkit().createImage(new
MemoryImageSource(w, h, cm,dim, 0, w));


It works if I use a buffer of type int[] with packed RGBA values and
DataBuffer.TYPE_INT. However, with the above configuration, the image
remains black when drawn.

I'm starting to wonder if createImage actually works with interleaved
component data. I used a writable raster before, but it was awfully slow.

If anyone has a solution for this problem, please tell it.
Thanks in advance.


You need a BufferedImage.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top