[JAI-IMAGEIO] How to prevent colors from getting changed
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();
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?