Reading an Image

I somehow completely forgot this even though I did it just a few months ago. Don’t worry I have researched this however I haven’t found anything that seems to relate to what I want to do.
What I need to do is read an image to load a map. Let’s say it’s a 15x15 image:
1 = black
0 = white


0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

This would be used to load a simple 2d map into my game. How would I read this image and determine if the pixels are red/white/black? I’m sure I could use a buffered image and it’s raster but I don’t quite know how to read the raster.
On a nice little side note I did my Ordeal for Boy Scouts Order of the Arrow this weekend :slight_smile: (if any of you know what that is :wink: ).
Thanks,
-cMp

Read the RGB (or was it ARGB?) values at a specific point and, depen ding on the value, do something. I can’t remember exactly how to do this but it is how I did it.

Without knowing more about your libraries, e.g. file handling, can’t be more specific

But what reference is from Notch’s Metagun, he used this approach for his level in a ludumdare some years ago

https://github.com/alexnick/metagun/blob/master/src/com/mojang/metagun/level/Level.java

specifically look at pixels, the above Level.java class
and
Art.level.getRGB(xo * 31, yo * 23, 32, 24, pixels, 0, 32);
In this case level is a bufferedImage
as seen here
https://github.com/alexnick/metagun/blob/master/src/com/mojang/metagun/Art.java

he uses the rgb value for determine wall or not wall. to make map

He then uses other factors for other variables as well, quite useful/neat.

Given a bufferedImage
load it up into the file

then have
int[] pixels = new int[1515]; //1515 in your case, cause thats size of image?
then bufferedImage.getRGB( startX, startY, width, height, pixelsOut, offset, scansize);
you can also just get the x,y of something specific too
read up on http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html

then your pixels has your data, and can just sweep thru it using x + y*w; // where w is 15 in your case.

and check if pixel == 0xFFFFFF or 0x000000 for white and black.



try {
    BufferedImage img = ImageIO.read(new File("res/lvl.png"));

    int w = img.getWidth();
    int h = img.getHeight();

    int[] pixels = new int[w * h];
    img.getRGB(0, 0, w, h, pixels, 0, w);

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            int col = pixels[(x + y * w)] & 0xFFFFFF;

            if (col == 0xFF66FF) /* Do something */ ;
        }
    }
} catch (IOException e) {
    e.printStackTrace();
    System.exit(0);
}


This should give you an idea of what to do.

I’m sorry I figured it out myself yesterday and forgot to post :frowning: sorry about that!