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:

Text file produced: View
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
bitmaskper 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, …, andbitmask[0]would represent the last column in that row. So two things need to be corrected in the unpacking phase: 1) loop fromx= width – 1 down to 0; 2) then unpack it by doing((mask >> x) & 1).After resizing your image to 50×50 and considering that a alpha value
<= 2represents the background, here is what I obtain: