Creating an Image from a rgb byte array

Discussion in 'Java' started by E. Naubauer, Feb 18, 2006.

  1. E. Naubauer

    E. Naubauer Guest

    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.
    E. Naubauer, Feb 18, 2006
    #1
    1. Advertising

  2. E. Naubauer wrote:
    > 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.

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Feb 23, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Schmid

    ModelSim RGB Singal -> Image ?

    Michael Schmid, Jul 24, 2004, in forum: VHDL
    Replies:
    1
    Views:
    609
    Frank Buss
    Jul 24, 2004
  2. Tom  Verbeure

    Re: ModelSim RGB Singal -> Image ?

    Tom Verbeure, Jul 24, 2004, in forum: VHDL
    Replies:
    3
    Views:
    1,828
    Michael Schmid
    Jul 30, 2004
  3. =?Utf-8?B?RGVhc3Vu?=

    Conversion issue: Image>Byte array>Image

    =?Utf-8?B?RGVhc3Vu?=, Jul 14, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    879
    =?Utf-8?B?RGVhc3Vu?=
    Jul 14, 2005
  4. Riri

    Get RGB array from image

    Riri, Jan 26, 2004, in forum: Java
    Replies:
    6
    Views:
    10,404
  5. Kirby
    Replies:
    3
    Views:
    628
    Kirby
    Oct 8, 2004
Loading...

Share This Page