2

I have a BufferedImage which represents a 2048X2048 pixels tiff image. I wish to retrieve such array (int [2048][2048] from the BufferedImage. How should I proceed?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Marcio
  • 597
  • 4
  • 7
  • 16
  • Welcome to StackOverflow! It may help you to review http://stackoverflow.com/help/how-to-ask. You're much more likely to get a meaningful answer if you've done some research on finding a solution and show what you've tried. A quick Google search on some of your keywords showed me a lot of potential solutions, including some answers on this site, which may be a good place to get started. – MattDavis Jun 18 '13 at 17:34

2 Answers2

2
arr = new int[2048][2048];

for(int i = 0; i < 2048; i++)
    for(int j = 0; j < 2048; j++)
        arr[i][j] = image.getRGB(i, j);

Since you can get the RGB value of each pixel from the image data structure itself, it might be a benefit to NOT copy everything over to a 2d array.

a_schimpf
  • 745
  • 1
  • 5
  • 16
  • If speed is an issue, and you _know_ the details of the type of the image, you can sometimes do it more efficiently with some non-robust code (typically some casting) looking inside the BufferedImage. – user949300 Jun 18 '13 at 17:23
1

This method will return the red, green and blue values directly for each pixel, and if there is an alpha channel it will add the alpha value. Using this method is harder in terms of calculating indices, but is much faster than the first approach.

 private static int[][] convertTo2DWithoutUsingGetRGB(BufferedImage image) {

  final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
  final int width = image.getWidth();
  final int height = image.getHeight();
  final boolean hasAlphaChannel = image.getAlphaRaster() != null;

  int[][] result = new int[height][width];
  if (hasAlphaChannel) {
     final int pixelLength = 4;
     for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
        int argb = 0;
        argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
        argb += ((int) pixels[pixel + 1] & 0xff); // blue
        argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green
        argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red
        result[row][col] = argb;
        col++;
        if (col == width) {
           col = 0;
           row++;
        }
     }
  } else {
     final int pixelLength = 3;
     for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
        int argb = 0;
        argb += -16777216; // 255 alpha
        argb += ((int) pixels[pixel] & 0xff); // blue
        argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green
        argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red
        result[row][col] = argb;
        col++;
        if (col == width) {
           col = 0;
           row++;
        }
     }
  }

  return result;
 }
Charlie
  • 4,827
  • 2
  • 31
  • 55
  • I have tried this method on my tiff image and the result was Exception in thread "main" java.lang.ClassCastException: java.awt.image.DataBufferUShort cannot be cast to java.awt.image.DataBufferByte on the first line – Marcio Jun 18 '13 at 18:13
  • @Marcio http://stackoverflow.com/questions/2898311/reading-and-writing-out-tiff-image-in-java – Charlie Jun 18 '13 at 18:42
  • BufferedImage and DataBufferByte not found. – Md Imran Choudhury Feb 06 '17 at 11:33