Confusion on YUV NV21 conversion to RGB
<p>According to <a href="http://developer.android.com/reference/android/graphics/ImageFormat.html#NV21">http://developer.android.com/reference/android/graphics/ImageFormat.html#NV21</a>, NV21 is the default used format.</p>

<p>There are quite a number of code on web regarding YUV NV21 to RGB conversion. However, when I go through the code, I doubt on the correctness of the code.</p>

<p><strong>The first component V should come first, followed by first component U</strong></p>

<p>According to <a href="http://msdn.microsoft.com/en-us/library/ms867704.aspx#yuvformats_420formats_12bitsperpixel">http://wiki.videolan.org/YUV#NV21</a>, <code>NV21 is like NV12, but with U and V order reversed: it starts with V.</code> However, when I went through the code implementation</p>

<ul>
<li><a href="http://pastebin.com/T0my7zSc">http://pastebin.com/T0my7zSc</a> - It assumes U comes first</li>
<li><a href="http://stackoverflow.com/a/8394202/72437">http://stackoverflow.com/a/8394202/72437</a> - It assumes U comes first too</li>
<li><a href="http://stackoverflow.com/a/10125048/72437">http://stackoverflow.com/a/10125048/72437</a> - It assmes U comes first too</li>
</ul>

<p><strong>R should be the most significant position</strong>
According implementation of <code>int argb</code> in <a href="http://www.netmite.com/android/mydroid/frameworks/base/graphics/java/android/graphics/Color.java">Color.java</a>, R suppose to be at the most significant position. However, I went through the following code implementation</p>

<ul>
<li><a href="http://pastebin.com/T0my7zSc">http://pastebin.com/T0my7zSc</a> - It assumes R is in least significant position</li>
<li><a href="http://stackoverflow.com/a/8394202/72437">http://stackoverflow.com/a/8394202/72437</a> - It assumes R is in least significant position</li>
</ul>

<p><strong>I was wondering, are they making common mistake, or I have overlooked something?</strong> </p>

<p>Currently, my implementation is as follow.</p>

<pre><code>public static void YUV_NV21_TO_RGB(int[] argb, byte[] yuv, int width, int height) {
    final int frameSize = width * height;

    final int ii = 0;
    final int ij = 0;
    final int di = +1;
    final int dj = +1;

    int a = 0;
    for (int i = 0, ci = ii; i &lt; height; ++i, ci += di) {
        for (int j = 0, cj = ij; j &lt; width; ++j, cj += dj) {
            int y = (0xff &amp; ((int) yuv[ci * width + cj]));
            int v = (0xff &amp; ((int) yuv[frameSize + (ci &gt;&gt; 1) * width + (cj &amp; ~1) + 0]));
            int u = (0xff &amp; ((int) yuv[frameSize + (ci &gt;&gt; 1) * width + (cj &amp; ~1) + 1]));
            y = y &lt; 16 ? 16 : y;

            int r = (int) (1.164f * (y - 16) + 1.596f * (v - 128));
            int g = (int) (1.164f * (y - 16) - 0.813f * (v - 128) - 0.391f * (u - 128));
            int b = (int) (1.164f * (y - 16) + 2.018f * (u - 128));

            r = r &lt; 0 ? 0 : (r &gt; 255 ? 255 : r);
            g = g &lt; 0 ? 0 : (g &gt; 255 ? 255 : g);
            b = b &lt; 0 ? 0 : (b &gt; 255 ? 255 : b);

            argb[a++] = 0xff000000 | (r &lt;&lt; 16) | (g &lt;&lt; 8) | b;
        }
    }
}
</code></pre>
