0

I have a 30 x 40 pixel .bmp file that I want to load into inputData that is declared like the following:

byte[][] inputData = new byte[30][40];

I relatively new to programming, so any one can tell me what classes should I being using to do so? thanks!

I don't know how to access the .bmp file in the same package and assign the corresponding (x, y) position onto my 2-D byte array. So far I have the following:

for (int x = 0; x < inputData.length; x++)
{
    for (int y = 0; y < inputData[x].length; y++)
    {
        // inputData[x][y] =
    }
}
Sathish
  • 4,403
  • 7
  • 31
  • 53
letter Q
  • 14,735
  • 33
  • 79
  • 118

2 Answers2

0

You have Idea that 1 pixel is 1 byte, it is not true. RGB pixel is already 3 bytes per pixel. Also BMP file is not pixel array but rather compressed image. Simple load to array will not help you. Much better you use some ready libraries.

Look here:

GIF http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html

BMP http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

TGA http://www.java-tips.org/other-api-tips/jogl/loading-compressed-and-uncompressed-tgas-nehe-tutorial-jogl.html

Boris Ivanov
  • 4,145
  • 1
  • 32
  • 40
  • 2
    For BMP example, this is for PDF only and it's not going to help in this case. – Buhake Sindi Oct 27 '12 at 07:33
  • Main idea load BMP and then manipulate with byte array: also check here http://stackoverflow.com/questions/3211156/how-to-convert-image-to-byte-array-in-java Loading BMP here: http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html – Boris Ivanov Oct 27 '12 at 07:34
0

You can use the ImageIO in Java 5+ to read a BMP file into a BufferedImage. The BufferedImage can already convert to int[]

In your case, extracting the green channel into the byte array:

BufferedImage img = ImageIO.read(new File("example.bmp"));
// you should stop here
byte[][] green = new byte[30][40];
for(int x=0; x<30; x++){
  for(int y=0; y<40; y++){
     int color = img.getRGB(x,y);
     //alpha[x][y] = (byte)(color>>24);
     //red[x][y] = (byte)(color>>16);
     green[x][y] = (byte)(color>>8);
     //blue[x][y] = (byte)(color);
  }
}
byte[][] inputData = green;
John Dvorak
  • 26,799
  • 13
  • 69
  • 83