title: Java: how to do fast copy of a BufferedImage's pixels? (unit test included)
body: <p>I want to do a copy (of a rectangle area) of the ARGB values from a source <code>BufferedImage</code> into a destination <code>BufferedImage</code>.  No compositing should be done: if I copy a pixel with an ARGB value of 0x8000BE50 (alpha value at 128), then the destination pixel must be <em>exactly</em> 0x8000BE50, totally overriding the destination pixel.</p>
  
  <p>I've got a very precise question and I made a unit test to show what I need.  The unit test is fully functional and self-contained and is passing fine and is doing precisely what I want.</p>
  
  <p>However, I want a <strong>faster and more memory efficient</strong> method to replace <em>copySrcIntoDstAt</em>(...).</p>
  
  <p>That's the whole point of my question: I'm not after how to "fill" the image in a faster way (what I did is just an example to have a unit test).  All I want is to know what would be a <em>fast and memory efficient</em> way to do it (ie fast and not creating needless objects).</p>
  
  <p>The proof-of-concept implementation I've made is obviously very memory efficient, but it is slow (doing one <code>getRGB</code> and one <code>setRGB</code> for every pixel).</p>
  
  <p>Schematically, I've got this: (where A indicates corresponding pixels from the destination image before the copy)</p>
  
  <pre><code>AAAAAAAAAAAAAAAAAAAA
  AAAAAAAAAAAAAAAAAAAA
  AAAAAAAAAAAAAAAAAAAA
  AAAAAAAAAAAAAAAAAAAA
  AAAAAAAAAAAAAAAAAAAA
  AAAAAAAAAAAAAAAAAAAA
  </code></pre>
  
  <p>And I want to have this:</p>
  
  <pre><code>AAAAAAAAAAAAAAAAAAAA
  AAAAAAAAAAAAAAAAAAAA
  AAAAAAAAAAAAAAAAAAAA
  AAAAAAAAAAAAABBBBAAA
  AAAAAAAAAAAAABBBBAAA
  AAAAAAAAAAAAAAAAAAAA
  </code></pre>
  
  <p>where 'B' represents the pixels from the <em>src</em> image.</p>
  
  <p>Note that I'm looking for an exact replacement of the <em>copySrcIntoDstAt(...)</em> method, not for an API link/quote.</p>
  
  <pre><code>import org.junit.Test;
  
  import java.awt.image.BufferedImage;
  
  import static org.junit.Assert.*;
  
  public class TestCopy {
  
      private static final int COL1 = 0x8000BE50;  // alpha at 128
      private static final int COL2 = 0x1732FE87;  // alpha at  23 
  
      @Test
      public void testPixelsCopy() {
          final BufferedImage src = new BufferedImage(  5,  5, BufferedImage.TYPE_INT_ARGB );
          final BufferedImage dst = new BufferedImage( 20, 20, BufferedImage.TYPE_INT_ARGB );
          convenienceFill( src, COL1 );
          convenienceFill( dst, COL2 );
          copySrcIntoDstAt( src, dst, 3, 4 );
          for (int x = 0; x &lt; dst.getWidth(); x++) {
              for (int y = 0; y &lt; dst.getHeight(); y++) {
                  if ( x &gt;= 3 &amp;&amp; x &lt;= 7 &amp;&amp; y &gt;= 4 &amp;&amp; y &lt;= 8 ) {
                      assertEquals( COL1, dst.getRGB(x,y) );
                  } else {
                      assertEquals( COL2, dst.getRGB(x,y) );
                  }
              }
          }
      }
  
      // clipping is unnecessary
      private static void copySrcIntoDstAt(
              final BufferedImage src,
              final BufferedImage dst,
              final int dx,
              final int dy
      ) {
          // TODO: replace this by a much more efficient method
          for (int x = 0; x &lt; src.getWidth(); x++) {
              for (int y = 0; y &lt; src.getHeight(); y++) {
                  dst.setRGB( dx + x, dy + y, src.getRGB(x,y) );
              }
          }
      }
  
      // This method is just a convenience method, there's
      // no point in optimizing this method, this is not what
      // this question is about
      private static void convenienceFill(
              final BufferedImage bi,
              final int color
      ) {
          for (int x = 0; x &lt; bi.getWidth(); x++) {
              for (int y = 0; y &lt; bi.getHeight(); y++) {
                  bi.setRGB( x, y, color );
              }
          }
      }
  
  }
  </code></pre>