title: Converting camera YUV-data to ARGB with renderscript
body: <p>My Problem is: I've set up a camera in Android and receive the preview data by using an onPreviewFrame-listener which passes me an byte[] array containing the image data in the default android YUV-format (device does not support R5G6B5-format). Each pixel consists of 12bits which makes the thing a little tricky. Now what I want to do is converting the YUV-data into ARGB-data in order to do image processing with it. This has to be done with renderscript, in order to maintain a high performance. </p>
  
  <p>My idea was to pass two pixels in one element (which would be 24bits = 3 bytes) and then return two ARGB pixels. The problem is, that in Renderscript a u8_3 (a 3dimensional 8bit vector) is stored in 32bit, which means that the last 8 bits are unused. But when copying the image data into the allocation all of the 32bits are used, so the last 8bit get lost. Even if I used a 32bit input data, the last 8bit are useless, because they're only 2/3 of a pixel. When defining an element consisting a 3-byte-array it actually has a real size of 3 bytes. But then the Allocation.copyFrom()-method doesn't fill the in-Allocation with data, argueing it doesn't has the right data type to be filled with a byte[].</p>
  
  <p>The renderscript documentation states, that there is a ScriptIntrinsicYuvToRGB which should do exactly that in API Level 17. But in fact the class doesn't exist. I've downloaded API Level 17 even though it seems not to be downloadable any more. Does anyone have any information about it? Does anyone have ever tried out a ScriptIntrinsic?</p>
  
  <p>So in conclusion my question is: How to convert the camera data into ARGB data fast, hardwareaccelerated?</p>
  
  <p>That's how to do it in Dalvik VM (found the code somewhere online, it works):</p>
  
  <pre><code>@SuppressWarnings("unused")
  private void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {  
      final int frameSize = width * height;  
      for (int j = 0, yp = 0; j &lt; height; j++) {
          int uvp = frameSize + (j &gt;&gt; 1) * width, u = 0, v = 0;  
          for (int i = 0; i &lt; width; i++, yp++) {  
              int y = (0xff &amp; ((int) yuv420sp[yp])) - 16;  
              if (y &lt; 0)
                  y = 0;  
              if ((i &amp; 1) == 0) {  
                  v = (0xff &amp; yuv420sp[uvp++]) - 128;  
                  u = (0xff &amp; yuv420sp[uvp++]) - 128;  
              }  
              int y1192 = 1192 * y;  
              int r = (y1192 + 1634 * v);  
              int g = (y1192 - 833 * v - 400 * u);  
              int b = (y1192 + 2066 * u);  
              if (r &lt; 0)
                  r = 0;
              else if (r &gt; 262143)
                  r = 262143;  
              if (g &lt; 0)
                  g = 0;
              else if (g &gt; 262143)
                  g = 262143;  
              if (b &lt; 0)
                  b = 0;
              else if (b &gt; 262143)  
                  b = 262143;  
              rgb[yp] = 0xff000000 | ((r &lt;&lt; 6) &amp; 0xff0000) | ((g &gt;&gt; 2) &amp; 0xff00) | ((b &gt;&gt; 10) &amp; 0xff);  
          }
      }
  }
  </code></pre>