title: Writing an image bitmask
body: <p>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.</p>
  
  <p>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?</p>
  
  <p><strong>Image Parsing</strong></p>
  
  <pre><code>    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 &lt; ht; y++) {
          long bitmask = 0;
          for(int x = 0; x &lt; wt; x++) {
              if(data[x + y * wt] != excludedColor)
                  bitmask = (bitmask &lt;&lt; 1) + 1;
              else
                  bitmask = bitmask &lt;&lt; 1;
          }
          bitmasks[y] = bitmask;
      }
  </code></pre>
  
  <p><strong>File Printing</strong></p>
  
  <pre><code>    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 &lt; bounds.getHeight(); y++) {
          long bitmask = bitmasks[y];
          for(int x = 0; x &lt; bounds.getWidth(); x++) {
              pw.print(String.valueOf((bitmask &gt;&gt; (x + y * (int) bounds.getWidth())) &amp; 1));
          }
          pw.println();
          pw.println();
      }
      pw.close();
  </code></pre>
  
  <p><strong>Image:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/hJdls.png" alt="Test image being parsed"></p>
  
  <p><strong>Text file produced:</strong> <a href="http://dl.dropbox.com/u/34206572/Misc/imageModel.txt" rel="nofollow noreferrer">View</a></p>