Skip to main content
gamedev.net
🔒 Locked

[java] Pixel Changing with BufferedImage

Started by Wutalife37 Aug 6, 2005 at 8:48 PM 1 replies 700+ views
Original Post
Wutalife37
Wutalife37
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.
Lotuspec
Lotuspec
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,...)
TheBluMage
TheBluMage
Yes, the get/set RGB methods are terribly slow for frequent changes to image data. Manually change the buffer data as Lotuspec suggested.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.