'Converting rgb format to yuv using ScriptIntrinsicColorMatrix

I am trying to convert an rgb image to yuv. Below is my code

private byte[] GetNV21(int inputWidth, int inputHeight, Bitmap scaled)
        {
            byte[] yuv = new byte[inputWidth * inputHeight * 3 / 2]
            scaled.EraseColor(Color.Red);
            Allocation ain = Allocation.createFromBitmap(renderScript, scaled);
            Type.Builder tb = new Type.Builder(renderScript, Element.YUV(renderScript))
                .setX(inputWidth).setY(inputHeight);
            Allocation aout = Allocation.createTyped(renderScript, tb.Create());
            ScriptIntrinsicColorMatrix colorMatrixScript = ScriptIntrinsicColorMatrix.Create(renderScript);
            colorMatrixScript.SetRGBtoYUV();

            colorMatrixScript.ForEach(ain, aout);
            aout.CopyTo(yuv);

            
            scaled.Recycle();
            return yuv
        }

This function return wrong byte array (just Y, without U and V)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source