Jump to content
  • Advertisement
Sign in to follow this  
Wutalife37

[java] Pixel Changing with BufferedImage

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone. I'm working on a game that allows the player to customize some of the colors on their sprite. Right now I have a default sprite and change the color of all white pixels in-game to match what the player wants. Unfortunately, the process is unusually long, so I suspect something is wrong with the code. Here's the method:
/**
*	Returns an image with the correct color changes
*/
static BufferedImage getRecoloredImage(BufferedImage source, Color replaced, Color changeTo) {

	int replacedRGB = replaced.getRGB();
	int changeToRGB = changeTo.getRGB();

	for (int b = 0, width = source.getWidth(); b < width; b++) {
		for (int c = 0, height = source.getHeight(); c < height; c++) {
			if (source.getRGB(b, c) == replacedRGB) {
				source.setRGB(b, c, changeToRGB);
			}
		}
	}
	return source;
}
Thanks for any help anyone can provide.

Share this post


Link to post
Share on other sites
Advertisement
You could do something like this:


static BufferedImage getRecoloredImage(BufferedImage source, Color replaced, Color changeTo) {

int replacedRGB = replaced.getRGB();
int changeToRGB = changeTo.getRGB();
DataBuffer buffer= im.getRaster().getDataBuffer();
int[] data = ((DataBufferInt)buffer).getData();

for(int i=0, i != data.length; ++i) {
if(data == replacedRGB) {
data = changeToRGB;
}
}

return source;

}



Note: this assumes your BufferedImage is in an INT colormodel! ( ex. BufferedImage.TYPE_INT_RGB,...)

Share this post


Link to post
Share on other sites
Yes, the get/set RGB methods are terribly slow for frequent changes to image data. Manually change the buffer data as Lotuspec suggested.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!