add color matrix test to CTS

bug 10427951

Change-Id: I79554e512dc3db59940f4b6cb42179a55f5e794c
diff --git a/tests/tests/rscpp/librscpptest/rs_jni.cpp b/tests/tests/rscpp/librscpptest/rs_jni.cpp
index 4593070..de0908b 100644
--- a/tests/tests/rscpp/librscpptest/rs_jni.cpp
+++ b/tests/tests/rscpp/librscpptest/rs_jni.cpp
@@ -203,3 +203,51 @@
     return (rs->getError() == RS_SUCCESS);
 
 }
+
+extern "C" JNIEXPORT jboolean JNICALL
+Java_android_cts_rscpp_RSColorMatrixTest_colorMatrixTest(JNIEnv * env, jclass obj, jint X,
+                                                         jint Y, jbyteArray inputByteArray,
+                                                         jbyteArray outputByteArray,
+                                                         jfloatArray coeffArray,
+                                                         jint optionFlag)
+{
+    jfloat * coeffs = env->GetFloatArrayElements(coeffArray, NULL);
+    jbyte * input = (jbyte *) env->GetPrimitiveArrayCritical(inputByteArray, 0);
+    jbyte * output = (jbyte *) env->GetPrimitiveArrayCritical(outputByteArray, 0);
+
+    sp<RS> rs = new RS();
+    rs->init();
+
+    sp<const Element> e = Element::RGBA_8888(rs);
+
+    sp<Allocation> inputAlloc = Allocation::createSized2D(rs, e, X, Y);
+    sp<Allocation> outputAlloc = Allocation::createSized2D(rs, e, X, Y);
+
+    inputAlloc->copy2DRangeFrom(0, 0, X, Y, input);
+
+    sp<ScriptIntrinsicColorMatrix> cm = ScriptIntrinsicColorMatrix::create(rs);
+    if (optionFlag == 0) {
+        cm->setColorMatrix3(coeffs);
+    } else if (optionFlag == 1) {
+        cm->setGreyscale();
+    } else if (optionFlag == 2) {
+        cm->setColorMatrix4(coeffs);
+    } else if (optionFlag == 3) {
+        cm->setYUVtoRGB();
+    } else if (optionFlag == 4) {
+        cm->setRGBtoYUV();
+    } else if (optionFlag == 5) {
+        cm->setColorMatrix4(coeffs);
+        float add[4] = {5.3f, 2.1f, 0.3f, 4.4f};
+        cm->setAdd(add);
+    }
+    cm->forEach(inputAlloc, outputAlloc);
+
+    outputAlloc->copy2DRangeTo(0, 0, X, Y, output);
+
+    env->ReleasePrimitiveArrayCritical(inputByteArray, input, 0);
+    env->ReleasePrimitiveArrayCritical(outputByteArray, output, 0);
+    env->ReleaseFloatArrayElements(coeffArray, coeffs, JNI_ABORT);
+    return (rs->getError() == RS_SUCCESS);
+
+}
diff --git a/tests/tests/rscpp/src/android/cts/rscpp/RSColorMatrixTest.java b/tests/tests/rscpp/src/android/cts/rscpp/RSColorMatrixTest.java
new file mode 100644
index 0000000..e4c0085
--- /dev/null
+++ b/tests/tests/rscpp/src/android/cts/rscpp/RSColorMatrixTest.java
@@ -0,0 +1,211 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.cts.rscpp;
+
+import com.android.cts.stub.R;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.test.AndroidTestCase;
+import android.renderscript.*;
+import android.util.Log;
+import java.lang.Integer;
+
+public class RSColorMatrixTest extends RSCppTest {
+    static {
+        System.loadLibrary("rscpptest_jni");
+    }
+
+    private final int X = 1024;
+    private final int Y = 1024;
+
+    native boolean colorMatrixTest(int X, int Y, byte[] input, byte[] output, float[] coeffs, int optionFlag);
+    public void testRSColorMatrix0() {
+        int[] baseAlloc = new int[X * Y * 4];
+        RSUtils.genRandom(0x251107, 255, 1, -128, baseAlloc);
+        RenderScript mRS = RenderScript.create(getContext());
+        byte[] byteAlloc = new byte[X * Y * 4];
+        for (int i = 0; i < X * Y * 4; i++) {
+            byteAlloc[i] = (byte)baseAlloc[i];
+        }
+
+        float[] coeffs = new float[9];
+        coeffs[0] = 1.f;
+        coeffs[1] = 0.3f;
+        coeffs[2] = 0.7f;
+        coeffs[3] =  0.2f;
+        coeffs[4] =  1.f;
+        coeffs[5] =  -0.1f;
+        coeffs[6] =  -0.5f;
+        coeffs[7] =  0.2f;
+        coeffs[8] =  1.f;
+
+        Type.Builder build = new Type.Builder(mRS, Element.RGBA_8888(mRS));
+        build.setX(X);
+        build.setY(Y);
+        Allocation rsInput = Allocation.createTyped(mRS, build.create());
+        Allocation rsOutput = Allocation.createTyped(mRS, build.create());
+        rsInput.copyFromUnchecked(byteAlloc);
+
+        ScriptIntrinsicColorMatrix cm = ScriptIntrinsicColorMatrix.create(mRS, Element.RGBA_8888(mRS));
+        Matrix3f mat = new Matrix3f(coeffs);
+        cm.setColorMatrix(mat);
+        cm.forEach(rsInput, rsOutput);
+
+        byte[] nativeByteAlloc = new byte[X * Y * 4];
+        colorMatrixTest(X, Y, byteAlloc, nativeByteAlloc, coeffs, 0);
+        rsOutput.copyTo(byteAlloc);
+
+        for (int i = 0; i < X * Y * 4; i++) {
+            assertTrue(byteAlloc[i] == nativeByteAlloc[i]);
+        }
+
+    }
+
+    public void testRSColorMatrix1() {
+        int[] baseAlloc = new int[X * Y * 4];
+        RSUtils.genRandom(0x251106, 255, 1, -128, baseAlloc);
+        RenderScript mRS = RenderScript.create(getContext());
+        byte[] byteAlloc = new byte[X * Y * 4];
+        for (int i = 0; i < X * Y * 4; i++) {
+            byteAlloc[i] = (byte)baseAlloc[i];
+        }
+
+        float[] coeffs = new float[9];
+
+        Type.Builder build = new Type.Builder(mRS, Element.RGBA_8888(mRS));
+        build.setX(X);
+        build.setY(Y);
+        Allocation rsInput = Allocation.createTyped(mRS, build.create());
+        Allocation rsOutput = Allocation.createTyped(mRS, build.create());
+        rsInput.copyFromUnchecked(byteAlloc);
+
+        ScriptIntrinsicColorMatrix cm = ScriptIntrinsicColorMatrix.create(mRS, Element.RGBA_8888(mRS));
+        cm.setGreyscale();
+        cm.forEach(rsInput, rsOutput);
+
+        byte[] nativeByteAlloc = new byte[X * Y * 4];
+        colorMatrixTest(X, Y, byteAlloc, nativeByteAlloc, coeffs, 1);
+        rsOutput.copyTo(byteAlloc);
+
+        for (int i = 0; i < X * Y * 4; i++) {
+            assertTrue(byteAlloc[i] == nativeByteAlloc[i]);
+        }
+
+    }
+
+    public void testRSColorMatrix2() {
+        int[] baseAlloc = new int[X * Y * 4];
+        RSUtils.genRandom(0x251105, 255, 1, -128, baseAlloc);
+        RenderScript mRS = RenderScript.create(getContext());
+        byte[] byteAlloc = new byte[X * Y * 4];
+        for (int i = 0; i < X * Y * 4; i++) {
+            byteAlloc[i] = (byte)baseAlloc[i];
+        }
+
+        float[] coeffs = new float[16];
+        for (int i = 0; i < 16; i++) {
+            coeffs[i] = -2.f + .25f*i;
+        }
+
+        Type.Builder build = new Type.Builder(mRS, Element.RGBA_8888(mRS));
+        build.setX(X);
+        build.setY(Y);
+        Allocation rsInput = Allocation.createTyped(mRS, build.create());
+        Allocation rsOutput = Allocation.createTyped(mRS, build.create());
+        rsInput.copyFromUnchecked(byteAlloc);
+
+        ScriptIntrinsicColorMatrix cm = ScriptIntrinsicColorMatrix.create(mRS, Element.RGBA_8888(mRS));
+        Matrix4f mat = new Matrix4f(coeffs);
+        cm.setColorMatrix(mat);
+        cm.forEach(rsInput, rsOutput);
+
+        byte[] nativeByteAlloc = new byte[X * Y * 4];
+        colorMatrixTest(X, Y, byteAlloc, nativeByteAlloc, coeffs, 2);
+        rsOutput.copyTo(byteAlloc);
+
+        for (int i = 0; i < X * Y * 4; i++) {
+            assertTrue(byteAlloc[i] == nativeByteAlloc[i]);
+        }
+
+    }
+
+    public void testRSColorMatrix3() {
+        int[] baseAlloc = new int[X * Y * 4];
+        RSUtils.genRandom(0x251104, 255, 1, -128, baseAlloc);
+        RenderScript mRS = RenderScript.create(getContext());
+        byte[] byteAlloc = new byte[X * Y * 4];
+        for (int i = 0; i < X * Y * 4; i++) {
+            byteAlloc[i] = (byte)baseAlloc[i];
+        }
+
+        float[] coeffs = new float[9];
+
+        Type.Builder build = new Type.Builder(mRS, Element.RGBA_8888(mRS));
+        build.setX(X);
+        build.setY(Y);
+        Allocation rsInput = Allocation.createTyped(mRS, build.create());
+        Allocation rsOutput = Allocation.createTyped(mRS, build.create());
+        rsInput.copyFromUnchecked(byteAlloc);
+
+        ScriptIntrinsicColorMatrix cm = ScriptIntrinsicColorMatrix.create(mRS, Element.RGBA_8888(mRS));
+        cm.setYUVtoRGB();
+        cm.forEach(rsInput, rsOutput);
+
+        byte[] nativeByteAlloc = new byte[X * Y * 4];
+        colorMatrixTest(X, Y, byteAlloc, nativeByteAlloc, coeffs, 3);
+        rsOutput.copyTo(byteAlloc);
+
+        for (int i = 0; i < X * Y * 4; i++) {
+            assertTrue(byteAlloc[i] == nativeByteAlloc[i]);
+        }
+
+    }
+
+    public void testRSColorMatrix4() {
+        int[] baseAlloc = new int[X * Y * 4];
+        RSUtils.genRandom(0x251103, 255, 1, -128, baseAlloc);
+        RenderScript mRS = RenderScript.create(getContext());
+        byte[] byteAlloc = new byte[X * Y * 4];
+        for (int i = 0; i < X * Y * 4; i++) {
+            byteAlloc[i] = (byte)baseAlloc[i];
+        }
+
+        float[] coeffs = new float[9];
+
+        Type.Builder build = new Type.Builder(mRS, Element.RGBA_8888(mRS));
+        build.setX(X);
+        build.setY(Y);
+        Allocation rsInput = Allocation.createTyped(mRS, build.create());
+        Allocation rsOutput = Allocation.createTyped(mRS, build.create());
+        rsInput.copyFromUnchecked(byteAlloc);
+
+        ScriptIntrinsicColorMatrix cm = ScriptIntrinsicColorMatrix.create(mRS, Element.RGBA_8888(mRS));
+        cm.setRGBtoYUV();
+        cm.forEach(rsInput, rsOutput);
+
+        byte[] nativeByteAlloc = new byte[X * Y * 4];
+        colorMatrixTest(X, Y, byteAlloc, nativeByteAlloc, coeffs, 4);
+        rsOutput.copyTo(byteAlloc);
+
+        for (int i = 0; i < X * Y * 4; i++) {
+            assertTrue(byteAlloc[i] == nativeByteAlloc[i]);
+        }
+
+    }
+
+}
\ No newline at end of file