Error when creating BufferedImage with IndexColorModel from a byte array.

Discussion in 'Java' started by weisburd@gmail.com, Oct 11, 2006.

  1. Guest

    Hi, I have a 1-dimentional byte array and an IndexColorTable, and I
    can't figure out how to combine the 2 into an BufferedImage without
    unnecessary copying/reallocating of the image buffer.

    The color model I have is:

    int [] cmap = new int [numColors];
    cmap[i++] = 0xffa0f000; /etc.
    ....
    new IndexColorModel(8, 22, cmap, 0, true, transparentIndex,
    DataBuffer.TYPE_BYTE );




    Thanks for your help
    -Ben


    Ps.

    I tried to do this based on some example code
    (http://javaalmanac.com/egs/java.awt.image/Mandelbrot2.html?l=rel), but
    can't figure out how to go from the color model they're using to the
    one I have (the 8 bit one specified above). When I replace the 4bit
    colormodel in the code below with the 8bit color model specified above,
    I get the following error:

    [java] java.lang.IllegalArgumentException: Raster
    ByteInterleavedRaster: width = 5120 height = 3520 #numDataElements 1
    dataOff[0] = 0 is incompatible with ColorModel IndexColorModel:
    #pixelBits = 8 numComponents = 4 color space =
    java.awt.color.ICC_ColorSpace@c51355 transparency = 2 transIndex = 22
    has alpha = true isAlphaPre = false
    [java] at java.awt.image.BufferedImage.<init>(BufferedImage.java:613)


    Code:

    byte[] pixelArray = (byte[]) getData_CHAR();
    int width = 5120;
    int height = 3520;
    int numbytes = width*height;

    //create DataBuffer using byte buffer of pixel data.
    DataBuffer dataBuffer = new DataBufferByte(pixelArray, numbytes, 0);

    //prepare a sample model that specifies a storage 8-bits of pixel data
    in an 8-bit data element
    int bitMasks[] = new int[]{0xf};
    SinglePixelPackedSampleModel sampleModel = new
    SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE, width, height,
    bitMasks);

    //create a raster using the sample model and data buffer
    WritableRaster writableRaster =
    Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0,0));

    //generate 16-color model
    byte[] r = new byte[16];
    byte[] g = new byte[16];
    byte[] b = new byte[16];

    r[0] = 0; g[0] = 0; b[0] = 0;
    r[1] = 0; g[1] = 0; b[1] = (byte)192;
    r[2] = 0; g[2] = 0; b[2] = (byte)255;
    r[3] = 0; g[3] = (byte)192; b[3] = 0;
    r[4] = 0; g[4] = (byte)255; b[4] = 0;
    r[5] = 0; g[5] = (byte)192; b[5] = (byte)192;
    r[6] = 0; g[6] = (byte)255; b[6] = (byte)255;
    r[7] = (byte)192; g[7] = 0; b[7] = 0;
    r[8] = (byte)255; g[8] = 0; b[8] = 0;
    r[9] = (byte)192; g[9] = 0; b[9] = (byte)192;
    r[10] = (byte)255; g[10] = 0; b[10] = (byte)255;
    r[11] = (byte)192; g[11] = (byte)192; b[11] = 0;
    r[12] = (byte)255; g[12] = (byte)255; b[12] = 0;
    r[13] = (byte)80; g[13] = (byte)80; b[13] = (byte)80;
    r[14] = (byte)192; g[14] = (byte)192; b[14] = (byte)192;
    r[15] = (byte)255; g[15] = (byte)255; b[15] = (byte)255;

    //create buffered image
    ColorModel colorModel = new IndexColorModel(4, 16, r, g, b);
    BufferedImage image = new BufferedImage(colorModel, writableRaster,
    false, null);
    , Oct 11, 2006
    #1
    1. Advertising

  2. PofN Guest

    wrote:
    > Hi, I have a 1-dimentional byte array and an IndexColorTable, and I
    > can't figure out how to combine the 2 into an BufferedImage without
    > unnecessary copying/reallocating of the image buffer.


    Try MemoryImageSource. It is supposed to require the least copying.

    > (http://javaalmanac.com/egs/java.awt.image/Mandelbrot2.html?l=rel)


    That's the most complicated way to build an image. Someone at Sun sure
    got a hard one when comming up with that image structure.

    PofN
    PofN, Oct 11, 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. Bernhard Messerer
    Replies:
    1
    Views:
    671
    Roedy Green
    Aug 14, 2003
  2. Michael C. Gilson
    Replies:
    4
    Views:
    948
    Ken DeLong
    Aug 31, 2003
  3. Martin Chan
    Replies:
    0
    Views:
    565
    Martin Chan
    Feb 17, 2004
  4. cyberco
    Replies:
    11
    Views:
    73,498
    greejose
    Jun 27, 2008
  5. Philipp
    Replies:
    4
    Views:
    5,298
    Philipp
    Dec 12, 2006
Loading...

Share This Page