title: Renderscript allocation from bitmap
body: <p>I'm trying to get into render script and was confused regarding allocations usage. Almost all examples show next algorithm:</p>
  
  <ol>
  <li>Create In and Out Bitmap</li>
  <li>Create In and Out Allocation from Bitmaps In and Out correspondingly</li>
  <li>Configure script and perform forEach method</li>
  <li>copy result from Out allocation into bitmap using copyTo method</li>
  </ol>
  
  <p>Something like that:</p>
  
  <pre><code>    Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lena);
      Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
  
      Allocation allocationIn = Allocation.createFromBitmap(renderScript, srcBitmap);
      Allocation allocationOut = Allocation.createFromBitmap(renderScript, dstBitmap);
  
      scriptColorMatrix.setGreyscale();
  
      scriptColorMatrix.forEach(allocationIn, allocationOut);
  
      //no difference after removing this line
      allocationOut.copyTo(dstBitmap);
  
      imagePreview.setImageBitmap(dstBitmap);
  </code></pre>
  
  <p>This works, but it also works even if I omit step 4 by removing:</p>
  
  <pre><code>allocationOut.copyTo(dstBitmap);
  </code></pre>
  
  <p>Lets go further and lower brightness after grayscale:</p>
  
  <pre><code>    Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lena);
      Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
  
      Allocation allocationIn = Allocation.createFromBitmap(renderScript, srcBitmap);
      Allocation allocationOut = Allocation.createFromBitmap(renderScript, dstBitmap);
  
      scriptColorMatrix.setGreyscale();
  
      scriptColorMatrix.forEach(allocationIn, allocationOut);
  
      //reset color matrix
      scriptColorMatrix.setColorMatrix(new Matrix4f());
  
      //adjust brightness
      scriptColorMatrix.setAdd(-0.5f, -0.5f, -0.5f, 0f);
  
      //Performing forEach vise versa (from out to in)
      scriptColorMatrix.forEach(allocationOut, allocationIn);
  
      imagePreview.setImageBitmap(srcBitmap);
  </code></pre>
  
  <p>Shortly describing the code above, we performed grayscale collor matrix from In allocation into Out one, and brightness adjustment in backward direction. I never called copyTo method, but at the end I've got result in srcBitmap and it was correct.</p>
  
  <p>That's not the end. Lets go deeper. I'll leave only one bitmap and one Allocation:</p>
  
  <pre><code>    Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lena);
  
      Allocation allocationIn = Allocation.createFromBitmap(renderScript, srcBitmap);
  
      scriptColorMatrix.setGreyscale();
  
      scriptColorMatrix.forEach(allocationIn, allocationIn);
  
      //reset color matrix
      scriptColorMatrix.setColorMatrix(new Matrix4f());
  
      //adjust brightness
      scriptColorMatrix.setAdd(-0.5f, -0.5f, -0.5f, 0f);
  
      scriptColorMatrix.forEach(allocationIn, allocationIn);
  
      imagePreview.setImageBitmap(srcBitmap);
  </code></pre>
  
  <p>The result is the same... </p>
  
  <p><br><br>Can anybody explain why does it happen and where to use copyTo and where I can use targeting Bitmap without it?</p>