set a color to transparent

Hi, well I have a BufferedImage (Sprite) and want to set the background color transparent…
I use this code


public class Transparency 
{
  	public static BufferedImage makeColorTransparent(BufferedImage im, final Color color)
  	{
    	ImageFilter filter = new RGBImageFilter()
    	{
      		public int markerRGB = color.getRGB() | 0xFF000000;

      		public final int filterRGB(int x, int y, int rgb)
      		{
        		if ( ( rgb | 0xFF000000 ) == markerRGB )
        		{
          			return 0x00FFFFFF & rgb;
          		}
        		else
        		{
          			return rgb;
          		}
        	}
    	}; 

    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    
    Image src = Toolkit.getDefaultToolkit().createImage(ip);

	BufferedImage jo = new BufferedImage(src.getWidth(null), src.getHeight(null), BufferedImage.TYPE_INT_ARGB);
	Graphics2D g2 = jo.createGraphics();
	g2.drawImage(src, 0, 0, null);
	g2.dispose();

    return jo;
    }
    
}

It was actually made so that it puts out awt.Image but I need BufferedImage, so I changed it this way…

But if Images are bigger its a bit laggy because I use this, I believe:


    Image src = Toolkit.getDefaultToolkit().createImage(ip);

	BufferedImage jo = new BufferedImage(src.getWidth(null), src.getHeight(null), BufferedImage.TYPE_INT_ARGB);
	Graphics2D g2 = jo.createGraphics();
	g2.drawImage(src, 0, 0, null);
	g2.dispose();

thats because you cannot simply cast an Image to BufferedImage…

or is there a faster possibility ?

Eh… just use some image format, which supports transparency like gif or png.

or if you must do it with colours, then why not just:

for(bla bla loop y) {
 for(bla bla loop x) {
  if(image.getRgb(x,y) blabla is the colour) image.setRgb(x,y,0x00000000);
 }
}

ya I have to do it this way… I have > 1000 bmp pics with black background…


		for(int y=0;y < pic.getHeight();y++)
		{
 			for(int x=0;x < pic.getWidth();x++)
 			{
  				if(pic.getRGB(x,y) == new Color(0,0,0)) {pic.setRGB(x,y,0x00000000);}
  			}
		}

this doesn’t work because getRGB(x,y) returns an integer… but how to describe a color with it ?
if the color is black, 0,0,0 pure black, he should replace this with transparency what hopefully 0x00000000 represents… but whats how looks black then ? ^^

in an image with type INT_ARGB, black is 0xFF000000
each pixel is an integer of the form 0xAARRGGBB

apprently the type of the BufferedImage is mostly 13 and sometimes 6, well how to say the BufferedImage to be INT_ARGB ?
there is no setType Method and the construktor requires width, height along with a type, but the size is different… so I cannot say that


 		BufferedImage transpic = new BufferedImage(pic.getWidth(), pic.getHeight(),BufferedImage.TYPE_INT_ARGB);
		
		Graphics2D g = (Graphics2D) transpic.getGraphics();
                           g.drawImage(pic, 0, 0, null);
		
		for(int y=0;y < transpic.getHeight();y++)
		{
 			for(int x=0;x < transpic.getWidth();x++)
 			{
  				if(transpic.getRGB(x,y) == 0xFF000000) {transpic.setRGB(x,y,0x00000000);}
  			}
		}

know getType() really is TYPE_INT_ARGB but it doesn’t matter, I can replace with any color, but not with none… which means the Alpha Value in 0xAARRGGBB does not work :frowning:

what doesn’t work? what happens? what goes wrong?
“doesn’t work” is one of the most undescriptive things you can say :wink:

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class BlackTrans{
	public static void main(String[]args)throws Exception{
		BufferedImage in=ImageIO.read(BlackTrans.class.getResourceAsStream("/blacktrans.png"));
		BufferedImage out=new BufferedImage(in.getWidth(),in.getHeight(),BufferedImage.TYPE_INT_ARGB);
		Graphics g=out.getGraphics();
		g.drawImage(in,0,0,null);
		g.dispose();
		for(int x=out.getHeight()-1;x>=0;--x){
			for(int y=out.getWidth()-1;y>=0;--y){
				if(out.getRGB(x,y)==0xff000000)
					out.setRGB(x,y,0);
			}
		}
		ImageIO.write(out,"png",new File("blacktransout.png"));
	}
}

Works just fine.

edit: You should do that offline… and not on startup.

here’s some information on ARGB data with AWT, though I don’t use that at all to set transparent colors but I simply do get the pictures transformed by a common Graphical Program like the GIMP.