• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.*;
20 import java.util.Random;
21 
22 public class YuvTest extends RSBaseCompute {
23     int width;
24     int height;
25     byte [] by;
26     byte [] bu;
27     byte [] bv;
28     Allocation ay;
29     Allocation au;
30     Allocation av;
31 
32     protected ScriptC_verify mVerify;
33 
34 
getCWidth()35     int getCWidth() {
36         return (width + 1) / 2;
37     }
getCHeight()38     int getCHeight() {
39         return (height + 1) / 2;
40     }
41 
makeYuvBuffer(int w, int h)42     protected void makeYuvBuffer(int w, int h) {
43         Random r = new Random();
44         width = w;
45         height = h;
46 
47         by = new byte[w*h];
48         bu = new byte[getCWidth() * getCHeight()];
49         bv = new byte[getCWidth() * getCHeight()];
50 
51         for (int i=0; i < by.length; i++) {
52             by[i] = (byte)r.nextInt(256);
53         }
54         for (int i=0; i < bu.length; i++) {
55             bu[i] = (byte)r.nextInt(256);
56         }
57         for (int i=0; i < bv.length; i++) {
58             bv[i] = (byte)r.nextInt(256);
59         }
60 
61         ay = Allocation.createTyped(mRS, Type.createXY(mRS, Element.U8(mRS), w, h));
62         final Type tuv = Type.createXY(mRS, Element.U8(mRS), w >> 1, h >> 1);
63         au = Allocation.createTyped(mRS, tuv);
64         av = Allocation.createTyped(mRS, tuv);
65 
66         ay.copyFrom(by);
67         au.copyFrom(bu);
68         av.copyFrom(bv);
69     }
70 
makeOutput()71     public Allocation makeOutput() {
72         return Allocation.createTyped(mRS, Type.createXY(mRS, Element.RGBA_8888(mRS), width, height));
73     }
74 
75     // Test for the API 17 conversion path
76     // This used a uchar buffer assuming nv21
testV17()77     public void testV17() {
78         mVerify = new ScriptC_verify(mRS);
79 
80         makeYuvBuffer(120, 96);
81         Allocation aout = makeOutput();
82         Allocation aref = makeOutput();
83 
84         byte tmp[] = new byte[(width * height) + (getCWidth() * getCHeight() * 2)];
85         int i = 0;
86         for (int j = 0; j < (width * height); j++) {
87             tmp[i++] = by[j];
88         }
89         for (int j = 0; j < (getCWidth() * getCHeight()); j++) {
90             tmp[i++] = bv[j];
91             tmp[i++] = bu[j];
92         }
93 
94         Allocation ta = Allocation.createSized(mRS, Element.U8(mRS), tmp.length);
95         ta.copyFrom(tmp);
96 
97 
98         ScriptIntrinsicYuvToRGB syuv = ScriptIntrinsicYuvToRGB.create(mRS, Element.U8(mRS));
99         syuv.setInput(ta);
100         syuv.forEach(aout);
101 
102         ScriptC_yuv script = new ScriptC_yuv(mRS);
103         script.invoke_makeRef(ay, au, av, aref);
104 
105         mVerify.invoke_verify(aref, aout, ay);
106 
107         mRS.finish();
108         mVerify.invoke_checkError();
109         waitForMessage();
110         checkForErrors();
111     }
112 
113     // Test for the API 18 conversion path with nv21
test_NV21()114     public void test_NV21() {
115         mVerify = new ScriptC_verify(mRS);
116         ScriptC_yuv script = new ScriptC_yuv(mRS);
117         ScriptIntrinsicYuvToRGB syuv = ScriptIntrinsicYuvToRGB.create(mRS, Element.YUV(mRS));
118 
119         makeYuvBuffer(512, 512);
120         Allocation aout = makeOutput();
121         Allocation aref = makeOutput();
122 
123 
124         Type.Builder tb = new Type.Builder(mRS, Element.YUV(mRS));
125         tb.setX(width);
126         tb.setY(height);
127         tb.setYuvFormat(android.graphics.ImageFormat.NV21);
128         Allocation ta = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
129 
130         byte tmp[] = new byte[(width * height) + (getCWidth() * getCHeight() * 2)];
131         int i = 0;
132         for (int j = 0; j < (width * height); j++) {
133             tmp[i++] = by[j];
134         }
135         for (int j = 0; j < (getCWidth() * getCHeight()); j++) {
136             tmp[i++] = bv[j];
137             tmp[i++] = bu[j];
138         }
139         ta.copyFrom(tmp);
140         script.invoke_makeRef(ay, au, av, aref);
141 
142         syuv.setInput(ta);
143         syuv.forEach(aout);
144         mVerify.invoke_verify(aref, aout, ay);
145 
146         script.set_mInput(ta);
147         script.forEach_cvt(aout);
148         mVerify.invoke_verify(aref, aout, ay);
149 
150         mRS.finish();
151         mVerify.invoke_checkError();
152         waitForMessage();
153         checkForErrors();
154     }
155 
156     // Test for the API 18 conversion path with yv12
test_YV12()157     public void test_YV12() {
158         mVerify = new ScriptC_verify(mRS);
159         ScriptC_yuv script = new ScriptC_yuv(mRS);
160         ScriptIntrinsicYuvToRGB syuv = ScriptIntrinsicYuvToRGB.create(mRS, Element.YUV(mRS));
161 
162         makeYuvBuffer(512, 512);
163         Allocation aout = makeOutput();
164         Allocation aref = makeOutput();
165 
166 
167         Type.Builder tb = new Type.Builder(mRS, Element.YUV(mRS));
168         tb.setX(width);
169         tb.setY(height);
170         tb.setYuvFormat(android.graphics.ImageFormat.YV12);
171         Allocation ta = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
172 
173         byte tmp[] = new byte[(width * height) + (getCWidth() * getCHeight() * 2)];
174         int i = 0;
175         for (int j = 0; j < (width * height); j++) {
176             tmp[i++] = by[j];
177         }
178         for (int j = 0; j < (getCWidth() * getCHeight()); j++) {
179             tmp[i++] = bu[j];
180         }
181         for (int j = 0; j < (getCWidth() * getCHeight()); j++) {
182             tmp[i++] = bv[j];
183         }
184         ta.copyFrom(tmp);
185         script.invoke_makeRef(ay, au, av, aref);
186 
187         syuv.setInput(ta);
188         syuv.forEach(aout);
189         mVerify.invoke_verify(aref, aout, ay);
190 
191         script.set_mInput(ta);
192         script.forEach_cvt(aout);
193         mVerify.invoke_verify(aref, aout, ay);
194 
195         mRS.finish();
196         mVerify.invoke_checkError();
197         waitForMessage();
198         checkForErrors();
199     }
200 
201 }
202