xref: /aosp_15_r20/cts/tests/tests/renderscript/src/android/renderscript/cts/RsPackColorTo8888Test.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.renderscript.cts;
18 
19 import android.renderscript.Allocation;
20 import android.renderscript.Element;
21 import android.renderscript.Float3;
22 import android.renderscript.Float4;
23 import android.renderscript.RSRuntimeException;
24 
25 public class RsPackColorTo8888Test extends RSBaseCompute {
26     private ScriptC_rs_pack_color_to_8888 script_f32;
27 
28     @Override
setUp()29     protected void setUp() throws Exception {
30         super.setUp();
31         script_f32 = new ScriptC_rs_pack_color_to_8888(mRS);
32     }
33 
34     @Override
tearDown()35     protected void tearDown() throws Exception {
36         script_f32.destroy();
37         super.tearDown();
38     }
39 
40     @Override
forEach(int testId, Allocation mIn, Allocation mOut)41     public void forEach(int testId, Allocation mIn, Allocation mOut) throws RSRuntimeException {
42         switch (testId) {
43         case 0:
44             script_f32.forEach_pack_color_to_8888_rgb(mIn, mOut);
45             break;
46         case 1:
47             script_f32.forEach_pack_color_to_8888_rgba(mIn, mOut);
48             break;
49         case 2:
50             script_f32.forEach_pack_color_to_8888_f32_3(mIn, mOut);
51             break;
52         case 3:
53             script_f32.forEach_pack_color_to_8888_f32_4(mIn, mOut);
54             break;
55         }
56     }
57 
testRsPackColorTo8888RGB()58     public void testRsPackColorTo8888RGB() {
59         float3input(0x17abc72, 0);
60     }
61 
testRsPackColorTo8888RGBA()62     public void testRsPackColorTo8888RGBA() {
63         float4input(0x76a6b, 1);
64     }
65 
testRsPackColorTo8888F32_3()66     public void testRsPackColorTo8888F32_3() {
67         float3input(0x17abc72, 2);
68     }
69 
testRsPackColorTo8888F32_4()70     public void testRsPackColorTo8888F32_4() {
71         float4input(0xabc72, 3);
72     }
73 
float3input(long seed, int testId)74     private void float3input(long seed, int testId) {
75         Allocation mAllocationIn = Allocation.createSized(mRS, Element.F32_3(mRS), INPUTSIZE);
76         Allocation mAllocationOut = Allocation.createSized(mRS, Element.U8_4(mRS), INPUTSIZE);
77         float[] inArray = new float[INPUTSIZE * 4];
78         byte[] outArray = new byte[INPUTSIZE * 4];
79         byte[] refArray = new byte[INPUTSIZE * 4];
80         RSUtils.genRandomFloats(seed, 0.0f, 1.0f, inArray, false);
81         mAllocationIn.copy1DRangeFrom(0, INPUTSIZE, inArray);
82         try {
83             forEach(testId, mAllocationIn, mAllocationOut);
84         } catch (RSRuntimeException e) {
85         }
86         mAllocationOut.copyTo(outArray);
87         for (int i = 0; i < outArray.length; i += 4) {
88             int offset = i;
89             Float3 inValues = new Float3(inArray[offset], inArray[offset + 1], inArray[offset + 2]);
90             byte[] cValue = rs_PackColorTo8888(inValues);
91             refArray[i] = cValue[0];
92             refArray[i + 1] = cValue[1];
93             refArray[i + 2] = cValue[2];
94             refArray[i + 3] = cValue[3];
95             for (int j = 0; j < 4; j++){
96                 assertEquals(refArray[i+j] & 0xff, outArray[i+j] & 0xff);
97             }
98         }
99 
100         mAllocationIn.destroy();
101         mAllocationOut.destroy();
102     }
103 
float4input(long seed, int testId)104     private void float4input(long seed, int testId) {
105         Allocation mAllocationIn = Allocation.createSized(mRS, Element.F32_4(mRS), INPUTSIZE);
106         Allocation mAllocationOut = Allocation.createSized(mRS, Element.U8_4(mRS), INPUTSIZE);
107         float[] inArray = new float[INPUTSIZE * 4];
108         byte[] outArray = new byte[INPUTSIZE * 4];
109         byte[] refArray = new byte[INPUTSIZE * 4];
110         RSUtils.genRandomFloats(seed, 0.0f, 1.0f, inArray, false);
111         mAllocationIn.copy1DRangeFrom(0, INPUTSIZE, inArray);
112         try {
113             forEach(testId, mAllocationIn, mAllocationOut);
114         } catch (RSRuntimeException e) {
115         }
116         mAllocationOut.copyTo(outArray);
117         for (int i = 0; i < outArray.length; i += 4) {
118             int offset = i;
119             Float4 inValues = new Float4( inArray[offset],inArray[offset + 1],inArray[offset + 2],inArray[offset + 3]);
120             byte[] cValue = rs_PackColorTo8888(inValues);
121             refArray[i] = cValue[0];
122             refArray[i + 1] = cValue[1];
123             refArray[i + 2] = cValue[2];
124             refArray[i + 3] = cValue[3];
125             for (int j = 0; j < 4; j++){
126                 assertEquals(refArray[i+j] & 0xff, outArray[i+j] & 0xff);
127             }
128         }
129 
130         mAllocationIn.destroy();
131         mAllocationOut.destroy();
132     }
133 
rs_PackColorTo8888(Float3 color)134     private byte[] rs_PackColorTo8888(Float3 color) {
135         color.x *= 255.f;
136         color.y *= 255.f;
137         color.z *= 255.f;
138         color.x += 0.5f;
139         color.y += 0.5f;
140         color.z += 0.5f;
141         byte[] c = {
142             (byte) color.x, (byte) color.y, (byte) color.z, (byte) 255
143         };
144         return c;
145     }
146 
rs_PackColorTo8888(Float4 color)147     private byte[] rs_PackColorTo8888(Float4 color) {
148         color.x *= 255.f;
149         color.y *= 255.f;
150         color.z *= 255.f;
151         color.x += 0.5f;
152         color.y += 0.5f;
153         color.z += 0.5f;
154         color.w *= 255.f;
155         color.w += 0.5f;
156         byte[] c = {
157             (byte) color.x, (byte) color.y, (byte) color.z, (byte) color.w
158         };
159         return c;
160     }
161 }
162