1

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?

Thayne
  • 6,619
  • 2
  • 42
  • 67
  • http://stackoverflow.com/questions/4380728/making-every-pixel-of-an-image-having-a-specific-color-transparent?rq=1 but reverse the logic? –  Jul 15 '16 at 06:10
  • What does unacceptably slow mean? An example with the time and image size etc. would be nice. – eldo Jul 28 '16 at 13:27
  • The fastest I have gotten is 10% slower than using `drawImage` on a Graphics Context to copy the image to a different BufferedImage without an alpha channel. – Thayne Jul 28 '16 at 16:09
  • (color.getRGB()&0x00ffffff) instead of this, use a predefined white color. Maybe it could help a little. – eldo Jul 29 '16 at 07:52

0 Answers0