bgroenks
bgroenks

Reputation: 1889

Writing an image bitmask

I am working on an image collision mechanism that uses long values as bitmasks for each image and sets each bit to 1 if the pixel is not excluded (transparent in most cases), 0 otherwise. I wrote a test method to write the bitmasks to a text file as 1s and 0s. It seems to me that, in theory, because the image is read row by row, the 1s and 0s should resemble the image itself.

That isn't really what I got. Is the problem with my bit-assigning code or my printing code? Or is it supposed to look like that?

Image Parsing

    int[] data = ColorUtils.getImageData(img);
    bounds = new Rectangle(img.getWidth(), img.getHeight());
    int wt = (int) bounds.getWidth();
    int ht = (int) bounds.getHeight();
    bitmasks = new long[ht];
    for(int y = 0; y < ht; y++) {
        long bitmask = 0;
        for(int x = 0; x < wt; x++) {
            if(data[x + y * wt] != excludedColor)
                bitmask = (bitmask << 1) + 1;
            else
                bitmask = bitmask << 1;
        }
        bitmasks[y] = bitmask;
    }

File Printing

    PrintWriter pw =  null;
    try {
        pw = new PrintWriter(new File("/home/brian/Desktop/imageModel.txt"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return;
    }
    for(int y = 0; y < bounds.getHeight(); y++) {
        long bitmask = bitmasks[y];
        for(int x = 0; x < bounds.getWidth(); x++) {
            pw.print(String.valueOf((bitmask >> (x + y * (int) bounds.getWidth())) & 1));
        }
        pw.println();
        pw.println();
    }
    pw.close();

Image:

Test image being parsed

Text file produced: View

Upvotes: 2

Views: 1385

Answers (1)

mmgp
mmgp

Reputation: 19221

Your image parsing looks mostly fine to me, supposing you can actually store an entire row into a long without overflowing it (and also supposing you are going through the data by considering all the channels correctly, which to me is wrong in your code). But on the "File Printing" I don't see how it could be correct. You are storing one bitmask per row, so if we could index it, bitmask[width - 1] would represent the first column in a given row, bitmask[width - 2] the second column, ..., and bitmask[0] would represent the last column in that row. So two things need to be corrected in the unpacking phase: 1) loop from x = width - 1 down to 0; 2) then unpack it by doing ((mask >> x) & 1).

After resizing your image to 50x50 and considering that a alpha value <= 2 represents the background, here is what I obtain:

00000000000000000111111111111111000000000000000000
00000000000000011111111111111111110000000000000000
00000000000001111111111111111111111100000000000000
00000000000011111111111111111111111111000000000000
00000000001111111111111111111111111111100000000000
00000000011111111111111111111111111111110000000000
00000000111111111111111111111111111111111000000000
00000001111111111111111111111111111111111100000000
00000011111111111111111111111111111111111110000000
00000011111111111111111111111111111111111111000000
00000111111111111111111111111111111111111111000000
00001111111111111111111111111111111111111111100000
00001111111111111111111111111111111111111111100000
00011111111111111111111111111111111111111111110000
00011111111111111111111111111111111111111111110000
00111111111111111111111111111111111111111111111000
00111111111111111111111111111111111111111111111000
00111111111111111111111111111111111111111111111000
01111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111100
01111111111111111111111111111111111111111111111100
00111111111111111111111111111111111111111111111100
00111111111111111111111111111111111111111111111000
00111111111111111111111111111111111111111111111000
00011111111111111111111111111111111111111111111000
00011111111111111111111111111111111111111111110000
00011111111111111111111111111111111111111111110000
00001111111111111111111111111111111111111111100000
00000111111111111111111111111111111111111111100000
00000111111111111111111111111111111111111111000000
00000011111111111111111111111111111111111110000000
00000001111111111111111111111111111111111100000000
00000001111111111111111111111111111111111100000000
00000000111111111111111111111111111111111000000000
00000000011111111111111111111111111111110000000000
00000000000111111111111111111111111111000000000000
00000000000011111111111111111111111110000000000000
00000000000000111111111111111111111000000000000000
00000000000000001111111111111111100000000000000000
00000000000000000011111111111110000000000000000000

Upvotes: 1

Related Questions