Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: More file stuff: Create an Image

Reply
Thread Tools

Re: More file stuff: Create an Image

 
 
David Zimmerman
Guest
Posts: n/a
 
      07-10-2003


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.

 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      07-11-2003
David Zimmerman <(E-Mail Removed)> wrote in
news:(E-Mail Removed):
>
> 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/
 
Reply With Quote
 
 
 
 
David Zimmerman
Guest
Posts: n/a
 
      07-11-2003


>
> 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

 
Reply With Quote
 
Guest
Posts: n/a
 
      07-11-2003
David Zimmerman <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
wx.Image: Couldn't add an image to the image list. Laszlo Zsolt Nagy Python 1 01-26-2005 09:55 PM
Random Image Script for more than one image slot Linda Javascript 5 01-07-2005 06:01 PM
xsl for an rdf file, src attribute won't create correct link to image file j erickson XML 1 06-09-2004 02:26 PM



Advertisments