Skip to main content

[JAVA2D] setting color with Graphics2D on a BufferedImage with IndexColorModel

1 reply [Last post]
Anonymous

I hava a BufferedImage from a GIF, the ColorModel is a IndexColorModel .
Since i finally want to write back the modified image to a GIF , i don't
want to change the color model, especially my application need to absolutely
keep the palette as it is used in the original GIF.
Then i did create a Graphics2d from this BufferedImage.
Then i call setColor() and drawString().

My problem: if I use a color that is not contained in the palette, or if I
call drawString() with font antialiasing , which will produce pixels with a
color not contained in the palette. How is are these new colors mapped to
the palette color?
Or how can i controll this mapping ?

Unfortunately I did not found the source code from SunGraphics2D , so that I
can see myself what exactly is goning on inside when calling setColor and
drawSting und the circumstances described above.
Any ideas where I can get the source code from ?
Bodo

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Jim Graham

> My problem: if I use a color that is not contained in the palette, or if I
> call drawString() with font antialiasing , which will produce pixels with
> a color not contained in the palette. How is are these new colors mapped
> to the palette color?

There is no specification to define how this mapping is done.

Implementations are free to do the mapping as best they can. They are
even free to ignore the antialiasing hint for palette destinations since
it is just a hint.

There are no really great algorithms for dealing with trying to choose
the best colors from an arbitrary palette to match the arbitrary
colors generated by antialiasing and alpha blending and have the
results look very pleasing. We have some fairly clever code that
tries to do so if you specify such an operation, but it is a compromise
for speed and accuracy and in the end it is not wise to do blending
and antialiasing on a palette destination.

> Or how can i controll this mapping ?

Unfortunately, there are no APIs for controlling how the internal
rendering equations choose their colors from the palette. It's a
really arcane section of the code and it is better to invest your
time in using deeper image formats than to try to optimize this
behavior.

I realize that your original description of your problem specified
that you needed to write back the results to a GIF, but that is
really the problem right there - to choose a limited image format
to store the results of some complex blending. If you have any
leeway to change that constraint, then you'd be better off
upgrading the image to a 24 or 32 bit format, doing the rendering
there, and writing back a full color PNG or even a lossy compressed
JPEG than trying to use a paletted image format and a GIF output...

...jim

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".