Skip to main content

[JAI-IMAGEIO] How to prevent colors from getting changed

2 replies [Last post]
aryland
Offline
Joined: 2006-04-22
Points: 0

I have the following problem:
I load an animated gif, and convert it to DirectColorModel
When I am done with it, I convert it back to IndexColorModel, I convert the colormodel by creating a lookup table, for the first frame, and then store the colormodel for the other frames and convert them in the following manner:

ColorModel cm = params.getInternalVariables().getCm();
BufferedImage newImage = new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_BYTE_INDEXED,(IndexColorModel)cm);
Graphics2D gfx = newImage.createGraphics();
gfx.drawImage(image,0,0,null);

The problem is that if i have an image with a transparent background, lets say black (rgb 0,0,0) and the image also contains some colors close to this, lets say rgb 10,0,10 then the resulting image will have its solid black background changed to a mix of rgb 0,0,0 and 10,0,10. The result being that since 0,0,0 is the transparent color, the 10,0,10 pixels come out as black dots which ruins the image.

If I do the full conversion for each frame of the animation then this will not happen, but creating the lookup table is so unbelievably slow, like 10 seconds for a small gif, so the time to process the image would increase by 10 seconds multiplied with the number of frames, that is not an option.

Is there a way that I can prevent imageio from changing the solid black background to a variation of black tones?

I know that if the image does not contain any colors close to the transparent color then it will work, but often this will be the case and if any colors resembling the transparent one if found when I run the above code, then I get the problem.

I dont know if I am providing a clear eanough problem here, does anyone have any input to this?

thanks

Reply viewing options

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

I think this question is about Graphics.drawImage() more than about Image I/O.
I will forward it internally but you might do well to post it also to
java2d-interest@java.sun.com.

Brian

On Mon, 5 Jun 2006, jai-imageio@javadesktop.org wrote:

> I have the following problem:
> I load an animated gif, and convert it to DirectColorModel
> When I am done with it, I convert it back to IndexColorModel, I convert the colormodel by creating a lookup table, for the first frame, and then store the colormodel for the other frames and convert them in the following manner:
>
> ColorModel cm = params.getInternalVariables().getCm();
> BufferedImage newImage = new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_BYTE_INDEXED,(IndexColorModel)cm);
> Graphics2D gfx = newImage.createGraphics();
> gfx.drawImage(image,0,0,null);
>
> The problem is that if i have an image with a transparent background, lets say black (rgb 0,0,0) and the image also contains some colors close to this, lets say rgb 10,0,10 then the resulting image will have its solid black background changed to a mix of rgb 0,0,0 and 10,0,10. The result being that since 0,0,0 is the transparent color, the 10,0,10 pixels come out as black dots which ruins the image.
>
> If I do the full conversion for each frame of the animation then this will not happen, but creating the lookup table is so unbelievably slow, like 10 seconds for a small gif, so the time to process the image would increase by 10 seconds multiplied with the number of frames, that is not an option.
>
> Is there a way that I can prevent imageio from changing the solid black background to a variation of black tones?
>
> I know that if the image does not contain any colors close to the transparent color then it will work, but often this will be the case and if any colors resembling the transparent one if found when I run the above code, then I get the problem.
>
> I dont know if I am providing a clear eanough problem here, does anyone have any input to this?
>
> thanks
> [Message sent by forum member 'aryland' (aryland)]
>
> http://forums.java.net/jive/thread.jspa?messageID=119283

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

aryland
Offline
Joined: 2006-04-22
Points: 0

OK, I have posted this to the JAVA 2D forum,

thanks