Tuesday, 19 April 2011

Java: compare different methods for copying BufferedImage

Tested with copying a 640x480 RGB image.
// Fastest method (0-1ms).
public void copy1(BufferedImage src, BufferedImage dst) {
  int[] srcArray = ((DataBufferInt) src.getRaster().getDataBuffer()).getData();
  int[] dstArray = ((DataBufferInt) bi.getRaster().getDataBuffer()).getData();
  System.arraycopy(srcArray, 0, dstArray, 0, srcArray.length);
}

// Second fastest (7-8ms).
public void copy1(BufferedImage src, BufferedImage dst) {
  int[] srcArray = ((DataBufferInt) src.getRaster().getDataBuffer()).getData();
  dst.setRGB(0, 0, dst.getWidth(), dst.getHeight(), srcArray, 0, dst.getWidth());
}

// Slowest method (18-19ms).
public void copy1(BufferedImage src, BufferedImage dst) {
  dst.setData(src.getData());
}

No comments :

Post a Comment