title: Convert raw pixel data (as byte array) to BufferedImage
body: <p>I have a requirement to use a native library which reads some proprietary image file formats (something about not reinventing our own wheel). The library works fine, it's just sometimes the images can get pretty big (the record I've seen being 13k x 15k pixels). The problem is my poor JVM keeps dying a painful death and / or throwing OutOfMemoryError's any time the images start getting huge.</p>
  
  <p>Here's what I have running</p>
  
  <pre><code>//the bands, width, and height fields are set in the native code
  //And the rawBytes array is also populated in the native code.
  
  public BufferedImage getImage(){
      int type = bands == 1 ? BufferedImage.TYPE_BYTE_GRAY : BufferedImage.TYPE_INT_BRG;
      BufferedImage bi = new BufferedImage(width, height, type);
      ImageFilter filter = new RGBImageFilter(){
          @Override
          public int filterRGB(int x, int y, int rgb){
              int r, g, b;
              if (bands == 3) {
                  r = (((int) rawBytes[y * (width / bands) * 3 + x * 3 + 2]) &amp; 0xFF) &lt;&lt; 16;
                  g = (((int) rawBytes[y * (width / bands) * 3 + x * 3 + 1]) &amp; 0xFF) &lt;&lt; 8;
                  b = (((int) rawBytes[y * (width / bands) * 3 + x * 3 + 0]) &amp; 0xFF);
              } else {
                  b = (((int) rawBytes[y * width + x]) &amp; 0xFF);
                  g = b &lt;&lt; 8;
                  r = b &lt;&lt; 16;
              }
              return 0xFF000000 | r | g | b;
          }
      };
  
      //this is the problematic block
      ImageProducer ip = new FilteredImageSource(bi.getSource(), filter);
      Image i = Toolkit.getDefaultToolkit().createImage(ip);
      Graphics g = bi.createGraphics();
      //with this next line being where the error tends to occur.
      g.drawImage(i, 0, 0, null);
      return bi;
  }
  </code></pre>
  
  <p>This snippet works great for most images so long as they're not obscenely large. Its speed is also just fine. The problem is that that <code>Image</code> drawing onto the <code>BufferedImage</code> step swallows way too much memory.</p>
  
  <p>Is there a way I could skip that step and go directly from raw bytes to buffered image?</p>