Buffered Image and Pixel Manipulation question

Can anyone give me a good example of how to load an image as a BufferedImage. I've been looking for a solid week or so, but nowhere gives a good example. I'm trying to avoid swing and like using 8 other objects before I convert to BufferedImage.

Also once I have my buffered image, I want to get all pixels that are a particular color and make them transparent (this way I can keep my images as a bitmap and create the transparency myself!)

Thanks!

[469 byte] By [untwisteda] at [2007-9-29 14:22:20]
# 1

This is a method I learned from Abuse a while ago (you can ignore/take out the logging info etc..)

public BufferedImage loadImage(String path) {

Log.getInstance().printDebug("Loading image " + path);

try {

Image preImage =javax.imageio.ImageIO.read(

getClass().getResource(path));

GraphicsConfiguration gc = window.getGraphicsConfiguration();

/**createas a fully accelerated image*/

BufferedImage toReturn = gc.createCompatibleImage(

preImage.getWidth(null),

preImage.getHeight(null),

Transparency.TRANSLUCENT);

Graphics2D toReturnG = (Graphics2D) toReturn.getGraphics();

toReturnG.setComposite(AlphaComposite.Src);

toReturnG.drawImage(preImage, 0, 0, null);

toReturnG.dispose();

return toReturn;

} catch (Exception e) {

Log.getInstance().printException(e,

"Error loading image, check path");

return null;

}

} //end loadImage

Seekelya at 2007-7-15 5:03:40 > top of Java-index,Other Topics,Java Game Development...
# 2

static final int TRANSPARENCY_COLOR = 0xFF00FF; //the usual pink used to indicate transparency

static final String IMAGE_NAME = "blahblah.blah";

....

BufferedImage bi = javax.imageio.ImageIO.read(getClass().getResource(IMAGE_NAME));

for(int x = 0;i < bi.getWidth();x++)

{

for(int y = 0;y<bi.getHeight();y++)

{

int argb = bi.getRGB(x,y);

if((argb&0xFFFFFF)==TRANSPARENCY_COLOR) bi.setRGB(x,y,0);

}

}

This will probably do what you want.

However I recommend against it. It is far more efficient to store the transparency within the image.

note.

You will subsequently need to copy the BufferedImage returned from ImageIO into a BufferedImage returned from graphicsConfiguration.createCompatibleImage(...) for it to be acceleratable.>

Abusea at 2007-7-15 5:03:40 > top of Java-index,Other Topics,Java Game Development...
# 3
hehe, posted at the same time ^_^
Abusea at 2007-7-15 5:03:40 > top of Java-index,Other Topics,Java Game Development...
# 4
btw my filtering code above isn't meant to be efficient or neat, its just to show u what you need to be doing.The same code logic can be incorporated into a subclass of RGBImageFilter.
Abusea at 2007-7-15 5:03:40 > top of Java-index,Other Topics,Java Game Development...