editing images using Java

ok, here goes… i’m trying to work out how to do, or if it’s possible to edit or alter images using Java2D or some other section of Java. this would include things such as…

  • altering the colours of images (per pixel)

  • taking small segments out of images (so that i can take a letter out of larger picture of letters)

any confirmation or help would be greatly appreciated as i really don’t want to make fifty thousand images of symbols where the only difference is the colour.

Well, when I want to examine or display an image one pixel at a time, I first getRaster() on the BufferedImage, and then getDataBuffer() from it, and then you can getData() from that. You will end up with a single array of all of the pixels in your image. But what I like to do is then convert that to a 2-dimensional array, so I can just pull out or change a pixel using the X,Y coordinates in the image.
Here’s an example:


// load the image
BufferedImage myImage = ImageIO.read(this.getClass().getResource("myImage.jpg"));

// retrieve the pixel array from the BufferedImage
int[] pixelArray = ((DataBufferInt)myImage.getRaster().getDataBuffer()).getData();

// create a new 2-dimensional array the size of the image
int[][] myImageArray = new int[myImage.getWidth()][myImage.getHeight()];

// loop through each pixel in the pixelArray and store in the appropriate [x][y] spot in the new 2-dimensional array
for (int x = 0; x < myImage.getWidth(); x++) {
  int[] columnPixels = new int[myImage.getHeight()];

  for (int y = 0; y < myImage.getHeight(); y++) {
    columnPixels[y] = pixelArray[(y * myImage.getWidth()) + x];
  }
 
  myImageArray[x] = columnPixels;
}

You may have to first convert your BufferedImage to TYPE_INT_RGB, or else when you cast to a DataBufferInt you may get an exception.
But in doing this, you now have an array that contains your entire image that you can now call with any x,y coordinates and either retrieve that pixel for display, or change that pixel. Or what’s also useful, you can retrieve just one “slice” of the image at a time. Let’s say you access it like: myImageArray[2]; This will return an array of all of the pixels in the 3rd column of the image.

I do this to preload and prepare all of the images for a level in my ray-casting type game.

[quote]ok, here goes… i’m trying to work out how to do, or if it’s possible to edit or alter images using Java2D or some other section of Java. this would include things such as…

  • altering the colours of images (per pixel)

  • taking small segments out of images (so that i can take a letter out of larger picture of letters)

any confirmation or help would be greatly appreciated as i really don’t want to make fifty thousand images of symbols where the only difference is the colour.
[/quote]
Sounds like you just want to recolor similar images. Besides manipulating pixels directly, there are other ways of accomplishing this:

  1. Use a BandCombineOp object to switch/modify the values of each band in your image. For example, say you have a red rectangle, you can use BandCombineOp to switch the values in the red band with the green band so you get a green rectangle.

  2. Use a RescaleOp/LookupOp to map a certain color to another color. Similar to the above.

  3. See this thread:
    http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=2D;action=display;num=1082125801;start=6#6

bingo, that’s exactly what i want to do (recolour similar images).

i have done some work with the pixelgrabber, but i was having alot of trouble putting it back into an image. hopefully this will help.