Painting with Graphics2D and an IndexColorModel...

Discussion in 'Java' started by Michael C. Gilson, Aug 27, 2003.

  1. 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
     
    Michael C. Gilson, Aug 27, 2003
    #1
    1. Advertising

  2. Michael C. Gilson

    Jezuch Guest

    U¿ytkownik Michael C. Gilson napisa³:
    > 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?


    Just paint like you would do with TYPE_INT. The ColorModel will handle
    conversion from Color to index for you. That's the point of abstraction,
    don't you think? :)
    --
    Ecce Jezuch
    "Ash nazg durbatuluk, ash nazg gimbatuluk,
    ash nazg thrakatuluk agh burzum - ishi krimpatul" - Sauron
     
    Jezuch, Aug 27, 2003
    #2
    1. Advertising

  3. fup2 comp.lang.java.gui

    [...]

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


    Can't you keep the RGB values for each index and then use the new
    Color version where you specify the RGB values? I'd hope that then the
    correct underlying index is used.

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Aug 28, 2003
    #3
  4. Jezuch <> wrote in message news:<bij2mh$daf$>...
    > U¿ytkownik Michael C. Gilson napisa³:
    > > 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?

    >
    > Just paint like you would do with TYPE_INT. The ColorModel will handle
    > conversion from Color to index for you. That's the point of abstraction,
    > don't you think? :)


    I tried that, believe me. But here is the problem: java does the
    color conversion for you, which is not good. It gets "close" with
    most of the colors, but it is WAY OFF with some of the others.

    My assumption is that when you create the BufferedImage of
    TYPE_BYTE_INDEXED, but you don't specify a custom ColorModel object,
    java just goes ahead and creates a generic, 256 (or whatever size)
    color map.

    There are two problems with this: 1) I only need to use 14 colors, not
    256 or whatever java comes up with. 2) These 14 colors need to be the
    exact RGB colors that I specify, not some "approximated" color that
    java mapped my actual RGB color to.

    In conclusion, I _could_ just paint into a TYPE_BYTE_INDEXED just like
    I would into a TYPE_INT_RGB. But I need the PRECISE colors, not
    Java's GUESSED colors; so I must specify the color map myself, don't
    you think? ;-)
     
    Michael C. Gilson, Aug 30, 2003
    #4
  5. Michael C. Gilson

    Ken DeLong Guest

    If you are going to construct a BufferedImage with an IndexColorModel
    that has a palette of 14 colors, the pixel samples in the image array
    must have integral values between 0 and 13. The G2 will look at the
    pixel value (say, 5), and look up that color in the IndexColorModel.

    I've done this with an image that consists of an array of bytes. The
    bytes range from 0 to 255, and I don't wish to normalize them, so I
    created an IndexColorModel with 256 colors. I take the original array
    of bytes, wrap it in a DataBuffer, wrap that in an InterleavedRaster,
    and finally stuff the Raster and the ColorModel into a BufferedImage.
    In my paintComponent() method, I just say
    g2.paintImage(bi, 0, 0, width, height, null);
    No need to set the color on the g2 or anything. The entire set of
    code is about 6 lines long.
     
    Ken DeLong, Aug 31, 2003
    #5
    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:
    708
    Roedy Green
    Aug 14, 2003
  2. Martin Chan
    Replies:
    0
    Views:
    583
    Martin Chan
    Feb 17, 2004
  3. bonkura_
    Replies:
    3
    Views:
    2,262
    Roedy Green
    Oct 22, 2005
  4. Replies:
    1
    Views:
    1,888
  5. Philipp
    Replies:
    4
    Views:
    5,426
    Philipp
    Dec 12, 2006
Loading...

Share This Page