title: Fast loading and drawing of RGB data in BufferedImage
body: <p>In some Java code running on Windows, I'm reading some large blocks of RGB data from disk and want to display this to screen as quickly as possible.  The RGB data is 8 bits per channel without any alpha. Currently I have code like the following to create the BufferedImage.</p>
  
  <pre><code>BufferedImage getBufferedImage(File file, int width, int height) {
  
      byte[] rgbData = readRGBFromFile(file);
  
      WritableRaster raster = Raster.createInterleavedRaster(
          rgbData, width, height, 
          width * 3, // scanlineStride
          3, // pixelStride
          new int[]{0, 1, 2}, // bandOffsets
          null);
  
      ColorModel colorModel = new ComponentColorModel(
          ColorSpace.getInstance(ColorSpace.CS_sRGB), 
          new int[]{8, 8, 8}, // bits
          false, // hasAlpha
          false, // isPreMultiplied
          ComponentColorModel.OPAQUE, 
          DataBuffer.TYPE_BYTE);
  
      return new BufferedImage(colorModel, raster, false, null);
  }
  </code></pre>
  
  <p>The problem is that the performance of rendering this to the screen is pretty slow. Around 250 - 300 ms. I've read that for the best performance you need to display in a BufferedImage that's compatible with the screen. To do that, I pass the buffered image returned from the above method to a method like this.</p>
  
  <pre><code>BufferedImage createCompatibleImage(BufferedImage image)
  {
      GraphicsConfiguration gc = GraphicsEnvironment.
          getLocalGraphicsEnvironment().
          getDefaultScreenDevice().
          getDefaultConfiguration();
  
      BufferedImage newImage = gc.createCompatibleImage(
          image.getWidth(), 
          image.getHeight(), 
          Transparency.TRANSLUCENT);
  
      Graphics2D g = newImage.createGraphics();
      g.drawImage(image, 0, 0, null);
      g.dispose();
  
      return newImage;
  }
  </code></pre>
  
  <p>That method essentially converts it from RGB to ARGB on Windows and it really speeds up the displaying, but this method takes ~300 ms for a 1600 x 1200 RGB data block. So now I've basically traded the performance hit of the drawing problem to a converting problem. </p>
  
  <p>300ms is about the same time as it takes to load the RGB data from disk. I would think I could do something faster.</p>
  
  <p>Is there a better way I can do the conversion? Or would it help if I modified the RGB data and added an alpha channel myself beforehand? If so what would my Raster and ColorModel look like. Also, since my RGB data doesn't contain transparency can I get any performance improvements by using pre multiplied alpha or something?</p>
  
  <p>Sorry, bit I'm a little lost on this ColorModel, Raster stuff.</p>
  
  <p>Thanks!</p>