Re: More file stuff: Create an Image

Discussion in 'Java' started by David Zimmerman, Jul 10, 2003.

  1. wrote:
    > This one is a bit more complex than it seems on the surface. What i
    > have is a rudimentarily RLE-compressed image file, in a non-standard
    > format, with an external palette. Also, subject to information from
    > yet another external source, there is possibly more than 1 image per
    > file.
    >
    > The files are essentially byte[] files, nothing fancy. i can read them
    > in, easily.
    >
    > What i don't know how to do is how do i read the next byte and then
    > append a pixel to an Image in order to create a new image for the
    > frame. I have to do this pixel by pixel, so i'm a little confused.
    >
    > Can someone let me in on the secret?
    >


    If you know how big the image is before you start, you can create a
    BufferedImage, get its Graphics2D and use Graphics.drawLine() to draw a
    zero length line at each pixel.

    Better would be to create the BufferedImage with a custom data buffer
    which is exactly the byte[] you're reading.

    Here's a fragment of some code that does that


    int[] pix = new int[w*h];
    ColorModel colorModel = new DirectColorModel(24, 0x00ff0000,
    0x0000ff00, 0x000000ff);
    DataBuffer dataBuffer = new DataBufferInt(pix, w*h);
    WritableRaster writableRaster =
    Raster.createPackedRaster(dataBuffer, w, h, w, new int[] {0x00ff0000,
    0x0000ff00, 0x000000ff}, new Point(0, 0));
    BufferedImage fimg = new BufferedImage(colorModel,
    writableRaster, false, null);


    This allows me to draw into the image by updating the pix array. I have
    decoded to use a 24 bit color model so that each int in pix has RGB
    information encoded using the masks you see in the DirectColorModel. In
    your case, I would use the buffer into which you're reading the data as
    the DataBuffer.
    David Zimmerman, Jul 10, 2003
    #1
    1. Advertising

  2. David Zimmerman

    Guest Guest

    David Zimmerman <> wrote in
    news::
    >
    > int[] pix = new int[w*h];
    > ColorModel colorModel = new DirectColorModel(24,
    > 0x00ff0000,
    > 0x0000ff00, 0x000000ff);
    > DataBuffer dataBuffer = new DataBufferInt(pix, w*h);
    > WritableRaster writableRaster =
    > Raster.createPackedRaster(dataBuffer, w, h, w, new int[]
    > {0x00ff0000, 0x0000ff00, 0x000000ff}, new Point(0, 0));
    > BufferedImage fimg = new BufferedImage(colorModel,
    > writableRaster, false, null);
    >
    >
    > This allows me to draw into the image by updating the pix array. I
    > have decoded to use a 24 bit color model so that each int in pix
    > has RGB information encoded using the masks you see in the
    > DirectColorModel. In your case, I would use the buffer into which
    > you're reading the data as the DataBuffer.
    >
    >


    I see how this works, more or less, although the data types in it are
    all new to me. Where i'm not totally clear is how the colour
    information works. What i currently have is an indexed palette object,
    with j.awt.Color objects for each element in the palette (256-color)
    and the image files **seem** to be simply a byte value indicating which
    colour to use. How can i map from the one to the other? The Java
    colour model is kind of greek to me (i typically write small utilities,
    and i've decided i should challenge myself, so i'm rewriting a game ;)

    Thanks for the help, i'll look at the one you have, and hope for a
    reply soon.

    --
    Chris R.
    =======
    http://offline.pointclark.net/
    Guest, Jul 11, 2003
    #2
    1. Advertising


  3. >
    > I see how this works, more or less, although the data types in it are
    > all new to me. Where i'm not totally clear is how the colour
    > information works. What i currently have is an indexed palette object,
    > with j.awt.Color objects for each element in the palette (256-color)
    > and the image files **seem** to be simply a byte value indicating which
    > colour to use. How can i map from the one to the other? The Java
    > colour model is kind of greek to me (i typically write small utilities,
    > and i've decided i should challenge myself, so i'm rewriting a game ;)


    I used a DirectColorModel which means that each pixel has the RGB data
    itself. Look at IndexColorModel which allows one to use an index (which
    is what you have). You would get to use a byte[] in a DataBuffer that
    used one byte[].

    Something like (warning, niot tried, just typed in)

    int[] r_array, g_array, b_array;
    // Fill in these arrays with your color table,
    // the pixel value will index into this table
    byte[] pix = new byte[w*h];
    ColorModel colorModel = new IndexColorModel(8, 256, r_array,
    g_array, b_array);
    DataBuffer dataBuffer = new DataBufferInt(pix, w*h);
    WritableRaster writableRaster =
    Raster.createPackedRaster(dataBuffer, w, h, w, new int[] {0xffffffff},
    new Point(0, 0));
    BufferedImage fimg = new BufferedImage(colorModel, writableRaster,
    false, null);

    Now you can write color indices (the stuff your reading) into the pix array
    David Zimmerman, Jul 11, 2003
    #3
  4. David Zimmerman

    Guest Guest

    David Zimmerman <> wrote in
    news::

    > I used a DirectColorModel which means that each pixel has the RGB
    > data itself. Look at IndexColorModel which allows one to use an
    > index (which
    > is what you have). You would get to use a byte[] in a DataBuffer
    > that
    > used one byte[].
    >
    > Something like (warning, niot tried, just typed in)
    >
    > <snip code>
    >
    > Now you can write color indices (the stuff your reading) into the
    > pix array
    >
    >


    Perfect, thank you so much. i'll do that right away, and i think
    you've solved a lot of my problems right there...

    --
    Chris R.
    =======
    http://offline.pointclark.net/
    Guest, Jul 11, 2003
    #4
    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. Christopher Benson-Manica

    More loop style stuff

    Christopher Benson-Manica, Nov 11, 2003, in forum: C Programming
    Replies:
    7
    Views:
    324
    E. Robert Tisdale
    Nov 13, 2003
  2. Michael
    Replies:
    4
    Views:
    408
    Matt Hammond
    Jun 26, 2006
  3. Replies:
    1
    Views:
    477
    Juan T. Llibre
    Oct 18, 2006
  4. Alf P. Steinbach
    Replies:
    16
    Views:
    521
    Ned Deily
    Dec 18, 2009
  5. Robert Klemme

    With a Ruby Yell: more, more more!

    Robert Klemme, Sep 28, 2005, in forum: Ruby
    Replies:
    5
    Views:
    214
    Jeff Wood
    Sep 29, 2005
Loading...

Share This Page