M
Michael C. Gilson
Hi all. I've searched high and low for a resolution to this problem,
but to no avail... thus I turn to you, gurus, for a helping
hand.........
The application I'm writing is meant to retrieve data from a database
( which happens to be of bacterial DNA sequences ) and then to display
this data as an image. These images are very large, sometimes on the
order of 500,000x200 pixels; and up to 20 of them need to be displayed
at once. To create these images, I construct a BufferedImage object
of type BufferedImage.TYPE_INT_RGB, and then use this image's
Graphics2D object to paint the "content." This works great, but it's
quite pricey with memory.
It turns out that these images only require 14 different colors in
their palette, so I would like to use a more compact ColorModel to
reduce the memory required by each image. Specifically, I'd like to
create an IndexColorModel with my custom palette. I have yet to find
on the web or in Sun's API docs an adequate description of how to use
a Graphics2D object to paint into an indexed-color image. There are
plenty of examples on how to create an image with a custom color map,
but in each of these cases the image is created from a pixel array,
rather than from explicit calls to a Graphics2D object associated with
the image. For my purposes, setting pixel values directly would be an
overwhelming task; I require the convenience methods of Graphics2D to
handle some geometry stuff that would be a NIGHTMARE to do by hand in
pixel arrays.
I do believe I am correctly constructing the IndexColorModel object.
As I understand it, you create the color bands like so:
byte[] r = new byte[numColors];
byte[] g = new byte[numColors];
byte[] b = new byte[numColors];
r[index] = (byte) // red component for "index" goes here
g[index] = (byte) // green component for "index" goes here
b[index] = (byte) // blue component for "index" goes here
And you repeat this for each color/index in your palette.
Here's where things go wrong for me. After I create a BufferedImage
with a ColorModel like the one described above, I use
BufferedImage.createGraphics() to get a handle on the Graphics2D
object so that I may begin painting in my image. I would normally
call:
Graphics.setColor( new Color(*explicit r,g,b values go here*) )
to set the Paint, but in an indexed-color image I need to specify an
index into a color map rather than the explicit red/green/blue values
of the desired color. I tried the following to access the colors in
my color map:
Graphics.setColor( new Color(index,index,index) )
But this almost never yields the actual color that is mapped to the
index.
What am I doing wrong? Is it at all possible to use a Graphics
object to paint into a paletted image? If not, how can one
"interactively" create an image of this sort, without resorting to raw
pixel manipulation?
I would greatly appreciate any advice, suggestions, or working code
samples that might steer me in the right direction.
Thank you all for your time,
Michael C. Gilson
University of Wisconsin - Madison
but to no avail... thus I turn to you, gurus, for a helping
hand.........
The application I'm writing is meant to retrieve data from a database
( which happens to be of bacterial DNA sequences ) and then to display
this data as an image. These images are very large, sometimes on the
order of 500,000x200 pixels; and up to 20 of them need to be displayed
at once. To create these images, I construct a BufferedImage object
of type BufferedImage.TYPE_INT_RGB, and then use this image's
Graphics2D object to paint the "content." This works great, but it's
quite pricey with memory.
It turns out that these images only require 14 different colors in
their palette, so I would like to use a more compact ColorModel to
reduce the memory required by each image. Specifically, I'd like to
create an IndexColorModel with my custom palette. I have yet to find
on the web or in Sun's API docs an adequate description of how to use
a Graphics2D object to paint into an indexed-color image. There are
plenty of examples on how to create an image with a custom color map,
but in each of these cases the image is created from a pixel array,
rather than from explicit calls to a Graphics2D object associated with
the image. For my purposes, setting pixel values directly would be an
overwhelming task; I require the convenience methods of Graphics2D to
handle some geometry stuff that would be a NIGHTMARE to do by hand in
pixel arrays.
I do believe I am correctly constructing the IndexColorModel object.
As I understand it, you create the color bands like so:
byte[] r = new byte[numColors];
byte[] g = new byte[numColors];
byte[] b = new byte[numColors];
r[index] = (byte) // red component for "index" goes here
g[index] = (byte) // green component for "index" goes here
b[index] = (byte) // blue component for "index" goes here
And you repeat this for each color/index in your palette.
Here's where things go wrong for me. After I create a BufferedImage
with a ColorModel like the one described above, I use
BufferedImage.createGraphics() to get a handle on the Graphics2D
object so that I may begin painting in my image. I would normally
call:
Graphics.setColor( new Color(*explicit r,g,b values go here*) )
to set the Paint, but in an indexed-color image I need to specify an
index into a color map rather than the explicit red/green/blue values
of the desired color. I tried the following to access the colors in
my color map:
Graphics.setColor( new Color(index,index,index) )
But this almost never yields the actual color that is mapped to the
index.
What am I doing wrong? Is it at all possible to use a Graphics
object to paint into a paletted image? If not, how can one
"interactively" create an image of this sort, without resorting to raw
pixel manipulation?
I would greatly appreciate any advice, suggestions, or working code
samples that might steer me in the right direction.
Thank you all for your time,
Michael C. Gilson
University of Wisconsin - Madison