3

How can I access the image data (array of palette indices) of an indexed image (png8 or gif)?

Example:

  • Image palette: {0xFF0000, 0x00FF00, 0x0000FF}
  • Image data: {0,1,1,0,1,2,2,2,0,1,0,2,0,1,1,0}

What I need is:

ArrayList<Integer> getImageData(File image) {
  /* ??? */
}
Aoshi
  • 53
  • 4
  • BTW: `ArrayList` is wrong. Generic class must be typed with reference type and must *not* be with a primitive type. You have to use the boxing class `Integer` -> `ArrayList` is correct – halex Sep 01 '12 at 10:34

1 Answers1

1

The code below will read the image data into imageData, an array of int values.

  BufferedImage image = ImageIO.read(imageFile);
  int width = image.getWidth();
  int height = image.getHeight();
  int[] imageData = new int[width * height * image.getColorModel().getNumComponents()];
  imageData = image.getData().getPixels(0, 0, width, height, imageData);
Dan D.
  • 32,246
  • 5
  • 63
  • 79