I would like to replace all completely transparent pixels in an image with a certain color (in my case white) in order to deal with sub-sampling issues when the image is embedded in a PDF.
I have code that looks something like this:
BufferedImage bi = new BufferedImage(bufferedImage.getWidth(),bufferedImage.getHeight(),BufferedImage.TYPE_INT_ARGB);
for (int x=0;x<bufferedImage.getWidth();x++){
for (int y=0;y<bufferedImage.getHeight();y++){
int rgba = bufferedImage.getRGB(x,y);
boolean isTrans = (rgba & 0xff000000) == 0;
if (isTrans){
bi.setRGB(x,y, 0x00ffffff);
} else {
bi.setRGB(x,y,rgba | 0xff000000);
}
}
}
But this is unacceptably slow. I have tried several things, including using a ColorConvertOp, and trying to operation the WriteableRaster directly. Every attempt has either been too slow, or didn't work.
It seems like there should be an efficient way to do this.
I think the solution might be to define a custom ColorModel, but I'm not really sure how to do that. Is there another way to do this efficiently?